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 { |
2185
|
209 really_flush_to_pager = 1; |
2093
|
210 flush (); |
|
211 delete pb; |
|
212 } |
|
213 |
|
214 octave_pager_stream& |
|
215 octave_pager_stream::stream (void) |
|
216 { |
|
217 if (! instance) |
|
218 instance = new octave_pager_stream (); |
|
219 |
|
220 return *instance; |
|
221 } |
|
222 |
|
223 octave_diary_stream *octave_diary_stream::instance = 0; |
|
224 |
|
225 octave_diary_stream::octave_diary_stream (void) : ostream (), db (0) |
|
226 { |
|
227 db = new octave_diary_buf; |
|
228 rdbuf (db); |
|
229 setf (unitbuf); |
|
230 } |
|
231 |
|
232 octave_diary_stream::~octave_diary_stream (void) |
|
233 { |
|
234 flush (); |
|
235 delete db; |
|
236 } |
|
237 |
|
238 octave_diary_stream& |
|
239 octave_diary_stream::stream (void) |
|
240 { |
|
241 if (! instance) |
|
242 instance = new octave_diary_stream (); |
|
243 |
|
244 return *instance; |
1
|
245 } |
|
246 |
|
247 void |
2093
|
248 flush_octave_stdout (void) |
1
|
249 { |
2100
|
250 begin_unwind_frame ("flush_octave_stdout"); |
|
251 |
|
252 unwind_protect_int (really_flush_to_pager); |
|
253 |
|
254 really_flush_to_pager = 1; |
|
255 |
2093
|
256 octave_stdout.flush (); |
1
|
257 |
2093
|
258 if (external_pager) |
1
|
259 { |
2093
|
260 octave_pager_pid = -1; |
1589
|
261 |
2093
|
262 delete external_pager; |
|
263 external_pager = 0; |
|
264 |
|
265 if (saved_sigint_handler) |
1589
|
266 { |
2093
|
267 octave_set_signal_handler (SIGINT, saved_sigint_handler); |
|
268 saved_sigint_handler = 0; |
1589
|
269 } |
1
|
270 } |
2100
|
271 |
|
272 run_unwind_frame ("flush_octave_stdout"); |
1
|
273 } |
|
274 |
1965
|
275 static void |
2093
|
276 close_diary_file (void) |
1
|
277 { |
2093
|
278 if (external_diary_file.is_open ()) |
1
|
279 { |
2093
|
280 octave_diary.flush (); |
|
281 external_diary_file.close (); |
1
|
282 } |
|
283 } |
|
284 |
581
|
285 static void |
|
286 open_diary_file (void) |
|
287 { |
2093
|
288 close_diary_file (); |
581
|
289 |
2093
|
290 external_diary_file.open (diary_file.c_str (), ios::app); |
581
|
291 |
2093
|
292 if (! external_diary_file) |
|
293 error ("diary: can't open diary file `%s'", diary_file.c_str ()); |
581
|
294 } |
|
295 |
1957
|
296 DEFUN_TEXT (diary, args, , |
581
|
297 "diary [on|off]\n\ |
|
298 diary [file]\n\ |
|
299 \n\ |
|
300 redirect all input and screen output to a file.") |
|
301 { |
2086
|
302 octave_value_list retval; |
581
|
303 |
1755
|
304 int argc = args.length () + 1; |
|
305 |
1965
|
306 string_vector argv = args.make_argv ("diary"); |
581
|
307 |
1755
|
308 if (error_state) |
|
309 return retval; |
|
310 |
|
311 if (diary_file.empty ()) |
|
312 diary_file = "diary"; |
1306
|
313 |
581
|
314 switch (argc) |
|
315 { |
|
316 case 1: |
|
317 write_to_diary_file = ! write_to_diary_file; |
|
318 open_diary_file (); |
|
319 break; |
623
|
320 |
581
|
321 case 2: |
|
322 { |
1755
|
323 string arg = argv[1]; |
|
324 |
|
325 if (arg == "on") |
581
|
326 { |
|
327 write_to_diary_file = 1; |
|
328 open_diary_file (); |
|
329 } |
1755
|
330 else if (arg == "off") |
2093
|
331 { |
|
332 close_diary_file (); |
|
333 write_to_diary_file = 0; |
|
334 } |
581
|
335 else |
|
336 { |
1755
|
337 diary_file = arg; |
581
|
338 open_diary_file (); |
|
339 } |
|
340 } |
|
341 break; |
777
|
342 |
581
|
343 default: |
|
344 print_usage ("diary"); |
|
345 break; |
|
346 } |
|
347 |
|
348 return retval; |
|
349 } |
|
350 |
1957
|
351 DEFUN_TEXT (more, args, , |
1409
|
352 "more on\n\ |
|
353 more off\n\ |
|
354 \n\ |
|
355 Turn output pagination on or off.") |
|
356 { |
2086
|
357 octave_value_list retval; |
1409
|
358 |
1755
|
359 int argc = args.length () + 1; |
|
360 |
1965
|
361 string_vector argv = args.make_argv ("more"); |
1755
|
362 |
|
363 if (error_state) |
|
364 return retval; |
1409
|
365 |
|
366 if (argc == 2) |
|
367 { |
1755
|
368 string arg = argv[1]; |
1409
|
369 |
1755
|
370 if (arg == "on") |
1409
|
371 bind_builtin_variable ("page_screen_output", "true"); |
1755
|
372 else if (arg == "off") |
1409
|
373 bind_builtin_variable ("page_screen_output", "false"); |
|
374 else |
1755
|
375 error ("more: unrecognized argument `%s'", arg.c_str ()); |
1409
|
376 } |
|
377 else |
|
378 print_usage ("more"); |
|
379 |
|
380 return retval; |
|
381 } |
|
382 |
2097
|
383 static string |
|
384 default_pager (void) |
|
385 { |
|
386 string pager_binary; |
|
387 |
|
388 char *pgr = getenv ("PAGER"); |
|
389 |
|
390 if (pgr) |
|
391 pager_binary = string (pgr); |
|
392 #ifdef DEFAULT_PAGER |
|
393 else |
|
394 { |
|
395 pager_binary = string (DEFAULT_PAGER); |
|
396 |
|
397 if (pager_binary == "less") |
|
398 { |
|
399 pager_binary.append (" -e"); |
|
400 |
|
401 if (! getenv ("LESS")) |
|
402 pager_binary.append |
2099
|
403 (" -P'-- less ?pB(%pB\\%):--. (f)orward, (b)ack, (q)uit$'"); |
2097
|
404 } |
|
405 } |
|
406 #endif |
|
407 |
|
408 return pager_binary; |
|
409 } |
|
410 |
2164
|
411 static int |
|
412 pager_binary (void) |
|
413 { |
|
414 int status = 0; |
|
415 |
|
416 string s = builtin_string_variable ("PAGER"); |
|
417 |
|
418 if (s.empty ()) |
|
419 { |
|
420 gripe_invalid_value_specified ("PAGER"); |
|
421 status = -1; |
|
422 } |
|
423 else |
|
424 Vpager_binary = s; |
|
425 |
|
426 return status; |
|
427 } |
|
428 |
|
429 static int |
|
430 page_output_immediately (void) |
|
431 { |
|
432 Vpage_output_immediately = check_preference ("page_output_immediately"); |
|
433 |
|
434 return 0; |
|
435 } |
|
436 |
|
437 static int |
|
438 page_screen_output (void) |
|
439 { |
|
440 Vpage_screen_output = check_preference ("page_screen_output"); |
|
441 |
|
442 return 0; |
|
443 } |
|
444 |
2097
|
445 void |
|
446 symbols_of_pager (void) |
|
447 { |
2164
|
448 DEFVAR (PAGER, default_pager (), 0, pager_binary, |
2097
|
449 "path to pager binary"); |
|
450 |
2146
|
451 DEFVAR (page_output_immediately, 0.0, 0, page_output_immediately, |
2102
|
452 "if paging output, start sending it as soon as it is available"); |
2100
|
453 |
2097
|
454 DEFVAR (page_screen_output, 1.0, 0, page_screen_output, |
|
455 "if possible, send output intended for the screen through the pager"); |
|
456 } |
|
457 |
1
|
458 /* |
|
459 ;;; Local Variables: *** |
|
460 ;;; mode: C++ *** |
|
461 ;;; End: *** |
|
462 */ |