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