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