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 #include <cstdlib> |
|
29 |
1755
|
30 #include <string> |
|
31 |
1
|
32 #include <iostream.h> |
|
33 #include <strstream.h> |
581
|
34 #include <fstream.h> |
1
|
35 |
|
36 #include "procstream.h" |
|
37 |
1806
|
38 #include "oct-term.h" |
1755
|
39 #include "str-vec.h" |
|
40 |
1352
|
41 #include "defun.h" |
|
42 #include "error.h" |
|
43 #include "help.h" |
|
44 #include "input.h" |
1755
|
45 #include "oct-obj.h" |
1352
|
46 #include "pager.h" |
1755
|
47 #include "pt-const.h" |
990
|
48 #include "sighandlers.h" |
1449
|
49 #include "unwind-prot.h" |
1
|
50 #include "user-prefs.h" |
581
|
51 #include "utils.h" |
1409
|
52 #include "variables.h" |
1
|
53 |
|
54 // Where we stash output headed for the screen. |
529
|
55 static ostrstream *pager_buf = 0; |
1
|
56 |
581
|
57 // Nonzero means we write to the diary file. |
|
58 static int write_to_diary_file = 0; |
|
59 |
|
60 // The name of the current diary file. |
1755
|
61 static string diary_file; |
581
|
62 |
|
63 // The diary file. |
|
64 static ofstream diary_stream; |
|
65 |
1
|
66 static int |
|
67 line_count (char *s) |
|
68 { |
|
69 int count = 0; |
529
|
70 if (s) |
1
|
71 { |
|
72 char c; |
|
73 while ((c = *s++) != '\0') |
|
74 if (c == '\n') |
|
75 count++; |
|
76 } |
|
77 return count; |
|
78 } |
|
79 |
|
80 void |
|
81 initialize_pager (void) |
|
82 { |
|
83 delete pager_buf; |
|
84 pager_buf = new ostrstream (); |
|
85 } |
|
86 |
|
87 void |
|
88 maybe_page_output (ostrstream& msg_buf) |
|
89 { |
|
90 msg_buf << ends; |
|
91 |
|
92 char *message = msg_buf.str (); |
|
93 |
1589
|
94 if (message) |
1
|
95 { |
1589
|
96 maybe_write_to_diary_file (message); |
|
97 |
|
98 if (interactive |
|
99 && user_pref.page_screen_output |
1755
|
100 && ! user_pref.pager_binary.empty ()) |
1589
|
101 { |
|
102 *pager_buf << message; |
|
103 } |
|
104 else |
|
105 { |
|
106 cout << message; |
|
107 cout.flush (); |
|
108 } |
|
109 |
1
|
110 delete [] message; |
|
111 } |
|
112 } |
|
113 |
1965
|
114 static void |
|
115 cleanup_oprocstream (void *p) |
|
116 { |
|
117 delete (oprocstream *) p; |
|
118 } |
|
119 |
1
|
120 void |
|
121 flush_output_to_pager (void) |
|
122 { |
1358
|
123 // Extract message from buffer, then delete the buffer so that any |
|
124 // new messages get sent separately. |
1
|
125 |
988
|
126 *pager_buf << ends; |
1
|
127 char *message = pager_buf->str (); |
988
|
128 initialize_pager (); |
1
|
129 |
529
|
130 if (! message || ! *message) |
1
|
131 { |
|
132 delete [] message; |
|
133 return; |
|
134 } |
|
135 |
|
136 int nlines = line_count (message); |
|
137 |
|
138 if (nlines > terminal_rows () - 2) |
|
139 { |
1755
|
140 string pgr = user_pref.pager_binary; |
|
141 |
|
142 if (! pgr.empty ()) |
1
|
143 { |
1449
|
144 volatile sig_handler *old_sigint_handler; |
|
145 old_sigint_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
|
146 |
1755
|
147 oprocstream *pager_stream = new oprocstream (pgr.c_str ()); |
1449
|
148 |
|
149 add_unwind_protect (cleanup_oprocstream, pager_stream); |
|
150 |
|
151 int output_paged = 0; |
|
152 if (pager_stream && *pager_stream) |
1
|
153 { |
1449
|
154 output_paged = 1; |
|
155 *pager_stream << message; |
988
|
156 delete [] message; |
1449
|
157 pager_stream->flush (); |
|
158 pager_stream->close (); |
|
159 } |
990
|
160 |
1449
|
161 run_unwind_protect (); |
990
|
162 |
1449
|
163 octave_set_signal_handler (SIGINT, old_sigint_handler); |
|
164 |
|
165 if (output_paged) |
|
166 return; |
1
|
167 } |
|
168 } |
|
169 |
|
170 cout << message; |
988
|
171 delete [] message; |
1
|
172 cout.flush (); |
|
173 } |
|
174 |
581
|
175 static void |
|
176 open_diary_file (void) |
|
177 { |
|
178 if (diary_stream.is_open ()) |
|
179 diary_stream.close (); |
|
180 |
1755
|
181 diary_stream.open (diary_file.c_str (), ios::app); |
581
|
182 |
|
183 if (! diary_stream) |
1755
|
184 error ("diary: can't open diary file `%s'", diary_file.c_str ()); |
581
|
185 } |
|
186 |
|
187 void |
|
188 close_diary_file (void) |
|
189 { |
|
190 if (diary_stream) |
|
191 diary_stream.close (); |
|
192 } |
|
193 |
|
194 void |
1760
|
195 maybe_write_to_diary_file (const string& s) |
581
|
196 { |
|
197 if (write_to_diary_file && diary_stream) |
|
198 diary_stream << s; |
|
199 } |
|
200 |
1957
|
201 DEFUN_TEXT (diary, args, , |
581
|
202 "diary [on|off]\n\ |
|
203 diary [file]\n\ |
|
204 \n\ |
|
205 redirect all input and screen output to a file.") |
|
206 { |
2086
|
207 octave_value_list retval; |
581
|
208 |
1755
|
209 int argc = args.length () + 1; |
|
210 |
1965
|
211 string_vector argv = args.make_argv ("diary"); |
581
|
212 |
1755
|
213 if (error_state) |
|
214 return retval; |
|
215 |
|
216 if (diary_file.empty ()) |
|
217 diary_file = "diary"; |
1306
|
218 |
581
|
219 switch (argc) |
|
220 { |
|
221 case 1: |
|
222 write_to_diary_file = ! write_to_diary_file; |
|
223 open_diary_file (); |
|
224 break; |
623
|
225 |
581
|
226 case 2: |
|
227 { |
1755
|
228 string arg = argv[1]; |
|
229 |
|
230 if (arg == "on") |
581
|
231 { |
|
232 write_to_diary_file = 1; |
|
233 open_diary_file (); |
|
234 } |
1755
|
235 else if (arg == "off") |
581
|
236 write_to_diary_file = 0; |
|
237 else |
|
238 { |
1755
|
239 diary_file = arg; |
581
|
240 open_diary_file (); |
|
241 } |
|
242 } |
|
243 break; |
777
|
244 |
581
|
245 default: |
|
246 print_usage ("diary"); |
|
247 break; |
|
248 } |
|
249 |
|
250 return retval; |
|
251 } |
|
252 |
1957
|
253 DEFUN_TEXT (more, args, , |
1409
|
254 "more on\n\ |
|
255 more off\n\ |
|
256 \n\ |
|
257 Turn output pagination on or off.") |
|
258 { |
2086
|
259 octave_value_list retval; |
1409
|
260 |
1755
|
261 int argc = args.length () + 1; |
|
262 |
1965
|
263 string_vector argv = args.make_argv ("more"); |
1755
|
264 |
|
265 if (error_state) |
|
266 return retval; |
1409
|
267 |
|
268 if (argc == 2) |
|
269 { |
1755
|
270 string arg = argv[1]; |
1409
|
271 |
1755
|
272 if (arg == "on") |
1409
|
273 bind_builtin_variable ("page_screen_output", "true"); |
1755
|
274 else if (arg == "off") |
1409
|
275 bind_builtin_variable ("page_screen_output", "false"); |
|
276 else |
1755
|
277 error ("more: unrecognized argument `%s'", arg.c_str ()); |
1409
|
278 } |
|
279 else |
|
280 print_usage ("more"); |
|
281 |
|
282 return retval; |
|
283 } |
|
284 |
1
|
285 /* |
|
286 ;;; Local Variables: *** |
|
287 ;;; mode: C++ *** |
|
288 ;;; End: *** |
|
289 */ |