Debian Codegraph @

WWW/Library/Implementation/HTPlain.c

raw
1 /*
2 * $LynxId: HTPlain.c,v 1.67 2025/09/17 22:32:29 tom Exp $
3 *
4 * Plain text object HTWrite.c
5 * =================
6 *
7 * This version of the stream object just writes to a socket.
8 * The socket is assumed open and left open.
9 *
10 * Bugs:
11 * strings written must be less than buffer size.
12 */
13
14 #define HTSTREAM_INTERNAL 1
15
16 #include <HTUtils.h>
17 #include <LYCharVals.h> /* S/390 -- gil -- 0288 */
18
19 #include <HTPlain.h>
20
21 #include <HTChunk.h>
22 #include <HText.h>
23 #include <HTStyle.h>
24 #define Lynx_HTML_Handler
25 #include <HTML.h> /* styles[] */
26
27 #define BUFFER_SIZE 4096; /* Tradeoff */
28
29 #include <HTMLDTD.h>
30 #include <HTCJK.h>
31 #include <UCMap.h>
32 #include <UCDefs.h>
33 #include <UCAux.h>
34
35 #include <LYCharSets.h>
36 #include <LYStrings.h>
37 #include <LYLeaks.h>
38
39 static int HTPlain_lastraw = -1;
40 static int HTPlain_bs_pending = 0; /* 1:bs 2:underline 3:underline+bs - kw */
41
42 /* HTML Object
43 * -----------
44 */
45 struct _HTStream {
46 const HTStreamClass *isa;
47 HText *text;
48 /*
49 * The node_anchor UCInfo and handle for the input (PARSER) stage. - FM
50 */
51 LYUCcharset *inUCI;
52 int inUCLYhndl;
53 /*
54 * The node_anchor UCInfo and handle for the output (HTEXT) stage. - FM
55 */
56 LYUCcharset *outUCI;
57 int outUCLYhndl;
58
59 UTFDecodeState U;
60 UCTransParams T;
61 };
62
63 static char replace_buf[64]; /* buffer for replacement strings */
64
65 static void HTPlain_getChartransInfo(HTStream *me, HTParentAnchor *anchor)
66 {
67 if (me->inUCLYhndl < 0) {
68 HTAnchor_copyUCInfoStage(anchor, UCT_STAGE_PARSER, UCT_STAGE_MIME,
69 UCT_SETBY_PARSER);
70 me->inUCLYhndl = HTAnchor_getUCLYhndl(anchor, UCT_STAGE_PARSER);
71 }
72 if (me->outUCLYhndl < 0) {
73 int chndl = HTAnchor_getUCLYhndl(anchor, UCT_STAGE_HTEXT);
74
75 if (chndl < 0) {
76 chndl = current_char_set;
77 HTAnchor_setUCInfoStage(anchor, chndl,
78 UCT_STAGE_HTEXT, UCT_SETBY_DEFAULT);
79 }
80 HTAnchor_setUCInfoStage(anchor, chndl,
81 UCT_STAGE_HTEXT, UCT_SETBY_DEFAULT);
82 me->outUCLYhndl = HTAnchor_getUCLYhndl(anchor, UCT_STAGE_HTEXT);
83 }
84 me->inUCI = HTAnchor_getUCInfoStage(anchor, UCT_STAGE_PARSER);
85 me->outUCI = HTAnchor_getUCInfoStage(anchor, UCT_STAGE_HTEXT);
86 }
87
88 /* Write the buffer out to the socket
89 * ----------------------------------
90 */
91
92 /*_________________________________________________________________________
93 *
94 * A C T I O N R O U T I N E S
95 */
96
97 static void HTPlain_write(HTStream *me, const char *s,
98 int l);
99
100 /* Character handling
101 * ------------------
102 */
103 static void HTPlain_put_character(HTStream *me, int c)
104 {
105 #ifdef REMOVE_CR_ONLY
106 /*
107 * Throw away \r's.
108 */
109 if (c != '\r') {
110 HText_appendCharacter(me->text, c);
111 }
112 #else
113 /*
114 * See HTPlain_write() for explanations of the following code (we've been
115 * called via HTPlain_put_string() to do for each character of a terminated
116 * string what HTPlain_write() does via a while loop for each character in
117 * a stream of given length). - FM
118 */
119 if ((HTPlain_lastraw == '\r') && c == '\n') {
120 HTPlain_lastraw = -1;
121 return;
122 }
123 if (c == '\b' || c == '_' || HTPlain_bs_pending) {
124 char temp[1];
125
126 temp[0] = (char) c;
127 HTPlain_write(me, temp, 1);
128 return;
129 }
130 HTPlain_lastraw = UCH(c);
131 if (c == '\r') {
132 HText_appendCharacter(me->text, '\n');
133 } else if (TOASCII(UCH(c)) >= 127) { /* S/390 -- gil -- 0305 */
134 char temp[1];
135
136 temp[0] = (char) c;
137 /*
138 * For now, don't repeat everything here that has been done below - KW
139 */
140 HTPlain_write(me, temp, 1);
141 } else if (IS_CJK_TTY) {
142 HText_appendCharacter(me->text, c);
143 } else if (TOASCII(UCH(c)) >= 127 && TOASCII(UCH(c)) < 161 &&
144 HTPassHighCtrlRaw) {
145 HText_appendCharacter(me->text, c);
146 #if CH_NBSP < 127
147 } else if (UCH(c) == CH_NBSP) { /* S/390 -- gil -- 0341 */
148 HText_appendCharacter(me->text, ' ');
149 #endif
150 #if CH_SHY < 127
151 } else if (UCH(c) == CH_SHY) {
152 return;
153 #endif
154 } else if ((UCH(c) >= ' ' && TOASCII(UCH(c)) < 127) ||
155 c == '\n' || c == '\t') {
156 HText_appendCharacter(me->text, c);
157 }
158 #endif /* REMOVE_CR_ONLY */
159 }
160
161 /* String handling
162 * ---------------
163 *
164 */
165 static void HTPlain_put_string(HTStream *me, const char *s)
166 {
167 #ifdef REMOVE_CR_ONLY
168 HText_appendText(me->text, s);
169 #else
170 const char *p;
171
172 if (s == NULL)
173 return;
174 for (p = s; *p; p++) {
175 HTPlain_put_character(me, *p);
176 }
177 #endif /* REMOVE_CR_ONLY */
178 }
179
180 /*
181 * Entry function for displayed text/plain and WWW_SOURCE strings. - FM
182 * ---------------------------------------------------------------
183 */
184 static void HTPlain_write(HTStream *me, const char *s, int l)
185 {
186 const char *p;
187 const char *e = s + l;
188 int c;
189 unsigned c_unsign;
190 BOOL chk;
191 UCode_t code, uck = ucError;
192 int saved_char_in = '\0';
193
194 for (p = s; p < e; p++) {
195 #ifdef REMOVE_CR_ONLY
196 /*
197 * Append the whole string, but remove any \r's. - FM
198 */
199 if (*p != '\r') {
200 HText_appendCharacter(me->text, *p);
201 }
202 #else
203 if (*p == '\b') {
204 if (HTPlain_lastraw >= UCH(' ') &&
205 HTPlain_lastraw != '\r' && HTPlain_lastraw != '\n') {
206 if (!HTPlain_bs_pending) {
207 HTPlain_bs_pending = 1;
208 continue;
209 } else if (HTPlain_bs_pending == 2) {
210 HTPlain_bs_pending = 3;
211 continue;
212 }
213 }
214 if (HTPlain_bs_pending >= 2)
215 HText_appendCharacter(me->text, '_');
216 HTPlain_bs_pending = 0;
217 } else if (*p == '_') {
218 if (!HTPlain_bs_pending) {
219 HTPlain_bs_pending = 2;
220 HTPlain_lastraw = UCH(*p);
221 continue;
222 }
223 }
224
225 /*
226 * Try to handle lone LFs, CRLFs and lone CRs as newline, and to deal
227 * with control, ASCII, and 8-bit characters based on best guesses of
228 * what's appropriate. - FM
229 */
230 if ((HTPlain_lastraw == '\r') && *p == '\n') {
231 HTPlain_lastraw = -1;
232 continue;
233 }
234
235 if (HTPlain_bs_pending &&
236 !(UCH(*p) >= ' ' && *p != '\r' && *p != '\n' &&
237 (HTPlain_lastraw == UCH(*p) ||
238 HTPlain_lastraw == UCH('_') ||
239 *p == '_'))) {
240 if (HTPlain_bs_pending >= 2)
241 HText_appendCharacter(me->text, '_');
242 HTPlain_bs_pending = 0;
243 } else if (HTPlain_bs_pending == 1) {
244 HTPlain_bs_pending = 0;
245 continue; /* ignore last two of "X\bX" or "X\b_" - kw */
246 } else if (HTPlain_bs_pending == 3) {
247 if (*p == '_') {
248 HTPlain_bs_pending = 2;
249 continue; /* ignore last two of "_\b_" - kw */
250 } else {
251 HTPlain_bs_pending = 0;
252 /* ignore first two of "_\bX" - kw */
253 }
254 } else if (HTPlain_bs_pending == 2) {
255 HText_appendCharacter(me->text, '_');
256 if (*p == '_')
257 continue; /* keep second of "__" pending - kw */
258 HTPlain_bs_pending = 0;
259 } else {
260 HTPlain_bs_pending = 0;
261 }
262 HTPlain_lastraw = UCH(*p);
263 if (*p == '\r') {
264 HText_appendCharacter(me->text, '\n');
265 continue;
266 }
267 /*
268 * Make sure the character is handled as Unicode whenever that's
269 * appropriate. - FM
270 */
271 c = *p;
272 c_unsign = UCH(c);
273 code = (UCode_t) c_unsign;
274 saved_char_in = '\0';
275 /*
276 * Combine any UTF-8 multibytes into Unicode to check for special
277 * characters. - FM, TD
278 */
279 if (me->T.decode_utf8) {
280 switch (HTDecodeUTF8(&(me->U), &c, &code)) {
281 case dUTF8_ok:
282 if (code < 256) {
283 c = FROMASCII((char) code);
284 c_unsign = UCH(c);
285 }
286 break;
287 case dUTF8_err:
288 code = UCS_REPL;
289 strcpy(me->U.utf_buf, "\357\277\275");
290 me->U.utf_buf_p = (me->U.utf_buf + 3);
291 break;
292 case dUTF8_more:
293 continue;
294 }
295 }
296 /*
297 * Convert characters from non-UTF-8 charsets to Unicode (if
298 * appropriate). - FM
299 */
300 if (!(me->T.decode_utf8 &&
301 UCH(*p) > 127)) {
302 if (me->T.trans_to_uni &&
303 (TOASCII(code) >= LYlowest_eightbit[me->inUCLYhndl] || /* S/390 -- gil -- 0389 */
304 (code < ' ' && code != 0 &&
305 me->T.trans_C0_to_uni))) {
306 /*
307 * Convert the octet to Unicode. - FM
308 */
309 code = (UCode_t) UCTransToUni(c, me->inUCLYhndl);
310 if (code > 0) {
311 saved_char_in = c;
312 if (code < 256) {
313 c = FROMASCII((char) code);
314 c_unsign = UCH(c);
315 }
316 }
317 } else if (code < 32 && code != 0 &&
318 me->T.trans_C0_to_uni) {
319 /*
320 * Quote from SGML.c:
321 * "This else if may be too ugly to keep. - KW"
322 */
323 if (me->T.trans_from_uni &&
324 (((code = UCTransToUni(c, me->inUCLYhndl)) >= 32) ||
325 (me->T.transp &&
326 (code = UCTransToUni(c, me->inUCLYhndl)) > 0))) {
327 saved_char_in = c;
328 if (code < 256) {
329 c = FROMASCII((char) code);
330 c_unsign = UCH(c);
331 }
332 } else {
333 uck = ucError;
334 if (me->T.transp) {
335 uck = UCTransCharStr(replace_buf, 60, c,
336 me->inUCLYhndl,
337 me->inUCLYhndl, NO);
338 }
339 if (!me->T.transp || uck < 0) {
340 uck = UCTransCharStr(replace_buf, 60, c,
341 me->inUCLYhndl,
342 me->outUCLYhndl, YES);
343 }
344 if (uck == 0) {
345 continue;
346 } else if (uck < 0) {
347 me->U.utf_buf[0] = '\0';
348 } else {
349 c = replace_buf[0];
350 if (c && replace_buf[1]) {
351 HText_appendText(me->text, replace_buf);
352 continue;
353 }
354 }
355 me->U.utf_buf[0] = '\0';
356 code = UCH(c);
357 } /* Next line end of ugly stuff for C0. - KW */
358 } else {
359 me->U.utf_buf[0] = '\0';
360 code = UCH(c);
361 }
362 }
363 /*
364 * At this point we have either code in Unicode (and c in latin1 if
365 * code is in the latin1 range), or code and c will have to be passed
366 * raw.
367 */
368
369 /*
370 * If CJK mode is on, we'll assume the document matches the user's
371 * display character set, and if not, the user should toggle off
372 * raw/CJK mode to reload. - FM
373 */
374 if (IS_CJK_TTY) {
375 HText_appendCharacter(me->text, c);
376
377 #define PASSHICTRL (me->T.transp || \
378 code >= LYlowest_eightbit[me->inUCLYhndl])
379 #define PASS8859SPECL me->T.pass_160_173_raw
380 #define PASSHI8BIT (HTPassEightBitRaw || \
381 (me->T.do_8bitraw && !me->T.trans_from_uni))
382 /*
383 * If HTPassHighCtrlRaw is set (e.g., for KOI8-R) assume the
384 * document matches and pass 127-160 8-bit characters. If it
385 * doesn't match, the user should toggle raw/CJK mode off. - FM
386 */
387 } else if (TOASCII(code) >= 127 && TOASCII(code) < 161 && /* S/390 -- gil -- 0427 */
388 PASSHICTRL && PASS8859SPECL) {
389 HText_appendCharacter(me->text, c);
390 } else if (code == CH_SHY && PASS8859SPECL) {
391 HText_appendCharacter(me->text, c);
392 /*
393 * If neither HTPassHighCtrlRaw nor CJK is set, play it safe and
394 * treat 160 (nbsp) as an ASCII space (32). - FM
395 */
396 } else if (code == CH_NBSP) {
397 HText_appendCharacter(me->text, ' ');
398 /*
399 * If neither HTPassHighCtrlRaw nor CJK is set, play it safe and
400 * ignore 173 (shy). - FM
401 * Now only ignore it for color style, which doesn't handle it
402 * anyway. Otherwise pass it on as LY_SOFT_HYPHEN and let HText
403 * deal with it. It should be either ignored, or displayed as a
404 * hyphen if it was indeed at the end of a line. Well it should.
405 * - kw
406 */
407 } else if (code == CH_SHY) {
408 #ifndef USE_COLOR_STYLE
409 HText_appendCharacter(me->text, LY_SOFT_HYPHEN);
410 #endif
411 continue;
412 /*
413 * If we get to here, pass the displayable ASCII characters. - FM
414 */
415 } else if ((code >= ' ' && code != UCS_REPL && TOASCII(code) < 127) ||
416 (PASSHI8BIT &&
417 c >= LYlowest_eightbit[me->outUCLYhndl]) ||
418 *p == '\n' || *p == '\t') {
419 HText_appendCharacter(me->text, c);
420 /*
421 * Use an ASCII space (32) for ensp, emsp or thinsp. - FM
422 */
423 } else if (code == 8194 || code == 8195 || code == 8201) {
424 HText_appendCharacter(me->text, ' ');
425 /*
426 * If we want the raw character, pass it now. - FM
427 */
428 } else if (me->T.use_raw_char_in && saved_char_in) {
429 HText_appendCharacter(me->text, saved_char_in);
430 /******************************************************************
431 * I. LATIN-1 OR UCS2 TO DISPLAY CHARSET
432 ******************************************************************/
433 } else if ((chk = (BOOL) (me->T.trans_from_uni && code >= 160)) &&
434 (uck = UCTransUniChar(code,
435 me->outUCLYhndl)) >= ' ' && /* S/390 -- gil -- 0464 */
436 uck < 256) {
437 CTRACE((tfp, "UCTransUniChar returned 0x%.2" PRI_UCode_t
438 ":'%c'.\n",
439 uck, FROMASCII(UCH(uck))));
440 HText_appendCharacter(me->text, ((char) (uck & 0xff)));
441 } else if (chk && (uck == ucZeroWidth)) {
442 ; /* EMPTY */
443 } else if (chk &&
444 (uck == ucNotFound ||
445 (me->T.repl_translated_C0 && uck > 0 && uck < ' ')) && /* S/390 -- gil -- 0481 */
446 /*
447 * Not found; look for replacement string.
448 */
449 (uck = UCTransUniCharStr(replace_buf, 60, code,
450 me->outUCLYhndl, 0) >= 0)) {
451 /*
452 * No further tests for validity - assume that whoever defined
453 * replacement strings knew what she was doing.
454 */
455 HText_appendText(me->text, replace_buf);
456 /*
457 * If we get to here, and should have translated, translation has
458 * failed so far.
459 */
460 } else if (chk && TOASCII(code) > 127 && me->T.output_utf8) { /* S/390 -- gil -- 0498 */
461 /*
462 * We want UTF-8 output, so do it now. - FM
463 */
464 if (*me->U.utf_buf) {
465 HText_appendText(me->text, me->U.utf_buf);
466 me->U.utf_buf[0] = '\0';
467 me->U.utf_buf_p = me->U.utf_buf;
468 } else if (UCConvertUniToUtf8(code, replace_buf)) {
469 HText_appendText(me->text, replace_buf);
470 } else {
471 /*
472 * Out of luck, so use the UHHH notation (ugh). - gil
473 */
474 /* S/390 -- gil -- 0517 */
475 sprintf(replace_buf, "U%.2lX", (unsigned long) TOASCII(code));
476 HText_appendText(me->text, replace_buf);
477 }
478 /*
479 * If we don't actually want the character, make it safe and output
480 * that now. - FM
481 */
482 } else if ((c_unsign > 0 &&
483 (int) c_unsign < LYlowest_eightbit[me->outUCLYhndl]) ||
484 (me->T.trans_from_uni && !HTPassEightBitRaw)) {
485 /*
486 * If we do not have the "7-bit approximations" as our output
487 * character set (in which case we did it already) seek a
488 * translation for that. Otherwise, or if the translation fails,
489 * use UHHH notation. - FM
490 */
491 if ((chk = (BOOL) (me->outUCLYhndl !=
492 UCGetLYhndl_byMIME("us-ascii"))) &&
493 (uck = UCTransUniChar(code,
494 UCGetLYhndl_byMIME("us-ascii")))
495 >= ' ' && TOASCII(uck) < 127) { /* S/390 -- gil -- 0535 */
496 /*
497 * Got an ASCII character (yippey). - FM
498 */
499 c = FROMASCII((char) uck);
500 HText_appendCharacter(me->text, c);
501 } else if ((chk && uck == ucNotFound) &&
502 (uck = UCTransUniCharStr(replace_buf,
503 60, code,
504 UCGetLYhndl_byMIME("us-ascii"),
505 0) >= 0)) {
506 /*
507 * Got a replacement string (yippey). - FM
508 */
509 HText_appendText(me->text, replace_buf);
510 } else if (is_ucs_zero_width(code)) {
511 /*
512 * Ignore zero-with and left/right marks
513 */
514 CTRACE((tfp, "HTPlain_write: Ignoring '%" PRI_UCode_t "'.\n", code));
515 } else {
516 /*
517 * Out of luck, so use the UHHH notation (ugh). - FM
518 */
519 /* do not print UHHH for now
520 sprintf(replace_buf, "U%.2lX", code);
521 HText_appendText(me->text, replace_buf);
522 */
523 }
524 /*
525 * If we get to here and have a monobyte character, pass it. - FM
526 */
527 } else if (c_unsign != 0 && c_unsign < 256) {
528 HText_appendCharacter(me->text, c);
529 }
530 #endif /* REMOVE_CR_ONLY */
531 }
532 }
533
534 /* Free an HTML object
535 * -------------------
536 *
537 * Note that the SGML parsing context is freed, but the created object is
538 * not, as it takes on an existence of its own unless explicitly freed.
539 */
540 static void HTPlain_free(HTStream *me)
541 {
542 if (HTPlain_bs_pending >= 2)
543 HText_appendCharacter(me->text, '_');
544 FREE(me);
545 }
546
547 /* End writing
548 */
549 static void HTPlain_abort(HTStream *me, HTError e GCC_UNUSED)
550 {
551 HTPlain_free(me);
552 }
553
554 /* Structured Object Class
555 * -----------------------
556 */
557 static const HTStreamClass HTPlain =
558 {
559 "PlainPresenter",
560 HTPlain_free,
561 HTPlain_abort,
562 HTPlain_put_character, HTPlain_put_string, HTPlain_write,
563 };
564
565 /* New object
566 * ----------
567 */
568 HTStream *HTPlainPresent(HTPresentation *pres GCC_UNUSED, HTParentAnchor *anchor,
569 HTStream *sink GCC_UNUSED)
570 {
571
572 HTStream *me = (HTStream *) malloc(sizeof(*me));
573
574 if (me == NULL)
575 outofmem(__FILE__, "HTPlain_new");
576
577 me->isa = &HTPlain;
578
579 HTPlain_lastraw = -1;
580
581 me->U.utf_count = 0;
582 me->U.utf_char = 0;
583 me->U.utf_buf[0] = me->U.utf_buf[6] = me->U.utf_buf[7] = '\0';
584 me->U.utf_buf_p = me->U.utf_buf;
585 me->outUCLYhndl = HTAnchor_getUCLYhndl(anchor, UCT_STAGE_HTEXT);
586 me->inUCLYhndl = HTAnchor_getUCLYhndl(anchor, UCT_STAGE_PARSER);
587 HTPlain_getChartransInfo(me, anchor);
588 UCSetTransParams(&me->T,
589 me->inUCLYhndl, me->inUCI,
590 me->outUCLYhndl,
591 HTAnchor_getUCInfoStage(anchor, UCT_STAGE_HTEXT));
592
593 me->text = HText_new(anchor);
594 HText_setStyle(me->text, LYstyles(HTML_XMP));
595 HText_beginAppend(me->text);
596
597 return (HTStream *) me;
598 }