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