Debian Codegraph @

terps/tads/tads3/vmconsol.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmconsol.h - TADS 3 console input reader and output formatter
12 Function
13 Provides console input and output for the TADS 3 built-in function set
14 for the T3 VM.
15
16 T3 uses the UTF-8 character set to represent character strings. The OS
17 functions use the local character set. We perform the mapping between
18 UTF-8 and the local character set within this module, so that OS routines
19 see local characters only, not UTF-8.
20
21 This code is based on the TADS 2 output formatter, but has been
22 substantially reworked for C++, Unicode, and the slightly different
23 TADS 3 formatting model.
24 Notes
25
26 Modified
27 09/04/99 MJRoberts - Creation
28 */
29
30 #ifndef VMCONSOL_H
31 #define VMCONSOL_H
32
33 #include <string.h>
34 #include <stdlib.h>
35 #include "wchar.h"
36
37 #include "os.h"
38 #include "t3std.h"
39 #include "vmtype.h"
40 #include "vmglob.h"
41
42
43 /* ------------------------------------------------------------------------ */
44 /*
45 * Synthetic console events. These are events we generate alongside
46 * OS_EVT_xxx codes; we start these at 10000 to ensure that we don't
47 * overlap any current or future OS_EVT_xxx event codes.
48 */
49 #define VMCON_EVT_END_QUIET_SCRIPT 10000 /* end quiet script playback */
50 #define VMCON_EVT_DIALOG 10001 /* result from dialog */
51 #define VMCON_EVT_FILE 10002 /* result from file dialog */
52
53
54 /*
55 * Event attributes. Some scriptable events have extra attributes
56 * associated with them in addition to the main payload. These attributes
57 * take the syntactic form of HTML tag attributes, so in principal we could
58 * have arbitrarily many attributes, each of which have arbitrary string
59 * values. However, at the moment, we only have a small number of
60 * attributes, and each attribute's value is merely a boolean, so it's
61 * adequate to represent these in the call interface with bit flags.
62 */
63
64 /* OVERWRITE, as in <file overwrite> */
65 #define VMCON_EVTATTR_OVERWRITE 0x00000001
66
67
68
69 /* ------------------------------------------------------------------------ */
70 /*
71 * Newline type codes. These specify how we are to perform line
72 * breaking after writing out text.
73 */
74 enum vm_nl_type
75 {
76 /*
77 * no line separation at all - write out this text and subsequent
78 * text as part of the same line with no separators
79 */
80 VM_NL_NONE,
81
82 /*
83 * flushing in preparation for input - don't show any line separation,
84 * and make sure that we display everything in the buffer, including
85 * trailing spaces
86 */
87 VM_NL_INPUT,
88
89 /* break the line at the end of this text and start a newline */
90 VM_NL_NEWLINE,
91
92 /* OS line separation - add a space after the text */
93 VM_NL_OSNEWLINE,
94
95 /*
96 * flushing internal buffers only: no line separation, and do not
97 * flush to underlying OS level yet
98 */
99 VM_NL_NONE_INTERNAL
100 };
101
102
103 /* ------------------------------------------------------------------------ */
104 /*
105 * Handle manager. This is a simple class for mapping system objects to
106 * integers, which we give as "handles" to byte code callers. We give only
107 * the integer handles to byte code to ensure that handles given back to us
108 * by the byte code are valid; if we handed back raw pointers to the byte
109 * code, it could call us with random garbage, and we'd have no way to
110 * protect against it.
111 */
112 class CVmHandleManager
113 {
114 public:
115 CVmHandleManager();
116
117 /*
118 * delete the object (use this rather than calling the destructor
119 * directly, since we need to call some virtuals in the course of
120 * preparing for deletion
121 */
122 void delete_obj(VMG0_);
123
124 protected:
125 /* delete */
126 virtual ~CVmHandleManager();
127
128 /* allocate a slot for a new item */
129 int alloc_handle(void *item);
130
131 /* clear the given handle's slot */
132 void clear_handle(int handle)
133 {
134 /* if the handle is valid, clear its associated slot */
135 if (is_valid_handle(handle))
136 handles_[handle - 1] = 0;
137 }
138
139 /* is the given handle valid? */
140 int is_valid_handle(int handle)
141 {
142 /*
143 * it's valid if it's within range for the allocated slots, and
144 * there's a non-null object in the handle's slot
145 */
146 return (handle >= 1
147 && (size_t)handle <= handles_max_
148 && handles_[handle - 1] != 0);
149 }
150
151 /* get the object for the given handle */
152 void *get_object(int handle)
153 {
154 /*
155 * If the handle is valid, get the item from the slot; the handle
156 * is indexed from 1, so decrement it to get the array index of the
157 * object for the handle. If the handle is invalid, return null.
158 */
159 return is_valid_handle(handle) ? handles_[handle - 1] : 0;
160 }
161
162 /*
163 * Delete the item in a slot, in preparation for destroying the handle
164 * manager itself - each subclass must override this to do the
165 * appropriate work on termination.
166 */
167 virtual void delete_handle_object(VMG_ int handle, void *obj) = 0;
168
169 /* array of window banners */
170 void **handles_;
171 size_t handles_max_;
172 };
173
174
175 /* ------------------------------------------------------------------------ */
176 /*
177 * Banner manager. This keeps track of banner windows outside of the main
178 * console window.
179 */
180 class CVmBannerManager: public CVmHandleManager
181 {
182 public:
183 CVmBannerManager() { }
184
185 /*
186 * Create a banner window. This creates an OS-level banner window, and
187 * creates a console object to format its output. Returns a banner
188 * handle that can be used to refer to the window. Banner handle zero
189 * is invalid and indicates failure.
190 *
191 * 'parent_id' is the banner ID of the parent of the new banner. The
192 * new banner is created as a child of the given parent. If parent_id
193 * is zero, then the new banner is created as a child of the main
194 * window. The parent determines how the new window is laid out: the
195 * new window's display area is carved out of the display area of the
196 * parent.
197 *
198 * 'where' is OS_BANNER_FIRST, OS_BANNER_LAST, OS_BANNER_BEFORE, or
199 * OS_BANNER_AFTER. 'other_id' is the banner ID of an existing child
200 * of the given parent, for the relative insertion point; this is
201 * ignored for OS_BANNER_FIRST and OS_BANNER_LAST.
202 *
203 * 'wintype' is an OS_BANNER_TYPE_xxx code giving the type of window to
204 * be created.
205 *
206 * 'siz' is the size, in units specified by 'siz_units', an
207 * OS_BANNER_SIZE_xxx value.
208 *
209 * 'style' is a combination of OS_BANNER_STYLE_xxx flags.
210 */
211 int create_banner(VMG_ int parent_id,
212 int where, int other_id, int wintype,
213 int align, int siz, int siz_units,
214 unsigned long style);
215
216 /* delete a banner */
217 void delete_banner(VMG_ int banner)
218 { delete_or_orphan_banner(vmg_ banner, FALSE); }
219
220 /*
221 * Get the OS-level handle for the banner - this handle can be used to
222 * call the os_banner_xxx functions directly.
223 */
224 void *get_os_handle(int banner);
225
226 /* get the banner's console object */
227 class CVmConsoleBanner *get_console(int banner)
228 {
229 /* the object behind the handle is the console */
230 return (CVmConsoleBanner *)get_object(banner);
231 }
232
233 /* flush all banners */
234 void flush_all(VMG_ vm_nl_type nl);
235
236 protected:
237 /* delete the object in a slot, in preparation for deleting the manager */
238 virtual void delete_handle_object(VMG_ int handle, void *)
239 {
240 /*
241 * delete the banner object, but orphan the system-level banner -
242 * this will allow the system-level banner to remain visible even
243 * after VM termination, in case the host application continues
244 * running even after the VM exits
245 */
246 delete_or_orphan_banner(vmg_ handle, TRUE);
247 }
248
249 /* delete or orphan a banner window */
250 void delete_or_orphan_banner(VMG_ int banner, int orphan);
251 };
252
253 /* ------------------------------------------------------------------------ */
254 /*
255 * Log console manager. This keeps track of log consoles, which are
256 * consoles created specifically for capturing text to a log file.
257 */
258 class CVmLogConsoleManager: public CVmHandleManager
259 {
260 public:
261 CVmLogConsoleManager() { }
262
263 /* create a log console - returns the console handle */
264 int create_log_console(VMG_ class CVmNetFile *nf, osfildef *fp,
265 class CCharmapToLocal *cmap, int width);
266
267 /* delete a log console */
268 void delete_log_console(VMG_ int handle);
269
270 /* get the log's console object */
271 class CVmConsoleLog *get_console(int banner)
272 {
273 /* the object behind the handle is the console */
274 return (CVmConsoleLog *)get_object(banner);
275 }
276
277 protected:
278 /* delete the object associated with a handle */
279 virtual void delete_handle_object(VMG_ int handle, void *)
280 {
281 /* delete the console */
282 delete_log_console(vmg_ handle);
283 }
284 };
285
286 /*
287 * Log console manager item. We use these to track individual log
288 * consoles.
289 */
290 class CVmLogConsoleItem
291 {
292 public:
293 CVmLogConsoleItem(const char *fname, class CCharmapToLocal *cmap);
294 ~CVmLogConsoleItem();
295
296 /* get the console */
297 class CVmConsoleLog *get_console() const { return console_; }
298
299 protected:
300 /* my console object */
301 class CVmConsoleLog *console_;
302 };
303
304
305 /* ------------------------------------------------------------------------ */
306 /*
307 * Script stack entry
308 */
309 struct script_stack_entry
310 {
311 script_stack_entry(VMG_ script_stack_entry *encp, int old_more,
312 class CVmNetFile *netfile, osfildef *outfp,
313 const vm_val_t *filespec,
314 int new_more, int is_quiet, int is_event_script);
315
316 void delobj(VMG0_);
317
318
319 /* the enclosing stack level */
320 script_stack_entry *enc;
321
322 /* network file descriptor for our script file */
323 class CVmNetFile *netfile;
324
325 /* the script file at this level */
326 osfildef *fp;
327
328 /* the MORE mode that was in effect before this script file */
329 int old_more_mode;
330
331 /* the MORE mode in effect during this script */
332 int more_mode;
333
334 /* are we reading quietly from this script? */
335 int quiet;
336
337 /* is this an event script? */
338 int event_script;
339
340 /* VM global variable, for gc protection for our file spec */
341 struct vm_globalvar_t *filespec;
342
343 private:
344 ~script_stack_entry() { }
345 };
346
347
348 /* ------------------------------------------------------------------------ */
349 /*
350 * Console. A console corresponds to device that shows information to
351 * and reads text from the user. On a text system, the console is
352 * simply the terminal. On a graphical system, the console is usually
353 * an application window.
354 */
355 class CVmConsole
356 {
357 public:
358 CVmConsole();
359 virtual void delete_obj(VMG0_);
360
361 /* write out a null-terminated UTF-8 string */
362 int format_text(VMG_ const char *p)
363 {
364 /* get its length and write it out */
365 return format_text(vmg_ p, strlen(p));
366 }
367
368 /* write out a UTF-8 string of a given byte length */
369 virtual int format_text(VMG_ const char *p, size_t len);
370
371 /* format text explicitly to the log file, if any */
372 int format_text_to_log(VMG_ const char *p, size_t len);
373
374 /* display a blank line */
375 void write_blank_line(VMG0_);
376
377 /* set the whitespace mode (returns the old whitespace mode) */
378 int set_obey_whitespace(int f);
379
380 /* set the text color */
381 void set_text_color(VMG_ os_color_t fg, os_color_t bg);
382
383 /* set the body color */
384 void set_body_color(VMG_ os_color_t color);
385
386 /* set the caps flag - capitalize the next character */
387 void caps();
388
389 /* set the nocaps flag - make the next letter miniscule */
390 void nocaps();
391
392 /* flush the output with the given newline type */
393 void flush(VMG_ vm_nl_type nl);
394
395 /* flush the output, ending the current line and starting a new line */
396 void flush(VMG0_) { flush(vmg_ VM_NL_NEWLINE); }
397
398 /* empty our buffers */
399 void empty_buffers(VMG0_);
400
401 /* clear the window */
402 virtual void clear_window(VMG0_) = 0;
403
404 /*
405 * Flush all windows we control. By default, we just flush our own
406 * window; consoles that manage multiple windows should flush their
407 * managed windows here as well.
408 */
409 virtual void flush_all(VMG_ vm_nl_type nl) { flush(vmg_ nl); }
410
411 /* immediately update the display window */
412 void update_display(VMG0_);
413
414 /*
415 * Open a new log file. Closes any previous log file. If the file
416 * already exists, we'll overwrite it with the new log information,
417 * otherwise we'll create a new file. Returns zero on success,
418 * non-zero on failure.
419 */
420 int open_log_file(VMG_ const char *fname);
421 int open_log_file(VMG_ const vm_val_t *filespec,
422 const struct vm_rcdesc *rc);
423
424 /*
425 * close any existing log file - returns zero on success, non-zero
426 * on failure
427 */
428 int close_log_file(VMG0_);
429
430 /*
431 * Open a new command log file. We'll log commands (and only
432 * commands) to the command log file. Returns zero on success,
433 * non-zero on failure.
434 */
435 int open_command_log(VMG_ const char *fname, int event_script);
436 int open_command_log(VMG_ const vm_val_t *filespec,
437 const struct vm_rcdesc *rc, int event_script);
438
439 /* close the command log file, if there is one */
440 int close_command_log(VMG0_);
441
442 /*
443 * Set the current MORE mode. Returns the old state. The state is
444 * true if we show MORE prompts, false if not. The state will be
445 * false if the underlying OS display layer handles prompting, so a
446 * return of false doesn't necessarily mean that MORE prompts are
447 * never shown, but merely that we don't handle MORE prompts in the
448 * output formatter itself.
449 */
450 virtual int set_more_state(int state) = 0;
451
452 /* determine if we're in MORE mode */
453 virtual int is_more_mode() const = 0;
454
455 /* get the line width of the display device */
456 virtual int get_line_width() const = 0;
457
458 /*
459 * Do we allow overrunning the line width when we can't find a natural
460 * breaking point (at a whitespace character, for example) such that
461 * we can fit some text within the line width?
462 *
463 * If this returns false, then we'll force a newline when we reach the
464 * line width, even if doing so breaks up a single word that doesn't
465 * have a natural breaking point within.
466 */
467 virtual int allow_overrun() const = 0;
468
469 /* get the page length of the display device */
470 virtual int get_page_length() const = 0;
471
472 /* get/set the double-space flag (for periods and other punctuation) */
473 int get_doublespace() const { return doublespace_; }
474 void set_doublespace(int f) { doublespace_ = f; }
475
476 /* reset the MORE prompt line count */
477 void reset_line_count(int clearing);
478
479 /*
480 * check to see if we're reading from a script input file - returns
481 * true if so, false if reading from the user (via the keyboard or
482 * other input device)
483 */
484 int is_reading_script() const { return (script_sp_ != 0); }
485
486 /*
487 * check to see if we're reading quietly from a script - if we're
488 * reading from a script, and this flag is true, we suppress all
489 * output
490 */
491 int is_quiet_script() const
492 { return (script_sp_ != 0 && script_sp_->quiet); }
493
494 /* is the script in MORE mode? */
495 int is_moremode_script() const
496 { return (script_sp_ != 0 && script_sp_->more_mode); }
497
498 /* is the script an <eventscript> type? */
499 int is_event_script() const
500 { return (script_sp_ != 0 && script_sp_->event_script); }
501
502 /*
503 * Open a script file. If 'quiet' is true, no output is displayed
504 * while the script is being processed. If 'script_more_mode' is true,
505 * MORE mode is in effect while processing the script, otherwise MORE
506 * mode is turned off while processing the script (to leave things as
507 * they are, simply pass in is_more_mode() for this argument).
508 *
509 * Returns 0 on success, non-zero on error.
510 */
511 int open_script_file(VMG_ const char *fname,
512 int quiet, int script_more_mode);
513 int open_script_file(VMG_ const vm_val_t *filespec,
514 const struct vm_rcdesc *rc,
515 int quiet, int script_more_mode);
516
517 /*
518 * Close the script file. Returns the original MORE mode that was
519 * in effect before the script file was opened; this MORE mode
520 * should be restored.
521 */
522 int close_script_file(VMG0_);
523
524 /*
525 * Read a line of text from the keyboard. Fills in the buffer with
526 * a null-terminated UTF-8 string. Returns zero on success,
527 * non-zero on end-of-file reading the console (which usually
528 * indicates that the user has closed the application, so we're in
529 * the process of terminating; it might also indicate that the
530 * user's terminal has been detached, in which case we also probably
531 * can't do much except terminate).
532 */
533 int read_line(VMG_ char *buf, size_t buflen, int bypass_script = FALSE);
534
535 /*
536 * Read a line of input with optional timeout. Fills in the buffer
537 * with a null-terminated UTF-8 string. Returns an OS_EVT_xxx code,
538 * according to how the input was terminated:
539 *
540 * OS_EVT_LINE - the user pressed Return to enter the text
541 *. OS_EVT_TIMEOUT - the timeout expired before the user pressed Return
542 *. OS_EVT_EOF - an error occurred reading the input
543 *
544 * This routine is a cover for the low-level os_gets_timeout(), and
545 * behaves essentially the same way. Note in particular that if this
546 * routine returns OS_EVT_TIMEOUT, then our read_line_cancel() routine
547 * must be called before any output or other display changes can be
548 * made, with the exception that another call to read_line_timeout()
549 * is always allowed.
550 */
551 int read_line_timeout(VMG_ char *buf, size_t buflen,
552 unsigned long timeout, int use_timeout,
553 int bypass_script = FALSE);
554
555 /*
556 * Cancel a line of input in progress, which was interrupted by a
557 * timeout in read_line_timeout(). If 'reset' is true, we'll forget
558 * any editing state from the prior line.
559 */
560 void read_line_cancel(VMG_ int reset);
561
562 /*
563 * Display a file dialog. This routine works exactly the same way
564 * as os_askfile(), but is implemented here to allow for a formatted
565 * text interface on systems where no dialog is available.
566 */
567 int askfile(VMG_ const char *prompt, size_t prompt_len,
568 char *reply, size_t replen,
569 int dialog_type, os_filetype_t file_type,
570 int bypass_script = FALSE);
571
572 /*
573 * Display a system dialog. This routine works exactly the same way
574 * as os_input_dialog(), but is implemented here to allow for a
575 * formatted text interface on systems where no dialog is available.
576 */
577 int input_dialog(VMG_ int icon_id, const char *prompt,
578 int standard_button_set,
579 const char **buttons, int button_count,
580 int default_index, int cancel_index,
581 int bypass_script = FALSE);
582
583 /* show the MORE prompt and wait for the user to acknowledge it */
584 virtual void show_more_prompt(VMG0_) = 0;
585
586 /*
587 * Log an event. This saves the event to the current script log, if
588 * there is one, in the proper format for the script. We return the
589 * event code.
590 *
591 * 'evt' is the event type, as an OS_EVT_xxx or VMCON_EVT_xxx code.
592 *
593 * 'param' can be given in the local UI character set or in UTF-8 -
594 * specify which it is via 'param_is_utf8'. We write the file in the
595 * local UI character set, so if the parameter is given in UTF-8, we
596 * have to translate it.
597 */
598 int log_event(VMG_ int evt,
599 const char *param, size_t paramlen, int param_is_utf8);
600 int log_event(VMG_ int evt)
601 { return log_event(vmg_ evt, 0, 0, FALSE); }
602
603 /* log an event with the given event type tag */
604 void log_event(VMG_ const char *tag,
605 const char *param, size_t paramlen,
606 int param_is_utf8);
607
608 /* read an event from an event script */
609 int read_event_script(VMG_ int *evt, char *buf, size_t buflen,
610 const int *filter, int filter_cnt,
611 unsigned long *attrs);
612
613 protected:
614 /* the destructor is protected - use delete_obj() to delete */
615 virtual ~CVmConsole() { }
616
617 /*
618 * Service routine - show MORE prompt on this console. This can be
619 * called from show_more_prompt() when a MORE prompt is desired at all
620 * in the subclassed console.
621 */
622 void show_con_more_prompt(VMG0_);
623
624 /* read a line from the script file */
625 int read_line_from_script(char *buf, size_t buflen, int *evt);
626
627 /* read the type tag from the next script event */
628 int read_script_event_type(int *evt, unsigned long *attrs);
629
630 /* skip to the next line of the script */
631 void skip_script_line(osfildef *fp);
632
633 /*
634 * read a script parameter - this reads the rest of the line into the
635 * given buffer, and skips to the start of the next line in the script;
636 * returns true on success, false if we reach EOF before reading
637 * anything
638 */
639 int read_script_param(char *buf, size_t buflen, osfildef *fp);
640
641 /* internal routine to terminate line reading */
642 void read_line_done(VMG0_);
643
644 /* write utf-8 text to a file, mapping to the given file character set */
645 void write_to_file(osfildef *fp, const char *txt,
646 class CCharmapToLocal *map);
647
648
649 /* open a comand log file - internal version */
650 int open_command_log(VMG_ class CVmNetFile *netfile, int event_script);
651
652 /* open a script file - internal version */
653 int open_script_file(VMG_ class CVmNetFile *netfile,
654 const vm_val_t *filspec,
655 int quiet, int script_more_mode);
656
657 /* our current display stream */
658 class CVmFormatter *disp_str_;
659
660 /* our log stream - this stream is written to the log file, if any */
661 class CVmFormatterLog *log_str_;
662
663 /*
664 * Flag: the log stream is enabled. We can temporarily disable the
665 * log stream, such as when writing to the statusline stream.
666 */
667 unsigned int log_enabled_ : 1;
668
669 /*
670 * Flag: display two spaces after a period-like punctuation mark.
671 * This should be true if the output should have two spaces after a
672 * period, question mark, or exclamation point, false for a single
673 * space. This should generally be true for fixed-width fonts,
674 * false for proportional fonts, although some users might prefer to
675 * use single-spacing even for fixed-width fonts.
676 */
677 unsigned int doublespace_ : 1;
678
679 /*
680 * flag: the command log is an event script; if this is set, we log all
681 * input events in the tagged <eventscript> file format, otherwise we
682 * log just command lines in the old-style ">line" format
683 */
684 unsigned int command_eventscript_ : 1;
685
686 /*
687 * Script-input stack. Each time we open a script, we create a new
688 * stack entry object and link it at the head of the list. So, the
689 * head of the list is the current state, the next element is the
690 * enclosing state, and so on.
691 */
692 script_stack_entry *script_sp_;
693
694 /* command log file, if there is one */
695 class CVmDataSource *command_fp_;
696 class CVmNetFile *command_nf_;
697 struct vm_globalvar_t *command_glob_;
698 };
699
700 /* ------------------------------------------------------------------------ */
701 /*
702 * Main system console. This console is attached to the OS-level primary
703 * console.
704 */
705 class CVmConsoleMain: public CVmConsole
706 {
707 public:
708 CVmConsoleMain(VMG0_);
709
710 /* delete */
711 void delete_obj(VMG0_);
712
713 /* get the system banner manager */
714 class CVmBannerManager *get_banner_manager() const
715 { return banner_manager_; }
716
717 /* get the system log console manager */
718 class CVmLogConsoleManager *get_log_console_manager() const
719 { return log_console_manager_; }
720
721 /*
722 * Switch in or out of statusline mode. When we're running on the text
723 * implementation of the OS layer, we must explicitly switch modes
724 * between the main text stream and statusline stream. 'mode' is true
725 * to switch to statusline mode, false to switch back to main text
726 * mode.
727 */
728 void set_statusline_mode(VMG_ int mode);
729
730 /* clear the window */
731 virtual void clear_window(VMG0_);
732
733 /* set MORE mode */
734 virtual int set_more_state(int state)
735 {
736 int old_state;
737
738 /* remember the old state */
739 old_state = G_os_moremode;
740
741 /* set the new mode */
742 G_os_moremode = state;
743
744 /* return the previous state */
745 return old_state;
746 }
747
748 /* get the MORE mode */
749 virtual int is_more_mode() const { return G_os_moremode; }
750
751 /*
752 * Flush everything - this flushes not only the main console, but any
753 * banner windows we're managing. This should be called before pausing
754 * for input or for a timed delay, to make sure that buffered output in
755 * all windows is shown.
756 */
757 void flush_all(VMG_ vm_nl_type nl);
758
759 /* get the line width of the display device */
760 virtual int get_line_width() const { return G_os_linewidth; }
761
762 /* do not allow overrunning the line width on the main console */
763 virtual int allow_overrun() const { return FALSE; }
764
765 /* get the page length of the display device */
766 virtual int get_page_length() const { return G_os_pagelength; }
767
768 /* show the MORE prompt */
769 virtual void show_more_prompt(VMG0_) { show_con_more_prompt(vmg0_); }
770
771 protected:
772 /* main text area display stream */
773 class CVmFormatterMain *main_disp_str_;
774
775 /* statusline display stream */
776 class CVmFormatterStatline *statline_str_;
777
778 /*
779 * The system banner window manager. Since the main console is
780 * inherently a singleton (as there's only one OS-level primary
781 * console), we keep track of the banner manager.
782 */
783 class CVmBannerManager *banner_manager_;
784
785 /* the system log console manager */
786 class CVmLogConsoleManager *log_console_manager_;
787 };
788
789
790 /* ------------------------------------------------------------------------ */
791 /*
792 * Banner-window console.
793 */
794 class CVmConsoleBanner: public CVmConsole
795 {
796 public:
797 /* create */
798 CVmConsoleBanner(void *banner_handle, int win_type, unsigned long style);
799
800 /* delete */
801 void delete_obj(VMG0_);
802
803 /* retrieve our OS-level banner handle */
804 void *get_os_handle() const { return banner_; }
805
806 /* get banner information */
807 int get_banner_info(os_banner_info_t *info);
808
809 /* clear the window */
810 virtual void clear_window(VMG0_);
811
812 /* set MORE mode */
813 virtual int set_more_state(int state)
814 {
815 /* banners never change the global MORE mode state */
816 return is_more_mode();
817 }
818
819 /* get the MORE mode - return the global mode flag */
820 virtual int is_more_mode() const { return G_os_moremode; }
821
822 /* show the MORE prompt - does nothing for a banner window */
823 virtual void show_more_prompt(VMG0_) { show_con_more_prompt(vmg0_); }
824
825 /* get the line width of the display device */
826 virtual int get_line_width() const
827 { return os_banner_get_charwidth(banner_); }
828
829 /* allow overrunning the line width in a banner */
830 virtual int allow_overrun() const { return TRUE; }
831
832 /* get the page length of the display device, for MORE mode */
833 virtual int get_page_length() const
834 { return os_banner_get_charheight(banner_); }
835
836 protected:
837 /* our underlying OS-level banner handle */
838 void *banner_;
839
840 /* our window type (an OS_BANNER_TYPE_xxx code) */
841 int win_type_;
842 };
843
844 /* ------------------------------------------------------------------------ */
845 /*
846 * Common base class for log-only consoles
847 */
848 class CVmConsoleLogBase: public CVmConsole
849 {
850 public:
851 /* clear the window - do nothing on a log console */
852 virtual void clear_window(VMG0_) { }
853
854 /* set MORE mode - doesn't apply to us */
855 virtual int set_more_state(int state) { return FALSE; }
856
857 /* show the MORE prompt - does nothing */
858 virtual void show_more_prompt(VMG0_) { }
859
860 /* get the MORE mode */
861 virtual int is_more_mode() const { return FALSE; }
862
863 /* get the line width of the display device */
864 virtual int get_line_width() const { return G_os_linewidth; }
865
866 /* allow overrunning the line width in a lot file */
867 virtual int allow_overrun() const { return TRUE; }
868
869 /*
870 * get the page length of the display device - this is arbitrary, since
871 * we don't use MORE mode anyway
872 */
873 virtual int get_page_length() const { return 55; }
874 };
875
876 /*
877 * Log console. This is used to create a console that has no display
878 * presence, but simply captures its output directly to a log file. (This
879 * is similar to the log file attached to a regular display console, but
880 * this kind of console ONLY has the log file.)
881 */
882 class CVmConsoleLog: public CVmConsoleLogBase
883 {
884 public:
885 /* create */
886 CVmConsoleLog(VMG_ CVmNetFile *nf, osfildef *fp,
887 class CCharmapToLocal *cmap, int width);
888
889 void delete_obj(VMG0_);
890
891 protected:
892 };
893
894 /*
895 * A special console that sends output to the *main* console's log file.
896 * This allows code to be written with a generic console pointer, rather
897 * than a test checking for the main console or some other console; just
898 * plug this in when there's not another console pointer and output will go
899 * to the main console automatically.
900 */
901 class CVmConsoleMainLog: public CVmConsoleLogBase
902 {
903 public:
904 /* create */
905 CVmConsoleMainLog() { }
906
907 /* we send text to the main console's log */
908 int format_text(VMG_ const char *p, size_t len)
909 {
910 /* send the text to the main console's log file */
911 return G_console->format_text_to_log(vmg_ p, len);
912 }
913
914 protected:
915 ~CVmConsoleMainLog() { }
916 };
917
918
919 /* ------------------------------------------------------------------------ */
920 /*
921 * constants
922 */
923
924 /*
925 * HTML lexical analysis modes. We use these modes for two purposes:
926 *
927 * First, for tracking our state while doing our own HTML parsing, which we
928 * do when the underlying renderer handles only plain text (in which case
929 * we must interpret and remove any HTML tags from the stream before
930 * sending the stream on to the underlying renderer).
931 *
932 * Second, for tracking the lexical state in the underlying renderer, when
933 * the underlying renderer handles full HTML interpretation. In this case,
934 * we simply pass HTML tags through to the underlying renderer; but even
935 * though we don't interpret the tags, we do keep track of the lexical
936 * structure of the text, so that we can tell when we're inside a tag and
937 * when we're in ordinary text. Certain operations we apply (such as case
938 * conversions with "\^" and "\v" sequences) apply only to ordinary text,
939 * so we need to know what's what in the stream text.
940 */
941 enum vmconsole_html_state
942 {
943 VMCON_HPS_NORMAL, /* normal text, not in a tag */
944 VMCON_HPS_TAG_START, /* parsing the start of a tag */
945 VMCON_HPS_TAG_NAME, /* parsing a tag name */
946 VMCON_HPS_TAG, /* parsing inside a tag */
947 VMCON_HPS_ATTR_NAME, /* parsing an attribute name */
948 VMCON_HPS_ATTR_VAL, /* parsing an attribute value */
949 VMCON_HPS_SQUOTE, /* in a single-quoted string in a tag */
950 VMCON_HPS_DQUOTE, /* in a double-quoted string in a tag */
951 VMCON_HPS_ENTITY_1ST, /* first character in an entity name */
952 VMCON_HPS_ENTITY_NUM_1ST, /* first character of a numeric entitiy */
953 VMCON_HPS_ENTITY_HEX, /* in a hexadecimal entitiy number */
954 VMCON_HPS_ENTITY_DEC, /* in a decimal entity number */
955 VMCON_HPS_ENTITY_NAME, /* in a named entity */
956 VMCON_HPS_MARKUP_END /* at last character of a markup */
957 };
958
959 /*
960 * HTML parsing mode flag for <BR> tags. We defer these until we've
961 * read the full tag in order to obey an HEIGHT attribute we find. When
962 * we encounter a <BR>, we figure out whether we think we'll need a
963 * flush or a blank line; if we find a HEIGHT attribute, we may change
964 * this opinion.
965 */
966 enum vmconsole_html_br_mode
967 {
968 HTML_DEFER_BR_NONE, /* no pending <BR> */
969 HTML_DEFER_BR_FLUSH, /* only need to flush output */
970 HTML_DEFER_BR_BLANK /* need a blank line */
971 };
972
973 /*
974 * Color/attribute information. Each character is tagged with its current
975 * color and attributes.
976 */
977 struct vmcon_color_t
978 {
979 /* foreground color */
980 os_color_t fg;
981
982 /* background color */
983 os_color_t bg;
984
985 /* the current OS_ATTR_xxx attributes */
986 int attr;
987
988 /* check for equality with another color structure */
989 int equals(const vmcon_color_t *other) const
990 {
991 return (fg == other->fg
992 && bg == other->bg
993 && attr == other->attr);
994 }
995 };
996
997 /* ------------------------------------------------------------------------ */
998 /*
999 * Output Buffer Flags. Each character in the output buffer has an
1000 * associated flag value associated with it; the flag value is a
1001 * combination of the bit flags defined here.
1002 */
1003
1004 /*
1005 * Unbreakable character point. This indicates that a line break is not
1006 * allowed to follow this character, even if the text at this point would
1007 * ordinarily allow a soft line break. Note that this does NOT override a
1008 * hard line break (i.e., an explicit newline); it merely prevents a soft
1009 * line break from occurring immediately after this character.
1010 *
1011 * We apply this flag to the character immediately preceding an explicit
1012 * non-breaking space in the text stream.
1013 */
1014 #define VMCON_OBF_NOBREAK 0x01
1015
1016 /*
1017 * Breakable character point. This indicates that a line break is allowed
1018 * to follow this character, even if the text at this point would not
1019 * ordinarily allow a soft line break.
1020 *
1021 * We apply this flag to the character immediately preceding an explicit
1022 * zero-width breakable space in the text stream.
1023 */
1024 #define VMCON_OBF_OKBREAK 0x02
1025
1026 /*
1027 * Break-anywhere mode. This indicates that this character is part of a
1028 * run of break-anywhere text. In break-anywhere text, we're allowed to
1029 * break lines between any two characters, except adjacent to explicit
1030 * non-breaking spaces in the text stream.
1031 *
1032 * Note that the NOBREAK flag overrides this flag, because this flag
1033 * merely indicates the mode, while the NOBREAK flag indicates an explicit
1034 * non-breaking indicator.
1035 */
1036 #define VMCON_OBF_BREAK_ANY 0x04
1037
1038 /*
1039 * Soft hyphenation point. This flag indicates that the text can be
1040 * hyphenated immediately following this character, which is to say that
1041 * we can insert a hyphen following this character and break the line
1042 * after the hyphen. If we do not hyphenate here, the soft hyphen has no
1043 * effect; in particular, no hyphen character appears in the output stream
1044 * when a soft hyphen is not used as a line break point.
1045 */
1046 #define VMCON_OBF_SHY 0x08
1047
1048 /*
1049 * Quoted space. This indicates that this is a space character that
1050 * doesn't collapse in runs of contiguous whitespace.
1051 */
1052 #define VMCON_OBF_QSPACE 0x10
1053
1054
1055 /* ------------------------------------------------------------------------ */
1056 /*
1057 * Output formatter stream interface. A formatter stream performs
1058 * formatting on a stream of displayed text.
1059 *
1060 * A given stream of display text might be fed into more than one
1061 * formatter stream. For example, if logging is turned on for a
1062 * console, we'll feed the same text to the console's main formatting
1063 * stream, which will end up being displayed to the user, and also to
1064 * the log file's formatting stream, which will end up written out to
1065 * the log file.
1066 *
1067 * Note that the formatter interface is internal to the console system.
1068 * Client code should never need to refer to a formatter object, but
1069 * should instead call the console object (CVmConsole).
1070 */
1071
1072 /* <TAB> tag alignment types */
1073 enum vmfmt_tab_align_t
1074 {
1075 VMFMT_TAB_NONE,
1076 VMFMT_TAB_LEFT,
1077 VMFMT_TAB_CENTER,
1078 VMFMT_TAB_RIGHT,
1079 VMFMT_TAB_DECIMAL
1080 };
1081
1082 /* maximum formatter state stack depth */
1083 const size_t CVMFMT_STACK_MAX = 25;
1084
1085 /* maximum depth of color stack */
1086 const size_t CVFMT_CLRSTK_MAX = 25;
1087
1088 /* maximum length of an attribute name/value */
1089 const size_t CVFMT_MAX_ATTR_NAME = 40;
1090 const size_t CVFMT_MAX_ATTR_VAL = 256;
1091
1092 /* output stream information structure (forward declaration) */
1093 typedef struct out_stream_info out_stream_info;
1094
1095 /* output formatter stream */
1096 class CVmFormatter
1097 {
1098 public:
1099 CVmFormatter(class CVmConsole *console)
1100 {
1101 /* remember our display object */
1102 console_ = console;
1103
1104 /* there's no title buffer by default */
1105 html_title_buf_ = 0;
1106 html_title_buf_size_ = 0;
1107
1108 /* we have no horizontal tab table yet (for the HTML mini-parser) */
1109 tabs_ = 0;
1110
1111 /* no character mapper yet */
1112 cmap_ = 0;
1113 }
1114
1115 /*
1116 * delete the object (call this instead of calling the destructor
1117 * directly, since some subclasses need global access)
1118 */
1119 virtual void delete_obj(VMG0_) { delete this; }
1120
1121 /* initialize */
1122 virtual void init()
1123 {
1124 /* start out at the first column */
1125 linepos_ = 0;
1126 linecol_ = 0;
1127 linebuf_[0] = '\0';
1128 is_continuation_ = FALSE;
1129
1130 /* start out in normal text color */
1131 cur_color_.fg = OS_COLOR_P_TEXT;
1132 cur_color_.bg = OS_COLOR_P_TRANSPARENT;
1133 cur_color_.attr = 0;
1134 os_color_ = cur_color_;
1135
1136 /* no pending tab yet */
1137 pending_tab_align_ = VMFMT_TAB_NONE;
1138
1139 /* start out at the first line */
1140 linecnt_ = 0;
1141
1142 /* we're not in either "caps", "nocaps", or "allcaps" mode yet */
1143 capsflag_ = nocapsflag_ = allcapsflag_ = FALSE;
1144
1145 /*
1146 * set the initial buffer flags: start out in word-break (not
1147 * break-anywhere) more
1148 */
1149 cur_flags_ = 0;
1150
1151 /*
1152 * start in normal spacing mode - treat runs of whitespace as
1153 * equivalent to a single space
1154 */
1155 obey_whitespace_ = FALSE;
1156
1157 /* start out in HTML interpretation mode */
1158 literal_mode_ = FALSE;
1159
1160 /* presume we won't have OS-level line wrapping */
1161 os_line_wrap_ = FALSE;
1162
1163 /* we haven't flushed a new line yet */
1164 just_did_nl_ = FALSE;
1165
1166 /* assume that the underlying system is not HTML-enabled */
1167 html_target_ = FALSE;
1168
1169 /* presume this target accepts OS highlighting sequences */
1170 plain_text_target_ = FALSE;
1171
1172 /*
1173 * start out in "normal" lexical state in our parser and in the
1174 * underlying output stream
1175 */
1176 html_parse_state_ = VMCON_HPS_NORMAL;
1177 html_passthru_state_ = VMCON_HPS_NORMAL;
1178
1179 /* not in an ignored tag yet (-> nesting depth is zero) */
1180 html_in_ignore_ = 0;
1181
1182 /* not in title mode yet (-> nesting depth is zero) */
1183 html_in_title_ = 0;
1184
1185 /* not yet deferring line breaks */
1186 html_defer_br_ = HTML_DEFER_BR_NONE;
1187
1188 /* not yet in quotes (-> nesting depth is zero) */
1189 html_quote_level_ = 0;
1190
1191 /* not in a PRE section */
1192 html_pre_level_ = 0;
1193
1194 /* we're not parsing inside any tags yet */
1195 html_allow_alt_ = FALSE;
1196 html_allow_color_ = FALSE;
1197 html_in_body_ = FALSE;
1198 html_in_tab_ = FALSE;
1199 html_in_wrap_ = FALSE;
1200
1201 /* assume we're a normal display stream */
1202 is_disp_stream_ = TRUE;
1203
1204 /* no stacked colors yet */
1205 color_sp_ = 0;
1206 }
1207
1208 /* get/set obey-whitespace mode */
1209 int get_obey_whitespace() const { return obey_whitespace_ != 0; }
1210 void set_obey_whitespace(int f) { obey_whitespace_ = (f != 0); }
1211
1212 /* turn on CAPS mode */
1213 void caps()
1214 {
1215 /* turn on CAPS mode */
1216 capsflag_ = TRUE;
1217
1218 /* turn off the NOCAPS and ALLCAPS modes */
1219 nocapsflag_ = FALSE;
1220 allcapsflag_ = FALSE;
1221 }
1222
1223 /* turn on NOCAPS mode */
1224 void nocaps()
1225 {
1226 /* turn on NOCAPS mode */
1227 nocapsflag_ = TRUE;
1228
1229 /* turn off CAPS and ALLCAPS mode */
1230 capsflag_ = FALSE;
1231 allcapsflag_ = FALSE;
1232 }
1233
1234 /* turn on or off ALLCAPS mode */
1235 void allcaps(int all_caps)
1236 {
1237 /* set the ALLCAPS flag */
1238 allcapsflag_ = all_caps;
1239
1240 /* clear the CAPS and NOCAPS flags */
1241 capsflag_ = FALSE;
1242 nocapsflag_ = FALSE;
1243 }
1244
1245 /*
1246 * Display a string of a given byte-length. The text is given in
1247 * UTF-8 format. We'll interpret embedded control codes (newlines,
1248 * blank lines, caps flags, etc); depending on the display mode, we
1249 * might also interpret HTML markup sequences.
1250 */
1251 int format_text(VMG_ const char *s, size_t len);
1252
1253 /* set the text attributes for subsequent format_text() displays */
1254 void set_text_attr(VMG_ int attr)
1255 {
1256 /* remember the new text attributes */
1257 cur_color_.attr = attr;
1258 }
1259
1260 /* set the text color for subsequent format_text() displays */
1261 void set_text_color(VMG_ os_color_t fg, os_color_t bg)
1262 {
1263 /* remember the new text colors */
1264 cur_color_.fg = fg;
1265 cur_color_.bg = bg;
1266 }
1267
1268 /* write a blank line to the stream */
1269 void write_blank_line(VMG0_);
1270
1271 /*
1272 * Flush the current line to the display, using the given type of
1273 * line termination.
1274 */
1275 void flush(VMG_ vm_nl_type nl);
1276
1277 /* clear our buffers */
1278 void empty_buffers(VMG0_);
1279
1280 /* immediately update the display window */
1281 void update_display(VMG0_);
1282
1283 /*
1284 * determine if I'm an HTML target - this returns true if the
1285 * underyling renderer accepts HTML, in which case we write HTML
1286 * markups directly to the os_xxx routines (os_printz, for example).
1287 */
1288 int is_html_target() const { return html_target_; }
1289
1290 /* reset the MORE prompt line counter */
1291 virtual void reset_line_count(int clearing) { linecnt_ = 0; }
1292
1293 /*
1294 * Note that we read a line from the keyboard with echo. Reading a
1295 * line displays a carriage return without our help, taking us back to
1296 * the first column - we need to know this so that we can deal
1297 * properly with output on the following line.
1298 */
1299 void note_input_line()
1300 {
1301 /*
1302 * a CR/LF will have been echoed automatically by the input
1303 * reader, which takes us back to the first column
1304 */
1305 linecol_ = 0;
1306
1307 /* note that we effectively just wrote a newline */
1308 just_did_nl_ = TRUE;
1309 }
1310
1311 /* -------------------------------------------------------------------- */
1312 /*
1313 * Virtual interface to underlying OS renderer
1314 */
1315
1316 /* turn HTML mode on/off in the underlying OS-level renderer */
1317 virtual void start_html_in_os() = 0;
1318 virtual void end_html_in_os() = 0;
1319
1320 /* set the text color */
1321 virtual void set_os_text_color(os_color_t fg, os_color_t bg) = 0;
1322
1323 /* set the body color */
1324 virtual void set_os_body_color(os_color_t) = 0;
1325
1326 /* set text attributes */
1327 virtual void set_os_text_attr(int attr) = 0;
1328
1329 /*
1330 * display text to the OS window; the text is given in the local
1331 * character set
1332 */
1333 virtual void print_to_os(const char *txt) = 0;
1334
1335 /* flush the underlying OS-level rendere */
1336 virtual void flush_to_os() = 0;
1337
1338 /* set the window title in the OS layer */
1339 virtual void set_title_in_os(const char *txt) = 0;
1340
1341 /* set a new character mapper */
1342 void set_charmap(class CCharmapToLocal *cmap);
1343
1344 protected:
1345 virtual ~CVmFormatter();
1346
1347 /*
1348 * Write a line (or a partial line) of text to the stream, using the
1349 * indicated line breaking. The text is given as wide Unicode
1350 * characters.
1351 */
1352 void write_text(VMG_ const wchar_t *txt, size_t cnt,
1353 const vmcon_color_t *colors, vm_nl_type nl);
1354
1355 /* write a tab to the stream */
1356 void write_tab(VMG_ int indent, int multiple);
1357
1358 /* flush the current line, with or without padding */
1359 void flush_line(VMG_ int padding);
1360
1361 /*
1362 * Buffer a character of output. The character is presented to us as a
1363 * wide Unicode character. We'll expand this character with the local
1364 * character mapping's expansion rules, then add the expansion to our
1365 * output buffer, performing word-wrapping as needed.
1366 */
1367 void buffer_char(VMG_ wchar_t c);
1368
1369 /*
1370 * buffer an expanded character - we'll buffer the given unicode
1371 * character, with no further expansion
1372 */
1373 void buffer_expchar(VMG_ wchar_t c);
1374
1375 /* buffer a rendered expanded character */
1376 void buffer_rendered(wchar_t c, unsigned char flags, int wid);
1377
1378 /*
1379 * Buffer a string of output to the stream. The string is in UTF-8
1380 * format.
1381 */
1382 void buffer_string(VMG_ const char *txt);
1383
1384 /* buffer a wide unicode character string */
1385 void buffer_wstring(VMG_ const wchar_t *txt);
1386
1387 /* get the next wide unicode character in a UTF8-encoded string */
1388 static wchar_t next_wchar(const char **s, size_t *len);
1389
1390 /*
1391 * Determine if we should use MORE mode in the formatter layer; if
1392 * this returns true, we handle MORE prompting ourselves, otherwise
1393 * we handle it through the OS layer.
1394 *
1395 * The default version of this function is implemented in the output
1396 * formatter configuration module. This function can also be
1397 * overridden in a subclass (for example, a log stream never uses
1398 * MORE mode, no matter what configuration we're using).
1399 */
1400 virtual int formatter_more_mode() const;
1401
1402 /*
1403 * Get the maximum column to store in our internal buffer. If we're
1404 * doing our own line breaking, we'll break off the buffer at the
1405 * actual line width of the underlying console. If we're doing OS
1406 * line wrapping, we'll simply fill up our internal buffer to its
1407 * maximum size, since the flushing points are irrelevant to the line
1408 * wrapping the underlying OS console will be doing and hence we might
1409 * as well buffer as much as we can for efficiency.
1410 */
1411 virtual int get_buffer_maxcol() const
1412 {
1413 /*
1414 * if the OS is doing the line wrapping, use the full buffer;
1415 * otherwise, use the actual line width from the underyling
1416 * console
1417 */
1418 if (os_line_wrap_)
1419 return OS_MAXWIDTH;
1420 else
1421 return console_->get_line_width();
1422 }
1423
1424 /*
1425 * Determine if the underlying stream interprets HTML markups. We
1426 * call this during initialization to set our html_target_ member
1427 * (we cache the result since we check it frequently).
1428 *
1429 * This is implemented in the formatter configuration module.
1430 */
1431 virtual int get_init_html_target() const;
1432
1433 /* -------------------------------------------------------------------- */
1434 /*
1435 * HTML mini-parser. This only needs to be implemented when linking
1436 * with non-HTML osifc implementations; when osifc provides HTML
1437 * parsing, we don't need to do any HTML parsing here, so these can use
1438 * dummy implementations.
1439 */
1440
1441 /*
1442 * Resume text-only HTML mini-parser. This is called when we start
1443 * writing a new string and discover that we're parsing inside an HTML
1444 * tag in our mini-parser.
1445 *
1446 * Returns the next character after the run of text we parse.
1447 */
1448 wchar_t resume_html_parsing(VMG_ wchar_t c,
1449 const char **sp, size_t *slenp);
1450
1451 /* parse an HTML entity ('&') markup */
1452 wchar_t parse_entity(VMG_ wchar_t *ent, const char **sp, size_t *slenp);
1453
1454 /*
1455 * Parse the beginning HTML markup. This is called when we are
1456 * scanning a '<' or '&' character in output text, and we're in HTML
1457 * mode, and the underlying target doesn't support HTML parsing.
1458 * Returns the next character to process after we finish our initial
1459 * parsing.
1460 */
1461 wchar_t parse_html_markup(VMG_ wchar_t c,
1462 const char **sp, size_t *slenp);
1463
1464 /*
1465 * Expand any pending tab. This should be called when we're doing
1466 * HTML mini-parsing, and we see a <TAB> tag or we reach the end of an
1467 * output line. We'll expand any pending RIGHT/CENTER tab by going
1468 * back to the tab's location and inserting the necessary number of
1469 * spaces now that we know the extent of the text affected.
1470 *
1471 * If 'allow_anon' is true, we will allow "anonymous" tabs, which is
1472 * to say tabs with no ID attribute. Anonymous tabs allow text to be
1473 * tabbed relative to the full width of the line, but these are
1474 * meaningful only with normal line endings; when a line is flushed
1475 * before the end of the line is reached (because the line will be
1476 * used for a prompt, for example), anonymous tabs should not be
1477 * expanded with the line.
1478 */
1479 void expand_pending_tab(VMG_ int allow_anon);
1480
1481 /*
1482 * find a tab definition object; if 'create' it true and the specified
1483 * tab doesn't exist, we'll create a new tab object
1484 */
1485 class CVmFmtTabStop *find_tab(wchar_t *id, int create);
1486
1487 /* service routine - translate a wide character string to an integer */
1488 static int wtoi(const wchar_t *p);
1489
1490 /* parse a COLOR attribute of a FONT tag */
1491 int parse_color_attr(VMG_ const wchar_t *val, os_color_t *result);
1492
1493 /* push the current color for later restoration */
1494 void push_color()
1495 {
1496 /*
1497 * add a level to the color stack, if possible; if it's not
1498 * possible, assume we've lost an end tag somewhere, so rotate the
1499 * entire stack down a level
1500 */
1501 if (color_sp_ == CVFMT_CLRSTK_MAX)
1502 {
1503 /* take everything down a level */
1504 memmove(color_stack_, color_stack_ + 1,
1505 (CVFMT_CLRSTK_MAX - 1)*sizeof(color_stack_[0]));
1506 --color_sp_;
1507 }
1508
1509 /* save the current color in the stack */
1510 color_stack_[color_sp_++] = cur_color_;
1511 }
1512
1513 /* pop the current color */
1514 void pop_color()
1515 {
1516 /*
1517 * if we're at the bottom of the stack, we must have had too many
1518 * close tags; ignore the extra operation
1519 */
1520 if (color_sp_ != 0)
1521 {
1522 /* restore the next color down */
1523 cur_color_ = color_stack_[--color_sp_];
1524 }
1525 }
1526
1527 /*
1528 * Process a <nolog> tag. By default, we ignore this tag entirely.
1529 * Log streams should hide the contents of this tag.
1530 */
1531 virtual void process_nolog_tag(int /*is_end_tag*/)
1532 {
1533 /* by default, ignore this tag */
1534 }
1535
1536 /*
1537 * Process a <log> tag. By default, we hide the contents of this tag;
1538 * this tag marks text that is to be shown only in a log stream.
1539 */
1540 virtual void process_log_tag(int is_end_tag)
1541 {
1542 /* turn hiding on or off as appropriate */
1543 if (is_end_tag)
1544 --html_in_ignore_;
1545 else
1546 ++html_in_ignore_;
1547 }
1548
1549 /* -------------------------------------------------------------------- */
1550 /*
1551 * Member variables
1552 */
1553
1554 /* the console that owns this formatter stream */
1555 class CVmConsole *console_;
1556
1557 /* our character mapper */
1558 class CCharmapToLocal *cmap_;
1559
1560 /* current line position and output column */
1561 int linepos_;
1562 int linecol_;
1563
1564 /*
1565 * flag: the current buffer is a "continuation" line; that is, we've
1566 * already flushed a partial line to the display without moving to a
1567 * new line vertically, and the current buffer will be displayed on the
1568 * same line on the terminal, to the right of the previously-displayed
1569 * material
1570 */
1571 int is_continuation_;
1572
1573 /* number of lines on the screen (since last MORE prompt) */
1574 int linecnt_;
1575
1576 /*
1577 * Output buffer. We keep the output buffer as wide Unicode
1578 * characters, and translate to the local character set when we
1579 * flush the buffer and send the text to the os_xxx routines for
1580 * display.
1581 */
1582 wchar_t linebuf_[OS_MAXWIDTH + 1];
1583
1584 /*
1585 * Output buffer character colors. We keep track of the display color
1586 * of each character in the buffer.
1587 */
1588 vmcon_color_t colorbuf_[OS_MAXWIDTH + 1];
1589
1590 /*
1591 * output buffer flags - we keep a flag value for each character in
1592 * the output buffer
1593 */
1594 unsigned char flagbuf_[OS_MAXWIDTH + 1];
1595
1596 /* current attribute name/value buffers */
1597 wchar_t attrname_[CVFMT_MAX_ATTR_NAME];
1598 wchar_t attrval_[CVFMT_MAX_ATTR_VAL];
1599 wchar_t attr_qu_;
1600
1601 /* current color of characters being added to our buffer */
1602 vmcon_color_t cur_color_;
1603
1604 /* color of last OS output (via write_text) */
1605 vmcon_color_t os_color_;
1606
1607 /* stack of color attributes, saved for nested tags */
1608 vmcon_color_t color_stack_[CVFMT_CLRSTK_MAX];
1609 size_t color_sp_;
1610
1611 /*
1612 * Current output character flags. This is the base value to write to
1613 * flagbuf_ for each character we output, given the current mode
1614 * settings.
1615 */
1616 unsigned char cur_flags_;
1617
1618 /* obey-whitespace mode - treat whitespace as literal */
1619 unsigned int obey_whitespace_ : 1;
1620
1621 /* literal mode - ignore HTML markups, pass everything literally */
1622 unsigned int literal_mode_ : 1;
1623
1624 /* CAPS mode - next character output is converted to upper-case */
1625 unsigned int capsflag_ : 1;
1626
1627 /* NOCAPS mode - next character output is converted to lower-case */
1628 unsigned int nocapsflag_ : 1;
1629
1630 /* ALLCAPS mode - all characters output are converted to upper-case */
1631 unsigned int allcapsflag_ : 1;
1632
1633 /* flag indicating that we just flushed a new line */
1634 unsigned int just_did_nl_ : 1;
1635
1636 /*
1637 * Flag indicating that the underlying output system wants to
1638 * receive its output as HTML.
1639 *
1640 * If this is true, we'll pass through HTML to the underlying output
1641 * system, and in addition generate HTML sequences for certain
1642 * TADS-native escapes (for example, we'll convert the "\n" sequence
1643 * to a <BR> sequence).
1644 *
1645 * If this is false, we'll do just the opposite: we'll remove HTML
1646 * from the output stream and convert it into normal text sequences.
1647 */
1648 unsigned int html_target_ : 1;
1649
1650 /*
1651 * Flag indicating that the target uses plain text. If this flag is
1652 * set, we won't add the OS escape codes for highlighted characters.
1653 */
1654 unsigned int plain_text_target_ : 1;
1655
1656 /*
1657 * flag: the underlying OS layer handles line wrapping, so we never
1658 * need to write newlines when flushing our line buffer except when
1659 * we want to indicate a hard line break
1660 */
1661 unsigned int os_line_wrap_ : 1;
1662
1663 /*
1664 * Current lexical analysis state for our own HTML parsing. This is
1665 * used to track our HTML state when we have an underlying plain text
1666 * renderer.
1667 */
1668 vmconsole_html_state html_parse_state_;
1669
1670 /*
1671 * Current lexical analysis mode for the text stream going to the
1672 * underlying renderer. This is used to track the lexical structure of
1673 * the stream when we're passing HTML tags through to the underlying
1674 * renderer.
1675 */
1676 vmconsole_html_state html_passthru_state_;
1677
1678 /* last tag name */
1679 char html_passthru_tag_[32];
1680 char *html_passthru_tagp_;
1681
1682 /* <BR> defer mode */
1683 vmconsole_html_br_mode html_defer_br_;
1684
1685 /*
1686 * HTML "ignore" mode - we suppress all output when parsing the
1687 * contents of a <TITLE> or <ABOUTBOX> tag. This is a counter that
1688 * keeps track of the nesting depth for ignored tags.
1689 */
1690 int html_in_ignore_;
1691
1692 /*
1693 * HTML <TITLE> mode - when we're in this mode, we're gathering the
1694 * title (i.e., we're inside a <TITLE> tag's contents). We'll copy
1695 * characters to the title buffer rather than the normal output
1696 * buffer, and then call os_set_title() when we reach the </TITLE>
1697 * tag. This is a counter that keeps track of the nesting depth of
1698 * <TITLE> tags.
1699 */
1700 int html_in_title_;
1701
1702 /* buffer for the title */
1703 char *html_title_buf_;
1704 size_t html_title_buf_size_;
1705
1706 /* pointer to next available character in title buffer */
1707 char *html_title_ptr_;
1708
1709 /*
1710 * quoting level - this is a counter that keeps track of the nesting
1711 * depth of <Q> tags
1712 */
1713 int html_quote_level_;
1714
1715 /* PRE nesting depth */
1716 int html_pre_level_;
1717
1718 /*
1719 * Parsing mode flag for ALT attributes. If we're parsing a tag
1720 * that allows ALT, such as IMG or SOUND, we'll set this flag, then
1721 * insert the ALT text if we encounter it during parsing.
1722 */
1723 unsigned int html_allow_alt_ : 1;
1724
1725 /* parsing mode flag for COLOR attributes */
1726 unsigned int html_allow_color_ : 1;
1727
1728 /* parsing a BODY tag's attributes */
1729 unsigned int html_in_body_ : 1;
1730
1731 /* parsing a TAB tag's attributes */
1732 unsigned int html_in_tab_ : 1;
1733
1734 /* parsing a WRAP tag's attributes */
1735 unsigned int html_in_wrap_ : 1;
1736
1737 /* hash table of <TAB> objects */
1738 class CVmHashTable *tabs_;
1739
1740 /* characteristics of TAB tag we're defining */
1741 class CVmFmtTabStop *new_tab_entry_;
1742 vmfmt_tab_align_t new_tab_align_;
1743 wchar_t new_tab_dp_;
1744
1745 /*
1746 * Characteristics of pending tab. We must handle this tab when we
1747 * reach the next <TAB> or the end of the current line. If
1748 * pending_tab_align_ is VMFMT_TAB_NONE, it indicates that there is no
1749 * pending tab (since a pending tab always requires alignment).
1750 */
1751 vmfmt_tab_align_t pending_tab_align_;
1752 class CVmFmtTabStop *pending_tab_entry_;
1753 wchar_t pending_tab_dp_;
1754
1755 /*
1756 * starting column of pending tab - this is the output column where we
1757 * were writing when the pending tab was encountered, so this is the
1758 * column where we insert spaces for the tab
1759 */
1760 int pending_tab_start_;
1761
1762 /*
1763 * color/attributes active at start of pending tab - this is the color
1764 * we'll use for the spaces we insert when we insert the tab
1765 */
1766 vmcon_color_t pending_tab_color_;
1767
1768 /*
1769 * Flag: this is a display stream. Other types of streams (such as
1770 * log file streams) should set this to false.
1771 */
1772 unsigned int is_disp_stream_ : 1;
1773 };
1774
1775 /* ------------------------------------------------------------------------ */
1776 /*
1777 * Formatter for display windows
1778 */
1779 class CVmFormatterDisp: public CVmFormatter
1780 {
1781 public:
1782 CVmFormatterDisp(class CVmConsole *console)
1783 : CVmFormatter(console)
1784 {
1785 }
1786
1787 /* initialize */
1788 void init()
1789 {
1790 /* inherit base class initialization */
1791 CVmFormatter::init();
1792
1793 /*
1794 * if we're compiled for HTML mode, set the standard output
1795 * stream so that it knows it has an HTML target - this will
1796 * ensure that HTML tags are passed through to the underlying
1797 * stream, and that we generate HTML equivalents for our own
1798 * control sequences
1799 */
1800 html_target_ = get_init_html_target();
1801
1802 /*
1803 * since we always use HTML mode, turn on HTML mode in the
1804 * underlying OS window if our underlying OS renderer is HTML-aware
1805 */
1806 if (html_target_)
1807 start_html_in_os();
1808 }
1809 };
1810
1811 /* ------------------------------------------------------------------------ */
1812 /*
1813 * Formatter subclass for the main display
1814 */
1815 class CVmFormatterMain: public CVmFormatterDisp
1816 {
1817 public:
1818 CVmFormatterMain(class CVmConsole *console, size_t html_title_buf_size)
1819 : CVmFormatterDisp(console)
1820 {
1821 /* allocate a title buffer */
1822 html_title_buf_size_ = html_title_buf_size;
1823 if (html_title_buf_size_ != 0)
1824 html_title_buf_ = (char *)t3malloc(html_title_buf_size_);
1825 }
1826
1827 /* initialize */
1828 void init()
1829 {
1830 /* inherit base class initialization */
1831 CVmFormatterDisp::init();
1832
1833 /* remember the OS line wrap setting from the console */
1834 os_line_wrap_ = get_os_line_wrap();
1835 }
1836
1837 /* set the window title in the OS layer */
1838 virtual void set_title_in_os(const char *txt)
1839 {
1840 /* set the window title */
1841 os_set_title(txt);
1842 }
1843
1844 protected:
1845 ~CVmFormatterMain()
1846 {
1847 /* delete our title buffer */
1848 if (html_title_buf_ != 0)
1849 t3free(html_title_buf_);
1850 }
1851
1852 /*
1853 * Determine if the main console uses OS-level line wrapping - if this
1854 * is returns true, then an output formatter on this console will not
1855 * insert a newline at the end of a line that it's flushing for word
1856 * wrapping, but will instead let the underlying OS display layer
1857 * handle the wrapping.
1858 *
1859 * The OS line wrapping status is a PERMANENT feature of the console,
1860 * so it is safe for the formatter to query this during initialization
1861 * and cache the value.
1862 */
1863 static int get_os_line_wrap();
1864
1865 /* turn HTML mode on/off in the underlying OS-level renderer */
1866 virtual void start_html_in_os();
1867 virtual void end_html_in_os();
1868
1869 /* display text directly to the OS renderer */
1870 virtual void print_to_os(const char *txt)
1871 {
1872 /* display the text on the primary OS console */
1873 os_printz(txt);
1874 }
1875
1876 /* flush the underlying OS-level renderer */
1877 virtual void flush_to_os() { os_flush(); }
1878
1879 /*
1880 * set text attributes for subsequent text directly in the underlying
1881 * OS window
1882 */
1883 virtual void set_os_text_attr(int attr)
1884 {
1885 /* if the target isn't in 'plain' mode, set the attributes */
1886 if (!plain_text_target_)
1887 os_set_text_attr(attr);
1888 }
1889
1890 /* set the text color in the underlying OS window */
1891 virtual void set_os_text_color(os_color_t fg, os_color_t bg)
1892 {
1893 /*
1894 * if the target is in 'plain' mode, don't use colors; otherwise,
1895 * ask the console to do the work
1896 */
1897 if (!plain_text_target_)
1898 os_set_text_color(fg, bg);
1899 }
1900
1901 /* set the "body" color in the underlying OS window */
1902 virtual void set_os_body_color(os_color_t color)
1903 {
1904 /* if not in "plain" mode, set the color */
1905 if (!plain_text_target_)
1906 os_set_screen_color(color);
1907 }
1908
1909 };
1910
1911 /* ------------------------------------------------------------------------ */
1912 /*
1913 * Formatter subclass for the status line
1914 */
1915 class CVmFormatterStatline: public CVmFormatterDisp
1916 {
1917 public:
1918 CVmFormatterStatline(class CVmConsole *console)
1919 : CVmFormatterDisp(console)
1920 {
1921 }
1922
1923 /* we never use 'more' mode in the status line */
1924 virtual int formatter_more_mode() const { return FALSE; }
1925
1926 /* HTML mode has no effect on the status line */
1927 virtual void start_html_in_os() { }
1928 virtual void end_html_in_os() { }
1929
1930 /* text colors and attributes are not used in the status line */
1931 virtual void set_os_text_color(os_color_t, os_color_t) { }
1932 virtual void set_os_body_color(os_color_t) { }
1933 virtual void set_os_text_attr(int) { }
1934
1935 /* text displayed in the status line goes directly to the main console */
1936 virtual void print_to_os(const char *txt) { os_printz(txt); }
1937
1938 /* flushing the status line simply flushes the main text stream */
1939 virtual void flush_to_os() { os_flush(); }
1940
1941 /* titles have no effect in the status line */
1942 virtual void set_title_in_os(const char *) { }
1943 };
1944
1945 /* ------------------------------------------------------------------------ */
1946 /*
1947 * Formatter subclass for banner windows
1948 */
1949 class CVmFormatterBanner: public CVmFormatterDisp
1950 {
1951 public:
1952 CVmFormatterBanner(void *banner, class CVmConsole *console,
1953 int win_type, unsigned long style)
1954 : CVmFormatterDisp(console)
1955 {
1956 /* remember my OS banner handle */
1957 banner_ = banner;
1958
1959 /* remember my window type */
1960 win_type_ = win_type;
1961
1962 /* remember the banner style */
1963 style_ = style;
1964 }
1965
1966 /* initialize */
1967 void init_banner(int os_line_wrap, int obey_whitespace, int literal_mode)
1968 {
1969 /* inherit base class initialization */
1970 CVmFormatterDisp::init();
1971
1972 /* remember the OS line wrapping mode in our underlying OS window */
1973 os_line_wrap_ = os_line_wrap;
1974
1975 /* remember the whitespace setting */
1976 obey_whitespace_ = obey_whitespace;
1977
1978 /* remember the literal-mode setting */
1979 literal_mode_ = literal_mode;
1980 }
1981
1982 /* set the window title in the OS layer (does nothing for a banner) */
1983 virtual void set_title_in_os(const char *) { }
1984
1985 /* reset the MORE prompt line counter */
1986 virtual void reset_line_count(int clearing)
1987 {
1988 /*
1989 * To ensure we always keep a line of context when we page-forward
1990 * from a MORE prompt, start the line counter at 2. Note that we
1991 * do this in banner windows, but not in the main window, because -
1992 * for historical reasons - the OS layer tells us the *paging* size
1993 * for the main window, but the *actual* height for banner window.
1994 * Note that we don't want to handle this adjustment in our own
1995 * page length calculation, because doing so would cause the
1996 * *first* MORE prompt (from a cleared window) to show up too
1997 * early.
1998 *
1999 * When we're clearing the screen, reset to zero, since we have no
2000 * context to retain.
2001 */
2002 linecnt_ = (clearing ? 0 : 1);
2003 }
2004
2005 protected:
2006 /*
2007 * Use MORE mode in a banner window if we have the MORE-mode banner
2008 * window style, AND the base display banner would use MORE mode. The
2009 * latter check tests to see if we handle MORE mode at the OS level or
2010 * in the formatter.
2011 */
2012 virtual int formatter_more_mode() const
2013 {
2014 return ((style_ & OS_BANNER_STYLE_MOREMODE) != 0
2015 && CVmFormatterDisp::formatter_more_mode());
2016 }
2017
2018 /* display text directly to the OS renderer */
2019 virtual void print_to_os(const char *txt)
2020 {
2021 /* display the text on our OS banner window */
2022 os_banner_disp(banner_, txt, strlen(txt));
2023 }
2024
2025 /* turn HTML mode on/off in the underlying OS-level renderer */
2026 virtual void start_html_in_os() { os_banner_start_html(banner_); }
2027 virtual void end_html_in_os() { os_banner_end_html(banner_); }
2028
2029 /* set the text color */
2030 virtual void set_os_text_color(os_color_t fg, os_color_t bg)
2031 {
2032 /* set the color in the banner */
2033 os_banner_set_color(banner_, fg, bg);
2034 }
2035
2036 /* set the body color */
2037 virtual void set_os_body_color(os_color_t color)
2038 {
2039 /* set the color in the banner */
2040 os_banner_set_screen_color(banner_, color);
2041 }
2042
2043 /* set text attributes */
2044 virtual void set_os_text_attr(int attr)
2045 {
2046 /* set the attributes on the underlying OS-level banner */
2047 os_banner_set_attr(banner_, attr);
2048 }
2049
2050 /* flush the underlying OS-level renderer */
2051 virtual void flush_to_os()
2052 {
2053 /* flush the OS banner window */
2054 os_banner_flush(banner_);
2055 }
2056
2057 /* determine if the target supports HTML */
2058 virtual int get_init_html_target() const
2059 {
2060 /* if we're a text grid, the underlying window does not use HTML */
2061 if (win_type_ == OS_BANNER_TYPE_TEXTGRID)
2062 return FALSE;
2063
2064 /* defer to the inherited determination in other cases */
2065 return CVmFormatterDisp::get_init_html_target();
2066 }
2067
2068 /* my OS banner handle */
2069 void *banner_;
2070
2071 /* my OS window type (OS_BANNER_TYPE_xxx) */
2072 int win_type_;
2073
2074 /* banner style (a combination of OS_BANNER_STLE_xxx bit flags) */
2075 unsigned long style_;
2076 };
2077
2078 /* ------------------------------------------------------------------------ */
2079 /*
2080 * Formatter subclass for the log file
2081 */
2082 class CVmFormatterLog: public CVmFormatter
2083 {
2084 friend class CVmConsole;
2085 friend class CVmConsoleLog;
2086
2087 public:
2088 CVmFormatterLog(class CVmConsole *console, int width)
2089 : CVmFormatter(console)
2090 {
2091 /* we have no log file yet */
2092 lognf_ = 0;
2093 logfp_ = 0;
2094 logglob_ = 0;
2095
2096 /* remember our width */
2097 width_ = width;
2098 }
2099
2100 void delete_obj(VMG0_)
2101 {
2102 /* close the log file */
2103 close_log_file(vmg0_);
2104
2105 /* do the basic deletion */
2106 CVmFormatter::delete_obj(vmg0_);
2107 }
2108
2109 /* initialize */
2110 void init()
2111 {
2112 /* inherit base class initialization */
2113 CVmFormatter::init();
2114
2115 /* use plain text in the log file stream */
2116 plain_text_target_ = TRUE;
2117
2118 /* we're not a display stream */
2119 is_disp_stream_ = FALSE;
2120
2121 /* we're not an HTML formatter */
2122 html_target_ = FALSE;
2123
2124 /*
2125 * we use our own internal line wrapping, since our underlying
2126 * display layer is simply dumping to a text file
2127 */
2128 os_line_wrap_ = FALSE;
2129
2130 /* no log file yet */
2131 logfp_ = 0;
2132 }
2133
2134 /* don't use MORE mode in a log stream */
2135 int formatter_more_mode() const { return FALSE; }
2136
2137 /* get my width */
2138 virtual int get_buffer_maxcol() const { return width_; }
2139
2140 protected:
2141 /* log streams do not support HTML at the OS level */
2142 virtual void start_html_in_os() { }
2143 virtual void end_html_in_os() { }
2144
2145 /* set the attributes in the underlying stream */
2146 virtual void set_os_text_attr(int)
2147 {
2148 /* log streams are plain text - they don't support attributes */
2149 }
2150
2151 /* set the color in the underlying stream */
2152 virtual void set_os_text_color(os_color_t, os_color_t)
2153 {
2154 /* log streams are plain text - they don't support colors */
2155 }
2156
2157 /* set the body color */
2158 virtual void set_os_body_color(os_color_t)
2159 {
2160 /* log streams are plain text - they don't support colors */
2161 }
2162
2163 /* display text to the underlying OS device */
2164 virtual void print_to_os(const char *txt)
2165 {
2166 /* display the text through the log file */
2167 if (logfp_ != 0)
2168 {
2169 os_fprintz(logfp_, txt);
2170 osfflush(logfp_);
2171 }
2172 }
2173
2174 /* flush the underlying OS-level rendere */
2175 virtual void flush_to_os() { }
2176
2177 /* set the window title in the OS layer - no effect for log streams */
2178 virtual void set_title_in_os(const char *) { }
2179
2180 /* open a log file */
2181 int open_log_file(VMG_ const char *fname);
2182 int open_log_file(VMG_ const vm_val_t *filespec,
2183 const struct vm_rcdesc *rc);
2184 int open_log_file(VMG_ class CVmNetFile *netfile);
2185
2186 /* set the log file to a file previously opened */
2187 int set_log_file(VMG_ CVmNetFile *nf, osfildef *fp);
2188
2189 /* close the log file */
2190 int close_log_file(VMG0_);
2191
2192 /*
2193 * Process a <nolog> tag. Since we're a log stream, we hide the
2194 * contents of this tag.
2195 */
2196 virtual void process_nolog_tag(int is_end_tag)
2197 {
2198 /* turn hiding on or off as appropriate */
2199 if (is_end_tag)
2200 --html_in_ignore_;
2201 else
2202 ++html_in_ignore_;
2203 }
2204
2205 /*
2206 * Process a <log> tag. Since we're a log stream, we show the contents
2207 * of this tag, so we can simply parse and ignore it.
2208 */
2209 virtual void process_log_tag(int /*is_end_tag*/)
2210 {
2211 }
2212
2213 /* my log file handle and network file descriptor */
2214 osfildef *logfp_;
2215 class CVmNetFile *lognf_;
2216 struct vm_globalvar_t *logglob_;
2217
2218 /* the maximum width to use for our lines */
2219 int width_;
2220 };
2221
2222
2223 #endif /* VMCONSOL_H */