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