1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1343
|
31 #include <cstring> |
|
32 |
1728
|
33 #include <string> |
|
34 |
1350
|
35 #include <fstream.h> |
|
36 #include <iostream.h> |
|
37 #include <strstream.h> |
|
38 |
|
39 #ifdef HAVE_UNISTD_H |
2442
|
40 #ifdef HAVE_SYS_TYPES_H |
524
|
41 #include <sys/types.h> |
2442
|
42 #endif |
524
|
43 #include <unistd.h> |
|
44 #endif |
1
|
45 |
524
|
46 #include "SLStack.h" |
|
47 #include "procstream.h" |
|
48 |
1802
|
49 #include "file-ops.h" |
2891
|
50 #include "lo-mappers.h" |
1755
|
51 #include "str-vec.h" |
|
52 |
1352
|
53 #include "defun.h" |
|
54 #include "error.h" |
|
55 #include "gripes.h" |
|
56 #include "load-save.h" |
1742
|
57 #include "oct-obj.h" |
|
58 #include "pt-cmd.h" |
|
59 #include "pt-exp.h" |
|
60 #include "pt-plot.h" |
2124
|
61 #include "pt-walk.h" |
2209
|
62 #include "sighandlers.h" |
1750
|
63 #include "sysdep.h" |
1
|
64 #include "utils.h" |
2371
|
65 #include "variables.h" |
1
|
66 |
2176
|
67 // If TRUE, a replot command is issued automatically each time a plot |
|
68 // changes in some way. |
|
69 static bool Vautomatic_replot; |
|
70 |
|
71 // The name of the shell command to execute to start gnuplot. |
|
72 static string Vgnuplot_binary; |
|
73 |
2289
|
74 // TRUE if gnuplot appears to support multiple plot windows with X11. |
|
75 static bool Vgnuplot_has_frames; |
|
76 |
2176
|
77 // TRUE if gnuplot appears to support multiplot. |
|
78 static bool Vgnuplot_has_multiplot; |
|
79 |
1358
|
80 // The number of lines we've plotted so far. |
735
|
81 static int plot_line_count = 0; |
1
|
82 |
|
83 // Is this a parametric plot? Makes a difference for 3D plotting. |
1827
|
84 static bool parametric_plot = false; |
1
|
85 |
943
|
86 // The gnuplot terminal type. |
|
87 static char *gnuplot_terminal_type = 0; |
|
88 |
478
|
89 // Should the graph window be cleared before plotting the next line? |
1827
|
90 static bool clear_before_plotting = true; |
478
|
91 |
524
|
92 // List of files to delete when we exit or crash. |
2614
|
93 // |
|
94 // XXX FIXME XXX -- this should really be static, but that causes |
|
95 // problems on some systems. |
|
96 SLStack <string> tmp_files; |
524
|
97 |
|
98 // Pipe to gnuplot. |
1449
|
99 static oprocstream *plot_stream = 0; |
524
|
100 |
2211
|
101 // ID of the plotter process. |
|
102 static pid_t plot_stream_pid = 0; |
|
103 |
3162
|
104 // Gnuplot command strings that we use. |
|
105 static string Vgnuplot_command_plot; |
|
106 static string Vgnuplot_command_replot; |
|
107 static string Vgnuplot_command_splot; |
|
108 static string Vgnuplot_command_using; |
|
109 static string Vgnuplot_command_with; |
3165
|
110 static string Vgnuplot_command_axes; |
3162
|
111 static string Vgnuplot_command_title; |
|
112 static string Vgnuplot_command_end; |
866
|
113 |
597
|
114 static void |
2209
|
115 plot_stream_death_handler (pid_t pid, int) |
|
116 { |
|
117 close_plot_stream (); |
|
118 |
|
119 warning ("connection to external plotter (pid = %d) lost --", pid); |
|
120 warning ("please try your plot command(s) again"); |
|
121 } |
|
122 |
|
123 static void |
597
|
124 open_plot_stream (void) |
|
125 { |
1827
|
126 static bool initialized = false; |
597
|
127 |
1449
|
128 if (plot_stream && ! *plot_stream) |
|
129 { |
|
130 delete plot_stream; |
|
131 plot_stream = 0; |
|
132 } |
|
133 |
|
134 if (! plot_stream) |
597
|
135 { |
1827
|
136 initialized = false; |
943
|
137 |
597
|
138 plot_line_count = 0; |
|
139 |
2176
|
140 string plot_prog = Vgnuplot_binary; |
1755
|
141 |
2209
|
142 if (plot_prog.empty ()) |
|
143 plot_prog = "gnuplot"; |
|
144 |
|
145 // XXX FIXME XXX -- I'm not sure this is the right thing to do, |
|
146 // but without it, C-c at the octave prompt will kill gnuplot... |
|
147 |
2211
|
148 #if defined (HAVE_POSIX_SIGNALS) |
|
149 sigset_t set, oset; |
|
150 sigemptyset (&set); |
|
151 sigaddset (&set, SIGINT); |
|
152 sigprocmask (SIG_BLOCK, &set, &oset); |
2553
|
153 #else |
2705
|
154 volatile octave_interrupt_handler old_interrupt_handler |
2553
|
155 = octave_ignore_interrupts (); |
2211
|
156 #endif |
2209
|
157 |
|
158 plot_stream = new oprocstream (plot_prog.c_str ()); |
|
159 |
|
160 if (plot_stream) |
597
|
161 { |
2209
|
162 if (! *plot_stream) |
1449
|
163 { |
|
164 delete plot_stream; |
|
165 plot_stream = 0; |
2209
|
166 |
|
167 error ("plot: unable to open pipe to `%s'", plot_prog.c_str ()); |
1449
|
168 } |
2209
|
169 else |
597
|
170 { |
2211
|
171 plot_stream_pid = plot_stream->pid (); |
|
172 octave_child_list::insert (plot_stream_pid, |
|
173 plot_stream_death_handler); |
597
|
174 } |
|
175 } |
|
176 else |
2209
|
177 error ("plot: unable to open pipe to `%s'", plot_prog.c_str ()); |
2211
|
178 |
|
179 #if defined (HAVE_POSIX_SIGNALS) |
|
180 sigprocmask (SIG_SETMASK, &oset, 0); |
2553
|
181 #else |
|
182 octave_set_interrupt_handler (old_interrupt_handler); |
2211
|
183 #endif |
597
|
184 } |
|
185 |
1449
|
186 if (! error_state && plot_stream && *plot_stream && ! initialized) |
597
|
187 { |
1827
|
188 initialized = true; |
1449
|
189 *plot_stream << "set data style lines\n"; |
943
|
190 |
|
191 if (gnuplot_terminal_type) |
3162
|
192 *plot_stream << "set term " << gnuplot_terminal_type |
|
193 << Vgnuplot_command_end; |
597
|
194 } |
|
195 } |
|
196 |
|
197 static int |
|
198 send_to_plot_stream (const char *cmd) |
|
199 { |
1449
|
200 if (! (plot_stream && *plot_stream)) |
597
|
201 { |
|
202 open_plot_stream (); |
|
203 |
|
204 if (error_state) |
|
205 return -1; |
|
206 } |
|
207 |
3162
|
208 int replot_len = Vgnuplot_command_replot.length (); |
|
209 int splot_len = Vgnuplot_command_splot.length (); |
|
210 int plot_len = Vgnuplot_command_plot.length (); |
930
|
211 |
3162
|
212 bool is_replot = (Vgnuplot_command_replot.compare (cmd, 0, replot_len) == 0); |
|
213 bool is_splot = (Vgnuplot_command_splot.compare (cmd, 0, splot_len) == 0); |
|
214 bool is_plot = (Vgnuplot_command_plot.compare (cmd, 0, plot_len) == 0); |
661
|
215 |
|
216 if (plot_line_count == 0 && is_replot) |
597
|
217 error ("replot: no previous plot"); |
|
218 else |
|
219 { |
1449
|
220 *plot_stream << cmd; |
930
|
221 |
661
|
222 if (! (is_replot || is_splot || is_plot) |
|
223 && plot_line_count > 0 |
2176
|
224 && Vautomatic_replot) |
3162
|
225 *plot_stream << Vgnuplot_command_replot << Vgnuplot_command_end; |
930
|
226 |
1449
|
227 plot_stream->flush (); |
597
|
228 } |
|
229 |
|
230 return 0; |
|
231 } |
|
232 |
581
|
233 // Plotting, eh? |
1
|
234 |
|
235 tree_plot_command::~tree_plot_command (void) |
|
236 { |
|
237 delete range; |
|
238 delete plot_list; |
|
239 } |
|
240 |
578
|
241 void |
|
242 tree_plot_command::eval (void) |
1
|
243 { |
143
|
244 if (error_state) |
578
|
245 return; |
143
|
246 |
597
|
247 open_plot_stream (); |
|
248 |
1
|
249 ostrstream plot_buf; |
|
250 |
|
251 switch (ndim) |
|
252 { |
476
|
253 case 1: |
478
|
254 if (plot_line_count == 0) |
|
255 { |
|
256 if (plot_list) |
3162
|
257 plot_buf << Vgnuplot_command_plot; |
478
|
258 else |
|
259 { |
|
260 ::error ("replot: must have something to plot"); |
578
|
261 return; |
478
|
262 } |
|
263 } |
|
264 else |
3162
|
265 plot_buf << Vgnuplot_command_replot; |
476
|
266 break; |
777
|
267 |
1
|
268 case 2: |
478
|
269 if (clear_before_plotting || plot_line_count == 0) |
|
270 { |
|
271 plot_line_count = 0; |
3162
|
272 plot_buf << Vgnuplot_command_plot; |
478
|
273 } |
|
274 else |
3162
|
275 plot_buf << Vgnuplot_command_replot; |
1
|
276 break; |
777
|
277 |
1
|
278 case 3: |
930
|
279 if (clear_before_plotting || plot_line_count == 0) |
|
280 { |
|
281 plot_line_count = 0; |
3162
|
282 plot_buf << Vgnuplot_command_splot; |
930
|
283 } |
|
284 else |
3162
|
285 plot_buf << Vgnuplot_command_replot; |
1
|
286 break; |
777
|
287 |
1
|
288 default: |
771
|
289 gripe_2_or_3_dim_plot (); |
|
290 return; |
1
|
291 } |
|
292 |
524
|
293 if (range) |
478
|
294 { |
|
295 if (plot_line_count == 0) |
|
296 range->print (ndim, plot_buf); |
|
297 else |
|
298 warning ("can't specify new plot ranges with `replot' or while\ |
|
299 hold is on"); |
|
300 } |
1
|
301 |
191
|
302 if (error_state) |
578
|
303 return; |
191
|
304 |
591
|
305 if (plot_list) |
1
|
306 { |
591
|
307 int status = plot_list->print (ndim, plot_buf); |
578
|
308 |
771
|
309 if (error_state || status < 0) |
578
|
310 return; |
1
|
311 } |
|
312 |
3162
|
313 plot_buf << Vgnuplot_command_end << ends; |
1
|
314 |
1358
|
315 // Just testing... |
|
316 // char *message = plot_buf.str (); |
|
317 // cout << "[*]" << message << "[*]\n"; |
1
|
318 |
|
319 if (parametric_plot && ndim == 2) |
|
320 { |
|
321 warning ("can't make 2D parametric plot -- setting noparametric..."); |
|
322 send_to_plot_stream ("set noparametric\n"); |
|
323 char *message = plot_buf.str (); |
|
324 send_to_plot_stream (message); |
|
325 delete [] message; |
|
326 send_to_plot_stream ("set parametric\n"); |
|
327 } |
|
328 else |
|
329 { |
|
330 char *message = plot_buf.str (); |
|
331 send_to_plot_stream (message); |
|
332 delete [] message; |
|
333 } |
|
334 } |
|
335 |
591
|
336 void |
2124
|
337 tree_plot_command::accept (tree_walker& tw) |
1
|
338 { |
2124
|
339 tw.visit_plot_command (*this); |
1
|
340 } |
|
341 |
578
|
342 plot_limits::~plot_limits (void) |
1
|
343 { |
|
344 delete x_range; |
|
345 delete y_range; |
|
346 delete z_range; |
|
347 } |
|
348 |
|
349 void |
578
|
350 plot_limits::print (int ndim, ostrstream& plot_buf) |
1
|
351 { |
|
352 if (ndim == 2 || ndim == 3) |
|
353 { |
524
|
354 if (x_range) |
1
|
355 x_range->print (plot_buf); |
|
356 else |
|
357 return; |
|
358 |
524
|
359 if (y_range) |
1
|
360 y_range->print (plot_buf); |
|
361 else |
|
362 return; |
|
363 } |
|
364 |
524
|
365 if (ndim == 3 && z_range) |
1
|
366 z_range->print (plot_buf); |
|
367 } |
|
368 |
591
|
369 void |
2124
|
370 plot_limits::accept (tree_walker& tw) |
591
|
371 { |
2124
|
372 tw.visit_plot_limits (*this); |
591
|
373 } |
|
374 |
578
|
375 plot_range::~plot_range (void) |
1
|
376 { |
|
377 delete lower; |
|
378 delete upper; |
|
379 } |
|
380 |
|
381 void |
578
|
382 plot_range::print (ostrstream& plot_buf) |
1
|
383 { |
|
384 plot_buf << " ["; |
|
385 |
524
|
386 if (lower) |
1
|
387 { |
2972
|
388 octave_value lower_val = lower->rvalue (); |
|
389 |
191
|
390 if (error_state) |
|
391 { |
240
|
392 ::error ("evaluating lower bound of plot range"); |
191
|
393 return; |
|
394 } |
|
395 else |
|
396 { |
629
|
397 double lo = lower_val.double_value (); |
191
|
398 plot_buf << lo; |
|
399 } |
1
|
400 } |
|
401 |
|
402 plot_buf << ":"; |
|
403 |
524
|
404 if (upper) |
1
|
405 { |
2972
|
406 octave_value upper_val = upper->rvalue (); |
|
407 |
191
|
408 if (error_state) |
|
409 { |
240
|
410 ::error ("evaluating upper bound of plot range"); |
191
|
411 return; |
|
412 } |
|
413 else |
|
414 { |
629
|
415 double hi = upper_val.double_value (); |
191
|
416 plot_buf << hi; |
|
417 } |
1
|
418 } |
|
419 |
|
420 plot_buf << "]"; |
|
421 } |
|
422 |
591
|
423 void |
2124
|
424 plot_range::accept (tree_walker& tw) |
591
|
425 { |
2124
|
426 tw.visit_plot_range (*this); |
591
|
427 } |
|
428 |
578
|
429 subplot_using::~subplot_using (void) |
1
|
430 { |
|
431 delete scanf_fmt; |
|
432 } |
|
433 |
|
434 int |
872
|
435 subplot_using::eval (int ndim, int n_max) |
1
|
436 { |
2124
|
437 if ((ndim == 2 && qual_count > 4) |
|
438 || (ndim == 3 && qual_count > 3)) |
1
|
439 return -1; |
|
440 |
2124
|
441 if (qual_count > 0) |
|
442 val.resize (qual_count); |
872
|
443 |
2124
|
444 for (int i = 0; i < qual_count; i++) |
1
|
445 { |
524
|
446 if (x[i]) |
1
|
447 { |
2972
|
448 octave_value tmp = x[i]->rvalue (); |
|
449 |
191
|
450 if (error_state) |
|
451 { |
240
|
452 ::error ("evaluating plot using command"); |
191
|
453 return -1; |
|
454 } |
|
455 |
872
|
456 double val_tmp; |
1
|
457 if (tmp.is_defined ()) |
|
458 { |
872
|
459 val_tmp = tmp.double_value (); |
1
|
460 |
872
|
461 if (error_state) |
|
462 return -1; |
1
|
463 |
1086
|
464 if (xisnan (val_tmp)) |
|
465 { |
|
466 ::error ("NaN is invalid as a column specifier"); |
|
467 return -1; |
|
468 } |
|
469 |
872
|
470 int n = NINT (val_tmp); |
1086
|
471 |
322
|
472 if (n < 1 || n_max > 0 && n > n_max) |
1
|
473 { |
240
|
474 ::error ("using: column %d out of range", n); |
1
|
475 return -1; |
|
476 } |
|
477 else |
2305
|
478 val (i) = n; |
1
|
479 } |
|
480 else |
|
481 return -1; |
|
482 } |
|
483 else |
|
484 return -1; |
|
485 } |
|
486 |
524
|
487 if (scanf_fmt) |
1
|
488 warning ("ignoring scanf format in plot command"); |
|
489 |
872
|
490 return 0; |
|
491 } |
|
492 |
|
493 ColumnVector |
|
494 subplot_using::values (int ndim, int n_max) |
|
495 { |
|
496 int status = eval (ndim, n_max); |
|
497 |
1203
|
498 if (status < 0) |
872
|
499 return -1; |
|
500 |
|
501 return val; |
|
502 } |
|
503 |
|
504 int |
|
505 subplot_using::print (int ndim, int n_max, ostrstream& plot_buf) |
|
506 { |
|
507 int status = eval (ndim, n_max); |
|
508 |
1203
|
509 if (status < 0) |
872
|
510 return -1; |
|
511 |
2124
|
512 for (int i = 0; i < qual_count; i++) |
872
|
513 { |
|
514 if (i == 0) |
3162
|
515 plot_buf << " " << Vgnuplot_command_using << " "; |
872
|
516 else |
|
517 plot_buf << ":"; |
|
518 |
2305
|
519 plot_buf << val (i); |
872
|
520 } |
|
521 |
1
|
522 return 0; |
|
523 } |
|
524 |
591
|
525 void |
2124
|
526 subplot_using::accept (tree_walker& tw) |
1
|
527 { |
2124
|
528 tw.visit_subplot_using (*this); |
1
|
529 } |
|
530 |
578
|
531 subplot_style::~subplot_style (void) |
1622
|
532 { |
2124
|
533 delete sp_linetype; |
|
534 delete sp_pointtype; |
1
|
535 } |
|
536 |
|
537 int |
578
|
538 subplot_style::print (ostrstream& plot_buf) |
1
|
539 { |
2124
|
540 if (! sp_style.empty ()) |
1
|
541 { |
3162
|
542 plot_buf << " " << Vgnuplot_command_with << " " << sp_style; |
1
|
543 |
2124
|
544 if (sp_linetype) |
1
|
545 { |
2972
|
546 octave_value tmp = sp_linetype->rvalue (); |
|
547 |
191
|
548 if (! error_state && tmp.is_defined ()) |
1
|
549 { |
629
|
550 double val = tmp.double_value (); |
1086
|
551 if (xisnan (val)) |
|
552 { |
|
553 ::error ("NaN is invalid a plotting line style"); |
|
554 return -1; |
|
555 } |
|
556 else |
|
557 plot_buf << " " << NINT (val); |
1
|
558 } |
|
559 else |
191
|
560 { |
240
|
561 ::error ("evaluating plot style command"); |
191
|
562 return -1; |
|
563 } |
1
|
564 } |
|
565 |
2124
|
566 if (sp_pointtype) |
1
|
567 { |
2972
|
568 octave_value tmp = sp_pointtype->rvalue (); |
|
569 |
191
|
570 if (! error_state && tmp.is_defined ()) |
1
|
571 { |
629
|
572 double val = tmp.double_value (); |
1086
|
573 if (xisnan (val)) |
|
574 { |
|
575 ::error ("NaN is invalid a plotting point style"); |
|
576 return -1; |
|
577 } |
|
578 else |
|
579 plot_buf << " " << NINT (val); |
1
|
580 } |
|
581 else |
191
|
582 { |
240
|
583 ::error ("evaluating plot style command"); |
191
|
584 return -1; |
|
585 } |
1
|
586 } |
|
587 } |
|
588 else |
|
589 return -1; |
|
590 |
|
591 return 0; |
|
592 } |
|
593 |
2543
|
594 bool |
|
595 subplot_style::columns_ok (int nc) |
872
|
596 { |
2543
|
597 bool retval = true; |
|
598 |
|
599 if ((almost_match ("boxes", sp_style, 5, 0) && nc != 3) |
|
600 || (almost_match ("boxerrorbars", sp_style, 5, 0) |
|
601 && (! (nc == 3 || nc == 4 || nc == 5))) |
|
602 || ((almost_match ("boxxyerrorbars", sp_style, 4, 0) |
|
603 || almost_match ("xyerrorbars", sp_style, 2, 0)) |
|
604 && (! (nc == 4 || nc == 6 || nc == 7))) |
|
605 || ((almost_match ("candlesticks", sp_style, 1, 0) |
|
606 || almost_match ("financebars", sp_style, 2, 0)) |
|
607 && nc != 5) |
|
608 || ((almost_match ("errorbars", sp_style, 1, 0) |
|
609 || almost_match ("xerrorbars", sp_style, 1, 0) |
|
610 || almost_match ("yerrorbars", sp_style, 1, 0)) |
|
611 && (! (nc == 3 || nc == 4)))) |
|
612 { |
|
613 error |
|
614 ("invalid number of data columns = %d specified for plot style `%s'", |
|
615 nc, sp_style.c_str ()); |
|
616 |
|
617 retval = false; |
|
618 } |
|
619 |
|
620 return retval; |
872
|
621 } |
|
622 |
591
|
623 void |
2124
|
624 subplot_style::accept (tree_walker& tw) |
591
|
625 { |
2124
|
626 tw.visit_subplot_style (*this); |
591
|
627 } |
|
628 |
3165
|
629 int |
|
630 subplot_axes::print (ostrstream& plot_buf) |
|
631 { |
|
632 if (! sp_axes.empty ()) |
|
633 plot_buf << " " << Vgnuplot_command_axes << " " << sp_axes; |
|
634 |
|
635 return 0; |
|
636 } |
|
637 |
|
638 void |
|
639 subplot_axes::accept (tree_walker& tw) |
|
640 { |
|
641 tw.visit_subplot_axes (*this); |
|
642 } |
|
643 |
878
|
644 subplot::~subplot (void) |
|
645 { |
2124
|
646 delete sp_plot_data; |
|
647 delete sp_using_clause; |
|
648 delete sp_title_clause; |
|
649 delete sp_style_clause; |
3165
|
650 delete sp_axes_clause; |
878
|
651 } |
|
652 |
2086
|
653 octave_value |
|
654 subplot::extract_plot_data (int ndim, octave_value& data) |
872
|
655 { |
2086
|
656 octave_value retval; |
872
|
657 |
2124
|
658 if (sp_using_clause) |
872
|
659 { |
2124
|
660 ColumnVector val = sp_using_clause->values (ndim); |
872
|
661 |
2086
|
662 octave_value_list args; |
2963
|
663 |
872
|
664 args(1) = val; |
2086
|
665 args(0) = octave_value::magic_colon_t; |
872
|
666 |
2963
|
667 retval = data.do_index_op (args); |
872
|
668 |
|
669 if (error_state) |
2086
|
670 return octave_value (); |
872
|
671 } |
|
672 else |
|
673 { |
|
674 retval = data; |
|
675 } |
|
676 |
2543
|
677 int nc = retval.columns (); |
872
|
678 |
2543
|
679 if (ndim == 2 && sp_style_clause && ! sp_style_clause->columns_ok (nc)) |
|
680 return octave_value (); |
872
|
681 |
|
682 return retval; |
|
683 } |
|
684 |
591
|
685 int |
872
|
686 subplot::handle_plot_data (int ndim, ostrstream& plot_buf) |
591
|
687 { |
2124
|
688 if (sp_plot_data) |
591
|
689 { |
2972
|
690 octave_value data = sp_plot_data->rvalue (); |
872
|
691 |
591
|
692 if (! error_state && data.is_defined ()) |
|
693 { |
1750
|
694 string file; |
|
695 |
610
|
696 if (data.is_string ()) |
591
|
697 { |
1358
|
698 // Should really try to look at data file to determine |
|
699 // n_max. Can't do much about other arbitrary gnuplot |
|
700 // commands though... |
872
|
701 |
|
702 int n_max = 0; |
|
703 |
2926
|
704 file = file_ops::tilde_expand (data.string_value ()); |
1728
|
705 |
1750
|
706 ifstream ftmp (file.c_str ()); |
|
707 |
591
|
708 if (ftmp) |
|
709 { |
|
710 plot_buf << " \"" << file << '"'; |
|
711 } |
|
712 else |
|
713 { |
1750
|
714 file = ""; |
591
|
715 |
1358
|
716 // Opening as a file failed. Let's try passing it |
|
717 // along as a plot command. |
|
718 |
591
|
719 plot_buf << " " << data.string_value (); |
872
|
720 } |
|
721 |
2124
|
722 if (sp_using_clause) |
872
|
723 { |
2124
|
724 int status = sp_using_clause->print (ndim, n_max, plot_buf); |
1750
|
725 |
872
|
726 if (status < 0) |
|
727 return -1; |
591
|
728 } |
|
729 } |
872
|
730 else |
591
|
731 { |
1358
|
732 // Eliminate the need for printing a using clause to |
|
733 // plot_buf. |
872
|
734 |
2086
|
735 octave_value tmp_data = extract_plot_data (ndim, data); |
872
|
736 |
|
737 if (tmp_data.is_defined ()) |
|
738 { |
|
739 switch (ndim) |
|
740 { |
|
741 case 2: |
|
742 file = save_in_tmp_file (tmp_data, ndim); |
|
743 break; |
777
|
744 |
872
|
745 case 3: |
|
746 file = save_in_tmp_file (tmp_data, ndim, |
|
747 parametric_plot); |
|
748 break; |
777
|
749 |
872
|
750 default: |
|
751 gripe_2_or_3_dim_plot (); |
|
752 break; |
|
753 } |
591
|
754 |
1750
|
755 if (file.length () > 0) |
872
|
756 { |
|
757 mark_for_deletion (file); |
|
758 plot_buf << " \"" << file << '"'; |
|
759 } |
|
760 } |
591
|
761 } |
|
762 } |
|
763 else |
|
764 return -1; |
|
765 } |
|
766 else |
|
767 return -1; |
|
768 |
872
|
769 return 0; |
|
770 } |
591
|
771 |
872
|
772 int |
|
773 subplot::print (int ndim, ostrstream& plot_buf) |
|
774 { |
|
775 int status = handle_plot_data (ndim, plot_buf); |
|
776 |
|
777 if (status < 0) |
|
778 return -1; |
591
|
779 |
3165
|
780 if (sp_axes_clause) |
|
781 { |
|
782 int status = sp_axes_clause->print (plot_buf); |
|
783 if (status < 0) |
|
784 return -1; |
|
785 } |
|
786 |
2124
|
787 if (sp_title_clause) |
591
|
788 { |
2972
|
789 octave_value tmp = sp_title_clause->rvalue (); |
|
790 |
610
|
791 if (! error_state && tmp.is_string ()) |
3162
|
792 plot_buf << " " << Vgnuplot_command_title << " " |
866
|
793 << '"' << tmp.string_value () << '"'; |
591
|
794 else |
|
795 { |
|
796 warning ("line title must be a string"); |
3162
|
797 plot_buf << " " << Vgnuplot_command_title << " " |
866
|
798 << '"' << "line " << plot_line_count << '"'; |
591
|
799 } |
|
800 } |
|
801 else |
3162
|
802 plot_buf << " " << Vgnuplot_command_title << " " |
866
|
803 << '"' << "line " << plot_line_count << '"'; |
591
|
804 |
2124
|
805 if (sp_style_clause) |
591
|
806 { |
2124
|
807 int status = sp_style_clause->print (plot_buf); |
591
|
808 if (status < 0) |
|
809 return -1; |
|
810 } |
|
811 |
|
812 return 0; |
|
813 } |
|
814 |
|
815 void |
2124
|
816 subplot::accept (tree_walker& tw) |
591
|
817 { |
2124
|
818 tw.visit_subplot (*this); |
591
|
819 } |
|
820 |
1622
|
821 subplot_list::~subplot_list (void) |
|
822 { |
|
823 while (! empty ()) |
|
824 { |
|
825 subplot *t = remove_front (); |
|
826 delete t; |
|
827 } |
|
828 } |
|
829 |
883
|
830 int |
591
|
831 subplot_list::print (int ndim, ostrstream& plot_buf) |
|
832 { |
|
833 int status = 0; |
|
834 |
|
835 for (Pix p = first (); p != 0; next (p)) |
|
836 { |
|
837 subplot *elt = this->operator () (p); |
|
838 |
|
839 plot_line_count++; |
|
840 |
|
841 if (p != first ()) |
|
842 plot_buf << ",\\\n "; |
|
843 |
|
844 status = elt->print (ndim, plot_buf); |
|
845 |
|
846 if (status < 0) |
|
847 break; |
|
848 } |
|
849 |
|
850 return status; |
|
851 } |
|
852 |
|
853 void |
2124
|
854 subplot_list::accept (tree_walker& tw) |
591
|
855 { |
2124
|
856 tw.visit_subplot_list (*this); |
591
|
857 } |
|
858 |
1755
|
859 string |
2086
|
860 save_in_tmp_file (octave_value& t, int ndim, bool parametric) |
524
|
861 { |
2936
|
862 string name = file_ops::tempnam ("", "oct-"); |
1755
|
863 |
|
864 if (! name.empty ()) |
524
|
865 { |
1755
|
866 ofstream file (name.c_str ()); |
|
867 |
524
|
868 if (file) |
|
869 { |
|
870 switch (ndim) |
|
871 { |
|
872 case 2: |
2799
|
873 save_ascii_data (file, t, name, true); |
524
|
874 break; |
777
|
875 |
524
|
876 case 3: |
607
|
877 save_three_d (file, t, parametric); |
524
|
878 break; |
777
|
879 |
524
|
880 default: |
771
|
881 gripe_2_or_3_dim_plot (); |
524
|
882 break; |
|
883 } |
|
884 } |
|
885 else |
|
886 { |
1755
|
887 error ("couldn't open temporary output file `%s'", name.c_str ()); |
|
888 name.resize (0); |
524
|
889 } |
|
890 } |
1755
|
891 |
524
|
892 return name; |
|
893 } |
|
894 |
|
895 void |
1750
|
896 mark_for_deletion (const string& file) |
524
|
897 { |
1750
|
898 tmp_files.push (file); |
524
|
899 } |
|
900 |
|
901 void |
|
902 cleanup_tmp_files (void) |
|
903 { |
|
904 while (! tmp_files.empty ()) |
|
905 { |
1750
|
906 string filename = tmp_files.pop (); |
|
907 unlink (filename.c_str ()); |
524
|
908 } |
|
909 } |
|
910 |
|
911 void |
|
912 close_plot_stream (void) |
|
913 { |
2211
|
914 octave_child_list::remove (plot_stream_pid); |
|
915 |
1449
|
916 if (plot_stream) |
|
917 { |
|
918 delete plot_stream; |
|
919 plot_stream = 0; |
|
920 } |
524
|
921 |
|
922 plot_line_count = 0; |
|
923 } |
|
924 |
1328
|
925 void |
1755
|
926 do_external_plotter_cd (const string& newdir) |
1328
|
927 { |
1449
|
928 if (plot_stream && *plot_stream) |
1328
|
929 { |
|
930 ostrstream plot_buf; |
3162
|
931 plot_buf << "cd \"" << newdir << "\"" << Vgnuplot_command_end << ends; |
1328
|
932 char *message = plot_buf.str (); |
|
933 send_to_plot_stream (message); |
|
934 delete [] message; |
|
935 } |
|
936 } |
|
937 |
1957
|
938 DEFUN (clearplot, , , |
883
|
939 "clearplot (): clear the plot window") |
|
940 { |
2086
|
941 octave_value_list retval; |
883
|
942 send_to_plot_stream ("clear\n"); |
930
|
943 |
1358
|
944 // XXX FIXME XXX -- instead of just clearing these things, it would |
|
945 // be nice if we could reset things to a user-specified default |
|
946 // state. |
930
|
947 |
|
948 send_to_plot_stream ("set title\n"); |
|
949 send_to_plot_stream ("set xlabel\n"); |
|
950 send_to_plot_stream ("set ylabel\n"); |
940
|
951 send_to_plot_stream ("set nogrid\n"); |
1000
|
952 send_to_plot_stream ("set nolabel\n"); |
940
|
953 |
1358
|
954 // This makes a simple `replot' not work after a `clearplot' command |
|
955 // has been issued. |
940
|
956 |
|
957 plot_line_count = 0; |
930
|
958 |
883
|
959 return retval; |
|
960 } |
|
961 |
884
|
962 DEFALIAS (clg, clearplot); |
|
963 |
1957
|
964 DEFUN (closeplot, , , |
524
|
965 "closeplot (): close the stream to plotter") |
|
966 { |
2086
|
967 octave_value_list retval; |
524
|
968 close_plot_stream (); |
|
969 return retval; |
|
970 } |
|
971 |
1957
|
972 DEFUN_TEXT (hold, args, , |
524
|
973 "hold [on|off]\n\ |
|
974 \n\ |
|
975 determine whether the plot window is cleared before the next line is\n\ |
|
976 drawn. With no argument, toggle the current state.") |
|
977 { |
2086
|
978 octave_value_list retval; |
524
|
979 |
1755
|
980 int argc = args.length () + 1; |
|
981 |
1968
|
982 string_vector argv = args.make_argv ("hold"); |
1755
|
983 |
|
984 if (error_state) |
|
985 return retval; |
524
|
986 |
|
987 switch (argc) |
|
988 { |
|
989 case 1: |
|
990 clear_before_plotting = ! clear_before_plotting; |
|
991 break; |
777
|
992 |
524
|
993 case 2: |
1755
|
994 if (argv[1] == "on") |
1827
|
995 clear_before_plotting = false; |
1755
|
996 else if (argv[1] == "off") |
1827
|
997 clear_before_plotting = true; |
524
|
998 else |
|
999 print_usage ("hold"); |
|
1000 break; |
777
|
1001 |
524
|
1002 default: |
|
1003 print_usage ("hold"); |
|
1004 break; |
|
1005 } |
|
1006 |
|
1007 return retval; |
|
1008 } |
|
1009 |
1957
|
1010 DEFUN (ishold, , , |
735
|
1011 "ishold\n\ |
|
1012 \n\ |
|
1013 Return 1 if hold is on, otherwise return 0.") |
|
1014 { |
2800
|
1015 return static_cast<double> (! clear_before_plotting); |
735
|
1016 } |
|
1017 |
1957
|
1018 DEFUN (purge_tmp_files, , , |
524
|
1019 "delete temporary data files used for plotting") |
|
1020 { |
2086
|
1021 octave_value_list retval; |
524
|
1022 cleanup_tmp_files (); |
|
1023 return retval; |
|
1024 } |
|
1025 |
3180
|
1026 |
|
1027 DEFUN (graw, args, , |
|
1028 "graw (string)\n\ |
|
1029 \n\ |
|
1030 Send STRING directly to gnuplot subprocess.") |
|
1031 { |
|
1032 octave_value_list retval; |
|
1033 |
|
1034 if (args.length () == 1 && args(0).is_string ()) |
|
1035 { |
|
1036 string cmd = args(0).string_value (); |
|
1037 |
|
1038 if (! (plot_stream && *plot_stream)) |
|
1039 open_plot_stream (); |
|
1040 |
|
1041 if (! error_state) |
|
1042 { |
|
1043 *plot_stream << cmd; |
|
1044 |
|
1045 plot_stream->flush (); |
|
1046 } |
|
1047 } |
|
1048 else |
|
1049 print_usage ("graw"); |
|
1050 |
|
1051 return retval; |
|
1052 } |
|
1053 |
2520
|
1054 DEFUN_TEXT (gset, args, , |
|
1055 "gset [options]\n\ |
524
|
1056 \n\ |
2520
|
1057 set plotting options for gnuplot") |
524
|
1058 { |
2086
|
1059 octave_value_list retval; |
524
|
1060 |
1755
|
1061 int argc = args.length () + 1; |
|
1062 |
1968
|
1063 string_vector argv = args.make_argv ("set"); |
1755
|
1064 |
|
1065 if (error_state) |
|
1066 return retval; |
524
|
1067 |
|
1068 ostrstream plot_buf; |
|
1069 |
|
1070 if (argc > 1) |
|
1071 { |
|
1072 if (almost_match ("parametric", argv[1], 3)) |
1827
|
1073 parametric_plot = true; |
524
|
1074 else if (almost_match ("noparametric", argv[1], 5)) |
1827
|
1075 parametric_plot = false; |
943
|
1076 else if (almost_match ("term", argv[1], 1)) |
|
1077 { |
|
1078 delete [] gnuplot_terminal_type; |
|
1079 ostrstream buf; |
|
1080 for (int i = 2; i < argc; i++) |
|
1081 buf << argv[i] << " "; |
3162
|
1082 buf << Vgnuplot_command_end << ends; |
943
|
1083 gnuplot_terminal_type = buf.str (); |
|
1084 } |
524
|
1085 } |
|
1086 |
|
1087 for (int i = 0; i < argc; i++) |
|
1088 plot_buf << argv[i] << " "; |
|
1089 |
3162
|
1090 plot_buf << Vgnuplot_command_end << ends; |
524
|
1091 |
|
1092 char *plot_command = plot_buf.str (); |
|
1093 send_to_plot_stream (plot_command); |
|
1094 |
|
1095 delete [] plot_command; |
|
1096 |
|
1097 return retval; |
|
1098 } |
|
1099 |
2520
|
1100 DEFUN_TEXT (set, args, nargout, |
|
1101 "This command is has been replaced by `gset'.") |
|
1102 { |
2521
|
1103 warning ("set is obsolete -- use gset instead"); |
2520
|
1104 return Fgset (args, nargout); |
|
1105 } |
|
1106 |
|
1107 DEFUN_TEXT (gshow, args, , |
|
1108 "gshow [options]\n\ |
524
|
1109 \n\ |
|
1110 show plotting options") |
|
1111 { |
2086
|
1112 octave_value_list retval; |
524
|
1113 |
1755
|
1114 int argc = args.length () + 1; |
|
1115 |
1968
|
1116 string_vector argv = args.make_argv ("show"); |
1755
|
1117 |
|
1118 if (error_state) |
|
1119 return retval; |
524
|
1120 |
|
1121 ostrstream plot_buf; |
|
1122 |
|
1123 for (int i = 0; i < argc; i++) |
|
1124 plot_buf << argv[i] << " "; |
|
1125 |
3162
|
1126 plot_buf << Vgnuplot_command_end << ends; |
524
|
1127 |
|
1128 char *plot_command = plot_buf.str (); |
|
1129 send_to_plot_stream (plot_command); |
|
1130 |
|
1131 delete [] plot_command; |
|
1132 |
|
1133 return retval; |
|
1134 } |
|
1135 |
2520
|
1136 DEFUN_TEXT (show, args, nargout, |
|
1137 "This command is has been replaced by `gshow'.") |
|
1138 { |
2521
|
1139 warning ("show is obsolete -- use gshow instead"); |
2520
|
1140 return Fgshow (args, nargout); |
|
1141 } |
|
1142 |
2176
|
1143 static int |
|
1144 automatic_replot (void) |
|
1145 { |
|
1146 Vautomatic_replot = check_preference ("automatic_replot"); |
|
1147 |
|
1148 return 0; |
|
1149 } |
|
1150 |
3162
|
1151 static int |
|
1152 set_string_var (string& var, const char *nm) |
2176
|
1153 { |
3162
|
1154 int retval = 0; |
2176
|
1155 |
3162
|
1156 string s = builtin_string_variable (nm); |
2176
|
1157 |
|
1158 if (s.empty ()) |
|
1159 { |
3162
|
1160 gripe_invalid_value_specified (nm); |
|
1161 retval = -1; |
2176
|
1162 } |
|
1163 else |
3162
|
1164 var = s; |
|
1165 |
|
1166 return retval; |
|
1167 } |
|
1168 |
|
1169 static int |
|
1170 gnuplot_binary (void) |
|
1171 { |
|
1172 return set_string_var (Vgnuplot_binary, "gnuplot_binary"); |
|
1173 } |
|
1174 |
|
1175 static int |
|
1176 gnuplot_command_plot (void) |
|
1177 { |
|
1178 return set_string_var (Vgnuplot_command_plot, "gnuplot_command_plot"); |
|
1179 } |
|
1180 |
|
1181 static int |
|
1182 gnuplot_command_replot (void) |
|
1183 { |
|
1184 return set_string_var (Vgnuplot_command_replot, "gnuplot_command_replot"); |
|
1185 } |
2176
|
1186 |
3162
|
1187 static int |
|
1188 gnuplot_command_splot (void) |
|
1189 { |
|
1190 return set_string_var (Vgnuplot_command_splot, "gnuplot_command_splot"); |
|
1191 } |
|
1192 |
|
1193 static int |
|
1194 gnuplot_command_using (void) |
|
1195 { |
|
1196 return set_string_var (Vgnuplot_command_using, "gnuplot_command_using"); |
|
1197 } |
|
1198 |
|
1199 static int |
|
1200 gnuplot_command_with (void) |
|
1201 { |
|
1202 return set_string_var (Vgnuplot_command_with, "gnuplot_command_with"); |
|
1203 } |
|
1204 |
|
1205 static int |
3165
|
1206 gnuplot_command_axes (void) |
|
1207 { |
|
1208 return set_string_var (Vgnuplot_command_axes, "gnuplot_command_axes"); |
|
1209 } |
|
1210 |
|
1211 static int |
3162
|
1212 gnuplot_command_title (void) |
|
1213 { |
|
1214 return set_string_var (Vgnuplot_command_title, "gnuplot_command_title"); |
|
1215 } |
|
1216 |
|
1217 static int |
|
1218 gnuplot_command_end (void) |
|
1219 { |
|
1220 return set_string_var (Vgnuplot_command_end, "gnuplot_command_end"); |
2176
|
1221 } |
|
1222 |
|
1223 static int |
2289
|
1224 gnuplot_has_frames (void) |
|
1225 { |
|
1226 Vgnuplot_has_frames = check_preference ("gnuplot_has_frames"); |
|
1227 |
|
1228 return 0; |
|
1229 } |
|
1230 |
|
1231 static int |
2176
|
1232 gnuplot_has_multiplot (void) |
|
1233 { |
|
1234 Vgnuplot_has_multiplot = check_preference ("gnuplot_has_multiplot"); |
|
1235 |
|
1236 return 0; |
|
1237 } |
|
1238 |
|
1239 void |
|
1240 symbols_of_pt_plot (void) |
|
1241 { |
|
1242 DEFVAR (automatic_replot, 0.0, 0, automatic_replot, |
|
1243 "if true, auto-insert a replot command when a plot changes"); |
|
1244 |
|
1245 DEFVAR (gnuplot_binary, "gnuplot", 0, gnuplot_binary, |
|
1246 "path to gnuplot binary"); |
|
1247 |
3162
|
1248 DEFVAR (gnuplot_command_plot, "pl", 0, gnuplot_command_plot, |
|
1249 ""); |
|
1250 |
|
1251 DEFVAR (gnuplot_command_replot, "rep", 0, gnuplot_command_replot, |
|
1252 ""); |
|
1253 |
|
1254 DEFVAR (gnuplot_command_splot, "sp", 0, gnuplot_command_splot, |
|
1255 ""); |
|
1256 |
|
1257 DEFVAR (gnuplot_command_using, "u", 0, gnuplot_command_using, |
|
1258 ""); |
|
1259 |
|
1260 DEFVAR (gnuplot_command_with, "w", 0, gnuplot_command_with, |
|
1261 ""); |
|
1262 |
3165
|
1263 DEFVAR (gnuplot_command_axes, "ax", 0, gnuplot_command_axes, |
|
1264 ""); |
|
1265 |
3162
|
1266 DEFVAR (gnuplot_command_title, "t", 0, gnuplot_command_title, |
|
1267 ""); |
|
1268 |
|
1269 DEFVAR (gnuplot_command_end, "\n", 0, gnuplot_command_end, |
|
1270 ""); |
|
1271 |
2289
|
1272 #ifdef GNUPLOT_HAS_FRAMES |
|
1273 double with_frames = 1.0; |
|
1274 #else |
|
1275 double with_frames = 0.0; |
|
1276 #endif |
|
1277 |
|
1278 DEFVAR (gnuplot_has_frames, with_frames, 0, gnuplot_has_frames, |
|
1279 "true if gnuplot supports multiple plot windows on X11, false otherwise"); |
|
1280 |
2176
|
1281 #ifdef GNUPLOT_HAS_MULTIPLOT |
|
1282 double with_multiplot = 1.0; |
|
1283 #else |
|
1284 double with_multiplot = 0.0; |
|
1285 #endif |
|
1286 |
|
1287 DEFVAR (gnuplot_has_multiplot, with_multiplot, 0, gnuplot_has_multiplot, |
|
1288 "true if gnuplot supports multiplot, false otherwise"); |
|
1289 } |
|
1290 |
1
|
1291 /* |
|
1292 ;;; Local Variables: *** |
|
1293 ;;; mode: C++ *** |
|
1294 ;;; End: *** |
|
1295 */ |