Debian Codegraph @

terps/tads/tads3/vmconhmp.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2002 by Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 vmconhmp.cpp - T3 VM Console - HTML mini-parser
10 Function
11 This is the console HTML "mini-parser," which provides some minimal
12 interpretation of HTML markups for text-only underlying output streams.
13
14 Note that the mini-parser is included even in full HTML-enabled versions
15 of TADS (such as HTML TADS or HyperTADS), because we still need text-only
16 interpretation of HTML markups for log-file targets.
17 Notes
18
19 Modified
20 06/08/02 MJRoberts - Creation
21 */
22
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <assert.h>
29 #include "wchar.h"
30
31 #include "os.h"
32 #include "t3std.h"
33 #include "utf8.h"
34 #include "vmuni.h"
35 #include "vmconsol.h"
36 #include "vmglob.h"
37 #include "charmap.h"
38 #include "vmhash.h"
39
40
41 /* ------------------------------------------------------------------------ */
42 /*
43 * Horizontal Tab object. We use these objects to record tab stops
44 * defined with <TAB ID=xxx> in our HTML mini-parser.
45 *
46 * This is a hash table entry subclass because we use a hash table of tab
47 * entries, keyed by ID strings. Note that we use a case-sensitive hash
48 * table entry - clients must convert ID's to lower-case if they don't
49 * want case sensitivity.
50 *
51 * Note that this code looks a bit cavalier with unicode vs ASCII, but
52 * this is actually safe. The underlying hash table code uses
53 * counted-length strings, so it doesn't care about embedded null bytes.
54 * We therefore can pass a wchar_t buffer to the underlying hash table,
55 * even though the underlying hash table uses char's, because we multiply
56 * our length by the size of a wchar_t to get the byte length.
57 */
58 class CVmFmtTabStop: public CVmHashEntryCS
59 {
60 public:
61 CVmFmtTabStop(const char *id, size_t idlen)
62 : CVmHashEntryCS(id, idlen, TRUE)
63 {
64 /* we don't know our column yet */
65 column_ = 0;
66 }
67
68 /* the column where the tab is defined */
69 int column_;
70
71 /*
72 * Find a tabstop object with a given ID, optionally creating a new
73 * object if an existing one isn't found.
74 */
75 static CVmFmtTabStop *find(CVmHashTable *hashtab, wchar_t *id, int create)
76 {
77 /*
78 * Get the length - in characters AND in bytes - of the new ID.
79 * We mostly work with the new ID in bytes rather than wchar_t's,
80 * because the underlying hash table does everything in bytes.
81 */
82
83 /*
84 * Convert the new ID to folded case. The hash table doesn't deal
85 * with unicode case conversions, so we use a case-sensitive hash
86 * table and simply make sure we only give it lower-case
87 * characters.
88 */
89 size_t flen = fold_case(0, id);
90 wchar_t *fid = 0;
91 if (flen != 0)
92 fid = new wchar_t[flen+1];
93
94 CVmFmtTabStop *entry = 0;
95 err_try
96 {
97 /* do the folding if applicable */
98 if (fid != 0)
99 {
100 fold_case(fid, id);
101 id = fid;
102 }
103
104 /* get the folded ID length in characters and bytes */
105 size_t id_chars = wcslen(id);
106 size_t id_bytes = id_chars * sizeof(wchar_t);
107
108 /* look for an existing tab-stop entry with the same ID */
109 entry = (CVmFmtTabStop *)hashtab->find((char *)id, id_bytes);
110
111 /* if we didn't find it, and they want to create it, do so */
112 if (entry == 0 && create)
113 hashtab->add(entry = new CVmFmtTabStop((char *)id, id_bytes));
114 }
115 err_finally
116 {
117 /* if we allocated the ID string, release it */
118 if (fid != 0)
119 delete [] fid;
120 }
121 err_end;
122
123 /* return what we found */
124 return entry;
125 }
126
127 /* get the case folding of a string */
128 static size_t fold_case(wchar_t *dst, const wchar_t *str)
129 {
130 size_t dstlen = 0;
131 for ( ; *str != 0 ; ++str)
132 {
133 /*
134 * get the case folding of the current character, defaulting to
135 * an identity mapping if there's no folding defined
136 */
137 const wchar_t *f = t3_to_fold(*str);
138 wchar_t fi[2] = { *str, 0 };
139 if (f == 0)
140 f = fi;
141
142 /* copy it to the output if there's an output buffer */
143 for ( ; *f != 0 ; ++f, ++dstlen)
144 {
145 if (dst != 0)
146 *dst++ = *f;
147 }
148 }
149
150 /* add the null */
151 if (dst != 0)
152 *dst = 0;
153
154 /* return the length (excluding the null) */
155 return dstlen;
156 }
157 };
158
159
160 /* ------------------------------------------------------------------------ */
161 /*
162 * HTML entity mapping table. When we're in non-HTML mode, we keep our own
163 * expansion table so that we can map HTML entity names into the local
164 * character set.
165 *
166 * The entries in this table must be in sorted order (by HTML entity name),
167 * because we use a binary search to find an entity name in the table.
168 */
169
170 /*
171 * '&' character name definition structure
172 */
173 struct amp_tbl_t
174 {
175 /* entity name */
176 const char *cname;
177
178 /* HTML Unicode character value */
179 wchar_t html_cval;
180 };
181
182 /*
183 * table of '&' character name sequences
184 */
185 static const struct amp_tbl_t amp_tbl[] =
186 {
187 { "AElig", 198 },
188 { "Aacute", 193 },
189 { "Abreve", 258 },
190 { "Acirc", 194 },
191 { "Agrave", 192 },
192 { "Alpha", 913 },
193 { "Aogon", 260 },
194 { "Aring", 197 },
195 { "Atilde", 195 },
196 { "Auml", 196 },
197 { "Beta", 914 },
198 { "Cacute", 262 },
199 { "Ccaron", 268 },
200 { "Ccedil", 199 },
201 { "Chi", 935 },
202 { "Dagger", 8225 },
203 { "Dcaron", 270 },
204 { "Delta", 916 },
205 { "Dstrok", 272 },
206 { "ETH", 208 },
207 { "Eacute", 201 },
208 { "Ecaron", 282 },
209 { "Ecirc", 202 },
210 { "Egrave", 200 },
211 { "Eogon", 280 },
212 { "Epsilon", 917 },
213 { "Eta", 919 },
214 { "Euml", 203 },
215 { "Gamma", 915 },
216 { "Iacute", 205 },
217 { "Icirc", 206 },
218 { "Igrave", 204 },
219 { "Iota", 921 },
220 { "Iuml", 207 },
221 { "Kappa", 922 },
222 { "Lacute", 313 },
223 { "Lambda", 923 },
224 { "Lcaron", 317 },
225 { "Lstrok", 321 },
226 { "Mu", 924 },
227 { "Nacute", 323 },
228 { "Ncaron", 327 },
229 { "Ntilde", 209 },
230 { "Nu", 925 },
231 { "OElig", 338 },
232 { "Oacute", 211 },
233 { "Ocirc", 212 },
234 { "Odblac", 336 },
235 { "Ograve", 210 },
236 { "Omega", 937 },
237 { "Omicron", 927 },
238 { "Oslash", 216 },
239 { "Otilde", 213 },
240 { "Ouml", 214 },
241 { "Phi", 934 },
242 { "Pi", 928 },
243 { "Prime", 8243 },
244 { "Psi", 936 },
245 { "Racute", 340 },
246 { "Rcaron", 344 },
247 { "Rho", 929 },
248 { "Sacute", 346 },
249 { "Scaron", 352 },
250 { "Scedil", 350 },
251 { "Sigma", 931 },
252 { "THORN", 222 },
253 { "Tau", 932 },
254 { "Tcaron", 356 },
255 { "Tcedil", 354 },
256 { "Theta", 920 },
257 { "Uacute", 218 },
258 { "Ucirc", 219 },
259 { "Udblac", 368 },
260 { "Ugrave", 217 },
261 { "Upsilon", 933 },
262 { "Uring", 366 },
263 { "Uuml", 220 },
264 { "Xi", 926 },
265 { "Yacute", 221 },
266 { "Yuml", 376 },
267 { "Zacute", 377 },
268 { "Zcaron", 381 },
269 { "Zdot", 379 },
270 { "Zeta", 918 },
271 { "aacute", 225 },
272 { "abreve", 259 },
273 { "acirc", 226 },
274 { "acute", 180 },
275 { "aelig", 230 },
276 { "agrave", 224 },
277 { "alefsym", 8501 },
278 { "alpha", 945 },
279 { "amp", '&' },
280 { "and", 8743 },
281 { "ang", 8736 },
282 { "aogon", 261 },
283 { "aring", 229 },
284 { "asymp", 8776 },
285 { "atilde", 227 },
286 { "auml", 228 },
287 { "bdquo", 8222 },
288 { "beta", 946 },
289 { "breve", 728 },
290 { "brvbar", 166 },
291 { "bull", 8226 },
292 { "cacute", 263 },
293 { "cap", 8745 },
294 { "caron", 711 },
295 { "ccaron", 269 },
296 { "ccedil", 231 },
297 { "cedil", 184 },
298 { "cent", 162 },
299 { "chi", 967 },
300 { "circ", 710 },
301 { "clubs", 9827 },
302 { "cong", 8773 },
303 { "copy", 169 },
304 { "crarr", 8629 },
305 { "cup", 8746 },
306 { "curren", 164 },
307 { "dArr", 8659 },
308 { "dagger", 8224 },
309 { "darr", 8595 },
310 { "dblac", 733 },
311 { "dcaron", 271 },
312 { "deg", 176 },
313 { "delta", 948 },
314 { "diams", 9830 },
315 { "divide", 247 },
316 { "dot", 729 },
317 { "dstrok", 273 },
318 { "eacute", 233 },
319 { "ecaron", 283 },
320 { "ecirc", 234 },
321 { "egrave", 232 },
322 { "emdash", 8212 },
323 { "empty", 8709 },
324 { "emsp", 0x2003 },
325 { "endash", 8211 },
326 { "ensp", 0x2002 },
327 { "eogon", 281 },
328 { "epsilon", 949 },
329 { "equiv", 8801 },
330 { "eta", 951 },
331 { "eth", 240 },
332 { "euml", 235 },
333 { "exist", 8707 },
334 { "figsp", 0x2007 },
335 { "fnof", 402 },
336 { "forall", 8704 },
337 { "fpmsp", 0x2005 },
338 { "frac12", 189 },
339 { "frac14", 188 },
340 { "frac34", 190 },
341 { "frasl", 8260 },
342 { "gamma", 947 },
343 { "ge", 8805 },
344 { "gt", '>' },
345 { "hArr", 8660 },
346 { "hairsp", 0x200a },
347 { "harr", 8596 },
348 { "hearts", 9829 },
349 { "hellip", 8230 },
350 { "iacute", 237 },
351 { "icirc", 238 },
352 { "iexcl", 161 },
353 { "igrave", 236 },
354 { "image", 8465 },
355 { "infin", 8734 },
356 { "int", 8747 },
357 { "iota", 953 },
358 { "iquest", 191 },
359 { "isin", 8712 },
360 { "iuml", 239 },
361 { "kappa", 954 },
362 { "lArr", 8656 },
363 { "lacute", 314 },
364 { "lambda", 955 },
365 { "lang", 9001 },
366 { "laquo", 171 },
367 { "larr", 8592 },
368 { "lcaron", 318 },
369 { "lceil", 8968 },
370 { "ldq", 8220 },
371 { "ldquo", 8220 },
372 { "le", 8804 },
373 { "lfloor", 8970 },
374 { "lowast", 8727 },
375 { "loz", 9674 },
376 { "lsaquo", 8249 },
377 { "lsq", 8216 },
378 { "lsquo", 8216 },
379 { "lstrok", 322 },
380 { "lt", '<' },
381 { "macr", 175 },
382 { "mdash", 8212 },
383 { "micro", 181 },
384 { "middot", 183 },
385 { "minus", 8722 },
386 { "mu", 956 },
387 { "nabla", 8711 },
388 { "nacute", 324 },
389 { "nbsp", 0x00A0 }, /* == 160 decimal */
390 { "ncaron", 328 },
391 { "ndash", 8211 },
392 { "ne", 8800 },
393 { "ni", 8715 },
394 { "not", 172 },
395 { "notin", 8713 },
396 { "nsub", 8836 },
397 { "ntilde", 241 },
398 { "nu", 957 },
399 { "oacute", 243 },
400 { "ocirc", 244 },
401 { "odblac", 337 },
402 { "oelig", 339 },
403 { "ogon", 731 },
404 { "ograve", 242 },
405 { "oline", 8254 },
406 { "omega", 969 },
407 { "omicron", 959 },
408 { "oplus", 8853 },
409 { "or", 8744 },
410 { "ordf", 170 },
411 { "ordm", 186 },
412 { "oslash", 248 },
413 { "otilde", 245 },
414 { "otimes", 8855 },
415 { "ouml", 246 },
416 { "para", 182 },
417 { "part", 8706 },
418 { "permil", 8240 },
419 { "perp", 8869 },
420 { "phi", 966 },
421 { "pi", 960 },
422 { "piv", 982 },
423 { "plusmn", 177 },
424 { "pound", 163 },
425 { "prime", 8242 },
426 { "prod", 8719 },
427 { "prop", 8733 },
428 { "psi", 968 },
429 { "puncsp", 0x2008 },
430 { "quot", '"' },
431 { "rArr", 8658 },
432 { "racute", 341 },
433 { "radic", 8730 },
434 { "rang", 9002 },
435 { "raquo", 187 },
436 { "rarr", 8594 },
437 { "rcaron", 345 },
438 { "rceil", 8969 },
439 { "rdq", 8221 },
440 { "rdquo", 8221 },
441 { "real", 8476 },
442 { "reg", 174 },
443 { "rfloor", 8971 },
444 { "rho", 961 },
445 { "rsaquo", 8250 },
446 { "rsq", 8217 },
447 { "rsquo", 8217 },
448 { "sacute", 347 },
449 { "sbquo", 8218 },
450 { "scaron", 353 },
451 { "scedil", 351 },
452 { "sdot", 8901 },
453 { "sect", 167 },
454 { "shy", 173 },
455 { "sigma", 963 },
456 { "sigmaf", 962 },
457 { "sim", 8764 },
458 { "spades", 9824 },
459 { "spmsp", 0x2006 },
460 { "sub", 8834 },
461 { "sube", 8838 },
462 { "sum", 8721 },
463 { "sup", 8835 },
464 { "sup1", 185 },
465 { "sup2", 178 },
466 { "sup3", 179 },
467 { "supe", 8839 },
468 { "szlig", 223 },
469 { "tau", 964 },
470 { "tcaron", 357 },
471 { "tcedil", 355 },
472 { "there4", 8756 },
473 { "theta", 952 },
474 { "thetasym", 977 },
475 { "thinsp", 0x2009 },
476 { "thorn", 254 },
477 { "tilde", 732 },
478 { "times", 215 },
479 { "tpmsp", 0x2004 },
480 { "trade", 8482 },
481 { "uArr", 8657 },
482 { "uacute", 250 },
483 { "uarr", 8593 },
484 { "ucirc", 251 },
485 { "udblac", 369 },
486 { "ugrave", 249 },
487 { "uml", 168 },
488 { "upsih", 978 },
489 { "upsilon", 965 },
490 { "uring", 367 },
491 { "uuml", 252 },
492 { "weierp", 8472 },
493 { "xi", 958 },
494 { "yacute", 253 },
495 { "yen", 165 },
496 { "yuml", 255 },
497 { "zacute", 378 },
498 { "zcaron", 382 },
499 { "zdot", 380 },
500 { "zeta", 950 },
501 { "zwnbsp", 0xfeff },
502 { "zwsp", 0x200b }
503 };
504
505 /*
506 * Color names. For text-mode interpreters, we parse certain tag
507 * attributes that can specify colors, so we must recognize the basic set
508 * of HTML color names for these attributes.
509 */
510 struct color_tbl_t
511 {
512 /* name of the color */
513 const wchar_t *name;
514
515 /* osifc color code */
516 os_color_t color_code;
517 };
518
519 static const color_tbl_t color_tbl[] =
520 {
521 { L"black", os_rgb_color(0x00, 0x00, 0x00) },
522 { L"white", os_rgb_color(0xFF, 0xFF, 0xFF) },
523 { L"red", os_rgb_color(0xFF, 0x00, 0x00) },
524 { L"blue", os_rgb_color(0x00, 0x00, 0xFF) },
525 { L"green", os_rgb_color(0x00, 0x80, 0x00) },
526 { L"yellow", os_rgb_color(0xFF, 0xFF, 0x00) },
527 { L"cyan", os_rgb_color(0x00, 0xFF, 0xFF) },
528 { L"aqua", os_rgb_color(0x00, 0xFF, 0xFF) },
529 { L"magenta", os_rgb_color(0xFF, 0x00, 0xFF) },
530 { L"silver", os_rgb_color(0xC0, 0xC0, 0xC0) },
531 { L"gray", os_rgb_color(0x80, 0x80, 0x80) },
532 { L"maroon", os_rgb_color(0x80, 0x00, 0x00) },
533 { L"purple", os_rgb_color(0x80, 0x00, 0x80) },
534 { L"fuchsia", os_rgb_color(0xFF, 0x00, 0xFF) },
535 { L"lime", os_rgb_color(0x00, 0xFF, 0x00) },
536 { L"olive", os_rgb_color(0x80, 0x80, 0x00) },
537 { L"navy", os_rgb_color(0x00, 0x00, 0x80) },
538 { L"teal", os_rgb_color(0x00, 0x80, 0x80) },
539
540 /* the parameterized colors */
541 { L"text", OS_COLOR_P_TEXT },
542 { L"bgcolor", OS_COLOR_P_TEXTBG },
543 { L"statustext", OS_COLOR_P_STATUSLINE },
544 { L"statusbg", OS_COLOR_P_STATUSBG },
545 { L"input", OS_COLOR_P_INPUT },
546
547 /* end-of-table marker */
548 { 0, 0 }
549 };
550
551
552
553 /* ------------------------------------------------------------------------ */
554 /*
555 * Parse the COLOR attribute of a FONT tag. Sets *result to the parsed
556 * color value. Returns true if we found a valid value, false if not.
557 */
558 int CVmFormatter::parse_color_attr(VMG_ const wchar_t *val,
559 os_color_t *result)
560 {
561 const color_tbl_t *p;
562
563 /*
564 * if the value starts with '#', it's a hex RGB value; otherwise, it's
565 * a color name
566 */
567 if (*val == '#')
568 {
569 unsigned long hexval;
570
571 /* parse the value as a hex number */
572 for (hexval = 0, ++val ; *val != '\0' && is_xdigit(*val) ; ++val)
573 {
574 /* add this digit */
575 hexval <<= 4;
576 hexval += (t3_is_digit(*val)
577 ? *val - '0'
578 : *val >= 'a' && *val <= 'f'
579 ? *val - 'a' + 0x0A
580 : *val - 'A' + 0x0A);
581 }
582
583 /*
584 * os_color_t uses the same encoding as HTML, so just return the
585 * value as given (ensuring that it doesn't overflow the 24 bits
586 * assigned to our RGB encoding)
587 */
588 *result = (os_color_t)(hexval & 0x00FFFFFF);
589 return TRUE;
590 }
591 else
592 {
593 /* scan the color table for a match for the name */
594 for (p = color_tbl ; p->name != 0 ; ++p)
595 {
596 /* if the name matches, use it */
597 if (CVmCaseFoldStr::wstreq(val, p->name))
598 {
599 /* this is the color - make it the current buffer color */
600 *result = p->color_code;
601
602 /* indicate that we found a valid color value */
603 return TRUE;
604 }
605 }
606 }
607
608 /* indicate that we didn't find a valid color value */
609 return FALSE;
610 }
611
612 /* ------------------------------------------------------------------------ */
613 /*
614 * Find a tab
615 */
616 CVmFmtTabStop *CVmFormatter::find_tab(wchar_t *id, int create)
617 {
618 /* if we don't have a hash table yet, create one */
619 if (tabs_ == 0)
620 tabs_ = new CVmHashTable(64, new CVmHashFuncCS(), TRUE);
621
622 /* find the tab */
623 return CVmFmtTabStop::find(tabs_, id, create);
624 }
625
626 /* ------------------------------------------------------------------------ */
627 /*
628 * Expand the pending tab.
629 */
630 void CVmFormatter::expand_pending_tab(VMG_ int allow_anon)
631 {
632 int txtlen;
633 int spaces;
634 int i;
635 int col;
636
637 /* check the alignment */
638 switch(pending_tab_align_)
639 {
640 case VMFMT_TAB_LEFT:
641 /*
642 * left-aligned tabs are never pending, so this should never
643 * happen
644 */
645 assert(FALSE);
646 return;
647
648 case VMFMT_TAB_NONE:
649 /* there's no pending tab, so there's nothing to do */
650 return;
651
652 case VMFMT_TAB_RIGHT:
653 /*
654 * It's a right-aligned tab. If we have a TO, expand the tab with
655 * enough spaces to push the text up to the defined tab's column;
656 * otherwise, insert spaces to push the text to the full line
657 * width.
658 */
659 if (pending_tab_entry_ != 0)
660 {
661 /*
662 * if we're not past the tab, insert enough spaces to get us
663 * there; if we're already past the tab, insert nothing
664 */
665 if (pending_tab_entry_->column_ > linecol_)
666 spaces = pending_tab_entry_->column_ - linecol_;
667 else
668 spaces = 0;
669 }
670 else if (allow_anon)
671 {
672 /* push the text to the full line width */
673 spaces = console_->get_line_width() - 1 - linecol_;
674 }
675 else
676 {
677 /* it's anonymous, but these aren't allowed - ignore it */
678 spaces = 0;
679 }
680 break;
681
682 case VMFMT_TAB_CENTER:
683 /*
684 * It's a center-aligned tab. If we have a TO, expand the tab
685 * with enough spaces to center the text on the defined tab's
686 * column; otherwise, center the text between the tab's starting
687 * column and the full line width.
688 */
689 txtlen = linecol_ - pending_tab_start_;
690 if (pending_tab_entry_ != 0)
691 {
692 int startcol;
693
694 /*
695 * find the column where the text would have to start for the
696 * text to be centered over the tab's column
697 */
698 startcol = pending_tab_entry_->column_ - (txtlen / 2);
699
700 /*
701 * if that's past the starting column, insert enough spaces to
702 * get us there; otherwise, do nothing, as we'd have to start
703 * to the left of the leftmost place we can put the text
704 */
705 if (startcol > pending_tab_start_)
706 spaces = startcol - pending_tab_start_;
707 else
708 spaces = 0;
709 }
710 else if (allow_anon)
711 {
712 /* center the text in the remaining space to the line width */
713 spaces = ((console_->get_line_width() - pending_tab_start_)
714 - txtlen) / 2;
715 }
716 else
717 {
718 /* it's anonymous, but these aren't allowed - ignore it */
719 spaces = 0;
720 }
721 break;
722
723 case VMFMT_TAB_DECIMAL:
724 /*
725 * Decimal tab. Scan the text after the tab, looking for the
726 * first occurrence of the decimal point character.
727 */
728 for (col = pending_tab_start_, i = linepos_ - (linecol_ - col) ;
729 i < linepos_ ; ++i, ++col)
730 {
731 /* if this is the decimal point character, stop looking */
732 if (linebuf_[i] == pending_tab_dp_)
733 break;
734 }
735
736 /*
737 * insert enough spaces to put the decimal point character in the
738 * defined tab's column; if the decimal point character is already
739 * past the tab's column, do nothing
740 */
741 if (pending_tab_entry_->column_ > col)
742 spaces = pending_tab_entry_->column_ - col;
743 else
744 spaces = 0;
745 break;
746 }
747
748 /* insert the spaces into the buffer where the <TAB> appeared */
749 if (spaces > 0)
750 {
751 /*
752 * Calculate the index in the buffer of the insertion point. To
753 * do this, start with linepos_, which the current output index in
754 * the buffer, and subtract the difference between linecol_ and
755 * the tab starting column, since this will give us the number of
756 * characters in the buffer since the tab insertion point.
757 */
758 i = linepos_ - (linecol_ - pending_tab_start_);
759
760 /*
761 * open up the line buffer and line color buffer by the number of
762 * spaces we're adding
763 */
764 memmove(linebuf_ + i + spaces, linebuf_ + i,
765 (linepos_ - i + 1)*sizeof(linebuf_[0]));
766 memmove(colorbuf_ + i + spaces, colorbuf_ + i,
767 (linepos_ - i + 1)*sizeof(colorbuf_[0]));
768
769 /* advance the line and column positions for the insertion */
770 linecol_ += spaces;
771 linepos_ += spaces;
772
773 /* insert the spaces */
774 for ( ; spaces != 0 ; --spaces, ++i)
775 {
776 /* insert a space of the same color active at the <TAB> */
777 linebuf_[i] = ' ';
778 colorbuf_[i] = pending_tab_color_;
779 }
780 }
781
782 /* we've handled the pending tab, so we can forget about it */
783 pending_tab_align_ = VMFMT_TAB_NONE;
784 }
785
786 /* ------------------------------------------------------------------------ */
787 /*
788 * Resume text-only HTML mini-parser. This is called when we start writing
789 * a new string and discover that we're parsing inside an HTML tag in our
790 * mini-parser.
791 *
792 * Returns the next character after the run of text we parse.
793 */
794 wchar_t CVmFormatter::resume_html_parsing(VMG_ wchar_t c,
795 const char **sp, size_t *slenp)
796 {
797 /* keep going until we run out of characters */
798 while (c != L'\0')
799 {
800 /*
801 * If we're parsing HTML here, and we're inside a tag, skip
802 * characters until we reach the end of the tag.
803 */
804 if (html_parse_state_ != VMCON_HPS_NORMAL)
805 {
806 wchar_t *dst;
807
808 /* check our parsing mode */
809 switch(html_parse_state_)
810 {
811 case VMCON_HPS_TAG:
812 /*
813 * keep skipping up to the closing '>', but note when we
814 * enter any quoted section
815 */
816 switch(c)
817 {
818 case '>':
819 /* we've reached the end of the tag */
820 html_parse_state_ = VMCON_HPS_NORMAL;
821
822 /* if we have a deferred <BR>, process it now */
823 switch(html_defer_br_)
824 {
825 case HTML_DEFER_BR_NONE:
826 /* no deferred <BR> */
827 break;
828
829 case HTML_DEFER_BR_FLUSH:
830 flush(vmg_ VM_NL_NEWLINE);
831 break;
832
833 case HTML_DEFER_BR_BLANK:
834 write_blank_line(vmg0_);
835 break;
836 }
837
838 /* no more deferred <BR> pending */
839 html_defer_br_ = HTML_DEFER_BR_NONE;
840
841 /* if we're parsing a <TAB> tag, finish it */
842 if (html_in_tab_)
843 {
844 /* check the attributes of the <TAB> */
845 if (new_tab_entry_ != 0
846 || new_tab_align_ != VMFMT_TAB_NONE)
847 {
848 /*
849 * We have either a <TAB TO>, maybe with an
850 * ALIGN attribute, or at least a <TAB ALIGN>.
851 *
852 * If there's no TO, then we use the default
853 * treatment based on the alignment: for
854 * ALIGN=RIGHT, the text up to the end of the
855 * line is aligned at the right edge of the
856 * display; for ALIGN=CENTER, it's centered
857 * within the margins. For other alignments,
858 * a TAB without a TO attribute has no effect.
859 *
860 * Check for ALIGN without TO, and ignore it
861 * if it's not CENTER or RIGHT alignment.
862 */
863 if (new_tab_entry_ == 0
864 && new_tab_align_ != VMFMT_TAB_RIGHT
865 && new_tab_align_ != VMFMT_TAB_CENTER)
866 {
867 /*
868 * meaningless alignment for <TAB> without
869 * a TO attribute - ignore the tab
870 */
871 }
872 else if (new_tab_align_ == VMFMT_TAB_LEFT
873 || new_tab_align_ == VMFMT_TAB_NONE)
874 {
875 int delta;
876
877 /*
878 * This is a left-aligned tab (explicitly
879 * or implicitly: if no alignment is
880 * specified, default to left alignment).
881 * This is easy: just insert spaces to the
882 * desired column.
883 */
884 delta = new_tab_entry_->column_ - linecol_;
885 if (delta > 0)
886 write_tab(vmg_ delta, 0);
887 }
888 else
889 {
890 /*
891 * For other alignments, we won't know how
892 * much space to insert until we find the
893 * the next <TAB> or the end of the line.
894 *
895 * Remember the starting column of the
896 * pending tab. When we reach another
897 * <TAB>, or we reach the end of the line,
898 * we'll insert spaces at this point as
899 * needed. Remember the current color, so
900 * we can use the color active at the
901 * <TAB> tag when we insert the spaces
902 * expanding the tab.
903 *
904 * Also remember the characteristics
905 * specified in the <TAB> tag (alignment,
906 * decimal-point alignment character), so
907 * we'll be able to apply the desired
908 * characteristics when we expand the tab.
909 */
910 pending_tab_start_ = linecol_;
911 pending_tab_color_ = cur_color_;
912 pending_tab_entry_ = new_tab_entry_;
913 pending_tab_align_ = new_tab_align_;
914 pending_tab_dp_ = new_tab_dp_;
915
916 /* default to '.' as the DP aligner */
917 if (pending_tab_align_ == VMFMT_TAB_DECIMAL
918 && pending_tab_dp_ == L'\0')
919 pending_tab_dp_ = L'.';
920 }
921 }
922 else
923 {
924 /*
925 * it's just a <TAB> with no attributes -
926 * default to <TAB MULTIPLE=4>, so that it
927 * behaves like a simple '\t'
928 */
929 write_tab(vmg_ 0, 4);
930 }
931 }
932
933 /* we're not parsing inside a tag any more */
934 html_allow_alt_ = FALSE;
935 html_allow_color_ = FALSE;
936 html_in_body_ = FALSE;
937 html_in_tab_ = FALSE;
938 html_in_wrap_ = FALSE;
939
940 /* done */
941 break;
942
943 case '"':
944 /* enter a double-quoted string */
945 html_parse_state_ = VMCON_HPS_DQUOTE;
946 break;
947
948 case '\'':
949 /* enter a single-quoted string */
950 html_parse_state_ = VMCON_HPS_SQUOTE;
951 break;
952
953 default:
954 /* if it's alphabetic, start an attribute name */
955 if (t3_is_alpha(c))
956 {
957 /* note that we're parsing an attribute name */
958 html_parse_state_ = VMCON_HPS_ATTR_NAME;
959
960 /* empty the buffer */
961 attrname_[0] = L'\0';
962
963 /* go back and re-read it in the new mode */
964 continue;
965 }
966 break;
967 }
968 break;
969
970 case VMCON_HPS_DQUOTE:
971 /* if we've reached the closing quote, return to tag state */
972 if (c == '"')
973 html_parse_state_ = VMCON_HPS_TAG;
974 break;
975
976 case VMCON_HPS_SQUOTE:
977 /* if we've reached the closing quote, return to tag state */
978 if (c == '\'')
979 html_parse_state_ = VMCON_HPS_TAG;
980 break;
981
982 case VMCON_HPS_ATTR_NAME:
983 /*
984 * parsing an attribute name; resume at the last character
985 * of the buffer so far
986 */
987 dst = attrname_ + wcslen(attrname_);
988
989 /* add characters to the attribute name */
990 while (t3_is_alpha(c) || t3_is_digit(c))
991 {
992 /* store this character if there's room */
993 if (dst + 1 < attrname_ + CVFMT_MAX_ATTR_NAME)
994 *dst++ = c;
995
996 /* get the next character */
997 c = next_wchar(sp, slenp);
998 }
999
1000 /* null-terminate the result so far */
1001 *dst++ = '\0';
1002
1003 /*
1004 * if we've reached the end of the string, stop here,
1005 * staying in the same mode
1006 */
1007 if (c == '\0')
1008 break;
1009
1010 /* skip any whitespace */
1011 while (t3_is_whitespace(c))
1012 c = next_wchar(sp, slenp);
1013
1014 /* if we found an '=', switch to parsing the value */
1015 if (c == '=')
1016 {
1017 /* switch to value mode */
1018 html_parse_state_ = VMCON_HPS_ATTR_VAL;
1019
1020 /* empty the value buffer */
1021 attrval_[0] = L'\0';
1022 attr_qu_ = L'\0';
1023
1024 /* proceed to the next character */
1025 break;
1026 }
1027 else
1028 {
1029 /*
1030 * There's no value explicitly given, so the value is
1031 * implicitly the same as the attribute name.
1032 */
1033 wcscpy(attrval_, attrname_);
1034
1035 /* this takes us back to tag mode */
1036 html_parse_state_ = VMCON_HPS_TAG;
1037
1038 /* go process the implicit attribute value */
1039 goto process_attr_val;
1040 }
1041
1042 case VMCON_HPS_ATTR_VAL:
1043 /* reading attribute value - pick up where we left off */
1044 dst = attrval_ + wcslen(attrval_);
1045
1046 /* if we haven't started the name yet, skip whitespace */
1047 if (attr_qu_ == 0 && attrval_[0] == L'\0')
1048 {
1049 while (t3_is_whitespace(c))
1050 c = next_wchar(sp, slenp);
1051 }
1052
1053 /* if we're out of characters, we're done */
1054 if (c == 0)
1055 break;
1056
1057 /* if this is the first character, check for a quote */
1058 if (attrval_[0] == L'\0' && (c == L'"' || c == L'\''))
1059 {
1060 /* note the quote */
1061 attr_qu_ = c;
1062
1063 /* skip it */
1064 c = next_wchar(sp, slenp);
1065 if (c == 0)
1066 break;
1067 }
1068
1069 /* keep going until we reach the end of the attribute */
1070 while (c != 0)
1071 {
1072 /* if this is our quote character, we're done */
1073 if (attr_qu_ != 0 && c == attr_qu_)
1074 {
1075 /* skip the quote */
1076 c = next_wchar(sp, slenp);
1077
1078 /* return to tag mode */
1079 html_parse_state_ = VMCON_HPS_TAG;
1080
1081 /* stop scanning */
1082 break;
1083 }
1084
1085 /*
1086 * if it's a space or a '>', and the attribute isn't
1087 * quoted, this marks the end of the attribute
1088 */
1089 if (attr_qu_ == 0 && (t3_is_whitespace(c) || c == '>'))
1090 {
1091 /* return to tag mode */
1092 html_parse_state_ = VMCON_HPS_TAG;
1093
1094 /* stop scanning */
1095 break;
1096 }
1097
1098 /* store the character if we have room */
1099 if (dst + 1 < attrval_ + CVFMT_MAX_ATTR_VAL)
1100 *dst++ = c;
1101
1102 /* read the next character */
1103 c = next_wchar(sp, slenp);
1104 }
1105
1106 /* null-terminate the destination */
1107 *dst = L'\0';
1108
1109 /*
1110 * if we're still in value-parsing mode, it means that we
1111 * reached the end of the string without reaching a value
1112 * delimiter - simply stop scanning and wait for more
1113 */
1114 if (html_parse_state_ == VMCON_HPS_ATTR_VAL)
1115 break;
1116
1117 process_attr_val:
1118 /*
1119 * We have our tag and value, so see if we recognize it,
1120 * and it's meaningful in the context of the current tag.
1121 */
1122 if (html_defer_br_ != HTML_DEFER_BR_NONE
1123 && CVmCaseFoldStr::wstreq(attrname_, L"height"))
1124 {
1125 int ht;
1126
1127 /*
1128 * If the height is zero, always treat this as a
1129 * non-blanking flush. If it's one, treat it as we
1130 * originally planned to. If it's greater than one,
1131 * add n blank lines.
1132 */
1133 ht = wtoi(attrval_);
1134 if (ht == 0)
1135 {
1136 /* always use non-blanking flush */
1137 html_defer_br_ = HTML_DEFER_BR_FLUSH;
1138 }
1139 else if (ht == 1)
1140 {
1141 /* keep original setting */
1142 }
1143 else
1144 {
1145 /*
1146 * write out the desired number of blank lines
1147 */
1148 for ( ; ht > 0 ; --ht)
1149 write_blank_line(vmg0_);
1150 }
1151 }
1152 else if (html_allow_alt_
1153 && !html_in_ignore_
1154 && CVmCaseFoldStr::wstreq(attrname_, L"alt"))
1155 {
1156 /* write out the ALT string */
1157 buffer_wstring(vmg_ attrval_);
1158 }
1159 else if (html_allow_color_
1160 && CVmCaseFoldStr::wstreq(attrname_, L"color"))
1161 {
1162 os_color_t new_color;
1163
1164 /* parse the color, and use it as the new foreground */
1165 if (parse_color_attr(vmg_ attrval_, &new_color))
1166 cur_color_.fg = new_color;
1167 }
1168 else if (html_allow_color_
1169 && CVmCaseFoldStr::wstreq(attrname_, L"bgcolor"))
1170 {
1171 os_color_t new_color;
1172
1173 /* parse the color, and use it as the new foreground */
1174 if (parse_color_attr(vmg_ attrval_, &new_color))
1175 cur_color_.bg = new_color;
1176 }
1177 else if (html_in_body_
1178 && CVmCaseFoldStr::wstreq(attrname_, L"text"))
1179 {
1180 os_color_t new_color;
1181
1182 /* parse the color, and use it as the new foreground */
1183 if (parse_color_attr(vmg_ attrval_, &new_color))
1184 cur_color_.fg = new_color;
1185 }
1186 else if (html_in_body_
1187 && CVmCaseFoldStr::wstreq(attrname_, L"bgcolor"))
1188 {
1189 os_color_t new_color;
1190
1191 /* parse the color, and use it as the new background */
1192 if (parse_color_attr(vmg_ attrval_, &new_color))
1193 set_os_body_color(new_color);
1194 }
1195 else if (html_in_tab_)
1196 {
1197 /* check the attribute name */
1198 if (CVmCaseFoldStr::wstreq(attrname_, L"id"))
1199 {
1200 /*
1201 * We're defining a new tab at the current output
1202 * position. Find or create a tab object with the
1203 * given ID, and set its position to the current
1204 * column position. (If we already have an entry
1205 * with the same name, the new definition replaces
1206 * the old definition, so just change the existing
1207 * entry.)
1208 */
1209 CVmFmtTabStop *entry = find_tab(attrval_, TRUE);
1210
1211 /* define the entry in the current column */
1212 entry->column_ = linecol_;
1213
1214 /* this finishes the <TAB> */
1215 html_in_tab_ = FALSE;
1216 }
1217 else if (CVmCaseFoldStr::wstreq(attrname_, L"to"))
1218 {
1219 /*
1220 * find the tab object with the given ID, and
1221 * remember it; when we're done parsing the tag,
1222 * we'll look at this to generate our tab output
1223 */
1224 new_tab_entry_ = find_tab(attrval_, FALSE);
1225
1226 /* if we didn't find it, ignore this <TAB> */
1227 if (new_tab_entry_ == 0)
1228 html_in_tab_ = FALSE;
1229 }
1230 else if (CVmCaseFoldStr::wstreq(attrname_, L"indent"))
1231 {
1232 /*
1233 * it's a simple <TAB INDENT=n> - this simply
1234 * inserts the given number of spaces
1235 */
1236 write_tab(vmg_ wtoi(attrval_), 0);
1237
1238 /* this finishes the <TAB> */
1239 html_in_tab_ = FALSE;
1240 }
1241 else if (CVmCaseFoldStr::wstreq(attrname_, L"multiple"))
1242 {
1243 /*
1244 * it's a simple <TAB MULTIPLE=n> - this simply
1245 * inserts spaces to the given column multiple
1246 */
1247 write_tab(vmg_ wtoi(attrval_), 0);
1248
1249 /* this finishes the <TAB> */
1250 html_in_tab_ = FALSE;
1251 }
1252 else if (CVmCaseFoldStr::wstreq(attrname_, L"align"))
1253 {
1254 /* note the alignment */
1255 if (CVmCaseFoldStr::wstreq(attrval_, L"left"))
1256 new_tab_align_ = VMFMT_TAB_LEFT;
1257 else if (CVmCaseFoldStr::wstreq(attrval_, L"right"))
1258 new_tab_align_ = VMFMT_TAB_RIGHT;
1259 else if (CVmCaseFoldStr::wstreq(attrval_, L"center"))
1260 new_tab_align_ = VMFMT_TAB_CENTER;
1261 else if (CVmCaseFoldStr::wstreq(attrval_, L"decimal"))
1262 new_tab_align_ = VMFMT_TAB_DECIMAL;
1263 else
1264 {
1265 /* invalid - ignore the <TAB> */
1266 html_in_tab_ = FALSE;
1267 }
1268 }
1269 else if (CVmCaseFoldStr::wstreq(attrname_, L"dp"))
1270 {
1271 /* note the decimal point character */
1272 new_tab_dp_ = attrval_[0];
1273 }
1274 }
1275 else if (html_in_wrap_)
1276 {
1277 /* it's a WRAP tag - check the attribute */
1278 if (CVmCaseFoldStr::wstreq(attrname_, L"word"))
1279 {
1280 /* word wrap mode - clear the 'any' wrap flag */
1281 cur_flags_ &= ~VMCON_OBF_BREAK_ANY;
1282 }
1283 else if (CVmCaseFoldStr::wstreq(attrname_, L"char"))
1284 {
1285 /* character wrap mode - set the 'any' flag */
1286 cur_flags_ |= VMCON_OBF_BREAK_ANY;
1287 }
1288 }
1289
1290 /*
1291 * since we already read the next character, simply loop
1292 * back immediately
1293 */
1294 continue;
1295
1296 default:
1297 /* ignore other states */
1298 break;
1299 }
1300
1301 /*
1302 * move on to the next character, and start over with the
1303 * new character
1304 */
1305 c = next_wchar(sp, slenp);
1306 continue;
1307 }
1308
1309 /*
1310 * If we're in a title, and this isn't the start of a new tag,
1311 * skip the character - we suppress all regular text output
1312 * inside a <TITLE> ... </TITLE> sequence.
1313 */
1314 if (html_in_ignore_)
1315 {
1316 /* check for the start of a new tag */
1317 if (c == '<')
1318 {
1319 /* stop skipping - return to normal parsing */
1320 return c;
1321 }
1322 else
1323 {
1324 wchar_t curCh;
1325
1326 /* check for entities */
1327 if (c == '&')
1328 {
1329 /* parse the entity (fetches the next character) */
1330 c = parse_entity(vmg_ &curCh, sp, slenp);
1331 }
1332 else
1333 {
1334 /* use this character literally */
1335 curCh = c;
1336
1337 /* get the next character */
1338 c = next_wchar(sp, slenp);
1339 }
1340
1341 /*
1342 * if we're gathering a title, and there's room in the
1343 * title buffer for more (always leaving room for a null
1344 * terminator), add this to the title buffer
1345 */
1346 if (html_in_title_ && html_title_ptr_ != 0)
1347 {
1348 size_t rem, orig_rem;
1349
1350 /*
1351 * figure the remaining title buffer space, leaving
1352 * room for a terminating null byte
1353 */
1354 orig_rem = rem = html_title_buf_size_ - 1;
1355
1356 /*
1357 * map this character into the local character set and
1358 * add it to the buffer
1359 */
1360 if (G_cmap_to_ui->map(curCh, &html_title_ptr_, &rem)
1361 > orig_rem)
1362 {
1363 /*
1364 * it didn't fit - add null termination and close
1365 * out the buffer (we might have to add more than
1366 * one byte, because a single character might take
1367 * up multiple bytes in the local character set; to
1368 * ensure that we don't skip a wide character then
1369 * later add a narrower character, close out the
1370 * entire buffer when first we encounter a
1371 * character that doesn't fit)
1372 */
1373 while (html_title_ptr_
1374 < html_title_buf_ + html_title_buf_size_)
1375 *html_title_ptr_++ = '\0';
1376 }
1377 }
1378 }
1379
1380 /* don't display anything in an ignore section */
1381 continue;
1382 }
1383
1384 /*
1385 * we didn't find any HTML parsing we need to do with this
1386 * character, so return the character
1387 */
1388 return c;
1389 }
1390
1391 /* return the character that made us stop parsing */
1392 return c;
1393 }
1394
1395 /* ------------------------------------------------------------------------ */
1396 /*
1397 * Parse the beginning HTML markup. This is called when we are scanning a
1398 * '<' or '&' character in output text, and we're in HTML mode, and the
1399 * underlying target doesn't support HTML parsing. Returns the next
1400 * character to process after we finish our initial parsing.
1401 */
1402 wchar_t CVmFormatter::parse_html_markup(VMG_ wchar_t c,
1403 const char **sp, size_t *slenp)
1404 {
1405 /* check what we have */
1406 if (c == '<')
1407 {
1408 const size_t MAX_TAG_SIZE = 50;
1409 wchar_t tagbuf[MAX_TAG_SIZE];
1410 wchar_t *dst;
1411 int is_end_tag;
1412
1413 /* skip the opening '<' */
1414 c = next_wchar(sp, slenp);
1415
1416 /* note if this is a closing tag */
1417 if (c == '/' || c == '\\')
1418 {
1419 /* it's an end tag - note it and skip the slash */
1420 is_end_tag = TRUE;
1421 c = next_wchar(sp, slenp);
1422 }
1423 else
1424 is_end_tag = FALSE;
1425
1426 /*
1427 * find the end of the tag name - the tag continues to the next
1428 * space, '>', or end of line
1429 */
1430 for (dst = tagbuf ; c != '\0' && c != ' ' && c != '>' ;
1431 c = next_wchar(sp, slenp))
1432 {
1433 /* add this to the tag buffer if it fits */
1434 if (dst + 1 < tagbuf + MAX_TAG_SIZE)
1435 *dst++ = c;
1436 }
1437
1438 /* null-terminate the tag name */
1439 *dst = '\0';
1440
1441 /*
1442 * Check to see if we recognize the tag. We only recognize a few
1443 * simple tags that map easily to character mode.
1444 */
1445 if (CVmCaseFoldStr::wstreq(tagbuf, L"br"))
1446 {
1447 /*
1448 * line break - if there's anything buffered up, just flush the
1449 * current line, otherwise write out a blank line
1450 */
1451 if (html_in_ignore_)
1452 /* suppress in ignore mode */;
1453 else if (linepos_ != 0)
1454 html_defer_br_ = HTML_DEFER_BR_FLUSH;
1455 else
1456 html_defer_br_ = HTML_DEFER_BR_BLANK;
1457 }
1458 else if (CVmCaseFoldStr::wstreq(tagbuf, L"b")
1459 || CVmCaseFoldStr::wstreq(tagbuf, L"i")
1460 || CVmCaseFoldStr::wstreq(tagbuf, L"em")
1461 || CVmCaseFoldStr::wstreq(tagbuf, L"strong")
1462 || CVmCaseFoldStr::wstreq(tagbuf, L"tt"))
1463 {
1464 /* suppress in ignore mode */
1465 if (!html_in_ignore_)
1466 {
1467 /* check to see if this is a start or end tag */
1468 if (!is_end_tag)
1469 {
1470 int attr;
1471
1472 /* it's a start tag - push the current attributes */
1473 push_color();
1474
1475 /* figure the attribute we're adding */
1476 switch(tagbuf[0])
1477 {
1478 case 'b':
1479 case 'B':
1480 attr = OS_ATTR_BOLD;
1481 break;
1482
1483 case 'i':
1484 case 'I':
1485 attr = OS_ATTR_ITALIC;
1486 break;
1487
1488 case 'e':
1489 case 'E':
1490 attr = OS_ATTR_EM;
1491 break;
1492
1493 case 's':
1494 case 'S':
1495 attr = OS_ATTR_STRONG;
1496 break;
1497
1498 case 't':
1499 case 'T':
1500 attr = OS_ATTR_MONOSP;
1501 break;
1502 }
1503
1504 /* add the attribute */
1505 cur_color_.attr |= attr;
1506 }
1507 else
1508 {
1509 /* it's an end tag - just pop the saved attributes */
1510 pop_color();
1511 }
1512 }
1513 }
1514 else if (CVmCaseFoldStr::wstreq(tagbuf, L"p"))
1515 {
1516 /* paragraph - send out a blank line */
1517 if (!html_in_ignore_)
1518 write_blank_line(vmg0_);
1519 }
1520 else if (CVmCaseFoldStr::wstreq(tagbuf, L"tab") && !html_in_ignore_)
1521 {
1522 if (!html_in_ignore_)
1523 {
1524 /* if we have a pending tab, handle it */
1525 expand_pending_tab(vmg_ TRUE);
1526
1527 /* start a tab definition */
1528 html_in_tab_ = TRUE;
1529
1530 /* the new tab has no known characteristics yet */
1531 new_tab_entry_ = 0;
1532 new_tab_align_ = VMFMT_TAB_NONE;
1533 new_tab_dp_ = L'\0';
1534 }
1535 }
1536 else if (CVmCaseFoldStr::wstreq(tagbuf, L"img")
1537 || CVmCaseFoldStr::wstreq(tagbuf, L"sound"))
1538 {
1539 /* IMG and SOUND - allow ALT attributes */
1540 html_allow_alt_ = TRUE;
1541 }
1542 else if (CVmCaseFoldStr::wstreq(tagbuf, L"font"))
1543 {
1544 /* FONT - look for COLOR attribute */
1545 if (html_in_ignore_)
1546 {
1547 /* do nothing while ignoring tags */
1548 }
1549 else if (!is_end_tag)
1550 {
1551 /* enable the COLOR attribute */
1552 html_allow_color_ = TRUE;
1553
1554 /* push the current color, in case we change it */
1555 push_color();
1556 }
1557 else
1558 {
1559 /* restore the color in the enclosing text */
1560 pop_color();
1561 }
1562 }
1563 else if (CVmCaseFoldStr::wstreq(tagbuf, L"body"))
1564 {
1565 /* BODY - look for BGCOLOR and TEXT attributes */
1566 if (!html_in_ignore_ && !is_end_tag)
1567 {
1568 /* note that we're parsing a BODY tag */
1569 html_in_body_ = TRUE;
1570 }
1571 }
1572 else if (CVmCaseFoldStr::wstreq(tagbuf, L"hr"))
1573 {
1574 int rem;
1575
1576 if (!html_in_ignore_)
1577 {
1578 /* start a new line */
1579 flush(vmg_ VM_NL_NEWLINE);
1580
1581 /* write out underscores to the display width */
1582 for (rem = console_->get_line_width() - 1 ; rem > 0 ; )
1583 {
1584 const size_t DASHBUFLEN = 100;
1585 wchar_t dashbuf[DASHBUFLEN];
1586 wchar_t *p;
1587 size_t cur;
1588
1589 /* do as much as we can on this pass */
1590 cur = rem;
1591 if (cur > DASHBUFLEN - 1)
1592 cur = DASHBUFLEN - 1;
1593
1594 /* deduct this from the total */
1595 rem -= cur;
1596
1597 /* fill the buffer with dashes */
1598 for (p = dashbuf ; cur != 0 ; --cur)
1599 *p++ = '-';
1600 *p = '\0';
1601
1602 /* write it out */
1603 buffer_wstring(vmg_ dashbuf);
1604 }
1605
1606 /* put a blank line after the underscores */
1607 write_blank_line(vmg0_);
1608 }
1609 }
1610 else if (CVmCaseFoldStr::wstreq(tagbuf, L"q"))
1611 {
1612 wchar_t htmlchar;
1613
1614 if (!html_in_ignore_)
1615 {
1616 /* if it's an open quote, increment the level */
1617 if (!is_end_tag)
1618 ++html_quote_level_;
1619
1620 /* add the open quote */
1621 htmlchar =
1622 (!is_end_tag
1623 ? ((html_quote_level_ & 1) == 1
1624 ? 8220 : 8216)
1625 : ((html_quote_level_ & 1) == 1
1626 ? 8221 : 8217));
1627
1628 /* write out the HTML quote character */
1629 buffer_char(vmg_ htmlchar);
1630
1631 /* if it's a close quote, decrement the level */
1632 if (is_end_tag)
1633 --html_quote_level_;
1634 }
1635 }
1636 else if (CVmCaseFoldStr::wstreq(tagbuf, L"title"))
1637 {
1638 /*
1639 * Turn ignore mode on or off as appropriate, and turn on or
1640 * off title mode as well.
1641 */
1642 if (is_end_tag)
1643 {
1644 /*
1645 * note that we're leaving an ignore section and a title
1646 * section
1647 */
1648 --html_in_ignore_;
1649 --html_in_title_;
1650
1651 /*
1652 * If we're no longer in a title, and we have a buffer
1653 * where we've gathered up the title string, call the OS
1654 * layer to tell it the title string, in case it wants to
1655 * change the window title or otherwise make use of the
1656 * title. (Note that some streams don't bother keeping
1657 * track of the title at all, so we might not have a title
1658 * buffer.)
1659 */
1660 if (html_in_title_ == 0 && html_title_ptr_ != 0)
1661 {
1662 /* null-terminate the title string */
1663 *html_title_ptr_ = '\0';
1664
1665 /* tell the OS about the title */
1666 if (!html_in_ignore_)
1667 set_title_in_os(html_title_buf_);
1668 }
1669 }
1670 else
1671 {
1672 /*
1673 * if we aren't already in a title, set up to capture the
1674 * title into the title buffer
1675 */
1676 if (!html_in_title_)
1677 html_title_ptr_ = html_title_buf_;
1678
1679 /*
1680 * note that we're in a title and in an ignore section,
1681 * since nothing within gets displayed
1682 */
1683 ++html_in_ignore_;
1684 ++html_in_title_;
1685 }
1686 }
1687 else if (CVmCaseFoldStr::wstreq(tagbuf, L"aboutbox")
1688 || CVmCaseFoldStr::wstreq(tagbuf, L"banner"))
1689 {
1690 /* turn ignore mode on or off as appropriate */
1691 if (is_end_tag)
1692 --html_in_ignore_;
1693 else
1694 ++html_in_ignore_;
1695 }
1696 else if (CVmCaseFoldStr::wstreq(tagbuf, L"log"))
1697 {
1698 /* handle the <log> tag according to our stream subclass */
1699 process_log_tag(is_end_tag);
1700 }
1701 else if (CVmCaseFoldStr::wstreq(tagbuf, L"nolog"))
1702 {
1703 /* handle the <nolog> tag according to our stream subclass */
1704 process_nolog_tag(is_end_tag);
1705 }
1706 else if (CVmCaseFoldStr::wstreq(tagbuf, L"wrap"))
1707 {
1708 /* note that we have a 'wrap' tag to process */
1709 if (!html_in_ignore_)
1710 html_in_wrap_ = TRUE;
1711 }
1712 else if (CVmCaseFoldStr::wstreq(tagbuf, L"pre"))
1713 {
1714 if (!is_end_tag) {
1715 ++html_pre_level_;
1716 write_blank_line(vmg0_);
1717 push_color();
1718 cur_color_.attr |= OS_ATTR_MONOSP;
1719 } else if (html_pre_level_ > 0){
1720 --html_pre_level_;
1721 pop_color();
1722 write_blank_line(vmg0_);
1723 }
1724 }
1725
1726 /* suppress everything up to the next '>' */
1727 html_parse_state_ = VMCON_HPS_TAG;
1728
1729 /*
1730 * return the next character - since we're now in TAG mode, we'll
1731 * resume the HTML mini-parser to parse the contents of the tag
1732 */
1733 return c;
1734 }
1735 else if (c == '&')
1736 {
1737 /* parse the HTML markup */
1738 wchar_t ent;
1739 c = parse_entity(vmg_ &ent, sp, slenp);
1740
1741 /* translate it and write it out */
1742 buffer_char(vmg_ ent);
1743
1744 /* proceed with the next character */
1745 return c;
1746 }
1747 else
1748 {
1749 /*
1750 * we have nothing special to do with this character - simply
1751 * buffer it and move on to the next character
1752 */
1753 buffer_char(vmg_ c);
1754 return next_wchar(sp, slenp);
1755 }
1756 }
1757
1758 /* ------------------------------------------------------------------------ */
1759 /*
1760 * Parse an HTML entity markup. Call this with the current character
1761 * pointer at the '&'. We'll parse the entity name and return it in *ent.
1762 * The return value is the next character to be parsed after the entity.
1763 */
1764 wchar_t CVmFormatter::parse_entity(VMG_ wchar_t *ent, const char **sp,
1765 size_t *slenp)
1766 {
1767 const size_t MAXAMPLEN = 10;
1768 char ampbuf[MAXAMPLEN];
1769 char *dst;
1770 const char *orig_s;
1771 size_t orig_slen;
1772 const struct amp_tbl_t *ampptr;
1773 size_t lo, hi, cur;
1774 wchar_t c;
1775
1776 /*
1777 * remember where the part after the '&' begins, so we can come back
1778 * here later if necessary
1779 */
1780 orig_s = *sp;
1781 orig_slen = *slenp;
1782
1783 /* get the character after the ampersand */
1784 c = next_wchar(sp, slenp);
1785
1786 /* if it's numeric, parse the number */
1787 if (c == '#')
1788 {
1789 uint val;
1790
1791 /* skip the '#' */
1792 c = next_wchar(sp, slenp);
1793
1794 /* check for hex */
1795 if (c == 'x' || c == 'X')
1796 {
1797 /* skip the 'x' */
1798 c = next_wchar(sp, slenp);
1799
1800 /* read the hex number */
1801 for (val = 0 ; is_xdigit(c) ; c = next_wchar(sp, slenp))
1802 {
1803 /* accumulate this digit into the value */
1804 val *= 16;
1805 if (is_digit(c))
1806 val += c - '0';
1807 else if (c >= 'a' && c <= 'f')
1808 val += c - 'a' + 10;
1809 else
1810 val += c - 'A' + 10;
1811 }
1812 }
1813 else
1814 {
1815 /* read the number */
1816 for (val = 0 ; is_digit(c) ; c = next_wchar(sp, slenp))
1817 {
1818 /* accumulate this digit into the value */
1819 val *= 10;
1820 val += c - '0';
1821 }
1822 }
1823
1824 /* if we found a ';' at the end, skip it */
1825 if (c == ';')
1826 c = next_wchar(sp, slenp);
1827
1828 /* return the entity from the numeric character value */
1829 *ent = (wchar_t)val;
1830
1831 /* we're done with this character */
1832 return c;
1833 }
1834
1835 /*
1836 * Parse the sequence after the '&'. Parse up to the closing
1837 * semicolon, or any non-alphanumeric, or until we fill up the buffer.
1838 */
1839 for (dst = ampbuf ;
1840 c != '\0' && (is_digit(c) || is_alpha(c))
1841 && dst < ampbuf + MAXAMPLEN - 1 ;
1842 c = next_wchar(sp, slenp))
1843 {
1844 /* copy this character to the name buffer */
1845 *dst++ = (char)c;
1846 }
1847
1848 /* null-terminate the name */
1849 *dst = '\0';
1850
1851 /* do a binary search for the name */
1852 lo = 0;
1853 hi = sizeof(amp_tbl)/sizeof(amp_tbl[0]) - 1;
1854 for (;;)
1855 {
1856 int diff;
1857
1858 /* if we've converged, look no further */
1859 if (lo > hi
1860 || lo >= sizeof(amp_tbl)/sizeof(amp_tbl[0]))
1861 {
1862 ampptr = 0;
1863 break;
1864 }
1865
1866 /* split the difference */
1867 cur = lo + (hi - lo)/2;
1868 ampptr = &amp_tbl[cur];
1869
1870 /* see where we are relative to the target item */
1871 diff = strcmp(ampptr->cname, ampbuf);
1872 if (diff == 0)
1873 {
1874 /* this is it */
1875 break;
1876 }
1877 else if (lo >= hi)
1878 {
1879 /* failed to find it */
1880 ampptr = 0;
1881 break;
1882 }
1883 else if (diff > 0)
1884 {
1885 /* this one is too high - check the lower half */
1886 hi = (cur == hi ? hi - 1 : cur);
1887 }
1888 else
1889 {
1890 /* this one is too low - check the upper half */
1891 lo = (cur == lo ? lo + 1 : cur);
1892 }
1893 }
1894
1895 /* skip to the appropriate next character */
1896 if (c == ';')
1897 {
1898 /* name ended with semicolon - skip the semicolon */
1899 c = next_wchar(sp, slenp);
1900 }
1901 else if (ampptr != 0)
1902 {
1903 int skipcnt;
1904
1905 /* found the name - skip its exact length */
1906 skipcnt = strlen(ampptr->cname);
1907 for (*sp = orig_s, *slenp = orig_slen ; skipcnt != 0 ;
1908 c = next_wchar(sp, slenp), --skipcnt) ;
1909
1910 /*
1911 * that positions us on the last character of the entity name; skip
1912 * one more, so that we're on the character after the entity name
1913 */
1914 c = next_wchar(sp, slenp);
1915 }
1916
1917 /* if we found the entry, write out the character */
1918 if (ampptr != 0)
1919 {
1920 /* we found it - return the character value */
1921 *ent = ampptr->html_cval;
1922 }
1923 else
1924 {
1925 /*
1926 * we didn't find it - ignore the entire sequence, and just write
1927 * it out verbatim; for our caller's purposes, the result of the
1928 * parse is just the '&' itself
1929 */
1930 *ent = '&';
1931
1932 /* now go back and scan from the next character after the ampersand */
1933 *sp = orig_s;
1934 *slenp = orig_slen;
1935 c = next_wchar(sp, slenp);
1936 }
1937
1938 /* return the next character */
1939 return c;
1940 }
1941
1942
1943 /* ------------------------------------------------------------------------ */
1944 /*
1945 * Service routine - read a number represented as a base-10 string of wide
1946 * characters
1947 */
1948 int CVmFormatter::wtoi(const wchar_t *p)
1949 {
1950 long acc;
1951 int neg = FALSE;
1952
1953 /* skip any leading whitespace */
1954 while (t3_is_whitespace(*p))
1955 ++p;
1956
1957 /* check for a leading sign */
1958 if (*p == '+')
1959 {
1960 /* positive sign - skip it */
1961 ++p;
1962 }
1963 else if (*p == '-')
1964 {
1965 /* negative sign - note it and skip it */
1966 neg = TRUE;
1967 ++p;
1968 }
1969
1970 /* scan the digits */
1971 for (acc = 0 ; is_digit(*p) ; ++p)
1972 {
1973 /* shift the accumulator and add this digit */
1974 acc *= 10;
1975 acc += value_of_digit(*p);
1976 }
1977
1978 /* apply the sign */
1979 if (neg)
1980 acc = -acc;
1981
1982 /* return the result */
1983 return acc;
1984 }
1985