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 |
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 |
|
115 void |
|
116 flush_output_to_pager (void) |
|
117 { |
1358
|
118 // Extract message from buffer, then delete the buffer so that any |
|
119 // new messages get sent separately. |
1
|
120 |
988
|
121 *pager_buf << ends; |
1
|
122 char *message = pager_buf->str (); |
988
|
123 initialize_pager (); |
1
|
124 |
529
|
125 if (! message || ! *message) |
1
|
126 { |
|
127 delete [] message; |
|
128 return; |
|
129 } |
|
130 |
|
131 int nlines = line_count (message); |
|
132 |
|
133 if (nlines > terminal_rows () - 2) |
|
134 { |
1755
|
135 string pgr = user_pref.pager_binary; |
|
136 |
|
137 if (! pgr.empty ()) |
1
|
138 { |
1449
|
139 volatile sig_handler *old_sigint_handler; |
|
140 old_sigint_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
|
141 |
1755
|
142 oprocstream *pager_stream = new oprocstream (pgr.c_str ()); |
1449
|
143 |
|
144 add_unwind_protect (cleanup_oprocstream, pager_stream); |
|
145 |
|
146 int output_paged = 0; |
|
147 if (pager_stream && *pager_stream) |
1
|
148 { |
1449
|
149 output_paged = 1; |
|
150 *pager_stream << message; |
988
|
151 delete [] message; |
1449
|
152 pager_stream->flush (); |
|
153 pager_stream->close (); |
|
154 } |
990
|
155 |
1449
|
156 run_unwind_protect (); |
990
|
157 |
1449
|
158 octave_set_signal_handler (SIGINT, old_sigint_handler); |
|
159 |
|
160 if (output_paged) |
|
161 return; |
1
|
162 } |
|
163 } |
|
164 |
|
165 cout << message; |
988
|
166 delete [] message; |
1
|
167 cout.flush (); |
|
168 } |
|
169 |
581
|
170 static void |
|
171 open_diary_file (void) |
|
172 { |
|
173 if (diary_stream.is_open ()) |
|
174 diary_stream.close (); |
|
175 |
1755
|
176 diary_stream.open (diary_file.c_str (), ios::app); |
581
|
177 |
|
178 if (! diary_stream) |
1755
|
179 error ("diary: can't open diary file `%s'", diary_file.c_str ()); |
581
|
180 } |
|
181 |
|
182 void |
|
183 close_diary_file (void) |
|
184 { |
|
185 if (diary_stream) |
|
186 diary_stream.close (); |
|
187 } |
|
188 |
|
189 void |
1760
|
190 maybe_write_to_diary_file (const string& s) |
581
|
191 { |
|
192 if (write_to_diary_file && diary_stream) |
|
193 diary_stream << s; |
|
194 } |
|
195 |
1488
|
196 DEFUN_TEXT ("diary", Fdiary, Sdiary, 10, |
581
|
197 "diary [on|off]\n\ |
|
198 diary [file]\n\ |
|
199 \n\ |
|
200 redirect all input and screen output to a file.") |
|
201 { |
|
202 Octave_object retval; |
|
203 |
1755
|
204 int argc = args.length () + 1; |
|
205 |
|
206 string_vector argv = make_argv (args, "diary"); |
581
|
207 |
1755
|
208 if (error_state) |
|
209 return retval; |
|
210 |
|
211 if (diary_file.empty ()) |
|
212 diary_file = "diary"; |
1306
|
213 |
581
|
214 switch (argc) |
|
215 { |
|
216 case 1: |
|
217 write_to_diary_file = ! write_to_diary_file; |
|
218 open_diary_file (); |
|
219 break; |
623
|
220 |
581
|
221 case 2: |
|
222 { |
1755
|
223 string arg = argv[1]; |
|
224 |
|
225 if (arg == "on") |
581
|
226 { |
|
227 write_to_diary_file = 1; |
|
228 open_diary_file (); |
|
229 } |
1755
|
230 else if (arg == "off") |
581
|
231 write_to_diary_file = 0; |
|
232 else |
|
233 { |
1755
|
234 diary_file = arg; |
581
|
235 open_diary_file (); |
|
236 } |
|
237 } |
|
238 break; |
777
|
239 |
581
|
240 default: |
|
241 print_usage ("diary"); |
|
242 break; |
|
243 } |
|
244 |
|
245 return retval; |
|
246 } |
|
247 |
1488
|
248 DEFUN_TEXT ("more", Fmore, Smore, 10, |
1409
|
249 "more on\n\ |
|
250 more off\n\ |
|
251 \n\ |
|
252 Turn output pagination on or off.") |
|
253 { |
|
254 Octave_object retval; |
|
255 |
1755
|
256 int argc = args.length () + 1; |
|
257 |
|
258 string_vector argv = make_argv (args, "more"); |
|
259 |
|
260 if (error_state) |
|
261 return retval; |
1409
|
262 |
|
263 if (argc == 2) |
|
264 { |
1755
|
265 string arg = argv[1]; |
1409
|
266 |
1755
|
267 if (arg == "on") |
1409
|
268 bind_builtin_variable ("page_screen_output", "true"); |
1755
|
269 else if (arg == "off") |
1409
|
270 bind_builtin_variable ("page_screen_output", "false"); |
|
271 else |
1755
|
272 error ("more: unrecognized argument `%s'", arg.c_str ()); |
1409
|
273 } |
|
274 else |
|
275 print_usage ("more"); |
|
276 |
|
277 return retval; |
|
278 } |
|
279 |
1
|
280 /* |
|
281 ;;; Local Variables: *** |
|
282 ;;; mode: C++ *** |
|
283 ;;; page-delimiter: "^/\\*" *** |
|
284 ;;; End: *** |
|
285 */ |