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