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