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