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 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
3503
|
27 #include <fstream> |
1755
|
28 #include <string> |
1
|
29 |
2926
|
30 #include "cmd-edit.h" |
|
31 #include "oct-env.h" |
2101
|
32 |
1
|
33 #include "procstream.h" |
|
34 |
2492
|
35 #include <defaults.h> |
1352
|
36 #include "defun.h" |
|
37 #include "error.h" |
2164
|
38 #include "gripes.h" |
2110
|
39 #include "input.h" |
1755
|
40 #include "oct-obj.h" |
1352
|
41 #include "pager.h" |
990
|
42 #include "sighandlers.h" |
2100
|
43 #include "unwind-prot.h" |
2201
|
44 #include "utils.h" |
2368
|
45 #include "variables.h" |
2093
|
46 |
2209
|
47 static pid_t octave_pager_pid = -1; |
1
|
48 |
2093
|
49 // Our actual connection to the external pager. |
|
50 static oprocstream *external_pager = 0; |
1
|
51 |
3018
|
52 // TRUE means we write to the diary file. |
|
53 static bool write_to_diary_file = false; |
581
|
54 |
|
55 // The name of the current diary file. |
3523
|
56 static std::string diary_file; |
581
|
57 |
|
58 // The diary file. |
3523
|
59 static std::ofstream external_diary_file; |
581
|
60 |
2164
|
61 // The shell command to run as the pager. |
3523
|
62 static std::string Vpager_binary; |
2164
|
63 |
|
64 // TRUE means that if output is going to the pager, it is sent as soon |
|
65 // as it is available. Otherwise, it is buffered and only sent to the |
|
66 // pager when it is time to print another prompt. |
|
67 static bool Vpage_output_immediately; |
|
68 |
|
69 // TRUE means all output intended for the screen should be passed |
|
70 // through the pager. |
|
71 static bool Vpage_screen_output; |
|
72 |
2705
|
73 // Only one pager can be active at once, so having these at file |
|
74 // scope should be ok. |
|
75 static octave_interrupt_handler saved_interrupt_handler; |
|
76 static bool interrupt_handler_saved = false; |
2093
|
77 |
3018
|
78 static bool really_flush_to_pager = false; |
2100
|
79 |
3018
|
80 static bool flushing_output_to_pager = false; |
2206
|
81 |
2093
|
82 static void |
2197
|
83 clear_external_pager (void) |
|
84 { |
2210
|
85 octave_child_list::remove (octave_pager_pid); |
|
86 |
2197
|
87 octave_pager_pid = -1; |
|
88 |
|
89 delete external_pager; |
|
90 external_pager = 0; |
|
91 |
2705
|
92 if (interrupt_handler_saved) |
2197
|
93 { |
2554
|
94 octave_set_interrupt_handler (saved_interrupt_handler); |
2705
|
95 interrupt_handler_saved = false; |
2197
|
96 } |
|
97 } |
|
98 |
|
99 static void |
2209
|
100 pager_death_handler (pid_t pid, int status) |
|
101 { |
|
102 if (pid > 0) |
|
103 { |
|
104 if (WIFEXITED (status) || WIFSIGNALLED (status)) |
|
105 { |
2795
|
106 // Avoid warning() or error(), since that will put us back in |
|
107 // the pager, which would be bad news. |
2209
|
108 |
3531
|
109 std::cerr << "warning: connection to external pager (pid = " |
2795
|
110 << pid << ") lost --\n" |
|
111 << "warning: attempting to finish pending computations...\n"; |
2209
|
112 } |
|
113 } |
|
114 } |
|
115 |
|
116 static void |
3233
|
117 do_sync (const char *msg, int len, bool bypass_pager) |
1
|
118 { |
3233
|
119 if (msg && len > 0) |
1
|
120 { |
2206
|
121 if (bypass_pager) |
2646
|
122 { |
3531
|
123 std::cout.write (msg, len); |
|
124 std::cout.flush (); |
2646
|
125 } |
2206
|
126 else |
2093
|
127 { |
2206
|
128 if (! external_pager) |
2093
|
129 { |
3523
|
130 std::string pgr = Vpager_binary; |
2093
|
131 |
2206
|
132 if (! pgr.empty ()) |
|
133 { |
2554
|
134 saved_interrupt_handler = octave_ignore_interrupts (); |
2705
|
135 interrupt_handler_saved = true; |
2093
|
136 |
2206
|
137 external_pager = new oprocstream (pgr.c_str ()); |
2093
|
138 |
2206
|
139 if (external_pager) |
2209
|
140 { |
|
141 octave_pager_pid = external_pager->pid (); |
|
142 |
|
143 octave_child_list::insert (octave_pager_pid, |
|
144 pager_death_handler); |
|
145 } |
2093
|
146 } |
2206
|
147 } |
2101
|
148 |
2206
|
149 if (external_pager) |
|
150 { |
|
151 if (octave_pager_pid > 0 && external_pager->good ()) |
2101
|
152 { |
3233
|
153 external_pager->write (msg, len); |
2101
|
154 |
2206
|
155 // These checks are needed if a signal handler |
|
156 // invoked since the last set of checks attempts |
|
157 // to flush output and then returns |
2197
|
158 |
2206
|
159 if (octave_pager_pid > 0 |
|
160 && external_pager |
|
161 && external_pager->good ()) |
|
162 external_pager->flush (); |
2101
|
163 } |
2795
|
164 else |
|
165 { |
|
166 // We had a pager, but it must have died. Restore |
|
167 // the interrupt state so we can escape back to the |
|
168 // prompt if there are lots of computations pending. |
|
169 |
|
170 if (interrupt_handler_saved) |
|
171 { |
|
172 octave_set_interrupt_handler (saved_interrupt_handler); |
|
173 interrupt_handler_saved = false; |
|
174 } |
|
175 } |
2093
|
176 } |
2206
|
177 else |
2646
|
178 { |
3531
|
179 std::cout.write (msg, len); |
|
180 std::cout.flush (); |
2646
|
181 } |
2093
|
182 } |
1
|
183 } |
|
184 } |
|
185 |
3233
|
186 // Assume our terminal wraps long lines. |
|
187 |
2101
|
188 static bool |
3233
|
189 more_than_a_screenful (const char *s, int len) |
2101
|
190 { |
|
191 if (s) |
|
192 { |
2926
|
193 int available_rows = command_editor::terminal_rows () - 2; |
2101
|
194 |
3233
|
195 int cols = command_editor::terminal_cols (); |
|
196 |
2103
|
197 int count = 0; |
|
198 |
3233
|
199 int chars_this_line = 0; |
2101
|
200 |
3233
|
201 for (int i = 0; i < len; i++) |
|
202 { |
|
203 if (*s++ == '\n') |
|
204 { |
|
205 count += chars_this_line / cols + 1; |
|
206 chars_this_line = 0; |
|
207 } |
|
208 else |
|
209 chars_this_line++; |
|
210 } |
2101
|
211 |
3233
|
212 if (count > available_rows) |
|
213 return true; |
2101
|
214 } |
|
215 |
|
216 return false; |
|
217 } |
|
218 |
2093
|
219 int |
|
220 octave_pager_buf::sync (void) |
|
221 { |
2186
|
222 if (! interactive |
|
223 || really_flush_to_pager |
2164
|
224 || (Vpage_screen_output && Vpage_output_immediately) |
|
225 || ! Vpage_screen_output) |
2100
|
226 { |
3233
|
227 char *buf = eback (); |
2093
|
228 |
3233
|
229 int len = pptr () - buf; |
2100
|
230 |
2110
|
231 bool bypass_pager = (! interactive |
2164
|
232 || ! Vpage_screen_output |
2110
|
233 || (really_flush_to_pager |
2164
|
234 && Vpage_screen_output |
|
235 && ! Vpage_output_immediately |
3233
|
236 && ! more_than_a_screenful (buf, len))); |
2475
|
237 |
3233
|
238 if (len > 0) |
|
239 { |
|
240 do_sync (buf, len, bypass_pager); |
2093
|
241 |
3544
|
242 seekoff (0, std::ios::beg); |
3233
|
243 |
|
244 octave_diary.write (buf, len); |
|
245 } |
2100
|
246 } |
2093
|
247 |
|
248 return 0; |
|
249 } |
|
250 |
3477
|
251 void |
|
252 octave_pager_buf::flush_current_contents_to_diary (void) |
|
253 { |
|
254 char *buf = eback (); |
|
255 |
|
256 int len = pptr () - buf; |
|
257 |
|
258 octave_diary.write (buf, len); |
|
259 } |
|
260 |
2093
|
261 int |
|
262 octave_diary_buf::sync (void) |
|
263 { |
3233
|
264 if (write_to_diary_file && external_diary_file) |
|
265 { |
|
266 int len = pptr () - eback (); |
2093
|
267 |
3233
|
268 if (len > 0) |
|
269 external_diary_file.write (eback (), len); |
|
270 } |
2093
|
271 |
3544
|
272 seekoff (0, std::ios::beg); |
2093
|
273 |
|
274 return 0; |
|
275 } |
|
276 |
|
277 octave_pager_stream *octave_pager_stream::instance = 0; |
|
278 |
3523
|
279 octave_pager_stream::octave_pager_stream (void) : std::ostream (), pb (0) |
1
|
280 { |
2093
|
281 pb = new octave_pager_buf; |
|
282 rdbuf (pb); |
|
283 setf (unitbuf); |
|
284 } |
|
285 |
|
286 octave_pager_stream::~octave_pager_stream (void) |
|
287 { |
|
288 flush (); |
|
289 delete pb; |
|
290 } |
|
291 |
|
292 octave_pager_stream& |
|
293 octave_pager_stream::stream (void) |
|
294 { |
|
295 if (! instance) |
|
296 instance = new octave_pager_stream (); |
2926
|
297 |
2093
|
298 return *instance; |
|
299 } |
|
300 |
3477
|
301 void |
|
302 octave_pager_stream::flush_current_contents_to_diary (void) |
|
303 { |
|
304 if (pb) |
|
305 pb->flush_current_contents_to_diary (); |
|
306 } |
|
307 |
2093
|
308 octave_diary_stream *octave_diary_stream::instance = 0; |
|
309 |
3523
|
310 octave_diary_stream::octave_diary_stream (void) : std::ostream (), db (0) |
2093
|
311 { |
|
312 db = new octave_diary_buf; |
|
313 rdbuf (db); |
|
314 setf (unitbuf); |
|
315 } |
|
316 |
|
317 octave_diary_stream::~octave_diary_stream (void) |
|
318 { |
|
319 flush (); |
|
320 delete db; |
|
321 } |
|
322 |
|
323 octave_diary_stream& |
|
324 octave_diary_stream::stream (void) |
|
325 { |
|
326 if (! instance) |
|
327 instance = new octave_diary_stream (); |
|
328 |
|
329 return *instance; |
1
|
330 } |
|
331 |
|
332 void |
2093
|
333 flush_octave_stdout (void) |
1
|
334 { |
2206
|
335 if (! flushing_output_to_pager) |
|
336 { |
2985
|
337 unwind_protect::begin_frame ("flush_octave_stdout"); |
2100
|
338 |
3018
|
339 unwind_protect_bool (really_flush_to_pager); |
|
340 unwind_protect_bool (flushing_output_to_pager); |
2100
|
341 |
3018
|
342 really_flush_to_pager = true; |
|
343 flushing_output_to_pager = true; |
2206
|
344 |
|
345 octave_stdout.flush (); |
1
|
346 |
2206
|
347 if (external_pager) |
|
348 clear_external_pager (); |
2100
|
349 |
2985
|
350 unwind_protect::run_frame ("flush_octave_stdout"); |
2206
|
351 } |
1
|
352 } |
|
353 |
1965
|
354 static void |
2093
|
355 close_diary_file (void) |
1
|
356 { |
3477
|
357 // Try to flush the current buffer to the diary now, so that things |
|
358 // like |
|
359 // |
|
360 // function foo () |
|
361 // diary on; |
|
362 // ... |
|
363 // diary off; |
|
364 // endfunction |
|
365 // |
|
366 // will do the right thing. |
|
367 |
|
368 octave_stdout.flush_current_contents_to_diary (); |
|
369 |
2093
|
370 if (external_diary_file.is_open ()) |
1
|
371 { |
2093
|
372 octave_diary.flush (); |
|
373 external_diary_file.close (); |
1
|
374 } |
|
375 } |
|
376 |
581
|
377 static void |
|
378 open_diary_file (void) |
|
379 { |
2093
|
380 close_diary_file (); |
581
|
381 |
3544
|
382 external_diary_file.open (diary_file.c_str (), std::ios::app); |
581
|
383 |
2093
|
384 if (! external_diary_file) |
|
385 error ("diary: can't open diary file `%s'", diary_file.c_str ()); |
581
|
386 } |
|
387 |
1957
|
388 DEFUN_TEXT (diary, args, , |
3332
|
389 "-*- texinfo -*-\n\ |
|
390 @deffn {Command} diary options\n\ |
|
391 Create a list of all commands @emph{and} the output they produce, mixed\n\ |
|
392 together just as you see them on your terminal. Valid options are:\n\ |
|
393 \n\ |
|
394 @table @code\n\ |
|
395 @item on\n\ |
|
396 Start recording your session in a file called @file{diary} in your\n\ |
|
397 current working directory.\n\ |
581
|
398 \n\ |
3332
|
399 @item off\n\ |
|
400 Stop recording your session in the diary file.\n\ |
|
401 \n\ |
|
402 @item @var{file}\n\ |
|
403 Record your session in the file named @var{file}.\n\ |
|
404 @end table\n\ |
|
405 \n\ |
|
406 Without any arguments, @code{diary} toggles the current diary state.\n\ |
3333
|
407 @end deffn") |
581
|
408 { |
2086
|
409 octave_value_list retval; |
581
|
410 |
1755
|
411 int argc = args.length () + 1; |
|
412 |
1965
|
413 string_vector argv = args.make_argv ("diary"); |
581
|
414 |
1755
|
415 if (error_state) |
|
416 return retval; |
|
417 |
|
418 if (diary_file.empty ()) |
|
419 diary_file = "diary"; |
1306
|
420 |
581
|
421 switch (argc) |
|
422 { |
|
423 case 1: |
|
424 write_to_diary_file = ! write_to_diary_file; |
|
425 open_diary_file (); |
|
426 break; |
623
|
427 |
581
|
428 case 2: |
|
429 { |
3523
|
430 std::string arg = argv[1]; |
1755
|
431 |
|
432 if (arg == "on") |
581
|
433 { |
3018
|
434 write_to_diary_file = true; |
581
|
435 open_diary_file (); |
|
436 } |
1755
|
437 else if (arg == "off") |
2093
|
438 { |
|
439 close_diary_file (); |
3018
|
440 write_to_diary_file = false; |
2093
|
441 } |
581
|
442 else |
|
443 { |
1755
|
444 diary_file = arg; |
3176
|
445 write_to_diary_file = true; |
581
|
446 open_diary_file (); |
|
447 } |
|
448 } |
|
449 break; |
777
|
450 |
581
|
451 default: |
|
452 print_usage ("diary"); |
|
453 break; |
|
454 } |
|
455 |
|
456 return retval; |
|
457 } |
|
458 |
1957
|
459 DEFUN_TEXT (more, args, , |
3372
|
460 "-*- texinfo -*-\n\ |
|
461 @deffn {Command} more\n\ |
|
462 @deffnx {Command} more on\n\ |
|
463 @deffnx {Command} more off\n\ |
|
464 Turn output pagination on or off. Without an argument, @code{more}\n\ |
|
465 toggles the current state.\n\ |
|
466 @end deffn") |
1409
|
467 { |
2086
|
468 octave_value_list retval; |
1409
|
469 |
1755
|
470 int argc = args.length () + 1; |
|
471 |
1965
|
472 string_vector argv = args.make_argv ("more"); |
1755
|
473 |
|
474 if (error_state) |
|
475 return retval; |
1409
|
476 |
|
477 if (argc == 2) |
|
478 { |
3523
|
479 std::string arg = argv[1]; |
1409
|
480 |
1755
|
481 if (arg == "on") |
2448
|
482 bind_builtin_variable ("page_screen_output", 1.0); |
1755
|
483 else if (arg == "off") |
2448
|
484 bind_builtin_variable ("page_screen_output", 0.0); |
1409
|
485 else |
1755
|
486 error ("more: unrecognized argument `%s'", arg.c_str ()); |
1409
|
487 } |
|
488 else |
|
489 print_usage ("more"); |
|
490 |
|
491 return retval; |
|
492 } |
|
493 |
3536
|
494 static std::string |
2097
|
495 default_pager (void) |
|
496 { |
3523
|
497 std::string pager_binary = octave_env::getenv ("PAGER"); |
2097
|
498 |
3584
|
499 #ifdef OCTAVE_DEFAULT_PAGER |
2926
|
500 if (pager_binary.empty ()) |
2097
|
501 { |
3584
|
502 pager_binary = std::string (OCTAVE_DEFAULT_PAGER); |
2097
|
503 |
|
504 if (pager_binary == "less") |
|
505 { |
|
506 pager_binary.append (" -e"); |
|
507 |
3523
|
508 std::string lessflags = octave_env::getenv ("LESS"); |
2926
|
509 if (lessflags.empty ()) |
2097
|
510 pager_binary.append |
2099
|
511 (" -P'-- less ?pB(%pB\\%):--. (f)orward, (b)ack, (q)uit$'"); |
2097
|
512 } |
|
513 } |
|
514 #endif |
|
515 |
|
516 return pager_binary; |
|
517 } |
|
518 |
2164
|
519 static int |
|
520 pager_binary (void) |
|
521 { |
|
522 int status = 0; |
|
523 |
3523
|
524 std::string s = builtin_string_variable ("PAGER"); |
2164
|
525 |
|
526 if (s.empty ()) |
|
527 { |
|
528 gripe_invalid_value_specified ("PAGER"); |
|
529 status = -1; |
|
530 } |
|
531 else |
|
532 Vpager_binary = s; |
|
533 |
|
534 return status; |
|
535 } |
|
536 |
|
537 static int |
|
538 page_output_immediately (void) |
|
539 { |
|
540 Vpage_output_immediately = check_preference ("page_output_immediately"); |
|
541 |
|
542 return 0; |
|
543 } |
|
544 |
|
545 static int |
|
546 page_screen_output (void) |
|
547 { |
|
548 Vpage_screen_output = check_preference ("page_screen_output"); |
|
549 |
|
550 return 0; |
|
551 } |
|
552 |
2097
|
553 void |
|
554 symbols_of_pager (void) |
|
555 { |
3258
|
556 DEFVAR (PAGER, default_pager (), pager_binary, |
3372
|
557 "-*- texinfo -*-\n\ |
|
558 @defvr {Built-in Variable} PAGER\n\ |
|
559 The default value is normally @code{\"less\"}, @code{\"more\"}, or\n\ |
|
560 @code{\"pg\"}, depending on what programs are installed on your system.\n\ |
|
561 @xref{Installation}.\n\ |
|
562 \n\ |
|
563 When running interactively, Octave sends any output intended for your\n\ |
|
564 terminal that is more than one screen long to the program named by the\n\ |
|
565 value of the variable @code{PAGER}.\n\ |
|
566 @end defvr"); |
2097
|
567 |
3258
|
568 DEFVAR (page_output_immediately, 0.0, page_output_immediately, |
3372
|
569 "-*- texinfo -*-\n\ |
|
570 @defvr {Built-in Variable} page_output_immediately\n\ |
|
571 If the value of @code{page_output_immediately} is nonzero, Octave sends\n\ |
|
572 output to the pager as soon as it is available. Otherwise, Octave\n\ |
|
573 buffers its output and waits until just before the prompt is printed to\n\ |
|
574 flush it to the pager. The default value is 0.\n\ |
|
575 @end defvr"); |
2100
|
576 |
3258
|
577 DEFVAR (page_screen_output, 1.0, page_screen_output, |
3372
|
578 "-*- texinfo -*-\n\ |
|
579 @defvr {Built-in Variable} page_screen_output\n\ |
|
580 If the value of @code{page_screen_output} is nonzero, all output\n\ |
|
581 intended for the screen that is longer than one page is sent through a\n\ |
|
582 pager. This allows you to view one screenful at a time. Some pagers\n\ |
|
583 (such as @code{less}---see @ref{Installation}) are also capable of moving\n\ |
|
584 backward on the output. The default value is 1.\n\ |
|
585 @end defvr"); |
2097
|
586 } |
|
587 |
1
|
588 /* |
|
589 ;;; Local Variables: *** |
|
590 ;;; mode: C++ *** |
|
591 ;;; End: *** |
|
592 */ |