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