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