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