Debian Codegraph @

WWW/Library/Implementation/SGML.c

raw
1 /*
2 * $LynxId: SGML.c,v 1.194 2026/04/19 22:48:57 tom Exp $
3 *
4 * General SGML Parser code SGML.c
5 * ========================
6 *
7 * This module implements an HTStream object. To parse an
8 * SGML file, create this object which is a parser. The object
9 * is (currently) created by being passed a DTD structure,
10 * and a target HTStructured object at which to throw the parsed stuff.
11 *
12 * 6 Feb 93 Binary searches used. Interface modified.
13 */
14
15 #define HTSTREAM_INTERNAL 1
16
17 #include <HTUtils.h>
18
19 #include <SGML.h>
20 #include <HTMLDTD.h>
21 #include <HTAccess.h>
22 #include <UCAux.h>
23
24 #include <HTChunk.h>
25 #include <HTUtils.h>
26
27 #include <LYCharSets.h>
28 #include <LYCharVals.h> /* S/390 -- gil -- 0635 */
29 #include <LYGlobalDefs.h>
30 #include <LYStrings.h>
31 #include <LYLeaks.h>
32 #include <LYUtils.h>
33
34 #ifdef USE_COLOR_STYLE
35 # include <LYStyle.h>
36 #endif
37 #ifdef USE_PRETTYSRC
38 # include <LYPrettySrc.h>
39 #endif
40
41 /* a global variable doesn't work with info-stages which convert encoding */
42 #if defined(EXP_CHINESEUTF8_SUPPORT)
43 #undef IS_CJK_TTY
44 #define IS_CJK_TTY me->T.do_cjk
45 #endif
46
47 #define AssumeCP1252(me) \
48 (((me)->inUCLYhndl == LATIN1 \
49 || (me)->inUCLYhndl == US_ASCII) \
50 && html5_charsets)
51
52 #define INVALID (-1)
53
54 static int sgml_offset;
55
56 #ifdef USE_PRETTYSRC
57
58 static char *entity_string; /* this is used for printing entity name.
59
60 Unconditionally added since redundant assignments don't hurt much */
61
62 static void fake_put_character(HTStream *p GCC_UNUSED,
63 int c GCC_UNUSED)
64 {
65 }
66
67 #define START TRUE
68 #define STOP FALSE
69
70 #define PUTS_TR(x) psrc_convert_string = TRUE; PUTS(x)
71
72 #endif
73
74 /* my_casecomp() - optimized by the first character, NOT_ASCII ok */
75 #define my_casecomp(a,b) ((TOUPPER(*a) == TOUPPER(*b)) ? \
76 AS_casecomp(a,b) : \
77 (TOASCII(TOUPPER(*a)) - TOASCII(TOUPPER(*b))))
78
79 /* ...used for comments and attributes value like href... */
80 #define HTChunkPutUtf8Char(ch,x) \
81 { \
82 if ((TOASCII(x) < 128) && (ch->size < ch->allocated)) \
83 ch->data[ch->size++] = (char)x; \
84 else \
85 (HTChunkPutUtf8Char)(ch,x); \
86 }
87
88 #define PUTS(str) ((*me->actions->put_string)(me->target, str))
89 #define PUTC(ch) ((*me->actions->put_character)(me->target, (char) ch))
90 #define PUTUTF8(code) (UCPutUtf8_charstring((HTStream *)me->target, \
91 (putc_func_t*)(me->actions->put_character), code))
92
93 #ifdef USE_PRETTYSRC
94 #define PRETTYSRC_PUTC(c) if (psrc_view) PUTC(c)
95 #else
96 #define PRETTYSRC_PUTC(c) /* nothing */
97 #endif
98
99 /*the following macros are used for pretty source view. */
100 #define IS_C(attr) (attr.type == HTMLA_CLASS)
101
102 #if defined(USE_JAPANESEUTF8_SUPPORT)
103 # define UTF8_TTY_ISO2022JP (me->T.output_utf8)
104 #else
105 # define UTF8_TTY_ISO2022JP 0
106 #endif
107
108 HTCJKlang HTCJK = NOCJK; /* CJK enum value. */
109 BOOL HTPassEightBitRaw = FALSE; /* Pass 161-172,174-255 raw. */
110 BOOL HTPassEightBitNum = FALSE; /* Pass ^ numeric entities raw. */
111 BOOL HTPassHighCtrlRaw = FALSE; /* Pass 127-160,173,&#127; raw. */
112 BOOL HTPassHighCtrlNum = FALSE; /* Pass &#128;-&#159; raw. */
113
114 /* The State (context) of the parser
115 *
116 * This is passed with each call to make the parser reentrant
117 */
118
119 #define MAX_ATTRIBUTES 36 /* Max number of attributes per element */
120
121 /* Element Stack
122 * -------------
123 * This allows us to return down the stack reselecting styles.
124 * As we return, attribute values will be garbage in general.
125 */
126 typedef struct _HTElement HTElement;
127 struct _HTElement {
128 HTElement *next; /* Previously nested element or 0 */
129 HTTag *tag; /* The tag at this level */
130 };
131
132 typedef enum {
133 S_text = 0
134 ,S_attr
135 ,S_attr_gap
136 ,S_comment
137 ,S_cro
138 ,S_doctype
139 ,S_dollar
140 ,S_dollar_dq
141 ,S_dollar_paren
142 ,S_dollar_paren_dq
143 ,S_dollar_paren_sq
144 ,S_dollar_sq
145 ,S_dquoted
146 ,S_end
147 ,S_entity
148 ,S_equals
149 ,S_ero
150 ,S_esc
151 ,S_esc_dq
152 ,S_esc_sq
153 ,S_exclamation
154 ,S_in_kanji
155 ,S_incro
156 ,S_junk_tag
157 ,S_litteral
158 ,S_marked
159 ,S_nonascii_text
160 ,S_nonascii_text_dq
161 ,S_nonascii_text_sq
162 ,S_paren
163 ,S_paren_dq
164 ,S_paren_sq
165 ,S_pcdata
166 ,S_pi
167 ,S_script
168 ,S_sgmlatt
169 ,S_sgmlele
170 ,S_sgmlent
171 ,S_squoted
172 ,S_tag
173 ,S_tag_gap
174 ,S_tagname_slash
175 ,S_value
176 } sgml_state;
177
178 /* Internal Context Data Structure
179 * -------------------------------
180 */
181 struct _HTStream {
182
183 const HTStreamClass *isa; /* inherited from HTStream */
184
185 const SGML_dtd *dtd;
186 const HTStructuredClass *actions; /* target class */
187 HTStructured *target; /* target object */
188
189 HTTag *current_tag;
190 HTTag *slashedtag;
191 const HTTag *unknown_tag;
192 BOOL extended_html; /* xhtml */
193 BOOL strict_xml; /* xml */
194 BOOL inSELECT;
195 BOOL no_lynx_specialcodes;
196 int current_attribute_number;
197 HTChunk *string;
198 int leading_spaces;
199 int trailing_spaces;
200 HTElement *element_stack;
201 sgml_state state;
202 unsigned char kanji_buf;
203 #ifdef CALLERDATA
204 void *callerData;
205 #endif /* CALLERDATA */
206 BOOL present[MAX_ATTRIBUTES]; /* Flags: attribute is present? */
207 char *value[MAX_ATTRIBUTES]; /* NULL, or strings alloc'd with StrAllocCopy_extra() */
208
209 BOOL lead_exclamation;
210 BOOL first_dash;
211 BOOL end_comment;
212 BOOL doctype_bracket;
213 BOOL first_bracket;
214 BOOL second_bracket;
215 BOOL isHex;
216
217 HTParentAnchor *node_anchor;
218 LYUCcharset *inUCI; /* pointer to anchor UCInfo */
219 int inUCLYhndl; /* charset we are fed */
220 LYUCcharset *outUCI; /* anchor UCInfo for target */
221 int outUCLYhndl; /* charset for target */
222 UTFDecodeState U;
223 UCTransParams T;
224 int current_tag_charset; /* charset to pass attributes */
225
226 char *recover;
227 int recover_index;
228 char *include;
229 char *active_include;
230 int include_index;
231 char *url;
232 char *csi;
233 int csi_index;
234 #ifdef USE_PRETTYSRC
235 BOOL cur_attr_is_href;
236 BOOL cur_attr_is_name;
237 #endif
238 };
239
240 #ifdef NO_LYNX_TRACE
241 #define state_name(n) "state"
242 #else
243 static const char *state_name(sgml_state n)
244 {
245 const char *result;
246 /* *INDENT-OFF* */
247 switch (n) {
248 default:
249 case S_attr: result = "S_attr"; break;
250 case S_attr_gap: result = "S_attr_gap"; break;
251 case S_comment: result = "S_comment"; break;
252 case S_cro: result = "S_cro"; break;
253 case S_doctype: result = "S_doctype"; break;
254 case S_dollar: result = "S_dollar"; break;
255 case S_dollar_dq: result = "S_dollar_dq"; break;
256 case S_dollar_paren: result = "S_dollar_paren"; break;
257 case S_dollar_paren_dq: result = "S_dollar_paren_dq"; break;
258 case S_dollar_paren_sq: result = "S_dollar_paren_sq"; break;
259 case S_dollar_sq: result = "S_dollar_sq"; break;
260 case S_dquoted: result = "S_dquoted"; break;
261 case S_end: result = "S_end"; break;
262 case S_entity: result = "S_entity"; break;
263 case S_equals: result = "S_equals"; break;
264 case S_ero: result = "S_ero"; break;
265 case S_esc: result = "S_esc"; break;
266 case S_esc_dq: result = "S_esc_dq"; break;
267 case S_esc_sq: result = "S_esc_sq"; break;
268 case S_exclamation: result = "S_exclamation"; break;
269 case S_in_kanji: result = "S_in_kanji"; break;
270 case S_incro: result = "S_incro"; break;
271 case S_pi: result = "S_pi"; break;
272 case S_junk_tag: result = "S_junk_tag"; break;
273 case S_litteral: result = "S_litteral"; break;
274 case S_marked: result = "S_marked"; break;
275 case S_nonascii_text: result = "S_nonascii_text"; break;
276 case S_nonascii_text_dq: result = "S_nonascii_text_dq"; break;
277 case S_nonascii_text_sq: result = "S_nonascii_text_sq"; break;
278 case S_paren: result = "S_paren"; break;
279 case S_paren_dq: result = "S_paren_dq"; break;
280 case S_paren_sq: result = "S_paren_sq"; break;
281 case S_pcdata: result = "S_pcdata"; break;
282 case S_script: result = "S_script"; break;
283 case S_sgmlatt: result = "S_sgmlatt"; break;
284 case S_sgmlele: result = "S_sgmlele"; break;
285 case S_sgmlent: result = "S_sgmlent"; break;
286 case S_squoted: result = "S_squoted"; break;
287 case S_tag: result = "S_tag"; break;
288 case S_tag_gap: result = "S_tag_gap"; break;
289 case S_tagname_slash: result = "S_tagname_slash"; break;
290 case S_text: result = "S_text"; break;
291 case S_value: result = "S_value"; break;
292 }
293 /* *INDENT-ON* */
294
295 return result;
296 }
297 #endif
298
299 /* storage for Element Stack */
300 #define DEPTH 10
301 static HTElement pool[DEPTH];
302 static int depth = 0;
303
304 static HTElement *pool_alloc(void)
305 {
306 depth++;
307 if (depth > DEPTH)
308 return (HTElement *) malloc(sizeof(HTElement));
309 return (pool + depth - 1);
310 }
311
312 static void pool_free(HTElement * e)
313 {
314 if (depth > DEPTH)
315 FREE(e);
316 depth--;
317 return;
318 }
319
320 #ifdef USE_PRETTYSRC
321
322 static void HTMLSRC_apply_markup(HTStream *me,
323 HTlexeme lexeme,
324 int start)
325 {
326 HT_tagspec *ts = *((start ? lexeme_start : lexeme_end) + lexeme);
327
328 while (ts) {
329 #ifdef USE_COLOR_STYLE
330 if (ts->start) {
331 current_tag_style = ts->style;
332 force_current_tag_style = TRUE;
333 forced_classname = ts->class_name;
334 force_classname = TRUE;
335 }
336 #endif
337 CTRACE((tfp, ts->start ? "SRCSTART %d\n" : "SRCSTOP %d\n", (int) lexeme));
338 if (ts->start)
339 (*me->actions->start_element) (me->target,
340 (int) ts->element,
341 ts->present,
342 (STRING2PTR) ts->value,
343 me->current_tag_charset,
344 &me->include);
345 else
346 (*me->actions->end_element) (me->target,
347 (int) ts->element,
348 &me->include);
349 ts = ts->next;
350 }
351 }
352
353 #define PSRCSTART(x) HTMLSRC_apply_markup(me,HTL_##x,START)
354 #define PSRCSTOP(x) HTMLSRC_apply_markup(me,HTL_##x,STOP)
355
356 #define attr_is_href me->cur_attr_is_href
357 #define attr_is_name me->cur_attr_is_name
358 #endif
359
360 static void set_chartrans_handling(HTStream *me,
361 HTParentAnchor *anchor,
362 int chndl)
363 {
364 if (chndl < 0) {
365 /*
366 * Nothing was set for the parser in earlier stages, so the HTML
367 * parser's UCLYhndl should still be its default. - FM
368 */
369 chndl = HTAnchor_getUCLYhndl(anchor, UCT_STAGE_STRUCTURED);
370 if (chndl < 0)
371 /*
372 * That wasn't set either, so seek the HText default. - FM
373 */
374 chndl = HTAnchor_getUCLYhndl(anchor, UCT_STAGE_HTEXT);
375 if (chndl < 0)
376 /*
377 * That wasn't set either, so assume the current display character
378 * set. - FM
379 */
380 chndl = current_char_set;
381 /*
382 * Try to set the HText and HTML stages' chartrans info with the
383 * default lock level (will not be changed if it was set previously
384 * with a higher lock level). - FM
385 */
386 HTAnchor_setUCInfoStage(anchor, chndl,
387 UCT_STAGE_HTEXT,
388 UCT_SETBY_DEFAULT);
389 HTAnchor_setUCInfoStage(anchor, chndl,
390 UCT_STAGE_STRUCTURED,
391 UCT_SETBY_DEFAULT);
392 /*
393 * Get the chartrans info for output to the HTML parser. - FM
394 */
395 me->outUCI = HTAnchor_getUCInfoStage(anchor,
396 UCT_STAGE_STRUCTURED);
397 me->outUCLYhndl = HTAnchor_getUCLYhndl(me->node_anchor,
398 UCT_STAGE_STRUCTURED);
399 }
400 /*
401 * Set the in->out transformation parameters. - FM
402 */
403 UCSetTransParams(&me->T,
404 me->inUCLYhndl, me->inUCI,
405 me->outUCLYhndl, me->outUCI);
406 /*
407 * This is intended for passing the SGML parser's input charset as an
408 * argument in each call to the HTML parser's start tag function, but it
409 * would be better to call a Lynx_HTML_parser function to set an element in
410 * its HTStructured object, itself, if this were needed. - FM
411 */
412 #ifndef USE_JAPANESEUTF8_SUPPORT
413 if (IS_CJK_TTY) {
414 me->current_tag_charset = -1;
415 } else
416 #endif
417 if (me->T.transp) {
418 me->current_tag_charset = me->inUCLYhndl;
419 } else if (me->T.decode_utf8) {
420 me->current_tag_charset = me->inUCLYhndl;
421 } else if (me->T.do_8bitraw ||
422 me->T.use_raw_char_in) {
423 me->current_tag_charset = me->inUCLYhndl;
424 } else if (me->T.output_utf8 ||
425 me->T.trans_from_uni) {
426 me->current_tag_charset = UCGetLYhndl_byMIME("utf-8");
427 } else {
428 me->current_tag_charset = LATIN1;
429 }
430 }
431
432 static void change_chartrans_handling(HTStream *me)
433 {
434 int new_LYhndl = HTAnchor_getUCLYhndl(me->node_anchor,
435 UCT_STAGE_PARSER);
436
437 if (new_LYhndl != me->inUCLYhndl &&
438 new_LYhndl >= 0) {
439 /*
440 * Something changed. but ignore if a META wants an unknown charset.
441 */
442 LYUCcharset *new_UCI = HTAnchor_getUCInfoStage(me->node_anchor,
443 UCT_STAGE_PARSER);
444
445 if (new_UCI) {
446 LYUCcharset *next_UCI = HTAnchor_getUCInfoStage(me->node_anchor,
447 UCT_STAGE_STRUCTURED);
448 int next_LYhndl = HTAnchor_getUCLYhndl(me->node_anchor, UCT_STAGE_STRUCTURED);
449
450 me->inUCI = new_UCI;
451 me->inUCLYhndl = new_LYhndl;
452 me->outUCI = next_UCI;
453 me->outUCLYhndl = next_LYhndl;
454 set_chartrans_handling(me,
455 me->node_anchor, next_LYhndl);
456 }
457 }
458 }
459
460 #ifdef USE_COLOR_STYLE
461 #include <AttrList.h>
462 static int current_is_class = 0;
463 #endif
464
465 /* Handle Attribute
466 * ----------------
467 */
468 /* PUBLIC const char * SGML_default = ""; ?? */
469
470 static void handle_attribute_name(HTStream *me, const char *s)
471 {
472 HTTag *tag = me->current_tag;
473 const attr *attributes = tag->attributes;
474 int high, low, i, diff;
475
476 #ifdef USE_PRETTYSRC
477 if (psrc_view) {
478 attr_is_href = FALSE;
479 attr_is_name = FALSE;
480 }
481 #endif
482 /*
483 * Ignore unknown tag. - KW
484 */
485 if (tag == me->unknown_tag) {
486 #ifdef USE_PRETTYSRC
487 if (psrc_view)
488 me->current_attribute_number = 1; /* anything !=INVALID */
489 #endif
490 return;
491 }
492
493 /*
494 * Binary search for attribute name.
495 */
496 for (low = 0, high = tag->number_of_attributes;
497 high > low;
498 diff < 0 ? (low = i + 1) : (high = i)) {
499 i = (low + (high - low) / 2);
500 diff = my_casecomp(attributes[i].name, s);
501 if (diff == 0) { /* success: found it */
502 me->current_attribute_number = i;
503 #ifdef USE_PRETTYSRC
504 if (psrc_view) {
505 attr_is_name = (BOOL) (attributes[i].type == HTMLA_ANAME);
506 attr_is_href = (BOOL) (attributes[i].type == HTMLA_HREF);
507 } else
508 #endif
509 {
510 me->present[i] = YES;
511 Clear_extra(me->value[i]);
512 #ifdef USE_COLOR_STYLE
513 # ifdef USE_PRETTYSRC
514 current_is_class = IS_C(attributes[i]);
515 # else
516 current_is_class = (!strcasecomp("class", s));
517 # endif
518 CTRACE((tfp, "SGML: found attribute %s, %d\n", s, current_is_class));
519 #endif
520 }
521 return;
522 }
523 /* if */
524 } /* for */
525
526 CTRACE((tfp, "SGML: Unknown attribute %s for tag %s\n",
527 s, NonNull(me->current_tag->name)));
528 me->current_attribute_number = INVALID; /* Invalid */
529 }
530
531 /* Handle attribute value
532 * ----------------------
533 */
534 static void handle_attribute_value(HTStream *me, const char *s)
535 {
536 if (me->current_attribute_number != INVALID) {
537 StrAllocCopy_extra(me->value[me->current_attribute_number], s);
538 #ifdef USE_COLOR_STYLE
539 if (current_is_class) {
540 StrNCpy(class_string, s, TEMPSTRINGSIZE);
541 CTRACE((tfp, "SGML: class is '%s'\n", s));
542 } else {
543 CTRACE((tfp, "SGML: attribute value is '%s'\n", s));
544 }
545 #endif
546 } else {
547 CTRACE((tfp, "SGML: Attribute value %s ***ignored\n", s));
548 }
549 me->current_attribute_number = INVALID; /* can't have two assignments! */
550 }
551
552 /*
553 * Translate some Unicodes to Lynx special codes and output them.
554 * Special codes - ones those output depend on parsing.
555 *
556 * Additional issue, like handling bidirectional text if necessary
557 * may be called from here:
558 * zwnj (U+200C), zwj (U+200D), lrm (U+200E), rlm (U+200F)
559 * Currently they are ignored in SGML.c and LYCharUtils.c
560 * but also in UCdomap.c because they are non printable...
561 *
562 */
563 static BOOL put_special_unicodes(HTStream *me, UCode_t code)
564 {
565 /* (Tgf_nolyspcl) */
566 if (me->no_lynx_specialcodes) {
567 /*
568 * We were asked by a "DTD" flag to not generate lynx specials. - kw
569 */
570 return NO;
571 }
572
573 if (code == CH_NBSP) { /* S/390 -- gil -- 0657 */
574 /*
575 * Use Lynx special character for nbsp.
576 */
577 #ifdef USE_PRETTYSRC
578 if (!psrc_view)
579 #endif
580 PUTC(HT_NON_BREAK_SPACE);
581 } else if (code == CH_SHY) {
582 /*
583 * Use Lynx special character for shy.
584 */
585 #ifdef USE_PRETTYSRC
586 if (!psrc_view)
587 #endif
588 PUTC(LY_SOFT_HYPHEN);
589 } else if (code == 8194 || code == 8201) {
590 /*
591 * Use Lynx special character for ensp or thinsp.
592 *
593 * Originally, Lynx use space '32' as word delimiter and omits this
594 * space at end of line if word is wrapped to the next line. There are
595 * several other spaces in the Unicode repertoire and we should teach
596 * Lynx to understand them, not only as regular characters but in the
597 * context of line wrapping. Unfortunately, if we use HT_EN_SPACE we
598 * override the chartrans tables for those spaces with a single '32'
599 * for all (but do line wrapping more fancy).
600 *
601 * We may treat emsp as one or two ensp (below).
602 */
603 #ifdef USE_PRETTYSRC
604 if (!psrc_view)
605 #endif
606 PUTC(HT_EN_SPACE);
607 } else if (code == 8195) {
608 /*
609 * Use Lynx special character for emsp.
610 */
611 #ifdef USE_PRETTYSRC
612 if (!psrc_view) {
613 #endif
614 /* PUTC(HT_EN_SPACE); let's stay with a single space :) */
615 PUTC(HT_EN_SPACE);
616 #ifdef USE_PRETTYSRC
617 }
618 #endif
619 } else {
620 /*
621 * Return NO if nothing done.
622 */
623 return NO;
624 }
625 /*
626 * We have handled it.
627 */
628 return YES;
629 }
630
631 #ifdef USE_PRETTYSRC
632 static void put_pretty_entity(HTStream *me, int term)
633 {
634 PSRCSTART(entity);
635 PUTC('&');
636 PUTS(entity_string);
637 if (term)
638 PUTC((char) term);
639 PSRCSTOP(entity);
640 }
641
642 static void put_pretty_number(HTStream *me)
643 {
644 PSRCSTART(entity);
645 PUTS((me->isHex ? "&#x" : "&#"));
646 PUTS(entity_string);
647 PUTC(';');
648 PSRCSTOP(entity);
649 }
650 #endif /* USE_PRETTYSRC */
651
652 /* Handle entity
653 * -------------
654 *
655 * On entry,
656 * s contains the entity name zero terminated
657 * Bugs:
658 * If the entity name is unknown, the terminator is treated as
659 * a printable non-special character in all cases, even if it is '<'
660 * Bug-fix:
661 * Modified SGML_character() so we only come here with terminator
662 * as '\0' and check a FoundEntity flag. -- Foteos Macrides
663 *
664 * Modified more (for use with Lynx character translation code):
665 */
666 static char replace_buf[64]; /* buffer for replacement strings */
667 static BOOL FoundEntity = FALSE;
668
669 static void handle_entity(HTStream *me, int term)
670 {
671 UCode_t code;
672 long uck = -1;
673 const char *s = me->string->data;
674
675 /*
676 * Handle all entities normally. - FM
677 */
678 FoundEntity = FALSE;
679 if ((code = HTMLGetEntityUCValue(s)) != 0) {
680 /*
681 * We got a Unicode value for the entity name. Check for special
682 * Unicodes. - FM
683 */
684 if (put_special_unicodes(me, code)) {
685 #ifdef USE_PRETTYSRC
686 if (psrc_view) {
687 put_pretty_entity(me, term);
688 }
689 #endif
690 FoundEntity = TRUE;
691 return;
692 }
693 /*
694 * Seek a translation from the chartrans tables.
695 */
696 if ((uck = UCTransUniChar(code, me->outUCLYhndl)) >= 32 &&
697 /* =============== work in ASCII below here =============== S/390 -- gil -- 0672 */
698 uck < 256 &&
699 (uck < 127 ||
700 uck >= LYlowest_eightbit[me->outUCLYhndl])) {
701 #ifdef USE_PRETTYSRC
702 if (psrc_view) {
703 put_pretty_entity(me, term);
704 } else
705 #endif
706 PUTC(FROMASCII((char) uck));
707 FoundEntity = TRUE;
708 return;
709 } else if ((uck == ucNotFound ||
710 (me->T.repl_translated_C0 &&
711 uck > 0 && uck < 32)) &&
712 /*
713 * Not found; look for replacement string.
714 */
715 (uck = UCTransUniCharStr(replace_buf, 60, code,
716 me->outUCLYhndl, 0) >= 0)) {
717 #ifdef USE_PRETTYSRC
718 if (psrc_view) {
719 put_pretty_entity(me, term);
720 } else
721 #endif
722 PUTS(replace_buf);
723 FoundEntity = TRUE;
724 return;
725 }
726 /*
727 * If we're displaying UTF-8, try that now. - FM
728 */
729 #ifndef USE_PRETTYSRC
730 if (me->T.output_utf8 && PUTUTF8(code)) {
731 FoundEntity = TRUE;
732 return;
733 }
734 #else
735 if (me->T.output_utf8 && (psrc_view
736 ? (UCPutUtf8_charstring((HTStream *) me->target,
737 (putc_func_t *) (fake_put_character),
738 code))
739 : PUTUTF8(code))) {
740
741 if (psrc_view) {
742 put_pretty_entity(me, term);
743 }
744
745 FoundEntity = TRUE;
746 return;
747 }
748 #endif
749 /*
750 * If it's safe ASCII, use it. - FM
751 */
752 if (code >= 32 && code < 127) {
753 #ifdef USE_PRETTYSRC
754 if (psrc_view) {
755 put_pretty_entity(me, term);
756 } else
757 #endif
758
759 PUTC(FROMASCII((char) code));
760 FoundEntity = TRUE;
761 return;
762 }
763 /* =============== work in ASCII above here =============== S/390 -- gil -- 0682 */
764 /*
765 * Ignore zwnj (U+200C) and zwj (U+200D), if we get to here. Note that
766 * zwnj may have been handled as <WBR> by the calling function. - FM
767 */
768 if (!strcmp(s, "zwnj") ||
769 !strcmp(s, "zwj")) {
770 CTRACE((tfp, "handle_entity: Ignoring '%s'.\n", s));
771 #ifdef USE_PRETTYSRC
772 if (psrc_view) {
773 put_pretty_entity(me, term);
774 }
775 #endif
776 FoundEntity = TRUE;
777 return;
778 }
779 /*
780 * Ignore lrm (U+200E), and rln (U+200F), if we get to here. - FM
781 */
782 if (!strcmp(s, "lrm") ||
783 !strcmp(s, "rlm")) {
784 CTRACE((tfp, "handle_entity: Ignoring '%s'.\n", s));
785 #ifdef USE_PRETTYSRC
786 if (psrc_view) {
787 put_pretty_entity(me, term);
788 }
789 #endif
790 FoundEntity = TRUE;
791 return;
792 }
793 }
794
795 /*
796 * If entity string not found, display as text.
797 */
798 #ifdef USE_PRETTYSRC
799 if (psrc_view)
800 PSRCSTART(badseq);
801 #endif
802 /* S/390 -- gil -- 0695 */
803 CTRACE((tfp, "SGML: Unknown entity '%s' %" PRI_UCode_t " %ld\n", s, code, uck));
804 PUTC('&');
805 PUTS(s);
806 if (term != '\0')
807 PUTC(term);
808 #ifdef USE_PRETTYSRC
809 if (psrc_view)
810 PSRCSTOP(badseq);
811 #endif
812 }
813
814 /* Handle comment
815 * --------------
816 */
817 static void handle_comment(HTStream *me)
818 {
819 const char *s = me->string->data;
820
821 CTRACE((tfp, "SGML Comment:\n<%s>\n", s));
822
823 if (me->csi == NULL &&
824 StrNCmp(s, "!--#", 4) == 0 &&
825 LYCheckForCSI(me->node_anchor, &me->url) == TRUE) {
826 LYDoCSI(me->url, s, &me->csi);
827 } else {
828 LYCommentHacks(me->node_anchor, me->string->data);
829 }
830
831 return;
832 }
833
834 /* Handle identifier
835 * -----------------
836 */
837 static void handle_identifier(HTStream *me)
838 {
839 const char *s = me->string->data;
840
841 CTRACE((tfp, "SGML Identifier:\n<%s>\n", s));
842
843 return;
844 }
845
846 /* Handle doctype
847 * --------------
848 */
849 static void handle_doctype(HTStream *me)
850 {
851 const char *s = me->string->data;
852
853 CTRACE((tfp, "SGML Doctype:\n<%s>\n", s));
854 if (strstr(s, "DTD XHTML ") != NULL) {
855 CTRACE((tfp, "...processing extended HTML\n"));
856 me->extended_html = TRUE;
857 }
858
859 return;
860 }
861
862 /* Handle marked
863 * -------------
864 */
865 static void handle_marked(HTStream *me)
866 {
867 const char *s = me->string->data;
868
869 CTRACE((tfp, "SGML Marked Section:\n<%s>\n", s));
870
871 if (!StrNCmp(me->string->data, "![INCLUDE[", 10)) {
872 me->string->data[me->string->size - 3] = '\0';
873 StrAllocCat(me->include, me->string->data + 10);
874 /* @@@ This needs to take charset into account! @@@
875 the wrong assumptions will be made about the data's
876 charset once it is in include - kw */
877
878 } else if (!StrNCmp(me->string->data, "![CDATA[", 8)) {
879 (*me->actions->put_block) (me->target,
880 me->string->data + 8,
881 me->string->size - 11);
882
883 }
884 return;
885 }
886
887 /* Handle processing instruction
888 * -----------------------------
889 */
890 static void handle_processing_instruction(HTStream *me)
891 {
892 const char *s = me->string->data;
893
894 CTRACE((tfp, "SGML Processing instruction:\n<%s>\n", s));
895
896 if (!StrNCmp(s, "?xml ", 5)) {
897 int flag = me->T.decode_utf8;
898
899 me->strict_xml = TRUE;
900 /*
901 * Switch to UTF-8 if the encoding is explicitly "utf-8".
902 */
903 if (!flag) {
904 const char *t = strstr(s, "encoding=");
905
906 if (t != NULL) {
907 char delim = 0;
908
909 t += 9;
910 if (*t == '"' || *t == '\'')
911 delim = *t++;
912 flag = (!strncasecomp(t, "utf-8", 5) &&
913 (delim == 0 || t[5] == delim));
914 }
915 if (flag) {
916 CTRACE((tfp, "...Use UTF-8 for XML\n"));
917 me->T.decode_utf8 = TRUE;
918 }
919 }
920 }
921
922 return;
923 }
924
925 /* Handle sgmlent
926 * --------------
927 */
928 static void handle_sgmlent(HTStream *me)
929 {
930 const char *s = me->string->data;
931
932 CTRACE((tfp, "SGML Entity Declaration:\n<%s>\n", s));
933
934 return;
935 }
936
937 /* Handle sgmlent
938 * --------------
939 */
940 static void handle_sgmlele(HTStream *me)
941 {
942 const char *s = me->string->data;
943
944 CTRACE((tfp, "SGML Element Declaration:\n<%s>\n", s));
945
946 return;
947 }
948
949 /* Handle sgmlatt
950 * --------------
951 */
952 static void handle_sgmlatt(HTStream *me)
953 {
954 const char *s = me->string->data;
955
956 CTRACE((tfp, "SGML Attribute Declaration:\n<%s>\n", s));
957
958 return;
959 }
960
961 /*
962 * Convenience macros - tags (elements) are identified sometimes by an int or
963 * enum value ('TAGNUM'), sometimes by a pointer to HTTag ('TAGP'). - kw
964 */
965 #define TAGNUM_OF_TAGP(t) (HTMLElement) (t - me->dtd->tags)
966 #define TAGP_OF_TAGNUM(e) (me->dtd->tags + e)
967
968 /*
969 * The following implement special knowledge about OBJECT. As long as
970 * HTML_OBJECT is the only tag for which an alternative variant exist, they can
971 * be simple macros. - kw
972 */
973 /* does 'TAGNUM' e have an alternative (variant) parsing mode? */
974 #define HAS_ALT_TAGNUM(e) (e == HTML_OBJECT)
975
976 /* return 'TAGNUM' of the alternative mode for 'TAGNUM' e, if any. */
977 #define ALT_TAGNUM(e) ((e == HTML_OBJECT) ? HTML_ALT_OBJECT : e)
978
979 /* return 'TAGNUM' of the normal mode for 'TAGNUM' e which may be alt. */
980 #define NORMAL_TAGNUM(e) (((int)(e) >= HTML_ELEMENTS) ? HTML_OBJECT : (HTMLElement)e)
981
982 /* More convenience stuff. - kw */
983 #define ALT_TAGP_OF_TAGNUM(e) TAGP_OF_TAGNUM(ALT_TAGNUM(e))
984 #define NORMAL_TAGP_OF_TAGNUM(e) TAGP_OF_TAGNUM(NORMAL_TAGNUM(e))
985
986 #define ALT_TAGP(t) ALT_TAGP_OF_TAGNUM(TAGNUM_OF_TAGP(t))
987 #define NORMAL_TAGP(t) NORMAL_TAGP_OF_TAGNUM(TAGNUM_OF_TAGP(t))
988
989 #define IsTagAlias(a,b) (((a) == (b)) || ((a) - (a)->alias == (b) - (b)->alias))
990
991 static BOOL element_valid_within(HTTag * new_tag, HTTag * stacked_tag, int direct)
992 {
993 BOOL result = YES;
994 TagClass usecontains, usecontained;
995
996 if (stacked_tag && new_tag) {
997 usecontains = (direct ? stacked_tag->contains : stacked_tag->icontains);
998 usecontained = (direct ? new_tag->contained : new_tag->icontained);
999 if (IsTagAlias(new_tag, stacked_tag)) {
1000 result = (BOOL) ((Tgc_same & usecontains) &&
1001 (Tgc_same & usecontained));
1002 } else {
1003 result = (BOOL) ((new_tag->tagclass & usecontains) &&
1004 (stacked_tag->tagclass & usecontained));
1005 }
1006 }
1007 return result;
1008 }
1009
1010 static BOOL element_really_within(HTTag * new_tag, HTTag * stacked_tag, int direct)
1011 {
1012 BOOL result = YES;
1013 TagClass usecontains, usecontained;
1014
1015 if (stacked_tag && new_tag) {
1016 usecontains = (direct ? stacked_tag->contains : stacked_tag->icontains);
1017 usecontained = (direct ? new_tag->contained : new_tag->icontained);
1018 if (IsTagAlias(new_tag, stacked_tag)) {
1019 result = (BOOL) ((Tgc_same & usecontains) &&
1020 (Tgc_same & usecontained));
1021 } else {
1022 result = (BOOL) ((new_tag->tagclass & usecontains) ==
1023 new_tag->tagclass &&
1024 (stacked_tag->tagclass & usecontained) == stacked_tag->tagclass);
1025 }
1026 }
1027 return result;
1028 }
1029
1030 typedef enum {
1031 close_NO = 0,
1032 close_error = 1,
1033 close_valid = 2
1034 } canclose_t;
1035
1036 static canclose_t can_close(HTTag * new_tag, HTTag * stacked_tag)
1037 {
1038 canclose_t result;
1039
1040 if (!stacked_tag) {
1041 result = close_NO;
1042 } else if (stacked_tag->flags & Tgf_endO) {
1043 result = close_valid;
1044 } else if (IsTagAlias(new_tag, stacked_tag)) {
1045 result = ((Tgc_same & new_tag->canclose)
1046 ? close_error
1047 : close_NO);
1048 } else {
1049 result = ((stacked_tag->tagclass & new_tag->canclose)
1050 ? close_error
1051 : close_NO);
1052 }
1053 return result;
1054 }
1055
1056 static void do_close_stacked(HTStream *me)
1057 {
1058 HTElement *stacked = me->element_stack;
1059 HTMLElement e;
1060
1061 if (!stacked)
1062 return; /* stack was empty */
1063 if (me->inSELECT && !strcasecomp(stacked->tag->name, "SELECT")) {
1064 me->inSELECT = FALSE;
1065 }
1066 e = NORMAL_TAGNUM(TAGNUM_OF_TAGP(stacked->tag));
1067 #ifdef USE_PRETTYSRC
1068 if (!psrc_view) /* Don't actually pass call on if viewing psrc - kw */
1069 #endif
1070 (*me->actions->end_element) (me->target,
1071 (int) e,
1072 &me->include);
1073 me->element_stack = stacked->next;
1074 pool_free(stacked);
1075 me->no_lynx_specialcodes =
1076 (BOOL) (me->element_stack
1077 ? (me->element_stack->tag->flags & Tgf_nolyspcl)
1078 : NO);
1079 }
1080
1081 static int is_on_stack(HTStream *me, HTTag * old_tag)
1082 {
1083 HTElement *stacked = me->element_stack;
1084 int i = 1;
1085
1086 for (; stacked; stacked = stacked->next, i++) {
1087 if (IsTagAlias(stacked->tag, old_tag) ||
1088 stacked->tag == ALT_TAGP(old_tag))
1089 return i;
1090 }
1091 return 0;
1092 }
1093
1094 /* End element
1095 * -----------
1096 */
1097 static void end_element(HTStream *me, HTTag * old_tag)
1098 {
1099 BOOL extra_action_taken = NO;
1100 canclose_t canclose_check = close_valid;
1101 int stackpos = is_on_stack(me, old_tag);
1102 BOOL direct_container = YES;
1103
1104 if (!Old_DTD) {
1105 if (old_tag->aliases) {
1106 if (me->element_stack) {
1107 if (!element_really_within(old_tag,
1108 me->element_stack->tag,
1109 direct_container) &&
1110 element_really_within(old_tag + 1,
1111 me->element_stack->tag,
1112 direct_container)) {
1113 ++old_tag;
1114 }
1115 }
1116 }
1117 while (canclose_check != close_NO &&
1118 me->element_stack &&
1119 (stackpos > 1 || (!extra_action_taken && stackpos == 0))) {
1120 if (stackpos == 0 && (old_tag->flags & Tgf_startO) &&
1121 element_valid_within(old_tag, me->element_stack->tag, YES)) {
1122 CTRACE((tfp, "SGML: </%s> ignored\n", old_tag->name));
1123 return;
1124 }
1125 canclose_check = can_close(old_tag, me->element_stack->tag);
1126 if (canclose_check != close_NO) {
1127 CTRACE((tfp, "SGML: End </%s> \t<- %s end </%s>\n",
1128 me->element_stack->tag->name,
1129 ((canclose_check == close_valid)
1130 ? "supplied,"
1131 : "***forced by"),
1132 old_tag->name));
1133 do_close_stacked(me);
1134 extra_action_taken = YES;
1135 stackpos = is_on_stack(me, old_tag);
1136 }
1137 }
1138
1139 if (stackpos == 0 && old_tag->contents != SGML_EMPTY) {
1140 CTRACE((tfp, "SGML: Still open %s, ***no open %s for </%s>\n",
1141 me->element_stack ?
1142 me->element_stack->tag->name : "none",
1143 old_tag->name,
1144 old_tag->name));
1145 return;
1146 }
1147 if (stackpos > 1) {
1148 CTRACE((tfp,
1149 "SGML: Nesting <%s>...<%s> \t<- ***invalid end </%s>\n",
1150 old_tag->name,
1151 me->element_stack ?
1152 me->element_stack->tag->name : "none",
1153 old_tag->name));
1154 return;
1155 }
1156 }
1157 /* Now let the non-extended code deal with the rest. - kw */
1158
1159 /*
1160 * If we are in a SELECT block, ignore anything but a SELECT end tag. - FM
1161 */
1162 if (me->inSELECT) {
1163 if (!strcasecomp(old_tag->name, "SELECT")) {
1164 /*
1165 * Turn off the inSELECT flag and fall through. - FM
1166 */
1167 me->inSELECT = FALSE;
1168 } else {
1169 /*
1170 * Ignore the end tag. - FM
1171 */
1172 CTRACE((tfp, "SGML: ***Ignoring end tag </%s> in SELECT block.\n",
1173 old_tag->name));
1174 return;
1175 }
1176 }
1177 /*
1178 * Handle the end tag. - FM
1179 */
1180 CTRACE((tfp, "SGML: End </%s>\n", old_tag->name));
1181 if (old_tag->contents == SGML_EMPTY) {
1182 CTRACE((tfp, "SGML: ***Illegal end tag </%s> found.\n",
1183 old_tag->name));
1184 return;
1185 }
1186 #ifdef WIND_DOWN_STACK
1187 while (me->element_stack) /* Loop is error path only */
1188 #else
1189 if (me->element_stack) /* Substitute and remove one stack element */
1190 #endif /* WIND_DOWN_STACK */
1191 {
1192 int status = HT_OK;
1193 HTMLElement e;
1194 HTElement *N = me->element_stack;
1195 HTTag *t = (N->tag != old_tag) ? NORMAL_TAGP(N->tag) : N->tag;
1196
1197 if (old_tag != t) { /* Mismatch: syntax error */
1198 if (me->element_stack->next) { /* This is not the last level */
1199 CTRACE((tfp,
1200 "SGML: Found </%s> when expecting </%s>. </%s> ***assumed.\n",
1201 old_tag->name, t->name, t->name));
1202 } else { /* last level */
1203 CTRACE((tfp,
1204 "SGML: Found </%s> when expecting </%s>. </%s> ***Ignored.\n",
1205 old_tag->name, t->name, old_tag->name));
1206 return; /* Ignore */
1207 }
1208 }
1209
1210 e = NORMAL_TAGNUM(TAGNUM_OF_TAGP(t));
1211 CTRACE2(TRACE_SGML, (tfp, "tagnum(%p) = %d\n", (void *) t, (int) e));
1212 #ifdef USE_PRETTYSRC
1213 if (!psrc_view) /* Don't actually pass call on if viewing psrc - kw */
1214 #endif
1215 status = (*me->actions->end_element) (me->target,
1216 (int) e,
1217 &me->include);
1218 if (status == HT_PARSER_REOPEN_ELT) {
1219 CTRACE((tfp, "SGML: Restart <%s>\n", t->name));
1220 (*me->actions->start_element) (me->target,
1221 (int) e,
1222 NULL,
1223 NULL,
1224 me->current_tag_charset,
1225 &me->include);
1226 } else if (status == HT_PARSER_OTHER_CONTENT) {
1227 CTRACE((tfp, "SGML: Continue with other content model for <%s>\n", t->name));
1228 me->element_stack->tag = ALT_TAGP_OF_TAGNUM(e);
1229 } else {
1230 me->element_stack = N->next; /* Remove from stack */
1231 pool_free(N);
1232 }
1233 me->no_lynx_specialcodes =
1234 (BOOL) (me->element_stack
1235 ? (me->element_stack->tag->flags & Tgf_nolyspcl)
1236 : NO);
1237 #ifdef WIND_DOWN_STACK
1238 if (IsTagAlias(old_tag, t))
1239 return; /* Correct sequence */
1240 #else
1241 return;
1242 #endif /* WIND_DOWN_STACK */
1243
1244 /* Syntax error path only */
1245
1246 }
1247 CTRACE((tfp, "SGML: Extra end tag </%s> found and ignored.\n",
1248 old_tag->name));
1249 }
1250
1251 /* Start a element
1252 */
1253 static void start_element(HTStream *me)
1254 {
1255 int status;
1256 HTTag *new_tag = me->current_tag;
1257 HTMLElement e = TAGNUM_OF_TAGP(new_tag);
1258 BOOL ok = FALSE;
1259
1260 BOOL valid = YES;
1261 BOOL direct_container = YES;
1262 BOOL extra_action_taken = NO;
1263 canclose_t canclose_check = close_valid;
1264
1265 if (!Old_DTD) {
1266 if (new_tag->aliases) {
1267 if (me->element_stack) {
1268 if (!element_really_within(new_tag,
1269 me->element_stack->tag,
1270 direct_container) &&
1271 element_really_within(new_tag + 1,
1272 me->element_stack->tag,
1273 direct_container)) {
1274 ++new_tag;
1275 }
1276 }
1277 }
1278 while (me->element_stack &&
1279 (canclose_check == close_valid ||
1280 (canclose_check == close_error &&
1281 IsTagAlias(new_tag, me->element_stack->tag))) &&
1282 !(valid = element_valid_within(new_tag,
1283 me->element_stack->tag,
1284 direct_container))) {
1285 canclose_check = can_close(new_tag, me->element_stack->tag);
1286 if (canclose_check != close_NO) {
1287 CTRACE((tfp, "SGML: End </%s> \t<- %s start <%s>\n",
1288 me->element_stack->tag->name,
1289 ((canclose_check == close_valid)
1290 ? "supplied,"
1291 : "***forced by"),
1292 new_tag->name));
1293 do_close_stacked(me);
1294 extra_action_taken = YES;
1295 if (canclose_check == close_error)
1296 direct_container = NO;
1297 } else {
1298 CTRACE((tfp,
1299 "SGML: Still open %s \t<- ***invalid start <%s>\n",
1300 me->element_stack->tag->name,
1301 new_tag->name));
1302 }
1303 }
1304 if (me->element_stack && !valid &&
1305 (me->element_stack->tag->flags & Tgf_strict) &&
1306 !(valid = element_valid_within(new_tag,
1307 me->element_stack->tag,
1308 direct_container))) {
1309 CTRACE((tfp, "SGML: Still open %s \t<- ***ignoring start <%s>\n",
1310 me->element_stack->tag->name,
1311 new_tag->name));
1312 return;
1313 }
1314
1315 if (me->element_stack &&
1316 !extra_action_taken &&
1317 (canclose_check == close_NO) &&
1318 !valid && (new_tag->flags & Tgf_mafse)) {
1319 BOOL has_attributes = NO;
1320 int i = 0;
1321
1322 for (; i < new_tag->number_of_attributes && !has_attributes; i++)
1323 has_attributes = me->present[i];
1324 if (!has_attributes) {
1325 CTRACE((tfp,
1326 "SGML: Still open %s, ***converting invalid <%s> to </%s>\n",
1327 me->element_stack->tag->name,
1328 new_tag->name,
1329 new_tag->name));
1330 end_element(me, new_tag);
1331 return;
1332 }
1333 }
1334
1335 if (me->element_stack &&
1336 (canclose_check == close_error) &&
1337 !element_valid_within(new_tag,
1338 me->element_stack->tag,
1339 direct_container)) {
1340 CTRACE((tfp, "SGML: Still open %s \t<- ***invalid start <%s>\n",
1341 me->element_stack->tag->name,
1342 new_tag->name));
1343 }
1344 }
1345 /* Fall through to the non-extended code - kw */
1346
1347 /*
1348 * If we are not in a SELECT block, check if this is a SELECT start tag.
1349 * Otherwise (i.e., we are in a SELECT block) accept only OPTION as valid,
1350 * terminate the SELECT block if it is any other form-related element, and
1351 * otherwise ignore it. - FM
1352 */
1353 if (!me->inSELECT) {
1354 /*
1355 * We are not in a SELECT block, so check if this starts one. - FM
1356 * (frequent case!)
1357 */
1358 /* my_casecomp() - optimized by the first character */
1359 if (!my_casecomp(new_tag->name, "SELECT")) {
1360 /*
1361 * Set the inSELECT flag and fall through. - FM
1362 */
1363 me->inSELECT = TRUE;
1364 }
1365 } else {
1366 /*
1367 * We are in a SELECT block. - FM
1368 */
1369 if (strcasecomp(new_tag->name, "OPTION")) {
1370 /*
1371 * Ugh, it is not an OPTION. - FM
1372 */
1373 switch (e) {
1374 case HTML_INPUT:
1375 case HTML_TEXTAREA:
1376 case HTML_SELECT:
1377 case HTML_BUTTON:
1378 case HTML_FIELDSET:
1379 case HTML_LABEL:
1380 case HTML_LEGEND:
1381 case HTML_FORM:
1382 ok = TRUE;
1383 break;
1384 default:
1385 break;
1386 }
1387 if (ok) {
1388 /*
1389 * It is another form-related start tag, so terminate the
1390 * current SELECT block and fall through. - FM
1391 */
1392 CTRACE((tfp,
1393 "SGML: ***Faking SELECT end tag before <%s> start tag.\n",
1394 new_tag->name));
1395 end_element(me, SGMLFindTag(me->dtd, "SELECT"));
1396 } else {
1397 /*
1398 * Ignore the start tag. - FM
1399 */
1400 CTRACE((tfp,
1401 "SGML: ***Ignoring start tag <%s> in SELECT block.\n",
1402 new_tag->name));
1403 return;
1404 }
1405 }
1406 }
1407 /*
1408 * Handle the start tag. - FM
1409 */
1410 CTRACE((tfp, "SGML: Start <%s>\n", new_tag->name));
1411 status = (*me->actions->start_element) (me->target,
1412 (int) TAGNUM_OF_TAGP(new_tag),
1413 me->present,
1414 (STRING2PTR) me->value, /* coerce type for think c */
1415 me->current_tag_charset,
1416 &me->include);
1417 if (status == HT_PARSER_OTHER_CONTENT)
1418 new_tag = ALT_TAGP(new_tag); /* this is only returned for OBJECT */
1419 if (new_tag->contents != SGML_EMPTY) { /* i.e., tag not empty */
1420 HTElement *N = pool_alloc();
1421
1422 if (N == NULL)
1423 outofmem(__FILE__, "start_element");
1424
1425 N->next = me->element_stack;
1426 N->tag = new_tag;
1427 me->element_stack = N;
1428 me->no_lynx_specialcodes = (BOOLEAN) (new_tag->flags & Tgf_nolyspcl);
1429
1430 } else if (e == HTML_META) {
1431 /*
1432 * Check for result of META tag. - KW & FM
1433 */
1434 change_chartrans_handling(me);
1435 }
1436 }
1437
1438 /* Find Tag in DTD tag list
1439 * ------------------------
1440 *
1441 * On entry,
1442 * dtd points to dtd structure including valid tag list
1443 * string points to name of tag in question
1444 *
1445 * On exit,
1446 * returns:
1447 * NULL tag not found
1448 * else address of tag structure in dtd
1449 */
1450 HTTag *SGMLFindTag(const SGML_dtd * dtd,
1451 const char *s)
1452 {
1453 int high, low, i, diff;
1454 static HTTag *last[64] =
1455 {NULL}; /*optimize using the previous results */
1456 HTTag **res = last + (UCH(*s) % 64); /*pointer arithmetic */
1457
1458 if (*res) {
1459 if ((*res)->name == NULL)
1460 return NULL;
1461 if (!strcasecomp((*res)->name, s))
1462 return *res;
1463 }
1464
1465 for (low = 0, high = dtd->number_of_tags;
1466 high > low;
1467 diff < 0 ? (low = i + 1) : (high = i)) { /* Binary search */
1468 i = (low + (high - low) / 2);
1469 /* my_casecomp() - optimized by the first character, NOT_ASCII ok */
1470 diff = my_casecomp(dtd->tags[i].name, s); /* Case insensitive */
1471 if (diff == 0) { /* success: found it */
1472 i -= dtd->tags[i].alias;
1473 *res = &dtd->tags[i];
1474 return *res;
1475 }
1476 }
1477 if (IsNmStart(*s)) {
1478 /*
1479 * Unrecognized, but may be valid. - KW
1480 */
1481 return &HTTag_unrecognized;
1482 }
1483 return NULL;
1484 }
1485
1486 /*________________________________________________________________________
1487 * Public Methods
1488 */
1489
1490 /* Could check that we are back to bottom of stack! @@ */
1491 /* Do check! - FM */
1492 /* */
1493 static void SGML_free(HTStream *me)
1494 {
1495 int i;
1496 HTElement *cur;
1497 HTTag *t;
1498
1499 /*
1500 * Free the buffers. - FM
1501 */
1502 FREE(me->recover);
1503 FREE(me->url);
1504 FREE(me->csi);
1505 FREE(me->include);
1506 FREE(me->active_include);
1507
1508 /*
1509 * Wind down stack if any elements are open. - FM
1510 */
1511 while (me->element_stack) {
1512 cur = me->element_stack;
1513 t = cur->tag;
1514 me->element_stack = cur->next; /* Remove from stack */
1515 pool_free(cur);
1516 #ifdef USE_PRETTYSRC
1517 if (!psrc_view) /* Don't actually call on target if viewing psrc - kw */
1518 #endif
1519 (*me->actions->end_element)
1520 (me->target,
1521 (int) NORMAL_TAGNUM(TAGNUM_OF_TAGP(t)),
1522 &me->include);
1523 FREE(me->include);
1524 }
1525
1526 /*
1527 * Finish off the target. - FM
1528 */
1529 (*me->actions->_free) (me->target);
1530
1531 /*
1532 * Free the strings and context structure. - FM
1533 */
1534 HTChunkFree(me->string);
1535 for (i = 0; i < MAX_ATTRIBUTES; i++)
1536 FREE_extra(me->value[i]);
1537 FREE(me);
1538
1539 #ifdef USE_PRETTYSRC
1540 sgml_in_psrc_was_initialized = FALSE;
1541 #endif
1542 }
1543
1544 static void SGML_abort(HTStream *me, HTError e)
1545 {
1546 int i;
1547 HTElement *cur;
1548
1549 /*
1550 * Abort the target. - FM
1551 */
1552 (*me->actions->_abort) (me->target, e);
1553
1554 /*
1555 * Free the buffers. - FM
1556 */
1557 FREE(me->recover);
1558 FREE(me->include);
1559 FREE(me->active_include);
1560 FREE(me->url);
1561 FREE(me->csi);
1562
1563 /*
1564 * Free stack memory if any elements were left open. - KW
1565 */
1566 while (me->element_stack) {
1567 cur = me->element_stack;
1568 me->element_stack = cur->next; /* Remove from stack */
1569 pool_free(cur);
1570 }
1571
1572 /*
1573 * Free the strings and context structure. - FM
1574 */
1575 HTChunkFree(me->string);
1576 for (i = 0; i < MAX_ATTRIBUTES; i++)
1577 FREE_extra(me->value[i]);
1578 FREE(me);
1579
1580 #ifdef USE_PRETTYSRC
1581 sgml_in_psrc_was_initialized = FALSE;
1582 #endif
1583 }
1584
1585 /* Read and write user callback handle
1586 * -----------------------------------
1587 *
1588 * The callbacks from the SGML parser have an SGML context parameter.
1589 * These calls allow the caller to associate his own context with a
1590 * particular SGML context.
1591 */
1592
1593 #ifdef CALLERDATA
1594 void *SGML_callerData(HTStream *me)
1595 {
1596 return me->callerData;
1597 }
1598
1599 void SGML_setCallerData(HTStream *me, void *data)
1600 {
1601 me->callerData = data;
1602 }
1603 #endif /* CALLERDATA */
1604
1605 #ifdef USE_PRETTYSRC
1606 static void transform_tag(HTStream *me, HTChunk *string)
1607 {
1608 if (!me->strict_xml) {
1609 if (tagname_transform != 1) {
1610 if (tagname_transform == 0)
1611 LYLowerCase(string->data);
1612 else
1613 LYUpperCase(string->data);
1614 }
1615 }
1616 }
1617 #endif /* USE_PRETTYSRC */
1618
1619 static BOOL ignore_when_empty(HTTag * tag)
1620 {
1621 BOOL result = FALSE;
1622
1623 if (!LYPreparsedSource
1624 && LYxhtml_parsing
1625 && tag->name != NULL
1626 && !(tag->flags & Tgf_mafse)
1627 && tag->contents != SGML_EMPTY
1628 && tag->tagclass != Tgc_Plike
1629 && (tag->tagclass == Tgc_APPLETlike
1630 || tag->tagclass == Tgc_SELECTlike
1631 || (tag->contains && tag->icontains))) {
1632 result = TRUE;
1633 }
1634 CTRACE((tfp, "SGML Do%s ignore_when_empty:%s\n",
1635 result ? "" : " not",
1636 NonNull(tag->name)));
1637 return result;
1638 }
1639
1640 static void discard_empty(HTStream *me)
1641 {
1642 static HTTag empty_tag;
1643
1644 CTRACE((tfp, "SGML discarding empty %s\n",
1645 NonNull(me->current_tag->name)));
1646 CTRACE_FLUSH(tfp);
1647
1648 memset(&empty_tag, 0, sizeof(empty_tag));
1649 me->current_tag = &empty_tag;
1650 me->string->size = 0;
1651
1652 /* do not call end_element() if start_element() was not called */
1653 }
1654
1655 #ifdef USE_PRETTYSRC
1656 static BOOL end_if_prettysrc(HTStream *me, HTChunk *string, int end_ch)
1657 {
1658 BOOL result = psrc_view;
1659
1660 if (psrc_view) {
1661 if (attr_is_name) {
1662 HTStartAnchor(me->target, string->data, NULL);
1663 (*me->actions->end_element) (me->target,
1664 HTML_A,
1665 &me->include);
1666 } else if (attr_is_href) {
1667 PSRCSTART(href);
1668 HTStartAnchor(me->target, NULL, string->data);
1669 }
1670 PUTS_TR(string->data);
1671 if (attr_is_href) {
1672 (*me->actions->end_element) (me->target,
1673 HTML_A,
1674 &me->include);
1675 PSRCSTOP(href);
1676 }
1677 if (end_ch)
1678 PUTC(end_ch);
1679 PSRCSTOP(attrval);
1680 }
1681 return result;
1682 }
1683 #endif
1684
1685 static void SGML_character(HTStream *me, int c_in)
1686 {
1687 const SGML_dtd *dtd = me->dtd;
1688 HTChunk *string = me->string;
1689 const char *EntityName;
1690 HTTag *testtag = NULL;
1691 BOOLEAN chk; /* Helps (?) walk through all the else ifs... */
1692 UCode_t clong, uck = 0; /* Enough bits for UCS4 ... */
1693 int testlast;
1694
1695 unsigned char c;
1696 unsigned char saved_char_in = '\0';
1697
1698 ++sgml_offset;
1699
1700 c = UCH(c_in);
1701 clong = UCH(c);
1702
1703 if (me->T.decode_utf8) {
1704 switch (HTDecodeUTF8(&(me->U), &c_in, &clong)) {
1705 case dUTF8_ok:
1706 if (clong < 256) {
1707 c_in = FROMASCII(UCH(clong));
1708 }
1709 break;
1710 case dUTF8_err:
1711 clong = UCS_REPL;
1712 strcpy(me->U.utf_buf, "\357\277\275");
1713 me->U.utf_buf_p = (me->U.utf_buf + 3);
1714 break;
1715 case dUTF8_more:
1716 return;
1717 }
1718
1719 c = UCH(c_in);
1720 if ((me->U.utf_buf_p - me->U.utf_buf) > 1) {
1721 goto top1;
1722 }
1723 }
1724
1725 /*
1726 * If we want the raw input converted to Unicode, try that now. - FM
1727 */
1728 #ifdef USE_JAPANESEUTF8_SUPPORT
1729 /* Convert ISO-2022-JP to Unicode (charset=iso-2022-jp is unrecognized) */
1730 #define IS_JIS7_HILO(c) (0x20<(c)&&(c)<0x7F)
1731 if (UTF8_TTY_ISO2022JP && (me->state == S_nonascii_text
1732 || me->state == S_nonascii_text_sq
1733 || me->state == S_nonascii_text_dq)) {
1734 /* end of ISO-2022-JP? || not in ISO-2022-JP range */
1735 if (TOASCII(c) == '\033' || !IS_JIS7_HILO(c)) {
1736 me->kanji_buf = '\0';
1737 goto top1;
1738 }
1739 if (me->kanji_buf == '\t') { /* flag for single byte kana in "ESC(I" */
1740 if (conv_jisx0201kana) {
1741 JISx0201TO0208_SJIS(c | 0200,
1742 (unsigned char *) me->U.utf_buf,
1743 (unsigned char *) me->U.utf_buf + 1);
1744 clong = UCTransJPToUni(me->U.utf_buf, 2,
1745 UCGetLYhndl_byMIME("shift_jis"));
1746 } else {
1747 clong = UCTransToUni(c | 0200, UCGetLYhndl_byMIME("shift_jis"));
1748 }
1749 } else if (me->kanji_buf) {
1750 me->U.utf_buf[0] = (char) (me->kanji_buf | 0200); /* to EUC-JP */
1751 me->U.utf_buf[1] = (char) (c | 0200);
1752 clong = UCTransJPToUni(me->U.utf_buf, 2,
1753 UCGetLYhndl_byMIME("euc-jp"));
1754 me->kanji_buf = '\0';
1755 } else {
1756 me->kanji_buf = c;
1757 clong = ucNeedMore;
1758 }
1759 goto top1;
1760 }
1761 #endif /* USE_JAPANESEUTF8_SUPPORT */
1762 #ifdef USE_JAPANESEUTF8_SUPPORT
1763 if (me->T.trans_to_uni &&
1764 ((strcmp(LYCharSet_UC[me->inUCLYhndl].MIMEname, "euc-jp") == 0) ||
1765 (strcmp(LYCharSet_UC[me->inUCLYhndl].MIMEname, "shift_jis") == 0))) {
1766 if (strcmp(LYCharSet_UC[me->inUCLYhndl].MIMEname, "shift_jis") == 0) {
1767 if (me->U.utf_count == 0) {
1768 if (IS_SJIS_HI1(c) ||
1769 IS_SJIS_HI2(c)) {
1770 me->U.utf_buf[0] = (char) c;
1771 me->U.utf_count = 1;
1772 clong = ucCannotConvert;
1773 } else if (IS_SJIS_X0201KANA(c)) {
1774 if (conv_jisx0201kana) {
1775 JISx0201TO0208_SJIS(c,
1776 (unsigned char *) me->U.utf_buf,
1777 (unsigned char *) me->U.utf_buf + 1);
1778 clong = UCTransJPToUni(me->U.utf_buf, 2, me->inUCLYhndl);
1779 } else {
1780 clong = UCTransToUni(c, me->inUCLYhndl);
1781 }
1782 }
1783 } else {
1784 if (IS_SJIS_LO(c)) {
1785 me->U.utf_buf[1] = (char) c;
1786 clong = UCTransJPToUni(me->U.utf_buf, 2, me->inUCLYhndl);
1787 }
1788 me->U.utf_count = 0;
1789 }
1790 } else {
1791 if (me->U.utf_count == 0) {
1792 if (IS_EUC_HI(c) || c == 0x8E) {
1793 me->U.utf_buf[0] = (char) c;
1794 me->U.utf_count = 1;
1795 clong = ucCannotConvert;
1796 }
1797 } else {
1798 if (IS_EUC_LOX(c)) {
1799 me->U.utf_buf[1] = (char) c;
1800 clong = UCTransJPToUni(me->U.utf_buf, 2, me->inUCLYhndl);
1801 }
1802 me->U.utf_count = 0;
1803 }
1804 }
1805 goto top1;
1806 } else
1807 #endif /* USE_JAPANESEUTF8_SUPPORT */
1808 #ifdef EXP_CHINESEUTF8_SUPPORT
1809 if (me->T.trans_to_uni &&
1810 ((strcmp(LYCharSet_UC[me->inUCLYhndl].MIMEname, "euc-cn") == 0))) {
1811 if (me->U.utf_count == 0) {
1812 if (IS_GBK_HI(c)) {
1813 me->U.utf_buf[0] = (char) c;
1814 me->U.utf_count = 1;
1815 clong = ucCannotConvert;
1816 CTRACE((tfp, "Get EUC-CN: 0x%02X\n", UCH(c)));
1817 }
1818 } else {
1819 if (IS_GBK_LO(c)) {
1820 me->U.utf_buf[1] = (char) c;
1821 clong = UCTransJPToUni(me->U.utf_buf, 2, me->inUCLYhndl);
1822 if (clong > 0) {
1823 CTRACE((tfp, "... second: [%02X%02X] U+%04lX\n",
1824 UCH(me->U.utf_buf[0]),
1825 UCH(me->U.utf_buf[1]),
1826 clong));
1827 } else {
1828 CTRACE((tfp, "... second: [%02X%02X] %ld\n",
1829 UCH(me->U.utf_buf[0]),
1830 UCH(me->U.utf_buf[1]),
1831 clong));
1832 }
1833 }
1834 me->U.utf_count = 0;
1835 }
1836 goto top1;
1837 } else
1838 #endif /* EXP_CHINESEUTF8_SUPPORT */
1839 #ifdef EXP_CHINESEUTF8_SUPPORT
1840 if (me->T.trans_to_uni &&
1841 ((strcmp(LYCharSet_UC[me->inUCLYhndl].MIMEname, "euc-kr") == 0))) {
1842 if (me->U.utf_count == 0) {
1843 if (IS_EUC_HI(c)) {
1844 me->U.utf_buf[0] = (char) c;
1845 me->U.utf_count = 1;
1846 clong = ucCannotConvert;
1847 CTRACE((tfp, "Get EUC-KR: 0x%02X\n", UCH(c)));
1848 }
1849 } else {
1850 if (IS_EUC_LOS(c) ||
1851 IS_EUC_LOX(c)) {
1852 me->U.utf_buf[1] = (char) c;
1853 clong = UCTransJPToUni(me->U.utf_buf, 2, me->inUCLYhndl);
1854 if (clong > 0) {
1855 CTRACE((tfp, "... second: [%02X%02X] U+%04lX\n",
1856 UCH(me->U.utf_buf[0]),
1857 UCH(me->U.utf_buf[1]),
1858 clong));
1859 } else {
1860 CTRACE((tfp, "... second: [%02X%02X] %ld\n",
1861 UCH(me->U.utf_buf[0]),
1862 UCH(me->U.utf_buf[1]),
1863 clong));
1864 }
1865 }
1866 me->U.utf_count = 0;
1867 }
1868 goto top1;
1869 } else
1870 #endif /* EXP_CHINESEUTF8_SUPPORT */
1871 #ifdef EXP_CHINESEUTF8_SUPPORT
1872 if (me->T.trans_to_uni &&
1873 ((strcmp(LYCharSet_UC[me->inUCLYhndl].MIMEname, "big5") == 0))) {
1874 if (me->U.utf_count == 0) {
1875 if (IS_BIG5_HI(c)) {
1876 me->U.utf_buf[0] = (char) c;
1877 me->U.utf_count = 1;
1878 clong = ucCannotConvert;
1879 CTRACE((tfp, "Get BIG5: 0x%02X\n", UCH(c)));
1880 }
1881 } else {
1882 if (IS_BIG5_LOS(c) ||
1883 IS_BIG5_LOX(c)) {
1884 me->U.utf_buf[1] = (char) c;
1885 clong = UCTransJPToUni(me->U.utf_buf, 2, me->inUCLYhndl);
1886 if (clong > 0) {
1887 CTRACE((tfp, "... second: [%02X%02X] U+%04lX\n",
1888 UCH(me->U.utf_buf[0]),
1889 UCH(me->U.utf_buf[1]),
1890 clong));
1891 } else {
1892 CTRACE((tfp, "... second: [%02X%02X] %ld\n",
1893 UCH(me->U.utf_buf[0]),
1894 UCH(me->U.utf_buf[1]),
1895 clong));
1896 }
1897 }
1898 me->U.utf_count = 0;
1899 }
1900 goto top1;
1901 } else
1902 #endif /* EXP_CHINESEUTF8_SUPPORT */
1903 if (me->T.trans_to_uni &&
1904 /* S/390 -- gil -- 0744 */
1905 ((TOASCII(clong) >= LYlowest_eightbit[me->inUCLYhndl]) ||
1906 (clong < ' ' && clong != 0 &&
1907 me->T.trans_C0_to_uni))) {
1908 /*
1909 * Convert the octet to Unicode. - FM
1910 */
1911 clong = UCTransToUni((char) c, me->inUCLYhndl);
1912 if (clong > 0) {
1913 saved_char_in = c;
1914 if (clong < 256) {
1915 c = FROMASCII(UCH(clong));
1916 }
1917 }
1918 goto top1;
1919 } else if (clong < ' ' && clong != 0 && /* S/390 -- gil -- 0768 */
1920 me->T.trans_C0_to_uni) {
1921 /*
1922 * This else if may be too ugly to keep. - KW
1923 */
1924 if (me->T.trans_from_uni &&
1925 (((clong = UCTransToUni((char) c, me->inUCLYhndl)) >= ' ') ||
1926 (me->T.transp &&
1927 (clong = UCTransToUni((char) c, me->inUCLYhndl)) > 0))) {
1928 saved_char_in = c;
1929 if (clong < 256) {
1930 c = FROMASCII(UCH(clong));
1931 }
1932 goto top1;
1933 } else {
1934 uck = ucError;
1935 if (me->T.transp) {
1936 uck = UCTransCharStr(replace_buf, 60, (char) c,
1937 me->inUCLYhndl,
1938 me->inUCLYhndl, NO);
1939 }
1940 if (!me->T.transp || uck < 0) {
1941 uck = UCTransCharStr(replace_buf, 60, (char) c,
1942 me->inUCLYhndl,
1943 me->outUCLYhndl, YES);
1944 }
1945 if (uck == 0) {
1946 return;
1947 } else if (uck < 0) {
1948 goto top0a;
1949 }
1950 c = UCH(replace_buf[0]);
1951 if (c && replace_buf[1]) {
1952 if (me->state == S_text) {
1953 PUTS(replace_buf);
1954 return;
1955 }
1956 StrAllocCat(me->recover, replace_buf + 1);
1957 }
1958 goto top0a;
1959 } /* Next line end of ugly stuff for C0. - KW */
1960 } else { /* end of me->T.trans_to_uni S/390 -- gil -- 0791 */
1961 goto top0a;
1962 }
1963
1964 /*
1965 * We jump up to here from below if we have
1966 * stuff in the recover, insert, or csi buffers
1967 * to process. We zero saved_char_in, in effect
1968 * as a flag that the octet is not that of the
1969 * actual call to this function. This may be OK
1970 * for now, for the stuff this function adds to
1971 * its recover buffer, but it might not be for
1972 * stuff other functions added to the insert or
1973 * csi buffer, so bear that in mind. - FM
1974 * Stuff from the recover buffer is now handled
1975 * as UTF-8 if we can expect that's what it is,
1976 * and in that case we don't come back up here. - kw
1977 */
1978 top:
1979 saved_char_in = '\0';
1980 /*
1981 * We jump to here from above when we don't have
1982 * UTF-8 input, haven't converted to Unicode, and
1983 * want clong set to the input octet (unsigned)
1984 * without zeroing its saved_char_in copy (which
1985 * is signed). - FM
1986 */
1987 top0a:
1988 *(me->U.utf_buf) = '\0';
1989 clong = UCH(c);
1990 /*
1991 * We jump to here from above if we have converted
1992 * the input, or a multibyte sequence across calls,
1993 * to a Unicode value and loaded it into clong (to
1994 * which unsign_c has been defined), and from below
1995 * when we are recycling a character (e.g., because
1996 * it terminated an entity but is not the standard
1997 * semi-colon). The character will already have
1998 * been put through the Unicode conversions. - FM
1999 */
2000 top1:
2001 /*
2002 * Ignore low ISO 646 7-bit control characters if HTCJK is not set. - FM
2003 */
2004 /*
2005 * Works for both ASCII and EBCDIC. -- gil
2006 * S/390 -- gil -- 0811
2007 */
2008 if (TOASCII(clong) < 32 &&
2009 c != '\t' && c != '\n' && c != '\r' &&
2010 !IS_CJK_TTY &&
2011 !(UTF8_TTY_ISO2022JP && (TOASCII(c) == '\033')))
2012 goto after_switch;
2013
2014 /*
2015 * Ignore 127 if we don't have HTPassHighCtrlRaw or HTCJK set. - FM
2016 */
2017 #define PASSHICTRL (me->T.transp || \
2018 clong >= LYlowest_eightbit[me->inUCLYhndl])
2019 if (TOASCII(c) == 127 && /* S/390 -- gil -- 0830 */
2020 !(PASSHICTRL || IS_CJK_TTY))
2021 goto after_switch;
2022
2023 /*
2024 * Ignore 8-bit control characters 128 - 159 if neither HTPassHighCtrlRaw
2025 * nor HTCJK is set. - FM
2026 */
2027 if (TOASCII(clong) > 127 && TOASCII(clong) < 160 && /* S/390 -- gil -- 0847 */
2028 !(PASSHICTRL || IS_CJK_TTY)) {
2029 /*
2030 * If we happen to be reading from an "ISO-8859-1" or "US-ASCII"
2031 * document, allow the cp-1252 codes, to accommodate the HTML5 draft
2032 * recommendation for replacement encoding:
2033 *
2034 * http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#character-encodings-0
2035 */
2036 if (AssumeCP1252(me)) {
2037 clong = LYcp1252ToUnicode((UCode_t) c);
2038 goto top1;
2039 }
2040 goto after_switch;
2041 }
2042
2043 /* Almost all CJK characters are double byte but only Japanese
2044 * JIS X0201 Kana is single byte. To prevent to fail SGML parsing
2045 * we have to take care of them here. -- TH
2046 */
2047 if ((HTCJK == JAPANESE) && (me->state == S_in_kanji) &&
2048 !IS_JAPANESE_2BYTE(me->kanji_buf, UCH(c))
2049 #ifdef USE_JAPANESEUTF8_SUPPORT
2050 && !me->T.decode_utf8
2051 #endif
2052 ) {
2053 #ifdef CONV_JISX0201KANA_JISX0208KANA
2054 if (IS_SJIS_X0201KANA(me->kanji_buf)) {
2055 unsigned char sjis_hi, sjis_lo;
2056
2057 JISx0201TO0208_SJIS(me->kanji_buf, &sjis_hi, &sjis_lo);
2058 PUTC(sjis_hi);
2059 PUTC(sjis_lo);
2060 } else
2061 #endif
2062 PUTC(me->kanji_buf);
2063 me->state = S_text;
2064 }
2065
2066 /*
2067 * Handle character based on me->state.
2068 */
2069 CTRACE2(TRACE_SGML, (tfp, "SGML before %s|%.*s|%c|\n",
2070 state_name(me->state),
2071 string->size,
2072 NonNull(string->data),
2073 UCH(c)));
2074 switch (me->state) {
2075
2076 case S_in_kanji:
2077 /*
2078 * Note that if we don't have a CJK input, then this is not the second
2079 * byte of a CJK di-byte, and we're trashing the input. That's why
2080 * 8-bit characters followed by, for example, '<' can cause the tag to
2081 * be treated as text, not markup. We could try to deal with it by
2082 * holding each first byte and then checking byte pairs, but that
2083 * doesn't seem worth the overhead (see below). - FM
2084 */
2085 me->state = S_text;
2086 PUTC(me->kanji_buf);
2087 PUTC(c);
2088 break;
2089
2090 case S_tagname_slash:
2091 /*
2092 * We had something link "<name/" so far, set state to S_text but keep
2093 * me->slashedtag as a flag; except if we get '>' directly
2094 * after the "<name/", and really have a tag for that name in
2095 * me->slashedtag, in which case keep state as is and let code
2096 * below deal with it. - kw
2097 */
2098 if (!(c == '>' && me->slashedtag && TOASCII(clong) < 127)) {
2099 me->state = S_text;
2100 }
2101 /* FALLTHRU */
2102 case S_text:
2103 #ifdef EXP_CHINESEUTF8_SUPPORT
2104 if (IS_CJK_TTY &&
2105 (!strcmp(LYCharSet_UC[me->inUCLYhndl].MIMEname, "euc-cn") ||
2106 !strcmp(LYCharSet_UC[me->inUCLYhndl].MIMEname, "big5") ||
2107 !strcmp(LYCharSet_UC[me->inUCLYhndl].MIMEname, "euc-kr"))) {
2108 /*
2109 * Leave the case statement if we have not collected both of the
2110 * bytes for the EUC-CN character. If we have, then continue on
2111 * to convert it to Unicode.
2112 */
2113 if (clong == ucCannotConvert) {
2114 break;
2115 }
2116 } else
2117 #endif
2118 if (IS_CJK_TTY && ((TOASCII(c) & 0200) != 0)
2119 #ifdef USE_JAPANESEUTF8_SUPPORT
2120 && !me->T.decode_utf8
2121 #endif
2122 ) { /* S/390 -- gil -- 0864 */
2123 /*
2124 * Setting up for Kanji multibyte handling (based on Takuya ASADA's
2125 * ([email protected]) CJK Lynx). Note that if the input is not
2126 * in fact CJK, the next byte also will be mishandled, as explained
2127 * above. Toggle raw mode off in such cases, or select the "7 bit
2128 * approximations" display character set, which is largely
2129 * equivalent to having raw mode off with CJK. - FM
2130 */
2131 me->state = S_in_kanji;
2132 me->kanji_buf = c;
2133 break;
2134 } else if ((IS_CJK_TTY || UTF8_TTY_ISO2022JP) && TOASCII(c) == '\033') {
2135 /* S/390 -- gil -- 0881 */
2136 /*
2137 * Setting up for CJK escape sequence handling (based on Takuya
2138 * ASADA's ([email protected]) CJK Lynx). - FM
2139 */
2140 me->state = S_esc;
2141 if (!UTF8_TTY_ISO2022JP)
2142 PUTC(c);
2143 break;
2144 }
2145
2146 if (c == '&' || c == '<') {
2147 #ifdef USE_PRETTYSRC
2148 if (psrc_view) { /*there is nothing useful in the element_stack */
2149 testtag = me->current_tag;
2150 } else
2151 #endif
2152 {
2153 testtag = me->element_stack ?
2154 me->element_stack->tag : NULL;
2155 }
2156 }
2157
2158 if (c == '&' && TOASCII(clong) < 127 && /* S/390 -- gil -- 0898 */
2159 (!testtag ||
2160 (testtag->contents == SGML_MIXED ||
2161 testtag->contents == SGML_ELEMENT ||
2162 testtag->contents == SGML_PCDATA ||
2163 #ifdef USE_PRETTYSRC
2164 testtag->contents == SGML_EMPTY ||
2165 #endif
2166 testtag->contents == SGML_RCDATA))) {
2167 /*
2168 * Setting up for possible entity, without the leading '&'. - FM
2169 */
2170 string->size = 0;
2171 me->state = S_ero;
2172 } else if (c == '<' && TOASCII(clong) < 127) { /* S/390 -- gil -- 0915 */
2173 /*
2174 * Setting up for possible tag. - FM
2175 */
2176 string->size = 0;
2177 if (testtag && testtag->contents == SGML_PCDATA) {
2178 me->state = S_pcdata;
2179 } else if (testtag && (testtag->contents == SGML_LITTERAL
2180 || testtag->contents == SGML_CDATA)) {
2181 me->state = S_litteral;
2182 } else if (testtag && (testtag->contents == SGML_SCRIPT)) {
2183 me->state = S_script;
2184 } else {
2185 me->state = S_tag;
2186 }
2187 me->slashedtag = NULL;
2188 } else if (me->slashedtag &&
2189 me->slashedtag->name &&
2190 (c == '/' ||
2191 (c == '>' && me->state == S_tagname_slash)) &&
2192 TOASCII(clong) < 127) {
2193 /*
2194 * We got either the second slash of a pending "<NAME/blah blah/"
2195 * shortref construct, or the '>' of a mere "<NAME/>". In both
2196 * cases generate a "</NAME>" end tag in the recover buffer for
2197 * reparsing unless NAME is really an empty element. - kw
2198 */
2199 #ifdef USE_PRETTYSRC
2200 if (psrc_view) {
2201 PSRCSTART(abracket);
2202 PUTC(c);
2203 PSRCSTOP(abracket);
2204 } else
2205 #endif
2206 if (me->slashedtag != me->unknown_tag &&
2207 !ReallyEmptyTag(me->slashedtag)) {
2208 if (me->recover == NULL) {
2209 StrAllocCopy(me->recover, "</");
2210 me->recover_index = 0;
2211 } else {
2212 StrAllocCat(me->recover, "</");
2213 }
2214 StrAllocCat(me->recover, me->slashedtag->name);
2215 StrAllocCat(me->recover, ">");
2216 }
2217 me->slashedtag = NULL;
2218
2219 } else if (me->element_stack &&
2220 (me->element_stack->tag->flags & Tgf_frecyc)) {
2221 /*
2222 * The element stack says we are within the contents of an element
2223 * that the next stage (HTML.c) may want to feed us back again (via
2224 * the *include string). So try to output text in UTF-8 if
2225 * possible, using the same logic as for attribute values (which
2226 * should be in line with what me->current_tag_charset
2227 * indicates). - kw
2228 */
2229 if (me->T.decode_utf8 &&
2230 *me->U.utf_buf) {
2231 PUTS(me->U.utf_buf);
2232 me->U.utf_buf_p = me->U.utf_buf;
2233 *(me->U.utf_buf_p) = '\0';
2234 } else if (!IS_CJK_TTY &&
2235 (me->T.output_utf8 ||
2236 me->T.trans_from_uni)) {
2237 if (LYIsASCII(clong)) {
2238 PUTC(c);
2239 } else if (clong == UCS_REPL && saved_char_in &&
2240 HTPassEightBitRaw &&
2241 saved_char_in >=
2242 LYlowest_eightbit[me->outUCLYhndl]) {
2243 PUTUTF8((UCode_t) (0xf000 | saved_char_in));
2244 } else {
2245 PUTUTF8(clong);
2246 }
2247 } else if (saved_char_in && me->T.use_raw_char_in) {
2248 PUTC(saved_char_in);
2249 } else {
2250 PUTC(c);
2251 }
2252
2253 #define PASS8859SPECL me->T.pass_160_173_raw
2254 /*
2255 * Convert 160 (nbsp) to Lynx special character if neither
2256 * HTPassHighCtrlRaw nor HTCJK is set. - FM
2257 */
2258 } else if (clong == CH_NBSP && /* S/390 -- gil -- 0932 */
2259 !me->no_lynx_specialcodes &&
2260 !(PASS8859SPECL || IS_CJK_TTY)) {
2261 PUTC(HT_NON_BREAK_SPACE);
2262 /*
2263 * Convert 173 (shy) to Lynx special character if neither
2264 * HTPassHighCtrlRaw nor HTCJK is set. - FM
2265 */
2266 } else if (clong == CH_SHY && /* S/390 -- gil -- 0949 */
2267 !me->no_lynx_specialcodes &&
2268 !(PASS8859SPECL || IS_CJK_TTY)) {
2269 PUTC(LY_SOFT_HYPHEN);
2270 /*
2271 * Handle the case in which we think we have a character which
2272 * doesn't need further processing (e.g., a koi8-r input for a
2273 * koi8-r output). - FM
2274 */
2275 } else if (me->T.use_raw_char_in && saved_char_in) {
2276 /*
2277 * Only if the original character is still in saved_char_in,
2278 * otherwise we may be iterating from a goto top. - KW
2279 */
2280 PUTC(saved_char_in);
2281 } else if ((chk = (BOOL) (me->T.trans_from_uni &&
2282 TOASCII(clong) >= 160)) && /* S/390 -- gil -- 0968 */
2283 (uck = UCTransUniChar(clong,
2284 me->outUCLYhndl)) >= ' ' &&
2285 uck < 256) {
2286 CTRACE((tfp, "UCTransUniChar returned 0x%.2" PRI_UCode_t
2287 ":'%c'.\n",
2288 uck, FROMASCII((char)uck)));
2289 /*
2290 * We got one octet from the conversions, so use it. - FM
2291 */
2292 PUTC(FROMASCII((char) uck));
2293 } else if ((chk &&
2294 (uck == ucNotFound ||
2295 (me->T.repl_translated_C0 &&
2296 uck > 0 && uck < 32))) &&
2297 /*
2298 * Not found; look for replacement string. - KW
2299 */
2300 (uck = UCTransUniCharStr(replace_buf, 60, clong,
2301 me->outUCLYhndl,
2302 0) >= 0)) {
2303 /*
2304 * Got a replacement string. No further tests for validity -
2305 * assume that whoever defined replacement strings knew what she
2306 * was doing. - KW
2307 */
2308 PUTS(replace_buf);
2309 /*
2310 * If we're displaying UTF-8, try that now. - FM
2311 */
2312 } else if (me->T.output_utf8 && PUTUTF8(clong)) {
2313 ; /* do nothing more */
2314 /*
2315 * If it's any other (> 160) 8-bit character, and we have not set
2316 * HTPassEightBitRaw nor HTCJK, nor have the "ISO Latin 1"
2317 * character set selected, back translate for our character set. -
2318 * FM
2319 */
2320 #define IncludesLatin1Enc \
2321 (me->outUCLYhndl == LATIN1 || \
2322 (me->outUCI && \
2323 (me->outUCI->enc & (UCT_CP_SUPERSETOF_LAT1))))
2324
2325 #define PASSHI8BIT (HTPassEightBitRaw || \
2326 (me->T.do_8bitraw && !me->T.trans_from_uni))
2327
2328 } else if (clong > 160 && clong < 256 &&
2329 !(PASSHI8BIT || IS_CJK_TTY) &&
2330 !IncludesLatin1Enc) {
2331 #ifdef USE_PRETTYSRC
2332 int psrc_view_backup = 0;
2333 #endif
2334
2335 string->size = 0;
2336 EntityName = HTMLGetEntityName((UCode_t) (clong - 160));
2337 HTChunkPuts(string, EntityName);
2338 HTChunkTerminate(string);
2339 #ifdef USE_PRETTYSRC
2340 /* we need to disable it temporarily */
2341 if (psrc_view) {
2342 psrc_view_backup = 1;
2343 psrc_view = 0;
2344 }
2345 #endif
2346 handle_entity(me, '\0');
2347 #ifdef USE_PRETTYSRC
2348 /* we need to disable it temporarily */
2349 if (psrc_view_backup)
2350 psrc_view = TRUE;
2351 #endif
2352
2353 string->size = 0;
2354 if (!FoundEntity)
2355 PUTC(';');
2356 /*
2357 * If we get to here and have an ASCII char, pass the character. -
2358 * KW
2359 */
2360 } else if (TOASCII(clong) < 127 && clong > 0) { /* S/390 -- gil -- 0987 */
2361 PUTC(c);
2362 /*
2363 * If we get to here, and should have translated, translation has
2364 * failed so far. - KW
2365 *
2366 * We should have sent UTF-8 output to the parser already, but what
2367 * the heck, try again. - FM
2368 */
2369 } else if (me->T.output_utf8 && *me->U.utf_buf) {
2370 PUTS(me->U.utf_buf);
2371 me->U.utf_buf_p = me->U.utf_buf;
2372 *(me->U.utf_buf_p) = '\0';
2373 /*
2374 * If we don't actually want the character, make it safe and output
2375 * that now. - FM
2376 */
2377 } else if (TOASCII(UCH(c)) < /* S/390 -- gil -- 0997 */
2378 LYlowest_eightbit[me->outUCLYhndl] ||
2379 (me->T.trans_from_uni && !HTPassEightBitRaw)) {
2380 /*
2381 * If we get to here, pass the character. - FM
2382 */
2383 } else {
2384 PUTC(c);
2385 }
2386 break;
2387
2388 /*
2389 * Found '<' in SGML_PCDATA content; treat this mode nearly like
2390 * S_litteral, but recognize '<!' and '<?' to filter out comments and
2391 * processing instructions. - kw
2392 */
2393 case S_pcdata:
2394 if (!string->size && TOASCII(clong) < 127) { /* first after '<' */
2395 if (c == '!') { /* <! */
2396 /*
2397 * Terminate and set up for possible comment, identifier,
2398 * declaration, or marked section as under S_tag. - kw
2399 */
2400 me->state = S_exclamation;
2401 me->lead_exclamation = TRUE;
2402 me->doctype_bracket = FALSE;
2403 me->first_bracket = FALSE;
2404 HTChunkPutc(string, c);
2405 break;
2406 } else if (c == '?') { /* <? - ignore as a PI until '>' - kw */
2407 CTRACE((tfp,
2408 "SGML: Found PI in PCDATA, junking it until '>'\n"));
2409 #ifdef USE_PRETTYSRC
2410 if (psrc_view) {
2411 PSRCSTART(abracket);
2412 PUTS("<?");
2413 PSRCSTOP(abracket);
2414 }
2415 #endif
2416 me->state = S_pi;
2417 break;
2418 }
2419 }
2420 goto case_S_litteral;
2421
2422 /*
2423 * Found '<' in SGML_SCRIPT content; treat this mode nearly like
2424 * S_litteral, but recognize '<!' to allow the content to be treated as
2425 * a comment by lynx.
2426 */
2427 case S_script:
2428 if (!string->size && TOASCII(clong) < 127) { /* first after '<' */
2429 if (c == '!') { /* <! */
2430 /*
2431 * Terminate and set up for possible comment, identifier,
2432 * declaration, or marked section as under S_tag. - kw
2433 */
2434 me->state = S_exclamation;
2435 me->lead_exclamation = TRUE;
2436 me->doctype_bracket = FALSE;
2437 me->first_bracket = FALSE;
2438 HTChunkPutc(string, c);
2439 break;
2440 }
2441 }
2442 goto case_S_litteral;
2443
2444 /*
2445 * In literal mode, waits only for specific end tag (for compatibility
2446 * with old servers, and for Lynx). - FM
2447 */
2448 case_S_litteral:
2449 case S_litteral:
2450 /*PSRC:this case not understood completely by HV, not done */
2451 HTChunkPutc(string, c);
2452 #ifdef USE_PRETTYSRC
2453 if (psrc_view) {
2454 /* there is nothing useful in the element_stack */
2455 testtag = me->current_tag;
2456 } else
2457 #endif
2458 testtag = (me->element_stack
2459 ? me->element_stack->tag
2460 : NULL);
2461
2462 if (testtag == NULL || testtag->name == NULL) {
2463 string->size--;
2464 me->state = S_text;
2465 goto top1;
2466 }
2467
2468 /*
2469 * Normally when we get the closing ">",
2470 * testtag contains something like "TITLE"
2471 * string contains something like "/title>"
2472 * so we decrement by 2 to compare the final character of each.
2473 */
2474 testlast = string->size - 2 - me->trailing_spaces - me->leading_spaces;
2475
2476 #ifdef USE_COLOR_STYLE
2477 #define TagSize(p) ((p)->name_len)
2478 #else
2479 #define TagSize(p) (strlen((p)->name))
2480 #endif
2481
2482 if (TOUPPER(c) != ((testlast < 0)
2483 ? '/'
2484 : ((testlast < (int) TagSize(testtag))
2485 ? testtag->name[testlast]
2486 : 0))) {
2487 int i;
2488
2489 /*
2490 * If complete match, end literal.
2491 */
2492 if ((c == '>') &&
2493 testlast >= 0 && !testtag->name[testlast]) {
2494 #ifdef USE_PRETTYSRC
2495 if (psrc_view) {
2496 char *trailing = NULL;
2497
2498 if (me->trailing_spaces) {
2499 StrAllocCopy(trailing,
2500 string->data
2501 + string->size
2502 - 1
2503 - me->trailing_spaces);
2504 trailing[me->trailing_spaces] = '\0';
2505 }
2506
2507 PSRCSTART(abracket);
2508 PUTS("</");
2509 PSRCSTOP(abracket);
2510 PSRCSTART(tag);
2511
2512 strcpy(string->data, me->current_tag->name);
2513 transform_tag(me, string);
2514 PUTS(string->data);
2515
2516 if (trailing) {
2517 PUTS(trailing);
2518 FREE(trailing);
2519 }
2520
2521 PSRCSTOP(tag);
2522 PSRCSTART(abracket);
2523 PUTC('>');
2524 PSRCSTOP(abracket);
2525
2526 me->current_tag = NULL;
2527 } else
2528 #endif
2529 end_element(me, me->element_stack->tag);
2530
2531 string->size = 0;
2532 me->current_attribute_number = INVALID;
2533 me->state = S_text;
2534 me->leading_spaces = 0;
2535 me->trailing_spaces = 0;
2536 break;
2537 }
2538
2539 /*
2540 * Allow whitespace between the "<" or ">" and the keyword, for
2541 * error-recovery.
2542 */
2543 if (isspace(UCH(c))) {
2544 if (testlast == -1) {
2545 me->leading_spaces += 1;
2546 CTRACE2(TRACE_SGML, (tfp, "leading spaces: %d\n", me->leading_spaces));
2547 break;
2548 } else if (testlast > 0) {
2549 me->trailing_spaces += 1;
2550 CTRACE2(TRACE_SGML, (tfp, "trailing spaces: %d\n", me->trailing_spaces));
2551 break;
2552 }
2553 }
2554
2555 /*
2556 * Mismatch - recover.
2557 */
2558 me->leading_spaces = 0;
2559 me->trailing_spaces = 0;
2560 if (((testtag->contents != SGML_LITTERAL &&
2561 (testtag->flags & Tgf_strict)) ||
2562 (me->state == S_pcdata &&
2563 (testtag->flags & (Tgf_strict | Tgf_endO)))) &&
2564 (testlast > -1 &&
2565 (c == '>' || testlast > 0 || IsNmStart(c)))) {
2566 me->state = S_end;
2567 string->size--;
2568 for (i = 0; i < string->size; i++) /* remove '/' */
2569 string->data[i] = string->data[i + 1];
2570 if ((string->size == 1) ? IsNmStart(c) : IsNmChar(c))
2571 break;
2572 string->size--;
2573 goto top1;
2574 }
2575 if (me->state == S_pcdata &&
2576 (testtag->flags & (Tgf_strict | Tgf_endO)) &&
2577 (testlast < 0 && IsNmStart(c))) {
2578 me->state = S_tag;
2579 break;
2580 }
2581 /*
2582 * If Mismatch: recover string literally.
2583 */
2584 PUTC('<');
2585 for (i = 0; i < string->size - 1; i++) /* recover, except last c */
2586 PUTC(string->data[i]);
2587 string->size = 0;
2588 me->state = S_text;
2589 goto top1; /* to recover last c */
2590 }
2591 break;
2592
2593 /*
2594 * Character reference (numeric entity) or named entity.
2595 */
2596 case S_ero:
2597 if (c == '#') {
2598 /*
2599 * Setting up for possible numeric entity.
2600 */
2601 me->state = S_cro; /* &# is Char Ref Open */
2602 break;
2603 }
2604 me->state = S_entity; /* Fall through! */
2605
2606 /*
2607 * Handle possible named entity.
2608 */
2609 case S_entity:
2610 if (TOASCII(clong) < 127 && (string->size
2611 ? IsNmChar(c)
2612 : IsNmStart(c))) {
2613 /*
2614 * Accept valid ASCII character. - FM
2615 */
2616 HTChunkPutc(string, c);
2617 } else if (string->size == 0) {
2618 /*
2619 * It was an ampersand that's just text, so output the ampersand
2620 * and recycle this character. - FM
2621 */
2622 #ifdef USE_PRETTYSRC
2623 if (psrc_view)
2624 PSRCSTART(badseq);
2625 #endif
2626 PUTC('&');
2627 #ifdef USE_PRETTYSRC
2628 if (psrc_view)
2629 PSRCSTOP(badseq);
2630 #endif
2631 me->state = S_text;
2632 goto top1;
2633 } else {
2634 /*
2635 * Terminate entity name and try to handle it. - FM
2636 */
2637 HTChunkTerminate(string);
2638 #ifdef USE_PRETTYSRC
2639 entity_string = string->data;
2640 #endif
2641 if (!strcmp(string->data, "zwnj") &&
2642 (!me->element_stack ||
2643 (me->element_stack->tag &&
2644 me->element_stack->tag->contents == SGML_MIXED))) {
2645 /*
2646 * Handle zwnj (U+200C) as <WBR>. - FM
2647 */
2648 char temp[8];
2649
2650 CTRACE((tfp,
2651 "SGML_character: Handling 'zwnj' entity as 'WBR' element.\n"));
2652
2653 if (c != ';') {
2654 sprintf(temp, "<WBR>%c", c);
2655 } else {
2656 sprintf(temp, "<WBR>");
2657 }
2658 if (me->recover == NULL) {
2659 StrAllocCopy(me->recover, temp);
2660 me->recover_index = 0;
2661 } else {
2662 StrAllocCat(me->recover, temp);
2663 }
2664 string->size = 0;
2665 me->state = S_text;
2666 break;
2667 } else {
2668 handle_entity(me, '\0');
2669 }
2670 string->size = 0;
2671 me->state = S_text;
2672 /*
2673 * Don't eat the terminator if we didn't find the entity name and
2674 * therefore sent the raw string via handle_entity(), or if the
2675 * terminator is not the "standard" semi-colon for HTML. - FM
2676 */
2677 #ifdef USE_PRETTYSRC
2678 if (psrc_view && FoundEntity && c == ';') {
2679 PSRCSTART(entity);
2680 PUTC(c);
2681 PSRCSTOP(entity);
2682 }
2683 #endif
2684 if (!FoundEntity || c != ';')
2685 goto top1;
2686 }
2687 break;
2688
2689 /*
2690 * Check for a numeric entity.
2691 */
2692 case S_cro:
2693 if (TOASCII(clong) < 127 && TOLOWER(UCH(c)) == 'x') { /* S/390 -- gil -- 1060 */
2694 me->isHex = TRUE;
2695 me->state = S_incro;
2696 } else if (TOASCII(clong) < 127 && isdigit(UCH(c))) {
2697 /*
2698 * Accept only valid ASCII digits. - FM
2699 */
2700 HTChunkPutc(string, c); /* accumulate a character NUMBER */
2701 me->isHex = FALSE;
2702 me->state = S_incro;
2703 } else if (string->size == 0) {
2704 /*
2705 * No 'x' or digit following the "&#" so recover them and recycle
2706 * the character. - FM
2707 */
2708 #ifdef USE_PRETTYSRC
2709 if (psrc_view)
2710 PSRCSTART(badseq);
2711 #endif
2712 PUTC('&');
2713 PUTC('#');
2714 #ifdef USE_PRETTYSRC
2715 if (psrc_view)
2716 PSRCSTOP(badseq);
2717 #endif
2718 me->state = S_text;
2719 goto top1;
2720 }
2721 break;
2722
2723 /*
2724 * Handle a numeric entity.
2725 */
2726 case S_incro:
2727 /* S/390 -- gil -- 1075 */
2728 if ((TOASCII(clong) < 127) &&
2729 (me->isHex
2730 ? isxdigit(UCH(c))
2731 : isdigit(UCH(c)))) {
2732 /*
2733 * Accept only valid hex or ASCII digits. - FM
2734 */
2735 HTChunkPutc(string, c); /* accumulate a character NUMBER */
2736 } else if (string->size == 0) {
2737 /*
2738 * No hex digit following the "&#x" so recover them and recycle the
2739 * character. - FM
2740 */
2741 #ifdef USE_PRETTYSRC
2742 if (psrc_view)
2743 PSRCSTART(badseq);
2744 #endif
2745 PUTS("&#x");
2746 #ifdef USE_PRETTYSRC
2747 if (psrc_view)
2748 PSRCSTOP(badseq);
2749 #endif
2750 me->isHex = FALSE;
2751 me->state = S_text;
2752 goto top1;
2753 } else {
2754 /*
2755 * Terminate the numeric entity and try to handle it. - FM
2756 */
2757 UCode_t code;
2758 int i;
2759
2760 HTChunkTerminate(string);
2761 #ifdef USE_PRETTYSRC
2762 entity_string = string->data;
2763 #endif
2764 if (UCScanCode(&code, string->data, me->isHex)) {
2765
2766 /* =============== work in ASCII below here =============== S/390 -- gil -- 1092 */
2767 if (AssumeCP1252(me)) {
2768 code = LYcp1252ToUnicode(code);
2769 }
2770 /*
2771 * Check for special values. - FM
2772 */
2773 if ((code == 0x200C) &&
2774 (!me->element_stack ||
2775 (me->element_stack->tag &&
2776 me->element_stack->tag->contents == SGML_MIXED))) {
2777 /*
2778 * Handle zwnj (U+200C) as <WBR>. - FM
2779 */
2780 char temp[8];
2781
2782 CTRACE((tfp,
2783 "SGML_character: Handling 'U+%04lX' (zwnj) reference as 'WBR' element.\n", code));
2784
2785 /*
2786 * Include the terminator if it is not the standard
2787 * semi-colon. - FM
2788 */
2789 if (c != ';') {
2790 sprintf(temp, "<WBR>%c", c);
2791 } else {
2792 sprintf(temp, "<WBR>");
2793 }
2794 /*
2795 * Add the replacement string to the recover buffer for
2796 * processing. - FM
2797 */
2798 if (me->recover == NULL) {
2799 StrAllocCopy(me->recover, temp);
2800 me->recover_index = 0;
2801 } else {
2802 StrAllocCat(me->recover, temp);
2803 }
2804 string->size = 0;
2805 me->isHex = FALSE;
2806 me->state = S_text;
2807 break;
2808 } else if (put_special_unicodes(me, code)) {
2809 /*
2810 * We handled the value as a special character, so recycle
2811 * the terminator or break. - FM
2812 */
2813 #ifdef USE_PRETTYSRC
2814 if (psrc_view) {
2815 PSRCSTART(entity);
2816 PUTS((me->isHex ? "&#x" : "&#"));
2817 PUTS(entity_string);
2818 if (c == ';')
2819 PUTC(';');
2820 PSRCSTOP(entity);
2821 }
2822 #endif
2823 string->size = 0;
2824 me->isHex = FALSE;
2825 me->state = S_text;
2826 if (c != ';')
2827 goto top1;
2828 break;
2829 }
2830 /*
2831 * Seek a translation from the chartrans tables.
2832 */
2833 if ((uck = UCTransUniChar(code,
2834 me->outUCLYhndl)) >= 32 &&
2835 uck < 256 &&
2836 (uck < 127 ||
2837 uck >= LYlowest_eightbit[me->outUCLYhndl])) {
2838 #ifdef USE_PRETTYSRC
2839 if (!psrc_view) {
2840 #endif
2841 PUTC(FROMASCII((char) uck));
2842 #ifdef USE_PRETTYSRC
2843 } else {
2844 put_pretty_number(me);
2845 }
2846 #endif
2847 } else if (uck == ucZeroWidth) {
2848 ; /* EMPTY */
2849 } else if ((uck == ucNotFound ||
2850 (me->T.repl_translated_C0 &&
2851 uck > 0 && uck < 32)) &&
2852 /*
2853 * Not found; look for replacement string.
2854 */
2855 (uck = UCTransUniCharStr(replace_buf, 60, code,
2856 me->outUCLYhndl,
2857 0) >= 0)) {
2858 #ifdef USE_PRETTYSRC
2859 if (psrc_view) {
2860 put_pretty_number(me);
2861 } else
2862 #endif
2863 PUTS(replace_buf);
2864 /*
2865 * If we're displaying UTF-8, try that now. - FM
2866 */
2867 } else if (me->T.output_utf8 && PUTUTF8(code)) {
2868 ; /* do nothing more */
2869 /*
2870 * Ignore zero-width and left/right marks
2871 */
2872 } else if (is_ucs_zero_width(code)) {
2873 if (TRACE) {
2874 string->size--;
2875 LYStrNCpy(replace_buf,
2876 string->data,
2877 (string->size < 64 ? string->size : 63));
2878 fprintf(tfp,
2879 "SGML_character: Ignoring '%s%s'.\n",
2880 (me->isHex ? "&#x" : "&#"),
2881 replace_buf);
2882 }
2883 #ifdef USE_PRETTYSRC
2884 if (psrc_view) {
2885 PSRCSTART(badseq);
2886 PUTS((me->isHex ? "&#x" : "&#"));
2887 PUTS(entity_string);
2888 if (c == ';')
2889 PUTC(';');
2890 PSRCSTOP(badseq);
2891 }
2892 #endif
2893 string->size = 0;
2894 me->isHex = FALSE;
2895 me->state = S_text;
2896 if (c != ';')
2897 goto top1;
2898 break;
2899 /*
2900 * Show the numeric entity if we get to here and the value:
2901 * (1) Is greater than 255 (but use ASCII characters for
2902 * spaces or dashes).
2903 * (2) Is less than 32, and not valid or we don't have
2904 * HTCJK set.
2905 * (3) Is 127 and we don't have HTPassHighCtrlRaw or HTCJK
2906 * set.
2907 * (4) Is 128 - 159 and we don't have HTPassHighCtrlNum
2908 * set.
2909 * - FM
2910 */
2911 } else if ((code > 255) ||
2912 (code < ' ' && /* S/390 -- gil -- 1140 */
2913 code != '\t' && code != '\n' && code != '\r' &&
2914 !IS_CJK_TTY) ||
2915 (TOASCII(code) == 127 &&
2916 !(HTPassHighCtrlRaw || IS_CJK_TTY)) ||
2917 (TOASCII(code) > 127 && code < 160 &&
2918 !HTPassHighCtrlNum)) {
2919 /*
2920 * Unhandled or illegal value. Recover the "&#" or "&#x"
2921 * and digit(s), and recycle the terminator. - FM
2922 */
2923 #ifdef USE_PRETTYSRC
2924 if (psrc_view) {
2925 PSRCSTART(badseq);
2926 }
2927 #endif
2928 if (me->isHex) {
2929 PUTS("&#x");
2930 me->isHex = FALSE;
2931 } else {
2932 PUTS("&#");
2933 }
2934 string->size--;
2935 for (i = 0; i < string->size; i++) /* recover */
2936 PUTC(string->data[i]);
2937 #ifdef USE_PRETTYSRC
2938 if (psrc_view) {
2939 PSRCSTOP(badseq);
2940 }
2941 #endif
2942 string->size = 0;
2943 me->isHex = FALSE;
2944 me->state = S_text;
2945 goto top1;
2946 } else if (TOASCII(code) < 161 || /* S/390 -- gil -- 1162 */
2947 HTPassEightBitNum ||
2948 IncludesLatin1Enc) {
2949 /*
2950 * No conversion needed. - FM
2951 */
2952 #ifdef USE_PRETTYSRC
2953 if (psrc_view) {
2954 put_pretty_number(me);
2955 } else
2956 #endif
2957 PUTC(FROMASCII((char) code));
2958 } else {
2959 /*
2960 * Handle as named entity. - FM
2961 */
2962 code -= 160;
2963 EntityName = HTMLGetEntityName(code);
2964 if (EntityName && EntityName[0] != '\0') {
2965 string->size = 0;
2966 HTChunkPuts(string, EntityName);
2967 HTChunkTerminate(string);
2968 handle_entity(me, '\0');
2969 /*
2970 * Add a semi-colon if something went wrong and
2971 * handle_entity() sent the string. - FM
2972 */
2973 if (!FoundEntity) {
2974 PUTC(';');
2975 }
2976 } else {
2977 /*
2978 * Our conversion failed, so recover the "&#" and
2979 * digit(s), and recycle the terminator. - FM
2980 */
2981 #ifdef USE_PRETTYSRC
2982 if (psrc_view)
2983 PSRCSTART(badseq);
2984 #endif
2985 if (me->isHex) {
2986 PUTS("&#x");
2987 me->isHex = FALSE;
2988 } else {
2989 PUTS("&#");
2990 }
2991 string->size--;
2992 for (i = 0; i < string->size; i++) /* recover */
2993 PUTC(string->data[i]);
2994 #ifdef USE_PRETTYSRC
2995 if (psrc_view)
2996 PSRCSTOP(badseq);
2997 #endif
2998 string->size = 0;
2999 me->isHex = FALSE;
3000 me->state = S_text;
3001 goto top1;
3002 }
3003 }
3004 /*
3005 * If we get to here, we succeeded. Hoorah!!! - FM
3006 */
3007 string->size = 0;
3008 me->isHex = FALSE;
3009 me->state = S_text;
3010 /*
3011 * Don't eat the terminator if it's not the "standard"
3012 * semi-colon for HTML. - FM
3013 */
3014 if (c != ';') {
3015 goto top1;
3016 }
3017 } else {
3018 /*
3019 * Not an entity, and don't know why not, so add the terminator
3020 * to the string, output the "&#" or "&#x", and process the
3021 * string via the recover element. - FM
3022 */
3023 string->size--;
3024 HTChunkPutc(string, c);
3025 HTChunkTerminate(string);
3026 #ifdef USE_PRETTYSRC
3027 if (psrc_view)
3028 PSRCSTART(badseq);
3029 #endif
3030 if (me->isHex) {
3031 PUTS("&#x");
3032 me->isHex = FALSE;
3033 } else {
3034 PUTS("&#");
3035 }
3036 #ifdef USE_PRETTYSRC
3037 if (psrc_view)
3038 PSRCSTOP(badseq);
3039 #endif
3040 if (me->recover == NULL) {
3041 StrAllocCopy(me->recover, string->data);
3042 me->recover_index = 0;
3043 } else {
3044 StrAllocCat(me->recover, string->data);
3045 }
3046 string->size = 0;
3047 me->isHex = FALSE;
3048 me->state = S_text;
3049 break;
3050 }
3051 }
3052 break;
3053
3054 /*
3055 * Tag
3056 */
3057 case S_tag: /* new tag */
3058 if (TOASCII(clong) < 127 && (string->size ? /* S/390 -- gil -- 1179 */
3059 IsNmChar(c) : IsNmStart(c))) {
3060 /*
3061 * Add valid ASCII character. - FM
3062 */
3063 HTChunkPutc(string, c);
3064 } else if (c == '!' && !string->size) { /* <! */
3065 /*
3066 * Terminate and set up for possible comment, identifier,
3067 * declaration, or marked section. - FM
3068 */
3069 me->state = S_exclamation;
3070 me->lead_exclamation = TRUE;
3071 me->doctype_bracket = FALSE;
3072 me->first_bracket = FALSE;
3073 HTChunkPutc(string, c);
3074 break;
3075 } else if (!string->size &&
3076 (TOASCII(clong) <= 160 && /* S/390 -- gil -- 1196 */
3077 (c != '/' && c != '?' && c != '_' && c != ':'))) {
3078 /*
3079 * '<' must be followed by an ASCII letter to be a valid start tag.
3080 * Here it isn't, nor do we have a '/' for an end tag, nor one of
3081 * some other characters with a special meaning for SGML or which
3082 * are likely to be legal Name Start characters in XML or some
3083 * other extension. So recover the '<' and following character as
3084 * data. - FM & KW
3085 */
3086 me->state = S_text;
3087 #ifdef USE_PRETTYSRC
3088 if (psrc_view)
3089 PSRCSTART(badseq);
3090 #endif
3091 PUTC('<');
3092 #ifdef USE_PRETTYSRC
3093 if (psrc_view)
3094 PSRCSTOP(badseq);
3095 #endif
3096 goto top1;
3097 } else { /* End of tag name */
3098 /*
3099 * Try to handle tag. - FM
3100 */
3101 HTTag *t;
3102
3103 if (c == '/') {
3104 if (string->size == 0) {
3105 me->state = S_end;
3106 break;
3107 }
3108 CTRACE((tfp, "SGML: `<%.*s/' found!\n", string->size, string->data));
3109 }
3110 HTChunkTerminate(string);
3111
3112 t = SGMLFindTag(dtd, string->data);
3113 if (t == me->unknown_tag &&
3114 ((c == ':' &&
3115 string->size == 4 && 0 == strcasecomp(string->data, "URL")) ||
3116 (string->size > 4 && 0 == strncasecomp(string->data, "URL:", 4)))) {
3117 /*
3118 * Treat <URL: as text rather than a junk tag, so we display
3119 * it and the URL (Lynxism 8-). - FM
3120 */
3121 #ifdef USE_PRETTYSRC
3122 if (psrc_view)
3123 PSRCSTART(badseq);
3124 #endif
3125 PUTC('<');
3126 PUTS(string->data); /* recover */
3127 PUTC(c);
3128 #ifdef USE_PRETTYSRC
3129 if (psrc_view)
3130 PSRCSTOP(badseq);
3131 #endif
3132 CTRACE((tfp, "SGML: Treating <%s%c as text\n",
3133 string->data, c));
3134 string->size = 0;
3135 me->state = S_text;
3136 break;
3137 }
3138 if (c == '/' && t) {
3139 /*
3140 * Element name was ended by '/'. Remember the tag that ended
3141 * thusly, we'll interpret this as either an indication of an
3142 * empty element (if '>' follows directly) or do some
3143 * SGMLshortref-ish treatment. - kw
3144 */
3145 me->slashedtag = t;
3146 }
3147 if (!t) {
3148 if (c == '?' && string->size <= 1) {
3149 CTRACE((tfp, "SGML: Found PI, looking for '>'\n"));
3150 #ifdef USE_PRETTYSRC
3151 if (psrc_view) {
3152 PSRCSTART(abracket);
3153 PUTS("<?");
3154 PSRCSTOP(abracket);
3155 }
3156 #endif
3157 string->size = 0;
3158 me->state = S_pi;
3159 HTChunkPutc(string, c);
3160 break;
3161 }
3162 CTRACE((tfp, "SGML: *** Invalid element %s\n",
3163 string->data));
3164
3165 #ifdef USE_PRETTYSRC
3166 if (psrc_view) {
3167 PSRCSTART(abracket);
3168 PUTC('<');
3169 PSRCSTOP(abracket);
3170 PSRCSTART(badtag);
3171 transform_tag(me, string);
3172 PUTS(string->data);
3173 if (c == '>') {
3174 PSRCSTOP(badtag);
3175 PSRCSTART(abracket);
3176 PUTC('>');
3177 PSRCSTOP(abracket);
3178 } else {
3179 PUTC(c);
3180 }
3181 }
3182 #endif
3183 me->state = (c == '>') ? S_text : S_junk_tag;
3184 break;
3185 } else if (t == me->unknown_tag) {
3186 CTRACE((tfp, "SGML: *** Unknown element \"%s\"\n",
3187 string->data));
3188 /*
3189 * Fall through and treat like valid tag for attribute parsing.
3190 * - KW
3191 */
3192
3193 }
3194 me->current_tag = t;
3195
3196 #ifdef USE_PRETTYSRC
3197 if (psrc_view) {
3198 PSRCSTART(abracket);
3199 PUTC('<');
3200 PSRCSTOP(abracket);
3201 if (t != me->unknown_tag)
3202 PSRCSTART(tag);
3203 else
3204 PSRCSTART(badtag);
3205 transform_tag(me, string);
3206 PUTS(string->data);
3207 if (t != me->unknown_tag)
3208 PSRCSTOP(tag);
3209 else
3210 PSRCSTOP(badtag);
3211 }
3212 if (!psrc_view) /*don't waste time */
3213 #endif
3214 {
3215 /*
3216 * Clear out attributes.
3217 */
3218 memset((void *) me->present, 0, sizeof(BOOL) *
3219 (unsigned) (me->current_tag->number_of_attributes));
3220 }
3221
3222 string->size = 0;
3223 me->current_attribute_number = INVALID;
3224 #ifdef USE_PRETTYSRC
3225 if (psrc_view) {
3226 if (c == '>' || c == '<' || (c == '/' && me->slashedtag)) {
3227 if (c != '<') {
3228 PSRCSTART(abracket);
3229 PUTC(c);
3230 PSRCSTOP(abracket);
3231 me->state = (c == '>') ? S_text : S_tagname_slash;
3232 } else {
3233 me->state = S_tag;
3234 }
3235 } else {
3236 if (!WHITE(c))
3237 PUTC(c);
3238 me->state = S_tag_gap;
3239 }
3240 } else
3241 #endif
3242 if (c == '>' || c == '<' || (c == '/' && me->slashedtag)) {
3243 if (me->current_tag->name)
3244 start_element(me);
3245 me->state = (c == '>') ? S_text :
3246 (c == '<') ? S_tag : S_tagname_slash;
3247 } else {
3248 me->state = S_tag_gap;
3249 }
3250 }
3251 break;
3252
3253 case S_exclamation:
3254 if (me->lead_exclamation && c == '-') {
3255 /*
3256 * Set up for possible comment. - FM
3257 */
3258 me->lead_exclamation = FALSE;
3259 me->first_dash = TRUE;
3260 HTChunkPutc(string, c);
3261 break;
3262 }
3263 if (me->lead_exclamation && c == '[') {
3264 /*
3265 * Set up for possible marked section. - FM
3266 */
3267 me->lead_exclamation = FALSE;
3268 me->first_bracket = TRUE;
3269 me->second_bracket = FALSE;
3270 HTChunkPutc(string, c);
3271 me->state = S_marked;
3272 break;
3273 }
3274 if (me->first_dash && c == '-') {
3275 /*
3276 * Set up to handle comment. - FM
3277 */
3278 me->lead_exclamation = FALSE;
3279 me->first_dash = FALSE;
3280 me->end_comment = FALSE;
3281 HTChunkPutc(string, c);
3282 me->state = S_comment;
3283 break;
3284 }
3285 me->lead_exclamation = FALSE;
3286 me->first_dash = FALSE;
3287 if (c == '>') {
3288 /*
3289 * Try to handle identifier. - FM
3290 */
3291 HTChunkTerminate(string);
3292 #ifdef USE_PRETTYSRC
3293 if (psrc_view) {
3294 PSRCSTART(sgmlspecial);
3295 PUTC('<');
3296 PUTS(string->data);
3297 PUTC('>');
3298 PSRCSTOP(sgmlspecial);
3299 } else
3300 #endif
3301 handle_identifier(me);
3302 string->size = 0;
3303 me->state = S_text;
3304 break;
3305 }
3306 if (WHITE(c)) {
3307 if (string->size == 8 &&
3308 !strncasecomp(string->data, "!DOCTYPE", 8)) {
3309 /*
3310 * Set up for DOCTYPE declaration. - FM
3311 */
3312 HTChunkPutc(string, c);
3313 me->doctype_bracket = FALSE;
3314 me->state = S_doctype;
3315 break;
3316 }
3317 if (string->size == 7 &&
3318 !strncasecomp(string->data, "!ENTITY", 7)) {
3319 /*
3320 * Set up for ENTITY declaration. - FM
3321 */
3322 HTChunkPutc(string, c);
3323 me->first_dash = FALSE;
3324 me->end_comment = TRUE;
3325 me->state = S_sgmlent;
3326 break;
3327 }
3328 if (string->size == 8 &&
3329 !strncasecomp(string->data, "!ELEMENT", 8)) {
3330 /*
3331 * Set up for ELEMENT declaration. - FM
3332 */
3333 HTChunkPutc(string, c);
3334 me->first_dash = FALSE;
3335 me->end_comment = TRUE;
3336 me->state = S_sgmlele;
3337 break;
3338 }
3339 if (string->size == 8 &&
3340 !strncasecomp(string->data, "!ATTLIST", 8)) {
3341 /*
3342 * Set up for ATTLIST declaration. - FM
3343 */
3344 HTChunkPutc(string, c);
3345 me->first_dash = FALSE;
3346 me->end_comment = TRUE;
3347 me->state = S_sgmlatt;
3348 break;
3349 }
3350 }
3351 HTChunkPutc(string, c);
3352 break;
3353
3354 case S_comment: /* Expecting comment. - FM */
3355 if (historical_comments) {
3356 /*
3357 * Any '>' terminates. - FM
3358 */
3359 if (c == '>') {
3360 HTChunkTerminate(string);
3361 #ifdef USE_PRETTYSRC
3362 if (psrc_view) {
3363 PSRCSTART(comm);
3364 PUTC('<');
3365 PUTS_TR(string->data);
3366 PUTC('>');
3367 PSRCSTOP(comm);
3368 } else
3369 #endif
3370 handle_comment(me);
3371 string->size = 0;
3372 me->end_comment = FALSE;
3373 me->first_dash = FALSE;
3374 me->state = S_text;
3375 break;
3376 }
3377 goto S_comment_put_c;
3378 }
3379 if (!me->first_dash && c == '-') {
3380 HTChunkPutc(string, c);
3381 me->first_dash = TRUE;
3382 break;
3383 }
3384 if (me->first_dash && c == '-') {
3385 HTChunkPutc(string, c);
3386 me->first_dash = FALSE;
3387 if (!me->end_comment)
3388 me->end_comment = TRUE;
3389 else if (!minimal_comments)
3390 /*
3391 * Validly treat '--' pairs as successive comments (for
3392 * minimal, any "--WHITE>" terminates). - FM
3393 */
3394 me->end_comment = FALSE;
3395 break;
3396 }
3397 if (me->end_comment && c == '>') {
3398 /*
3399 * Terminate and handle the comment. - FM
3400 */
3401 HTChunkTerminate(string);
3402 #ifdef USE_PRETTYSRC
3403 if (psrc_view) {
3404 PSRCSTART(comm);
3405 PUTC('<');
3406 PUTS_TR(string->data);
3407 PUTC('>');
3408 PSRCSTOP(comm);
3409 } else
3410 #endif
3411 handle_comment(me);
3412 string->size = 0;
3413 me->end_comment = FALSE;
3414 me->first_dash = FALSE;
3415 me->state = S_text;
3416 break;
3417 }
3418 me->first_dash = FALSE;
3419 if (me->end_comment && !isspace(UCH(c)))
3420 me->end_comment = FALSE;
3421
3422 S_comment_put_c:
3423 if (me->T.decode_utf8 &&
3424 *me->U.utf_buf) {
3425 HTChunkPuts(string, me->U.utf_buf);
3426 me->U.utf_buf_p = me->U.utf_buf;
3427 *(me->U.utf_buf_p) = '\0';
3428 } else if (!IS_CJK_TTY &&
3429 (me->T.output_utf8 ||
3430 me->T.trans_from_uni)) {
3431 if (clong == UCS_REPL && saved_char_in &&
3432 HTPassEightBitRaw &&
3433 saved_char_in >=
3434 LYlowest_eightbit[me->outUCLYhndl]) {
3435 (HTChunkPutUtf8Char) (string,
3436 (UCode_t) (0xf000 | saved_char_in));
3437 } else {
3438 HTChunkPutUtf8Char(string, clong);
3439 }
3440 } else if (saved_char_in && me->T.use_raw_char_in) {
3441 HTChunkPutc(string, saved_char_in);
3442 } else {
3443 HTChunkPutc(string, c);
3444 }
3445 break;
3446
3447 case S_doctype: /* Expecting DOCTYPE. - FM */
3448 if (me->doctype_bracket) {
3449 HTChunkPutc(string, c);
3450 if (c == ']')
3451 me->doctype_bracket = FALSE;
3452 break;
3453 }
3454 if (c == '[' && WHITE(string->data[string->size - 1])) {
3455 HTChunkPutc(string, c);
3456 me->doctype_bracket = TRUE;
3457 break;
3458 }
3459 if (c == '>') {
3460 HTChunkTerminate(string);
3461 #ifdef USE_PRETTYSRC
3462 if (psrc_view) {
3463 PSRCSTART(sgmlspecial);
3464 PUTC('<');
3465 PUTS(string->data);
3466 PUTC('>');
3467 PSRCSTOP(sgmlspecial);
3468 } else
3469 #endif
3470 handle_doctype(me);
3471 string->size = 0;
3472 me->state = S_text;
3473 break;
3474 }
3475 HTChunkPutc(string, c);
3476 break;
3477
3478 case S_marked: /* Expecting marked section. - FM */
3479 if (me->first_bracket && c == '[') {
3480 HTChunkPutc(string, c);
3481 me->first_bracket = FALSE;
3482 me->second_bracket = TRUE;
3483 break;
3484 }
3485 if (me->second_bracket && c == ']' &&
3486 string->data[string->size - 1] == ']') {
3487 HTChunkPutc(string, c);
3488 me->second_bracket = FALSE;
3489 break;
3490 }
3491 if (!me->second_bracket && c == '>') {
3492 HTChunkTerminate(string);
3493 #ifdef USE_PRETTYSRC
3494 if (psrc_view) {
3495 PSRCSTART(sgmlspecial);
3496 PUTC('<');
3497 PUTS(string->data);
3498 PUTC('>');
3499 PSRCSTOP(sgmlspecial);
3500 } else
3501 #endif
3502 handle_marked(me);
3503 string->size = 0;
3504 me->state = S_text;
3505 break;
3506 }
3507
3508 if (me->T.decode_utf8) {
3509 HTChunkPutUtf8Char(string, clong);
3510 } else {
3511 HTChunkPutc(string, c);
3512 }
3513 break;
3514 case S_sgmlent: /* Expecting ENTITY. - FM */
3515 if (!me->first_dash && c == '-') {
3516 HTChunkPutc(string, c);
3517 me->first_dash = TRUE;
3518 break;
3519 }
3520 if (me->first_dash && c == '-') {
3521 HTChunkPutc(string, c);
3522 me->first_dash = FALSE;
3523 if (!me->end_comment)
3524 me->end_comment = TRUE;
3525 else
3526 me->end_comment = FALSE;
3527 break;
3528 }
3529 if (me->end_comment && c == '>') {
3530 HTChunkTerminate(string);
3531 #ifdef USE_PRETTYSRC
3532 if (psrc_view) {
3533 PSRCSTART(sgmlspecial);
3534 PUTC('<');
3535 PUTS(string->data);
3536 PUTC('>');
3537 PSRCSTOP(sgmlspecial);
3538 } else
3539 #endif
3540 handle_sgmlent(me);
3541 string->size = 0;
3542 me->end_comment = FALSE;
3543 me->first_dash = FALSE;
3544 me->state = S_text;
3545 break;
3546 }
3547 me->first_dash = FALSE;
3548 HTChunkPutc(string, c);
3549 break;
3550
3551 case S_sgmlele: /* Expecting ELEMENT. - FM */
3552 if (!me->first_dash && c == '-') {
3553 HTChunkPutc(string, c);
3554 me->first_dash = TRUE;
3555 break;
3556 }
3557 if (me->first_dash && c == '-') {
3558 HTChunkPutc(string, c);
3559 me->first_dash = FALSE;
3560 if (!me->end_comment)
3561 me->end_comment = TRUE;
3562 else
3563 me->end_comment = FALSE;
3564 break;
3565 }
3566 if (me->end_comment && c == '>') {
3567 HTChunkTerminate(string);
3568 #ifdef USE_PRETTYSRC
3569 if (psrc_view) {
3570 PSRCSTART(sgmlspecial);
3571 PUTC('<');
3572 PUTS(string->data);
3573 PUTC('>');
3574 PSRCSTOP(sgmlspecial);
3575 } else
3576 #endif
3577 handle_sgmlele(me);
3578 string->size = 0;
3579 me->end_comment = FALSE;
3580 me->first_dash = FALSE;
3581 me->state = S_text;
3582 break;
3583 }
3584 me->first_dash = FALSE;
3585 HTChunkPutc(string, c);
3586 break;
3587
3588 case S_sgmlatt: /* Expecting ATTLIST. - FM */
3589 if (!me->first_dash && c == '-') {
3590 HTChunkPutc(string, c);
3591 me->first_dash = TRUE;
3592 break;
3593 }
3594 if (me->first_dash && c == '-') {
3595 HTChunkPutc(string, c);
3596 me->first_dash = FALSE;
3597 if (!me->end_comment)
3598 me->end_comment = TRUE;
3599 else
3600 me->end_comment = FALSE;
3601 break;
3602 }
3603 if (me->end_comment && c == '>') {
3604 HTChunkTerminate(string);
3605 #ifdef USE_PRETTYSRC
3606 if (psrc_view) {
3607 PSRCSTART(sgmlspecial);
3608 PUTC('<');
3609 PUTS(string->data);
3610 PUTC('>');
3611 PSRCSTOP(sgmlspecial);
3612 } else
3613 #endif
3614 handle_sgmlatt(me);
3615 string->size = 0;
3616 me->end_comment = FALSE;
3617 me->first_dash = FALSE;
3618 me->state = S_text;
3619 break;
3620 }
3621 me->first_dash = FALSE;
3622 HTChunkPutc(string, c);
3623 break;
3624
3625 case S_tag_gap: /* Expecting attribute or '>' */
3626 if (WHITE(c)) {
3627 /* PUTC(c); - no, done as special case */
3628 break; /* Gap between attributes */
3629 }
3630 if (c == '>') { /* End of tag */
3631 #ifdef USE_PRETTYSRC
3632 if (!psrc_view)
3633 #endif
3634 if (me->current_tag->name)
3635 start_element(me);
3636 #ifdef USE_PRETTYSRC
3637 if (psrc_view) {
3638 PSRCSTART(abracket);
3639 PUTC('>');
3640 PSRCSTOP(abracket);
3641 }
3642 #endif
3643 me->state = S_text;
3644 break;
3645 }
3646 HTChunkPutc(string, c);
3647 me->state = S_attr; /* Get attribute */
3648 break;
3649
3650 /* accumulating value */
3651 case S_attr:
3652 if (WHITE(c) || (c == '>') || (c == '=')) { /* End of word */
3653 if ((c == '>')
3654 && (string->size >= 1)
3655 && (string->data[string->size - 1] == '/')) {
3656 if ((LYxhtml_parsing || me->extended_html)
3657 && ignore_when_empty(me->current_tag)) {
3658 discard_empty(me);
3659 } else {
3660 HTChunkTerminate(string);
3661 }
3662 } else {
3663 HTChunkTerminate(string);
3664 handle_attribute_name(me, string->data);
3665 }
3666 #ifdef USE_PRETTYSRC
3667 if (!psrc_view) {
3668 #endif
3669 string->size = 0;
3670 if (c == '>') { /* End of tag */
3671 if (me->current_tag->name)
3672 start_element(me);
3673 me->state = S_text;
3674 break;
3675 }
3676 #ifdef USE_PRETTYSRC
3677 } else {
3678 PUTC(' ');
3679 if (me->current_attribute_number == INVALID)
3680 PSRCSTART(badattr);
3681 else
3682 PSRCSTART(attrib);
3683 if (attrname_transform != 1) {
3684 if (attrname_transform == 0)
3685 LYLowerCase(string->data);
3686 else
3687 LYUpperCase(string->data);
3688 }
3689 PUTS(string->data);
3690 if (c == '=' || WHITE(c))
3691 PUTC(c);
3692 if (c == '=' || c == '>') {
3693 if (me->current_attribute_number == INVALID) {
3694 PSRCSTOP(badattr);
3695 } else {
3696 PSRCSTOP(attrib);
3697 }
3698 }
3699 if (c == '>') {
3700 PSRCSTART(abracket);
3701 PUTC('>');
3702 PSRCSTOP(abracket);
3703 me->state = S_text;
3704 break;
3705 }
3706 string->size = 0;
3707 }
3708 #endif
3709 me->state = (c == '=' ? S_equals : S_attr_gap);
3710 } else {
3711 HTChunkPutc(string, c);
3712 }
3713 break;
3714
3715 case S_attr_gap: /* Expecting attribute or '=' or '>' */
3716 if (WHITE(c)) {
3717 PRETTYSRC_PUTC(c);
3718 break; /* Gap after attribute */
3719 }
3720 if (c == '>') { /* End of tag */
3721 #ifdef USE_PRETTYSRC
3722 if (psrc_view) {
3723 if (me->current_attribute_number == INVALID) {
3724 PSRCSTOP(badattr);
3725 } else {
3726 PSRCSTOP(attrib);
3727 }
3728 PSRCSTART(abracket);
3729 PUTC('>');
3730 PSRCSTOP(abracket);
3731 } else
3732 #endif
3733 if (me->current_tag->name)
3734 start_element(me);
3735 me->state = S_text;
3736 break;
3737 } else if (c == '=') {
3738 #ifdef USE_PRETTYSRC
3739 if (psrc_view) {
3740 PUTC('=');
3741 if (me->current_attribute_number == INVALID) {
3742 PSRCSTOP(badattr);
3743 } else {
3744 PSRCSTOP(attrib);
3745 }
3746 }
3747 #endif
3748 me->state = S_equals;
3749 break;
3750 }
3751 HTChunkPutc(string, c);
3752 me->state = S_attr; /* Get next attribute */
3753 break;
3754
3755 case S_equals: /* After attr = */
3756 if (WHITE(c)) {
3757 PRETTYSRC_PUTC(c);
3758 break; /* Before attribute value */
3759 }
3760 if (c == '>') { /* End of tag */
3761 CTRACE((tfp, "SGML: found = but no value\n"));
3762 #ifdef USE_PRETTYSRC
3763 if (psrc_view) {
3764 PSRCSTART(abracket);
3765 PUTC('>');
3766 PSRCSTOP(abracket);
3767 } else
3768 #endif
3769 if (me->current_tag->name)
3770 start_element(me);
3771 me->state = S_text;
3772 break;
3773
3774 } else if (c == '\'') {
3775 #ifdef USE_PRETTYSRC
3776 if (psrc_view) {
3777 PSRCSTART(attrval);
3778 PUTC(c);
3779 }
3780 #endif
3781 me->state = S_squoted;
3782 break;
3783
3784 } else if (c == '"') {
3785 #ifdef USE_PRETTYSRC
3786 if (psrc_view) {
3787 PSRCSTART(attrval);
3788 PUTC(c);
3789 }
3790 #endif
3791 me->state = S_dquoted;
3792 break;
3793 }
3794 #ifdef USE_PRETTYSRC
3795 if (psrc_view)
3796 PSRCSTART(attrval);
3797 #endif
3798 me->state = S_value;
3799 /* FALLTHRU */
3800
3801 case S_value:
3802 if (WHITE(c) || (c == '>')) { /* End of word */
3803 HTChunkTerminate(string);
3804 #ifdef USE_PRETTYSRC
3805 if (!end_if_prettysrc(me, string, 0))
3806 #endif
3807 {
3808 #ifdef CJK_EX /* Quick hack. - JH7AYN */
3809 if (IS_CJK_TTY) {
3810 if (string->data[0] == '$') {
3811 if (string->data[1] == 'B' || string->data[1] == '@') {
3812 char *jis_buf = NULL;
3813
3814 HTSprintf0(&jis_buf, "\033%s", string->data);
3815 TO_EUC((const unsigned char *) jis_buf,
3816 (unsigned char *) string->data);
3817 FREE(jis_buf);
3818 }
3819 }
3820 }
3821 #endif
3822 handle_attribute_value(me, string->data);
3823 }
3824 string->size = 0;
3825 if (c == '>') { /* End of tag */
3826 #ifdef USE_PRETTYSRC
3827 if (psrc_view) {
3828 PSRCSTART(abracket);
3829 PUTC('>');
3830 PSRCSTOP(abracket);
3831 } else
3832 #endif
3833 if (me->current_tag->name)
3834 start_element(me);
3835 me->state = S_text;
3836 break;
3837 } else
3838 me->state = S_tag_gap;
3839 } else if (me->T.decode_utf8 &&
3840 *me->U.utf_buf) {
3841 HTChunkPuts(string, me->U.utf_buf);
3842 me->U.utf_buf_p = me->U.utf_buf;
3843 *(me->U.utf_buf_p) = '\0';
3844 } else if (!IS_CJK_TTY &&
3845 (me->T.output_utf8 ||
3846 me->T.trans_from_uni)) {
3847 if (clong == UCS_REPL && saved_char_in &&
3848 HTPassEightBitRaw &&
3849 saved_char_in >=
3850 LYlowest_eightbit[me->outUCLYhndl]) {
3851 (HTChunkPutUtf8Char) (string,
3852 (UCode_t) (0xf000 | saved_char_in));
3853 } else {
3854 HTChunkPutUtf8Char(string, clong);
3855 }
3856 } else if (saved_char_in && me->T.use_raw_char_in) {
3857 HTChunkPutc(string, saved_char_in);
3858 } else {
3859 HTChunkPutc(string, c);
3860 }
3861 break;
3862
3863 case S_squoted: /* Quoted attribute value */
3864 if (c == '\'') { /* End of attribute value */
3865 HTChunkTerminate(string);
3866 #ifdef USE_PRETTYSRC
3867 if (!end_if_prettysrc(me, string, '\''))
3868 #endif
3869 handle_attribute_value(me, string->data);
3870 string->size = 0;
3871 me->state = S_tag_gap;
3872 } else if (TOASCII(c) == '\033') { /* S/390 -- gil -- 1213 */
3873 /*
3874 * Setting up for possible single quotes in CJK escape sequences.
3875 * - Takuya ASADA ([email protected])
3876 */
3877 me->state = S_esc_sq;
3878 if (!UTF8_TTY_ISO2022JP)
3879 HTChunkPutc(string, c);
3880 } else if (me->T.decode_utf8 &&
3881 *me->U.utf_buf) {
3882 HTChunkPuts(string, me->U.utf_buf);
3883 me->U.utf_buf_p = me->U.utf_buf;
3884 *(me->U.utf_buf_p) = '\0';
3885 } else if (!IS_CJK_TTY &&
3886 (me->T.output_utf8 ||
3887 me->T.trans_from_uni)) {
3888 if (clong == UCS_REPL && saved_char_in &&
3889 HTPassEightBitRaw &&
3890 saved_char_in >=
3891 LYlowest_eightbit[me->outUCLYhndl]) {
3892 (HTChunkPutUtf8Char) (string,
3893 (UCode_t) (0xf000 | saved_char_in));
3894 } else {
3895 HTChunkPutUtf8Char(string, clong);
3896 }
3897 } else if (saved_char_in && me->T.use_raw_char_in) {
3898 HTChunkPutc(string, saved_char_in);
3899 } else {
3900 HTChunkPutc(string, c);
3901 }
3902 break;
3903
3904 case S_dquoted: /* Quoted attribute value */
3905 if (c == '"' || /* Valid end of attribute value */
3906 (soft_dquotes && /* If emulating old Netscape bug, treat '>' */
3907 c == '>')) { /* as a co-terminator of dquoted and tag */
3908 HTChunkTerminate(string);
3909 #ifdef USE_PRETTYSRC
3910 if (!end_if_prettysrc(me, string, (char) c))
3911 #endif
3912 handle_attribute_value(me, string->data);
3913 string->size = 0;
3914 me->state = S_tag_gap;
3915 if (c == '>') /* We emulated the Netscape bug, so we go */
3916 goto top1; /* back and treat it as the tag terminator */
3917 } else if (TOASCII(c) == '\033') { /* S/390 -- gil -- 1230 */
3918 /*
3919 * Setting up for possible double quotes in CJK escape sequences.
3920 * - Takuya ASADA ([email protected])
3921 */
3922 me->state = S_esc_dq;
3923 if (!UTF8_TTY_ISO2022JP)
3924 HTChunkPutc(string, c);
3925 } else if (me->T.decode_utf8 &&
3926 *me->U.utf_buf) {
3927 HTChunkPuts(string, me->U.utf_buf);
3928 me->U.utf_buf_p = me->U.utf_buf;
3929 *(me->U.utf_buf_p) = '\0';
3930 } else if (!IS_CJK_TTY &&
3931 (me->T.output_utf8 ||
3932 me->T.trans_from_uni)) {
3933 if (clong == UCS_REPL && saved_char_in &&
3934 HTPassEightBitRaw &&
3935 saved_char_in >=
3936 LYlowest_eightbit[me->outUCLYhndl]) {
3937 (HTChunkPutUtf8Char) (string,
3938 (UCode_t) (0xf000 | saved_char_in));
3939 } else {
3940 HTChunkPutUtf8Char(string, clong);
3941 }
3942 } else if (saved_char_in && me->T.use_raw_char_in) {
3943 HTChunkPutc(string, saved_char_in);
3944 } else {
3945 HTChunkPutc(string, c);
3946 }
3947 break;
3948
3949 case S_end: /* </ */
3950 if (TOASCII(clong) < 127 && (string->size ? /* S/390 -- gil -- 1247 */
3951 IsNmChar(c) : IsNmStart(c))) {
3952 HTChunkPutc(string, c);
3953 } else { /* End of end tag name */
3954 HTTag *t = NULL;
3955
3956 #ifdef USE_PRETTYSRC
3957 BOOL psrc_tagname_processed = FALSE;
3958 #endif
3959
3960 HTChunkTerminate(string);
3961 if (!*string->data) { /* Empty end tag */
3962 if (me->element_stack)
3963 t = me->element_stack->tag;
3964 } else {
3965 t = SGMLFindTag(dtd, string->data);
3966 }
3967 if (!t || t == me->unknown_tag) {
3968 CTRACE((tfp, "Unknown end tag </%s>\n", string->data));
3969 #ifdef USE_PRETTYSRC
3970 if (psrc_view) {
3971 PSRCSTART(abracket);
3972 PUTS("</");
3973 PSRCSTOP(abracket);
3974 PSRCSTART(badtag);
3975 transform_tag(me, string);
3976 PUTS(string->data);
3977 if (c != '>') {
3978 PUTC(c);
3979 } else {
3980 PSRCSTOP(badtag);
3981 PSRCSTART(abracket);
3982 PUTC('>');
3983 PSRCSTOP(abracket);
3984 }
3985 psrc_tagname_processed = TRUE;
3986 }
3987 } else if (psrc_view) {
3988 #endif
3989 } else {
3990 BOOL tag_OK = (BOOL) (c == '>' || WHITE(c));
3991 HTMLElement e = TAGNUM_OF_TAGP(t);
3992 int branch = 2; /* it can be 0,1,2 */
3993
3994 me->current_tag = t;
3995 if (HAS_ALT_TAGNUM(TAGNUM_OF_TAGP(t)) &&
3996 me->element_stack &&
3997 ALT_TAGP(t) == me->element_stack->tag)
3998 me->element_stack->tag = NORMAL_TAGP(me->element_stack->tag);
3999
4000 if (tag_OK && Old_DTD) {
4001 switch (e) {
4002 case HTML_DD:
4003 case HTML_DT:
4004 case HTML_LI:
4005 case HTML_LH:
4006 case HTML_TD:
4007 case HTML_TH:
4008 case HTML_TR:
4009 case HTML_THEAD:
4010 case HTML_TFOOT:
4011 case HTML_TBODY:
4012 case HTML_COLGROUP:
4013 branch = 0;
4014 break;
4015
4016 case HTML_A:
4017 case HTML_B:
4018 case HTML_BLINK:
4019 case HTML_CITE:
4020 case HTML_EM:
4021 case HTML_FONT:
4022 case HTML_FORM:
4023 case HTML_I:
4024 case HTML_P:
4025 case HTML_STRONG:
4026 case HTML_TT:
4027 case HTML_U:
4028 branch = 1;
4029 break;
4030 default:
4031 break;
4032 }
4033 }
4034
4035 /*
4036 * Just handle ALL end tags normally :-) - kw
4037 */
4038 if (!Old_DTD) {
4039 end_element(me, me->current_tag);
4040 } else if (tag_OK && (branch == 0)) {
4041 /*
4042 * Don't treat these end tags as invalid, nor act on them.
4043 * - FM
4044 */
4045 CTRACE((tfp, "SGML: `</%s%c' found! Ignoring it.\n",
4046 string->data, c));
4047 string->size = 0;
4048 me->current_attribute_number = INVALID;
4049 if (c != '>') {
4050 me->state = S_junk_tag;
4051 } else {
4052 me->current_tag = NULL;
4053 me->state = S_text;
4054 }
4055 break;
4056 } else if (tag_OK && (branch == 1)) {
4057 /*
4058 * Handle end tags for container elements declared as
4059 * SGML_EMPTY to prevent "expected tag substitution" but
4060 * still processed via HTML_end_element() in HTML.c with
4061 * checks there to avoid throwing the HTML.c stack out of
4062 * whack (Ugh, what a hack! 8-). - FM
4063 */
4064 if (me->inSELECT) {
4065 /*
4066 * We are in a SELECT block. - FM
4067 */
4068 if (strcasecomp(string->data, "FORM")) {
4069 /*
4070 * It is not at FORM end tag, so ignore it. - FM
4071 */
4072 CTRACE((tfp,
4073 "SGML: ***Ignoring end tag </%s> in SELECT block.\n",
4074 string->data));
4075 } else {
4076 /*
4077 * End the SELECT block and then handle the FORM
4078 * end tag. - FM
4079 */
4080 CTRACE((tfp,
4081 "SGML: ***Faking SELECT end tag before </%s> end tag.\n",
4082 string->data));
4083 end_element(me,
4084 SGMLFindTag(me->dtd, "SELECT"));
4085 CTRACE((tfp, "SGML: End </%s>\n", string->data));
4086
4087 #ifdef USE_PRETTYSRC
4088 if (!psrc_view) /* Don't actually call if viewing psrc - kw */
4089 #endif
4090 (*me->actions->end_element)
4091 (me->target,
4092 (int) TAGNUM_OF_TAGP(me->current_tag),
4093 &me->include);
4094 }
4095 } else if (!strcasecomp(string->data, "P")) {
4096 /*
4097 * Treat a P end tag like a P start tag (Ugh, what a
4098 * hack! 8-). - FM
4099 */
4100 CTRACE((tfp,
4101 "SGML: `</%s%c' found! Treating as '<%s%c'.\n",
4102 string->data, c, string->data, c));
4103 {
4104 int i;
4105
4106 for (i = 0;
4107 i < me->current_tag->number_of_attributes;
4108 i++) {
4109 me->present[i] = NO;
4110 }
4111 }
4112 if (me->current_tag->name)
4113 start_element(me);
4114 } else {
4115 CTRACE((tfp, "SGML: End </%s>\n", string->data));
4116
4117 #ifdef USE_PRETTYSRC
4118 if (!psrc_view) /* Don't actually call if viewing psrc - kw */
4119 #endif
4120 (*me->actions->end_element)
4121 (me->target,
4122 (int) TAGNUM_OF_TAGP(me->current_tag),
4123 &me->include);
4124 }
4125 string->size = 0;
4126 me->current_attribute_number = INVALID;
4127 if (c != '>') {
4128 me->state = S_junk_tag;
4129 } else {
4130 me->current_tag = NULL;
4131 me->state = S_text;
4132 }
4133 break;
4134 } else {
4135 /*
4136 * Handle all other end tags normally. - FM
4137 */
4138 end_element(me, me->current_tag);
4139 }
4140 }
4141
4142 #ifdef USE_PRETTYSRC
4143 if (psrc_view && !psrc_tagname_processed) {
4144 PSRCSTART(abracket);
4145 PUTS("</");
4146 PSRCSTOP(abracket);
4147 PSRCSTART(tag);
4148 if (tagname_transform != 1) {
4149 if (tagname_transform == 0)
4150 LYLowerCase(string->data);
4151 else
4152 LYUpperCase(string->data);
4153 }
4154 PUTS(string->data);
4155 PSRCSTOP(tag);
4156 if (c != '>') {
4157 PSRCSTART(badtag);
4158 PUTC(c);
4159 } else {
4160 PSRCSTART(abracket);
4161 PUTC('>');
4162 PSRCSTOP(abracket);
4163 }
4164 }
4165 #endif
4166
4167 string->size = 0;
4168 me->current_attribute_number = INVALID;
4169 if (c != '>') {
4170 if (!WHITE(c))
4171 CTRACE((tfp, "SGML: `</%s%c' found!\n", string->data, c));
4172 me->state = S_junk_tag;
4173 } else {
4174 me->current_tag = NULL;
4175 me->state = S_text;
4176 }
4177 }
4178 break;
4179
4180 case S_esc: /* Expecting '$'or '(' following CJK ESC. */
4181 if (c == '$') {
4182 me->state = S_dollar;
4183 } else if (c == '(') {
4184 me->state = S_paren;
4185 } else {
4186 me->state = S_text;
4187 if (UTF8_TTY_ISO2022JP)
4188 goto top1;
4189 }
4190 if (!UTF8_TTY_ISO2022JP)
4191 PUTC(c);
4192 break;
4193
4194 case S_dollar: /* Expecting '@', 'B', 'A' or '(' after CJK "ESC$". */
4195 if (c == '@' || c == 'B' || c == 'A') {
4196 me->state = S_nonascii_text;
4197 } else if (c == '(') {
4198 me->state = S_dollar_paren;
4199 }
4200 if (!UTF8_TTY_ISO2022JP)
4201 PUTC(c);
4202 break;
4203
4204 case S_dollar_paren: /* Expecting 'C' after CJK "ESC$(". */
4205 if (c == 'C') {
4206 me->state = S_nonascii_text;
4207 } else {
4208 me->state = S_text;
4209 if (UTF8_TTY_ISO2022JP) {
4210 PUTS("$(");
4211 goto top1;
4212 }
4213 }
4214 if (!UTF8_TTY_ISO2022JP)
4215 PUTC(c);
4216 break;
4217
4218 case S_paren: /* Expecting 'B', 'J', 'T' or 'I' after CJK "ESC(". */
4219 if (c == 'B' || c == 'J' || c == 'T') {
4220 me->state = S_text;
4221 } else if (c == 'I') {
4222 me->state = S_nonascii_text;
4223 if (UTF8_TTY_ISO2022JP)
4224 me->kanji_buf = '\t'; /* flag for single byte katakana */
4225 } else {
4226 me->state = S_text;
4227 if (UTF8_TTY_ISO2022JP) {
4228 PUTC('(');
4229 goto top1;
4230 }
4231 }
4232 if (!UTF8_TTY_ISO2022JP)
4233 PUTC(c);
4234 break;
4235
4236 case S_nonascii_text: /* Expecting CJK ESC after non-ASCII text. */
4237 if (TOASCII(c) == '\033') { /* S/390 -- gil -- 1264 */
4238 me->state = S_esc;
4239 } else if (c < 32) {
4240 me->state = S_text;
4241 }
4242 if (UTF8_TTY_ISO2022JP) {
4243 if (TOASCII(c) != '\033')
4244 PUTUTF8(clong);
4245 } else
4246 PUTC(c);
4247 break;
4248
4249 case S_esc_sq: /* Expecting '$'or '(' following CJK ESC. */
4250 if (c == '$') {
4251 me->state = S_dollar_sq;
4252 } else if (c == '(') {
4253 me->state = S_paren_sq;
4254 } else {
4255 me->state = S_squoted;
4256 if (UTF8_TTY_ISO2022JP)
4257 goto top1;
4258 }
4259 if (!UTF8_TTY_ISO2022JP)
4260 HTChunkPutc(string, c);
4261 break;
4262
4263 case S_dollar_sq: /* Expecting '@', 'B', 'A' or '(' after CJK "ESC$". */
4264 if (c == '@' || c == 'B' || c == 'A') {
4265 me->state = S_nonascii_text_sq;
4266 } else if (c == '(') {
4267 me->state = S_dollar_paren_sq;
4268 }
4269 if (!UTF8_TTY_ISO2022JP)
4270 HTChunkPutc(string, c);
4271 break;
4272
4273 case S_dollar_paren_sq: /* Expecting 'C' after CJK "ESC$(". */
4274 if (c == 'C') {
4275 me->state = S_nonascii_text_sq;
4276 } else {
4277 me->state = S_squoted;
4278 if (UTF8_TTY_ISO2022JP) {
4279 HTChunkPuts(string, "$(");
4280 goto top1;
4281 }
4282 }
4283 if (!UTF8_TTY_ISO2022JP)
4284 HTChunkPutc(string, c);
4285 break;
4286
4287 case S_paren_sq: /* Expecting 'B', 'J', 'T' or 'I' after CJK "ESC(". */
4288 if (c == 'B' || c == 'J' || c == 'T') {
4289 me->state = S_squoted;
4290 } else if (c == 'I') {
4291 me->state = S_nonascii_text_sq;
4292 if (UTF8_TTY_ISO2022JP)
4293 me->kanji_buf = '\t'; /* flag for single byte katakana */
4294 } else {
4295 me->state = S_squoted;
4296 if (UTF8_TTY_ISO2022JP) {
4297 HTChunkPutc(string, '(');
4298 goto top1;
4299 }
4300 }
4301 if (!UTF8_TTY_ISO2022JP)
4302 HTChunkPutc(string, c);
4303 break;
4304
4305 case S_nonascii_text_sq: /* Expecting CJK ESC after non-ASCII text. */
4306 if (TOASCII(c) == '\033') { /* S/390 -- gil -- 1281 */
4307 me->state = S_esc_sq;
4308 }
4309 if (UTF8_TTY_ISO2022JP) {
4310 if (TOASCII(c) != '\033')
4311 HTChunkPutUtf8Char(string, clong);
4312 } else
4313 HTChunkPutc(string, c);
4314 break;
4315
4316 case S_esc_dq: /* Expecting '$'or '(' following CJK ESC. */
4317 if (c == '$') {
4318 me->state = S_dollar_dq;
4319 } else if (c == '(') {
4320 me->state = S_paren_dq;
4321 } else {
4322 me->state = S_dquoted;
4323 if (UTF8_TTY_ISO2022JP)
4324 goto top1;
4325 }
4326 if (!UTF8_TTY_ISO2022JP)
4327 HTChunkPutc(string, c);
4328 break;
4329
4330 case S_dollar_dq: /* Expecting '@', 'B', 'A' or '(' after CJK "ESC$". */
4331 if (c == '@' || c == 'B' || c == 'A') {
4332 me->state = S_nonascii_text_dq;
4333 } else if (c == '(') {
4334 me->state = S_dollar_paren_dq;
4335 }
4336 if (!UTF8_TTY_ISO2022JP)
4337 HTChunkPutc(string, c);
4338 break;
4339
4340 case S_dollar_paren_dq: /* Expecting 'C' after CJK "ESC$(". */
4341 if (c == 'C') {
4342 me->state = S_nonascii_text_dq;
4343 } else {
4344 me->state = S_dquoted;
4345 if (UTF8_TTY_ISO2022JP) {
4346 HTChunkPuts(string, "$(");
4347 goto top1;
4348 }
4349 }
4350 if (!UTF8_TTY_ISO2022JP)
4351 HTChunkPutc(string, c);
4352 break;
4353
4354 case S_paren_dq: /* Expecting 'B', 'J', 'T' or 'I' after CJK "ESC(". */
4355 if (c == 'B' || c == 'J' || c == 'T') {
4356 me->state = S_dquoted;
4357 } else if (c == 'I') {
4358 me->state = S_nonascii_text_dq;
4359 if (UTF8_TTY_ISO2022JP)
4360 me->kanji_buf = '\t'; /* flag for single byte katakana */
4361 } else {
4362 me->state = S_dquoted;
4363 if (UTF8_TTY_ISO2022JP) {
4364 HTChunkPutc(string, '(');
4365 goto top1;
4366 }
4367 }
4368 if (!UTF8_TTY_ISO2022JP)
4369 HTChunkPutc(string, c);
4370 break;
4371
4372 case S_nonascii_text_dq: /* Expecting CJK ESC after non-ASCII text. */
4373 if (TOASCII(c) == '\033') { /* S/390 -- gil -- 1298 */
4374 me->state = S_esc_dq;
4375 }
4376 if (UTF8_TTY_ISO2022JP) {
4377 if (TOASCII(c) != '\033')
4378 HTChunkPutUtf8Char(string, clong);
4379 } else
4380 HTChunkPutc(string, c);
4381 break;
4382
4383 case S_junk_tag:
4384 case S_pi:
4385 if (c == '>') {
4386 HTChunkTerminate(string);
4387 #ifdef USE_PRETTYSRC
4388 if (psrc_view) {
4389 if (me->state == S_junk_tag) {
4390 PSRCSTOP(badtag);
4391 }
4392 PSRCSTART(abracket);
4393 PUTC('>');
4394 PSRCSTOP(abracket);
4395 }
4396 #endif
4397 if (me->state == S_pi)
4398 handle_processing_instruction(me);
4399 string->size = 0;
4400 me->current_tag = NULL;
4401 me->state = S_text;
4402 } else {
4403 HTChunkPutc(string, c);
4404 #ifdef USE_PRETTYSRC
4405 if (psrc_view) {
4406 PUTC(c);
4407 }
4408 #endif
4409 }
4410
4411 } /* switch on me->state */
4412 CTRACE2(TRACE_SGML, (tfp, "SGML after %s|%.*s|%c|\n",
4413 state_name(me->state),
4414 string->size,
4415 NonNull(string->data),
4416 UCH(c)));
4417
4418 after_switch:
4419 /*
4420 * Check whether an external function has added anything to the include
4421 * buffer. If so, move the new stuff to the beginning of active_include.
4422 * - kw
4423 */
4424 if (me->include != NULL) {
4425 if (me->include[0] == '\0') {
4426 FREE(me->include);
4427 } else {
4428 if (me->active_include &&
4429 me->active_include[me->include_index] != '\0')
4430 StrAllocCat(me->include,
4431 me->active_include + me->include_index);
4432 FREE(me->active_include);
4433 me->active_include = me->include;
4434 me->include_index = 0;
4435 me->include = NULL;
4436 }
4437 }
4438
4439 /*
4440 * Check whether we've added anything to the recover buffer. - FM
4441 */
4442 if (me->recover != NULL) {
4443 if (me->recover[me->recover_index] == '\0') {
4444 FREE(me->recover);
4445 me->recover_index = 0;
4446 } else {
4447 c = UCH(me->recover[me->recover_index]);
4448 me->recover_index++;
4449 goto top;
4450 }
4451 }
4452
4453 /*
4454 * Check whether an external function had added anything to the include
4455 * buffer; it should now be in active_include. - FM / kw
4456 */
4457 if (me->active_include != NULL) {
4458 if (me->active_include[me->include_index] == '\0') {
4459 FREE(me->active_include);
4460 me->include_index = 0;
4461 } else {
4462 if (me->current_tag_charset == UTF8_handle ||
4463 me->T.trans_from_uni) {
4464 /*
4465 * If it looks like we would have fed UTF-8 to the next
4466 * processing stage, assume that whatever we were fed back is
4467 * in UTF-8 form, too. This won't be always true for all uses
4468 * of the include buffer, but it's a start. - kw
4469 */
4470 const char *puni = me->active_include + me->include_index;
4471
4472 c = UCH(*puni);
4473 clong = UCGetUniFromUtf8String(&puni);
4474 if (clong < 256 && clong >= 0) {
4475 c = UCH((clong & 0xff));
4476 }
4477 saved_char_in = '\0';
4478 me->include_index = (int) (puni
4479 - me->active_include
4480 + 1);
4481 goto top1;
4482 } else {
4483 /*
4484 * Otherwise assume no UTF-8 - do charset-naive processing and
4485 * hope for the best. - kw
4486 */
4487 c = UCH(me->active_include[me->include_index]);
4488 me->include_index++;
4489 goto top;
4490 }
4491 }
4492 }
4493
4494 /*
4495 * Check whether an external function has added anything to the csi buffer.
4496 * - FM
4497 */
4498 if (me->csi != NULL) {
4499 if (me->csi[me->csi_index] == '\0') {
4500 FREE(me->csi);
4501 me->csi_index = 0;
4502 } else {
4503 c = UCH(me->csi[me->csi_index]);
4504 me->csi_index++;
4505 goto top;
4506 }
4507 }
4508 } /* SGML_character */
4509
4510 static void InferUtfFromBom(HTStream *me, int chndl)
4511 {
4512 HTAnchor_setUCInfoStage(me->node_anchor, chndl,
4513 UCT_STAGE_PARSER,
4514 UCT_SETBY_PARSER);
4515 change_chartrans_handling(me);
4516 }
4517
4518 /*
4519 * Avoid rewrite of SGML_character() to handle hypothetical case of UTF-16
4520 * webpages, by pretending that the data is UTF-8.
4521 */
4522 static void SGML_widechar(HTStream *me, int ch)
4523 {
4524 if (!UCPutUtf8_charstring(me, SGML_character, (UCode_t) ch)) {
4525 SGML_character(me, ch);
4526 }
4527 }
4528
4529 static void SGML_write(HTStream *me, const char *str, int l)
4530 {
4531 const char *p;
4532 const char *e = str + l;
4533
4534 if (sgml_offset == 0) {
4535 if (l > 3
4536 && !MemCmp(str, "\357\273\277", 3)) {
4537 CTRACE((tfp, "SGML_write found UTF-8 BOM\n"));
4538 InferUtfFromBom(me, UTF8_handle);
4539 str += 3;
4540 } else if (l > 2) {
4541 if (!MemCmp(str, "\377\376", 2)) {
4542 CTRACE((tfp, "SGML_write found UCS-2 LE BOM\n"));
4543 InferUtfFromBom(me, UTF8_handle);
4544 str += 2;
4545 me->T.ucs_mode = -1;
4546 } else if (!MemCmp(str, "\376\377", 2)) {
4547 CTRACE((tfp, "SGML_write found UCS-2 BE BOM\n"));
4548 InferUtfFromBom(me, UTF8_handle);
4549 str += 2;
4550 me->T.ucs_mode = 1;
4551 }
4552 }
4553 }
4554 switch (me->T.ucs_mode) {
4555 case -1:
4556 for (p = str; p < e; p += 2)
4557 SGML_widechar(me, (UCH(p[1]) << 8) | UCH(p[0]));
4558 break;
4559 case 1:
4560 for (p = str; p < e; p += 2)
4561 SGML_widechar(me, (UCH(p[0]) << 8) | UCH(p[1]));
4562 break;
4563 default:
4564 for (p = str; p < e; p++)
4565 SGML_character(me, *p);
4566 break;
4567 }
4568 }
4569
4570 static void SGML_string(HTStream *me, const char *str)
4571 {
4572 SGML_write(me, str, (int) strlen(str));
4573 }
4574
4575 /*_______________________________________________________________________
4576 */
4577
4578 /* Structured Object Class
4579 * -----------------------
4580 */
4581 const HTStreamClass SGMLParser =
4582 {
4583 "SGMLParser",
4584 SGML_free,
4585 SGML_abort,
4586 SGML_character,
4587 SGML_string,
4588 SGML_write,
4589 };
4590
4591 /* Create SGML Engine
4592 * ------------------
4593 *
4594 * On entry,
4595 * dtd represents the DTD, along with
4596 * actions is the sink for the data as a set of routines.
4597 *
4598 */
4599
4600 HTStream *SGML_new(const SGML_dtd * dtd,
4601 HTParentAnchor *anchor,
4602 HTStructured * target,
4603 int extended_html)
4604 {
4605 HTStream *me = typecalloc(struct _HTStream);
4606
4607 if (!me)
4608 outofmem(__FILE__, "SGML_begin");
4609
4610 me->isa = &SGMLParser;
4611 me->string = HTChunkCreate(128); /* Grow by this much */
4612 me->dtd = dtd;
4613 me->target = target;
4614 me->actions = (const HTStructuredClass *) (((HTStream *) target)->isa);
4615 /* Ugh: no OO */
4616 me->unknown_tag = &HTTag_unrecognized;
4617 me->current_tag = me->slashedtag = NULL;
4618 me->state = S_text;
4619 #ifdef CALLERDATA
4620 me->callerData = (void *) callerData;
4621 #endif /* CALLERDATA */
4622
4623 me->node_anchor = anchor; /* Could be NULL? */
4624 me->U.utf_buf_p = me->U.utf_buf;
4625 UCTransParams_clear(&me->T);
4626 me->inUCLYhndl = HTAnchor_getUCLYhndl(anchor,
4627 UCT_STAGE_PARSER);
4628 if (me->inUCLYhndl < 0) {
4629 HTAnchor_copyUCInfoStage(anchor,
4630 UCT_STAGE_PARSER,
4631 UCT_STAGE_MIME,
4632 -1);
4633 me->inUCLYhndl = HTAnchor_getUCLYhndl(anchor,
4634 UCT_STAGE_PARSER);
4635 }
4636 #ifdef CAN_SWITCH_DISPLAY_CHARSET /* Allow a switch to a more suitable display charset */
4637 else if (anchor->UCStages
4638 && anchor->UCStages->s[UCT_STAGE_PARSER].LYhndl >= 0
4639 && anchor->UCStages->s[UCT_STAGE_PARSER].LYhndl != current_char_set) {
4640 int o = anchor->UCStages->s[UCT_STAGE_PARSER].LYhndl;
4641
4642 anchor->UCStages->s[UCT_STAGE_PARSER].LYhndl = -1; /* Force reset */
4643 HTAnchor_resetUCInfoStage(anchor, o, UCT_STAGE_PARSER,
4644 /* Preserve change this: */
4645 anchor->UCStages->s[UCT_STAGE_PARSER].lock);
4646 }
4647 #endif
4648
4649 me->inUCI = HTAnchor_getUCInfoStage(anchor,
4650 UCT_STAGE_PARSER);
4651 set_chartrans_handling(me, anchor, -1);
4652
4653 me->recover = NULL;
4654 me->recover_index = 0;
4655 me->include = NULL;
4656 me->active_include = NULL;
4657 me->include_index = 0;
4658 me->url = NULL;
4659 me->csi = NULL;
4660 me->csi_index = 0;
4661
4662 #ifdef USE_PRETTYSRC
4663 if (psrc_view) {
4664 psrc_view = FALSE;
4665 mark_htext_as_source = TRUE;
4666 SGML_string(me,
4667 "<HTML><HEAD><TITLE>source</TITLE></HEAD><BODY><PRE>");
4668 psrc_view = TRUE;
4669 psrc_convert_string = FALSE;
4670 sgml_in_psrc_was_initialized = TRUE;
4671 }
4672 #endif
4673 if (extended_html) {
4674 me->extended_html = TRUE;
4675 }
4676
4677 sgml_offset = 0;
4678 return me;
4679 }
4680
4681 /*
4682 * Return the offset within the document where we're parsing. This is used
4683 * to help identify anchors which shift around while reparsing.
4684 */
4685 int SGML_offset(void)
4686 {
4687 int result = sgml_offset;
4688
4689 #ifdef USE_PRETTYSRC
4690 result += psrc_view;
4691 #endif
4692 return result;
4693 }
4694
4695 /* Asian character conversion functions
4696 * ====================================
4697 *
4698 * Added 24-Mar-96 by FM, based on:
4699 *
4700 ////////////////////////////////////////////////////////////////////////
4701 Copyright (c) 1993 Electrotechnical Laboratory (ETL)
4702
4703 Permission to use, copy, modify, and distribute this material
4704 for any purpose and without fee is hereby granted, provided
4705 that the above copyright notice and this permission notice
4706 appear in all copies, and that the name of ETL not be
4707 used in advertising or publicity pertaining to this
4708 material without the specific, prior written permission
4709 of an authorized representative of ETL.
4710 ETL MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
4711 OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
4712 WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
4713 /////////////////////////////////////////////////////////////////////////
4714 Content-Type: program/C; charset=US-ASCII
4715 Program: SJIS.c
4716 Author: Yutaka Sato <[email protected]>
4717 Description:
4718 History:
4719 930923 extracted from codeconv.c of cosmos
4720 ///////////////////////////////////////////////////////////////////////
4721 */
4722
4723 static int TREAT_SJIS = 1;
4724
4725 void JISx0201TO0208_EUC(unsigned IHI,
4726 unsigned ILO,
4727 unsigned char *OHI,
4728 unsigned char *OLO)
4729 {
4730 static const char *table[] =
4731 {
4732 "\241\243", /* A1,A3 */
4733 "\241\326", /* A1,D6 */
4734 "\241\327", /* A1,D7 */
4735 "\241\242", /* A1,A2 */
4736 "\241\246", /* A1,A6 */
4737 "\245\362", /* A5,F2 */
4738 "\245\241", /* A5,A1 */
4739 "\245\243", /* A5,A3 */
4740 "\245\245", /* A5,A5 */
4741 "\245\247", /* A5,A7 */
4742 "\245\251", /* A5,A9 */
4743 "\245\343", /* A5,E3 */
4744 "\245\345", /* A5,E5 */
4745 "\245\347", /* A5,E7 */
4746 "\245\303", /* A5,C3 */
4747 "\241\274", /* A1,BC */
4748 "\245\242", /* A5,A2 */
4749 "\245\244", /* A5,A4 */
4750 "\245\246", /* A5,A6 */
4751 "\245\250", /* A5,A8 */
4752 "\245\252", /* A5,AA */
4753 "\245\253", /* A5,AB */
4754 "\245\255", /* A5,AD */
4755 "\245\257", /* A5,AF */
4756 "\245\261", /* A5,B1 */
4757 "\245\263", /* A5,B3 */
4758 "\245\265", /* A5,B5 */
4759 "\245\267", /* A5,B7 */
4760 "\245\271", /* A5,B9 */
4761 "\245\273", /* A5,BB */
4762 "\245\275", /* A5,BD */
4763 "\245\277", /* A5,BF */
4764 "\245\301", /* A5,C1 */
4765 "\245\304", /* A5,C4 */
4766 "\245\306", /* A5,C6 */
4767 "\245\310", /* A5,C8 */
4768 "\245\312", /* A5,CA */
4769 "\245\313", /* A5,CB */
4770 "\245\314", /* A5,CC */
4771 "\245\315", /* A5,CD */
4772 "\245\316", /* A5,CE */
4773 "\245\317", /* A5,CF */
4774 "\245\322", /* A5,D2 */
4775 "\245\325", /* A5,D5 */
4776 "\245\330", /* A5,D8 */
4777 "\245\333", /* A5,DB */
4778 "\245\336", /* A5,DE */
4779 "\245\337", /* A5,DF */
4780 "\245\340", /* A5,E0 */
4781 "\245\341", /* A5,E1 */
4782 "\245\342", /* A5,E2 */
4783 "\245\344", /* A5,E4 */
4784 "\245\346", /* A5,E6 */
4785 "\245\350", /* A5,E8 */
4786 "\245\351", /* A5,E9 */
4787 "\245\352", /* A5,EA */
4788 "\245\353", /* A5,EB */
4789 "\245\354", /* A5,EC */
4790 "\245\355", /* A5,ED */
4791 "\245\357", /* A5,EF */
4792 "\245\363", /* A5,F3 */
4793 "\241\253", /* A1,AB */
4794 "\241\254" /* A1,AC */
4795 };
4796
4797 if ((IHI == 0x8E) && (ILO >= 0xA1) && (ILO <= 0xDF)) {
4798 *OHI = UCH(table[ILO - 0xA1][0]);
4799 *OLO = UCH(table[ILO - 0xA1][1]);
4800 } else {
4801 *OHI = UCH(IHI);
4802 *OLO = UCH(ILO);
4803 }
4804 }
4805
4806 static int IS_SJIS_STR(const unsigned char *str)
4807 {
4808 const unsigned char *s;
4809 unsigned char ch;
4810 int is_sjis = 0;
4811
4812 s = str;
4813 while ((ch = *s++) != '\0') {
4814 if (ch & 0x80)
4815 if (IS_SJIS(ch, *s, is_sjis))
4816 return 1;
4817 }
4818 return 0;
4819 }
4820
4821 unsigned char *SJIS_TO_JIS1(unsigned HI,
4822 unsigned LO,
4823 unsigned char *JCODE)
4824 {
4825 HI = UCH(HI - (unsigned) UCH((HI <= 0x9F) ? 0x71 : 0xB1));
4826 HI = UCH((HI << 1) + 1);
4827 if (0x7F < LO)
4828 LO--;
4829 if (0x9E <= LO) {
4830 LO = UCH(LO - UCH(0x7D));
4831 HI++;
4832 } else {
4833 LO = UCH(LO - UCH(0x1F));
4834 }
4835 JCODE[0] = UCH(HI);
4836 JCODE[1] = UCH(LO);
4837 return JCODE;
4838 }
4839
4840 unsigned char *JIS_TO_SJIS1(unsigned HI,
4841 unsigned LO,
4842 unsigned char *SJCODE)
4843 {
4844 if (HI & 1)
4845 LO = UCH(LO + UCH(0x1F));
4846 else
4847 LO = UCH(LO + UCH(0x7D));
4848 if (0x7F <= LO)
4849 LO++;
4850
4851 HI = UCH(((HI - 0x21) >> 1) + 0x81);
4852 if (0x9F < HI)
4853 HI = UCH(HI + UCH(0x40));
4854 SJCODE[0] = UCH(HI);
4855 SJCODE[1] = UCH(LO);
4856 return SJCODE;
4857 }
4858
4859 unsigned char *EUC_TO_SJIS1(unsigned HI,
4860 unsigned LO,
4861 unsigned char *SJCODE)
4862 {
4863 unsigned char HI_data[2];
4864 unsigned char LO_data[2];
4865
4866 HI_data[0] = UCH(HI);
4867 LO_data[0] = UCH(LO);
4868 if (HI == 0x8E) {
4869 JISx0201TO0208_EUC(HI, LO, HI_data, LO_data);
4870 }
4871 JIS_TO_SJIS1(UCH(HI_data[0] & 0x7F), UCH(LO_data[0] & 0x7F), SJCODE);
4872 return SJCODE;
4873 }
4874
4875 void JISx0201TO0208_SJIS(unsigned I,
4876 unsigned char *OHI,
4877 unsigned char *OLO)
4878 {
4879 unsigned char SJCODE[2];
4880
4881 JISx0201TO0208_EUC(0x8E, I, OHI, OLO);
4882 JIS_TO_SJIS1(UCH(*OHI & 0x7F), UCH(*OLO & 0x7F), SJCODE);
4883 *OHI = SJCODE[0];
4884 *OLO = SJCODE[1];
4885 }
4886
4887 unsigned char *SJIS_TO_EUC1(unsigned HI,
4888 unsigned LO,
4889 unsigned char *data)
4890 {
4891 SJIS_TO_JIS1(HI, LO, data);
4892 data[0] |= 0x80;
4893 data[1] |= 0x80;
4894 return data;
4895 }
4896
4897 unsigned char *SJIS_TO_EUC(unsigned char *src,
4898 unsigned char *dst)
4899 {
4900 unsigned char hi, lo, *sp, *dp;
4901 int in_sjis = 0;
4902
4903 in_sjis = IS_SJIS_STR(src);
4904 for (sp = src, dp = dst; (hi = sp[0]) != '\0';) {
4905 lo = sp[1];
4906 if (TREAT_SJIS && IS_SJIS(hi, lo, in_sjis)) {
4907 SJIS_TO_JIS1(hi, lo, dp);
4908 dp[0] |= 0x80;
4909 dp[1] |= 0x80;
4910 dp += 2;
4911 sp += 2;
4912 } else
4913 *dp++ = *sp++;
4914 }
4915 *dp = 0;
4916 return dst;
4917 }
4918
4919 unsigned char *EUC_TO_SJIS(unsigned char *src,
4920 unsigned char *dst)
4921 {
4922 unsigned char *sp, *dp;
4923
4924 for (sp = src, dp = dst; *sp;) {
4925 if (*sp & 0x80) {
4926 if (sp[1] && (sp[1] & 0x80)) {
4927 JIS_TO_SJIS1(UCH(sp[0] & 0x7F), UCH(sp[1] & 0x7F), dp);
4928 dp += 2;
4929 sp += 2;
4930 } else {
4931 sp++;
4932 }
4933 } else {
4934 *dp++ = *sp++;
4935 }
4936 }
4937 *dp = 0;
4938 return dst;
4939 }
4940
4941 #define Strcpy(a,b) (strcpy((char*)a,(const char*)b),&a[strlen((const char*)a)])
4942
4943 unsigned char *EUC_TO_JIS(unsigned char *src,
4944 unsigned char *dst,
4945 const char *toK,
4946 const char *toA)
4947 {
4948 unsigned char kana_mode = 0;
4949 unsigned char cch;
4950 unsigned char *sp = src;
4951 unsigned char *dp = dst;
4952 int is_JIS = 0;
4953
4954 while ((cch = *sp++) != '\0') {
4955 if (cch & 0x80) {
4956 if (!IS_EUC(cch, *sp)) {
4957 if (cch == 0xA0 && is_JIS) /* ignore NBSP */
4958 continue;
4959 is_JIS++;
4960 *dp++ = cch;
4961 continue;
4962 }
4963 if (!kana_mode) {
4964 kana_mode = UCH(~kana_mode);
4965 dp = Strcpy(dp, toK);
4966 }
4967 if (*sp & 0x80) {
4968 *dp++ = UCH(cch & ~0x80);
4969 *dp++ = UCH(*sp++ & ~0x80);
4970 }
4971 } else {
4972 if (kana_mode) {
4973 kana_mode = UCH(~kana_mode);
4974 dp = Strcpy(dp, toA);
4975 }
4976 *dp++ = cch;
4977 }
4978 }
4979 if (kana_mode)
4980 dp = Strcpy(dp, toA);
4981
4982 if (dp)
4983 *dp = 0;
4984 return dst;
4985 }
4986
4987 #define IS_JIS7(c1,c2) (0x20<(c1)&&(c1)<0x7F && 0x20<(c2)&&(c2)<0x7F)
4988 #define SO ('N'-0x40)
4989 #define SI ('O'-0x40)
4990
4991 static int repair_JIS = 0;
4992
4993 static const unsigned char *repairJIStoEUC(const unsigned char *src,
4994 unsigned char **dstp)
4995 {
4996 const unsigned char *s;
4997 unsigned char *d, ch1, ch2;
4998
4999 d = *dstp;
5000 s = src;
5001 while ((ch1 = s[0]) && (ch2 = s[1])) {
5002 s += 2;
5003 if (ch1 == '(')
5004 if (ch2 == 'B' || ch2 == 'J') {
5005 *dstp = d;
5006 return s;
5007 }
5008 if (!IS_JIS7(ch1, ch2))
5009 return NULL;
5010
5011 *d++ = UCH(0x80 | ch1);
5012 *d++ = UCH(0x80 | ch2);
5013 }
5014 return NULL;
5015 }
5016
5017 unsigned char *TO_EUC(const unsigned char *jis,
5018 unsigned char *euc)
5019 {
5020 const unsigned char *s;
5021 unsigned char c, jis_stat;
5022 unsigned char *d;
5023 int to1B, to2B;
5024 int in_sjis = 0;
5025 int n8bits;
5026 int is_JIS;
5027
5028 n8bits = 0;
5029 s = jis;
5030 d = euc;
5031 jis_stat = 0;
5032 to2B = TO_2BCODE;
5033 to1B = TO_1BCODE;
5034 in_sjis = IS_SJIS_STR(jis);
5035 is_JIS = 0;
5036
5037 while ((c = *s++) != '\0') {
5038 if (c == 0x80)
5039 continue; /* ignore it */
5040 if (c == 0xA0 && is_JIS)
5041 continue; /* ignore Non-breaking space */
5042
5043 if (c == to2B && jis_stat == 0 && repair_JIS) {
5044 if (*s == 'B' || *s == '@') {
5045 const unsigned char *ts;
5046
5047 if ((ts = repairJIStoEUC(s + 1, &d)) != NULL) {
5048 s = ts;
5049 continue;
5050 }
5051 }
5052 }
5053 if (c == CH_ESC) {
5054 if (*s == to2B) {
5055 if ((s[1] == 'B') || (s[1] == '@')) {
5056 jis_stat = 0x80;
5057 s += 2;
5058 is_JIS++;
5059 continue;
5060 }
5061 jis_stat = 0;
5062 } else if (*s == to1B) {
5063 jis_stat = 0;
5064 if ((s[1] == 'B') || (s[1] == 'J') || (s[1] == 'H')) {
5065 s += 2;
5066 continue;
5067 }
5068 } else if (*s == ',') { /* MULE */
5069 jis_stat = 0;
5070 }
5071 }
5072 if (c & 0x80)
5073 n8bits++;
5074
5075 if (IS_SJIS(c, *s, in_sjis)) {
5076 SJIS_TO_EUC1(c, *s, d);
5077 d += 2;
5078 s++;
5079 is_JIS++;
5080 } else if (jis_stat) {
5081 if (c <= 0x20 || 0x7F <= c) {
5082 *d++ = c;
5083 if (c == '\n')
5084 jis_stat = 0;
5085 } else {
5086 if (IS_JIS7(c, *s)) {
5087 *d++ = jis_stat | c;
5088 *d++ = jis_stat | *s++;
5089 } else
5090 *d++ = c;
5091 }
5092 } else {
5093 if (n8bits == 0 && (c == SI || c == SO)) {
5094 } else {
5095 *d++ = c;
5096 }
5097 }
5098 }
5099 *d = 0;
5100 return euc;
5101 }
5102
5103 #define non94(ch) ((ch) <= 0x20 || (ch) == 0x7F)
5104
5105 static int is_EUC_JP(unsigned char *euc)
5106 {
5107 unsigned char *cp;
5108 int ch1, ch2;
5109
5110 for (cp = euc; (ch1 = *cp) != '\0'; cp++) {
5111 if (ch1 & 0x80) {
5112 ch2 = cp[1] & 0xFF;
5113 if ((ch2 & 0x80) == 0) {
5114 /* sv1log("NOT_EUC1[%x][%x]\n",ch1,ch2); */
5115 return 0;
5116 }
5117 if (non94(ch1 & 0x7F) || non94(ch2 & 0x7F)) {
5118 /* sv1log("NOT_EUC2[%x][%x]\n",ch1,ch2); */
5119 return 0;
5120 }
5121 cp++;
5122 }
5123 }
5124 return 1;
5125 }
5126
5127 void TO_SJIS(const unsigned char *arg,
5128 unsigned char *sjis)
5129 {
5130 unsigned char *euc;
5131
5132 euc = typeMallocn(unsigned char, strlen((const char *) arg) + 1);
5133
5134 #ifdef CJK_EX
5135 if (!euc)
5136 outofmem(__FILE__, "TO_SJIS");
5137 #endif
5138 TO_EUC(arg, euc);
5139 if (is_EUC_JP(euc))
5140 EUC_TO_SJIS(euc, sjis);
5141 else
5142 strcpy((char *) sjis, (const char *) arg);
5143 free(euc);
5144 }
5145
5146 void TO_JIS(const unsigned char *arg,
5147 unsigned char *jis)
5148 {
5149 unsigned char *euc;
5150
5151 if (arg[0] == 0) {
5152 jis[0] = 0;
5153 return;
5154 }
5155 euc = typeMallocn(unsigned char, strlen((const char *)arg) + 1);
5156 #ifdef CJK_EX
5157 if (!euc)
5158 outofmem(__FILE__, "TO_JIS");
5159 #endif
5160 TO_EUC(arg, euc);
5161 EUC_TO_JIS(euc, jis, TO_KANJI, TO_ASCII);
5162
5163 free(euc);
5164 }