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