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