1
|
1 // pager.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 |
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 |
581
|
80 // For now, use the variables from readline. It already handles |
|
81 // SIGWINCH, so these values have a good chance of being correct even |
|
82 // if the window changes size (they will be wrong if, for example, the |
|
83 // luser changes the window size while the pager is running, and the |
|
84 // signal is handled by the pager instead of us. |
|
85 |
1
|
86 int |
|
87 terminal_columns (void) |
|
88 { |
|
89 extern int screenwidth; |
|
90 return screenwidth > 0 ? screenwidth : 80; |
|
91 } |
|
92 |
|
93 int |
|
94 terminal_rows (void) |
|
95 { |
|
96 extern int screenheight; |
|
97 return screenheight > 0 ? screenheight : 24; |
|
98 } |
|
99 |
|
100 void |
|
101 initialize_pager (void) |
|
102 { |
|
103 delete pager_buf; |
|
104 pager_buf = new ostrstream (); |
|
105 } |
|
106 |
|
107 void |
|
108 maybe_page_output (ostrstream& msg_buf) |
|
109 { |
|
110 msg_buf << ends; |
|
111 |
|
112 char *message = msg_buf.str (); |
|
113 |
1589
|
114 if (message) |
1
|
115 { |
1589
|
116 maybe_write_to_diary_file (message); |
|
117 |
|
118 if (interactive |
|
119 && user_pref.page_screen_output |
1755
|
120 && ! user_pref.pager_binary.empty ()) |
1589
|
121 { |
|
122 *pager_buf << message; |
|
123 } |
|
124 else |
|
125 { |
|
126 cout << message; |
|
127 cout.flush (); |
|
128 } |
|
129 |
1
|
130 delete [] message; |
|
131 } |
|
132 } |
|
133 |
|
134 void |
|
135 flush_output_to_pager (void) |
|
136 { |
1358
|
137 // Extract message from buffer, then delete the buffer so that any |
|
138 // new messages get sent separately. |
1
|
139 |
988
|
140 *pager_buf << ends; |
1
|
141 char *message = pager_buf->str (); |
988
|
142 initialize_pager (); |
1
|
143 |
529
|
144 if (! message || ! *message) |
1
|
145 { |
|
146 delete [] message; |
|
147 return; |
|
148 } |
|
149 |
|
150 int nlines = line_count (message); |
|
151 |
|
152 if (nlines > terminal_rows () - 2) |
|
153 { |
1755
|
154 string pgr = user_pref.pager_binary; |
|
155 |
|
156 if (! pgr.empty ()) |
1
|
157 { |
1449
|
158 volatile sig_handler *old_sigint_handler; |
|
159 old_sigint_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
|
160 |
1755
|
161 oprocstream *pager_stream = new oprocstream (pgr.c_str ()); |
1449
|
162 |
|
163 add_unwind_protect (cleanup_oprocstream, pager_stream); |
|
164 |
|
165 int output_paged = 0; |
|
166 if (pager_stream && *pager_stream) |
1
|
167 { |
1449
|
168 output_paged = 1; |
|
169 *pager_stream << message; |
988
|
170 delete [] message; |
1449
|
171 pager_stream->flush (); |
|
172 pager_stream->close (); |
|
173 } |
990
|
174 |
1449
|
175 run_unwind_protect (); |
990
|
176 |
1449
|
177 octave_set_signal_handler (SIGINT, old_sigint_handler); |
|
178 |
|
179 if (output_paged) |
|
180 return; |
1
|
181 } |
|
182 } |
|
183 |
|
184 cout << message; |
988
|
185 delete [] message; |
1
|
186 cout.flush (); |
|
187 } |
|
188 |
581
|
189 static void |
|
190 open_diary_file (void) |
|
191 { |
|
192 if (diary_stream.is_open ()) |
|
193 diary_stream.close (); |
|
194 |
1755
|
195 diary_stream.open (diary_file.c_str (), ios::app); |
581
|
196 |
|
197 if (! diary_stream) |
1755
|
198 error ("diary: can't open diary file `%s'", diary_file.c_str ()); |
581
|
199 } |
|
200 |
|
201 void |
|
202 close_diary_file (void) |
|
203 { |
|
204 if (diary_stream) |
|
205 diary_stream.close (); |
|
206 } |
|
207 |
|
208 void |
|
209 maybe_write_to_diary_file (const char *s) |
|
210 { |
|
211 if (write_to_diary_file && diary_stream) |
|
212 diary_stream << s; |
|
213 } |
|
214 |
1488
|
215 DEFUN_TEXT ("diary", Fdiary, Sdiary, 10, |
581
|
216 "diary [on|off]\n\ |
|
217 diary [file]\n\ |
|
218 \n\ |
|
219 redirect all input and screen output to a file.") |
|
220 { |
|
221 Octave_object retval; |
|
222 |
1755
|
223 int argc = args.length () + 1; |
|
224 |
|
225 string_vector argv = make_argv (args, "diary"); |
581
|
226 |
1755
|
227 if (error_state) |
|
228 return retval; |
|
229 |
|
230 if (diary_file.empty ()) |
|
231 diary_file = "diary"; |
1306
|
232 |
581
|
233 switch (argc) |
|
234 { |
|
235 case 1: |
|
236 write_to_diary_file = ! write_to_diary_file; |
|
237 open_diary_file (); |
|
238 break; |
623
|
239 |
581
|
240 case 2: |
|
241 { |
1755
|
242 string arg = argv[1]; |
|
243 |
|
244 if (arg == "on") |
581
|
245 { |
|
246 write_to_diary_file = 1; |
|
247 open_diary_file (); |
|
248 } |
1755
|
249 else if (arg == "off") |
581
|
250 write_to_diary_file = 0; |
|
251 else |
|
252 { |
1755
|
253 diary_file = arg; |
581
|
254 open_diary_file (); |
|
255 } |
|
256 } |
|
257 break; |
777
|
258 |
581
|
259 default: |
|
260 print_usage ("diary"); |
|
261 break; |
|
262 } |
|
263 |
|
264 return retval; |
|
265 } |
|
266 |
1488
|
267 DEFUN_TEXT ("more", Fmore, Smore, 10, |
1409
|
268 "more on\n\ |
|
269 more off\n\ |
|
270 \n\ |
|
271 Turn output pagination on or off.") |
|
272 { |
|
273 Octave_object retval; |
|
274 |
1755
|
275 int argc = args.length () + 1; |
|
276 |
|
277 string_vector argv = make_argv (args, "more"); |
|
278 |
|
279 if (error_state) |
|
280 return retval; |
1409
|
281 |
|
282 if (argc == 2) |
|
283 { |
1755
|
284 string arg = argv[1]; |
1409
|
285 |
1755
|
286 if (arg == "on") |
1409
|
287 bind_builtin_variable ("page_screen_output", "true"); |
1755
|
288 else if (arg == "off") |
1409
|
289 bind_builtin_variable ("page_screen_output", "false"); |
|
290 else |
1755
|
291 error ("more: unrecognized argument `%s'", arg.c_str ()); |
1409
|
292 } |
|
293 else |
|
294 print_usage ("more"); |
|
295 |
|
296 return retval; |
|
297 } |
|
298 |
1
|
299 /* |
|
300 ;;; Local Variables: *** |
|
301 ;;; mode: C++ *** |
|
302 ;;; page-delimiter: "^/\\*" *** |
|
303 ;;; End: *** |
|
304 */ |