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