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 |
|
126 char c; |
|
127 |
|
128 while ((c = *s++) != '\0') |
|
129 if (c == '\n') |
|
130 { |
|
131 count++; |
|
132 |
|
133 if (count > available_rows) |
|
134 return true; |
|
135 } |
|
136 } |
|
137 |
|
138 return false; |
|
139 } |
|
140 |
2093
|
141 int |
|
142 octave_pager_buf::sync (void) |
|
143 { |
2100
|
144 if (really_flush_to_pager |
|
145 || (user_pref.page_screen_output && user_pref.page_output_immediately) |
|
146 || ! user_pref.page_screen_output) |
|
147 { |
|
148 sputc ('\0'); |
2093
|
149 |
2100
|
150 char *buf = eback (); |
|
151 |
2101
|
152 bool bypass_pager = (really_flush_to_pager |
|
153 && user_pref.page_screen_output |
|
154 && ! user_pref.page_output_immediately |
|
155 && more_than_a_screenful (buf)); |
|
156 |
|
157 do_sync (buf, bypass_pager); |
2093
|
158 |
2100
|
159 octave_diary << buf; |
2093
|
160 |
2100
|
161 seekoff (0, ios::beg); |
|
162 } |
2093
|
163 |
|
164 return 0; |
|
165 } |
|
166 |
|
167 int |
|
168 octave_diary_buf::sync (void) |
|
169 { |
|
170 sputc ('\0'); |
|
171 |
|
172 if (write_to_diary_file && external_diary_file) |
|
173 external_diary_file << eback (); |
|
174 |
|
175 seekoff (0, ios::beg); |
|
176 |
|
177 return 0; |
|
178 } |
|
179 |
|
180 octave_pager_stream *octave_pager_stream::instance = 0; |
|
181 |
|
182 octave_pager_stream::octave_pager_stream (void) : ostream (), pb (0) |
1
|
183 { |
2093
|
184 pb = new octave_pager_buf; |
|
185 rdbuf (pb); |
|
186 setf (unitbuf); |
|
187 } |
|
188 |
|
189 octave_pager_stream::~octave_pager_stream (void) |
|
190 { |
|
191 flush (); |
|
192 delete pb; |
|
193 } |
|
194 |
|
195 octave_pager_stream& |
|
196 octave_pager_stream::stream (void) |
|
197 { |
|
198 if (! instance) |
|
199 instance = new octave_pager_stream (); |
|
200 |
|
201 return *instance; |
|
202 } |
|
203 |
|
204 octave_diary_stream *octave_diary_stream::instance = 0; |
|
205 |
|
206 octave_diary_stream::octave_diary_stream (void) : ostream (), db (0) |
|
207 { |
|
208 db = new octave_diary_buf; |
|
209 rdbuf (db); |
|
210 setf (unitbuf); |
|
211 } |
|
212 |
|
213 octave_diary_stream::~octave_diary_stream (void) |
|
214 { |
|
215 flush (); |
|
216 delete db; |
|
217 } |
|
218 |
|
219 octave_diary_stream& |
|
220 octave_diary_stream::stream (void) |
|
221 { |
|
222 if (! instance) |
|
223 instance = new octave_diary_stream (); |
|
224 |
|
225 return *instance; |
1
|
226 } |
|
227 |
|
228 void |
2093
|
229 flush_octave_stdout (void) |
1
|
230 { |
2100
|
231 begin_unwind_frame ("flush_octave_stdout"); |
|
232 |
|
233 unwind_protect_int (really_flush_to_pager); |
|
234 |
|
235 really_flush_to_pager = 1; |
|
236 |
2093
|
237 octave_stdout.flush (); |
1
|
238 |
2093
|
239 if (external_pager) |
1
|
240 { |
2093
|
241 octave_pager_pid = -1; |
1589
|
242 |
2093
|
243 delete external_pager; |
|
244 external_pager = 0; |
|
245 |
|
246 if (saved_sigint_handler) |
1589
|
247 { |
2093
|
248 octave_set_signal_handler (SIGINT, saved_sigint_handler); |
|
249 saved_sigint_handler = 0; |
1589
|
250 } |
1
|
251 } |
2100
|
252 |
|
253 run_unwind_frame ("flush_octave_stdout"); |
1
|
254 } |
|
255 |
1965
|
256 static void |
2093
|
257 close_diary_file (void) |
1
|
258 { |
2093
|
259 if (external_diary_file.is_open ()) |
1
|
260 { |
2093
|
261 octave_diary.flush (); |
|
262 external_diary_file.close (); |
1
|
263 } |
|
264 } |
|
265 |
581
|
266 static void |
|
267 open_diary_file (void) |
|
268 { |
2093
|
269 close_diary_file (); |
581
|
270 |
2093
|
271 external_diary_file.open (diary_file.c_str (), ios::app); |
581
|
272 |
2093
|
273 if (! external_diary_file) |
|
274 error ("diary: can't open diary file `%s'", diary_file.c_str ()); |
581
|
275 } |
|
276 |
1957
|
277 DEFUN_TEXT (diary, args, , |
581
|
278 "diary [on|off]\n\ |
|
279 diary [file]\n\ |
|
280 \n\ |
|
281 redirect all input and screen output to a file.") |
|
282 { |
2086
|
283 octave_value_list retval; |
581
|
284 |
1755
|
285 int argc = args.length () + 1; |
|
286 |
1965
|
287 string_vector argv = args.make_argv ("diary"); |
581
|
288 |
1755
|
289 if (error_state) |
|
290 return retval; |
|
291 |
|
292 if (diary_file.empty ()) |
|
293 diary_file = "diary"; |
1306
|
294 |
581
|
295 switch (argc) |
|
296 { |
|
297 case 1: |
|
298 write_to_diary_file = ! write_to_diary_file; |
|
299 open_diary_file (); |
|
300 break; |
623
|
301 |
581
|
302 case 2: |
|
303 { |
1755
|
304 string arg = argv[1]; |
|
305 |
|
306 if (arg == "on") |
581
|
307 { |
|
308 write_to_diary_file = 1; |
|
309 open_diary_file (); |
|
310 } |
1755
|
311 else if (arg == "off") |
2093
|
312 { |
|
313 close_diary_file (); |
|
314 write_to_diary_file = 0; |
|
315 } |
581
|
316 else |
|
317 { |
1755
|
318 diary_file = arg; |
581
|
319 open_diary_file (); |
|
320 } |
|
321 } |
|
322 break; |
777
|
323 |
581
|
324 default: |
|
325 print_usage ("diary"); |
|
326 break; |
|
327 } |
|
328 |
|
329 return retval; |
|
330 } |
|
331 |
1957
|
332 DEFUN_TEXT (more, args, , |
1409
|
333 "more on\n\ |
|
334 more off\n\ |
|
335 \n\ |
|
336 Turn output pagination on or off.") |
|
337 { |
2086
|
338 octave_value_list retval; |
1409
|
339 |
1755
|
340 int argc = args.length () + 1; |
|
341 |
1965
|
342 string_vector argv = args.make_argv ("more"); |
1755
|
343 |
|
344 if (error_state) |
|
345 return retval; |
1409
|
346 |
|
347 if (argc == 2) |
|
348 { |
1755
|
349 string arg = argv[1]; |
1409
|
350 |
1755
|
351 if (arg == "on") |
1409
|
352 bind_builtin_variable ("page_screen_output", "true"); |
1755
|
353 else if (arg == "off") |
1409
|
354 bind_builtin_variable ("page_screen_output", "false"); |
|
355 else |
1755
|
356 error ("more: unrecognized argument `%s'", arg.c_str ()); |
1409
|
357 } |
|
358 else |
|
359 print_usage ("more"); |
|
360 |
|
361 return retval; |
|
362 } |
|
363 |
2097
|
364 static string |
|
365 default_pager (void) |
|
366 { |
|
367 string pager_binary; |
|
368 |
|
369 char *pgr = getenv ("PAGER"); |
|
370 |
|
371 if (pgr) |
|
372 pager_binary = string (pgr); |
|
373 #ifdef DEFAULT_PAGER |
|
374 else |
|
375 { |
|
376 pager_binary = string (DEFAULT_PAGER); |
|
377 |
|
378 if (pager_binary == "less") |
|
379 { |
|
380 pager_binary.append (" -e"); |
|
381 |
|
382 if (! getenv ("LESS")) |
|
383 pager_binary.append |
2099
|
384 (" -P'-- less ?pB(%pB\\%):--. (f)orward, (b)ack, (q)uit$'"); |
2097
|
385 } |
|
386 } |
|
387 #endif |
|
388 |
|
389 return pager_binary; |
|
390 } |
|
391 |
|
392 void |
|
393 symbols_of_pager (void) |
|
394 { |
|
395 DEFVAR (PAGER, default_pager (), 0, sv_pager_binary, |
|
396 "path to pager binary"); |
|
397 |
2100
|
398 DEFVAR (page_output_immediately, 1.0, 0, page_output_immediately, |
2102
|
399 "if paging output, start sending it as soon as it is available"); |
2100
|
400 |
2097
|
401 DEFVAR (page_screen_output, 1.0, 0, page_screen_output, |
|
402 "if possible, send output intended for the screen through the pager"); |
|
403 } |
|
404 |
1
|
405 /* |
|
406 ;;; Local Variables: *** |
|
407 ;;; mode: C++ *** |
|
408 ;;; End: *** |
|
409 */ |