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