GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pager.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_pager_h)
27 #define octave_pager_h 1
28 
29 #include "octave-config.h"
30 
31 #include <fstream>
32 #include <iosfwd>
33 #include <sstream>
34 #include <string>
35 
36 class octave_value;
37 class octave_value_list;
38 
40 
41 class interpreter;
42 class oprocstream;
43 
44 class
45 OCTINTERP_API
46 pager_buf : public std::stringbuf
47 {
48 public:
49 
50  pager_buf () : std::stringbuf (), m_diary_skip (0) { }
51 
52  OCTAVE_DISABLE_COPY_MOVE (pager_buf)
53 
54  ~pager_buf () = default;
55 
56  void flush_current_contents_to_diary ();
57 
58  void set_diary_skip ();
59 
60 protected:
61 
62  int sync ();
63 
64 private:
65 
66  std::size_t m_diary_skip;
67 };
68 
69 class
70 OCTINTERP_API
71 pager_stream : public std::ostream
72 {
73 public:
74 
75  pager_stream ();
76 
77  OCTAVE_DISABLE_COPY_MOVE (pager_stream)
78 
79  ~pager_stream ();
80 
81  void flush_current_contents_to_diary ();
82 
83  void set_diary_skip ();
84 
85  std::ostream& stream ();
86 
87  void reset ();
88 
89 private:
90 
91  pager_buf *m_pb;
92 };
93 
94 class
95 OCTINTERP_API
96 diary_buf : public std::stringbuf
97 {
98 public:
99 
100  OCTAVE_DEFAULT_CONSTRUCT_DELETE (diary_buf)
101  OCTAVE_DISABLE_COPY_MOVE (diary_buf)
102 
103 protected:
104 
105  int sync ();
106 };
107 
108 class
109 OCTINTERP_API
110 diary_stream : public std::ostream
111 {
112 public:
113 
114  diary_stream ();
115 
116  OCTAVE_DISABLE_COPY_MOVE (diary_stream)
117 
118  ~diary_stream ();
119 
120  std::ostream& stream ();
121 
122  void reset ();
123 
124 private:
125 
126  diary_buf *m_db;
127 };
128 
129 extern OCTINTERP_API void flush_stdout ();
130 
132 {
133 public:
134 
135  output_system (interpreter& interp);
136 
137  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (output_system)
138 
139  ~output_system () = default;
140 
141  pager_stream& pager () { return m_pager_stream; }
142 
143  diary_stream& diary () { return m_diary_stream; }
144 
145  std::string diary_file_name () const { return m_diary_file_name; }
146 
147  std::string diary_file_name (const std::string& nm)
148  {
149  std::string val = m_diary_file_name;
150  m_diary_file_name = nm.empty () ? "diary" : nm;
151  return val;
152  }
153 
154  octave_value PAGER (const octave_value_list& args, int nargout);
155 
156  std::string PAGER () const { return m_PAGER; }
157 
158  std::string PAGER (const std::string& s)
159  {
160  std::string val = m_PAGER;
161  m_PAGER = s;
162  return val;
163  }
164 
165  octave_value PAGER_FLAGS (const octave_value_list& args, int nargout);
166 
167  std::string PAGER_FLAGS () const { return m_PAGER_FLAGS; }
168 
169  std::string PAGER_FLAGS (const std::string& s)
170  {
171  std::string val = m_PAGER_FLAGS;
172  m_PAGER_FLAGS = s;
173  return val;
174  }
175 
177  int nargout);
178 
180  {
181  return m_page_output_immediately;
182  }
183 
184  bool page_output_immediately (bool flag)
185  {
186  bool val = m_page_output_immediately;
187  m_page_output_immediately = flag;
188  return val;
189  }
190 
192  int nargout);
193 
194  bool page_screen_output () const { return m_page_screen_output; }
195 
196  bool page_screen_output (bool flag)
197  {
198  bool val = m_page_screen_output;
199  m_page_screen_output = flag;
200  return val;
201  }
202 
203  bool write_to_diary_file () const
204  {
205  return m_write_to_diary_file;
206  }
207 
208  bool write_to_diary_file (bool flag)
209  {
210  bool val = m_write_to_diary_file;
211  m_write_to_diary_file = flag;
212  return val;
213  }
214 
215  bool really_flush_to_pager () const
216  {
217  return m_really_flush_to_pager;
218  }
219 
220  bool really_flush_to_pager (bool flag)
221  {
222  bool val = m_really_flush_to_pager;
223  m_really_flush_to_pager = flag;
224  return val;
225  }
226 
228  {
229  return m_flushing_output_to_pager;
230  }
231 
232  bool flushing_output_to_pager (bool flag)
233  {
234  bool val = m_flushing_output_to_pager;
235  m_flushing_output_to_pager = flag;
236  return val;
237  }
238 
239  std::string pager_command () const;
240 
241  std::ofstream& external_diary_file () { return m_external_diary_file; }
242 
243  void reset ();
244 
245  void flush_stdout ();
246 
247  bool sync (const char *msg, int len);
248 
249  void clear_external_pager ();
250 
251  void open_diary ();
252 
253  void close_diary ();
254 
255  std::ostream& __stdout__ () { return m_pager_stream.stream (); }
256 
257  std::ostream& __diary__ () { return m_diary_stream.stream (); }
258 
259 private:
260 
261  interpreter& m_interpreter;
262 
263  pager_stream m_pager_stream;
264 
265  diary_stream m_diary_stream;
266 
267  // Our actual connection to the external pager.
268  oprocstream *m_external_pager = nullptr;
269 
270  // The diary file.
271  std::ofstream m_external_diary_file;
272 
273  // The name of the current diary file.
274  std::string m_diary_file_name;
275 
276  // The shell command to run as the pager.
277  std::string m_PAGER;
278 
279  // The options to pass to the pager.
280  std::string m_PAGER_FLAGS;
281 
282  // TRUE means that if output is going to the pager, it is sent as soon
283  // as it is available. Otherwise, it is buffered and only sent to the
284  // pager when it is time to print another prompt.
285  bool m_page_output_immediately;
286 
287  // TRUE means all output intended for the screen should be passed
288  // through the pager.
289  bool m_page_screen_output;
290 
291  // TRUE means we write to the diary file.
292  bool m_write_to_diary_file;
293 
294  bool m_really_flush_to_pager;
295 
296  bool m_flushing_output_to_pager;
297 
298  void start_external_pager ();
299 
300  void do_sync (const char *msg, int len, bool bypass_pager);
301 };
302 
303 extern OCTINTERP_API std::ostream& __stdout__ ();
304 
305 extern OCTINTERP_API std::ostream& __diary__ ();
306 
307 OCTAVE_END_NAMESPACE(octave)
308 
309 #define octave_stdout (octave::__stdout__ ())
310 
311 #define octave_diary (octave::__diary__ ())
312 
313 #endif
std::ostream & stream()
Definition: pager.cc:248
~output_system()=default
void close_diary()
Definition: pager.cc:355
bool page_output_immediately(bool flag)
Definition: pager.h:184
std::string PAGER_FLAGS() const
Definition: pager.h:167
void open_diary()
Definition: pager.cc:378
bool page_screen_output() const
Definition: pager.h:194
bool page_output_immediately() const
Definition: pager.h:179
void flush_stdout()
Definition: pager.cc:336
bool flushing_output_to_pager(bool flag)
Definition: pager.h:232
pager_stream & pager()
Definition: pager.h:141
std::string diary_file_name() const
Definition: pager.h:145
std::ofstream & external_diary_file()
Definition: pager.h:241
bool write_to_diary_file(bool flag)
Definition: pager.h:208
std::string PAGER(const std::string &s)
Definition: pager.h:158
bool really_flush_to_pager(bool flag)
Definition: pager.h:220
bool sync(const char *msg, int len)
Definition: pager.cc:394
void reset()
Definition: pager.cc:327
bool flushing_output_to_pager() const
Definition: pager.h:227
void clear_external_pager()
Definition: pager.cc:426
std::ostream & __stdout__()
Definition: pager.h:255
bool really_flush_to_pager() const
Definition: pager.h:215
output_system(interpreter &interp)
Definition: pager.cc:275
std::string diary_file_name(const std::string &nm)
Definition: pager.h:147
bool write_to_diary_file() const
Definition: pager.h:203
std::string pager_command() const
Definition: pager.cc:316
std::string PAGER_FLAGS(const std::string &s)
Definition: pager.h:169
std::ostream & __diary__()
Definition: pager.h:257
diary_stream & diary()
Definition: pager.h:143
bool page_screen_output(bool flag)
Definition: pager.h:196
std::string PAGER() const
Definition: pager.h:156
~pager_buf()=default
pager_buf()
Definition: pager.h:50
std::ostream & stream()
Definition: pager.cc:201
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
Definition: lo-sysdep.cc:635
std::ostream & __diary__()
std::ostream & __stdout__()
void flush_stdout()
F77_RET_T len
Definition: xerbla.cc:61