5102
|
1 %option prefix="gpt" |
|
2 %option noyywrap |
|
3 |
|
4 %{ |
5215
|
5 // PKG_ADD: mark_as_rawcommand ("__gnuplot_plot__"); |
|
6 // PKG_ADD: mark_as_rawcommand ("__gnuplot_set__"); |
|
7 // PKG_ADD: mark_as_rawcommand ("__gnuplot_splot__"); |
|
8 // PKG_ADD: mark_as_rawcommand ("__gnuplot_replot__"); |
|
9 |
5102
|
10 // PKG_ADD: mark_as_rawcommand ("gplot"); |
|
11 // PKG_ADD: mark_as_rawcommand ("gset"); |
|
12 // PKG_ADD: mark_as_rawcommand ("gsplot"); |
|
13 |
5215
|
14 // PKG_ADD: mark_as_command ("__gnuplot_show__"); |
|
15 |
5102
|
16 // PKG_ADD: mark_as_command ("gshow"); |
5215
|
17 |
5102
|
18 // PKG_ADD: mark_as_command ("set"); |
|
19 // PKG_ADD: mark_as_command ("show"); |
|
20 |
5215
|
21 // PKG_ADD: __gnuplot_init__ (); |
5102
|
22 |
|
23 // PKG_ADD: atexit ("closeplot"); |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
5489
|
29 #include <map> |
5102
|
30 #include <string> |
|
31 #include <fstream> |
|
32 #include <iostream> |
|
33 |
|
34 #ifdef HAVE_UNISTD_H |
|
35 #ifdef HAVE_SYS_TYPES_H |
|
36 #include <sys/types.h> |
|
37 #endif |
|
38 #include <unistd.h> |
|
39 #endif |
|
40 |
|
41 #include "file-ops.h" |
5489
|
42 #include "oct-env.h" |
5102
|
43 |
|
44 #include "defun-dld.h" |
|
45 #include "file-io.h" |
|
46 #include "gripes.h" |
|
47 #include "load-save.h" |
|
48 #include "parse.h" |
|
49 #include "procstream.h" |
|
50 #include "sighandlers.h" |
|
51 #include "utils.h" |
|
52 #include "variables.h" |
|
53 |
|
54 enum _toktype |
|
55 { |
|
56 START_PAREN = 1, |
|
57 END_PAREN, |
|
58 START_BRACE, |
|
59 END_BRACE, |
|
60 START_BRACKET, |
|
61 END_BRACKET, |
|
62 COLON, |
|
63 SEMICOLON, |
|
64 COMMA, |
|
65 QUOTE, |
|
66 IDENT, |
|
67 NUMBER, |
|
68 BINOP, |
|
69 UNOP, |
|
70 STRING, |
|
71 OTHER, |
|
72 TITLE, |
|
73 USING, |
|
74 WITH, |
|
75 AXES, |
|
76 CLEAR |
|
77 }; |
|
78 |
|
79 typedef bool (*pred) (const int); |
|
80 |
|
81 class |
|
82 gpt_parse_error |
|
83 { |
|
84 public: |
|
85 gpt_parse_error (void) : msg () { } |
|
86 gpt_parse_error (std::string errmsg) : msg (errmsg) { } |
|
87 |
|
88 std::string msg; |
|
89 }; |
|
90 |
|
91 static bool gpt_quote_is_transpose; |
|
92 static bool gpt_allow_plotkw; |
|
93 static int gpt_parens; |
|
94 static int gpt_brackets; |
|
95 static int gpt_braces; |
|
96 |
5489
|
97 static bool can_be_plotkw (void); |
5102
|
98 |
5489
|
99 static int is_plot_keyword (const std::string& s); |
5102
|
100 |
|
101 %} |
|
102 |
|
103 D [0-9] |
|
104 S [ \t] |
|
105 IDENT ([_a-zA-Z@][_a-zA-Z0-9]*) |
|
106 EXPON ([DdEe][+-]?{D}+) |
|
107 NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)|(0[xX][0-9a-fA-F]+)) |
|
108 NOT ((\~)|(\!)) |
|
109 /* NOT is not strictly a binary operator, but is close enough for us. */ |
|
110 BINOP (({NOT})|(\.?([\*/\\^+-]|\*\*)=?)|([<=~!>&|]=)|([=&|<>]{1,2})|(<<=)|(>>=)|(\.)) |
5106
|
111 /* single quote (') transpose operator is handled separately. */ |
|
112 UNOP ((\+\+)|(\-\-)|(\.')) |
5102
|
113 |
|
114 %% |
|
115 |
|
116 "(" { |
|
117 gpt_quote_is_transpose = false; |
|
118 gpt_parens++; |
|
119 return START_PAREN; |
|
120 } |
|
121 |
|
122 ")" { |
|
123 gpt_quote_is_transpose = true; |
|
124 gpt_parens--; |
|
125 return END_PAREN; |
|
126 } |
|
127 |
|
128 "{" { |
|
129 gpt_quote_is_transpose = false; |
|
130 gpt_braces++; |
|
131 return START_BRACE; |
|
132 } |
|
133 |
|
134 "}" { |
|
135 gpt_quote_is_transpose = true; |
|
136 gpt_braces--; |
|
137 return END_BRACE; |
|
138 } |
|
139 |
|
140 "[" { |
|
141 gpt_quote_is_transpose = false; |
|
142 gpt_brackets++; |
|
143 return START_BRACKET; |
|
144 } |
|
145 |
|
146 "]" { |
|
147 gpt_quote_is_transpose = true; |
|
148 gpt_brackets--; |
|
149 return END_BRACKET; |
|
150 } |
|
151 |
|
152 ":" { |
|
153 gpt_quote_is_transpose = false; |
|
154 return COLON; |
|
155 } |
|
156 |
|
157 ";" { |
|
158 gpt_quote_is_transpose = false; |
|
159 return SEMICOLON; |
|
160 } |
|
161 |
|
162 "," { |
|
163 gpt_quote_is_transpose = false; |
|
164 return COMMA; |
|
165 } |
|
166 |
|
167 "'" { |
|
168 if (gpt_quote_is_transpose) |
|
169 { |
|
170 gpt_allow_plotkw = true; |
|
171 return QUOTE; |
|
172 } |
|
173 else |
|
174 { |
|
175 gpt_quote_is_transpose = true; |
|
176 gpt_allow_plotkw = true; |
5489
|
177 warning ("unknown token = \"%s\" in plot command", yytext); |
|
178 return OTHER; |
5102
|
179 } |
|
180 } |
|
181 |
|
182 {IDENT} { |
5489
|
183 int itok = 0; |
5102
|
184 if (can_be_plotkw () && (itok = is_plot_keyword (yytext))) |
|
185 { |
|
186 gpt_quote_is_transpose = false; |
|
187 gpt_allow_plotkw = true; |
|
188 return itok; |
|
189 } |
|
190 else if (std::string (yytext) == "function") |
5489
|
191 throw gpt_parse_error ("function keyword not allowed in plot commands"); |
5102
|
192 else |
|
193 { |
|
194 gpt_quote_is_transpose = true; |
|
195 gpt_allow_plotkw = true; |
|
196 return IDENT; |
|
197 } |
|
198 } |
|
199 |
|
200 {D}+/\.[\*/\\^'] | /* ' */ |
|
201 {NUMBER} { |
|
202 gpt_quote_is_transpose = true; |
|
203 gpt_allow_plotkw = true; |
|
204 return NUMBER; |
|
205 } |
|
206 |
|
207 {BINOP} { |
|
208 gpt_quote_is_transpose = false; |
|
209 gpt_allow_plotkw = false; |
|
210 return BINOP; |
|
211 } |
|
212 |
|
213 {UNOP} { |
|
214 gpt_quote_is_transpose = false; |
|
215 gpt_allow_plotkw = true; |
|
216 return UNOP; |
|
217 } |
|
218 |
|
219 {S} { /* Ignore spaces and tabs outside of character strings. */ } |
|
220 |
|
221 . { |
|
222 gpt_quote_is_transpose = false; |
|
223 gpt_allow_plotkw = true; |
|
224 warning ("unknown token = \"%s\" in plot command", yytext); |
|
225 return OTHER; |
|
226 } |
|
227 |
|
228 %% |
|
229 |
5489
|
230 // ------------------------------------------------------------ |
|
231 // Interface to external gnuplot process(es), including gnuplot |
|
232 // command parser. |
|
233 // ------------------------------------------------------------ |
5102
|
234 |
|
235 // The name of the shell command to execute to start gnuplot. |
|
236 static std::string Vgnuplot_binary; |
|
237 |
5489
|
238 // Append -title "Figure NN" to the gnuplot command? |
|
239 static bool Vgnuplot_use_title_option; |
5102
|
240 |
|
241 // Gnuplot command strings that we use. |
|
242 static std::string Vgnuplot_command_plot; |
|
243 static std::string Vgnuplot_command_replot; |
|
244 static std::string Vgnuplot_command_splot; |
|
245 static std::string Vgnuplot_command_using; |
|
246 static std::string Vgnuplot_command_with; |
|
247 static std::string Vgnuplot_command_axes; |
|
248 static std::string Vgnuplot_command_title; |
|
249 static std::string Vgnuplot_command_end; |
|
250 |
5489
|
251 // If TRUE, a replot command is issued automatically each time a plot |
|
252 // changes in some way. |
|
253 static bool Vautomatic_replot; |
5102
|
254 |
5489
|
255 // Check if the parser state is such that a plot keyword can occur. |
5102
|
256 |
5142
|
257 static bool |
5102
|
258 can_be_plotkw (void) |
|
259 { |
|
260 return (gpt_allow_plotkw |
|
261 && (gpt_braces == 0) |
|
262 && (gpt_brackets == 0) |
|
263 && (gpt_parens == 0)); |
|
264 } |
|
265 |
5489
|
266 // Check to see if a character string matches any one of the plot |
|
267 // option keywords. Don't match abbreviations for clear, since that's |
|
268 // not a gnuplot keyword (users will probably only expect to be able |
|
269 // to abbreviate actual gnuplot keywords). |
|
270 |
|
271 static int |
|
272 is_plot_keyword (const std::string& s) |
|
273 { |
|
274 if (almost_match ("title", s, 1)) |
|
275 return TITLE; |
|
276 else if (almost_match ("using", s, 1)) |
|
277 return USING; |
|
278 else if (almost_match ("with", s, 1)) |
|
279 return WITH; |
|
280 else if (almost_match ("axes", s, 2) || almost_match ("axis", s, 2)) |
|
281 return AXES; |
|
282 else if ("clear" == s) |
|
283 return CLEAR; |
|
284 else |
|
285 return 0; |
|
286 } |
|
287 |
|
288 // This is used to handle single-quote delimited strings. Kludge alert. |
|
289 |
|
290 static int |
|
291 handle_string (char delim) |
|
292 { |
|
293 static char *buf = 0; |
|
294 |
|
295 int c; |
|
296 bool escape_pending = false; |
|
297 |
|
298 std::string strbuf (1, static_cast<char> (delim)); |
|
299 |
|
300 while ((c = yyinput ()) != EOF) |
|
301 { |
|
302 if (c == '\\') |
|
303 { |
|
304 if (escape_pending) |
|
305 { |
|
306 strbuf += static_cast<char> (c); |
|
307 escape_pending = false; |
|
308 } |
|
309 else |
|
310 { |
|
311 strbuf += static_cast<char> (c); |
|
312 escape_pending = true; |
|
313 } |
|
314 continue; |
|
315 } |
|
316 else if (c == '\n') |
|
317 { |
|
318 error ("unterminated string constant"); |
|
319 break; |
|
320 } |
|
321 else if (c == delim) |
|
322 { |
|
323 if (escape_pending) |
|
324 strbuf += static_cast<char> (c); |
|
325 else |
|
326 { |
|
327 c = yyinput (); |
|
328 |
|
329 if (c == delim) |
|
330 { |
|
331 strbuf += static_cast<char> (c); |
|
332 strbuf += static_cast<char> (c); |
|
333 } |
|
334 else |
|
335 { |
|
336 yyunput (c, yytext); |
|
337 strbuf += static_cast<char> (delim); |
|
338 delete [] buf; |
|
339 yytext = strsave (strbuf.c_str ()); |
|
340 return STRING; |
|
341 } |
|
342 } |
|
343 } |
|
344 else |
|
345 strbuf += static_cast<char> (c); |
|
346 |
|
347 escape_pending = false; |
|
348 } |
|
349 |
|
350 throw gpt_parse_error ("unterminated string"); |
|
351 |
|
352 return 0; |
|
353 } |
|
354 |
5102
|
355 // (Probably not necessesary, but current Matlab style plot functions |
|
356 // break without this (they emit too short gnuplot commands)) |
5489
|
357 |
5102
|
358 static std::string |
|
359 plot_style_token (const std::string& s) |
|
360 { |
|
361 std::string retval; |
|
362 |
|
363 // XXX FIXME XXX -- specify minimum match length for these. |
|
364 static const char *plot_styles[] = |
|
365 { |
|
366 "boxes", |
|
367 "boxerrorbars", |
|
368 "boxxyerrorbars", |
|
369 "candlesticks", |
|
370 "dots", |
|
371 "errorbars", |
|
372 "financebars", |
|
373 "fsteps", |
|
374 "histeps", |
|
375 "impulses", |
|
376 "lines", |
|
377 "linespoints", |
|
378 "points", |
|
379 "steps", |
|
380 "vector", |
|
381 "xerrorbars", |
|
382 "xyerrorbars", |
|
383 "yerrorbars", |
|
384 0, |
|
385 }; |
|
386 |
|
387 const char * const *tmp = plot_styles; |
|
388 while (*tmp) |
|
389 { |
|
390 if (almost_match (*tmp, s.c_str ())) |
|
391 { |
|
392 retval = *tmp; |
|
393 break; |
|
394 } |
|
395 |
|
396 tmp++; |
|
397 } |
|
398 |
|
399 return retval; |
|
400 } |
|
401 |
|
402 // Some predicates on tokens |
|
403 |
|
404 // Return true for ":". |
5489
|
405 |
|
406 static bool |
5102
|
407 colonp (const int tok) |
|
408 { |
|
409 return (tok == COLON); |
|
410 } |
|
411 |
|
412 // Return TRUE for "]". |
5489
|
413 |
|
414 static bool |
5102
|
415 endbracketp (const int tok) |
|
416 { |
|
417 return (tok == END_BRACKET); |
|
418 } |
|
419 |
|
420 // Return TRUE for plot token, comma or end of input. |
5489
|
421 |
|
422 static bool |
5102
|
423 plottok_or_end_p (const int tok) |
|
424 { |
|
425 return (tok == TITLE |
|
426 || tok == USING |
|
427 || tok == WITH |
|
428 || tok == AXES |
|
429 || tok == CLEAR |
|
430 || tok == COMMA |
|
431 || tok == 0); |
|
432 } |
|
433 |
|
434 // Equivalent to (colonp (tok) || plottok_or_end_p (tok)). |
5489
|
435 |
|
436 static bool |
5102
|
437 colon_plottok_or_end_p (const int tok) |
|
438 { |
|
439 return (tok == COLON || plottok_or_end_p (tok)); |
|
440 } |
|
441 |
|
442 // read until test is true and delimiters are balanced, or end of input. |
|
443 // Return the last token in lasttok |
5489
|
444 |
5102
|
445 static std::string |
|
446 read_until (pred test, int& lasttok) throw (gpt_parse_error) |
|
447 { |
|
448 int tok; |
|
449 |
|
450 // We have to maintain balanced delimiters per subexpression too. |
|
451 int brackets = 0; |
|
452 int parens = 0; |
|
453 int braces = 0; |
|
454 std::string s; |
|
455 |
|
456 tok = gptlex (); |
|
457 |
|
458 while (tok && ! (test (tok) |
|
459 && brackets == 0 |
|
460 && parens == 0 |
|
461 && braces == 0)) |
|
462 { |
|
463 switch (tok) |
|
464 { |
|
465 case START_BRACKET: |
|
466 brackets++; |
|
467 break; |
|
468 |
|
469 case END_BRACKET: |
|
470 brackets--; |
|
471 break; |
|
472 |
|
473 case START_PAREN: |
|
474 parens++; |
|
475 break; |
|
476 |
|
477 case END_PAREN: |
|
478 parens--; |
|
479 break; |
|
480 |
|
481 case START_BRACE: |
|
482 braces++; |
|
483 break; |
|
484 |
|
485 case END_BRACE: |
|
486 braces--; |
|
487 break; |
|
488 |
|
489 default: |
|
490 break; |
|
491 } |
|
492 |
|
493 s += std::string (yytext) + " "; |
|
494 |
|
495 tok = gptlex (); |
|
496 } |
|
497 |
|
498 // Throw error only if we've reached the end token and the test |
|
499 // doesn't accept it. |
|
500 |
|
501 if (! test (tok) && ! tok) |
|
502 throw gpt_parse_error ("unexpected end of input"); |
|
503 |
|
504 lasttok = tok; |
|
505 |
|
506 return s; |
|
507 } |
|
508 |
|
509 // Eval the two expressions giving limits of range and print it. |
5489
|
510 |
5102
|
511 static std::string |
|
512 printrange (std::string starts, std::string ends) |
|
513 { |
|
514 octave_value startv, endv; |
|
515 int status; |
|
516 std::string s; |
|
517 OSSTREAM range_buf; |
|
518 |
|
519 range_buf << "["; |
|
520 |
|
521 if (! starts.empty ()) |
|
522 { |
|
523 startv = eval_string (starts, true, status); |
|
524 if (! startv.is_real_scalar ()) |
|
525 throw gpt_parse_error (); |
|
526 startv.print_raw (range_buf); |
|
527 } |
|
528 |
|
529 range_buf << ":"; |
|
530 |
|
531 if (! ends.empty ()) |
|
532 { |
|
533 endv = eval_string (ends, true, status); |
|
534 if (! endv.is_real_scalar ()) |
|
535 throw gpt_parse_error (); |
|
536 endv.print_raw (range_buf); |
|
537 } |
|
538 |
|
539 range_buf << "]"; |
|
540 range_buf << OSSTREAM_ENDS; |
|
541 |
|
542 s = OSSTREAM_STR (range_buf); |
|
543 |
|
544 return s; |
|
545 } |
|
546 |
|
547 // Handle plot parameters. |
|
548 |
|
549 // Parse, evaluate and print colon separated expressions in the using |
|
550 // plot parameter. The use of trailing format string is not supported. |
5489
|
551 |
5102
|
552 static std::string |
|
553 handle_using (int& lasttok) |
|
554 { |
|
555 int tok; |
|
556 std::string expr_str; |
|
557 std::string retstr = Vgnuplot_command_using + " "; |
|
558 bool out = false; |
|
559 |
|
560 octave_value tmp_data; |
|
561 int status; |
|
562 while (! out) |
|
563 { |
|
564 expr_str = read_until (colon_plottok_or_end_p, tok); |
|
565 |
|
566 tmp_data = eval_string (expr_str, true, status); |
|
567 if (status != 0 || ! tmp_data.is_real_scalar ()) |
|
568 throw gpt_parse_error (); |
|
569 |
|
570 OSSTREAM tmp_buf; |
|
571 tmp_data.print_raw (tmp_buf); |
|
572 tmp_buf << OSSTREAM_ENDS; |
|
573 retstr += OSSTREAM_STR (tmp_buf); |
|
574 |
|
575 if (tok == COLON) |
|
576 retstr += ":"; |
|
577 else |
|
578 out = true; |
|
579 } |
|
580 |
|
581 lasttok = tok; |
|
582 |
|
583 return retstr; |
|
584 } |
|
585 |
|
586 // Presently just passes the linewidth, pointtype etc. tokens as they are. |
5489
|
587 |
5102
|
588 static std::string |
|
589 handle_style (int& lasttok) |
|
590 { |
|
591 std::string retstr = Vgnuplot_command_with + " "; |
|
592 std::string style; |
|
593 |
|
594 lasttok = gptlex (); |
|
595 |
|
596 if (lasttok != IDENT) |
|
597 throw gpt_parse_error ("expected plot style token"); |
|
598 |
|
599 style = std::string (yytext); |
|
600 style = plot_style_token (style); |
|
601 |
|
602 if (! style.empty ()) |
|
603 retstr += style; |
|
604 else |
|
605 retstr += std::string (yytext); |
|
606 |
|
607 // XXX FIXME XXX -- should evaluate the remaining tokens, but this |
|
608 // needs changes in the parser. |
|
609 retstr += " " + read_until (plottok_or_end_p, lasttok); |
|
610 |
|
611 return retstr; |
|
612 } |
|
613 |
|
614 // Axes has only one qualifier keyword, which is not evaluated. |
5489
|
615 |
5102
|
616 static std::string |
|
617 handle_axes (int& lasttok) |
|
618 { |
|
619 return Vgnuplot_command_axes + " " + read_until (plottok_or_end_p, lasttok); |
|
620 } |
|
621 |
5489
|
622 static std::string |
|
623 save_in_tmp_file (const octave_value& t, int ndim = 2, bool parametric = false) |
|
624 { |
|
625 std::string name = file_ops::tempnam ("", "oct-"); |
|
626 |
|
627 if (! name.empty ()) |
|
628 { |
|
629 std::ofstream file (name.c_str ()); |
|
630 |
|
631 if (file) |
|
632 { |
|
633 switch (ndim) |
|
634 { |
|
635 case 2: |
|
636 save_ascii_data_for_plotting (file, t, name); |
|
637 break; |
|
638 |
|
639 case 3: |
|
640 save_three_d (file, t, parametric); |
|
641 break; |
|
642 |
|
643 default: |
|
644 gripe_2_or_3_dim_plot (); |
|
645 break; |
|
646 } |
|
647 } |
|
648 else |
|
649 { |
|
650 error ("couldn't open temporary output file `%s'", name.c_str ()); |
|
651 name.resize (0); |
|
652 } |
|
653 } |
|
654 |
|
655 return name; |
|
656 } |
|
657 |
5102
|
658 static int |
5489
|
659 get_current_figure (void) |
|
660 { |
|
661 int retval = 1; |
|
662 |
|
663 octave_value cf = get_global_value ("__current_figure__", true); |
|
664 |
|
665 if (cf.is_defined ()) |
|
666 retval = cf.int_value (); |
|
667 else |
|
668 set_global_value ("__current_figure__", retval); |
|
669 |
|
670 return retval; |
|
671 } |
|
672 |
|
673 class |
|
674 gnuplot |
|
675 { |
|
676 protected: |
|
677 |
|
678 gnuplot (void) |
|
679 : plot_line_count (0), parametric_plot (false), |
|
680 use_title_option (Vgnuplot_use_title_option), |
|
681 gnuplot_exe (Vgnuplot_binary), |
|
682 gnuplot_terminal_type (), plot_stream () |
|
683 { } |
|
684 |
|
685 public: |
|
686 |
|
687 ~gnuplot (void) { } |
|
688 |
|
689 static bool have_instance (void); |
|
690 |
|
691 static bool ensure_instance (void); |
|
692 |
|
693 static bool ensure_plot_stream (void); |
|
694 |
|
695 static void open (void); |
|
696 |
|
697 static void close (void); |
|
698 |
|
699 static bool plot_stream_event_handler (pid_t pid, int status); |
|
700 |
|
701 static void init (void) |
|
702 { |
|
703 if (ensure_instance ()) |
|
704 instance->do_init (); |
|
705 } |
|
706 |
|
707 static void send (const std::string& cmd) |
|
708 { |
|
709 if (ensure_plot_stream ()) |
|
710 instance->do_send (cmd); |
|
711 } |
|
712 |
|
713 static void send_raw (const std::string& cmd) |
|
714 { |
|
715 if (ensure_plot_stream ()) |
|
716 instance->do_send_raw (cmd); |
|
717 } |
|
718 |
|
719 static void clear (void) |
|
720 { |
|
721 if (ensure_plot_stream ()) |
|
722 instance->do_clear (); |
|
723 } |
|
724 |
|
725 static void set (const string_vector& argv) |
|
726 { |
|
727 if (ensure_plot_stream ()) |
|
728 instance->do_set (argv); |
|
729 } |
|
730 |
|
731 static void show (const string_vector& argv) |
|
732 { |
|
733 if (ensure_plot_stream ()) |
|
734 instance->do_show (argv); |
|
735 } |
|
736 |
|
737 // XXX FIXME XXX -- should only remove tmp files associated with |
|
738 // gnuplot? |
|
739 static void cleanup_tmp_files (void) { ::cleanup_tmp_files (); } |
|
740 |
|
741 static void plot (const string_vector& argv) |
|
742 { |
|
743 if (ensure_plot_stream ()) |
|
744 instance->do_plot (argv); |
|
745 } |
|
746 |
|
747 private: |
|
748 |
|
749 static gnuplot *instance; |
|
750 |
|
751 static std::map<int, gnuplot *> instance_map; |
|
752 |
|
753 // The number of lines we've plotted so far. |
|
754 int plot_line_count; |
|
755 |
|
756 // Is this a parametric plot? Makes a difference for 3D plotting. |
|
757 bool parametric_plot; |
|
758 |
|
759 // Should we append '-title "TITLE"' to the gnuplot command? |
|
760 bool use_title_option; |
|
761 |
|
762 // The executable program to run. |
|
763 std::string gnuplot_exe; |
|
764 |
|
765 // The gnuplot terminal type. |
|
766 std::string gnuplot_terminal_type; |
|
767 |
|
768 // Pipe to gnuplot. |
|
769 oprocstream *plot_stream; |
|
770 |
|
771 pid_t pid (void) const; |
|
772 |
|
773 static gnuplot *lookup_by_pid (pid_t pid); |
|
774 |
|
775 void do_open (void); |
|
776 |
|
777 void do_close (void); |
|
778 |
|
779 void delete_plot_stream (void); |
|
780 |
|
781 void reset_plot_stream (void); |
|
782 |
|
783 void do_send (const std::string& cmd); |
|
784 |
|
785 void do_send_raw (const std::string& cmd); |
|
786 |
|
787 void do_clear (void); |
|
788 |
|
789 void do_set (const string_vector& argv); |
|
790 |
|
791 void do_show (const string_vector& argv); |
|
792 |
|
793 void do_plot (const string_vector& argv); |
|
794 |
|
795 void do_init (void); |
|
796 |
|
797 std::string |
|
798 makeplot (std::string caller, std::string args) throw (gpt_parse_error); |
|
799 |
|
800 std::string gnuplot::handle_title (int& lasttok); |
|
801 }; |
|
802 |
|
803 gnuplot *gnuplot::instance = 0; |
|
804 |
|
805 std::map<int, gnuplot *> gnuplot::instance_map; |
|
806 |
|
807 bool |
|
808 gnuplot::have_instance (void) |
|
809 { |
|
810 int current_figure = get_current_figure (); |
|
811 |
|
812 if (instance_map.find (current_figure) != instance_map.end ()) |
|
813 { |
|
814 instance = instance_map[current_figure]; |
|
815 return true; |
|
816 } |
|
817 else |
|
818 return false; |
|
819 } |
|
820 |
|
821 bool |
|
822 gnuplot::ensure_instance (void) |
|
823 { |
|
824 if (! have_instance ()) |
|
825 { |
|
826 instance = new gnuplot (); |
|
827 |
|
828 if (! instance) |
|
829 { |
|
830 ::error ("unable to create gnuplot object!"); |
|
831 |
|
832 return false; |
|
833 } |
|
834 else |
|
835 instance_map[get_current_figure ()] = instance; |
|
836 } |
|
837 |
|
838 return true; |
|
839 } |
|
840 |
|
841 bool |
|
842 gnuplot::ensure_plot_stream (void) |
|
843 { |
|
844 if (ensure_instance ()) |
|
845 { |
|
846 instance->do_open (); |
|
847 |
|
848 if (error_state) |
|
849 return false; |
|
850 } |
|
851 |
|
852 return true; |
|
853 } |
|
854 |
|
855 void |
|
856 gnuplot::close (void) |
|
857 { |
|
858 if (have_instance ()) |
|
859 { |
|
860 instance->do_close (); |
|
861 |
|
862 instance_map.erase (get_current_figure ()); |
|
863 } |
|
864 } |
|
865 |
|
866 pid_t |
|
867 gnuplot::pid (void) const |
|
868 { |
|
869 return plot_stream ? plot_stream->pid () : -1; |
|
870 } |
|
871 |
|
872 gnuplot * |
|
873 gnuplot::lookup_by_pid (pid_t pid) |
|
874 { |
|
875 gnuplot *retval = 0; |
|
876 |
|
877 for (std::map<int, gnuplot *>::const_iterator p = instance_map.begin (); |
|
878 p != instance_map.end (); |
|
879 p++) |
|
880 { |
|
881 gnuplot *elt = p->second; |
|
882 |
|
883 if (elt && elt->pid () == pid) |
|
884 { |
|
885 retval = elt; |
|
886 break; |
|
887 } |
|
888 } |
|
889 |
|
890 return retval; |
|
891 } |
|
892 |
|
893 void |
|
894 gnuplot::do_open (void) |
5102
|
895 { |
5489
|
896 static bool initialized = false; |
|
897 |
|
898 if (plot_stream && ! *plot_stream) |
|
899 do_close (); |
|
900 |
|
901 if (! plot_stream) |
|
902 { |
|
903 initialized = false; |
|
904 |
|
905 plot_line_count = 0; |
|
906 |
|
907 std::string cmd; |
|
908 |
|
909 if (gnuplot_exe.empty ()) |
|
910 cmd = "gnuplot"; |
|
911 else |
|
912 cmd = "\"" + gnuplot_exe + "\""; |
|
913 |
|
914 // XXX FIXME XXX -- I'm not sure this is the right thing to do, |
|
915 // but without it, C-c at the octave prompt will kill gnuplot... |
|
916 |
|
917 #if defined (HAVE_POSIX_SIGNALS) |
|
918 sigset_t nset, oset; |
|
919 sigemptyset (&nset); |
|
920 sigaddset (&nset, SIGINT); |
|
921 sigprocmask (SIG_BLOCK, &nset, &oset); |
|
922 #else |
|
923 volatile octave_interrupt_handler old_interrupt_handler |
|
924 = octave_ignore_interrupts (); |
|
925 #endif |
|
926 |
|
927 if (use_title_option) |
|
928 { |
|
929 OSSTREAM buf; |
|
930 |
|
931 buf << cmd |
|
932 << " -title \"Figure " << get_current_figure () << "\"" |
|
933 << OSSTREAM_ENDS; |
|
934 |
|
935 cmd = OSSTREAM_STR (buf); |
|
936 |
|
937 OSSTREAM_FREEZE (buf); |
|
938 } |
|
939 |
|
940 plot_stream = new oprocstream (cmd.c_str ()); |
|
941 |
|
942 if (plot_stream && *plot_stream) |
|
943 octave_child_list::insert (plot_stream->pid (), |
|
944 plot_stream_event_handler); |
|
945 else |
|
946 { |
|
947 delete_plot_stream (); |
|
948 |
|
949 error ("plot: unable to open pipe to `%s'", cmd.c_str ()); |
|
950 } |
|
951 |
|
952 #if defined (HAVE_POSIX_SIGNALS) |
|
953 sigprocmask (SIG_SETMASK, &oset, 0); |
|
954 #else |
|
955 octave_set_interrupt_handler (old_interrupt_handler); |
|
956 #endif |
|
957 } |
|
958 |
|
959 if (! error_state && plot_stream && *plot_stream && ! initialized) |
|
960 { |
|
961 initialized = true; |
|
962 |
|
963 do_send_raw ("set style data lines\n"); |
|
964 |
|
965 if (! gnuplot_terminal_type.empty ()) |
|
966 do_send_raw ("set term " + gnuplot_terminal_type |
|
967 + Vgnuplot_command_end + "\n"); |
|
968 } |
|
969 } |
|
970 |
|
971 void |
|
972 gnuplot::delete_plot_stream (void) |
|
973 { |
|
974 do_send_raw ("\nquit\n"); |
|
975 |
|
976 delete plot_stream; |
|
977 plot_stream = 0; |
|
978 } |
|
979 |
|
980 void |
|
981 gnuplot::reset_plot_stream (void) |
|
982 { |
|
983 delete_plot_stream (); |
|
984 |
|
985 plot_line_count = 0; |
|
986 parametric_plot = false; |
|
987 } |
|
988 |
|
989 void |
|
990 gnuplot::do_close (void) |
|
991 { |
|
992 if (plot_stream) |
|
993 { |
|
994 octave_child_list::remove (plot_stream->pid ()); |
|
995 |
|
996 delete_plot_stream (); |
|
997 } |
|
998 |
|
999 plot_line_count = 0; |
|
1000 } |
|
1001 |
|
1002 bool |
|
1003 gnuplot::plot_stream_event_handler (pid_t pid, int status) |
|
1004 { |
|
1005 bool retval = false; |
|
1006 |
|
1007 if (pid > 0) |
|
1008 { |
|
1009 if (WIFEXITED (status) || WIFSIGNALLED (status)) |
|
1010 { |
|
1011 gnuplot *plotter = gnuplot::lookup_by_pid (pid); |
|
1012 |
|
1013 // We should only print a warning or request removal of the |
|
1014 // process from the child list if the process died |
|
1015 // unexpectedly. If do_close is responsible for |
|
1016 // gnuplot's death, then plotter will be 0 here and we don't |
|
1017 // need to do anything. |
|
1018 |
|
1019 if (plotter) |
|
1020 { |
|
1021 plotter->reset_plot_stream (); |
|
1022 |
|
1023 warning ("connection to external plotter (pid = %d) lost --", pid); |
|
1024 // Request removal of this PID from the list of child |
|
1025 // processes. |
|
1026 |
|
1027 retval = true; |
|
1028 } |
|
1029 } |
|
1030 } |
|
1031 |
|
1032 return retval; |
|
1033 } |
|
1034 |
|
1035 void |
|
1036 gnuplot::do_send (const std::string& cmd) |
|
1037 { |
|
1038 int replot_len = Vgnuplot_command_replot.length (); |
|
1039 |
|
1040 bool is_replot = (Vgnuplot_command_replot == cmd.substr (0, replot_len)); |
|
1041 |
|
1042 if (! (plot_line_count == 0 && is_replot)) |
|
1043 do_send_raw (cmd); |
|
1044 } |
|
1045 |
|
1046 void |
|
1047 gnuplot::do_send_raw (const std::string& cmd) |
|
1048 { |
|
1049 if (plot_stream && *plot_stream) |
|
1050 { |
|
1051 *plot_stream << cmd; |
|
1052 |
|
1053 plot_stream->flush (); |
|
1054 } |
|
1055 } |
|
1056 |
|
1057 void |
|
1058 gnuplot::do_clear (void) |
|
1059 { |
|
1060 do_send_raw ("clear\n"); |
|
1061 |
|
1062 // XXX FIXME XXX -- instead of just clearing these things, it would |
|
1063 // be nice if we could reset things to a user-specified default |
|
1064 // state. |
|
1065 |
|
1066 do_send_raw ("set title\n"); |
|
1067 do_send_raw ("set xlabel\n"); |
|
1068 do_send_raw ("set ylabel\n"); |
|
1069 do_send_raw ("set nogrid\n"); |
|
1070 do_send_raw ("set nolabel\n"); |
|
1071 |
|
1072 // This makes a simple `replot' not work after a `clearplot' command |
|
1073 // has been issued. |
|
1074 |
|
1075 plot_line_count = 0; |
|
1076 } |
|
1077 |
|
1078 void |
|
1079 gnuplot::do_set (const string_vector& argv) |
|
1080 { |
|
1081 int argc = argv.length (); |
|
1082 |
|
1083 OSSTREAM plot_buf; |
|
1084 |
|
1085 if (argc > 1) |
|
1086 { |
|
1087 if (almost_match ("parametric", argv[1], 3)) |
|
1088 parametric_plot = true; |
|
1089 else if (almost_match ("noparametric", argv[1], 5)) |
|
1090 parametric_plot = false; |
|
1091 else if (almost_match ("term", argv[1], 1)) |
|
1092 { |
|
1093 gnuplot_terminal_type = ""; |
|
1094 OSSTREAM buf; |
|
1095 int i; |
|
1096 for (i = 2; i < argc-1; i++) |
|
1097 buf << argv[i] << " "; |
|
1098 if (i < argc) |
|
1099 buf << argv[i]; |
|
1100 buf << Vgnuplot_command_end << OSSTREAM_ENDS; |
|
1101 gnuplot_terminal_type = OSSTREAM_STR (buf); |
|
1102 OSSTREAM_FREEZE (buf); |
|
1103 } |
|
1104 } |
|
1105 |
|
1106 int i; |
|
1107 for (i = 0; i < argc-1; i++) |
|
1108 plot_buf << argv[i] << " "; |
|
1109 |
|
1110 if (i < argc) |
|
1111 plot_buf << argv[i]; |
|
1112 |
|
1113 plot_buf << Vgnuplot_command_end << OSSTREAM_ENDS; |
|
1114 |
|
1115 do_send_raw (OSSTREAM_STR (plot_buf)); |
|
1116 |
|
1117 OSSTREAM_FREEZE (plot_buf); |
|
1118 } |
|
1119 |
|
1120 void |
|
1121 gnuplot::do_show (const string_vector& argv) |
|
1122 { |
|
1123 int argc = argv.length (); |
|
1124 |
|
1125 OSSTREAM plot_buf; |
|
1126 |
|
1127 int i; |
|
1128 for (i = 0; i < argc-1; i++) |
|
1129 plot_buf << argv[i] << " "; |
|
1130 if (i < argc) |
|
1131 plot_buf << argv[i]; |
|
1132 |
|
1133 plot_buf << Vgnuplot_command_end << OSSTREAM_ENDS; |
|
1134 |
|
1135 do_send (OSSTREAM_STR (plot_buf)); |
|
1136 |
|
1137 OSSTREAM_FREEZE (plot_buf); |
|
1138 } |
|
1139 |
|
1140 void |
|
1141 gnuplot::do_plot (const string_vector& argv) |
|
1142 { |
|
1143 std::string s; |
|
1144 |
|
1145 for (int i = 1; i < argv.length (); i++) |
|
1146 s += argv[i] + " "; |
|
1147 |
|
1148 try |
|
1149 { |
|
1150 std::string cmd = makeplot (argv[0], s); |
|
1151 |
|
1152 do_send (cmd); |
|
1153 } |
|
1154 catch (gpt_parse_error& e) |
|
1155 { |
|
1156 if (e.msg.empty ()) |
|
1157 error ("could not parse plot command"); |
|
1158 else |
|
1159 error (e.msg.c_str ()); |
|
1160 } |
|
1161 } |
|
1162 |
|
1163 // Parse and evaluate parameter string and pass it to gnuplot pipe. |
|
1164 |
|
1165 std::string |
|
1166 gnuplot::makeplot (std::string caller, std::string args) |
|
1167 throw (gpt_parse_error) |
|
1168 { |
|
1169 std::string retval; |
|
1170 |
5102
|
1171 YY_BUFFER_STATE bstate; |
|
1172 |
|
1173 bstate = yy_scan_string (args.c_str ()); |
|
1174 yy_switch_to_buffer (bstate); |
|
1175 std::string outstr; |
|
1176 int ndim = 2; |
|
1177 |
|
1178 if (caller == "replot") |
|
1179 { |
|
1180 ndim = 1; |
|
1181 outstr += Vgnuplot_command_replot + " "; |
|
1182 } |
|
1183 else if (caller == "plot") |
|
1184 { |
|
1185 ndim = 2; |
5406
|
1186 plot_line_count = 0; |
|
1187 outstr += Vgnuplot_command_plot + " "; |
5102
|
1188 } |
|
1189 else if (caller == "splot") |
|
1190 { |
|
1191 ndim = 3; |
5406
|
1192 plot_line_count = 0; |
|
1193 outstr += Vgnuplot_command_splot + " "; |
5102
|
1194 } |
|
1195 else |
|
1196 throw gpt_parse_error ("unknown plot command"); |
|
1197 |
|
1198 gpt_quote_is_transpose = false; |
|
1199 gpt_allow_plotkw = false; |
|
1200 gpt_parens = 0; |
|
1201 gpt_braces = 0; |
|
1202 gpt_brackets = 0; |
|
1203 |
|
1204 int tok; |
|
1205 tok = gptlex (); |
|
1206 if (plottok_or_end_p (tok) && caller != "replot") |
|
1207 throw gpt_parse_error ("must have something to plot"); |
|
1208 |
|
1209 while (tok) |
|
1210 { |
|
1211 bool title_set = false; |
|
1212 bool using_set = false; |
|
1213 bool style_set = false; |
|
1214 bool axes_set = false; |
|
1215 |
|
1216 if (tok == START_BRACKET) |
|
1217 { |
|
1218 if (caller == "replot") |
|
1219 throw gpt_parse_error ("can't specify new plot ranges with `replot' or while hold is on"); |
|
1220 |
|
1221 std::string xrange_start_str = read_until (colonp, tok); |
|
1222 std::string xrange_end_str = read_until (endbracketp, tok); |
|
1223 outstr += printrange (xrange_start_str, xrange_end_str) + " "; |
|
1224 tok = gptlex (); |
|
1225 if (tok == START_BRACKET) |
|
1226 { |
|
1227 std::string yrange_start_str = read_until (colonp, tok); |
|
1228 std::string yrange_end_str = read_until (endbracketp, tok); |
|
1229 outstr += printrange (yrange_start_str, yrange_end_str) + " "; |
|
1230 tok = gptlex (); |
|
1231 if (tok == START_BRACKET && caller == "gsplot") |
|
1232 { |
|
1233 std::string zrange_start_str = read_until (colonp, tok); |
|
1234 std::string zrange_end_str = read_until (endbracketp, tok); |
|
1235 outstr += printrange (zrange_start_str, zrange_end_str) + " "; |
|
1236 tok = gptlex (); |
|
1237 } |
|
1238 } |
|
1239 } |
|
1240 |
|
1241 if (plottok_or_end_p (tok)) |
5489
|
1242 return std::string (); |
5102
|
1243 else |
|
1244 { |
|
1245 std::string file; |
|
1246 plot_line_count++; |
|
1247 |
5489
|
1248 std::string plot_expr_str; |
|
1249 plot_expr_str += std::string (yytext) + " "; |
|
1250 plot_expr_str += read_until (plottok_or_end_p, tok); |
|
1251 |
|
1252 int status = 0; |
|
1253 octave_value tmp_data = eval_string (plot_expr_str, |
|
1254 true, status); |
|
1255 |
|
1256 if (status != 0 || ! tmp_data.is_defined ()) |
|
1257 throw gpt_parse_error (); |
|
1258 |
|
1259 OSSTREAM tmp_buf; |
|
1260 tmp_data.print_raw (tmp_buf); |
|
1261 tmp_buf << OSSTREAM_ENDS; |
|
1262 |
|
1263 if (tmp_data.is_string ()) |
5102
|
1264 { |
5489
|
1265 file = file_ops::tilde_expand (tmp_data.string_value ()); |
5102
|
1266 // XXX FIXME XXX -- perhaps should check if the file exists? |
|
1267 outstr += file + " "; |
5489
|
1268 } |
5102
|
1269 else |
|
1270 { |
5489
|
1271 switch (ndim) |
5102
|
1272 { |
5489
|
1273 case 2: |
|
1274 file = save_in_tmp_file (tmp_data, ndim); |
|
1275 break; |
5102
|
1276 |
5489
|
1277 case 3: |
|
1278 file = save_in_tmp_file (tmp_data, ndim, |
|
1279 parametric_plot); |
|
1280 break; |
5102
|
1281 |
5489
|
1282 default: |
|
1283 gripe_2_or_3_dim_plot (); |
|
1284 break; |
|
1285 } |
5102
|
1286 |
5489
|
1287 if (file.length () > 0) |
|
1288 { |
|
1289 mark_for_deletion (file); |
|
1290 outstr += "'" + file + "' "; |
5152
|
1291 } |
5489
|
1292 } |
5102
|
1293 } |
|
1294 |
|
1295 std::string title_str; |
|
1296 std::string using_str; |
|
1297 std::string style_str; |
|
1298 std::string axes_str; |
|
1299 |
|
1300 bool out = false; |
|
1301 while (tok && ! out) |
|
1302 { |
|
1303 switch (tok) |
|
1304 { |
|
1305 case COMMA: |
|
1306 out = true; |
|
1307 break; |
|
1308 |
|
1309 case TITLE: |
|
1310 if (! title_set) |
|
1311 title_str += handle_title (tok) + " "; |
|
1312 else |
|
1313 throw gpt_parse_error ("only one title option may be specified"); |
|
1314 title_set = true; |
|
1315 break; |
|
1316 |
|
1317 case USING: |
|
1318 if (! using_set) |
|
1319 using_str += handle_using (tok) + " "; |
|
1320 else |
|
1321 throw gpt_parse_error ("only one using option may be specified"); |
|
1322 using_set = true; |
|
1323 break; |
|
1324 |
|
1325 case WITH: |
|
1326 if (! style_set) |
|
1327 style_str += handle_style (tok) + " "; |
|
1328 else |
|
1329 throw gpt_parse_error ("only one style option may be specified"); |
|
1330 style_set = true; |
|
1331 break; |
|
1332 |
|
1333 case AXES: |
|
1334 if (! axes_set) |
|
1335 axes_str += handle_axes (tok) + " "; |
|
1336 else |
|
1337 throw gpt_parse_error ("only one axes option may be specified"); |
|
1338 axes_set = true; |
|
1339 break; |
|
1340 |
|
1341 default: |
|
1342 tok = 0; |
|
1343 break; |
|
1344 } |
|
1345 } |
|
1346 |
|
1347 if (! title_set) |
|
1348 { |
|
1349 OSSTREAM tmp_buf; |
|
1350 tmp_buf << Vgnuplot_command_title << " \"line " |
|
1351 << plot_line_count << "\" " << OSSTREAM_ENDS; |
|
1352 title_str = OSSTREAM_STR (tmp_buf); |
|
1353 title_set = true; |
|
1354 } |
|
1355 |
|
1356 // Plot parameters have to be output in this order. |
|
1357 if (using_set) |
|
1358 outstr += using_str; |
|
1359 |
|
1360 if (axes_set) |
|
1361 outstr += axes_str; |
|
1362 |
|
1363 if (title_set) |
|
1364 outstr += title_str; |
|
1365 |
|
1366 if (style_set) |
|
1367 outstr += style_str; |
|
1368 |
|
1369 if (out) |
|
1370 { |
|
1371 // Saw comma on while loop. |
|
1372 outstr += ", "; |
|
1373 gpt_quote_is_transpose = false; |
|
1374 gpt_allow_plotkw = false; |
|
1375 gpt_parens = 0; |
|
1376 gpt_braces = 0; |
|
1377 gpt_brackets = 0; |
|
1378 tok = gptlex (); |
|
1379 } |
|
1380 } |
|
1381 |
|
1382 outstr += Vgnuplot_command_end; |
|
1383 |
5489
|
1384 return outstr; |
5102
|
1385 } |
|
1386 |
5489
|
1387 // Title has one string expression which is evaluated and printed to the |
|
1388 // gnuplot command string. |
5102
|
1389 |
5489
|
1390 std::string |
|
1391 gnuplot::handle_title (int& lasttok) |
|
1392 { |
|
1393 int tok; |
|
1394 std::string retstr = Vgnuplot_command_title + " "; |
|
1395 std::string title_expr_str; |
5102
|
1396 |
5489
|
1397 title_expr_str += read_until (plottok_or_end_p, tok); |
5102
|
1398 |
5489
|
1399 int status; |
|
1400 octave_value tmp_data = eval_string (title_expr_str, true, status); |
5102
|
1401 |
5489
|
1402 if (status != 0 || ! tmp_data.is_defined ()) |
|
1403 throw gpt_parse_error (); |
|
1404 |
|
1405 OSSTREAM tmp_buf; |
|
1406 if (tmp_data.is_string ()) |
5102
|
1407 { |
5489
|
1408 tmp_buf << '"'; |
|
1409 tmp_data.print_raw (tmp_buf); |
|
1410 tmp_buf << '"' << OSSTREAM_ENDS; |
|
1411 } |
|
1412 else |
|
1413 { |
|
1414 warning ("line title must be a string"); |
|
1415 tmp_buf << '"' << "line " << plot_line_count << '"'; |
5102
|
1416 } |
|
1417 |
5489
|
1418 retstr += OSSTREAM_STR (tmp_buf); |
5215
|
1419 |
5489
|
1420 lasttok = tok; |
5215
|
1421 |
5489
|
1422 return retstr; |
5102
|
1423 } |
|
1424 |
|
1425 static int |
|
1426 automatic_replot (void) |
|
1427 { |
|
1428 Vautomatic_replot = check_preference ("automatic_replot"); |
|
1429 |
|
1430 return 0; |
|
1431 } |
|
1432 |
|
1433 static int |
|
1434 set_string_var (std::string& var, const char *nm) |
|
1435 { |
|
1436 int retval = 0; |
|
1437 |
|
1438 std::string s = builtin_string_variable (nm); |
|
1439 |
|
1440 if (s.empty ()) |
|
1441 { |
|
1442 gripe_invalid_value_specified (nm); |
|
1443 retval = -1; |
|
1444 } |
|
1445 else |
|
1446 var = s; |
|
1447 |
|
1448 return retval; |
|
1449 } |
|
1450 |
|
1451 static int |
|
1452 gnuplot_binary (void) |
|
1453 { |
|
1454 return set_string_var (Vgnuplot_binary, "gnuplot_binary"); |
|
1455 } |
|
1456 |
|
1457 static int |
|
1458 gnuplot_command_plot (void) |
|
1459 { |
|
1460 return set_string_var (Vgnuplot_command_plot, "gnuplot_command_plot"); |
|
1461 } |
|
1462 |
|
1463 static int |
|
1464 gnuplot_command_replot (void) |
|
1465 { |
|
1466 return set_string_var (Vgnuplot_command_replot, "gnuplot_command_replot"); |
|
1467 } |
|
1468 |
|
1469 static int |
|
1470 gnuplot_command_splot (void) |
|
1471 { |
|
1472 return set_string_var (Vgnuplot_command_splot, "gnuplot_command_splot"); |
|
1473 } |
|
1474 |
|
1475 static int |
|
1476 gnuplot_command_using (void) |
|
1477 { |
|
1478 return set_string_var (Vgnuplot_command_using, "gnuplot_command_using"); |
|
1479 } |
|
1480 |
|
1481 static int |
|
1482 gnuplot_command_with (void) |
|
1483 { |
|
1484 return set_string_var (Vgnuplot_command_with, "gnuplot_command_with"); |
|
1485 } |
|
1486 |
|
1487 static int |
|
1488 gnuplot_command_axes (void) |
|
1489 { |
|
1490 return set_string_var (Vgnuplot_command_axes, "gnuplot_command_axes"); |
|
1491 } |
|
1492 |
|
1493 static int |
|
1494 gnuplot_command_title (void) |
|
1495 { |
|
1496 return set_string_var (Vgnuplot_command_title, "gnuplot_command_title"); |
|
1497 } |
|
1498 |
|
1499 static int |
|
1500 gnuplot_command_end (void) |
|
1501 { |
|
1502 return set_string_var (Vgnuplot_command_end, "gnuplot_command_end"); |
|
1503 } |
|
1504 |
|
1505 static int |
5489
|
1506 gnuplot_use_title_option (void) |
5102
|
1507 { |
5489
|
1508 Vgnuplot_use_title_option = check_preference ("gnuplot_use_title_option"); |
5102
|
1509 |
|
1510 return 0; |
|
1511 } |
|
1512 |
5489
|
1513 void |
|
1514 gnuplot::do_init (void) |
5102
|
1515 { |
5215
|
1516 static bool gnuplot_initialized = false; |
5102
|
1517 |
5215
|
1518 if (gnuplot_initialized) |
5311
|
1519 return; |
5102
|
1520 |
5215
|
1521 gnuplot_initialized = true; |
5102
|
1522 |
5311
|
1523 if (is_builtin_variable ("automatic_replot")) |
|
1524 automatic_replot (); |
|
1525 else |
|
1526 DEFVAR (automatic_replot, true, automatic_replot, |
5102
|
1527 "-*- texinfo -*-\n\ |
|
1528 @defvr {Built-in Variable} automatic_replot\n\ |
|
1529 You can tell Octave to redisplay the plot each time anything about it\n\ |
|
1530 changes by setting the value of the builtin variable\n\ |
|
1531 @code{automatic_replot} to a nonzero value. Although it is fairly\n\ |
|
1532 inefficient, especially for large plots, the default value is 1 for\n\ |
|
1533 compatibility with Matlab.\n\ |
|
1534 @end defvr"); |
|
1535 |
5311
|
1536 if (is_builtin_variable ("gnuplot_binary")) |
|
1537 gnuplot_binary (); |
|
1538 else |
|
1539 DEFVAR (gnuplot_binary, GNUPLOT_BINARY, gnuplot_binary, |
5102
|
1540 "-*- texinfo -*-\n\ |
|
1541 @defvr {Built-in Variable} gnuplot_binary\n\ |
|
1542 The name of the program invoked by the plot command. The default value\n\ |
|
1543 is @code{\"gnuplot\"}. @xref{Installation}.\n\ |
|
1544 @end defvr"); |
|
1545 |
5311
|
1546 if (is_builtin_variable ("gnuplot_command_plot")) |
|
1547 gnuplot_command_plot (); |
|
1548 else |
|
1549 DEFVAR (gnuplot_command_plot, "pl", gnuplot_command_plot, |
5102
|
1550 "-*- texinfo -*-\n\ |
|
1551 @defvr {Built-in Variable} gnuplot_command_plot\n\ |
|
1552 @end defvr"); |
|
1553 |
5311
|
1554 if (is_builtin_variable ("gnuplot_command_replot")) |
|
1555 gnuplot_command_replot (); |
|
1556 else |
|
1557 DEFVAR (gnuplot_command_replot, "rep", gnuplot_command_replot, |
5102
|
1558 "-*- texinfo -*-\n\ |
|
1559 @defvr {Built-in Variable} gnuplot_command_replot\n\ |
|
1560 @end defvr"); |
|
1561 |
5311
|
1562 if (is_builtin_variable ("gnuplot_command_splot")) |
|
1563 gnuplot_command_splot (); |
|
1564 else |
|
1565 DEFVAR (gnuplot_command_splot, "sp", gnuplot_command_splot, |
5102
|
1566 "-*- texinfo -*-\n\ |
|
1567 @defvr {Built-in Variable} gnuplot_command_splot\n\ |
|
1568 @end defvr"); |
|
1569 |
5311
|
1570 if (is_builtin_variable ("gnuplot_command_using")) |
|
1571 gnuplot_command_using (); |
|
1572 else |
|
1573 DEFVAR (gnuplot_command_using, "u", gnuplot_command_using, |
5102
|
1574 "-*- texinfo -*-\n\ |
|
1575 @defvr {Built-in Variable} gnuplot_command_using\n\ |
|
1576 @end defvr"); |
|
1577 |
5311
|
1578 if (is_builtin_variable ("gnuplot_command_with")) |
|
1579 gnuplot_command_with (); |
|
1580 else |
|
1581 DEFVAR (gnuplot_command_with, "w", gnuplot_command_with, |
5102
|
1582 "-*- texinfo -*-\n\ |
|
1583 @defvr {Built-in Variable} gnuplot_command_with\n\ |
|
1584 @end defvr"); |
|
1585 |
5311
|
1586 if (is_builtin_variable ("gnuplot_command_axes")) |
|
1587 gnuplot_command_axes (); |
|
1588 else |
|
1589 DEFVAR (gnuplot_command_axes, "ax", gnuplot_command_axes, |
5102
|
1590 "-*- texinfo -*-\n\ |
|
1591 @defvr {Built-in Variable} gnuplot_command_axes\n\ |
|
1592 @end defvr"); |
|
1593 |
5311
|
1594 if (is_builtin_variable ("gnuplot_command_title")) |
|
1595 gnuplot_command_title (); |
|
1596 else |
|
1597 DEFVAR (gnuplot_command_title, "t", gnuplot_command_title, |
5102
|
1598 "-*- texinfo -*-\n\ |
|
1599 @defvr {Built-in Variable} gnuplot_command_title\n\ |
|
1600 @end defvr"); |
|
1601 |
5311
|
1602 if (is_builtin_variable ("gnuplot_command_end")) |
|
1603 gnuplot_command_end (); |
|
1604 else |
|
1605 DEFVAR (gnuplot_command_end, "\n", gnuplot_command_end, |
5102
|
1606 "-*- texinfo -*-\n\ |
|
1607 @defvr {Built-in Variable} gnuplot_command_end\n\ |
|
1608 @end defvr"); |
|
1609 |
5489
|
1610 if (is_builtin_variable ("gnuplot_use_title_option")) |
|
1611 gnuplot_use_title_option (); |
5311
|
1612 else |
5489
|
1613 DEFVAR (gnuplot_use_title_option, octave_env::have_x11_display (), |
|
1614 gnuplot_use_title_option, |
5102
|
1615 "-*- texinfo -*-\n\ |
5489
|
1616 @defvr {Built-in Variable} gnuplot_use_title_option\n\ |
|
1617 If nonzero, append @samp{-title \"Figure NN\"} to the gnuplot command.\n\ |
5102
|
1618 @end defvr"); |
|
1619 |
5311
|
1620 } |
|
1621 |
5489
|
1622 // ----------------------- |
|
1623 // User-callable functions |
|
1624 // ----------------------- |
|
1625 |
5311
|
1626 DEFUN_DLD (__gnuplot_init__, , , |
|
1627 "-*- texinfo -*-\n\ |
|
1628 @deftypefn {Loadable Function} __gnuplot_init__ ()\n\ |
|
1629 @end deftypefn") |
|
1630 { |
5489
|
1631 gnuplot::init (); |
|
1632 |
|
1633 return octave_value_list (); |
|
1634 } |
|
1635 |
|
1636 DEFUN_DLD (clearplot, , , |
|
1637 "-*- texinfo -*-\n\ |
|
1638 @deftypefn {Built-in Function} {} clearplot\n\ |
|
1639 @deftypefnx {Built-in Function} {} clg\n\ |
|
1640 Clear the plot window and any titles or axis labels. The name\n\ |
|
1641 @code{clg} is aliased to @code{clearplot} for compatibility with\n\ |
|
1642 @sc{Matlab}.\n\ |
|
1643 @end deftypefn") |
|
1644 { |
|
1645 gnuplot::clear (); |
|
1646 |
|
1647 return octave_value_list (); |
|
1648 } |
|
1649 |
|
1650 DEFUN_DLD (closeplot, , , |
|
1651 "-*- texinfo -*-\n\ |
|
1652 @deftypefn {Built-in Function} {} closeplot\n\ |
|
1653 Close stream to the @code{gnuplot} subprocess. If you are using X11,\n\ |
|
1654 this will close the plot window.\n\ |
|
1655 @end deftypefn") |
|
1656 { |
|
1657 gnuplot::close (); |
|
1658 |
|
1659 return octave_value_list (); |
|
1660 } |
|
1661 |
|
1662 DEFUN_DLD (purge_tmp_files, , , |
|
1663 "-*- texinfo -*-\n\ |
|
1664 @deftypefn {Built-in Function} {} purge_tmp_files\n\ |
|
1665 Delete the temporary files created by the plotting commands.\n\ |
|
1666 \n\ |
|
1667 Octave creates temporary data files for @code{gnuplot} and then sends\n\ |
|
1668 commands to @code{gnuplot} through a pipe. Octave will delete the\n\ |
|
1669 temporary files on exit, but if you are doing a lot of plotting you may\n\ |
|
1670 want to clean up in the middle of a session.\n\ |
|
1671 \n\ |
|
1672 A future version of Octave will eliminate the need to use temporary\n\ |
|
1673 files to hold the plot data.\n\ |
|
1674 @end deftypefn") |
|
1675 { |
|
1676 gnuplot::cleanup_tmp_files (); |
|
1677 |
|
1678 return octave_value_list (); |
|
1679 } |
|
1680 |
|
1681 DEFUN_DLD (__gnuplot_raw__, args, , |
|
1682 "-*- texinfo -*-\n\ |
|
1683 @deftypefn {Built-in Function} {} __gnuplot_raw__ (@var{string})\n\ |
|
1684 Send @var{string} directly to gnuplot subprocess.\n\ |
|
1685 @end deftypefn") |
|
1686 { |
|
1687 if (args.length () == 1 && args(0).is_string ()) |
|
1688 { |
|
1689 std::string cmd = args(0).string_value (); |
|
1690 |
|
1691 gnuplot::send_raw (cmd); |
|
1692 } |
|
1693 else |
|
1694 print_usage ("raw"); |
|
1695 |
|
1696 return octave_value_list (); |
|
1697 } |
|
1698 |
|
1699 DEFUN_DLD (__gnuplot_set__, args, , |
|
1700 "-*- texinfo -*-\n\ |
|
1701 @deffn {Command} __gnuplot_set__ options\n\ |
|
1702 Set plotting options for gnuplot\n\ |
|
1703 @end deffn") |
|
1704 { |
|
1705 string_vector argv = args.make_argv ("set"); |
|
1706 |
|
1707 if (! error_state) |
|
1708 gnuplot::set (argv); |
|
1709 |
|
1710 return octave_value_list (); |
|
1711 } |
|
1712 |
|
1713 DEFUN_DLD (__gnuplot_show__, args, , |
|
1714 "-*- texinfo -*-\n\ |
|
1715 @deffn {Command} __gnuplot_show__ options\n\ |
|
1716 Show plotting options.\n\ |
|
1717 @end deffn") |
|
1718 { |
|
1719 string_vector argv = args.make_argv ("show"); |
|
1720 |
|
1721 if (! error_state) |
|
1722 gnuplot::show (argv); |
|
1723 |
|
1724 return octave_value_list (); |
|
1725 } |
5311
|
1726 |
5489
|
1727 DEFUN_DLD (__gnuplot_plot__, args, , |
|
1728 "Plot with gnuplot.\n") |
|
1729 { |
|
1730 string_vector argv = args.make_argv ("plot"); |
|
1731 |
|
1732 if (! error_state) |
|
1733 gnuplot::plot (argv); |
|
1734 |
|
1735 return octave_value_list (); |
|
1736 } |
|
1737 |
|
1738 DEFUN_DLD (__gnuplot_splot__, args, , |
|
1739 "Plot with gnuplot.\n") |
|
1740 { |
|
1741 string_vector argv = args.make_argv ("splot"); |
|
1742 |
|
1743 if (! error_state) |
|
1744 gnuplot::plot (argv); |
|
1745 |
|
1746 return octave_value_list (); |
|
1747 } |
|
1748 |
|
1749 DEFUN_DLD (__gnuplot_replot__, args, , |
|
1750 "Plot with gnuplot.\n") |
|
1751 { |
|
1752 string_vector argv = args.make_argv ("replot"); |
|
1753 |
|
1754 if (! error_state) |
|
1755 gnuplot::plot (argv); |
|
1756 |
|
1757 return octave_value_list (); |
|
1758 } |
|
1759 |
|
1760 // -------------------- |
|
1761 // Deprecated functions |
|
1762 // -------------------- |
5311
|
1763 |
5489
|
1764 #define WARN_DEPRECATED(FOLD, FNEW) \ |
|
1765 do \ |
|
1766 { \ |
|
1767 static bool warned = false; \ |
|
1768 if (! warned) \ |
|
1769 { \ |
|
1770 warned = true; \ |
|
1771 warning (#FOLD " is deprecated and will be removed from a future"); \ |
|
1772 warning ("version of Octave."); \ |
|
1773 warning ("You should use the higher-level plot functions"); \ |
|
1774 warning ("(\"plot\", \"mesh\", \"semilogx\", etc.) instead"); \ |
|
1775 warning ("of the low-level plotting commands."); \ |
|
1776 warning ("If you absolutely must use this function, use the"); \ |
|
1777 warning ("internal version " #FNEW " instead."); \ |
|
1778 } \ |
|
1779 } \ |
|
1780 while (0) |
|
1781 |
|
1782 #define DEPRECATED_BODY(FOLD, FNEW) \ |
|
1783 WARN_DEPRECATED (FOLD, FNEW); \ |
|
1784 return feval (#FNEW, args) |
|
1785 |
|
1786 // We can't further simplify this by putting the DEFUN_DLD in a macro |
|
1787 // because then the mk-oct-links script will fail. |
|
1788 |
|
1789 DEFUN_DLD (gplot, args, , |
|
1790 "") |
|
1791 { |
|
1792 DEPRECATED_BODY (gplot, __gnuplot_plot__); |
5102
|
1793 } |
5489
|
1794 |
|
1795 DEFUN_DLD (gsplot, args, , |
|
1796 "") |
|
1797 { |
|
1798 DEPRECATED_BODY (gsplot, __gnuplot_splot__); |
|
1799 } |
|
1800 |
|
1801 DEFUN_DLD (graw, args, , |
|
1802 "") |
|
1803 { |
|
1804 DEPRECATED_BODY (graw, __gnuplot_raw__); |
|
1805 } |
|
1806 |
|
1807 DEFUN_DLD (gset, args, , |
|
1808 "") |
|
1809 { |
|
1810 DEPRECATED_BODY (gset, __gnuplot_set__); |
|
1811 } |
|
1812 |
|
1813 DEFUN_DLD (gshow, args, , |
|
1814 "") |
|
1815 { |
|
1816 DEPRECATED_BODY (gshow, __gnuplot_show__); |
|
1817 } |
|
1818 |
|
1819 /* |
|
1820 ;;; Local Variables: *** |
|
1821 ;;; mode: C++ *** |
|
1822 ;;; End: *** |
|
1823 */ |