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