1
|
1 // pager.cc -*- C++ -*- |
|
2 /* |
|
3 |
529
|
4 Copyright (C) 1992, 1993, 1994 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
990
|
28 #include <signal.h> |
1
|
29 #include <iostream.h> |
|
30 #include <strstream.h> |
581
|
31 #include <fstream.h> |
1
|
32 #include <stdlib.h> |
|
33 |
|
34 #include "procstream.h" |
|
35 |
990
|
36 #include "sighandlers.h" |
1
|
37 #include "user-prefs.h" |
581
|
38 #include "oct-obj.h" |
|
39 #include "error.h" |
|
40 #include "defun.h" |
1
|
41 #include "input.h" |
|
42 #include "pager.h" |
581
|
43 #include "utils.h" |
|
44 #include "help.h" |
1
|
45 |
|
46 // Where we stash output headed for the screen. |
529
|
47 static ostrstream *pager_buf = 0; |
1
|
48 |
581
|
49 // Nonzero means we write to the diary file. |
|
50 static int write_to_diary_file = 0; |
|
51 |
|
52 // The name of the current diary file. |
|
53 static char *diary_file = "diary"; |
|
54 |
|
55 // The diary file. |
|
56 static ofstream diary_stream; |
|
57 |
1
|
58 static int |
|
59 line_count (char *s) |
|
60 { |
|
61 int count = 0; |
529
|
62 if (s) |
1
|
63 { |
|
64 char c; |
|
65 while ((c = *s++) != '\0') |
|
66 if (c == '\n') |
|
67 count++; |
|
68 } |
|
69 return count; |
|
70 } |
|
71 |
581
|
72 // For now, use the variables from readline. It already handles |
|
73 // SIGWINCH, so these values have a good chance of being correct even |
|
74 // if the window changes size (they will be wrong if, for example, the |
|
75 // luser changes the window size while the pager is running, and the |
|
76 // signal is handled by the pager instead of us. |
|
77 |
1
|
78 int |
|
79 terminal_columns (void) |
|
80 { |
|
81 extern int screenwidth; |
|
82 return screenwidth > 0 ? screenwidth : 80; |
|
83 } |
|
84 |
|
85 int |
|
86 terminal_rows (void) |
|
87 { |
|
88 extern int screenheight; |
|
89 return screenheight > 0 ? screenheight : 24; |
|
90 } |
|
91 |
|
92 void |
|
93 initialize_pager (void) |
|
94 { |
|
95 delete pager_buf; |
|
96 pager_buf = new ostrstream (); |
|
97 } |
|
98 |
|
99 void |
|
100 maybe_page_output (ostrstream& msg_buf) |
|
101 { |
|
102 msg_buf << ends; |
|
103 |
|
104 char *message = msg_buf.str (); |
|
105 |
|
106 if (interactive |
|
107 && user_pref.page_screen_output |
529
|
108 && user_pref.pager_binary) |
1
|
109 { |
|
110 *pager_buf << message; |
|
111 delete [] message; |
|
112 } |
|
113 else |
|
114 { |
|
115 cout << message; |
|
116 cout.flush (); |
|
117 delete [] message; |
|
118 } |
|
119 } |
|
120 |
|
121 void |
|
122 flush_output_to_pager (void) |
|
123 { |
988
|
124 // Extract message from buffer, then delete the buffer so that any |
|
125 // new messages get sent separately. |
1
|
126 |
988
|
127 *pager_buf << ends; |
1
|
128 char *message = pager_buf->str (); |
988
|
129 initialize_pager (); |
1
|
130 |
529
|
131 if (! message || ! *message) |
1
|
132 { |
|
133 delete [] message; |
|
134 return; |
|
135 } |
|
136 |
581
|
137 maybe_write_to_diary_file (message); |
|
138 |
1
|
139 int nlines = line_count (message); |
|
140 |
|
141 if (nlines > terminal_rows () - 2) |
|
142 { |
|
143 char *pgr = user_pref.pager_binary; |
529
|
144 if (pgr) |
1
|
145 { |
|
146 oprocstream pager_stream (pgr); |
|
147 if (pager_stream) |
|
148 { |
990
|
149 volatile sig_handler *old_sigint_handler; |
|
150 old_sigint_handler = signal (SIGINT, SIG_IGN); |
|
151 |
1
|
152 pager_stream << message; |
988
|
153 delete [] message; |
1
|
154 pager_stream.flush (); |
990
|
155 |
|
156 signal (SIGINT, old_sigint_handler); |
|
157 |
1
|
158 return; |
|
159 } |
|
160 } |
|
161 } |
|
162 |
|
163 cout << message; |
988
|
164 delete [] message; |
1
|
165 cout.flush (); |
|
166 } |
|
167 |
581
|
168 static void |
|
169 open_diary_file (void) |
|
170 { |
|
171 if (diary_stream.is_open ()) |
|
172 diary_stream.close (); |
|
173 |
|
174 diary_stream.open (diary_file, ios::app); |
|
175 |
|
176 if (! diary_stream) |
|
177 error ("diary: can't open diary file `%s'", diary_file); |
|
178 } |
|
179 |
|
180 void |
|
181 close_diary_file (void) |
|
182 { |
|
183 if (diary_stream) |
|
184 diary_stream.close (); |
|
185 } |
|
186 |
|
187 void |
|
188 maybe_write_to_diary_file (const char *s) |
|
189 { |
|
190 if (write_to_diary_file && diary_stream) |
|
191 diary_stream << s; |
|
192 } |
|
193 |
|
194 DEFUN_TEXT ("diary", Fdiary, Sdiary, -1, 1, |
|
195 "diary [on|off]\n\ |
|
196 diary [file]\n\ |
|
197 \n\ |
|
198 redirect all input and screen output to a file.") |
|
199 { |
|
200 Octave_object retval; |
|
201 |
|
202 DEFINE_ARGV("diary"); |
|
203 |
|
204 switch (argc) |
|
205 { |
|
206 case 1: |
|
207 write_to_diary_file = ! write_to_diary_file; |
|
208 open_diary_file (); |
|
209 break; |
623
|
210 |
581
|
211 case 2: |
|
212 { |
|
213 char *arg = argv[1]; |
|
214 if (strcmp (arg, "on") == 0) |
|
215 { |
|
216 write_to_diary_file = 1; |
|
217 open_diary_file (); |
|
218 } |
|
219 else if (strcmp (arg, "off") == 0) |
|
220 write_to_diary_file = 0; |
|
221 else |
|
222 { |
|
223 delete [] diary_file; |
|
224 diary_file = strsave (arg); |
|
225 open_diary_file (); |
|
226 } |
|
227 } |
|
228 break; |
777
|
229 |
581
|
230 default: |
|
231 print_usage ("diary"); |
|
232 break; |
|
233 } |
|
234 |
|
235 DELETE_ARGV; |
|
236 |
|
237 return retval; |
|
238 } |
|
239 |
1
|
240 /* |
|
241 ;;; Local Variables: *** |
|
242 ;;; mode: C++ *** |
|
243 ;;; page-delimiter: "^/\\*" *** |
|
244 ;;; End: *** |
|
245 */ |