Mercurial > hg > octave-nkf
annotate src/pager.cc @ 13266:c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
* pager.h, pager.cc (octave_pager_stream::reset,
octave_pager_stream::do_reset): New functions.
(octave_diary_stream::reset, octave_diary_stream::do_reset):
New functions.
* input.cc (octave_gets, get_user_input): Call
octave_pager_stream::reset and octave_diary_stream::reset prior to
printing prompt and getting input.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 03 Oct 2011 11:03:40 -0400 |
parents | 12df7854fa7c |
children | 79aa00a94e9e |
rev | line source |
---|---|
1 | 1 /* |
2 | |
11523 | 3 Copyright (C) 1993-2011 John W. Eaton |
1 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
1 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
1 | 20 |
21 */ | |
22 | |
240 | 23 #ifdef HAVE_CONFIG_H |
1192 | 24 #include <config.h> |
1 | 25 #endif |
26 | |
3503 | 27 #include <fstream> |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
28 #include <iostream> |
1755 | 29 #include <string> |
1 | 30 |
2926 | 31 #include "cmd-edit.h" |
32 #include "oct-env.h" | |
2101 | 33 |
1 | 34 #include "procstream.h" |
35 | |
2492 | 36 #include <defaults.h> |
1352 | 37 #include "defun.h" |
38 #include "error.h" | |
2164 | 39 #include "gripes.h" |
2110 | 40 #include "input.h" |
1755 | 41 #include "oct-obj.h" |
1352 | 42 #include "pager.h" |
990 | 43 #include "sighandlers.h" |
2100 | 44 #include "unwind-prot.h" |
2201 | 45 #include "utils.h" |
2368 | 46 #include "variables.h" |
2093 | 47 |
48 // Our actual connection to the external pager. | |
49 static oprocstream *external_pager = 0; | |
1 | 50 |
3018 | 51 // TRUE means we write to the diary file. |
52 static bool write_to_diary_file = false; | |
581 | 53 |
54 // The name of the current diary file. | |
3523 | 55 static std::string diary_file; |
581 | 56 |
57 // The diary file. | |
3523 | 58 static std::ofstream external_diary_file; |
581 | 59 |
5794 | 60 static std::string |
61 default_pager (void) | |
62 { | |
63 std::string pager_binary = octave_env::getenv ("PAGER"); | |
64 | |
65 #ifdef OCTAVE_DEFAULT_PAGER | |
66 if (pager_binary.empty ()) | |
6144 | 67 pager_binary = OCTAVE_DEFAULT_PAGER; |
5794 | 68 #endif |
69 | |
70 return pager_binary; | |
71 } | |
72 | |
2164 | 73 // The shell command to run as the pager. |
5794 | 74 static std::string VPAGER = default_pager (); |
2164 | 75 |
6144 | 76 // The options to pass to the pager. |
77 static std::string VPAGER_FLAGS; | |
78 | |
2164 | 79 // TRUE means that if output is going to the pager, it is sent as soon |
80 // as it is available. Otherwise, it is buffered and only sent to the | |
81 // pager when it is time to print another prompt. | |
5794 | 82 static bool Vpage_output_immediately = false; |
2164 | 83 |
84 // TRUE means all output intended for the screen should be passed | |
85 // through the pager. | |
5794 | 86 static bool Vpage_screen_output = true; |
2164 | 87 |
3018 | 88 static bool really_flush_to_pager = false; |
2100 | 89 |
3018 | 90 static bool flushing_output_to_pager = false; |
2206 | 91 |
2093 | 92 static void |
2197 | 93 clear_external_pager (void) |
94 { | |
5142 | 95 if (external_pager) |
96 { | |
97 octave_child_list::remove (external_pager->pid ()); | |
2197 | 98 |
5142 | 99 delete external_pager; |
100 external_pager = 0; | |
2197 | 101 } |
102 } | |
103 | |
5142 | 104 static bool |
105 pager_event_handler (pid_t pid, int status) | |
2209 | 106 { |
5142 | 107 bool retval = false; |
108 | |
2209 | 109 if (pid > 0) |
110 { | |
111 if (WIFEXITED (status) || WIFSIGNALLED (status)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
112 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
113 // Avoid warning() since that will put us back in the pager, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
114 // which would be bad news. |
2209 | 115 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
116 std::cerr << "warning: connection to external pager lost (pid = " |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
117 << pid << ")" << std::endl; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
118 std::cerr << "warning: flushing pending output (please wait)" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
119 << std::endl; |
5142 | 120 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
121 // Request removal of this PID from the list of child |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
122 // processes. |
5142 | 123 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
124 retval = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
125 } |
2209 | 126 } |
5142 | 127 |
128 return retval; | |
2209 | 129 } |
130 | |
6144 | 131 static std::string |
132 pager_command (void) | |
133 { | |
134 std::string cmd = VPAGER; | |
135 | |
136 if (! (cmd.empty () || VPAGER_FLAGS.empty ())) | |
137 cmd += " " + VPAGER_FLAGS; | |
138 | |
139 return cmd; | |
140 } | |
141 | |
2209 | 142 static void |
3233 | 143 do_sync (const char *msg, int len, bool bypass_pager) |
1 | 144 { |
3233 | 145 if (msg && len > 0) |
1 | 146 { |
2206 | 147 if (bypass_pager) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
148 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
149 std::cout.write (msg, len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
150 std::cout.flush (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
151 } |
2206 | 152 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
153 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
154 if (! external_pager) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
155 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
156 std::string pgr = pager_command (); |
2093 | 157 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
158 if (! pgr.empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
159 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
160 external_pager = new oprocstream (pgr.c_str ()); |
2093 | 161 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
162 if (external_pager) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
163 octave_child_list::insert (external_pager->pid (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
164 pager_event_handler); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
165 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
166 } |
2101 | 167 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
168 if (external_pager) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
169 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
170 if (external_pager->good ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
171 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
172 external_pager->write (msg, len); |
2101 | 173 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
174 external_pager->flush (); |
2197 | 175 |
5142 | 176 #if defined (EPIPE) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
177 if (errno == EPIPE) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
178 external_pager->setstate (std::ios::failbit); |
5142 | 179 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
180 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
181 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
182 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
183 // FIXME -- omething is not right with the |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
184 // pager. If it died then we should receive a |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
185 // signal for that. If there is some other problem, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
186 // then what? |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
187 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
188 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
189 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
190 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
191 std::cout.write (msg, len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
192 std::cout.flush (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
193 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
194 } |
1 | 195 } |
196 } | |
197 | |
3233 | 198 // Assume our terminal wraps long lines. |
199 | |
2101 | 200 static bool |
3233 | 201 more_than_a_screenful (const char *s, int len) |
2101 | 202 { |
203 if (s) | |
204 { | |
2926 | 205 int available_rows = command_editor::terminal_rows () - 2; |
2101 | 206 |
3233 | 207 int cols = command_editor::terminal_cols (); |
208 | |
2103 | 209 int count = 0; |
210 | |
3233 | 211 int chars_this_line = 0; |
2101 | 212 |
3233 | 213 for (int i = 0; i < len; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
214 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
215 if (*s++ == '\n') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
216 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
217 count += chars_this_line / cols + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
218 chars_this_line = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
219 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
220 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
221 chars_this_line++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
222 } |
2101 | 223 |
3233 | 224 if (count > available_rows) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
225 return true; |
2101 | 226 } |
227 | |
228 return false; | |
229 } | |
230 | |
2093 | 231 int |
232 octave_pager_buf::sync (void) | |
233 { | |
2186 | 234 if (! interactive |
235 || really_flush_to_pager | |
2164 | 236 || (Vpage_screen_output && Vpage_output_immediately) |
237 || ! Vpage_screen_output) | |
2100 | 238 { |
3233 | 239 char *buf = eback (); |
2093 | 240 |
3233 | 241 int len = pptr () - buf; |
2100 | 242 |
2110 | 243 bool bypass_pager = (! interactive |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
244 || ! Vpage_screen_output |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
245 || (really_flush_to_pager |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
246 && Vpage_screen_output |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
247 && ! Vpage_output_immediately |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
248 && ! more_than_a_screenful (buf, len))); |
2475 | 249 |
3233 | 250 if (len > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
251 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
252 do_sync (buf, len, bypass_pager); |
2093 | 253 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
254 flush_current_contents_to_diary (); |
3233 | 255 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
256 seekoff (0, std::ios::beg); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
257 } |
2100 | 258 } |
2093 | 259 |
260 return 0; | |
261 } | |
262 | |
3477 | 263 void |
264 octave_pager_buf::flush_current_contents_to_diary (void) | |
265 { | |
3756 | 266 char *buf = eback () + diary_skip; |
3477 | 267 |
3756 | 268 size_t len = pptr () - buf; |
3477 | 269 |
270 octave_diary.write (buf, len); | |
3756 | 271 |
3870 | 272 diary_skip = 0; |
3756 | 273 } |
274 | |
275 void | |
276 octave_pager_buf::set_diary_skip (void) | |
277 { | |
278 diary_skip = pptr () - eback (); | |
3477 | 279 } |
280 | |
2093 | 281 int |
282 octave_diary_buf::sync (void) | |
283 { | |
3233 | 284 if (write_to_diary_file && external_diary_file) |
285 { | |
3870 | 286 char *buf = eback (); |
287 | |
288 int len = pptr () - buf; | |
2093 | 289 |
3233 | 290 if (len > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
291 external_diary_file.write (buf, len); |
3233 | 292 } |
2093 | 293 |
3544 | 294 seekoff (0, std::ios::beg); |
2093 | 295 |
296 return 0; | |
297 } | |
298 | |
299 octave_pager_stream *octave_pager_stream::instance = 0; | |
300 | |
3775 | 301 octave_pager_stream::octave_pager_stream (void) : std::ostream (0), pb (0) |
1 | 302 { |
4051 | 303 pb = new octave_pager_buf (); |
2093 | 304 rdbuf (pb); |
305 setf (unitbuf); | |
306 } | |
307 | |
308 octave_pager_stream::~octave_pager_stream (void) | |
309 { | |
310 flush (); | |
311 delete pb; | |
312 } | |
313 | |
314 octave_pager_stream& | |
315 octave_pager_stream::stream (void) | |
316 { | |
317 if (! instance) | |
318 instance = new octave_pager_stream (); | |
2926 | 319 |
2093 | 320 return *instance; |
321 } | |
322 | |
3477 | 323 void |
324 octave_pager_stream::flush_current_contents_to_diary (void) | |
325 { | |
326 if (pb) | |
327 pb->flush_current_contents_to_diary (); | |
328 } | |
329 | |
3756 | 330 void |
331 octave_pager_stream::set_diary_skip (void) | |
332 { | |
333 if (pb) | |
334 pb->set_diary_skip (); | |
335 } | |
336 | |
13266
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
337 // Reinitialize the pager buffer to avoid hanging on to large internal |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
338 // buffers when they might not be needed. This function should only be |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
339 // called when the pager is not in use. For example, just before |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
340 // getting command-line input. |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
341 |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
342 void |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
343 octave_pager_stream::reset (void) |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
344 { |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
345 if (! instance) |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
346 instance = new octave_pager_stream (); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
347 |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
348 instance->do_reset (); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
349 } |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
350 |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
351 void |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
352 octave_pager_stream::do_reset (void) |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
353 { |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
354 delete pb; |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
355 pb = new octave_pager_buf (); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
356 rdbuf (pb); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
357 setf (unitbuf); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
358 } |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
359 |
2093 | 360 octave_diary_stream *octave_diary_stream::instance = 0; |
361 | |
3775 | 362 octave_diary_stream::octave_diary_stream (void) : std::ostream (0), db (0) |
2093 | 363 { |
4051 | 364 db = new octave_diary_buf (); |
2093 | 365 rdbuf (db); |
366 setf (unitbuf); | |
367 } | |
368 | |
369 octave_diary_stream::~octave_diary_stream (void) | |
370 { | |
371 flush (); | |
372 delete db; | |
373 } | |
374 | |
375 octave_diary_stream& | |
376 octave_diary_stream::stream (void) | |
377 { | |
378 if (! instance) | |
379 instance = new octave_diary_stream (); | |
380 | |
381 return *instance; | |
1 | 382 } |
383 | |
13266
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
384 // Reinitialize the diary buffer to avoid hanging on to large internal |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
385 // buffers when they might not be needed. This function should only be |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
386 // called when the pager is not in use. For example, just before |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
387 // getting command-line input. |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
388 |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
389 void |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
390 octave_diary_stream::reset (void) |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
391 { |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
392 if (! instance) |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
393 instance = new octave_diary_stream (); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
394 |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
395 instance->do_reset (); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
396 } |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
397 |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
398 void |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
399 octave_diary_stream::do_reset (void) |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
400 { |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
401 delete db; |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
402 db = new octave_diary_buf (); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
403 rdbuf (db); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
404 setf (unitbuf); |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
405 } |
c053740eb2aa
improve memory use for the pager and diary streams (bug #34431)
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
406 |
1 | 407 void |
2093 | 408 flush_octave_stdout (void) |
1 | 409 { |
2206 | 410 if (! flushing_output_to_pager) |
411 { | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
412 unwind_protect frame; |
2100 | 413 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
414 frame.protect_var (really_flush_to_pager); |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
415 frame.protect_var (flushing_output_to_pager); |
2100 | 416 |
3018 | 417 really_flush_to_pager = true; |
418 flushing_output_to_pager = true; | |
2206 | 419 |
420 octave_stdout.flush (); | |
1 | 421 |
5142 | 422 clear_external_pager (); |
2206 | 423 } |
1 | 424 } |
425 | |
1965 | 426 static void |
2093 | 427 close_diary_file (void) |
1 | 428 { |
3477 | 429 // Try to flush the current buffer to the diary now, so that things |
430 // like | |
431 // | |
432 // function foo () | |
433 // diary on; | |
434 // ... | |
435 // diary off; | |
436 // endfunction | |
437 // | |
438 // will do the right thing. | |
439 | |
440 octave_stdout.flush_current_contents_to_diary (); | |
441 | |
2093 | 442 if (external_diary_file.is_open ()) |
1 | 443 { |
2093 | 444 octave_diary.flush (); |
445 external_diary_file.close (); | |
1 | 446 } |
447 } | |
448 | |
581 | 449 static void |
450 open_diary_file (void) | |
451 { | |
2093 | 452 close_diary_file (); |
581 | 453 |
3756 | 454 // If there is pending output in the pager buf, it should not go |
455 // into the diary file. | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
456 |
3756 | 457 octave_stdout.set_diary_skip (); |
458 | |
3544 | 459 external_diary_file.open (diary_file.c_str (), std::ios::app); |
581 | 460 |
2093 | 461 if (! external_diary_file) |
462 error ("diary: can't open diary file `%s'", diary_file.c_str ()); | |
581 | 463 } |
464 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
7432
diff
changeset
|
465 DEFUN (diary, args, , |
3332 | 466 "-*- texinfo -*-\n\ |
11547 | 467 @deftypefn {Command} {} diary options\n\ |
9134
a3739e27b017
Update section 2.4 of basics.txi
Rik <rdrider0-list@yahoo.com>
parents:
8950
diff
changeset
|
468 Record a list of all commands @emph{and} the output they produce, mixed\n\ |
3332 | 469 together just as you see them on your terminal. Valid options are:\n\ |
470 \n\ | |
471 @table @code\n\ | |
472 @item on\n\ | |
473 Start recording your session in a file called @file{diary} in your\n\ | |
474 current working directory.\n\ | |
581 | 475 \n\ |
3332 | 476 @item off\n\ |
477 Stop recording your session in the diary file.\n\ | |
478 \n\ | |
479 @item @var{file}\n\ | |
480 Record your session in the file named @var{file}.\n\ | |
481 @end table\n\ | |
482 \n\ | |
9134
a3739e27b017
Update section 2.4 of basics.txi
Rik <rdrider0-list@yahoo.com>
parents:
8950
diff
changeset
|
483 With no arguments, @code{diary} toggles the current diary state.\n\ |
11547 | 484 @end deftypefn") |
581 | 485 { |
2086 | 486 octave_value_list retval; |
581 | 487 |
1755 | 488 int argc = args.length () + 1; |
489 | |
1965 | 490 string_vector argv = args.make_argv ("diary"); |
581 | 491 |
1755 | 492 if (error_state) |
493 return retval; | |
494 | |
495 if (diary_file.empty ()) | |
496 diary_file = "diary"; | |
1306 | 497 |
581 | 498 switch (argc) |
499 { | |
500 case 1: | |
501 write_to_diary_file = ! write_to_diary_file; | |
502 open_diary_file (); | |
503 break; | |
623 | 504 |
581 | 505 case 2: |
506 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
507 std::string arg = argv[1]; |
1755 | 508 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
509 if (arg == "on") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
510 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
511 write_to_diary_file = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
512 open_diary_file (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
513 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
514 else if (arg == "off") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
515 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
516 close_diary_file (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
517 write_to_diary_file = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
518 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
519 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
520 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
521 diary_file = arg; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
522 write_to_diary_file = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
523 open_diary_file (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
524 } |
581 | 525 } |
526 break; | |
777 | 527 |
581 | 528 default: |
5823 | 529 print_usage (); |
581 | 530 break; |
531 } | |
532 | |
533 return retval; | |
534 } | |
535 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
7432
diff
changeset
|
536 DEFUN (more, args, , |
3372 | 537 "-*- texinfo -*-\n\ |
11572
7d6d8c1e471f
Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents:
11547
diff
changeset
|
538 @deftypefn {Command} {} more\n\ |
11547 | 539 @deftypefnx {Command} {} more on\n\ |
540 @deftypefnx {Command} {} more off\n\ | |
3372 | 541 Turn output pagination on or off. Without an argument, @code{more}\n\ |
542 toggles the current state.\n\ | |
7432 | 543 The current state can be determined via @code{page_screen_output}.\n\ |
11547 | 544 @end deftypefn") |
1409 | 545 { |
2086 | 546 octave_value_list retval; |
1409 | 547 |
1755 | 548 int argc = args.length () + 1; |
549 | |
1965 | 550 string_vector argv = args.make_argv ("more"); |
1755 | 551 |
552 if (error_state) | |
553 return retval; | |
1409 | 554 |
555 if (argc == 2) | |
556 { | |
3523 | 557 std::string arg = argv[1]; |
1409 | 558 |
1755 | 559 if (arg == "on") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
560 Vpage_screen_output = true; |
1755 | 561 else if (arg == "off") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
562 Vpage_screen_output = false; |
1409 | 563 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
564 error ("more: unrecognized argument `%s'", arg.c_str ()); |
1409 | 565 } |
4324 | 566 else if (argc == 1) |
5794 | 567 Vpage_screen_output = ! Vpage_screen_output; |
1409 | 568 else |
5823 | 569 print_usage (); |
1409 | 570 |
571 return retval; | |
572 } | |
573 | |
5673 | 574 DEFUN (terminal_size, , , |
575 "-*- texinfo -*-\n\ | |
576 @deftypefn {Built-in Function} {} terminal_size ()\n\ | |
577 Return a two-element row vector containing the current size of the\n\ | |
578 terminal window in characters (rows and columns).\n\ | |
5778 | 579 @seealso{list_in_columns}\n\ |
5673 | 580 @end deftypefn") |
581 { | |
582 RowVector size (2, 0.0); | |
583 | |
584 size(0) = command_editor::terminal_rows (); | |
585 size(1) = command_editor::terminal_cols (); | |
586 | |
587 return octave_value (size); | |
588 } | |
589 | |
5794 | 590 DEFUN (page_output_immediately, args, nargout, |
591 "-*- texinfo -*-\n\ | |
10840 | 592 @deftypefn {Built-in Function} {@var{val} =} page_output_immediately ()\n\ |
5794 | 593 @deftypefnx {Built-in Function} {@var{val} =} page_output_immediately (@var{new_val})\n\ |
594 Query or set the internal variable that controls whether Octave sends\n\ | |
595 output to the pager as soon as it is available. Otherwise, Octave\n\ | |
596 buffers its output and waits until just before the prompt is printed to\n\ | |
597 flush it to the pager.\n\ | |
598 @end deftypefn") | |
2097 | 599 { |
5794 | 600 return SET_INTERNAL_VARIABLE (page_output_immediately); |
2164 | 601 } |
602 | |
5794 | 603 DEFUN (page_screen_output, args, nargout, |
604 "-*- texinfo -*-\n\ | |
10840 | 605 @deftypefn {Built-in Function} {@var{val} =} page_screen_output ()\n\ |
5794 | 606 @deftypefnx {Built-in Function} {@var{old_val} =} page_screen_output (@var{new_val})\n\ |
607 Query or set the internal variable that controls whether output intended\n\ | |
608 for the terminal window that is longer than one page is sent through a\n\ | |
609 pager. This allows you to view one screenful at a time. Some pagers\n\ | |
610 (such as @code{less}---see @ref{Installation}) are also capable of moving\n\ | |
611 backward on the output.\n\ | |
612 @end deftypefn") | |
2164 | 613 { |
5794 | 614 return SET_INTERNAL_VARIABLE (page_screen_output); |
2164 | 615 } |
616 | |
5794 | 617 DEFUN (PAGER, args, nargout, |
618 "-*- texinfo -*-\n\ | |
10840 | 619 @deftypefn {Built-in Function} {@var{val} =} PAGER ()\n\ |
5794 | 620 @deftypefnx {Built-in Function} {@var{old_val} =} PAGER (@var{new_val})\n\ |
621 Query or set the internal variable that specifies the program to use\n\ | |
622 to display terminal output on your system. The default value is\n\ | |
623 normally @code{\"less\"}, @code{\"more\"}, or\n\ | |
3372 | 624 @code{\"pg\"}, depending on what programs are installed on your system.\n\ |
625 @xref{Installation}.\n\ | |
6144 | 626 @seealso{more, page_screen_output, page_output_immediately, PAGER_FLAGS}\n\ |
5794 | 627 @end deftypefn") |
628 { | |
629 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (PAGER); | |
2097 | 630 } |
631 | |
6144 | 632 DEFUN (PAGER_FLAGS, args, nargout, |
633 "-*- texinfo -*-\n\ | |
10840 | 634 @deftypefn {Built-in Function} {@var{val} =} PAGER_FLAGS ()\n\ |
6144 | 635 @deftypefnx {Built-in Function} {@var{old_val} =} PAGER_FLAGS (@var{new_val})\n\ |
636 Query or set the internal variable that specifies the options to pass\n\ | |
637 to the pager.\n\ | |
638 @seealso{PAGER}\n\ | |
639 @end deftypefn") | |
640 { | |
641 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (PAGER_FLAGS); | |
642 } |