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