1
|
1 // pager.cc -*- C++ -*- |
|
2 /* |
|
3 |
1884
|
4 Copyright (C) 1996 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <csignal> |
|
29 #include <cstdlib> |
|
30 |
1755
|
31 #include <string> |
|
32 |
1
|
33 #include <iostream.h> |
|
34 #include <strstream.h> |
581
|
35 #include <fstream.h> |
1
|
36 |
|
37 #include "procstream.h" |
|
38 |
1806
|
39 #include "oct-term.h" |
1755
|
40 #include "str-vec.h" |
|
41 |
1352
|
42 #include "defun.h" |
|
43 #include "error.h" |
|
44 #include "help.h" |
|
45 #include "input.h" |
1755
|
46 #include "oct-obj.h" |
1352
|
47 #include "pager.h" |
1755
|
48 #include "pt-const.h" |
990
|
49 #include "sighandlers.h" |
1449
|
50 #include "unwind-prot.h" |
1
|
51 #include "user-prefs.h" |
581
|
52 #include "utils.h" |
1409
|
53 #include "variables.h" |
1
|
54 |
|
55 // Where we stash output headed for the screen. |
529
|
56 static ostrstream *pager_buf = 0; |
1
|
57 |
581
|
58 // Nonzero means we write to the diary file. |
|
59 static int write_to_diary_file = 0; |
|
60 |
|
61 // The name of the current diary file. |
1755
|
62 static string diary_file; |
581
|
63 |
|
64 // The diary file. |
|
65 static ofstream diary_stream; |
|
66 |
1
|
67 static int |
|
68 line_count (char *s) |
|
69 { |
|
70 int count = 0; |
529
|
71 if (s) |
1
|
72 { |
|
73 char c; |
|
74 while ((c = *s++) != '\0') |
|
75 if (c == '\n') |
|
76 count++; |
|
77 } |
|
78 return count; |
|
79 } |
|
80 |
|
81 void |
|
82 initialize_pager (void) |
|
83 { |
|
84 delete pager_buf; |
|
85 pager_buf = new ostrstream (); |
|
86 } |
|
87 |
|
88 void |
|
89 maybe_page_output (ostrstream& msg_buf) |
|
90 { |
|
91 msg_buf << ends; |
|
92 |
|
93 char *message = msg_buf.str (); |
|
94 |
1589
|
95 if (message) |
1
|
96 { |
1589
|
97 maybe_write_to_diary_file (message); |
|
98 |
|
99 if (interactive |
|
100 && user_pref.page_screen_output |
1755
|
101 && ! user_pref.pager_binary.empty ()) |
1589
|
102 { |
|
103 *pager_buf << message; |
|
104 } |
|
105 else |
|
106 { |
|
107 cout << message; |
|
108 cout.flush (); |
|
109 } |
|
110 |
1
|
111 delete [] message; |
|
112 } |
|
113 } |
|
114 |
1965
|
115 static void |
|
116 cleanup_oprocstream (void *p) |
|
117 { |
|
118 delete (oprocstream *) p; |
|
119 } |
|
120 |
1
|
121 void |
|
122 flush_output_to_pager (void) |
|
123 { |
1358
|
124 // Extract message from buffer, then delete the buffer so that any |
|
125 // new messages get sent separately. |
1
|
126 |
988
|
127 *pager_buf << ends; |
1
|
128 char *message = pager_buf->str (); |
988
|
129 initialize_pager (); |
1
|
130 |
529
|
131 if (! message || ! *message) |
1
|
132 { |
|
133 delete [] message; |
|
134 return; |
|
135 } |
|
136 |
|
137 int nlines = line_count (message); |
|
138 |
|
139 if (nlines > terminal_rows () - 2) |
|
140 { |
1755
|
141 string pgr = user_pref.pager_binary; |
|
142 |
|
143 if (! pgr.empty ()) |
1
|
144 { |
1449
|
145 volatile sig_handler *old_sigint_handler; |
|
146 old_sigint_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
|
147 |
1755
|
148 oprocstream *pager_stream = new oprocstream (pgr.c_str ()); |
1449
|
149 |
|
150 add_unwind_protect (cleanup_oprocstream, pager_stream); |
|
151 |
|
152 int output_paged = 0; |
|
153 if (pager_stream && *pager_stream) |
1
|
154 { |
1449
|
155 output_paged = 1; |
|
156 *pager_stream << message; |
988
|
157 delete [] message; |
1449
|
158 pager_stream->flush (); |
|
159 pager_stream->close (); |
|
160 } |
990
|
161 |
1449
|
162 run_unwind_protect (); |
990
|
163 |
1449
|
164 octave_set_signal_handler (SIGINT, old_sigint_handler); |
|
165 |
|
166 if (output_paged) |
|
167 return; |
1
|
168 } |
|
169 } |
|
170 |
|
171 cout << message; |
988
|
172 delete [] message; |
1
|
173 cout.flush (); |
|
174 } |
|
175 |
581
|
176 static void |
|
177 open_diary_file (void) |
|
178 { |
|
179 if (diary_stream.is_open ()) |
|
180 diary_stream.close (); |
|
181 |
1755
|
182 diary_stream.open (diary_file.c_str (), ios::app); |
581
|
183 |
|
184 if (! diary_stream) |
1755
|
185 error ("diary: can't open diary file `%s'", diary_file.c_str ()); |
581
|
186 } |
|
187 |
|
188 void |
|
189 close_diary_file (void) |
|
190 { |
|
191 if (diary_stream) |
|
192 diary_stream.close (); |
|
193 } |
|
194 |
|
195 void |
1760
|
196 maybe_write_to_diary_file (const string& s) |
581
|
197 { |
|
198 if (write_to_diary_file && diary_stream) |
|
199 diary_stream << s; |
|
200 } |
|
201 |
1957
|
202 DEFUN_TEXT (diary, args, , |
581
|
203 "diary [on|off]\n\ |
|
204 diary [file]\n\ |
|
205 \n\ |
|
206 redirect all input and screen output to a file.") |
|
207 { |
|
208 Octave_object retval; |
|
209 |
1755
|
210 int argc = args.length () + 1; |
|
211 |
1965
|
212 string_vector argv = args.make_argv ("diary"); |
581
|
213 |
1755
|
214 if (error_state) |
|
215 return retval; |
|
216 |
|
217 if (diary_file.empty ()) |
|
218 diary_file = "diary"; |
1306
|
219 |
581
|
220 switch (argc) |
|
221 { |
|
222 case 1: |
|
223 write_to_diary_file = ! write_to_diary_file; |
|
224 open_diary_file (); |
|
225 break; |
623
|
226 |
581
|
227 case 2: |
|
228 { |
1755
|
229 string arg = argv[1]; |
|
230 |
|
231 if (arg == "on") |
581
|
232 { |
|
233 write_to_diary_file = 1; |
|
234 open_diary_file (); |
|
235 } |
1755
|
236 else if (arg == "off") |
581
|
237 write_to_diary_file = 0; |
|
238 else |
|
239 { |
1755
|
240 diary_file = arg; |
581
|
241 open_diary_file (); |
|
242 } |
|
243 } |
|
244 break; |
777
|
245 |
581
|
246 default: |
|
247 print_usage ("diary"); |
|
248 break; |
|
249 } |
|
250 |
|
251 return retval; |
|
252 } |
|
253 |
1957
|
254 DEFUN_TEXT (more, args, , |
1409
|
255 "more on\n\ |
|
256 more off\n\ |
|
257 \n\ |
|
258 Turn output pagination on or off.") |
|
259 { |
|
260 Octave_object retval; |
|
261 |
1755
|
262 int argc = args.length () + 1; |
|
263 |
1965
|
264 string_vector argv = args.make_argv ("more"); |
1755
|
265 |
|
266 if (error_state) |
|
267 return retval; |
1409
|
268 |
|
269 if (argc == 2) |
|
270 { |
1755
|
271 string arg = argv[1]; |
1409
|
272 |
1755
|
273 if (arg == "on") |
1409
|
274 bind_builtin_variable ("page_screen_output", "true"); |
1755
|
275 else if (arg == "off") |
1409
|
276 bind_builtin_variable ("page_screen_output", "false"); |
|
277 else |
1755
|
278 error ("more: unrecognized argument `%s'", arg.c_str ()); |
1409
|
279 } |
|
280 else |
|
281 print_usage ("more"); |
|
282 |
|
283 return retval; |
|
284 } |
|
285 |
1
|
286 /* |
|
287 ;;; Local Variables: *** |
|
288 ;;; mode: C++ *** |
|
289 ;;; page-delimiter: "^/\\*" *** |
|
290 ;;; End: *** |
|
291 */ |