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