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