Debian Codegraph @

terps/tads/tads2/output.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/Output.c,v 1.2 1999/05/17 02:52:12 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1987, 1988 by Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 output - TADS Interpreter and Compiler formatted Output routines
15 Function
16 Provides formatted output support. Text that is sent through outformat()
17 is displayed with word-wrap so that words fill the line but are not broken
18 across lines. Other routines allow flushing the output buffer, displaying
19 blank lines, and logging output to a file as well as to the display.
20 Notes
21 We use the global variables G_os_pagelength and G_os_linewidth, defined
22 in the OS code, to determine the size of the display area and to determine
23 the number of lines to display between page pauses (MORE prompts). Line
24 breaking and MORE prompting can be handled by the OS code if desired;
25 the USE_MORE macro controls this setting.
26 Returns
27 None
28 Modified
29 04/05/92 MJRoberts - TADS 2.0 changes
30 03/29/92 MJRoberts - fix unfound formatstring handling
31 08/01/91 MJRoberts - no more mode when debugger is running
32 07/18/91 MJRoberts - improve t_outline [more] behavior
33 07/01/91 MJRoberts - Mac porting changes
34 06/05/91 MJRoberts - add format string support
35 03/27/91 MJRoberts - debugger enhancements
36 03/10/91 MJRoberts - integrate John's qa-scripter mods
37 04/24/90 MJRoberts - add \^ (equivalent to calling "caps()" intrinsic)
38 04/16/89 MJRoberts - add outcformat for compressed strings
39 10/30/88 MJRoberts - add outhide() and outshow() functions
40 10/29/88 MJRoberts - break lines on hyphens
41 12/22/87 MJRoberts - created
42 */
43
44 #include <stdio.h>
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <stdarg.h>
49
50 #include "os.h"
51 #include "std.h"
52 #include "run.h"
53 #include "voc.h"
54 #include "tio.h"
55 #include "mcm.h"
56 #include "dbg.h"
57 #include "cmap.h"
58
59 /*
60 * use our own isxxx - anything outside the US ASCII range is not reliably
61 * classifiable by the normal C isxxx routines
62 */
63 #define outissp(c) (((uchar)(c)) <= 127 && isspace((uchar)(c)))
64 #define outisal(c) (((uchar)(c)) <= 127 && isalpha((uchar)(c)))
65 #define outisdg(c) (((uchar)(c)) <= 127 && isdigit((uchar)(c)))
66 #define outisup(c) (((uchar)(c)) <= 127 && isupper((uchar)(c)))
67 #define outislo(c) (((uchar)(c)) <= 127 && islower((uchar)(c)))
68
69
70 /*
71 * Turn on formatter-level MORE mode, EXCEPT under any of the following
72 * conditions:
73 *
74 * - this is a MAC OS port
75 *. - this is an HTML TADS interpreter
76 *. - USE_OS_LINEWRAP is defined
77 *
78 * Formatter-level MORE mode and formatter-level line wrapping go together;
79 * you can't have one without the other. So, if USE_OS_LINEWRAP is
80 * defined, we must also use OS-level MORE mode, which means we don't want
81 * formatter-level MORE mode.
82 *
83 * For historical reasons, we check specifically for MAC_OS. This was the
84 * first platform for which OS-level MORE mode and OS-level line wrapping
85 * were invented; at the time, we foolishly failed to anticipate that more
86 * platforms might eventually come along with the same needs, so we coded a
87 * test for MAC_OS rather than some more abstract marker. For
88 * compatibility, we retain this specific test.
89 *
90 * USE_OS_LINEWRAP is intended as the more abstract marker we should
91 * originally have used. A port should #define USE_OS_LINEWRAP in its
92 * system-specific os_xxx.h header to turn on OS-level line wrapping and
93 * OS-level MORE mode. Ports should avoid adding new #ifndef tests for
94 * specific platforms here; we've only retained the MAC_OS test because we
95 * don't want to break the existing MAC_OS port.
96 */
97 #ifndef MAC_OS
98 # ifndef USE_HTML
99 # ifndef USE_OS_LINEWRAP
100 # define USE_MORE /* activate formatter-level more-mode */
101 # endif /* USE_OS_LINEWRAP */
102 # endif /* USE_HTML */
103 #endif /* MAC_OS */
104
105 /*
106 * In HTML mode, don't use MORE mode. Note that we explicitly turn MORE
107 * mode OFF, even though we won't have turned it on above, because it might
108 * have been turned on by an os_xxx.h header. This is here for historical
109 * reasons; in particular, some of the HTML interpreter builds include
110 * headers that were originally written for the normal builds for those
111 * same platforms, and those original headers explicitly #define USE_MORE
112 * somewhere. So, to be absolutely sure we get it right here, we have to
113 * explicitly turn off USE_MORE when compiling for HTML mode.
114 */
115 #ifdef USE_HTML
116 # ifdef USE_MORE
117 # undef USE_MORE
118 # endif
119 #endif
120
121 /*
122 * QSPACE is the special character for a quoted space (internally, the
123 * sequence "\ " (backslash-space) is converted to QSPACE). It must not
124 * be any printable character. The value here may need to be changed in
125 * the extremely unlikely event that TADS is ever ported to an EBCDIC
126 * machine.
127 */
128 #define QSPACE 26
129
130 /*
131 * QTAB is a special hard tab character indicator. We use this when we
132 * need to generate a hard tab to send to the underlying output layer
133 * (in particular, we use this to send hard tabs to the HTML formatter
134 * when we're in HTML mode).
135 */
136 #define QTAB 25
137
138
139 /* maximum width of the display */
140 #define MAXWIDTH OS_MAXWIDTH
141
142
143 /* ------------------------------------------------------------------------ */
144 /*
145 * Globals and statics. These should really be moved into a context
146 * structure, so that the output formatter subsystem could be shared
147 * among multiple clients. For now, there's no practical problem using
148 * statics, because we only need a single output subsystem at one time.
149 */
150
151 /* current script (command input) file */
152 extern osfildef *scrfp;
153
154 /*
155 * This should be TRUE if the output should have two spaces after a
156 * period (or other such punctuation. It should generally be TRUE for
157 * fixed-width fonts, and FALSE for proportional fonts.
158 */
159 static int doublespace = 1;
160
161 /*
162 * Log file handle and name. If we're copying output to a log file,
163 * these will tell us about the file.
164 */
165 osfildef *logfp;
166 static char logfname[OSFNMAX];
167
168 /* flag indicating whether output has occurred since last check */
169 static uchar outcnt;
170
171 /* flag indicating whether hidden output has occurred */
172 static uchar hidout;
173
174 /* flag indicating whether to show (TRUE) or hide (FALSE) output */
175 static uchar outflag;
176
177 /* flag indicating whether output is hidden for debugging purposes */
178 int dbghid;
179
180 /*
181 * Current recursion level in formatter invocation
182 */
183 static int G_recurse = 0;
184
185 /* active stream in current recursion level */
186 static struct out_stream_info *G_cur_stream;
187
188 /* watchpoint mode flag */
189 static uchar outwxflag;
190
191 /*
192 * User filter function. When this function is set, we'll invoke this
193 * function for each string that's displayed through the output
194 * formatter.
195 */
196 static objnum G_user_filter = MCMONINV;
197
198
199 /* ------------------------------------------------------------------------ */
200 /*
201 * Hack to run with TADS 2.0 with minimal reworking. Rather than using
202 * an allocated output layer context, store our subsystem context
203 * information in some statics. This is less clean than using a real
204 * context, but doesn't create any practical problems as we don't need
205 * to share the output formatter subsystem among multiple simultaneous
206 * callers.
207 */
208 static runcxdef *runctx; /* execution context */
209 static uchar *fmsbase; /* format string area base */
210 static uchar *fmstop; /* format string area top */
211 static objnum cmdActor; /* current actor */
212
213 /* forward declarations of static functions */
214 static void outstring_stream(struct out_stream_info *stream, char *s);
215 static void outchar_noxlat_stream(struct out_stream_info *stream, char c);
216 static char out_parse_entity(char *outbuf, size_t outbuf_size,
217 char **sp, size_t *slenp);
218
219
220 /* ------------------------------------------------------------------------ */
221 /*
222 * HTML lexical analysis mode
223 */
224 #define HTML_MODE_NORMAL 0 /* normal text, not in a tag */
225 #define HTML_MODE_TAG 1 /* parsing inside a tag */
226 #define HTML_MODE_SQUOTE 2 /* in a single-quoted string in a tag */
227 #define HTML_MODE_DQUOTE 3 /* in a double-quoted string in a tag */
228
229 /*
230 * HTML parsing mode flag for <BR> tags. We defer these until we've
231 * read the full tag in order to obey an HEIGHT attribute we find. When
232 * we encounter a <BR>, we figure out whether we think we'll need a
233 * flush or a blank line; if we find a HEIGHT attribute, we may change
234 * this opinion.
235 */
236 #define HTML_DEFER_BR_NONE 0 /* no pending <BR> */
237 #define HTML_DEFER_BR_FLUSH 1 /* only need an outflush() */
238 #define HTML_DEFER_BR_BLANK 2 /* need an outblank() */
239
240 /*
241 * If we're compiling for an HTML-enabled underlying output subsystem,
242 * we want to call the underlying OS layer when switching in and out of
243 * HTML mode. If the underlying system doesn't process HTML, we don't
244 * need to let it know anything about HTML mode.
245 */
246 #ifdef USE_HTML
247 # define out_start_html(stream) os_start_html()
248 # define out_end_html(stream) os_end_html()
249 #else
250 # define out_start_html(stream)
251 # define out_end_html(stream)
252 #endif
253
254
255 /* ------------------------------------------------------------------------ */
256 /*
257 * Output formatter stream state structure. This structure encapsulates
258 * the state of an individual output stream.
259 */
260 typedef struct out_stream_info out_stream_info;
261 struct out_stream_info
262 {
263 /* low-level display routine (va_list version) */
264 void (*do_print)(out_stream_info *stream, const char *str);
265
266 /* current line position and output column */
267 uchar linepos;
268 uchar linecol;
269
270 /* number of lines on the screen (since last MORE prompt) */
271 int linecnt;
272
273 /* output buffer */
274 char linebuf[MAXWIDTH];
275
276 /*
277 * attribute buffer - we keep one attribute entry for each character in
278 * the line buffer
279 */
280 int attrbuf[MAXWIDTH];
281
282 /* current attribute for text we're buffering into linebuf */
283 int cur_attr;
284
285 /* last attribute we wrote to the osifc layer */
286 int os_attr;
287
288 /* CAPS mode - next character output is converted to upper-case */
289 uchar capsflag;
290
291 /* NOCAPS mode - next character output is converted to lower-case */
292 uchar nocapsflag;
293
294 /* ALLCAPS mode - all characters output are converted to upper-case */
295 uchar allcapsflag;
296
297 /* capture information */
298 mcmcxdef *capture_ctx; /* memory context to use for capturing */
299 mcmon capture_obj; /* object holding captured output */
300 uint capture_ofs; /* write offset in capture object */
301 int capturing; /* true -> we are capturing output */
302
303 /* "preview" state for line flushing */
304 int preview;
305
306 /* flag indicating that we just flushed a new line */
307 int just_did_nl;
308
309 /* this output stream uses "MORE" mode */
310 int use_more_mode;
311
312 /*
313 * This output stream uses OS-level line wrapping - if this is set,
314 * the output formatter will not insert a newline at the end of a
315 * line that it's flushing for word wrapping, but will instead let
316 * the underlying OS display layer handle the wrapping.
317 */
318 int os_line_wrap;
319
320 /*
321 * Flag indicating that the underlying output system wants to
322 * receive its output as HTML.
323 *
324 * If this is true, we'll pass through HTML to the underlying output
325 * system, and in addition generate HTML sequences for certain
326 * TADS-native escapes (for example, we'll convert the "\n" sequence
327 * to a <BR> sequence).
328 *
329 * If this is false, we'll do just the opposite: we'll remove HTML
330 * from the output stream and convert it into normal text sequences.
331 */
332 int html_target;
333
334 /*
335 * Flag indicating that the target uses plain text. If this flag is
336 * set, we won't add the OS escape codes for highlighted characters.
337 */
338 int plain_text_target;
339
340 /*
341 * Flag indicating that the caller is displaying HTML. We always
342 * start off in text mode; the client can switch to HTML mode by
343 * displaying a special escape sequence, and can switch back to text
344 * mode by displaying a separate special escape sequence.
345 */
346 int html_mode;
347
348 /* current lexical analysis mode */
349 unsigned int html_mode_flag;
350
351 /* <BR> defer mode */
352 unsigned int html_defer_br;
353
354 /*
355 * HTML "ignore" mode - we suppress all output when parsing the
356 * contents of a <TITLE> or <ABOUTBOX> tag
357 */
358 int html_in_ignore;
359
360 /*
361 * HTML <TITLE> mode - when we're in this mode, we're gathering the
362 * title (i.e., we're inside a <TITLE> tag's contents). We'll copy
363 * characters to the title buffer rather than the normal output
364 * buffer, and then call os_set_title() when we reach the </TITLE>
365 * tag.
366 */
367 int html_in_title;
368
369 /* buffer for the title */
370 char html_title_buf[256];
371
372 /* pointer to next available character in title buffer */
373 char *html_title_ptr;
374
375 /* quoting level */
376 int html_quote_level;
377
378 /* PRE nesting level */
379 int html_pre_level;
380
381 /*
382 * Parsing mode flag for ALT attributes. If we're parsing a tag
383 * that allows ALT, such as IMG or SOUND, we'll set this flag, then
384 * insert the ALT text if we encounter it during parsing.
385 */
386 int html_allow_alt;
387 };
388
389 /*
390 * Default output converter. This is the output converter for the
391 * standard display. Functions in the public interface that do not
392 * specify an output converter will use this converter by default.
393 */
394 static out_stream_info G_std_disp;
395
396 /*
397 * Log file converter. This is the output converter for a log file.
398 * Whenever we open a log file, we'll initialize this converter; as we
399 * display text to the main display, we'll also copy it to the log file.
400 *
401 * We maintain an entire separate conversion context for the log file,
402 * so that we can perform a different set of conversions on it. We may
403 * want, for example, to pass HTML text through to the OS display
404 * subsystem (this is the case for the HTML-enabled interpreter), but
405 * we'll still want to convert log file output to text. By keeping a
406 * separate display context for the log file, we can format output to
407 * the log file using an entirely different style than we do for the
408 * display.
409 */
410 static out_stream_info G_log_disp;
411
412
413 /* ------------------------------------------------------------------------ */
414 /*
415 * low-level output handlers for the standard display and log file
416 */
417
418 /* standard display printer */
419 static void do_std_print(out_stream_info *stream, const char *str)
420 {
421 VARUSED(stream);
422
423 /* display the text through the OS layer */
424 os_printz(str);
425 }
426
427 /* log file printer */
428 static void do_log_print(out_stream_info *stream, const char *str)
429 {
430 VARUSED(stream);
431
432 /* display to the log file */
433 if (logfp != 0 && G_os_moremode)
434 {
435 os_fprintz(logfp, str);
436 osfflush(logfp);
437 }
438 }
439
440
441 /* ------------------------------------------------------------------------ */
442 /*
443 * initialize a generic output formatter state structure
444 */
445 static void out_state_init(out_stream_info *stream)
446 {
447 /* start out at the first column */
448 stream->linepos = 0;
449 stream->linecol = 0;
450 stream->linebuf[0] = '\0';
451
452 /* set normal text attributes */
453 stream->cur_attr = 0;
454 stream->os_attr = 0;
455
456 /* start out at the first line */
457 stream->linecnt = 0;
458
459 /* we're not in either "caps", "nocaps", or "allcaps" mode yet */
460 stream->capsflag = stream->nocapsflag = stream->allcapsflag = FALSE;
461
462 /* we're not capturing yet */
463 stream->capturing = FALSE;
464 stream->capture_obj = MCMONINV;
465
466 /* we aren't previewing a line yet */
467 stream->preview = 0;
468
469 /* we haven't flushed a new line yet */
470 stream->just_did_nl = FALSE;
471
472 /* presume this stream does not use "MORE" mode */
473 stream->use_more_mode = FALSE;
474
475 /* presume this stream uses formatter-level line wrapping */
476 stream->os_line_wrap = FALSE;
477
478 /* assume that the underlying system is not HTML-enabled */
479 stream->html_target = FALSE;
480
481 /* presume this target accepts OS highlighting sequences */
482 stream->plain_text_target = FALSE;
483
484 /* start out in text mode */
485 stream->html_mode = FALSE;
486
487 /* start out in "normal" lexical state */
488 stream->html_mode_flag = HTML_MODE_NORMAL;
489
490 /* not in an ignored tag yet */
491 stream->html_in_ignore = FALSE;
492
493 /* not in title mode yet */
494 stream->html_in_title = FALSE;
495
496 /* not yet deferring line breaks */
497 stream->html_defer_br = HTML_DEFER_BR_NONE;
498
499 /* not yet in quotes */
500 stream->html_quote_level = 0;
501
502 /* not yet in a PRE block */
503 stream->html_pre_level = 0;
504
505 /* not in an ALT tag yet */
506 stream->html_allow_alt = FALSE;
507 }
508
509
510 /* ------------------------------------------------------------------------ */
511 /*
512 * initialize a standard display stream
513 */
514 static void out_init_std(out_stream_info *stream)
515 {
516 /* there's no user output filter function yet */
517 out_set_filter(MCMONINV);
518
519 /* initialize the basic stream state */
520 out_state_init(stream);
521
522 /* set up the low-level output routine */
523 G_std_disp.do_print = do_std_print;
524
525 #ifdef USE_MORE
526 /*
527 * We're compiled for MORE mode, and we're not compiling for an
528 * underlying HTML formatting layer, so use MORE mode for the
529 * standard display stream.
530 */
531 stream->use_more_mode = TRUE;
532 #else
533 /*
534 * We're compiled for OS-layer (or HTML-layer) MORE handling. For
535 * this case, use OS-layer (or HTML-layer) line wrapping as well.
536 */
537 stream->os_line_wrap = TRUE;
538 #endif
539
540 #ifdef USE_HTML
541 /*
542 * if we're compiled for HTML mode, set the standard output stream
543 * so that it knows it has an HTML target - this will ensure that
544 * HTML tags are passed through to the underlying stream, and that
545 * we generate HTML equivalents for our own control sequences
546 */
547 stream->html_target = TRUE;
548 #endif
549 }
550
551 /*
552 * initialize a standard log file stream
553 */
554 static void out_init_log(out_stream_info *stream)
555 {
556 /* initialize the basic stream state */
557 out_state_init(stream);
558
559 /* set up the low-level output routine */
560 stream->do_print = do_log_print;
561
562 /* use plain text in the log file stream */
563 stream->plain_text_target = TRUE;
564 }
565
566
567
568 /* ------------------------------------------------------------------------ */
569 /*
570 * table of '&' character name sequences
571 */
572 struct amp_tbl_t
573 {
574 /* entity name */
575 const char *cname;
576
577 /* HTML Unicode character value */
578 uint html_cval;
579
580 /* native character set expansion */
581 char *expan;
582 };
583
584 /*
585 * HTML entity mapping table. When we're in non-HTML mode, we keep our
586 * own expansion table so that we can map HTML entity names into the
587 * local character set.
588 *
589 * The entries in this table must be in sorted order (by HTML entity
590 * name), because we use a binary search to find an entity name in the
591 * table.
592 */
593 static struct amp_tbl_t amp_tbl[] =
594 {
595 { "AElig", 198, 0 },
596 { "Aacute", 193, 0 },
597 { "Abreve", 258, 0 },
598 { "Acirc", 194, 0 },
599 { "Agrave", 192, 0 },
600 { "Alpha", 913, 0 },
601 { "Aogon", 260, 0 },
602 { "Aring", 197, 0 },
603 { "Atilde", 195, 0 },
604 { "Auml", 196, 0 },
605 { "Beta", 914, 0 },
606 { "Cacute", 262, 0 },
607 { "Ccaron", 268, 0 },
608 { "Ccedil", 199, 0 },
609 { "Chi", 935, 0 },
610 { "Dagger", 8225, 0 },
611 { "Dcaron", 270, 0 },
612 { "Delta", 916, 0 },
613 { "Dstrok", 272, 0 },
614 { "ETH", 208, 0 },
615 { "Eacute", 201, 0 },
616 { "Ecaron", 282, 0 },
617 { "Ecirc", 202, 0 },
618 { "Egrave", 200, 0 },
619 { "Eogon", 280, 0 },
620 { "Epsilon", 917, 0 },
621 { "Eta", 919, 0 },
622 { "Euml", 203, 0 },
623 { "Gamma", 915, 0 },
624 { "Iacute", 205, 0 },
625 { "Icirc", 206, 0 },
626 { "Igrave", 204, 0 },
627 { "Iota", 921, 0 },
628 { "Iuml", 207, 0 },
629 { "Kappa", 922, 0 },
630 { "Lacute", 313, 0 },
631 { "Lambda", 923, 0 },
632 { "Lcaron", 317, 0 },
633 { "Lstrok", 321, 0 },
634 { "Mu", 924, 0 },
635 { "Nacute", 323, 0 },
636 { "Ncaron", 327, 0 },
637 { "Ntilde", 209, 0 },
638 { "Nu", 925, 0 },
639 { "OElig", 338, 0 },
640 { "Oacute", 211, 0 },
641 { "Ocirc", 212, 0 },
642 { "Odblac", 336, 0 },
643 { "Ograve", 210, 0 },
644 { "Omega", 937, 0 },
645 { "Omicron", 927, 0 },
646 { "Oslash", 216, 0 },
647 { "Otilde", 213, 0 },
648 { "Ouml", 214, 0 },
649 { "Phi", 934, 0 },
650 { "Pi", 928, 0 },
651 { "Prime", 8243, 0 },
652 { "Psi", 936, 0 },
653 { "Racute", 340, 0 },
654 { "Rcaron", 344, 0 },
655 { "Rho", 929, 0 },
656 { "Sacute", 346, 0 },
657 { "Scaron", 352, 0 },
658 { "Scedil", 350, 0 },
659 { "Sigma", 931, 0 },
660 { "THORN", 222, 0 },
661 { "Tau", 932, 0 },
662 { "Tcaron", 356, 0 },
663 { "Tcedil", 354, 0 },
664 { "Theta", 920, 0 },
665 { "Uacute", 218, 0 },
666 { "Ucirc", 219, 0 },
667 { "Udblac", 368, 0 },
668 { "Ugrave", 217, 0 },
669 { "Upsilon", 933, 0 },
670 { "Uring", 366, 0 },
671 { "Uuml", 220, 0 },
672 { "Xi", 926, 0 },
673 { "Yacute", 221, 0 },
674 { "Yuml", 376, 0 },
675 { "Zacute", 377, 0 },
676 { "Zcaron", 381, 0 },
677 { "Zdot", 379, 0 },
678 { "Zeta", 918, 0 },
679 { "aacute", 225, 0 },
680 { "abreve", 259, 0 },
681 { "acirc", 226, 0 },
682 { "acute", 180, 0 },
683 { "aelig", 230, 0 },
684 { "agrave", 224, 0 },
685 { "alefsym", 8501, 0 },
686 { "alpha", 945, 0 },
687 { "amp", '&', 0 },
688 { "and", 8743, 0 },
689 { "ang", 8736, 0 },
690 { "aogon", 261, 0 },
691 { "aring", 229, 0 },
692 { "asymp", 8776, 0 },
693 { "atilde", 227, 0 },
694 { "auml", 228, 0 },
695 { "bdquo", 8222, 0 },
696 { "beta", 946, 0 },
697 { "breve", 728, 0 },
698 { "brvbar", 166, 0 },
699 { "bull", 8226, 0 },
700 { "cacute", 263, 0 },
701 { "cap", 8745, 0 },
702 { "caron", 711, 0 },
703 { "ccaron", 269, 0 },
704 { "ccedil", 231, 0 },
705 { "cedil", 184, 0 },
706 { "cent", 162, 0 },
707 { "chi", 967, 0 },
708 { "circ", 710, 0 },
709 { "clubs", 9827, 0 },
710 { "cong", 8773, 0 },
711 { "copy", 169, 0 },
712 { "crarr", 8629, 0 },
713 { "cup", 8746, 0 },
714 { "curren", 164, 0 },
715 { "dArr", 8659, 0 },
716 { "dagger", 8224, 0 },
717 { "darr", 8595, 0 },
718 { "dblac", 733, 0 },
719 { "dcaron", 271, 0 },
720 { "deg", 176, 0 },
721 { "delta", 948, 0 },
722 { "diams", 9830, 0 },
723 { "divide", 247, 0 },
724 { "dot", 729, 0 },
725 { "dstrok", 273, 0 },
726 { "eacute", 233, 0 },
727 { "ecaron", 283, 0 },
728 { "ecirc", 234, 0 },
729 { "egrave", 232, 0 },
730 { "emdash", 8212, 0 },
731 { "empty", 8709, 0 },
732 { "endash", 8211, 0 },
733 { "eogon", 281, 0 },
734 { "epsilon", 949, 0 },
735 { "equiv", 8801, 0 },
736 { "eta", 951, 0 },
737 { "eth", 240, 0 },
738 { "euml", 235, 0 },
739 { "exist", 8707, 0 },
740 { "fnof", 402, 0 },
741 { "forall", 8704, 0 },
742 { "frac12", 189, 0 },
743 { "frac14", 188, 0 },
744 { "frac34", 190, 0 },
745 { "frasl", 8260, 0 },
746 { "gamma", 947, 0 },
747 { "ge", 8805, 0 },
748 { "gt", '>', 0 },
749 { "hArr", 8660, 0 },
750 { "harr", 8596, 0 },
751 { "hearts", 9829, 0 },
752 { "hellip", 8230, 0 },
753 { "iacute", 237, 0 },
754 { "icirc", 238, 0 },
755 { "iexcl", 161, 0 },
756 { "igrave", 236, 0 },
757 { "image", 8465, 0 },
758 { "infin", 8734, 0 },
759 { "int", 8747, 0 },
760 { "iota", 953, 0 },
761 { "iquest", 191, 0 },
762 { "isin", 8712, 0 },
763 { "iuml", 239, 0 },
764 { "kappa", 954, 0 },
765 { "lArr", 8656, 0 },
766 { "lacute", 314, 0 },
767 { "lambda", 955, 0 },
768 { "lang", 9001, 0 },
769 { "laquo", 171, 0 },
770 { "larr", 8592, 0 },
771 { "lcaron", 318, 0 },
772 { "lceil", 8968, 0 },
773 { "ldq", 8220, 0 },
774 { "ldquo", 8220, 0 },
775 { "le", 8804, 0 },
776 { "lfloor", 8970, 0 },
777 { "lowast", 8727, 0 },
778 { "loz", 9674, 0 },
779 { "lsaquo", 8249, 0 },
780 { "lsq", 8216, 0 },
781 { "lsquo", 8216, 0 },
782 { "lstrok", 322, 0 },
783 { "lt", '<', 0 },
784 { "macr", 175, 0 },
785 { "mdash", 8212, 0 },
786 { "micro", 181, 0 },
787 { "middot", 183, 0 },
788 { "minus", 8722, 0 },
789 { "mu", 956, 0 },
790 { "nabla", 8711, 0 },
791 { "nacute", 324, 0 },
792 { "nbsp", QSPACE, 0 },
793 { "ncaron", 328, 0 },
794 { "ndash", 8211, 0 },
795 { "ne", 8800, 0 },
796 { "ni", 8715, 0 },
797 { "not", 172, 0 },
798 { "notin", 8713, 0 },
799 { "nsub", 8836, 0 },
800 { "ntilde", 241, 0 },
801 { "nu", 957, 0 },
802 { "oacute", 243, 0 },
803 { "ocirc", 244, 0 },
804 { "odblac", 337, 0 },
805 { "oelig", 339, 0 },
806 { "ogon", 731, 0 },
807 { "ograve", 242, 0 },
808 { "oline", 8254, 0 },
809 { "omega", 969, 0 },
810 { "omicron", 959, 0 },
811 { "oplus", 8853, 0 },
812 { "or", 8744, 0 },
813 { "ordf", 170, 0 },
814 { "ordm", 186, 0 },
815 { "oslash", 248, 0 },
816 { "otilde", 245, 0 },
817 { "otimes", 8855, 0 },
818 { "ouml", 246, 0 },
819 { "para", 182, 0 },
820 { "part", 8706, 0 },
821 { "permil", 8240, 0 },
822 { "perp", 8869, 0 },
823 { "phi", 966, 0 },
824 { "pi", 960, 0 },
825 { "piv", 982, 0 },
826 { "plusmn", 177, 0 },
827 { "pound", 163, 0 },
828 { "prime", 8242, 0 },
829 { "prod", 8719, 0 },
830 { "prop", 8733, 0 },
831 { "psi", 968, 0 },
832 { "quot", '"', 0 },
833 { "rArr", 8658, 0 },
834 { "racute", 341, 0 },
835 { "radic", 8730, 0 },
836 { "rang", 9002, 0 },
837 { "raquo", 187, 0 },
838 { "rarr", 8594, 0 },
839 { "rcaron", 345, 0 },
840 { "rceil", 8969, 0 },
841 { "rdq", 8221, 0 },
842 { "rdquo", 8221, 0 },
843 { "real", 8476, 0 },
844 { "reg", 174, 0 },
845 { "rfloor", 8971, 0 },
846 { "rho", 961, 0 },
847 { "rsaquo", 8250, 0 },
848 { "rsq", 8217, 0 },
849 { "rsquo", 8217, 0 },
850 { "sacute", 347, 0 },
851 { "sbquo", 8218, 0 },
852 { "scaron", 353, 0 },
853 { "scedil", 351, 0 },
854 { "sdot", 8901, 0 },
855 { "sect", 167, 0 },
856 { "shy", 173, 0 },
857 { "sigma", 963, 0 },
858 { "sigmaf", 962, 0 },
859 { "sim", 8764, 0 },
860 { "spades", 9824, 0 },
861 { "sub", 8834, 0 },
862 { "sube", 8838, 0 },
863 { "sum", 8721, 0 },
864 { "sup", 8835, 0 },
865 { "sup1", 185, 0 },
866 { "sup2", 178, 0 },
867 { "sup3", 179, 0 },
868 { "supe", 8839, 0 },
869 { "szlig", 223, 0 },
870 { "tau", 964, 0 },
871 { "tcaron", 357, 0 },
872 { "tcedil", 355, 0 },
873 { "there4", 8756, 0 },
874 { "theta", 952, 0 },
875 { "thetasym", 977, 0 },
876 { "thorn", 254, 0 },
877 { "thorn", 254, 0 },
878 { "tilde", 732, 0 },
879 { "times", 215, 0 },
880 { "trade", 8482, 0 },
881 { "uArr", 8657, 0 },
882 { "uacute", 250, 0 },
883 { "uarr", 8593, 0 },
884 { "ucirc", 251, 0 },
885 { "udblac", 369, 0 },
886 { "ugrave", 249, 0 },
887 { "uml", 168, 0 },
888 { "upsih", 978, 0 },
889 { "upsilon", 965, 0 },
890 { "uring", 367, 0 },
891 { "uuml", 252, 0 },
892 { "weierp", 8472, 0 },
893 { "xi", 958, 0 },
894 { "yacute", 253, 0 },
895 { "yen", 165, 0 },
896 { "yuml", 255, 0 },
897 { "zacute", 378, 0 },
898 { "zcaron", 382, 0 },
899 { "zdot", 380, 0 },
900 { "zeta", 950, 0 }
901 };
902
903
904 /* ------------------------------------------------------------------------ */
905 /*
906 * turn on CAPS mode for a stream
907 */
908 static void outcaps_stream(out_stream_info *stream)
909 {
910 /* turn on CAPS mode */
911 stream->capsflag = TRUE;
912
913 /* turn off NOCAPS and ALLCAPS mode */
914 stream->nocapsflag = FALSE;
915 stream->allcapsflag = FALSE;
916 }
917
918 /*
919 * turn on NOCAPS mode for a stream
920 */
921 static void outnocaps_stream(out_stream_info *stream)
922 {
923 /* turn on NOCAPS mode */
924 stream->nocapsflag = TRUE;
925
926 /* turn off CAPS and ALLCAPS mode */
927 stream->capsflag = FALSE;
928 stream->allcapsflag = FALSE;
929 }
930
931 /*
932 * turn on or off ALLCAPS mode for a stream
933 */
934 static void outallcaps_stream(out_stream_info *stream, int all_caps)
935 {
936 /* set the ALLCAPS flag */
937 stream->allcapsflag = all_caps;
938
939 /* clear the CAPS and NOCAPS flags */
940 stream->capsflag = FALSE;
941 stream->nocapsflag = FALSE;
942 }
943
944 /* ------------------------------------------------------------------------ */
945 /*
946 * write a string to a stream
947 */
948 static void stream_print(out_stream_info *stream, char *str)
949 {
950 /* call the stream's do_print method */
951 (*stream->do_print)(stream, str);
952 }
953
954 /*
955 * Write out a line
956 */
957 static void t_outline(out_stream_info *stream, int nl,
958 const char *txt, const int *attr)
959 {
960 extern int scrquiet;
961
962 /*
963 * Check the "script quiet" mode - this indicates that we're reading
964 * a script and not echoing output to the display. If this mode is
965 * on, and we're writing to the display, suppress this write. If
966 * the mode is off, or we're writing to another stream (such as the
967 * log file), show the output as normal.
968 */
969 if (!scrquiet || stream != &G_std_disp)
970 {
971 size_t i;
972 char buf[MAXWIDTH];
973 char *dst;
974
975 /*
976 * Check to see if we've reached the end of the screen, and if
977 * so run the MORE prompt. Note that we don't make this check
978 * at all if USE_MORE is undefined, since this means that the OS
979 * layer code is taking responsibility for pagination issues.
980 * We also don't display a MORE prompt when reading from a
981 * script file.
982 *
983 * Note that we suppress the MORE prompt if nl == 0, since this
984 * is used to flush a partial line of text without starting a
985 * new line (for example, when displaying a prompt where the
986 * input will appear on the same line following the prompt).
987 *
988 * Skip the MORE prompt if this stream doesn't use it.
989 */
990 if (stream->use_more_mode
991 && scrfp == 0
992 && G_os_moremode
993 && nl != 0 && nl != 4
994 && stream->linecnt++ >= G_os_pagelength)
995 {
996 /* display the MORE prompt */
997 out_more_prompt();
998 }
999
1000 /*
1001 * Display the text. Run through the text in pieces; each time the
1002 * attributes change, set attributes at the osifc level.
1003 */
1004 for (i = 0, dst = buf ; txt[i] != '\0' ; ++i)
1005 {
1006 /* if the attribute is changing, notify osifc */
1007 if (attr != 0 && attr[i] != stream->os_attr)
1008 {
1009 /* flush the preceding text */
1010 if (dst != buf)
1011 {
1012 *dst = '\0';
1013 stream_print(stream, buf);
1014 }
1015
1016 /* set the new attribute */
1017 os_set_text_attr(attr[i]);
1018
1019 /* remember this as the last OS attribute */
1020 stream->os_attr = attr[i];
1021
1022 /* start with a fresh buffer */
1023 dst = buf;
1024 }
1025
1026 /* buffer this character */
1027 *dst++ = txt[i];
1028 }
1029
1030 /* flush the last chunk of text */
1031 if (dst != buf)
1032 {
1033 *dst = '\0';
1034 stream_print(stream, buf);
1035 }
1036 }
1037 }
1038
1039 /* ------------------------------------------------------------------------ */
1040 /*
1041 * Flush the current line to the display. The 'nl' argument specifies
1042 * what kind of flushing to do:
1043 *
1044 * 0: flush the current line but do not start a new line; more text will
1045 * follow on the current line. This is used, for example, to flush text
1046 * after displaying a prompt and before waiting for user input.
1047 *
1048 * 1: flush the line and start a new line.
1049 *
1050 * 2: flush the line as though starting a new line, but don't add an
1051 * actual newline character to the output, since the underlying OS
1052 * display code will handle this. Instead, add a space after the line.
1053 * (This differs from mode 0 in that mode 0 shouldn't add anything at
1054 * all after the line.)
1055 *
1056 * 3: "preview" mode. Flush the line, but do not start a new line, and
1057 * retain the current text in the buffer. This is used for systems that
1058 * handle the line wrapping in the underlying system code to flush a
1059 * partially filled line that will need to be flushed again later.
1060 *
1061 * 4: same as mode 0, but used for internal buffer flushes only. Do not
1062 * involve the underlying OS layer in this type of flush - simply flush
1063 * our buffers with no separation.
1064 */
1065
1066 /* flush a given output stream */
1067 static void outflushn_stream(out_stream_info *stream, int nl)
1068 {
1069 int i;
1070
1071 /* null-terminate the current output line buffer */
1072 stream->linebuf[stream->linepos] = '\0';
1073
1074 /* note the position of the last character to display */
1075 i = stream->linepos - 1;
1076
1077 /* if we're adding anything, remove trailing spaces */
1078 if (nl != 0 && nl != 4)
1079 {
1080 /* look for last non-space character */
1081 for ( ; i >= 0 && outissp(stream->linebuf[i]) ; --i) ;
1082 }
1083
1084 /* check the output mode */
1085 if (nl == 3)
1086 {
1087 /*
1088 * this is the special "preview" mode -- only display the part
1089 * that we haven't already previewed for this same line
1090 */
1091 if (i + 1 > stream->preview)
1092 {
1093 /* write out the line */
1094 t_outline(stream, 0, &stream->linebuf[stream->preview],
1095 &stream->attrbuf[stream->preview]);
1096
1097 /* skip past the part we wrote */
1098 stream->preview += strlen(&stream->linebuf[stream->preview]);
1099 }
1100 }
1101 else
1102 {
1103 char *suffix; /* extra text to add after the flushed text */
1104 int countnl = 0; /* true if line counts for [more] paging */
1105
1106 /* null-terminate the buffer at the current position */
1107 stream->linebuf[++i] = '\0';
1108
1109 /* check the mode */
1110 switch(nl)
1111 {
1112 case 0:
1113 case 3:
1114 case 4:
1115 /* no newline - just flush out what we have with no suffix */
1116 suffix = 0;
1117 break;
1118
1119 case 1:
1120 /*
1121 * Add a newline. If there's nothing in the current line,
1122 * or we just wrote out a newline, do not add an extra
1123 * newline. Keep all newlines in PRE mode.
1124 */
1125 if (stream->linecol != 0 || !stream->just_did_nl
1126 || stream->html_pre_level != 0)
1127 {
1128 /* add a newline after the text */
1129 suffix = "\n";
1130
1131 /* count the line in the page size */
1132 countnl = 1;
1133 }
1134 else
1135 {
1136 /* don't add a newline */
1137 suffix = 0;
1138 }
1139 break;
1140
1141 case 2:
1142 /*
1143 * we're going to depend on the underlying OS output layer
1144 * to do line breaking, so don't add a newline, but do add a
1145 * space, so that the underlying OS layer knows we have a
1146 * word break here
1147 */
1148 suffix = " ";
1149 break;
1150 }
1151
1152 /*
1153 * display the line, as long as we have something buffered to
1154 * display; even if we don't, display it if our column is
1155 * non-zero and we didn't just do a newline, since this must
1156 * mean that we've flushed a partial line and are just now doing
1157 * the newline
1158 */
1159 if (stream->linebuf[stream->preview] != '\0'
1160 || (stream->linecol != 0 && !stream->just_did_nl)
1161 || stream->html_pre_level > 0)
1162 {
1163 /* write it out */
1164 t_outline(stream, countnl, &stream->linebuf[stream->preview],
1165 &stream->attrbuf[stream->preview]);
1166
1167 /* write the suffix, if any */
1168 if (suffix != 0)
1169 t_outline(stream, 0, suffix, 0);
1170 }
1171
1172 /* generate an HTML line break if necessary */
1173 if (nl == 1 && stream->html_mode && stream->html_target)
1174 t_outline(stream, 0, "<BR HEIGHT=0>", 0);
1175
1176 if (nl == 0)
1177 {
1178 /* we're not displaying a newline, so flush what we have */
1179 os_flush();
1180 }
1181 else
1182 {
1183 /* we displayed a newline, so reset the column position */
1184 stream->linecol = 0;
1185 }
1186
1187 /* reset the line output buffer position */
1188 stream->linepos = stream->preview = 0;
1189
1190 /*
1191 * If we just output a newline, note it. If we didn't just
1192 * output a newline, but we did write out anything else, note
1193 * that we're no longer at the start of a line on the underlying
1194 * output device.
1195 */
1196 if (nl == 1)
1197 stream->just_did_nl = TRUE;
1198 else if (stream->linebuf[stream->preview] != '\0')
1199 stream->just_did_nl = FALSE;
1200 }
1201
1202 /*
1203 * If the osifc-level attributes don't match the current attributes,
1204 * bring the osifc layer up to date. This is necessary in cases where
1205 * we set attributes immediately before asking for input - we
1206 * essentially need to flush the attributes without flushing any text.
1207 */
1208 if (stream->cur_attr != stream->os_attr)
1209 {
1210 /* set the osifc attributes */
1211 os_set_text_attr(stream->cur_attr);
1212
1213 /* remember the new attributes as the current osifc attributes */
1214 stream->os_attr = stream->cur_attr;
1215 }
1216 }
1217
1218 /* ------------------------------------------------------------------------ */
1219 /*
1220 * Determine if we're showing output. Returns true if output should be
1221 * displayed, false if it should be suppressed. We'll note the output
1222 * for hidden display accounting as needed.
1223 */
1224 static int out_is_hidden()
1225 {
1226 /* check the output flag */
1227 if (!outflag)
1228 {
1229 /* trace the hidden output if desired */
1230 if (dbghid && !hidout)
1231 trchid();
1232
1233 /* note the hidden output */
1234 hidout = 1;
1235
1236 /*
1237 * unless we're showing hidden text in the debugger, we're
1238 * suppressing output, so return true
1239 */
1240 if (!dbghid)
1241 return TRUE;
1242 }
1243
1244 /* we're not suppressing output */
1245 return FALSE;
1246 }
1247
1248 /* ------------------------------------------------------------------------ */
1249 /*
1250 * Display a blank line to the given stream
1251 */
1252 static void outblank_stream(out_stream_info *stream)
1253 {
1254 /* flush the stream */
1255 outflushn_stream(stream, 1);
1256
1257 /* if generating for an HTML display target, add an HTML line break */
1258 if (stream->html_mode && stream->html_target)
1259 outstring_stream(stream, "<BR>");
1260
1261 /* write out the newline */
1262 t_outline(stream, 1, "\n", 0);
1263 }
1264
1265 /* ------------------------------------------------------------------------ */
1266 /*
1267 * Generate a tab for a "\t" sequence in the game text.
1268 *
1269 * Standard (non-HTML) version: we'll generate enough spaces to take us
1270 * to the next tab stop.
1271 *
1272 * HTML version: if we're in native HTML mode, we'll just generate a
1273 * <TAB MULTIPLE=4>; if we're not in HTML mode, we'll generate a hard
1274 * tab character, which the HTML formatter will interpret as a <TAB
1275 * MULTIPLE=4>.
1276 */
1277 static void outtab_stream(out_stream_info *stream)
1278 {
1279 /* check to see what the underlying system is expecting */
1280 if (stream->html_target)
1281 {
1282 /* the underlying system is HTML - check for HTML mode */
1283 if (stream->html_mode)
1284 {
1285 /* we're in HTML mode, so use the HTML <TAB> tag */
1286 outstring_stream(stream, "<TAB MULTIPLE=4>");
1287 }
1288 else
1289 {
1290 /* we're not in HTML mode, so generate a hard tab character */
1291 outchar_noxlat_stream(stream, QTAB);
1292 }
1293 }
1294 else
1295 {
1296 int maxcol;
1297
1298 /*
1299 * We're not in HTML mode - expand the tab with spaces. Figure
1300 * the maximum column: if we're doing our own line wrapping, never
1301 * go beyond the actual display width.
1302 */
1303 maxcol = (stream->os_line_wrap ? OS_MAXWIDTH : G_os_linewidth);
1304
1305 /* add the spaces */
1306 do
1307 {
1308 stream->attrbuf[stream->linepos] = stream->cur_attr;
1309 stream->linebuf[stream->linepos++] = ' ';
1310 ++(stream->linecol);
1311 } while (((stream->linecol + 1) & 3) != 0
1312 && stream->linecol < maxcol);
1313 }
1314 }
1315
1316
1317 /* ------------------------------------------------------------------------ */
1318 /*
1319 * Flush a line
1320 */
1321 static void out_flushline(out_stream_info *stream, int padding)
1322 {
1323 /*
1324 * check to see if we're using the underlying display layer's line
1325 * wrapping
1326 */
1327 if (stream->os_line_wrap)
1328 {
1329 /*
1330 * In the HTML version, we don't need the normal *MORE*
1331 * processing, since the HTML layer will handle that.
1332 * Furthermore, we don't need to provide actual newline breaks
1333 * -- that happens after the HTML is parsed, so we don't have
1334 * enough information here to figure out actual line breaks.
1335 * So, we'll just flush out our buffer whenever it fills up, and
1336 * suppress newlines.
1337 *
1338 * Similarly, if we have OS-level MORE processing, don't try to
1339 * figure out where the line breaks go -- just flush our buffer
1340 * without a trailing newline whenever the buffer is full, and
1341 * let the OS layer worry about formatting lines and paragraphs.
1342 *
1343 * If we're using padding, use mode 2. If we don't want padding
1344 * (which is the case if we completely fill up the buffer
1345 * without finding any word breaks), write out in mode 0, which
1346 * just flushes the buffer exactly like it is.
1347 */
1348 outflushn_stream(stream, padding ? 2 : 4);
1349 }
1350 else
1351 {
1352 /*
1353 * Normal mode - we process the *MORE* prompt ourselves, and we
1354 * are responsible for figuring out where the actual line breaks
1355 * go. Use outflush() to generate an actual newline whenever we
1356 * flush out our buffer.
1357 */
1358 outflushn_stream(stream, 1);
1359 }
1360 }
1361
1362
1363 /* ------------------------------------------------------------------------ */
1364 /*
1365 * Write a character to an output stream without translation
1366 */
1367 static void outchar_noxlat_stream(out_stream_info *stream, char c)
1368 {
1369 int i;
1370 int qspace;
1371
1372 /* check for the special quoted space character */
1373 if (c == QSPACE)
1374 {
1375 /* it's a quoted space - note it and convert it to a regular space */
1376 qspace = 1;
1377 c = ' ';
1378 }
1379 else if (c == QTAB)
1380 {
1381 /* it's a hard tab - convert it to an ordinary tab */
1382 c = '\t';
1383 qspace = 0;
1384 }
1385 else
1386 {
1387 /* translate any whitespace character to a regular space character */
1388 if (outissp(c))
1389 c = ' ';
1390
1391 /* it's not a quoted space */
1392 qspace = 0;
1393 }
1394
1395 /* check for the caps/nocaps flags */
1396 if ((stream->capsflag || stream->allcapsflag) && outisal(c))
1397 {
1398 /* capsflag is set, so capitalize this character */
1399 if (outislo(c))
1400 c = toupper(c);
1401
1402 /* okay, we've capitalized something; clear flag */
1403 stream->capsflag = 0;
1404 }
1405 else if (stream->nocapsflag && outisal(c))
1406 {
1407 /* nocapsflag is set, so minisculize this character */
1408 if (outisup(c))
1409 c = tolower(c);
1410
1411 /* clear the flag now that we've done the job */
1412 stream->nocapsflag = 0;
1413 }
1414
1415 /* if in capture mode, simply capture the character */
1416 if (stream->capturing)
1417 {
1418 uchar *p;
1419
1420 /* if we have a valid capture object, copy to it */
1421 if (stream->capture_obj != MCMONINV)
1422 {
1423 /* lock the object holding the captured text */
1424 p = mcmlck(stream->capture_ctx, stream->capture_obj);
1425
1426 /* make sure the capture object is big enough */
1427 if (mcmobjsiz(stream->capture_ctx, stream->capture_obj)
1428 <= stream->capture_ofs)
1429 {
1430 /* expand the object by another 256 bytes */
1431 p = mcmrealo(stream->capture_ctx, stream->capture_obj,
1432 (ushort)(stream->capture_ofs + 256));
1433 }
1434
1435 /* add this character */
1436 *(p + stream->capture_ofs++) = c;
1437
1438 /* unlock the capture object */
1439 mcmtch(stream->capture_ctx, stream->capture_obj);
1440 mcmunlck(stream->capture_ctx, stream->capture_obj);
1441 }
1442
1443 /*
1444 * we're done - we don't want to actually display the character
1445 * while capturing
1446 */
1447 return;
1448 }
1449
1450 /* add the character to out output buffer, flushing as needed */
1451 if (stream->linecol + 1 < G_os_linewidth)
1452 {
1453 /*
1454 * there's room for this character, so add it to the buffer
1455 */
1456
1457 /* ignore non-quoted space at start of line outside of PRE */
1458 if (outissp(c) && c != '\t' && stream->linecol == 0 && !qspace
1459 && stream->html_pre_level == 0)
1460 return;
1461
1462 /* is this a non-quoted space not at the start of the line? */
1463 if (outissp(c) && c != '\t' && stream->linecol != 0 && !qspace
1464 && stream->html_pre_level == 0)
1465 {
1466 int pos1 = stream->linepos - 1;
1467 char p = stream->linebuf[pos1]; /* check previous character */
1468
1469 /* ignore repeated spaces - collapse into a single space */
1470 if (outissp(p))
1471 return;
1472
1473 /*
1474 * Certain punctuation requires a double space: a period, a
1475 * question mark, an exclamation mark, or a colon; or any of
1476 * these characters followed by any number of single and/or
1477 * double quotes. First, scan back to before any quotes, if
1478 * are on one now, then check the preceding character; if
1479 * it's one of the punctuation marks requiring a double
1480 * space, add this space a second time. (In addition to
1481 * scanning back past quotes, scan past parentheses,
1482 * brackets, and braces.) Don't double the spacing if we're
1483 * not in the normal doublespace mode; some people may
1484 * prefer single spacing after punctuation, so we make this
1485 * a run-time option.
1486 */
1487 if (doublespace)
1488 {
1489 /* find the previous relevant punctuation character */
1490 while (pos1 &&
1491 (p == '"' || p == '\'' || p == ')' || p == ']'
1492 || p == '}'))
1493 {
1494 p = stream->linebuf[--pos1];
1495 }
1496 if ( p == '.' || p == '?' || p == '!' || p == ':' )
1497 {
1498 /* a double-space is required after this character */
1499 stream->attrbuf[stream->linepos] = stream->cur_attr;
1500 stream->linebuf[stream->linepos++] = c;
1501 ++(stream->linecol);
1502 }
1503 }
1504 }
1505
1506 /* add this character to the buffer */
1507 stream->attrbuf[stream->linepos] = stream->cur_attr;
1508 stream->linebuf[stream->linepos++] = c;
1509
1510 /* advance the output column position */
1511 ++(stream->linecol);
1512 return;
1513 }
1514
1515 /*
1516 * The line would overflow if this character were added. Find the
1517 * most recent word break, and output the line up to the previous
1518 * word. Note that if we're trying to output a space, we'll just
1519 * add it to the line buffer. If the last character of the line
1520 * buffer is already a space, we won't do anything right now.
1521 */
1522 if (outissp(c) && c != '\t' && !qspace)
1523 {
1524 /* this is a space, so we're at a word break */
1525 if (stream->linebuf[stream->linepos - 1] != ' ')
1526 {
1527 stream->attrbuf[stream->linepos] = stream->cur_attr;
1528 stream->linebuf[stream->linepos++] = ' ';
1529 }
1530 return;
1531 }
1532
1533 /*
1534 * Find the most recent word break: look for a space or dash, starting
1535 * at the end of the line.
1536 *
1537 * If we're about to write a hyphen, we want to skip all contiguous
1538 * hyphens, because we want to keep them together as a single
1539 * punctuation mark; then keep going in the normal manner, which will
1540 * keep the hyphens plus the word they're attached to together as a
1541 * single unit. If spaces precede the sequence of hyphens, include
1542 * the prior word as well.
1543 */
1544 i = stream->linepos - 1;
1545 if (c == '-')
1546 {
1547 /* skip any contiguous hyphens at the end of the line */
1548 for ( ; i >= 0 && stream->linebuf[i] == '-' ; --i) ;
1549
1550 /* skip any spaces preceding the sequence of hyphens */
1551 for ( ; i >= 0 && outissp(stream->linebuf[i]) ; --i) ;
1552 }
1553
1554 /*
1555 * Now find the preceding space. If we're doing our own wrapping
1556 * (i.e., we're not using OS line wrapping), then look for the
1557 * nearest hyphen as well.
1558 */
1559 for ( ; i >= 0 && !outissp(stream->linebuf[i])
1560 && !(!stream->os_line_wrap && stream->linebuf[i] == '-') ; --i) ;
1561
1562 /* check to see if we found a good place to break */
1563 if (i < 0)
1564 {
1565 /*
1566 * we didn't find any good place to break - flush the entire
1567 * line as-is, breaking arbitrarily in the middle of a word
1568 */
1569 out_flushline(stream, FALSE);
1570
1571 /*
1572 * we've completely cleared out the line buffer, so reset all of
1573 * the line buffer counters
1574 */
1575 stream->linepos = 0;
1576 stream->linecol = 0;
1577 stream->linebuf[0] = '\0';
1578 }
1579 else
1580 {
1581 char brkchar;
1582 char tmpbuf[MAXWIDTH];
1583 int tmpattr[MAXWIDTH];
1584 size_t tmpcnt;
1585
1586 /* remember the word-break character */
1587 brkchar = stream->linebuf[i];
1588
1589 /* null-terminate the line buffer */
1590 stream->linebuf[stream->linepos] = '\0';
1591
1592 /* the next line starts after the break - save a copy */
1593 tmpcnt = strlen(&stream->linebuf[i+1]);
1594 memcpy(tmpbuf, &stream->linebuf[i+1], tmpcnt + 1);
1595 memcpy(tmpattr, &stream->attrbuf[i+1], tmpcnt * sizeof(tmpattr[0]));
1596
1597 /*
1598 * terminate the buffer at the space or after the hyphen,
1599 * depending on where we broke
1600 */
1601 if (outissp(brkchar))
1602 stream->linebuf[i] = '\0';
1603 else
1604 stream->linebuf[i+1] = '\0';
1605
1606 /* write out everything up to the word break */
1607 out_flushline(stream, TRUE);
1608
1609 /* copy the next line into line buffer */
1610 memcpy(stream->linebuf, tmpbuf, tmpcnt + 1);
1611 memcpy(stream->attrbuf, tmpattr, tmpcnt * sizeof(tmpattr[0]));
1612 stream->linepos = tmpcnt;
1613
1614 /*
1615 * figure what column we're now in - count all of the printable
1616 * characters in the new line
1617 */
1618 for (stream->linecol = 0, i = 0 ; i < stream->linepos ; ++i)
1619 {
1620 /* if it's printable, count it */
1621 if (((unsigned char)stream->linebuf[i]) >= 26)
1622 ++(stream->linecol);
1623 }
1624 }
1625
1626 /* add the new character to buffer */
1627 stream->attrbuf[stream->linepos] = stream->cur_attr;
1628 stream->linebuf[stream->linepos++] = c;
1629
1630 /* advance the column counter */
1631 ++(stream->linecol);
1632 }
1633
1634 /* ------------------------------------------------------------------------ */
1635 /*
1636 * Write out a character, translating to the local system character set
1637 * from the game's internal character set.
1638 */
1639 static void outchar_stream(out_stream_info *stream, char c)
1640 {
1641 outchar_noxlat_stream(stream, cmap_i2n(c));
1642 }
1643
1644 /*
1645 * write out a string, translating to the local system character set
1646 */
1647 static void outstring_stream(out_stream_info *stream, char *s)
1648 {
1649 /* write out each character in the string */
1650 for ( ; *s ; ++s)
1651 outchar_stream(stream, *s);
1652 }
1653
1654 /*
1655 * write out a string without translation
1656 */
1657 static void outstring_noxlat_stream(out_stream_info *stream, char *s)
1658 {
1659 for ( ; *s ; ++s)
1660 outchar_noxlat_stream(stream, *s);
1661 }
1662
1663
1664 /* ------------------------------------------------------------------------ */
1665 /*
1666 * Write out an HTML character value, translating to the local character
1667 * set.
1668 */
1669 static void outchar_html_stream(out_stream_info *stream,
1670 unsigned int htmlchar)
1671 {
1672 struct amp_tbl_t *ampptr;
1673
1674 /*
1675 * search for a mapping entry for this entity, in case it's defined
1676 * in an external mapping file
1677 */
1678 for (ampptr = amp_tbl ;
1679 ampptr < amp_tbl + sizeof(amp_tbl)/sizeof(amp_tbl[0]) ; ++ampptr)
1680 {
1681 /* if this is the one, stop looking */
1682 if (ampptr->html_cval == htmlchar)
1683 break;
1684 }
1685
1686 /*
1687 * If we found a mapping table entry, and the entry has an expansion
1688 * from the external character mapping table file, use the external
1689 * expansion; otherwise, use the default expansion.
1690 */
1691 if (ampptr >= amp_tbl + sizeof(amp_tbl)/sizeof(amp_tbl[0])
1692 || ampptr->expan == 0)
1693 {
1694 char xlat_buf[50];
1695
1696 /*
1697 * there's no external mapping table file expansion -- use the
1698 * default OS mapping routine
1699 */
1700 os_xlat_html4(htmlchar, xlat_buf, sizeof(xlat_buf));
1701 outstring_noxlat_stream(stream, xlat_buf);
1702 }
1703 else
1704 {
1705 /*
1706 * use the explicit mapping from the mapping table file
1707 */
1708 outstring_noxlat_stream(stream, ampptr->expan);
1709 }
1710 }
1711
1712
1713 /* ------------------------------------------------------------------------ */
1714 /*
1715 * Enter a recursion level. Returns TRUE if the caller should proceed
1716 * with the operation, FALSE if not.
1717 *
1718 * If we're making a recursive call, thereby re-entering the formatter,
1719 * and this stream is not the same as the enclosing stream, we want to
1720 * ignore this call and suppress any output to this stream, so we'll
1721 * return FALSE.
1722 */
1723 static int out_push_stream(out_stream_info *stream)
1724 {
1725 /*
1726 * if we're already in the formatter, and the new stream doesn't
1727 * match the enclosing recursion level's stream, tell the caller to
1728 * abort the operation
1729 */
1730 if (G_recurse != 0 && G_cur_stream != stream)
1731 return FALSE;
1732
1733 /* note the active stream */
1734 G_cur_stream = stream;
1735
1736 /* count the entry */
1737 ++G_recurse;
1738
1739 /* tell the caller to proceed */
1740 return TRUE;
1741 }
1742
1743 /*
1744 * Leave a recursion level
1745 */
1746 static void out_pop_stream()
1747 {
1748 /* count the exit */
1749 --G_recurse;
1750 }
1751
1752 /* ------------------------------------------------------------------------ */
1753 /*
1754 * nextout() returns the next character in a string, and updates the
1755 * string pointer and remaining length. Returns zero if no more
1756 * characters are available in the string.
1757 */
1758 /* static char nextout(char **s, uint *len); */
1759 #define nextout(s, len) ((char)(*(len) == 0 ? 0 : (--(*(len)), *((*(s))++))))
1760
1761
1762 /* ------------------------------------------------------------------------ */
1763 /*
1764 * Get the next character, writing the previous character to the given
1765 * output stream if it's not null.
1766 */
1767 static char nextout_copy(char **s, size_t *slen,
1768 char prv, out_stream_info *stream)
1769 {
1770 /* if there's a stream, write the previous character to the stream */
1771 if (stream != 0)
1772 outchar_stream(stream, prv);
1773
1774 /* return the next character */
1775 return nextout(s, slen);
1776 }
1777
1778 /* ------------------------------------------------------------------------ */
1779 /*
1780 * Read an HTML tag, for our primitive mini-parser. If 'stream' is not
1781 * null, we'll copy each character we read to the output stream. Returns
1782 * the next character after the tag name.
1783 */
1784 static char read_tag(char *dst, size_t dstlen, int *is_end_tag,
1785 char **s, size_t *slen, out_stream_info *stream)
1786 {
1787 char c;
1788
1789 /* skip the opening '<' */
1790 c = nextout_copy(s, slen, '<', stream);
1791
1792 /* skip spaces */
1793 while (outissp(c))
1794 c = nextout_copy(s, slen, c, stream);
1795
1796 /* note if this is a closing tag */
1797 if (c == '/' || c == '\\')
1798 {
1799 /* it's an end tag - note it and skip the slash */
1800 *is_end_tag = TRUE;
1801 c = nextout_copy(s, slen, c, stream);
1802
1803 /* skip yet more spaces */
1804 while (outissp(c))
1805 c = nextout_copy(s, slen, c, stream);
1806 }
1807 else
1808 *is_end_tag = FALSE;
1809
1810 /*
1811 * find the end of the tag name - the tag continues to the next space,
1812 * '>', or end of line
1813 */
1814 for ( ; c != '\0' && !outissp(c) && c != '>' ;
1815 c = nextout_copy(s, slen, c, stream))
1816 {
1817 /* add this to the tag buffer if it fits */
1818 if (dstlen > 1)
1819 {
1820 *dst++ = c;
1821 --dstlen;
1822 }
1823 }
1824
1825 /* null-terminate the tag name */
1826 if (dstlen > 0)
1827 *dst = '\0';
1828
1829 /* return the next character */
1830 return c;
1831 }
1832
1833
1834 /* ------------------------------------------------------------------------ */
1835 /*
1836 * display a string of a given length to a given stream
1837 */
1838 static int outformatlen_stream(out_stream_info *stream,
1839 char *s, size_t slen)
1840 {
1841 char c;
1842 int done = 0;
1843 char fmsbuf[40]; /* space for constructing translation string */
1844 uint fmslen;
1845 char *f = 0;
1846 char *f1;
1847 int infmt = 0;
1848
1849 /*
1850 * This routine can recurse because of format strings ("%xxx%"
1851 * sequences). When we recurse, we want to ensure that the
1852 * recursion is directed to the original stream only. So, note the
1853 * current stream statically in case we re-enter the formatter.
1854 */
1855 if (!out_push_stream(stream))
1856 return 0;
1857
1858 /* get the first character */
1859 c = nextout(&s, &slen);
1860
1861 /* if we have anything to show, show it */
1862 while (c != '\0')
1863 {
1864 /* check if we're collecting translation string */
1865 if (infmt)
1866 {
1867 /*
1868 * if the string is too long for our buffer, or we've come
1869 * across a backslash (illegal in a format string), or we've
1870 * come across an HTML-significant character ('&' or '<') in
1871 * HTML mode, we must have a stray percent sign; dump the
1872 * whole string so far and act as though we have no format
1873 * string
1874 */
1875 if (c == '\\'
1876 || f == &fmsbuf[sizeof(fmsbuf)]
1877 || (stream->html_mode && (c == '<' || c == '&')))
1878 {
1879 outchar_stream(stream, '%');
1880 for (f1 = fmsbuf ; f1 < f ; ++f1)
1881 outchar_stream(stream, *f1);
1882 infmt = 0;
1883
1884 /* process this character again */
1885 continue;
1886 }
1887 else if (c == '%' && f == fmsbuf) /* double percent sign? */
1888 {
1889 outchar_stream(stream, '%'); /* send out a single '%' */
1890 infmt = 0; /* no longer processing translation string */
1891 }
1892 else if (c == '%') /* found end of string? translate it if so */
1893 {
1894 uchar *fms;
1895 int initcap = FALSE;
1896 int allcaps = FALSE;
1897 char fmsbuf_srch[sizeof(fmsbuf)];
1898
1899 /* null-terminate the string */
1900 *f = '\0';
1901
1902 /* check for an init cap */
1903 if (outisup(fmsbuf[0]))
1904 {
1905 /*
1906 * note the initial capital, so that we follow the
1907 * original capitalization in the substituted string
1908 */
1909 initcap = TRUE;
1910
1911 /*
1912 * if the second letter is capitalized as well,
1913 * capitalize the entire substituted string
1914 */
1915 if (fmsbuf[1] != '\0' && outisup(fmsbuf[1]))
1916 {
1917 /* use all caps */
1918 allcaps = TRUE;
1919 }
1920 }
1921
1922 /* convert the entire string to lower case for searching */
1923 strcpy(fmsbuf_srch, fmsbuf);
1924 os_strlwr(fmsbuf_srch);
1925
1926 /* find the string in the format string table */
1927 fmslen = strlen(fmsbuf_srch);
1928 for (fms = fmsbase ; fms < fmstop ; )
1929 {
1930 uint propnum;
1931 uint len;
1932
1933 /* get the information on this entry */
1934 propnum = osrp2(fms);
1935 len = osrp2(fms + 2) - 2;
1936
1937 /* check for a match */
1938 if (len == fmslen &&
1939 !memcmp(fms + 4, fmsbuf_srch, (size_t)len))
1940 {
1941 int old_all_caps;
1942
1943 /* note the current ALLCAPS mode */
1944 old_all_caps = stream->allcapsflag;
1945
1946 /*
1947 * we have a match - set the appropriate
1948 * capitalization mode
1949 */
1950 if (allcaps)
1951 outallcaps_stream(stream, TRUE);
1952 else if (initcap)
1953 outcaps_stream(stream);
1954
1955 /*
1956 * evaluate the associated property to generate
1957 * the substitution text
1958 */
1959 runppr(runctx, cmdActor, (prpnum)propnum, 0);
1960
1961 /* turn off ALLCAPS mode */
1962 outallcaps_stream(stream, old_all_caps);
1963
1964 /* no need to look any further */
1965 break;
1966 }
1967
1968 /* move on to next formatstring if not yet found */
1969 fms += len + 4;
1970 }
1971
1972 /* if we can't find it, dump the format string as-is */
1973 if (fms == fmstop)
1974 {
1975 outchar_stream(stream, '%');
1976 for (f1 = fmsbuf ; f1 < f ; ++f1)
1977 outchar_stream(stream, *f1);
1978 outchar_stream(stream, '%');
1979 }
1980
1981 /* no longer reading format string */
1982 infmt = 0;
1983 }
1984 else
1985 {
1986 /* copy this character of the format string */
1987 *f++ = c;
1988 }
1989
1990 /* move on to the next character and continue scanning */
1991 c = nextout(&s, &slen);
1992 continue;
1993 }
1994
1995 /*
1996 * If we're parsing HTML here, and we're inside a tag, skip
1997 * characters until we reach the end of the tag.
1998 */
1999 if (stream->html_mode_flag != HTML_MODE_NORMAL)
2000 {
2001 switch(stream->html_mode_flag)
2002 {
2003 case HTML_MODE_TAG:
2004 /*
2005 * keep skipping up to the closing '>', but note when we
2006 * enter any quoted section
2007 */
2008 switch(c)
2009 {
2010 case '>':
2011 /* we've reached the end of the tag */
2012 stream->html_mode_flag = HTML_MODE_NORMAL;
2013
2014 /* if we have a deferred <BR>, process it now */
2015 switch(stream->html_defer_br)
2016 {
2017 case HTML_DEFER_BR_NONE:
2018 /* no deferred <BR> */
2019 break;
2020
2021 case HTML_DEFER_BR_FLUSH:
2022 outflushn_stream(stream, 1);
2023 break;
2024
2025 case HTML_DEFER_BR_BLANK:
2026 outblank_stream(stream);
2027 break;
2028 }
2029
2030 /* no more deferred <BR> pending */
2031 stream->html_defer_br = HTML_DEFER_BR_NONE;
2032
2033 /* no more ALT attribute allowed */
2034 stream->html_allow_alt = FALSE;
2035 break;
2036
2037 case '"':
2038 /* enter a double-quoted string */
2039 stream->html_mode_flag = HTML_MODE_DQUOTE;
2040 break;
2041
2042 case '\'':
2043 /* enter a single-quoted string */
2044 stream->html_mode_flag = HTML_MODE_SQUOTE;
2045 break;
2046
2047 default:
2048 /* if it's alphabetic, note the attribute name */
2049 if (outisal(c))
2050 {
2051 char attrname[128];
2052 char attrval[256];
2053 char *dst;
2054
2055 /* gather up the attribute name */
2056 for (dst = attrname ;
2057 dst + 1 < attrname + sizeof(attrname) ; )
2058 {
2059 /* store this character */
2060 *dst++ = c;
2061
2062 /* get the next character */
2063 c = nextout(&s, &slen);
2064
2065 /* if it's not alphanumeric, stop scanning */
2066 if (!outisal(c) && !outisdg(c))
2067 break;
2068 }
2069
2070 /* null-terminate the result */
2071 *dst++ = '\0';
2072
2073 /* gather the value if present */
2074 if (c == '=')
2075 {
2076 char qu;
2077
2078 /* skip the '=' */
2079 c = nextout(&s, &slen);
2080
2081 /* if we have a quote, so note */
2082 if (c == '"' || c == '\'')
2083 {
2084 /* remember the quote */
2085 qu = c;
2086
2087 /* skip it */
2088 c = nextout(&s, &slen);
2089 }
2090 else
2091 {
2092 /* no quote */
2093 qu = 0;
2094 }
2095
2096 /* read the value */
2097 for (dst = attrval ;
2098 dst + 1 < attrval + sizeof(attrval) ; )
2099 {
2100 /* store this character */
2101 *dst++ = c;
2102
2103 /* read the next one */
2104 c = nextout(&s, &slen);
2105 if (c == '\0')
2106 {
2107 /*
2108 * we've reached the end of the
2109 * string, and we're still inside
2110 * this attribute - abandon the
2111 * attribute but note that we're
2112 * inside a quoted string if
2113 * necessary
2114 */
2115 if (qu == '"')
2116 stream->html_mode_flag =
2117 HTML_MODE_DQUOTE;
2118 else if (qu == '\'')
2119 stream->html_mode_flag =
2120 HTML_MODE_SQUOTE;
2121 else
2122 stream->html_mode_flag
2123 = HTML_MODE_TAG;
2124
2125 /* stop scanning the string */
2126 break;
2127 }
2128
2129 /*
2130 * if we're looking for a quote, check
2131 * for the closing quote; otherwise,
2132 * check for alphanumerics
2133 */
2134 if (qu != 0)
2135 {
2136 /* if this is our quote, stop scanning */
2137 if (c == qu)
2138 break;
2139 }
2140 else
2141 {
2142 /* if it's non-alphanumeric, we're done */
2143 if (!outisal(c) && !outisdg(c))
2144 break;
2145 }
2146 }
2147
2148 /* skip the closing quote, if necessary */
2149 if (qu != 0 && c == qu)
2150 c = nextout(&s, &slen);
2151
2152 /* null-terminate the value string */
2153 *dst = '\0';
2154 }
2155 else
2156 {
2157 /* no value */
2158 attrval[0] = '\0';
2159 }
2160
2161 /*
2162 * see if we recognize it, and it's meaningful
2163 * in the context of the current tag
2164 */
2165 if (!stricmp(attrname, "height")
2166 && stream->html_defer_br != HTML_DEFER_BR_NONE)
2167 {
2168 int ht;
2169
2170 /*
2171 * If the height is zero, always treat this
2172 * as a non-blanking flush. If it's one,
2173 * treat it as we originally planned to. If
2174 * it's greater than one, add n blank lines.
2175 */
2176 ht = atoi(attrval);
2177 if (ht == 0)
2178 {
2179 /* always use non-blanking flush */
2180 stream->html_defer_br = HTML_DEFER_BR_FLUSH;
2181 }
2182 else if (ht == 1)
2183 {
2184 /* keep original setting */
2185 }
2186 else
2187 {
2188 for ( ; ht > 0 ; --ht)
2189 outblank_stream(stream);
2190 }
2191 }
2192 else if (!stricmp(attrname, "alt")
2193 && !stream->html_in_ignore
2194 && stream->html_allow_alt)
2195 {
2196 /* write out the ALT string */
2197 outstring_stream(stream, attrval);
2198 }
2199
2200 /*
2201 * since we already read the next character,
2202 * simply loop back immediately
2203 */
2204 continue;
2205 }
2206 break;
2207 }
2208 break;
2209
2210 case HTML_MODE_DQUOTE:
2211 /* if we've reached the closing quote, return to tag state */
2212 if (c == '"')
2213 stream->html_mode_flag = HTML_MODE_TAG;
2214 break;
2215
2216 case HTML_MODE_SQUOTE:
2217 /* if we've reached the closing quote, return to tag state */
2218 if (c == '\'')
2219 stream->html_mode_flag = HTML_MODE_TAG;
2220 break;
2221 }
2222
2223 /*
2224 * move on to the next character, and start over with the
2225 * new character
2226 */
2227 c = nextout(&s, &slen);
2228 continue;
2229 }
2230
2231 /*
2232 * If we're in a title, and this isn't the start of a new tag,
2233 * skip the character - we suppress all regular text output
2234 * inside a <TITLE> ... </TITLE> sequence.
2235 */
2236 if (stream->html_in_ignore && c != '<')
2237 {
2238 /* check for entities */
2239 char cbuf[50];
2240 if (c == '&')
2241 {
2242 /* translate the entity */
2243 c = out_parse_entity(cbuf, sizeof(cbuf), &s, &slen);
2244 }
2245 else
2246 {
2247 /* it's an ordinary character - copy it out literally */
2248 cbuf[0] = c;
2249 cbuf[1] = '\0';
2250
2251 /* get the next character */
2252 c = nextout(&s, &slen);
2253 }
2254
2255 /*
2256 * if we're gathering a title, and there's room in the title
2257 * buffer for more (always leaving room for a null
2258 * terminator), add this to the title buffer
2259 */
2260 if (stream->html_in_title)
2261 {
2262 char *cbp;
2263 for (cbp = cbuf ; *cbp != '\0' ; ++cbp)
2264 {
2265 /* if there's room, add it */
2266 if (stream->html_title_ptr + 1 <
2267 stream->html_title_buf
2268 + sizeof(stream->html_title_buf))
2269 *stream->html_title_ptr++ = *cbp;
2270 }
2271 }
2272
2273 /* don't display anything in an ignore section */
2274 continue;
2275 }
2276
2277 if ( c == '%' ) /* translation string? */
2278 {
2279 infmt = 1;
2280 f = fmsbuf;
2281 }
2282 else if ( c == '\\' ) /* special escape code? */
2283 {
2284 c = nextout(&s, &slen);
2285
2286 if (stream->capturing && c != '^' && c != 'v' && c != '\0')
2287 {
2288 outchar_stream(stream, '\\');
2289 outchar_stream(stream, c);
2290
2291 /* keep the \- and also put out the next two chars */
2292 if (c == '-')
2293 {
2294 outchar_stream(stream, nextout(&s, &slen));
2295 outchar_stream(stream, nextout(&s, &slen));
2296 }
2297 }
2298 else
2299 {
2300 switch(c)
2301 {
2302 case 'H': /* HTML mode entry */
2303 /* turn on HTML mode in the renderer */
2304 switch(c = nextout(&s, &slen))
2305 {
2306 case '-':
2307 /* if we have an HTML target, notify it */
2308 if (stream->html_target)
2309 {
2310 /* flush its stream */
2311 outflushn_stream(stream, 0);
2312
2313 /* tell the OS layer to switch to normal mode */
2314 out_end_html(stream);
2315 }
2316
2317 /* switch to normal mode */
2318 stream->html_mode = FALSE;
2319 break;
2320
2321 case '+':
2322 default:
2323 /* if we have an HTML target, notify it */
2324 if (stream->html_target)
2325 {
2326 /* flush the underlying stream */
2327 outflushn_stream(stream, 0);
2328
2329 /* tell the OS layer to switch to HTML mode */
2330 out_start_html(stream);
2331 }
2332
2333 /* switch to HTML mode */
2334 stream->html_mode = TRUE;
2335
2336 /*
2337 * if the character wasn't a "+", it's not part
2338 * of the "\H" sequence, so display it normally
2339 */
2340 if (c != '+' && c != 0)
2341 outchar_stream(stream, c);
2342 break;
2343 }
2344
2345 /* this sequence doesn't result in any actual output */
2346 break;
2347
2348 case 'n': /* newline? */
2349 outflushn_stream(stream, 1); /* yes, output line */
2350 break;
2351
2352 case 't': /* tab? */
2353 outtab_stream(stream);
2354 break;
2355
2356 case 'b': /* blank line? */
2357 outblank_stream(stream);
2358 break;
2359
2360 case '\0': /* line ends here? */
2361 done = 1;
2362 break;
2363
2364 case ' ': /* quoted space */
2365 if (stream->html_target && stream->html_mode)
2366 {
2367 /*
2368 * we're generating for an HTML target and we're
2369 * in HTML mode - generate the HTML non-breaking
2370 * space
2371 */
2372 outstring_stream(stream, "&nbsp;");
2373 }
2374 else
2375 {
2376 /*
2377 * we're not in HTML mode - generate our
2378 * internal quoted space character
2379 */
2380 outchar_stream(stream, QSPACE);
2381 }
2382 break;
2383
2384 case '^': /* capitalize next character */
2385 stream->capsflag = 1;
2386 stream->nocapsflag = 0;
2387 break;
2388
2389 case 'v':
2390 stream->nocapsflag = 1;
2391 stream->capsflag = 0;
2392 break;
2393
2394 case '(':
2395 /* generate HTML if in the appropriate mode */
2396 if (stream->html_mode && stream->html_target)
2397 {
2398 /* send HTML to the renderer */
2399 outstring_stream(stream, "<B>");
2400 }
2401 else
2402 {
2403 /* turn on the 'hilite' attribute */
2404 stream->cur_attr |= OS_ATTR_HILITE;
2405 }
2406 break;
2407
2408 case ')':
2409 /* generate HTML if in the appropriate mode */
2410 if (stream->html_mode && stream->html_target)
2411 {
2412 /* send HTML to the renderer */
2413 outstring_stream(stream, "</B>");
2414 }
2415 else
2416 {
2417 /* turn off the 'hilite' attribute */
2418 stream->cur_attr &= ~OS_ATTR_HILITE;
2419 }
2420 break;
2421
2422 case '-':
2423 outchar_stream(stream, nextout(&s, &slen));
2424 outchar_stream(stream, nextout(&s, &slen));
2425 break;
2426
2427 default: /* just pass invalid escapes as-is */
2428 outchar_stream(stream, c);
2429 break;
2430 }
2431 }
2432 }
2433 else if (!stream->html_target
2434 && stream->html_mode
2435 && (c == '<' || c == '&'))
2436 {
2437 /*
2438 * We're in HTML mode, but the underlying target does not
2439 * accept HTML sequences. It appears we're at the start of
2440 * an "&" entity or a tag sequence, so parse it, remove it,
2441 * and replace it (if possible) with a text-only equivalent.
2442 */
2443 if (c == '<')
2444 {
2445 /* read the tag */
2446 char tagbuf[50];
2447 int is_end_tag;
2448 c = read_tag(tagbuf, sizeof(tagbuf), &is_end_tag,
2449 &s, &slen, 0);
2450
2451 /*
2452 * Check to see if we recognize the tag. We only
2453 * recognize a few simple tags that map easily to
2454 * character mode.
2455 */
2456 if (!stricmp(tagbuf, "br"))
2457 {
2458 /*
2459 * line break - if there's anything buffered up,
2460 * just flush the current line, otherwise write out
2461 * a blank line
2462 */
2463 if (stream->html_in_ignore)
2464 /* suppress breaks in ignore mode */;
2465 else if (stream->linepos != 0)
2466 stream->html_defer_br = HTML_DEFER_BR_FLUSH;
2467 else
2468 stream->html_defer_br = HTML_DEFER_BR_BLANK;
2469 }
2470 else if (!stricmp(tagbuf, "b")
2471 || !stricmp(tagbuf, "i")
2472 || !stricmp(tagbuf, "em")
2473 || !stricmp(tagbuf, "strong")
2474 || !stricmp(tagbuf, "tt"))
2475 {
2476 int attr;
2477
2478 /* choose the attribute flag */
2479 switch (tagbuf[0])
2480 {
2481 case 'b':
2482 case 'B':
2483 attr = OS_ATTR_BOLD;
2484 break;
2485
2486 case 'i':
2487 case 'I':
2488 attr = OS_ATTR_ITALIC;
2489 break;
2490
2491 case 'e':
2492 case 'E':
2493 attr = OS_ATTR_EM;
2494 break;
2495
2496 case 's':
2497 case 'S':
2498 attr = OS_ATTR_STRONG;
2499 break;
2500
2501 case 't':
2502 case 'T':
2503 attr = OS_ATTR_MONOSP;
2504 break;
2505 }
2506
2507 /* bold on/off - send out appropriate os-layer code */
2508 if (stream->html_in_ignore)
2509 {
2510 /* suppress any change in 'ignore' mode */
2511 }
2512 else if (!is_end_tag)
2513 {
2514 /* turn on the selected attribute */
2515 stream->cur_attr |= attr;
2516 }
2517 else
2518 {
2519 /* turn off the selected attribute */
2520 stream->cur_attr &= ~attr;
2521 }
2522 }
2523 else if (!stricmp(tagbuf, "p"))
2524 {
2525 /* paragraph - send out a blank line */
2526 if (!stream->html_in_ignore)
2527 outblank_stream(stream);
2528 }
2529 else if (!stricmp(tagbuf, "tab"))
2530 {
2531 /* tab - send out a \t */
2532 if (!stream->html_in_ignore)
2533 outtab_stream(stream);
2534 }
2535 else if (!stricmp(tagbuf, "img") || !stricmp(tagbuf, "sound"))
2536 {
2537 /* IMG and SOUND - allow ALT attributes */
2538 stream->html_allow_alt = TRUE;
2539 }
2540 else if (!stricmp(tagbuf, "hr"))
2541 {
2542 int rem;
2543
2544 if (!stream->html_in_ignore)
2545 {
2546 /* start a new line */
2547 outflushn_stream(stream, 1);
2548
2549 /* write out underscores to the display width */
2550 for (rem = G_os_linewidth - 1 ; rem > 0 ; )
2551 {
2552 char dashbuf[100];
2553 int cur;
2554
2555 /* do as much as we can on this pass */
2556 cur = rem;
2557 if ((size_t)cur > sizeof(dashbuf) - 1)
2558 cur = sizeof(dashbuf) - 1;
2559
2560 /* do a buffer-full of dashes */
2561 memset(dashbuf, '_', cur);
2562 dashbuf[cur] = '\0';
2563 outstring_stream(stream, dashbuf);
2564
2565 /* deduct this from the total */
2566 rem -= cur;
2567 }
2568
2569 /* put a blank line after the underscores */
2570 outblank_stream(stream);
2571 }
2572 }
2573 else if (!stricmp(tagbuf, "q"))
2574 {
2575 unsigned int htmlchar;
2576
2577 if (!stream->html_in_ignore)
2578 {
2579 /* if it's an open quote, increment the level */
2580 if (!is_end_tag)
2581 ++(stream->html_quote_level);
2582
2583 /* add the open quote */
2584 htmlchar =
2585 (!is_end_tag
2586 ? ((stream->html_quote_level & 1) == 1
2587 ? 8220 : 8216)
2588 : ((stream->html_quote_level & 1) == 1
2589 ? 8221 : 8217));
2590
2591 /*
2592 * write out the HTML character, translated to
2593 * the local character set
2594 */
2595 outchar_html_stream(stream, htmlchar);
2596
2597 /* if it's a close quote, decrement the level */
2598 if (is_end_tag)
2599 --(stream->html_quote_level);
2600 }
2601 }
2602 else if (!stricmp(tagbuf, "title"))
2603 {
2604 /*
2605 * Turn ignore mode on or off as appropriate, and
2606 * turn on or off title mode as well.
2607 */
2608 if (is_end_tag)
2609 {
2610 /*
2611 * note that we're leaving an ignore section and
2612 * a title section
2613 */
2614 --(stream->html_in_ignore);
2615 --(stream->html_in_title);
2616
2617 /*
2618 * if we're no longer in a title, call the OS
2619 * layer to tell it the title string, in case it
2620 * wants to change the window title or otherwise
2621 * make use of the title
2622 */
2623 if (stream->html_in_title == 0)
2624 {
2625 /* null-terminate the title string */
2626 *stream->html_title_ptr = '\0';
2627
2628 /* tell the OS about the title */
2629 os_set_title(stream->html_title_buf);
2630 }
2631 }
2632 else
2633 {
2634 /*
2635 * if we aren't already in a title, set up to
2636 * capture the title into the title buffer
2637 */
2638 if (!stream->html_in_title)
2639 stream->html_title_ptr = stream->html_title_buf;
2640
2641 /*
2642 * note that we're in a title and in an ignore
2643 * section, since nothing within gets displayed
2644 */
2645 ++(stream->html_in_ignore);
2646 ++(stream->html_in_title);
2647 }
2648 }
2649 else if (!stricmp(tagbuf, "aboutbox"))
2650 {
2651 /* turn ignore mode on or off as appropriate */
2652 if (is_end_tag)
2653 --(stream->html_in_ignore);
2654 else
2655 ++(stream->html_in_ignore);
2656 }
2657 else if (!stricmp(tagbuf, "pre"))
2658 {
2659 if (!is_end_tag) {
2660 /* entering PRE mode: count nesting level, send line break, select monospace font */
2661 stream->html_pre_level += 1;
2662 outblank_stream(stream);
2663 stream->cur_attr |= OS_ATTR_MONOSP;
2664 } else if (stream->html_pre_level > 0) {
2665 /* ending PRE mode: reduce nesting level */
2666 stream->html_pre_level -= 1;
2667 if (stream->html_pre_level == 0)
2668 /* get rid of monospace font if we're completely out of PRE mode */
2669 stream->cur_attr &= (~OS_ATTR_MONOSP);
2670 /* send line break */
2671 outblank_stream(stream);
2672 }
2673 }
2674
2675 /* suppress everything up to the next '>' */
2676 stream->html_mode_flag = HTML_MODE_TAG;
2677
2678 /*
2679 * continue with the current character; since we're in
2680 * html tag mode, we'll skip everything until we get to
2681 * the closing '>'
2682 */
2683 continue;
2684 }
2685 else if (c == '&')
2686 {
2687 /* parse it */
2688 char xlat_buf[50];
2689 c = out_parse_entity(xlat_buf, sizeof(xlat_buf), &s, &slen);
2690
2691 /* write it out (we've already translated it) */
2692 outstring_noxlat_stream(stream, xlat_buf);
2693
2694 /* proceed with the next character */
2695 continue;
2696 }
2697 }
2698 else if (stream->html_target && stream->html_mode && c == '<')
2699 {
2700 /*
2701 * We're in HTML mode, and we have an underlying HTML target.
2702 * We don't need to do much HTML interpretation at this level.
2703 * However, we do need to keep track of when we're in a PRE
2704 * block, so that we can pass whitespaces and newlines through
2705 * to the underlying HTML engine without filtering when we're
2706 * in preformatted text.
2707 */
2708 char tagbuf[50];
2709 int is_end_tag;
2710 c = read_tag(tagbuf, sizeof(tagbuf), &is_end_tag,
2711 &s, &slen, stream);
2712
2713 /* check for special tags */
2714 if (!stricmp(tagbuf, "pre"))
2715 {
2716 /* count the nesting level */
2717 if (!is_end_tag)
2718 stream->html_pre_level += 1;
2719 else if (is_end_tag && stream->html_pre_level != 0)
2720 stream->html_pre_level -= 1;
2721 }
2722
2723 /* copy the last character after the tag to the stream */
2724 outchar_stream(stream, c);
2725 }
2726 else
2727 {
2728 /* normal character */
2729 outchar_stream(stream, c);
2730 }
2731
2732 /* move on to the next character, unless we're finished */
2733 if (done)
2734 c = '\0';
2735 else
2736 c = nextout(&s, &slen);
2737 }
2738
2739 /* if we ended up inside what looked like a format string, dump string */
2740 if (infmt)
2741 {
2742 outchar_stream(stream, '%');
2743 for (f1 = fmsbuf ; f1 < f ; ++f1)
2744 outchar_stream(stream, *f1);
2745 }
2746
2747 /* exit a recursion level */
2748 out_pop_stream();
2749
2750 /* success */
2751 return 0;
2752 }
2753
2754 /* ------------------------------------------------------------------------ */
2755 /*
2756 * Parse an HTML entity markup
2757 */
2758 static char out_parse_entity(char *outbuf, size_t outbuf_size,
2759 char **sp, size_t *slenp)
2760 {
2761 char ampbuf[10];
2762 char *dst;
2763 char *orig_s;
2764 size_t orig_slen;
2765 const struct amp_tbl_t *ampptr;
2766 size_t lo, hi, cur;
2767 char c;
2768
2769 /*
2770 * remember where the part after the '&' begins, so we can come back
2771 * here later if necessary
2772 */
2773 orig_s = *sp;
2774 orig_slen = *slenp;
2775
2776 /* get the character after the ampersand */
2777 c = nextout(sp, slenp);
2778
2779 /* if it's numeric, parse the number */
2780 if (c == '#')
2781 {
2782 uint val;
2783
2784 /* skip the '#' */
2785 c = nextout(sp, slenp);
2786
2787 /* check for hex */
2788 if (c == 'x' || c == 'X')
2789 {
2790 /* skip the 'x' */
2791 c = nextout(sp, slenp);
2792
2793 /* read the hex number */
2794 for (val = 0 ; isxdigit((uchar)c) ; c = nextout(sp, slenp))
2795 {
2796 /* accumulate the current digit into the value */
2797 val *= 16;
2798 if (outisdg(c))
2799 val += c - '0';
2800 else if (c >= 'a' && c <= 'f')
2801 val += c - 'a' + 10;
2802 else
2803 val += c - 'A' + 10;
2804 }
2805 }
2806 else
2807 {
2808 /* read the number */
2809 for (val = 0 ; outisdg(c) ; c = nextout(sp, slenp))
2810 {
2811 /* accumulate the current digit into the value */
2812 val *= 10;
2813 val += c - '0';
2814 }
2815 }
2816
2817 /* if we found a ';' at the end, skip it */
2818 if (c == ';')
2819 c = nextout(sp, slenp);
2820
2821 /* translate the character into the output buffer */
2822 os_xlat_html4(val, outbuf, outbuf_size);
2823
2824 /* we're done with this character */
2825 return c;
2826 }
2827
2828 /*
2829 * Parse the sequence after the '&'. Parse up to the closing
2830 * semicolon, or any non-alphanumeric, or until we fill up the buffer.
2831 */
2832 for (dst = ampbuf ;
2833 c != '\0' && (outisdg(c) || outisal(c))
2834 && dst < ampbuf + sizeof(ampbuf) - 1 ;
2835 *dst++ = c, c = nextout(sp, slenp)) ;
2836
2837 /* null-terminate the name */
2838 *dst = '\0';
2839
2840 /* do a binary search for the name */
2841 lo = 0;
2842 hi = sizeof(amp_tbl)/sizeof(amp_tbl[0]) - 1;
2843 for (;;)
2844 {
2845 int diff;
2846
2847 /* if we've converged, look no further */
2848 if (lo > hi || lo >= sizeof(amp_tbl)/sizeof(amp_tbl[0]))
2849 {
2850 ampptr = 0;
2851 break;
2852 }
2853
2854 /* split the difference */
2855 cur = lo + (hi - lo)/2;
2856 ampptr = &amp_tbl[cur];
2857
2858 /* see where we are relative to the target item */
2859 diff = strcmp(ampptr->cname, ampbuf);
2860 if (diff == 0)
2861 {
2862 /* this is it */
2863 break;
2864 }
2865 else if (diff > 0)
2866 {
2867 /* make sure we don't go off the end */
2868 if (cur == hi && cur == 0)
2869 {
2870 /* we've failed to find it */
2871 ampptr = 0;
2872 break;
2873 }
2874
2875 /* this one is too high - check the lower half */
2876 hi = (cur == hi ? hi - 1 : cur);
2877 }
2878 else
2879 {
2880 /* this one is too low - check the upper half */
2881 lo = (cur == lo ? lo + 1 : cur);
2882 }
2883 }
2884
2885 /* skip to the appropriate next character */
2886 if (c == ';')
2887 {
2888 /* name ended with semicolon - skip the semicolon */
2889 c = nextout(sp, slenp);
2890 }
2891 else if (ampptr != 0)
2892 {
2893 int skipcnt;
2894
2895 /* found the name - skip its exact length */
2896 skipcnt = strlen(ampptr->cname);
2897 for (*sp = orig_s, *slenp = orig_slen ; skipcnt != 0 ;
2898 c = nextout(sp, slenp), --skipcnt) ;
2899 }
2900
2901 /* if we found the entry, write out the character */
2902 if (ampptr != 0)
2903 {
2904 /*
2905 * if this one has an external mapping table entry, use the mapping
2906 * table entry; otherwise, use the default OS routine mapping
2907 */
2908 if (ampptr->expan != 0)
2909 {
2910 /*
2911 * we have an explicit expansion from the mapping table file -
2912 * use it
2913 */
2914 size_t copylen = strlen(ampptr->expan);
2915 if (copylen > outbuf_size - 1)
2916 copylen = outbuf_size - 1;
2917
2918 memcpy(outbuf, ampptr->expan, copylen);
2919 outbuf[copylen] = '\0';
2920 }
2921 else
2922 {
2923 /*
2924 * there's no mapping table expansion - use the default OS code
2925 * expansion
2926 */
2927 os_xlat_html4(ampptr->html_cval, outbuf, outbuf_size);
2928 }
2929 }
2930 else
2931 {
2932 /*
2933 * didn't find it - output the '&' literally, then back up and
2934 * output the entire sequence following
2935 */
2936 *sp = orig_s;
2937 *slenp = orig_slen;
2938 c = nextout(sp, slenp);
2939
2940 /* fill in the '&' return value */
2941 outbuf[0] = '&';
2942 outbuf[1] = '\0';
2943 }
2944
2945 /* return the next character */
2946 return c;
2947 }
2948
2949
2950
2951 /* ------------------------------------------------------------------------ */
2952 /*
2953 * Initialize the output formatter
2954 */
2955 void out_init()
2956 {
2957 /* not yet hiding output */
2958 outflag = 1;
2959 outcnt = 0;
2960 hidout = 0;
2961
2962 /* initialize the standard display stream */
2963 out_init_std(&G_std_disp);
2964
2965 /* initialize the log file stream */
2966 out_init_log(&G_log_disp);
2967 }
2968
2969
2970 /* ------------------------------------------------------------------------ */
2971 /*
2972 * initialize the property translation table
2973 */
2974 void tiosetfmt(tiocxdef *ctx, runcxdef *rctx, uchar *fbase, uint flen)
2975 {
2976 VARUSED(ctx);
2977 fmsbase = fbase;
2978 fmstop = fbase + flen;
2979 runctx = rctx;
2980 }
2981
2982
2983 /* ------------------------------------------------------------------------ */
2984 /*
2985 * Map an HTML entity to a local character value. The character table
2986 * reader will call this routine during initialization if it finds HTML
2987 * entities in the mapping table file. We'll remember these mappings
2988 * for use in translating HTML entities to the local character set.
2989 *
2990 * Note that the standard run-time can only display a single character
2991 * set, so every HTML entity that we display must be mapped to the
2992 * single active native character set.
2993 */
2994 void tio_set_html_expansion(unsigned int html_char_val,
2995 const char *expansion, size_t expansion_len)
2996 {
2997 struct amp_tbl_t *p;
2998
2999 /* find the character value */
3000 for (p = amp_tbl ;
3001 p < amp_tbl + sizeof(amp_tbl)/sizeof(amp_tbl[0]) ; ++p)
3002 {
3003 /* if this is the one, store it */
3004 if (p->html_cval == html_char_val)
3005 {
3006 /* allocate space for it */
3007 p->expan = (char *)osmalloc(expansion_len + 1);
3008
3009 /* save it */
3010 memcpy(p->expan, expansion, expansion_len);
3011 p->expan[expansion_len] = '\0';
3012
3013 /* no need to look any further */
3014 return;
3015 }
3016 }
3017 }
3018
3019
3020 /* ------------------------------------------------------------------------ */
3021 /*
3022 * Write out a c-style (null-terminated) string.
3023 */
3024 int outformat(char *s)
3025 {
3026 return outformatlen(s, strlen(s));
3027 }
3028
3029
3030 /* ------------------------------------------------------------------------ */
3031 /*
3032 * This routine sends out a string, one character at a time (via outchar).
3033 * Escape codes ('\n', and so forth) are handled here.
3034 */
3035 int outformatlen(char *s, uint slen)
3036 {
3037 char c;
3038 uint orig_slen;
3039 char *orig_s;
3040 int ret;
3041 int called_filter;
3042
3043 /* presume we'll return success */
3044 ret = 0;
3045
3046 /* presume we won't call the filter function */
3047 called_filter = FALSE;
3048
3049 /* if there's a user filter function to invoke, call it */
3050 if (G_user_filter != MCMONINV)
3051 {
3052 /* push the string */
3053 runpstr(runctx, s, slen, 1);
3054
3055 /* call the filter */
3056 runfn(runctx, G_user_filter, 1);
3057
3058 /*
3059 * note that we called the filter, so that we'll remove the
3060 * result of the filter from the stack before we return
3061 */
3062 called_filter = TRUE;
3063
3064 /* if the result is a string, use it in place of the original text */
3065 if (runtostyp(runctx) == DAT_SSTRING)
3066 {
3067 runsdef val;
3068 uchar *p;
3069
3070 /* pop the value */
3071 runpop(runctx, &val);
3072
3073 /*
3074 * get the text from the string, and use it as a replacement
3075 * for the original string
3076 */
3077 p = val.runsv.runsvstr;
3078 slen = osrp2(p) - 2;
3079 s = (char *)(p + 2);
3080
3081 /*
3082 * push the string back onto the stack - this will ensure
3083 * that the string stays referenced while we're working, so
3084 * that the garbage collector won't delete it
3085 */
3086 runrepush(runctx, &val);
3087 }
3088 }
3089
3090 /* remember the original string, before we scan the first character */
3091 orig_s = s;
3092 orig_slen = slen;
3093
3094 /* get the first character to display */
3095 c = nextout(&s, &slen);
3096
3097 /* if the string is non-empty, note that we've displayed something */
3098 if (c != 0)
3099 outcnt = 1;
3100
3101 /* check to see if we're hiding output */
3102 if (out_is_hidden())
3103 goto done;
3104
3105 /* if the debugger is showing watchpoints, suppress all output */
3106 if (outwxflag)
3107 goto done;
3108
3109 /* display the string */
3110 ret = outformatlen_stream(&G_std_disp, orig_s, orig_slen);
3111
3112 /* if there's a log file, write to the log file as well */
3113 if (logfp != 0)
3114 {
3115 outformatlen_stream(&G_log_disp, orig_s, orig_slen);
3116 osfflush(logfp);
3117 }
3118
3119 done:
3120 /* if we called the filter, remove the result from the stack */
3121 if (called_filter)
3122 rundisc(runctx);
3123
3124 /* return the result from displaying to the screen */
3125 return ret;
3126 }
3127
3128 /* ------------------------------------------------------------------------ */
3129 /*
3130 * Display a blank line
3131 */
3132 void outblank()
3133 {
3134 /* note that we've displayed something */
3135 outcnt = 1;
3136
3137 /* check to see if we're hiding output */
3138 if (out_is_hidden())
3139 return;
3140
3141 /* generate the newline to the standard display */
3142 outblank_stream(&G_std_disp);
3143
3144 /* if we're logging, generate the newline to the log file as well */
3145 if (logfp != 0)
3146 {
3147 outblank_stream(&G_log_disp);
3148 osfflush(logfp);
3149 }
3150 }
3151
3152
3153 /* ------------------------------------------------------------------------ */
3154 /*
3155 * outcaps() - sets an internal flag which makes the next letter output
3156 * a capital, whether it came in that way or not. Set the same state in
3157 * both formatters (standard and log).
3158 */
3159 void outcaps(void)
3160 {
3161 outcaps_stream(&G_std_disp);
3162 outcaps_stream(&G_log_disp);
3163 }
3164
3165 /*
3166 * outnocaps() - sets the next letter to a miniscule, whether it came in
3167 * that way or not.
3168 */
3169 void outnocaps(void)
3170 {
3171 outnocaps_stream(&G_std_disp);
3172 outnocaps_stream(&G_log_disp);
3173 }
3174
3175 /* ------------------------------------------------------------------------ */
3176 /*
3177 * Open a log file
3178 */
3179 int tiologopn(tiocxdef *ctx, char *fn)
3180 {
3181 /* if there's an old log file, close it */
3182 if (tiologcls(ctx))
3183 return 1;
3184
3185 /* save the filename for later */
3186 strcpy(logfname, fn);
3187
3188 /* open the new file */
3189 logfp = osfopwt(fn, OSFTLOG);
3190
3191 /*
3192 * Reset the log file's output formatter state, since we're opening
3193 * a new file.
3194 */
3195 out_init_log(&G_log_disp);
3196
3197 /*
3198 * Set the log file's HTML source mode flag to the same value as is
3199 * currently being used in the main display stream, so that it will
3200 * interpret source markups the same way that the display stream is
3201 * going to.
3202 */
3203 G_log_disp.html_mode = G_std_disp.html_mode;
3204
3205 /* return 0 on success, non-zero on failure */
3206 return (logfp == 0);
3207 }
3208
3209 /*
3210 * Close the log file
3211 */
3212 int tiologcls(tiocxdef *ctx)
3213 {
3214 /* if we have a file, close it */
3215 if (logfp != 0)
3216 {
3217 /* close the handle */
3218 osfcls(logfp);
3219
3220 /* set the system file type to "log file" */
3221 os_settype(logfname, OSFTLOG);
3222
3223 /* forget about our log file handle */
3224 logfp = 0;
3225 }
3226
3227 /* success */
3228 return 0;
3229 }
3230
3231 /* ------------------------------------------------------------------------ */
3232 /*
3233 * Write text explicitly to the log file. This can be used to add
3234 * special text (such as prompt text) that would normally be suppressed
3235 * from the log file. When more mode is turned off, we don't
3236 * automatically copy text to the log file; any text that the caller
3237 * knows should be in the log file during times when more mode is turned
3238 * off can be explicitly added with this function.
3239 *
3240 * If nl is true, we'll add a newline at the end of this text. The
3241 * caller should not include any newlines in the text being displayed
3242 * here.
3243 */
3244 void out_logfile_print(char *txt, int nl)
3245 {
3246 /* if there's no log file, there's nothing to do */
3247 if (logfp == 0)
3248 return;
3249
3250 /* add the text */
3251 os_fprintz(logfp, txt);
3252
3253 /* add a newline if desired */
3254 if (nl)
3255 {
3256 /* add a normal newline */
3257 os_fprintz(logfp, "\n");
3258
3259 /* if the logfile is an html target, write an HTML line break */
3260 if (G_log_disp.html_target && G_log_disp.html_mode)
3261 os_fprintz(logfp, "<BR HEIGHT=0>\n");
3262 }
3263
3264 /* flush the output */
3265 osfflush(logfp);
3266 }
3267
3268 /* ------------------------------------------------------------------------ */
3269 /*
3270 * Set the current MORE mode
3271 */
3272 int setmore(int state)
3273 {
3274 int oldstate = G_os_moremode;
3275
3276 G_os_moremode = state;
3277 return oldstate;
3278 }
3279
3280 /* ------------------------------------------------------------------------ */
3281 /*
3282 * Run the MORE prompt. If the output layer takes responsibility for
3283 * pagination issues (i.e., USE_MORE is defined), we'll simply display
3284 * the prompt and wait for input. Otherwise, the OS layer controls the
3285 * MORE prompt, so we'll call the OS-layer function to display the
3286 * prompt.
3287 */
3288 void out_more_prompt()
3289 {
3290 #ifdef USE_MORE
3291 /*
3292 * USE_MORE defined - we take responsibility for pagination. Show
3293 * our default MORE prompt and wait for a keystroke.
3294 */
3295
3296 int done;
3297 int next_page;
3298
3299 /* display the "MORE" prompt */
3300 os_printz("[More]");
3301 os_flush();
3302
3303 /* wait for an acceptable keystroke */
3304 for (done = FALSE ; !done ; )
3305 {
3306 os_event_info_t evt;
3307
3308 /* get an event */
3309 switch(os_get_event(0, FALSE, &evt))
3310 {
3311 case OS_EVT_KEY:
3312 switch(evt.key[0])
3313 {
3314 case ' ':
3315 /* stop waiting, show one page */
3316 done = TRUE;
3317 next_page = TRUE;
3318 break;
3319
3320 case '\r':
3321 case '\n':
3322 /* stop waiting, show one line */
3323 done = TRUE;
3324 next_page = FALSE;
3325 break;
3326
3327 default:
3328 /* ignore any other keystrokes */
3329 break;
3330 }
3331 break;
3332
3333 case OS_EVT_EOF:
3334 /* end of file - there's nothing to wait for now */
3335 done = TRUE;
3336 next_page = TRUE;
3337
3338 /* don't use more prompts any more, as the user can't respond */
3339 G_os_moremode = FALSE;
3340 break;
3341
3342 default:
3343 /* ignore other events */
3344 break;
3345 }
3346 }
3347
3348 /*
3349 * Remove the prompt from the screen by backing up and overwriting
3350 * it with spaces. (Note that this assumes that we're running in
3351 * some kind of terminal or character mode with a fixed-pitch font;
3352 * if that's not the case, the OS layer should be taking
3353 * responsibility for pagination anyway, so this code shouldn't be
3354 * in use in the first place.)
3355 */
3356 os_printz("\r \r");
3357
3358 /*
3359 * if they pressed the space key, it means that we should show an
3360 * entire new page, so reset the line count to zero; otherwise,
3361 * we'll want to display another MORE prompt at the very next line,
3362 * so leave the line count alone
3363 */
3364 if (next_page)
3365 G_std_disp.linecnt = 0;
3366
3367 #else /* USE_MORE */
3368
3369 /*
3370 * USE_MORE is undefined - this means that the OS layer is taking
3371 * all responsibility for pagination. We must ask the OS layer to
3372 * display the MORE prompt, because we can't make any assumptions
3373 * about what the prompt looks like.
3374 */
3375
3376 os_more_prompt();
3377
3378 #endif /* USE_MORE */
3379 }
3380
3381 /* ------------------------------------------------------------------------ */
3382 /*
3383 * reset output
3384 */
3385 void outreset(void)
3386 {
3387 G_std_disp.linecnt = 0;
3388 }
3389
3390 /* ------------------------------------------------------------------------ */
3391 /*
3392 * Determine if HTML mode is active. Returns true if so, false if not.
3393 * Note that this merely indicates whether an "\H+" sequence is
3394 * currently active -- this will return true after an "\H+" sequence,
3395 * even on text-only interpreters.
3396 */
3397 int tio_is_html_mode()
3398 {
3399 /* return the current HTML mode flag for the standard display stream */
3400 return G_std_disp.html_mode;
3401 }
3402
3403
3404 /* ------------------------------------------------------------------------ */
3405 /*
3406 * Capture routines. Capture affects only the standard display output
3407 * stream; there's no need to capture information redundantly in the log
3408 * file stream.
3409 */
3410
3411 /*
3412 * Begin/end capturing
3413 */
3414 void tiocapture(tiocxdef *tioctx, mcmcxdef *memctx, int flag)
3415 {
3416 if (flag)
3417 {
3418 /* create a new object if necessary */
3419 if (G_std_disp.capture_obj == MCMONINV)
3420 {
3421 mcmalo(memctx, 256, &G_std_disp.capture_obj);
3422 mcmunlck(memctx, G_std_disp.capture_obj);
3423 }
3424
3425 /* remember the memory context */
3426 G_std_disp.capture_ctx = memctx;
3427 }
3428
3429 /*
3430 * remember capture status in the standard output stream as well as
3431 * the log stream
3432 */
3433 G_std_disp.capturing = flag;
3434 G_log_disp.capturing = flag;
3435 }
3436
3437 /* clear all captured output */
3438 void tioclrcapture(tiocxdef *tioctx)
3439 {
3440 G_std_disp.capture_ofs = 0;
3441 }
3442
3443 /* clear captured output back to a given size */
3444 void tiopopcapture(tiocxdef *tioctx, uint orig_size)
3445 {
3446 G_std_disp.capture_ofs = orig_size;
3447 }
3448
3449 /* get the object handle of the captured output */
3450 mcmon tiogetcapture(tiocxdef *ctx)
3451 {
3452 return G_std_disp.capture_obj;
3453 }
3454
3455 /* get the amount of text captured */
3456 uint tiocapturesize(tiocxdef *ctx)
3457 {
3458 return G_std_disp.capture_ofs;
3459 }
3460
3461 /* ------------------------------------------------------------------------ */
3462 /*
3463 * set the current actor
3464 */
3465 void tiosetactor(tiocxdef *ctx, objnum actor)
3466 {
3467 VARUSED(ctx);
3468 cmdActor = actor;
3469 }
3470
3471 /*
3472 * get the current actor
3473 */
3474 objnum tiogetactor(tiocxdef *ctx)
3475 {
3476 VARUSED(ctx);
3477 return cmdActor;
3478 }
3479
3480 /* ------------------------------------------------------------------------ */
3481 /*
3482 * Flush the output line. We'll write to both the standard display and
3483 * the log file, as needed.
3484 */
3485 void outflushn(int nl)
3486 {
3487 /* flush the display stream */
3488 outflushn_stream(&G_std_disp, nl);
3489
3490 /* flush the log stream, if we have an open log file */
3491 if (logfp != 0)
3492 {
3493 outflushn_stream(&G_log_disp, nl);
3494 osfflush(logfp);
3495 }
3496 }
3497
3498 /*
3499 * flush the current line, and start a new line
3500 */
3501 void outflush(void)
3502 {
3503 /* use the common flushing routine in mode 1 (regular newline) */
3504 outflushn(1);
3505 }
3506
3507 /* ------------------------------------------------------------------------ */
3508 /*
3509 * Hidden text routines
3510 */
3511
3512 /*
3513 * outhide - hide output in the standard display stream
3514 */
3515 void outhide(void)
3516 {
3517 outflag = 0;
3518 outcnt = 0;
3519 hidout = 0;
3520 }
3521
3522 /*
3523 * Check output status. Indicate whether output is currently hidden,
3524 * and whether any hidden output has occurred.
3525 */
3526 void outstat(int *hidden, int *output_occurred)
3527 {
3528 *hidden = !outflag;
3529 *output_occurred = outcnt;
3530 }
3531
3532 /* set the flag to indicate that output has occurred */
3533 void outsethidden(void)
3534 {
3535 outcnt = 1;
3536 hidout = 1;
3537 }
3538
3539 /*
3540 * outshow() - turns output back on, and returns TRUE (1) if any output
3541 * has occurred since the last outshow(), FALSE (0) otherwise.
3542 */
3543 int outshow(void)
3544 {
3545 /* turn output back on */
3546 outflag = 1;
3547
3548 /* if we're debugging, note the end of hidden output */
3549 if (dbghid && hidout)
3550 {
3551 hidout = 0;
3552 trcsho();
3553 }
3554
3555 /* return the flag indicating whether hidden output occurred */
3556 return outcnt;
3557 }
3558
3559 /* ------------------------------------------------------------------------ */
3560 /*
3561 * start/end watchpoint evaluation - suppress all dstring output
3562 */
3563 void outwx(int flag)
3564 {
3565 outwxflag = flag;
3566 }
3567
3568
3569 /* ------------------------------------------------------------------------ */
3570 /*
3571 * Set the user filter function. Setting this to MCMONINV clears the
3572 * filter.
3573 */
3574 void out_set_filter(objnum filter_fn)
3575 {
3576 /* remember the filter function */
3577 G_user_filter = filter_fn;
3578 }
3579
3580 /* ------------------------------------------------------------------------ */
3581 /*
3582 * Set the double-space mode
3583 */
3584 void out_set_doublespace(int dbl)
3585 {
3586 /* remember the new setting */
3587 doublespace = dbl;
3588 }
3589