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