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