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" |
1352
|
41 #include "help.h" |
2110
|
42 #include "input.h" |
1755
|
43 #include "oct-obj.h" |
1352
|
44 #include "pager.h" |
990
|
45 #include "sighandlers.h" |
2100
|
46 #include "unwind-prot.h" |
2201
|
47 #include "utils.h" |
2368
|
48 #include "variables.h" |
2093
|
49 |
2209
|
50 static pid_t octave_pager_pid = -1; |
1
|
51 |
2093
|
52 // Our actual connection to the external pager. |
|
53 static oprocstream *external_pager = 0; |
1
|
54 |
581
|
55 // Nonzero means we write to the diary file. |
|
56 static int write_to_diary_file = 0; |
|
57 |
|
58 // The name of the current diary file. |
1755
|
59 static string diary_file; |
581
|
60 |
|
61 // The diary file. |
2093
|
62 static ofstream external_diary_file; |
581
|
63 |
2164
|
64 // The shell command to run as the pager. |
|
65 static string Vpager_binary; |
|
66 |
|
67 // TRUE means that if output is going to the pager, it is sent as soon |
|
68 // as it is available. Otherwise, it is buffered and only sent to the |
|
69 // pager when it is time to print another prompt. |
|
70 static bool Vpage_output_immediately; |
|
71 |
|
72 // TRUE means all output intended for the screen should be passed |
|
73 // through the pager. |
|
74 static bool Vpage_screen_output; |
|
75 |
2705
|
76 // Only one pager can be active at once, so having these at file |
|
77 // scope should be ok. |
|
78 static octave_interrupt_handler saved_interrupt_handler; |
|
79 static bool interrupt_handler_saved = false; |
2093
|
80 |
2100
|
81 static int really_flush_to_pager = 0; |
|
82 |
2206
|
83 static int flushing_output_to_pager = 0; |
|
84 |
2093
|
85 static void |
2197
|
86 clear_external_pager (void) |
|
87 { |
2210
|
88 octave_child_list::remove (octave_pager_pid); |
|
89 |
2197
|
90 octave_pager_pid = -1; |
|
91 |
|
92 delete external_pager; |
|
93 external_pager = 0; |
|
94 |
2705
|
95 if (interrupt_handler_saved) |
2197
|
96 { |
2554
|
97 octave_set_interrupt_handler (saved_interrupt_handler); |
2705
|
98 interrupt_handler_saved = false; |
2197
|
99 } |
|
100 } |
|
101 |
|
102 static void |
2209
|
103 pager_death_handler (pid_t pid, int status) |
|
104 { |
|
105 if (pid > 0) |
|
106 { |
|
107 if (WIFEXITED (status) || WIFSIGNALLED (status)) |
|
108 { |
2795
|
109 // Avoid warning() or error(), since that will put us back in |
|
110 // the pager, which would be bad news. |
2209
|
111 |
2795
|
112 cerr << "warning: connection to external pager (pid = " |
|
113 << pid << ") lost --\n" |
|
114 << "warning: attempting to finish pending computations...\n"; |
2209
|
115 } |
|
116 } |
|
117 } |
|
118 |
|
119 static void |
2101
|
120 do_sync (const char *msg, bool bypass_pager) |
1
|
121 { |
2206
|
122 if (msg && *msg) |
1
|
123 { |
2206
|
124 if (bypass_pager) |
2646
|
125 { |
|
126 cout << msg; |
2652
|
127 cout.flush (); |
2646
|
128 } |
2206
|
129 else |
2093
|
130 { |
2206
|
131 if (! external_pager) |
2093
|
132 { |
2206
|
133 string pgr = Vpager_binary; |
2093
|
134 |
2206
|
135 if (! pgr.empty ()) |
|
136 { |
2554
|
137 saved_interrupt_handler = octave_ignore_interrupts (); |
2705
|
138 interrupt_handler_saved = true; |
2093
|
139 |
2206
|
140 external_pager = new oprocstream (pgr.c_str ()); |
2093
|
141 |
2206
|
142 if (external_pager) |
2209
|
143 { |
|
144 octave_pager_pid = external_pager->pid (); |
|
145 |
|
146 octave_child_list::insert (octave_pager_pid, |
|
147 pager_death_handler); |
|
148 } |
2093
|
149 } |
2206
|
150 } |
2101
|
151 |
2206
|
152 if (external_pager) |
|
153 { |
|
154 if (octave_pager_pid > 0 && external_pager->good ()) |
2101
|
155 { |
2206
|
156 *external_pager << msg; |
2101
|
157 |
2206
|
158 // These checks are needed if a signal handler |
|
159 // invoked since the last set of checks attempts |
|
160 // to flush output and then returns |
2197
|
161 |
2206
|
162 if (octave_pager_pid > 0 |
|
163 && external_pager |
|
164 && external_pager->good ()) |
|
165 external_pager->flush (); |
2101
|
166 } |
2795
|
167 else |
|
168 { |
|
169 // We had a pager, but it must have died. Restore |
|
170 // the interrupt state so we can escape back to the |
|
171 // prompt if there are lots of computations pending. |
|
172 |
|
173 if (interrupt_handler_saved) |
|
174 { |
|
175 octave_set_interrupt_handler (saved_interrupt_handler); |
|
176 interrupt_handler_saved = false; |
|
177 } |
|
178 } |
2093
|
179 } |
2206
|
180 else |
2646
|
181 { |
|
182 cout << msg; |
2652
|
183 cout.flush (); |
2646
|
184 } |
2093
|
185 } |
1
|
186 } |
|
187 } |
|
188 |
2101
|
189 static bool |
|
190 more_than_a_screenful (const char *s) |
|
191 { |
|
192 if (s) |
|
193 { |
2926
|
194 int available_rows = command_editor::terminal_rows () - 2; |
2101
|
195 |
2103
|
196 int count = 0; |
|
197 |
2101
|
198 char c; |
|
199 |
|
200 while ((c = *s++) != '\0') |
|
201 if (c == '\n') |
|
202 { |
|
203 count++; |
|
204 |
|
205 if (count > available_rows) |
|
206 return true; |
|
207 } |
|
208 } |
|
209 |
|
210 return false; |
|
211 } |
|
212 |
2093
|
213 int |
|
214 octave_pager_buf::sync (void) |
|
215 { |
2186
|
216 if (! interactive |
|
217 || really_flush_to_pager |
2164
|
218 || (Vpage_screen_output && Vpage_output_immediately) |
|
219 || ! Vpage_screen_output) |
2100
|
220 { |
|
221 sputc ('\0'); |
2093
|
222 |
2100
|
223 char *buf = eback (); |
|
224 |
2110
|
225 bool bypass_pager = (! interactive |
2164
|
226 || ! Vpage_screen_output |
2110
|
227 || (really_flush_to_pager |
2164
|
228 && Vpage_screen_output |
|
229 && ! Vpage_output_immediately |
2110
|
230 && ! more_than_a_screenful (buf))); |
2101
|
231 |
2475
|
232 seekoff (0, ios::beg); |
|
233 |
2101
|
234 do_sync (buf, bypass_pager); |
2093
|
235 |
2100
|
236 octave_diary << buf; |
|
237 } |
2093
|
238 |
|
239 return 0; |
|
240 } |
|
241 |
|
242 int |
|
243 octave_diary_buf::sync (void) |
|
244 { |
|
245 sputc ('\0'); |
|
246 |
|
247 if (write_to_diary_file && external_diary_file) |
|
248 external_diary_file << eback (); |
|
249 |
|
250 seekoff (0, ios::beg); |
|
251 |
|
252 return 0; |
|
253 } |
|
254 |
|
255 octave_pager_stream *octave_pager_stream::instance = 0; |
|
256 |
|
257 octave_pager_stream::octave_pager_stream (void) : ostream (), pb (0) |
1
|
258 { |
2093
|
259 pb = new octave_pager_buf; |
|
260 rdbuf (pb); |
|
261 setf (unitbuf); |
|
262 } |
|
263 |
|
264 octave_pager_stream::~octave_pager_stream (void) |
|
265 { |
|
266 flush (); |
|
267 delete pb; |
|
268 } |
|
269 |
|
270 octave_pager_stream& |
|
271 octave_pager_stream::stream (void) |
|
272 { |
|
273 if (! instance) |
|
274 instance = new octave_pager_stream (); |
2926
|
275 |
2093
|
276 return *instance; |
|
277 } |
|
278 |
|
279 octave_diary_stream *octave_diary_stream::instance = 0; |
|
280 |
|
281 octave_diary_stream::octave_diary_stream (void) : ostream (), db (0) |
|
282 { |
|
283 db = new octave_diary_buf; |
|
284 rdbuf (db); |
|
285 setf (unitbuf); |
|
286 } |
|
287 |
|
288 octave_diary_stream::~octave_diary_stream (void) |
|
289 { |
|
290 flush (); |
|
291 delete db; |
|
292 } |
|
293 |
|
294 octave_diary_stream& |
|
295 octave_diary_stream::stream (void) |
|
296 { |
|
297 if (! instance) |
|
298 instance = new octave_diary_stream (); |
|
299 |
|
300 return *instance; |
1
|
301 } |
|
302 |
|
303 void |
2093
|
304 flush_octave_stdout (void) |
1
|
305 { |
2206
|
306 if (! flushing_output_to_pager) |
|
307 { |
|
308 begin_unwind_frame ("flush_octave_stdout"); |
2100
|
309 |
2206
|
310 unwind_protect_int (really_flush_to_pager); |
|
311 unwind_protect_int (flushing_output_to_pager); |
2100
|
312 |
2206
|
313 really_flush_to_pager = 1; |
|
314 flushing_output_to_pager = 1; |
|
315 |
|
316 octave_stdout.flush (); |
1
|
317 |
2206
|
318 if (external_pager) |
|
319 clear_external_pager (); |
2100
|
320 |
2206
|
321 run_unwind_frame ("flush_octave_stdout"); |
|
322 } |
1
|
323 } |
|
324 |
1965
|
325 static void |
2093
|
326 close_diary_file (void) |
1
|
327 { |
2093
|
328 if (external_diary_file.is_open ()) |
1
|
329 { |
2093
|
330 octave_diary.flush (); |
|
331 external_diary_file.close (); |
1
|
332 } |
|
333 } |
|
334 |
581
|
335 static void |
|
336 open_diary_file (void) |
|
337 { |
2093
|
338 close_diary_file (); |
581
|
339 |
2093
|
340 external_diary_file.open (diary_file.c_str (), ios::app); |
581
|
341 |
2093
|
342 if (! external_diary_file) |
|
343 error ("diary: can't open diary file `%s'", diary_file.c_str ()); |
581
|
344 } |
|
345 |
1957
|
346 DEFUN_TEXT (diary, args, , |
581
|
347 "diary [on|off]\n\ |
|
348 diary [file]\n\ |
|
349 \n\ |
|
350 redirect all input and screen output to a file.") |
|
351 { |
2086
|
352 octave_value_list retval; |
581
|
353 |
1755
|
354 int argc = args.length () + 1; |
|
355 |
1965
|
356 string_vector argv = args.make_argv ("diary"); |
581
|
357 |
1755
|
358 if (error_state) |
|
359 return retval; |
|
360 |
|
361 if (diary_file.empty ()) |
|
362 diary_file = "diary"; |
1306
|
363 |
581
|
364 switch (argc) |
|
365 { |
|
366 case 1: |
|
367 write_to_diary_file = ! write_to_diary_file; |
|
368 open_diary_file (); |
|
369 break; |
623
|
370 |
581
|
371 case 2: |
|
372 { |
1755
|
373 string arg = argv[1]; |
|
374 |
|
375 if (arg == "on") |
581
|
376 { |
|
377 write_to_diary_file = 1; |
|
378 open_diary_file (); |
|
379 } |
1755
|
380 else if (arg == "off") |
2093
|
381 { |
|
382 close_diary_file (); |
|
383 write_to_diary_file = 0; |
|
384 } |
581
|
385 else |
|
386 { |
1755
|
387 diary_file = arg; |
581
|
388 open_diary_file (); |
|
389 } |
|
390 } |
|
391 break; |
777
|
392 |
581
|
393 default: |
|
394 print_usage ("diary"); |
|
395 break; |
|
396 } |
|
397 |
|
398 return retval; |
|
399 } |
|
400 |
1957
|
401 DEFUN_TEXT (more, args, , |
1409
|
402 "more on\n\ |
|
403 more off\n\ |
|
404 \n\ |
|
405 Turn output pagination on or off.") |
|
406 { |
2086
|
407 octave_value_list retval; |
1409
|
408 |
1755
|
409 int argc = args.length () + 1; |
|
410 |
1965
|
411 string_vector argv = args.make_argv ("more"); |
1755
|
412 |
|
413 if (error_state) |
|
414 return retval; |
1409
|
415 |
|
416 if (argc == 2) |
|
417 { |
1755
|
418 string arg = argv[1]; |
1409
|
419 |
1755
|
420 if (arg == "on") |
2448
|
421 bind_builtin_variable ("page_screen_output", 1.0); |
1755
|
422 else if (arg == "off") |
2448
|
423 bind_builtin_variable ("page_screen_output", 0.0); |
1409
|
424 else |
1755
|
425 error ("more: unrecognized argument `%s'", arg.c_str ()); |
1409
|
426 } |
|
427 else |
|
428 print_usage ("more"); |
|
429 |
|
430 return retval; |
|
431 } |
|
432 |
2097
|
433 static string |
|
434 default_pager (void) |
|
435 { |
2926
|
436 string pager_binary = octave_env::getenv ("PAGER"); |
2097
|
437 |
|
438 #ifdef DEFAULT_PAGER |
2926
|
439 if (pager_binary.empty ()) |
2097
|
440 { |
|
441 pager_binary = string (DEFAULT_PAGER); |
|
442 |
|
443 if (pager_binary == "less") |
|
444 { |
|
445 pager_binary.append (" -e"); |
|
446 |
2926
|
447 string lessflags = octave_env::getenv ("LESS"); |
|
448 if (lessflags.empty ()) |
2097
|
449 pager_binary.append |
2099
|
450 (" -P'-- less ?pB(%pB\\%):--. (f)orward, (b)ack, (q)uit$'"); |
2097
|
451 } |
|
452 } |
|
453 #endif |
|
454 |
|
455 return pager_binary; |
|
456 } |
|
457 |
2164
|
458 static int |
|
459 pager_binary (void) |
|
460 { |
|
461 int status = 0; |
|
462 |
|
463 string s = builtin_string_variable ("PAGER"); |
|
464 |
|
465 if (s.empty ()) |
|
466 { |
|
467 gripe_invalid_value_specified ("PAGER"); |
|
468 status = -1; |
|
469 } |
|
470 else |
|
471 Vpager_binary = s; |
|
472 |
|
473 return status; |
|
474 } |
|
475 |
|
476 static int |
|
477 page_output_immediately (void) |
|
478 { |
|
479 Vpage_output_immediately = check_preference ("page_output_immediately"); |
|
480 |
|
481 return 0; |
|
482 } |
|
483 |
|
484 static int |
|
485 page_screen_output (void) |
|
486 { |
|
487 Vpage_screen_output = check_preference ("page_screen_output"); |
|
488 |
|
489 return 0; |
|
490 } |
|
491 |
2097
|
492 void |
|
493 symbols_of_pager (void) |
|
494 { |
2164
|
495 DEFVAR (PAGER, default_pager (), 0, pager_binary, |
2097
|
496 "path to pager binary"); |
|
497 |
2146
|
498 DEFVAR (page_output_immediately, 0.0, 0, page_output_immediately, |
2102
|
499 "if paging output, start sending it as soon as it is available"); |
2100
|
500 |
2097
|
501 DEFVAR (page_screen_output, 1.0, 0, page_screen_output, |
|
502 "if possible, send output intended for the screen through the pager"); |
|
503 } |
|
504 |
1
|
505 /* |
|
506 ;;; Local Variables: *** |
|
507 ;;; mode: C++ *** |
|
508 ;;; End: *** |
|
509 */ |