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