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