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