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