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