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 |
7138
|
692 #define WARN_OBSOLETE(nm) \ |
|
693 do \ |
|
694 { \ |
7139
|
695 static bool warned = false; \ |
7138
|
696 \ |
|
697 if (! warned) \ |
|
698 { \ |
7140
|
699 warning ("%s: this function is obsolete and will be removed from a future version of Octave", nm); \ |
7138
|
700 \ |
|
701 warned = true; \ |
|
702 } \ |
|
703 } \ |
|
704 while (0) |
|
705 |
6171
|
706 DEFUN_DLD (__gnuplot_save_data__, args, , |
|
707 "-*- texinfo -*-\n\ |
|
708 @deftypefn {Loadable Function} {} __gnuplot_save_data__ (@var{data}, @var{ndim}, @var{parametric})\n\ |
7138
|
709 @strong{This function is obsolete and will be removed from a future version\n\ |
|
710 of Octave.}\n\ |
6171
|
711 @end deftypefn") |
|
712 { |
|
713 octave_value retval; |
|
714 |
|
715 int nargin = args.length (); |
|
716 |
|
717 if (nargin > 0 && nargin < 4) |
|
718 { |
7141
|
719 WARN_OBSOLETE ("__gnuplot_save_data__"); |
|
720 |
6171
|
721 int ndim = 2; |
|
722 bool parametric = false; |
|
723 |
|
724 if (nargin > 1) |
|
725 { |
|
726 ndim = args(1).int_value (); |
|
727 |
|
728 if (! error_state) |
|
729 { |
|
730 if (nargin > 2) |
|
731 parametric = args(2).bool_value (); |
|
732 } |
|
733 } |
|
734 |
|
735 if (! error_state) |
|
736 { |
|
737 std::string file = save_in_tmp_file (args(0), ndim, parametric); |
|
738 |
|
739 mark_for_deletion (file); |
|
740 |
|
741 retval = file; |
|
742 } |
|
743 } |
|
744 else |
|
745 print_usage (); |
|
746 |
|
747 return retval; |
|
748 } |
|
749 |
5790
|
750 static int |
|
751 get_current_figure (void) |
|
752 { |
|
753 int retval = 1; |
|
754 |
|
755 octave_value cf = get_global_value ("__current_figure__", true); |
|
756 |
|
757 if (cf.is_defined ()) |
|
758 retval = cf.int_value (); |
|
759 else |
|
760 set_global_value ("__current_figure__", retval); |
|
761 |
|
762 return retval; |
|
763 } |
|
764 |
|
765 class |
|
766 gnuplot |
|
767 { |
|
768 protected: |
|
769 |
|
770 gnuplot (void) |
|
771 : plot_line_count (0), parametric_plot (false), |
|
772 use_title_option (Vgnuplot_use_title_option), |
|
773 gnuplot_exe (Vgnuplot_binary), |
|
774 gnuplot_terminal_type (), plot_stream () { } |
|
775 |
|
776 public: |
|
777 |
|
778 ~gnuplot (void) { } |
|
779 |
|
780 static bool have_instance (void); |
|
781 |
|
782 static bool ensure_instance (void); |
|
783 |
|
784 static bool ensure_plot_stream (void); |
|
785 |
|
786 static void open (void); |
|
787 |
|
788 static void close (void); |
|
789 |
|
790 static void close_all (void); |
|
791 |
|
792 static bool plot_stream_event_handler (pid_t pid, int status); |
|
793 |
|
794 static void send (const std::string& cmd) |
|
795 { |
|
796 if (ensure_plot_stream ()) |
|
797 instance->do_send (cmd); |
|
798 } |
|
799 |
|
800 static void send_raw (const std::string& cmd) |
|
801 { |
|
802 if (ensure_plot_stream ()) |
|
803 instance->do_send_raw (cmd); |
|
804 } |
|
805 |
6171
|
806 static void send_inline_data (const octave_value& val, int ndim, |
|
807 bool parametric) |
|
808 { |
|
809 if (ensure_plot_stream ()) |
|
810 instance->do_send_inline_data (val, ndim, parametric); |
|
811 } |
|
812 |
5790
|
813 static void clear (void) |
|
814 { |
|
815 if (ensure_plot_stream ()) |
|
816 instance->do_clear (); |
|
817 } |
|
818 |
|
819 static void set (const string_vector& argv) |
|
820 { |
|
821 if (ensure_plot_stream ()) |
|
822 instance->do_set (argv); |
|
823 } |
|
824 |
|
825 static void show (const string_vector& argv) |
|
826 { |
|
827 if (ensure_plot_stream ()) |
|
828 instance->do_show (argv); |
|
829 } |
|
830 |
|
831 static void set_gnuplot_exe (const std::string& exe) |
|
832 { |
|
833 if (ensure_instance ()) |
|
834 instance->do_set_gnuplot_exe (exe); |
|
835 } |
|
836 |
|
837 static void set_gnuplot_use_title_option (bool opt) |
|
838 { |
|
839 if (ensure_instance ()) |
|
840 instance->do_set_gnuplot_use_title_option (opt); |
|
841 } |
|
842 |
|
843 // FIXME -- should only remove tmp files associated with |
|
844 // gnuplot? |
|
845 static void cleanup_tmp_files (void) { ::cleanup_tmp_files (); } |
|
846 |
|
847 static void plot (const string_vector& argv) |
|
848 { |
|
849 if (ensure_plot_stream ()) |
|
850 instance->do_plot (argv); |
|
851 } |
|
852 |
|
853 private: |
|
854 |
|
855 static gnuplot *instance; |
|
856 |
|
857 static std::map<int, gnuplot *> instance_map; |
|
858 |
|
859 // The number of lines we've plotted so far. |
|
860 int plot_line_count; |
|
861 |
|
862 // Is this a parametric plot? Makes a difference for 3D plotting. |
|
863 bool parametric_plot; |
|
864 |
|
865 // Should we append '-title "TITLE"' to the gnuplot command? |
|
866 bool use_title_option; |
|
867 |
|
868 // The executable program to run. |
|
869 std::string gnuplot_exe; |
|
870 |
|
871 // The gnuplot terminal type. |
|
872 std::string gnuplot_terminal_type; |
|
873 |
|
874 // Pipe to gnuplot. |
|
875 oprocstream *plot_stream; |
|
876 |
|
877 pid_t pid (void) const; |
|
878 |
|
879 static gnuplot *lookup_by_pid (pid_t pid); |
|
880 |
|
881 void do_open (void); |
|
882 |
|
883 void do_close (void); |
|
884 |
|
885 void delete_plot_stream (void); |
|
886 |
|
887 void reset_plot_stream (void); |
|
888 |
|
889 void do_send (const std::string& cmd); |
|
890 |
|
891 void do_send_raw (const std::string& cmd); |
|
892 |
6171
|
893 void do_send_inline_data (const octave_value& val, int ndim, |
|
894 bool parametric); |
|
895 |
5790
|
896 void do_clear (void); |
|
897 |
|
898 void do_set (const string_vector& argv); |
|
899 |
|
900 void do_show (const string_vector& argv); |
|
901 |
|
902 void do_set_gnuplot_exe (const std::string& exe) { gnuplot_exe = exe; } |
|
903 |
|
904 void do_set_gnuplot_use_title_option (bool opt) { use_title_option = opt; } |
|
905 |
|
906 void do_plot (const string_vector& argv); |
|
907 |
|
908 std::string |
|
909 makeplot (std::string caller, std::string args) throw (gpt_parse_error); |
|
910 |
|
911 std::string handle_title (int& lasttok); |
|
912 }; |
|
913 |
|
914 gnuplot *gnuplot::instance = 0; |
|
915 |
|
916 std::map<int, gnuplot *> gnuplot::instance_map; |
|
917 |
|
918 bool |
|
919 gnuplot::have_instance (void) |
|
920 { |
|
921 int current_figure = get_current_figure (); |
|
922 |
|
923 if (instance_map.find (current_figure) != instance_map.end ()) |
|
924 { |
|
925 instance = instance_map[current_figure]; |
|
926 return true; |
|
927 } |
|
928 else |
|
929 return false; |
|
930 } |
|
931 |
|
932 bool |
|
933 gnuplot::ensure_instance (void) |
|
934 { |
|
935 if (! have_instance ()) |
|
936 { |
|
937 instance = new gnuplot (); |
|
938 |
|
939 if (! instance) |
|
940 { |
|
941 ::error ("unable to create gnuplot object!"); |
|
942 |
|
943 return false; |
|
944 } |
|
945 else |
|
946 instance_map[get_current_figure ()] = instance; |
|
947 } |
|
948 |
|
949 return true; |
|
950 } |
|
951 |
|
952 bool |
|
953 gnuplot::ensure_plot_stream (void) |
|
954 { |
|
955 if (ensure_instance ()) |
|
956 { |
|
957 instance->do_open (); |
|
958 |
|
959 if (error_state) |
|
960 return false; |
|
961 } |
|
962 |
|
963 return true; |
|
964 } |
|
965 |
|
966 void |
|
967 gnuplot::close (void) |
|
968 { |
|
969 if (have_instance ()) |
|
970 { |
|
971 instance->do_close (); |
|
972 |
|
973 instance_map.erase (get_current_figure ()); |
|
974 } |
|
975 } |
|
976 |
|
977 void |
|
978 gnuplot::close_all (void) |
|
979 { |
|
980 for (std::map<int, gnuplot *>::const_iterator p = instance_map.begin (); |
|
981 p != instance_map.end (); |
|
982 p++) |
|
983 { |
|
984 gnuplot *elt = p->second; |
|
985 |
|
986 elt->do_close (); |
|
987 } |
|
988 } |
|
989 |
|
990 pid_t |
|
991 gnuplot::pid (void) const |
|
992 { |
|
993 return plot_stream ? plot_stream->pid () : -1; |
|
994 } |
|
995 |
|
996 gnuplot * |
|
997 gnuplot::lookup_by_pid (pid_t pid) |
|
998 { |
|
999 gnuplot *retval = 0; |
|
1000 |
|
1001 for (std::map<int, gnuplot *>::const_iterator p = instance_map.begin (); |
|
1002 p != instance_map.end (); |
|
1003 p++) |
|
1004 { |
|
1005 gnuplot *elt = p->second; |
|
1006 |
|
1007 if (elt && elt->pid () == pid) |
|
1008 { |
|
1009 retval = elt; |
|
1010 break; |
|
1011 } |
|
1012 } |
|
1013 |
|
1014 return retval; |
|
1015 } |
|
1016 |
|
1017 void |
|
1018 gnuplot::do_open (void) |
|
1019 { |
|
1020 static bool initialized = false; |
|
1021 |
|
1022 if (plot_stream && ! *plot_stream) |
|
1023 do_close (); |
|
1024 |
|
1025 if (! plot_stream) |
|
1026 { |
|
1027 initialized = false; |
|
1028 |
|
1029 plot_line_count = 0; |
|
1030 |
|
1031 std::string cmd; |
|
1032 |
|
1033 if (gnuplot_exe.empty ()) |
|
1034 cmd = "gnuplot"; |
|
1035 else |
|
1036 cmd = "\"" + gnuplot_exe + "\""; |
|
1037 |
|
1038 // FIXME -- I'm not sure this is the right thing to do, |
|
1039 // but without it, C-c at the octave prompt will kill gnuplot... |
|
1040 |
|
1041 #if defined (HAVE_POSIX_SIGNALS) |
|
1042 sigset_t nset, oset; |
|
1043 sigemptyset (&nset); |
|
1044 sigaddset (&nset, SIGINT); |
|
1045 sigprocmask (SIG_BLOCK, &nset, &oset); |
|
1046 #else |
|
1047 volatile octave_interrupt_handler old_interrupt_handler |
|
1048 = octave_ignore_interrupts (); |
|
1049 #endif |
|
1050 |
|
1051 if (use_title_option) |
|
1052 { |
|
1053 std::ostringstream buf; |
|
1054 |
|
1055 buf << cmd << " -title \"Figure " << get_current_figure () << "\""; |
|
1056 |
|
1057 cmd = buf.str (); |
|
1058 } |
|
1059 |
|
1060 plot_stream = new oprocstream (cmd.c_str ()); |
|
1061 |
|
1062 if (plot_stream && *plot_stream) |
|
1063 octave_child_list::insert (plot_stream->pid (), |
|
1064 plot_stream_event_handler); |
|
1065 else |
|
1066 { |
|
1067 delete_plot_stream (); |
|
1068 |
|
1069 error ("plot: unable to open pipe to `%s'", cmd.c_str ()); |
|
1070 } |
|
1071 |
|
1072 #if defined (HAVE_POSIX_SIGNALS) |
|
1073 sigprocmask (SIG_SETMASK, &oset, 0); |
|
1074 #else |
|
1075 octave_set_interrupt_handler (old_interrupt_handler); |
|
1076 #endif |
|
1077 } |
|
1078 |
|
1079 if (! error_state && plot_stream && *plot_stream && ! initialized) |
|
1080 { |
|
1081 initialized = true; |
|
1082 |
|
1083 do_send_raw ("set style data lines\n"); |
|
1084 |
|
1085 if (! gnuplot_terminal_type.empty ()) |
|
1086 do_send_raw ("set term " + gnuplot_terminal_type |
|
1087 + Vgnuplot_command_end + "\n"); |
|
1088 } |
|
1089 } |
|
1090 |
|
1091 void |
|
1092 gnuplot::delete_plot_stream (void) |
|
1093 { |
|
1094 do_send_raw ("\nquit\n"); |
|
1095 |
|
1096 delete plot_stream; |
|
1097 plot_stream = 0; |
|
1098 } |
|
1099 |
|
1100 void |
|
1101 gnuplot::reset_plot_stream (void) |
|
1102 { |
|
1103 delete_plot_stream (); |
|
1104 |
|
1105 plot_line_count = 0; |
|
1106 parametric_plot = false; |
|
1107 } |
|
1108 |
|
1109 void |
|
1110 gnuplot::do_close (void) |
|
1111 { |
|
1112 if (plot_stream) |
|
1113 { |
|
1114 octave_child_list::remove (plot_stream->pid ()); |
|
1115 |
|
1116 delete_plot_stream (); |
|
1117 } |
|
1118 |
|
1119 plot_line_count = 0; |
|
1120 } |
|
1121 |
|
1122 bool |
|
1123 gnuplot::plot_stream_event_handler (pid_t pid, int status) |
|
1124 { |
|
1125 bool retval = false; |
|
1126 |
|
1127 if (pid > 0) |
|
1128 { |
|
1129 if (WIFEXITED (status) || WIFSIGNALLED (status)) |
|
1130 { |
|
1131 gnuplot *plotter = gnuplot::lookup_by_pid (pid); |
|
1132 |
|
1133 // We should only print a warning or request removal of the |
|
1134 // process from the child list if the process died |
|
1135 // unexpectedly. If do_close is responsible for |
|
1136 // gnuplot's death, then plotter will be 0 here and we don't |
|
1137 // need to do anything. |
|
1138 |
|
1139 if (plotter) |
|
1140 { |
|
1141 plotter->reset_plot_stream (); |
|
1142 |
|
1143 warning ("connection to external plotter (pid = %d) lost --", pid); |
|
1144 // Request removal of this PID from the list of child |
|
1145 // processes. |
|
1146 |
|
1147 retval = true; |
|
1148 } |
|
1149 } |
|
1150 } |
|
1151 |
|
1152 return retval; |
|
1153 } |
|
1154 |
|
1155 void |
|
1156 gnuplot::do_send (const std::string& cmd) |
|
1157 { |
|
1158 int replot_len = Vgnuplot_command_replot.length (); |
|
1159 |
|
1160 bool is_replot = (Vgnuplot_command_replot == cmd.substr (0, replot_len)); |
|
1161 |
|
1162 if (! (plot_line_count == 0 && is_replot)) |
|
1163 do_send_raw (cmd); |
|
1164 } |
|
1165 |
|
1166 void |
|
1167 gnuplot::do_send_raw (const std::string& cmd) |
|
1168 { |
|
1169 if (plot_stream && *plot_stream) |
|
1170 { |
|
1171 *plot_stream << cmd; |
|
1172 |
|
1173 plot_stream->flush (); |
|
1174 } |
|
1175 } |
|
1176 |
|
1177 void |
6171
|
1178 gnuplot::do_send_inline_data (const octave_value& val, int ndim, |
|
1179 bool parametric) |
|
1180 { |
|
1181 if (plot_stream && *plot_stream) |
|
1182 { |
|
1183 write_inline_data (*plot_stream, val, ndim, parametric); |
|
1184 |
|
1185 plot_stream->flush (); |
|
1186 } |
|
1187 } |
|
1188 |
|
1189 void |
5790
|
1190 gnuplot::do_clear (void) |
|
1191 { |
|
1192 do_send_raw ("clear\n"); |
|
1193 |
|
1194 // FIXME -- instead of just clearing these things, it would |
|
1195 // be nice if we could reset things to a user-specified default |
|
1196 // state. |
|
1197 |
|
1198 do_send_raw ("set title\n"); |
|
1199 do_send_raw ("set xlabel\n"); |
|
1200 do_send_raw ("set ylabel\n"); |
|
1201 do_send_raw ("set nogrid\n"); |
|
1202 do_send_raw ("set nolabel\n"); |
|
1203 |
|
1204 // This makes a simple `replot' not work after a `clearplot' command |
|
1205 // has been issued. |
|
1206 |
|
1207 plot_line_count = 0; |
|
1208 } |
|
1209 |
|
1210 void |
|
1211 gnuplot::do_set (const string_vector& argv) |
|
1212 { |
|
1213 int argc = argv.length (); |
|
1214 |
|
1215 std::ostringstream plot_buf; |
|
1216 |
|
1217 if (argc > 1) |
|
1218 { |
|
1219 if (almost_match ("parametric", argv[1], 3)) |
|
1220 parametric_plot = true; |
|
1221 else if (almost_match ("noparametric", argv[1], 5)) |
|
1222 parametric_plot = false; |
|
1223 else if (almost_match ("term", argv[1], 1)) |
|
1224 { |
|
1225 gnuplot_terminal_type = ""; |
|
1226 std::ostringstream buf; |
|
1227 int i; |
|
1228 for (i = 2; i < argc-1; i++) |
|
1229 buf << argv[i] << " "; |
|
1230 if (i < argc) |
|
1231 buf << argv[i]; |
|
1232 buf << Vgnuplot_command_end; |
|
1233 gnuplot_terminal_type = buf.str (); |
|
1234 } |
|
1235 } |
|
1236 |
|
1237 int i; |
|
1238 for (i = 0; i < argc-1; i++) |
|
1239 plot_buf << argv[i] << " "; |
|
1240 |
|
1241 if (i < argc) |
|
1242 plot_buf << argv[i]; |
|
1243 |
|
1244 plot_buf << Vgnuplot_command_end; |
|
1245 |
|
1246 do_send_raw (plot_buf.str ()); |
|
1247 } |
|
1248 |
|
1249 void |
|
1250 gnuplot::do_show (const string_vector& argv) |
|
1251 { |
|
1252 int argc = argv.length (); |
|
1253 |
|
1254 std::ostringstream plot_buf; |
|
1255 |
|
1256 int i; |
|
1257 for (i = 0; i < argc-1; i++) |
|
1258 plot_buf << argv[i] << " "; |
|
1259 if (i < argc) |
|
1260 plot_buf << argv[i]; |
|
1261 |
|
1262 plot_buf << Vgnuplot_command_end; |
|
1263 |
|
1264 do_send (plot_buf.str ()); |
|
1265 } |
|
1266 |
|
1267 void |
|
1268 gnuplot::do_plot (const string_vector& argv) |
|
1269 { |
|
1270 std::string s; |
|
1271 |
|
1272 for (int i = 1; i < argv.length (); i++) |
|
1273 s += argv[i] + " "; |
|
1274 |
|
1275 try |
|
1276 { |
|
1277 std::string cmd = makeplot (argv[0], s); |
|
1278 |
|
1279 do_send (cmd); |
|
1280 } |
|
1281 catch (gpt_parse_error& e) |
|
1282 { |
|
1283 if (e.msg.empty ()) |
|
1284 error ("could not parse plot command"); |
|
1285 else |
|
1286 error (e.msg.c_str ()); |
|
1287 } |
|
1288 } |
|
1289 |
|
1290 // Parse and evaluate parameter string and pass it to gnuplot pipe. |
|
1291 |
|
1292 std::string |
|
1293 gnuplot::makeplot (std::string caller, std::string args) |
|
1294 throw (gpt_parse_error) |
|
1295 { |
|
1296 std::string retval; |
|
1297 |
|
1298 YY_BUFFER_STATE bstate; |
|
1299 |
|
1300 bstate = yy_scan_string (args.c_str ()); |
|
1301 yy_switch_to_buffer (bstate); |
|
1302 std::string outstr; |
|
1303 int ndim = 2; |
|
1304 |
|
1305 if (caller == "replot") |
|
1306 { |
|
1307 ndim = 1; |
|
1308 outstr += Vgnuplot_command_replot + " "; |
|
1309 } |
|
1310 else if (caller == "plot") |
|
1311 { |
|
1312 ndim = 2; |
|
1313 plot_line_count = 0; |
|
1314 outstr += Vgnuplot_command_plot + " "; |
|
1315 } |
|
1316 else if (caller == "splot") |
|
1317 { |
|
1318 ndim = 3; |
|
1319 plot_line_count = 0; |
|
1320 outstr += Vgnuplot_command_splot + " "; |
|
1321 } |
|
1322 else |
|
1323 throw gpt_parse_error ("unknown plot command"); |
|
1324 |
|
1325 gpt_quote_is_transpose = false; |
|
1326 gpt_allow_plotkw = false; |
|
1327 gpt_parens = 0; |
|
1328 gpt_braces = 0; |
|
1329 gpt_brackets = 0; |
|
1330 |
|
1331 int tok; |
|
1332 tok = gptlex (); |
|
1333 if (plottok_or_end_p (tok) && caller != "replot") |
|
1334 throw gpt_parse_error ("must have something to plot"); |
|
1335 |
|
1336 while (tok) |
|
1337 { |
|
1338 bool title_set = false; |
|
1339 bool using_set = false; |
|
1340 bool style_set = false; |
|
1341 bool axes_set = false; |
|
1342 |
|
1343 if (tok == START_BRACKET) |
|
1344 { |
|
1345 if (caller == "replot") |
|
1346 throw gpt_parse_error ("can't specify new plot ranges with `replot' or while hold is on"); |
|
1347 |
|
1348 std::string xrange_start_str = read_until (colonp, tok); |
|
1349 std::string xrange_end_str = read_until (endbracketp, tok); |
|
1350 outstr += printrange (xrange_start_str, xrange_end_str) + " "; |
|
1351 tok = gptlex (); |
|
1352 if (tok == START_BRACKET) |
|
1353 { |
|
1354 std::string yrange_start_str = read_until (colonp, tok); |
|
1355 std::string yrange_end_str = read_until (endbracketp, tok); |
|
1356 outstr += printrange (yrange_start_str, yrange_end_str) + " "; |
|
1357 tok = gptlex (); |
6548
|
1358 if (tok == START_BRACKET && caller == "splot") |
5790
|
1359 { |
|
1360 std::string zrange_start_str = read_until (colonp, tok); |
|
1361 std::string zrange_end_str = read_until (endbracketp, tok); |
|
1362 outstr += printrange (zrange_start_str, zrange_end_str) + " "; |
|
1363 tok = gptlex (); |
|
1364 } |
|
1365 } |
|
1366 } |
|
1367 |
|
1368 if (plottok_or_end_p (tok)) |
|
1369 return std::string (); |
|
1370 else |
|
1371 { |
|
1372 std::string file; |
|
1373 plot_line_count++; |
|
1374 |
|
1375 std::string plot_expr_str; |
|
1376 plot_expr_str += std::string (yytext) + " "; |
|
1377 plot_expr_str += read_until (plottok_or_end_p, tok); |
|
1378 |
|
1379 int status = 0; |
|
1380 octave_value tmp_data = eval_string (plot_expr_str, |
|
1381 true, status); |
|
1382 |
|
1383 if (status != 0 || ! tmp_data.is_defined ()) |
|
1384 throw gpt_parse_error (); |
|
1385 |
|
1386 std::ostringstream tmp_buf; |
|
1387 tmp_data.print_raw (tmp_buf); |
|
1388 |
|
1389 if (tmp_data.is_string ()) |
|
1390 { |
|
1391 file = file_ops::tilde_expand (tmp_data.string_value ()); |
|
1392 // FIXME -- perhaps should check if the file exists? |
|
1393 outstr += file + " "; |
|
1394 } |
|
1395 else |
|
1396 { |
|
1397 switch (ndim) |
|
1398 { |
|
1399 case 2: |
|
1400 file = save_in_tmp_file (tmp_data, ndim); |
|
1401 break; |
|
1402 |
|
1403 case 3: |
|
1404 file = save_in_tmp_file (tmp_data, ndim, |
|
1405 parametric_plot); |
|
1406 break; |
|
1407 |
|
1408 default: |
|
1409 gripe_2_or_3_dim_plot (); |
|
1410 break; |
|
1411 } |
|
1412 |
|
1413 if (file.length () > 0) |
|
1414 { |
|
1415 mark_for_deletion (file); |
|
1416 outstr += "'" + file + "' "; |
|
1417 } |
|
1418 } |
|
1419 } |
|
1420 |
|
1421 std::string title_str; |
|
1422 std::string using_str; |
|
1423 std::string style_str; |
|
1424 std::string axes_str; |
|
1425 |
|
1426 bool out = false; |
|
1427 while (tok && ! out) |
|
1428 { |
|
1429 switch (tok) |
|
1430 { |
|
1431 case COMMA: |
|
1432 out = true; |
|
1433 break; |
|
1434 |
|
1435 case TITLE: |
|
1436 if (! title_set) |
|
1437 title_str += handle_title (tok) + " "; |
|
1438 else |
|
1439 throw gpt_parse_error ("only one title option may be specified"); |
|
1440 title_set = true; |
|
1441 break; |
|
1442 |
|
1443 case USING: |
|
1444 if (! using_set) |
|
1445 using_str += handle_using (tok) + " "; |
|
1446 else |
|
1447 throw gpt_parse_error ("only one using option may be specified"); |
|
1448 using_set = true; |
|
1449 break; |
|
1450 |
|
1451 case WITH: |
|
1452 if (! style_set) |
|
1453 style_str += handle_style (tok) + " "; |
|
1454 else |
|
1455 throw gpt_parse_error ("only one style option may be specified"); |
|
1456 style_set = true; |
|
1457 break; |
|
1458 |
|
1459 case AXES: |
|
1460 if (! axes_set) |
|
1461 axes_str += handle_axes (tok) + " "; |
|
1462 else |
|
1463 throw gpt_parse_error ("only one axes option may be specified"); |
|
1464 axes_set = true; |
|
1465 break; |
|
1466 |
|
1467 default: |
|
1468 tok = 0; |
|
1469 break; |
|
1470 } |
|
1471 } |
|
1472 |
|
1473 if (! title_set) |
|
1474 { |
|
1475 std::ostringstream tmp_buf; |
|
1476 tmp_buf << Vgnuplot_command_title << " \"line " |
|
1477 << plot_line_count << "\" "; |
|
1478 title_str = tmp_buf.str (); |
|
1479 title_set = true; |
|
1480 } |
|
1481 |
|
1482 // Plot parameters have to be output in this order. |
|
1483 if (using_set) |
|
1484 outstr += using_str; |
|
1485 |
|
1486 if (axes_set) |
|
1487 outstr += axes_str; |
|
1488 |
|
1489 if (title_set) |
|
1490 outstr += title_str; |
|
1491 |
|
1492 if (style_set) |
|
1493 outstr += style_str; |
|
1494 |
|
1495 if (out) |
|
1496 { |
|
1497 // Saw comma on while loop. |
|
1498 outstr += ", "; |
|
1499 gpt_quote_is_transpose = false; |
|
1500 gpt_allow_plotkw = false; |
|
1501 gpt_parens = 0; |
|
1502 gpt_braces = 0; |
|
1503 gpt_brackets = 0; |
|
1504 tok = gptlex (); |
|
1505 } |
|
1506 } |
|
1507 |
|
1508 outstr += Vgnuplot_command_end; |
|
1509 |
|
1510 return outstr; |
|
1511 } |
|
1512 |
|
1513 // Title has one string expression which is evaluated and printed to the |
|
1514 // gnuplot command string. |
|
1515 |
|
1516 std::string |
|
1517 gnuplot::handle_title (int& lasttok) |
|
1518 { |
|
1519 int tok; |
|
1520 std::string retstr = Vgnuplot_command_title + " "; |
|
1521 std::string title_expr_str; |
|
1522 |
|
1523 title_expr_str += read_until (plottok_or_end_p, tok); |
|
1524 |
|
1525 int status; |
|
1526 octave_value tmp_data = eval_string (title_expr_str, true, status); |
|
1527 |
|
1528 if (status != 0 || ! tmp_data.is_defined ()) |
|
1529 throw gpt_parse_error (); |
|
1530 |
|
1531 std::ostringstream tmp_buf; |
|
1532 if (tmp_data.is_string ()) |
|
1533 { |
|
1534 tmp_buf << '"'; |
|
1535 tmp_data.print_raw (tmp_buf); |
|
1536 tmp_buf << '"'; |
|
1537 } |
|
1538 else |
|
1539 { |
|
1540 warning ("line title must be a string"); |
|
1541 tmp_buf << '"' << "line " << plot_line_count << '"'; |
|
1542 } |
|
1543 |
|
1544 retstr += tmp_buf.str (); |
|
1545 |
|
1546 lasttok = tok; |
|
1547 |
|
1548 return retstr; |
|
1549 } |
|
1550 |
|
1551 // The static instance of this class is here so that |
|
1552 // gnuplot::close_all will be called when the .oct file is unloaded. |
|
1553 |
|
1554 class |
|
1555 gnuplot_X |
|
1556 { |
|
1557 public: |
|
1558 gnuplot_X (void) { } |
|
1559 ~gnuplot_X (void) { gnuplot::close_all (); } |
|
1560 }; |
|
1561 |
|
1562 static gnuplot_X X; |
|
1563 |
|
1564 // ----------------------- |
|
1565 // User-callable functions |
|
1566 // ----------------------- |
|
1567 |
5794
|
1568 DEFUN_DLD (gnuplot_binary, args, nargout, |
5790
|
1569 "-*- texinfo -*-\n\ |
5794
|
1570 @deftypefn {Loadable Function} {@var{val} =} gnuplot_binary ()\n\ |
|
1571 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_binary (@var{new_val})\n\ |
|
1572 Query or set the name of the program invoked by the plot command.\n\ |
|
1573 The default value @code{\"gnuplot\"}. @xref{Installation}.\n\ |
5790
|
1574 @end deftypefn") |
|
1575 { |
|
1576 octave_value retval = SET_INTERNAL_VARIABLE (gnuplot_binary); |
|
1577 |
|
1578 if (! error_state && args.length () == 1) |
|
1579 gnuplot::set_gnuplot_exe (Vgnuplot_binary); |
|
1580 |
|
1581 return retval; |
|
1582 } |
|
1583 |
5794
|
1584 DEFUN_DLD (gnuplot_command_plot, args, nargout, |
5790
|
1585 "-*- texinfo -*-\n\ |
|
1586 @deftypefn {Loadable Function} {@var{val} = } gnuplot_command_plot ()\n\ |
|
1587 @deftypefnx {Loadable Function} {@var{old_val} = } gnuplot_command_plot (@var{new_val})\n\ |
7117
|
1588 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1589 of Octave.}\n\ |
5790
|
1590 @end deftypefn") |
|
1591 { |
7138
|
1592 WARN_OBSOLETE ("gnuplot_command_plot"); |
|
1593 |
5790
|
1594 return SET_INTERNAL_VARIABLE (gnuplot_command_plot); |
|
1595 } |
|
1596 |
5794
|
1597 DEFUN_DLD (gnuplot_command_replot, args, nargout, |
5790
|
1598 "-*- texinfo -*-\n\ |
|
1599 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_replot ()\n\ |
|
1600 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_replot (@var{new_val})\n\ |
7117
|
1601 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1602 of Octave.}\n\ |
5790
|
1603 @end deftypefn") |
|
1604 { |
7138
|
1605 WARN_OBSOLETE ("gnuplot_command_replot"); |
|
1606 |
5790
|
1607 return SET_INTERNAL_VARIABLE (gnuplot_command_replot); |
|
1608 } |
|
1609 |
5794
|
1610 DEFUN_DLD (gnuplot_command_splot, args, nargout, |
5790
|
1611 "-*- texinfo -*-\n\ |
|
1612 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_splot ()\n\ |
|
1613 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_splot (@var{new_val})\n\ |
7117
|
1614 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1615 of Octave.}\n\ |
5790
|
1616 @end deftypefn") |
|
1617 { |
7138
|
1618 WARN_OBSOLETE ("gnuplot_command_splot"); |
|
1619 |
5790
|
1620 return SET_INTERNAL_VARIABLE (gnuplot_command_splot); |
|
1621 } |
|
1622 |
5794
|
1623 DEFUN_DLD (gnuplot_command_using, args, nargout, |
5790
|
1624 "-*- texinfo -*-\n\ |
|
1625 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_using ()\n\ |
|
1626 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_using (@var{new_val})\n\ |
7117
|
1627 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1628 of Octave.}\n\ |
5790
|
1629 @end deftypefn") |
|
1630 { |
7138
|
1631 WARN_OBSOLETE ("gnuplot_command_using"); |
|
1632 |
5790
|
1633 return SET_INTERNAL_VARIABLE (gnuplot_command_using); |
|
1634 } |
|
1635 |
5794
|
1636 DEFUN_DLD (gnuplot_command_with, args, nargout, |
5790
|
1637 "-*- texinfo -*-\n\ |
|
1638 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_with ()\n\ |
|
1639 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_with (@var{new_val})\n\ |
7117
|
1640 of Octave.}\n\ |
5790
|
1641 @end deftypefn") |
|
1642 { |
7138
|
1643 WARN_OBSOLETE ("gnuplot_command_with"); |
|
1644 |
5790
|
1645 return SET_INTERNAL_VARIABLE (gnuplot_command_with); |
|
1646 } |
|
1647 |
5794
|
1648 DEFUN_DLD (gnuplot_command_axes, args, nargout, |
5790
|
1649 "-*- texinfo -*-\n\ |
|
1650 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_axes ()\n\ |
|
1651 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_axes (@var{new_val})\n\ |
7117
|
1652 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1653 of Octave.}\n\ |
5790
|
1654 @end deftypefn") |
|
1655 { |
7138
|
1656 WARN_OBSOLETE ("gnuplot_command_axes"); |
|
1657 |
5790
|
1658 return SET_INTERNAL_VARIABLE (gnuplot_command_axes); |
|
1659 } |
|
1660 |
5794
|
1661 DEFUN_DLD (gnuplot_command_title, args, nargout, |
5790
|
1662 "-*- texinfo -*-\n\ |
|
1663 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_title ()\n\ |
|
1664 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_title (@var{new_val})\n\ |
7117
|
1665 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1666 of Octave.}\n\ |
5790
|
1667 @end deftypefn") |
|
1668 { |
7138
|
1669 WARN_OBSOLETE ("gnuplot_command_title"); |
|
1670 |
5790
|
1671 return SET_INTERNAL_VARIABLE (gnuplot_command_title); |
|
1672 } |
|
1673 |
5794
|
1674 DEFUN_DLD (gnuplot_command_end, args, nargout, |
5790
|
1675 "-*- texinfo -*-\n\ |
|
1676 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_end ()\n\ |
|
1677 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_end (@var{new_val})\n\ |
7117
|
1678 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1679 of Octave.}\n\ |
|
1680 \n\ |
5790
|
1681 @end deftypefn") |
|
1682 { |
7138
|
1683 WARN_OBSOLETE ("gnuplot_command_end"); |
|
1684 |
5790
|
1685 return SET_INTERNAL_VARIABLE (gnuplot_command_end); |
|
1686 } |
|
1687 |
5794
|
1688 DEFUN_DLD (gnuplot_use_title_option, args, nargout, |
5790
|
1689 "-*- texinfo -*-\n\ |
|
1690 @deftypefn {Loadable Function} {@var{val} =} gnuplot_use_title_option ()\n\ |
|
1691 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_use_title_option (@var{new_val})\n\ |
|
1692 If enabled, append @samp{-title \"Figure NN\"} to the gnuplot command.\n\ |
|
1693 By default, this feature is enabled if the @code{DISPLAY} environment\n\ |
|
1694 variable is set when Octave starts.\n\ |
7117
|
1695 \n\ |
|
1696 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1697 of Octave.}\n\ |
5790
|
1698 @end deftypefn") |
|
1699 { |
7138
|
1700 WARN_OBSOLETE ("gnuplot_command_use_title_option"); |
|
1701 |
5790
|
1702 return SET_INTERNAL_VARIABLE (gnuplot_use_title_option); |
|
1703 } |
|
1704 |
6030
|
1705 DEFUN_DLD (__clear_plot_window__, , , |
5790
|
1706 "-*- texinfo -*-\n\ |
|
1707 @deftypefn {Loadable Function} {} clearplot\n\ |
|
1708 @deftypefnx {Loadable Function} {} clg\n\ |
6030
|
1709 Clear the plot window and any titles or axis labels.\n\ |
7117
|
1710 \n\ |
|
1711 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1712 of Octave.}\n\ |
5790
|
1713 @end deftypefn") |
|
1714 { |
7138
|
1715 WARN_OBSOLETE ("__clear_plot_window__"); |
|
1716 |
5790
|
1717 gnuplot::clear (); |
|
1718 |
|
1719 return octave_value_list (); |
|
1720 } |
|
1721 |
|
1722 DEFUN_DLD (closeplot, , , |
|
1723 "-*- texinfo -*-\n\ |
|
1724 @deftypefn {Loadable Function} {} closeplot\n\ |
7117
|
1725 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1726 of Octave.}\n\ |
|
1727 \n\ |
5790
|
1728 Close stream to the @code{gnuplot} subprocess. If you are using X11,\n\ |
|
1729 this will close the plot window.\n\ |
7117
|
1730 @seealso{close}\n\ |
5790
|
1731 @end deftypefn") |
|
1732 { |
7138
|
1733 WARN_OBSOLETE ("closeplot"); |
|
1734 |
5790
|
1735 gnuplot::close (); |
|
1736 |
|
1737 return octave_value_list (); |
|
1738 } |
|
1739 |
|
1740 DEFUN_DLD (purge_tmp_files, , , |
|
1741 "-*- texinfo -*-\n\ |
|
1742 @deftypefn {Loadable Function} {} purge_tmp_files\n\ |
|
1743 Delete the temporary files created by the plotting commands.\n\ |
|
1744 \n\ |
|
1745 Octave creates temporary data files for @code{gnuplot} and then sends\n\ |
|
1746 commands to @code{gnuplot} through a pipe. Octave will delete the\n\ |
|
1747 temporary files on exit, but if you are doing a lot of plotting you may\n\ |
|
1748 want to clean up in the middle of a session.\n\ |
|
1749 \n\ |
7117
|
1750 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1751 of Octave.}\n\ |
5790
|
1752 @end deftypefn") |
|
1753 { |
7138
|
1754 WARN_OBSOLETE ("purge_tmp_files"); |
|
1755 |
5790
|
1756 gnuplot::cleanup_tmp_files (); |
|
1757 |
|
1758 return octave_value_list (); |
|
1759 } |
|
1760 |
|
1761 DEFUN_DLD (__gnuplot_raw__, args, , |
|
1762 "-*- texinfo -*-\n\ |
|
1763 @deftypefn {Loadable Function} {} __gnuplot_raw__ (@var{string})\n\ |
|
1764 Send @var{string} directly to gnuplot subprocess.\n\ |
7117
|
1765 \n\ |
|
1766 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1767 of Octave.}\n\ |
5790
|
1768 @end deftypefn") |
|
1769 { |
|
1770 if (args.length () == 1 && args(0).is_string ()) |
|
1771 { |
7141
|
1772 WARN_OBSOLETE ("__gnuplot_raw__"); |
|
1773 |
5790
|
1774 std::string cmd = args(0).string_value (); |
|
1775 |
|
1776 gnuplot::send_raw (cmd); |
|
1777 } |
|
1778 else |
5823
|
1779 print_usage (); |
5790
|
1780 |
|
1781 return octave_value_list (); |
|
1782 } |
|
1783 |
6171
|
1784 DEFUN_DLD (__gnuplot_send_inline_data__, args, , |
|
1785 "-*- texinfo -*-\n\ |
6180
|
1786 @deftypefn {Loadable Function} {} __gnuplot_send_inline_data__ (@var{data}, @var{ndim}, @var{parametric})\n\ |
6171
|
1787 Send @var{val} to gnuplot subprocess as inline data.\n\ |
7117
|
1788 \n\ |
|
1789 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1790 of Octave.}\n\ |
6171
|
1791 @end deftypefn") |
|
1792 { |
|
1793 octave_value retval; |
|
1794 |
|
1795 int nargin = args.length (); |
|
1796 |
|
1797 if (nargin > 0 && nargin < 4) |
|
1798 { |
7141
|
1799 WARN_OBSOLETE ("__gnuplot_send_inline_data__"); |
|
1800 |
6171
|
1801 int ndim = 2; |
|
1802 bool parametric = false; |
|
1803 |
|
1804 if (nargin > 1) |
|
1805 { |
|
1806 ndim = args(1).int_value (); |
|
1807 |
|
1808 if (! error_state) |
|
1809 { |
|
1810 if (nargin > 2) |
|
1811 parametric = args(2).bool_value (); |
|
1812 } |
|
1813 } |
|
1814 |
|
1815 if (! error_state) |
|
1816 gnuplot::send_inline_data (args(0), ndim, parametric); |
|
1817 } |
|
1818 else |
|
1819 print_usage (); |
|
1820 |
|
1821 return retval; |
|
1822 } |
|
1823 |
5790
|
1824 DEFUN_DLD (__gnuplot_set__, args, , |
|
1825 "-*- texinfo -*-\n\ |
5832
|
1826 @deffn {Loadable Function} __gnuplot_set__ options\n\ |
5790
|
1827 Set plotting options for gnuplot\n\ |
7117
|
1828 \n\ |
|
1829 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1830 of Octave.}\n\ |
5790
|
1831 @end deffn") |
|
1832 { |
7138
|
1833 WARN_OBSOLETE ("__gnuplot_set__"); |
|
1834 |
5790
|
1835 string_vector argv = args.make_argv ("set"); |
|
1836 |
|
1837 if (! error_state) |
|
1838 gnuplot::set (argv); |
|
1839 |
|
1840 return octave_value_list (); |
|
1841 } |
|
1842 |
|
1843 DEFUN_DLD (__gnuplot_show__, args, , |
|
1844 "-*- texinfo -*-\n\ |
5832
|
1845 @deffn {Loadable Function} __gnuplot_show__ options\n\ |
5790
|
1846 Show plotting options.\n\ |
7117
|
1847 \n\ |
|
1848 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1849 of Octave.}\n\ |
5790
|
1850 @end deffn") |
|
1851 { |
7138
|
1852 WARN_OBSOLETE ("__gnuplot_show__"); |
|
1853 |
5790
|
1854 string_vector argv = args.make_argv ("show"); |
|
1855 |
|
1856 if (! error_state) |
|
1857 gnuplot::show (argv); |
|
1858 |
|
1859 return octave_value_list (); |
|
1860 } |
|
1861 |
|
1862 DEFUN_DLD (__gnuplot_plot__, args, , |
7117
|
1863 "-*- texinfo -*-\n\ |
|
1864 Plot with gnuplot.\n\ |
|
1865 \n\ |
|
1866 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1867 of Octave.}") |
5790
|
1868 { |
7138
|
1869 WARN_OBSOLETE ("__gnuplot_plot__"); |
|
1870 |
5790
|
1871 string_vector argv = args.make_argv ("plot"); |
|
1872 |
|
1873 if (! error_state) |
|
1874 gnuplot::plot (argv); |
|
1875 |
|
1876 return octave_value_list (); |
|
1877 } |
|
1878 |
|
1879 DEFUN_DLD (__gnuplot_splot__, args, , |
7117
|
1880 "-*- texinfo -*-\n\ |
|
1881 Plot with gnuplot.\n\ |
|
1882 \n\ |
|
1883 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1884 of Octave.}") |
5790
|
1885 { |
7138
|
1886 WARN_OBSOLETE ("__gnuplot_splot__"); |
|
1887 |
5790
|
1888 string_vector argv = args.make_argv ("splot"); |
|
1889 |
|
1890 if (! error_state) |
|
1891 gnuplot::plot (argv); |
|
1892 |
|
1893 return octave_value_list (); |
|
1894 } |
|
1895 |
|
1896 DEFUN_DLD (__gnuplot_replot__, args, , |
7117
|
1897 "-*- texinfo -*-\n\ |
|
1898 Plot with gnuplot.\n\ |
|
1899 \n\ |
|
1900 @strong{This function is obsolete and will be removed from a future version\n\ |
|
1901 of Octave.}") |
5790
|
1902 { |
7138
|
1903 WARN_OBSOLETE ("__gnuplot_replot__"); |
|
1904 |
5790
|
1905 string_vector argv = args.make_argv ("replot"); |
|
1906 |
|
1907 if (! error_state) |
|
1908 gnuplot::plot (argv); |
|
1909 |
|
1910 return octave_value_list (); |
|
1911 } |
|
1912 |
|
1913 /* |
|
1914 ;;; Local Variables: *** |
|
1915 ;;; mode: C++ *** |
|
1916 ;;; End: *** |
|
1917 */ |