Graphviz 16.1.3~dev.20261121.1645
Loading...
Searching...
No Matches
htmllex.c
Go to the documentation of this file.
1
3/*************************************************************************
4 * Copyright (c) 2011 AT&T Intellectual Property
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
9 *
10 * Contributors: Details at https://graphviz.org
11 *************************************************************************/
12
13#include "config.h"
14
15#include "htmlparse.h"
16#include <assert.h>
17#include <cdt/cdt.h>
18#include <common/htmllex.h>
19#include <common/htmltable.h>
20#include <common/render.h>
21#include <inttypes.h>
22#include <limits.h>
23#include <stdatomic.h>
24#include <stdbool.h>
25#include <stddef.h>
26#include <stdint.h>
27#include <util/alloc.h>
28#include <util/gv_ctype.h>
29#include <util/startswith.h>
30#include <util/strcasecmp.h>
31#include <util/strview.h>
32#include <util/tokenize.h>
33#include <util/unused.h>
34
35#ifdef HAVE_EXPAT
36#include <expat.h>
37#endif
38
39static uint64_t htmllineno(htmllexstate_t *ctx);
40
41/* error_context:
42 * Print the last 2 "token"s seen.
43 */
44static void error_context(htmllexstate_t *ctx) {
45 agerr(AGPREV, "... %.*s%.*s ...\n", (int)ctx->prevtok.size, ctx->prevtok.data,
46 (int)ctx->currtok.size, ctx->currtok.data);
47}
48
49/* htmlerror:
50 * yyerror - called by yacc output
51 */
52void htmlerror(htmlscan_t *scanner, const char *msg) {
53 htmllexstate_t *ctx = &scanner->lexer;
54 if (ctx->error)
55 return;
56 ctx->error = 1;
57 agerrorf("%s in line %" PRIu64 " \n", msg, htmllineno(ctx));
58 error_context(&scanner->lexer);
59}
60
61#ifdef HAVE_EXPAT
62/* lexerror:
63 * called by lexer when unknown <..> is found.
64 */
65static void lexerror(htmllexstate_t *ctx, const char *name) {
66 ctx->tok = T_error;
67 ctx->error = 1;
68 agerrorf("Unknown HTML element <%s> on line %" PRIu64 " \n", name,
69 htmllineno(ctx));
70}
71
73static int icmp(const void *name, const void *item) {
74 const char *const *const j = item;
75 return strcasecmp(name, *j);
76}
77
78static int bgcolorfn(htmldata_t *p, const char *v) {
79 p->bgcolor = strdup(v);
80 return 0;
81}
82
83static int pencolorfn(htmldata_t *p, const char *v) {
84 p->pencolor = strdup(v);
85 return 0;
86}
87
88static int hreffn(htmldata_t *p, const char *v) {
89 p->href = strdup(v);
90 return 0;
91}
92
93static int sidesfn(htmldata_t *p, const char *v) {
94 unsigned short flags = 0;
95 char c;
96
97 while ((c = *v++)) {
98 switch (gv_tolower(c)) {
99 case 'l':
101 break;
102 case 't':
103 flags |= BORDER_TOP;
104 break;
105 case 'r':
107 break;
108 case 'b':
110 break;
111 default:
112 agwarningf("Unrecognized character '%c' (%d) in sides attribute\n", c, c);
113 break;
114 }
115 }
116 if (flags != BORDER_MASK)
117 p->flags |= flags;
118 return 0;
119}
120
121static int titlefn(htmldata_t *p, const char *v) {
122 p->title = strdup(v);
123 return 0;
124}
125
126static int portfn(htmldata_t *p, const char *v) {
127 p->port = strdup(v);
128 return 0;
129}
130
131#define DELIM " ,"
132
133static int stylefn(htmldata_t *p, const char *v) {
134 int rv = 0;
135 for (tok_t t = tok(v, DELIM); !tok_end(&t); tok_next(&t)) {
136 strview_t tk = tok_get(&t);
137 if (strview_case_str_eq(tk, "ROUNDED"))
138 p->style.rounded = true;
139 else if (strview_case_str_eq(tk, "RADIAL"))
140 p->style.radial = true;
141 else if (strview_case_str_eq(tk, "SOLID")) {
142 p->style.dotted = false;
143 p->style.dashed = false;
144 } else if (strview_case_str_eq(tk, "INVISIBLE") ||
145 strview_case_str_eq(tk, "INVIS"))
146 p->style.invisible = true;
147 else if (strview_case_str_eq(tk, "DOTTED"))
148 p->style.dotted = true;
149 else if (strview_case_str_eq(tk, "DASHED"))
150 p->style.dashed = true;
151 else {
152 agwarningf("Illegal value %.*s for STYLE - ignored\n", (int)tk.size,
153 tk.data);
154 rv = 1;
155 }
156 }
157 return rv;
158}
159
160static int targetfn(htmldata_t *p, const char *v) {
161 p->target = strdup(v);
162 return 0;
163}
164
165static int idfn(htmldata_t *p, const char *v) {
166 p->id = strdup(v);
167 return 0;
168}
169
170/* doInt:
171 * Scan v for integral value. Check that
172 * the value is >= min and <= max. Return value in ul.
173 * String s is name of value.
174 * Return 0 if okay; 1 otherwise.
175 */
176static int doInt(const char *v, char *s, int min, int max, long *ul) {
177 int rv = 0;
178 char *ep;
179 long b = strtol(v, &ep, 10);
180
181 if (ep == v) {
182 agwarningf("Improper %s value %s - ignored", s, v);
183 rv = 1;
184 } else if (b > max) {
185 agwarningf("%s value %s > %d - too large - ignored", s, v, max);
186 rv = 1;
187 } else if (b < min) {
188 agwarningf("%s value %s < %d - too small - ignored", s, v, min);
189 rv = 1;
190 } else
191 *ul = b;
192 return rv;
193}
194
195static int gradientanglefn(htmldata_t *p, const char *v) {
196 long u;
197
198 if (doInt(v, "GRADIENTANGLE", 0, 360, &u))
199 return 1;
200 p->gradientangle = (unsigned short)u;
201 return 0;
202}
203
204static int borderfn(htmldata_t *p, const char *v) {
205 long u;
206
207 if (doInt(v, "BORDER", 0, UCHAR_MAX, &u))
208 return 1;
209 p->border = (unsigned char)u;
210 p->flags |= BORDER_SET;
211 return 0;
212}
213
214static int cellpaddingfn(htmldata_t *p, const char *v) {
215 long u;
216
217 if (doInt(v, "CELLPADDING", 0, UCHAR_MAX, &u))
218 return 1;
219 p->pad = (unsigned char)u;
220 p->flags |= PAD_SET;
221 return 0;
222}
223
224static int cellspacingfn(htmldata_t *p, const char *v) {
225 long u;
226
227 if (doInt(v, "CELLSPACING", SCHAR_MIN, SCHAR_MAX, &u))
228 return 1;
229 p->space = (signed char)u;
230 p->flags |= SPACE_SET;
231 return 0;
232}
233
234static int cellborderfn(htmldata_t *data, const char *v) {
235 long u;
236 htmltbl_t *const p =
237 (htmltbl_t *)((uintptr_t)data - offsetof(htmltbl_t, data));
238
239 if (doInt(v, "CELLBORDER", 0, INT8_MAX, &u))
240 return 1;
241 p->cellborder = (int8_t)u;
242 return 0;
243}
244
245static int columnsfn(htmldata_t *data, const char *v) {
246 htmltbl_t *const p =
247 (htmltbl_t *)((uintptr_t)data - offsetof(htmltbl_t, data));
248 if (*v != '*') {
249 agwarningf("Unknown value %s for COLUMNS - ignored\n", v);
250 return 1;
251 }
252 p->vrule = true;
253 return 0;
254}
255
256static int rowsfn(htmldata_t *data, const char *v) {
257 htmltbl_t *const p =
258 (htmltbl_t *)((uintptr_t)data - offsetof(htmltbl_t, data));
259 if (*v != '*') {
260 agwarningf("Unknown value %s for ROWS - ignored\n", v);
261 return 1;
262 }
263 p->hrule = true;
264 return 0;
265}
266
267static int fixedsizefn(htmldata_t *p, const char *v) {
268 int rv = 0;
269 if (!strcasecmp(v, "TRUE"))
270 p->flags |= FIXED_FLAG;
271 else if (strcasecmp(v, "FALSE")) {
272 agwarningf("Illegal value %s for FIXEDSIZE - ignored\n", v);
273 rv = 1;
274 }
275 return rv;
276}
277
278static int valignfn(htmldata_t *p, const char *v) {
279 int rv = 0;
280 if (!strcasecmp(v, "BOTTOM"))
281 p->flags |= VALIGN_BOTTOM;
282 else if (!strcasecmp(v, "TOP"))
283 p->flags |= VALIGN_TOP;
284 else if (strcasecmp(v, "MIDDLE")) {
285 agwarningf("Illegal value %s for VALIGN - ignored\n", v);
286 rv = 1;
287 }
288 return rv;
289}
290
291static int halignfn(htmldata_t *p, const char *v) {
292 int rv = 0;
293 if (!strcasecmp(v, "LEFT"))
294 p->flags |= HALIGN_LEFT;
295 else if (!strcasecmp(v, "RIGHT"))
296 p->flags |= HALIGN_RIGHT;
297 else if (strcasecmp(v, "CENTER")) {
298 agwarningf("Illegal value %s for ALIGN - ignored\n", v);
299 rv = 1;
300 }
301 return rv;
302}
303
304static int cell_halignfn(htmldata_t *p, const char *v) {
305 int rv = 0;
306 if (!strcasecmp(v, "LEFT"))
307 p->flags |= HALIGN_LEFT;
308 else if (!strcasecmp(v, "RIGHT"))
309 p->flags |= HALIGN_RIGHT;
310 else if (!strcasecmp(v, "TEXT"))
311 p->flags |= HALIGN_TEXT;
312 else if (strcasecmp(v, "CENTER"))
313 rv = 1;
314 if (rv)
315 agwarningf("Illegal value %s for ALIGN in TD - ignored\n", v);
316 return rv;
317}
318
319static int balignfn(htmldata_t *p, const char *v) {
320 int rv = 0;
321 if (!strcasecmp(v, "LEFT"))
322 p->flags |= BALIGN_LEFT;
323 else if (!strcasecmp(v, "RIGHT"))
324 p->flags |= BALIGN_RIGHT;
325 else if (strcasecmp(v, "CENTER"))
326 rv = 1;
327 if (rv)
328 agwarningf("Illegal value %s for BALIGN in TD - ignored\n", v);
329 return rv;
330}
331
332static int heightfn(htmldata_t *p, const char *v) {
333 long u;
334
335 if (doInt(v, "HEIGHT", 0, USHRT_MAX, &u))
336 return 1;
337 p->height = (unsigned short)u;
338 return 0;
339}
340
341static int widthfn(htmldata_t *p, const char *v) {
342 long u;
343
344 if (doInt(v, "WIDTH", 0, USHRT_MAX, &u))
345 return 1;
346 p->width = (unsigned short)u;
347 return 0;
348}
349
350static int rowspanfn(htmldata_t *data, const char *v) {
351 long u;
352 htmlcell_t *const p =
353 (htmlcell_t *)((uintptr_t)data - offsetof(htmlcell_t, data));
354
355 if (doInt(v, "ROWSPAN", 0, UINT16_MAX, &u))
356 return 1;
357 if (u == 0) {
358 agwarningf("ROWSPAN value cannot be 0 - ignored\n");
359 return 1;
360 }
361 p->rowspan = (uint16_t)u;
362 return 0;
363}
364
365static int colspanfn(htmldata_t *data, const char *v) {
366 long u;
367 htmlcell_t *const p =
368 (htmlcell_t *)((uintptr_t)data - offsetof(htmlcell_t, data));
369
370 if (doInt(v, "COLSPAN", 0, UINT16_MAX, &u))
371 return 1;
372 if (u == 0) {
373 agwarningf("COLSPAN value cannot be 0 - ignored\n");
374 return 1;
375 }
376 p->colspan = (uint16_t)u;
377 return 0;
378}
379
380static int fontcolorfn(textfont_t *p, const char *v) {
381 // no strdup because HTML font usage is contained
382 p->color = (char *)v;
383 return 0;
384}
385
386static int facefn(textfont_t *p, const char *v) {
387 // no strdup because HTML font usage is contained
388 p->name = (char *)v;
389 return 0;
390}
391
392static int ptsizefn(textfont_t *p, const char *v) {
393 long u;
394
395 if (doInt(v, "POINT-SIZE", 0, UCHAR_MAX, &u))
396 return 1;
397 p->size = (double)u;
398 return 0;
399}
400
401static int srcfn(htmlimg_t *p, const char *v) {
402 p->src = strdup(v);
403 return 0;
404}
405
406static int scalefn(htmlimg_t *p, const char *v) {
407 p->scale = strdup(v);
408 return 0;
409}
410
411static int alignfn(int *p, const char *v) {
412 int rv = 0;
413 if (!strcasecmp(v, "RIGHT"))
414 *p = 'r';
415 else if (!strcasecmp(v, "LEFT"))
416 *p = 'l';
417 else if (!strcasecmp(v, "CENTER"))
418 *p = 'n';
419 else {
420 agwarningf("Illegal value %s for ALIGN - ignored\n", v);
421 rv = 1;
422 }
423 return rv;
424}
425
426typedef struct {
427 char *name;
428 int (*action)(htmldata_t *,
429 const char *);
430} html_item_t;
431
432/* Tables used in binary search; MUST be alphabetized */
433static const html_item_t tbl_items[] = {
434 {"align", halignfn},
435 {"bgcolor", bgcolorfn},
436 {"border", borderfn},
437 {"cellborder", cellborderfn},
438 {"cellpadding", cellpaddingfn},
439 {"cellspacing", cellspacingfn},
440 {"color", pencolorfn},
441 {"columns", columnsfn},
442 {"fixedsize", fixedsizefn},
443 {"gradientangle", gradientanglefn},
444 {"height", heightfn},
445 {"href", hreffn},
446 {"id", idfn},
447 {"port", portfn},
448 {"rows", rowsfn},
449 {"sides", sidesfn},
450 {"style", stylefn},
451 {"target", targetfn},
452 {"title", titlefn},
453 {"tooltip", titlefn},
454 {"valign", valignfn},
455 {"width", widthfn},
456};
457
458static const html_item_t cell_items[] = {
459 {"align", cell_halignfn},
460 {"balign", balignfn},
461 {"bgcolor", bgcolorfn},
462 {"border", borderfn},
463 {"cellpadding", cellpaddingfn},
464 {"cellspacing", cellspacingfn},
465 {"color", pencolorfn},
466 {"colspan", colspanfn},
467 {"fixedsize", fixedsizefn},
468 {"gradientangle", gradientanglefn},
469 {"height", heightfn},
470 {"href", hreffn},
471 {"id", idfn},
472 {"port", portfn},
473 {"rowspan", rowspanfn},
474 {"sides", sidesfn},
475 {"style", stylefn},
476 {"target", targetfn},
477 {"title", titlefn},
478 {"tooltip", titlefn},
479 {"valign", valignfn},
480 {"width", widthfn},
481};
482
483typedef struct {
484 char *name;
485 int (*action)(textfont_t *,
486 const char *);
487} font_item_t;
488
489static const font_item_t font_items[] = {
490 {"color", fontcolorfn},
491 {"face", facefn},
492 {"point-size", ptsizefn},
493};
494
495typedef struct {
496 char *name;
497 int (*action)(htmlimg_t *,
498 const char *);
499} img_item_t;
500
501static const img_item_t img_items[] = {
502 {"scale", scalefn},
503 {"src", srcfn},
504};
505
506typedef struct {
507 char *name;
508 int (*action)(int *, const char *);
509} br_item_t;
510
511static const br_item_t br_items[] = {
512 {"align", alignfn},
513};
514
519#define CALL_ACTION(list, elem, tp, val) \
520 (_Generic((list), \
521 const html_item_t *: (const html_item_t *)(elem), \
522 const font_item_t *: (const font_item_t *)(elem), \
523 const img_item_t *: (const img_item_t *)(elem), \
524 const br_item_t *: (const br_item_t *)(elem)) \
525 ->action((tp), (val)))
526
527/* doAttrs:
528 * General function for processing list of name/value attributes.
529 * Do binary search on items table. If match found, invoke action
530 * passing it tp and attribute value.
531 * Table size is given by nel
532 * Name/value pairs are in array atts, which is null terminated.
533 * s is the name of the HTML element being processed.
534 */
535#define doAttrs(ctx, tp, items, nel, atts, s) \
536 do { \
537 const char *name; \
538 \
539 while ((name = *(atts)++) != NULL) { \
540 const char *val = *(atts)++; \
541 void *const ip = \
542 bsearch(name, (items), (nel), sizeof((items)[0]), icmp); \
543 if (ip) \
544 (ctx)->warn |= CALL_ACTION((items), ip, (tp), val); \
545 else { \
546 agwarningf("Illegal attribute %s in %s - ignored\n", name, (s)); \
547 (ctx)->warn = 1; \
548 } \
549 } \
550 } while (0)
551
552static void mkBR(htmllexstate_t *ctx, const char **atts) {
553 ctx->htmllval->i = UNSET_ALIGN;
554 doAttrs(ctx, &ctx->htmllval->i, br_items,
555 sizeof(br_items) / sizeof(br_items[0]), atts, "<BR>");
556}
557
558static htmlimg_t *mkImg(htmllexstate_t *ctx, const char **atts) {
559 htmlimg_t *img = gv_alloc(sizeof(htmlimg_t));
560
561 doAttrs(ctx, img, img_items, sizeof(img_items) / sizeof(img_items[0]), atts,
562 "<IMG>");
563
564 return img;
565}
566
567static textfont_t *mkFont(htmllexstate_t *ctx, const char **atts,
568 unsigned char flags) {
569 textfont_t tf = {NULL, NULL, NULL, 0.0, 0, 0};
570
571 tf.size = -1.0; /* unassigned */
572 enum { FLAGS_MAX = (1 << GV_TEXTFONT_FLAGS_WIDTH) - 1 };
573 assert(flags <= FLAGS_MAX);
574 tf.flags = (unsigned char)(flags & FLAGS_MAX);
575 if (atts)
576 doAttrs(ctx, &tf, font_items, sizeof(font_items) / sizeof(font_items[0]),
577 atts, "<FONT>");
578
579 return dtinsert(ctx->gvc->textfont_dt, &tf);
580}
581
582static htmlcell_t *mkCell(htmllexstate_t *ctx, const char **atts) {
584
585 cell->colspan = 1;
586 cell->rowspan = 1;
587 doAttrs(ctx, &cell->data, cell_items,
588 sizeof(cell_items) / sizeof(cell_items[0]), atts, "<TD>");
589
590 return cell;
591}
592
593static htmltbl_t *mkTbl(htmllexstate_t *ctx, const char **atts) {
594 htmltbl_t *tbl = gv_alloc(sizeof(htmltbl_t));
595
596 tbl->row_count = SIZE_MAX; // flag that table is a raw, parsed table
597 tbl->rows = (rows_t){.dtor = free_ritem};
598 tbl->cellborder = -1; // unset cell border attribute
599 doAttrs(ctx, &tbl->data, tbl_items, sizeof(tbl_items) / sizeof(tbl_items[0]),
600 atts, "<TABLE>");
601
602 return tbl;
603}
604
605static void startElement(void *user, const char *name, const char **atts) {
606 htmllexstate_t *ctx = user;
607
608 if (strcasecmp(name, "TABLE") == 0) {
609 ctx->htmllval->tbl = mkTbl(ctx, atts);
610 ctx->inCell = 0;
611 ctx->tok = T_table;
612 } else if (strcasecmp(name, "TR") == 0 || strcasecmp(name, "TH") == 0) {
613 ctx->inCell = 0;
614 ctx->tok = T_row;
615 } else if (strcasecmp(name, "TD") == 0) {
616 ctx->inCell = 1;
617 ctx->htmllval->cell = mkCell(ctx, atts);
618 ctx->tok = T_cell;
619 } else if (strcasecmp(name, "FONT") == 0) {
620 ctx->htmllval->font = mkFont(ctx, atts, 0);
621 ctx->tok = T_font;
622 } else if (strcasecmp(name, "B") == 0) {
623 ctx->htmllval->font = mkFont(ctx, 0, HTML_BF);
624 ctx->tok = T_bold;
625 } else if (strcasecmp(name, "S") == 0) {
626 ctx->htmllval->font = mkFont(ctx, 0, HTML_S);
627 ctx->tok = T_s;
628 } else if (strcasecmp(name, "U") == 0) {
629 ctx->htmllval->font = mkFont(ctx, 0, HTML_UL);
630 ctx->tok = T_underline;
631 } else if (strcasecmp(name, "O") == 0) {
632 ctx->htmllval->font = mkFont(ctx, 0, HTML_OL);
633 ctx->tok = T_overline;
634 } else if (strcasecmp(name, "I") == 0) {
635 ctx->htmllval->font = mkFont(ctx, 0, HTML_IF);
636 ctx->tok = T_italic;
637 } else if (strcasecmp(name, "SUP") == 0) {
638 ctx->htmllval->font = mkFont(ctx, 0, HTML_SUP);
639 ctx->tok = T_sup;
640 } else if (strcasecmp(name, "SUB") == 0) {
641 ctx->htmllval->font = mkFont(ctx, 0, HTML_SUB);
642 ctx->tok = T_sub;
643 } else if (strcasecmp(name, "BR") == 0) {
644 mkBR(ctx, atts);
645 ctx->tok = T_br;
646 } else if (strcasecmp(name, "HR") == 0) {
647 ctx->tok = T_hr;
648 } else if (strcasecmp(name, "VR") == 0) {
649 ctx->tok = T_vr;
650 } else if (strcasecmp(name, "IMG") == 0) {
651 ctx->htmllval->img = mkImg(ctx, atts);
652 ctx->tok = T_img;
653 } else if (strcasecmp(name, "HTML") == 0) {
654 ctx->tok = T_html;
655 } else {
656 lexerror(ctx, name);
657 }
658}
659
660static void endElement(void *user, const char *name) {
661 htmllexstate_t *ctx = user;
662
663 if (strcasecmp(name, "TABLE") == 0) {
664 ctx->tok = T_end_table;
665 ctx->inCell = 1;
666 } else if (strcasecmp(name, "TR") == 0 || strcasecmp(name, "TH") == 0) {
667 ctx->tok = T_end_row;
668 } else if (strcasecmp(name, "TD") == 0) {
669 ctx->tok = T_end_cell;
670 ctx->inCell = 0;
671 } else if (strcasecmp(name, "HTML") == 0) {
672 ctx->tok = T_end_html;
673 } else if (strcasecmp(name, "FONT") == 0) {
674 ctx->tok = T_end_font;
675 } else if (strcasecmp(name, "B") == 0) {
676 ctx->tok = T_n_bold;
677 } else if (strcasecmp(name, "U") == 0) {
678 ctx->tok = T_n_underline;
679 } else if (strcasecmp(name, "O") == 0) {
680 ctx->tok = T_n_overline;
681 } else if (strcasecmp(name, "I") == 0) {
682 ctx->tok = T_n_italic;
683 } else if (strcasecmp(name, "SUP") == 0) {
684 ctx->tok = T_n_sup;
685 } else if (strcasecmp(name, "SUB") == 0) {
686 ctx->tok = T_n_sub;
687 } else if (strcasecmp(name, "S") == 0) {
688 ctx->tok = T_n_s;
689 } else if (strcasecmp(name, "BR") == 0) {
690 if (ctx->tok == T_br)
691 ctx->tok = T_BR;
692 else
693 ctx->tok = T_end_br;
694 } else if (strcasecmp(name, "HR") == 0) {
695 if (ctx->tok == T_hr)
696 ctx->tok = T_HR;
697 else
698 ctx->tok = T_end_hr;
699 } else if (strcasecmp(name, "VR") == 0) {
700 if (ctx->tok == T_vr)
701 ctx->tok = T_VR;
702 else
703 ctx->tok = T_end_vr;
704 } else if (strcasecmp(name, "IMG") == 0) {
705 if (ctx->tok == T_img)
706 ctx->tok = T_IMG;
707 else
708 ctx->tok = T_end_img;
709 } else {
710 lexerror(ctx, name);
711 }
712}
713
714/* characterData:
715 * Generate T_string token. Do this only when immediately in
716 * <TD>..</TD> or <HTML>..</HTML>, i.e., when inCell is true.
717 * Strip out formatting characters but keep spaces.
718 * Distinguish between all whitespace vs. strings with non-whitespace
719 * characters.
720 */
721static void characterData(void *user, const char *s, int length) {
722 htmllexstate_t *ctx = user;
723
724 int i, cnt = 0;
725 unsigned char c;
726
727 if (ctx->inCell) {
728 for (i = length; i; i--) {
729 c = *s++;
730 if (c >= ' ') {
731 cnt++;
732 agxbputc(ctx->xb, (char)c);
733 }
734 }
735 if (cnt)
736 ctx->tok = T_string;
737 }
738}
739#endif
740
741int initHTMLlexer(htmlscan_t *scanner, char *src, agxbuf *xb, htmlenv_t *env) {
742#ifdef HAVE_EXPAT
743 htmllexstate_t *ctx = &scanner->lexer;
744
745 ctx->xb = xb;
746 ctx->lb = (agxbuf){0};
747 ctx->ptr = src;
748 ctx->mode = 0;
749 ctx->warn = 0;
750 ctx->error = 0;
751 ctx->currtok = (strview_t){0};
752 ctx->prevtok = (strview_t){0};
753 ctx->inCell = 1;
754 ctx->parser = XML_ParserCreate(charsetToStr(GD_charset(env->g)));
755 ctx->gvc = GD_gvc(env->g);
756 XML_SetUserData(ctx->parser, ctx);
757 XML_SetElementHandler(ctx->parser, startElement, endElement);
758 XML_SetCharacterDataHandler(ctx->parser, characterData);
759 return 0;
760#else
761 (void)scanner;
762 (void)src;
763 (void)xb;
764 (void)env;
765
766 static atomic_flag first;
767 if (!atomic_flag_test_and_set(&first)) {
768 agwarningf("Not built with libexpat. Table formatting is not available.\n");
769 }
770 return 1;
771#endif
772}
773
775#ifdef HAVE_EXPAT
776 htmllexstate_t *ctx = &scanner->lexer;
777 int rv = ctx->error ? 3 : ctx->warn;
778 XML_ParserFree(ctx->parser);
779 agxbfree(&ctx->lb);
780 return rv;
781#else
782 (void)scanner;
783
784 return 1;
785#endif
786}
787
789static UNUSED void agxbput_move(agxbuf *dst, const char *src) {
790 // we cannot call `agxbput` itself because it calls `memcpy`, thereby
791 // implicitly assuming that source and destination do not overlap
792 char *src_copy = gv_strdup(src);
793 agxbput(dst, src_copy);
794 free(src_copy);
795}
796
797#ifdef HAVE_EXPAT
798/* eatComment:
799 * Given first character after open comment, eat characters
800 * up to comment close, returning pointer to closing > if it exists,
801 * or null character otherwise.
802 * We rely on HTML strings having matched nested <>.
803 */
804static char *eatComment(htmllexstate_t *ctx, char *p) {
805 int depth = 1;
806 char *s = p;
807 char c;
808
809 while (depth && (c = *s++)) {
810 if (c == '<')
811 depth++;
812 else if (c == '>')
813 depth--;
814 }
815 s--; /* move back to '\0' or '>' */
816 if (*s) {
817 char *t = s - 2;
818 if (t < p || !startswith(t, "--")) {
819 agwarningf("Unclosed comment\n");
820 ctx->warn = 1;
821 }
822 }
823 return s;
824}
825
826/* findNext:
827 * Return next XML unit. This is either <..>, an HTML
828 * comment <!-- ... -->, or characters up to next <.
829 */
830static char *findNext(htmllexstate_t *ctx, char *s, agxbuf *xb) {
831 char *t = s + 1;
832 char c;
833
834 if (*s == '<') {
835 if (startswith(t, "!--"))
836 t = eatComment(ctx, t + 3);
837 else
838 while (*t && *t != '>')
839 t++;
840 if (*t != '>') {
841 agwarningf("Label closed before end of HTML element\n");
842 ctx->warn = 1;
843 } else
844 t++;
845 } else {
846 t = s;
847 while ((c = *t) && c != '<') {
848 if (c == '&' && *(t + 1) != '#') {
849 t = scanEntity(t + 1, xb);
850 } else {
851 agxbputc(xb, c);
852 t++;
853 }
854 }
855 }
856 return t;
857}
858
875static void protect_rsqb(agxbuf *xb) {
876
877 // if the buffer is empty, we have nothing to do
878 if (agxblen(xb) == 0) {
879 return;
880 }
881
882 // check the last character and if it is not ], we have nothing to do
883 char *data = agxbuse(xb);
884 size_t size = strlen(data);
885 assert(size > 0);
886 if (data[size - 1] != ']') {
887 agxbput_move(xb, data);
888 return;
889 }
890
891 // truncate ] and write back the remaining prefix
892 data[size - 1] = '\0';
893 agxbput_move(xb, data);
894
895 // write an XML-escaped version of ] as a replacement
896 agxbput(xb, "&#93;");
897}
898#endif
899
900#ifdef HAVE_EXPAT
901static uint64_t expat_get_lineno(XML_Parser parser) {
902 // Expat 2.9.0 introduced `XML_GetCurrentLineNumber64` and deprecated
903 // `XML_GetCurrentLineNumber`
904#if XML_MAJOR_VERSION > 2 || (XML_MAJOR_VERSION == 2 && XML_MINOR_VERSION >= 9)
905 return XML_GetCurrentLineNumber64(parser);
906#else
907 return XML_GetCurrentLineNumber(parser);
908#endif
909}
910#endif
911
912static uint64_t htmllineno(htmllexstate_t *ctx) {
913#ifdef HAVE_EXPAT
914 return expat_get_lineno(ctx->parser);
915#else
916 (void)ctx;
917
918 return 0;
919#endif
920}
921
922#ifdef DEBUG
923static void printTok(htmllexstate_t *ctx, int tok) {
924 char *s;
925
926 switch (tok) {
927 case T_end_br:
928 s = "T_end_br";
929 break;
930 case T_end_img:
931 s = "T_end_img";
932 break;
933 case T_row:
934 s = "T_row";
935 break;
936 case T_end_row:
937 s = "T_end_row";
938 break;
939 case T_html:
940 s = "T_html";
941 break;
942 case T_end_html:
943 s = "T_end_html";
944 break;
945 case T_end_table:
946 s = "T_end_table";
947 break;
948 case T_end_cell:
949 s = "T_end_cell";
950 break;
951 case T_end_font:
952 s = "T_end_font";
953 break;
954 case T_string:
955 s = "T_string";
956 break;
957 case T_error:
958 s = "T_error";
959 break;
960 case T_n_italic:
961 s = "T_n_italic";
962 break;
963 case T_n_bold:
964 s = "T_n_bold";
965 break;
966 case T_n_underline:
967 s = "T_n_underline";
968 break;
969 case T_n_overline:
970 s = "T_n_overline";
971 break;
972 case T_n_sup:
973 s = "T_n_sup";
974 break;
975 case T_n_sub:
976 s = "T_n_sub";
977 break;
978 case T_n_s:
979 s = "T_n_s";
980 break;
981 case T_HR:
982 s = "T_HR";
983 break;
984 case T_hr:
985 s = "T_hr";
986 break;
987 case T_end_hr:
988 s = "T_end_hr";
989 break;
990 case T_VR:
991 s = "T_VR";
992 break;
993 case T_vr:
994 s = "T_vr";
995 break;
996 case T_end_vr:
997 s = "T_end_vr";
998 break;
999 case T_BR:
1000 s = "T_BR";
1001 break;
1002 case T_br:
1003 s = "T_br";
1004 break;
1005 case T_IMG:
1006 s = "T_IMG";
1007 break;
1008 case T_img:
1009 s = "T_img";
1010 break;
1011 case T_table:
1012 s = "T_table";
1013 break;
1014 case T_cell:
1015 s = "T_cell";
1016 break;
1017 case T_font:
1018 s = "T_font";
1019 break;
1020 case T_italic:
1021 s = "T_italic";
1022 break;
1023 case T_bold:
1024 s = "T_bold";
1025 break;
1026 case T_underline:
1027 s = "T_underline";
1028 break;
1029 case T_overline:
1030 s = "T_overline";
1031 break;
1032 case T_sup:
1033 s = "T_sup";
1034 break;
1035 case T_sub:
1036 s = "T_sub";
1037 break;
1038 case T_s:
1039 s = "T_s";
1040 break;
1041 default:
1042 s = "<unknown>";
1043 }
1044 if (tok == T_string) {
1045 const char *token_text = agxbuse(ctx->xb);
1046 fprintf(stderr, "%s \"%s\"\n", s, token_text);
1047 agxbput_move(ctx->xb, token_text);
1048 } else
1049 fprintf(stderr, "%s\n", s);
1050}
1051
1052#endif
1053
1054int htmllex(union HTMLSTYPE *htmllval, htmlscan_t *scanner) {
1055#ifdef HAVE_EXPAT
1056 static char *begin_html = "<HTML>";
1057 static char *end_html = "</HTML>";
1058
1059 char *s;
1060 char *endp = 0;
1061 size_t len, llen;
1062 int rv;
1063 htmllexstate_t *ctx = &scanner->lexer;
1064
1065 ctx->htmllval = htmllval;
1066 ctx->tok = 0;
1067 do {
1068 if (ctx->mode == 2)
1069 return EOF;
1070 if (ctx->mode == 0) {
1071 ctx->mode = 1;
1072 s = begin_html;
1073 len = strlen(s);
1074 endp = 0;
1075 } else {
1076 s = ctx->ptr;
1077 if (*s == '\0') {
1078 ctx->mode = 2;
1079 s = end_html;
1080 len = strlen(s);
1081 } else {
1082 endp = findNext(ctx, s, &ctx->lb);
1083 len = (size_t)(endp - s);
1084 }
1085 }
1086
1087 protect_rsqb(&ctx->lb);
1088
1089 ctx->prevtok = ctx->currtok;
1090 ctx->currtok = (strview_t){.data = s, .size = len};
1091 if ((llen = agxblen(&ctx->lb))) {
1092 assert(llen <= INT_MAX && "XML token too long for expat API");
1093 rv = XML_Parse(ctx->parser, agxbuse(&ctx->lb), (int)llen, 0);
1094 } else {
1095 assert(len <= INT_MAX && "XML token too long for expat API");
1096 rv = XML_Parse(ctx->parser, s, (int)len, len ? 0 : 1);
1097 }
1098 if (rv == XML_STATUS_ERROR) {
1099 if (!ctx->error) {
1100 agerrorf("%s in line %" PRIu64 " \n",
1101 XML_ErrorString(XML_GetErrorCode(ctx->parser)),
1102 htmllineno(ctx));
1103 error_context(ctx);
1104 ctx->error = 1;
1105 ctx->tok = T_error;
1106 }
1107 }
1108 if (endp)
1109 ctx->ptr = endp;
1110 } while (ctx->tok == 0);
1111#ifdef DEBUG
1112 printTok(ctx, ctx->tok);
1113#endif
1114 return ctx->tok;
1115#else
1116 (void)htmllval;
1117 (void)scanner;
1118
1119 return EOF;
1120#endif
1121}
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:97
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:325
static size_t agxblen(const agxbuf *xb)
return number of characters currently stored
Definition agxbuf.h:108
static int agxbputc(agxbuf *xb, char c)
add character to buffer
Definition agxbuf.h:295
Memory allocation wrappers that exit on failure.
static char * gv_strdup(const char *original)
Definition alloc.h:101
static void * gv_alloc(size_t size)
Definition alloc.h:47
container data types API
#define dtinsert(d, o)
Definition cdt.h:186
char * scanEntity(char *t, agxbuf *xb)
Definition utils.c:1025
static Extype_t length(Exid_t *rhs, Exdisc_t *disc)
Definition compile.c:1619
static int flags
Definition gc.c:63
static double len(glCompPoint p)
Definition glutils.c:138
void free(void *)
#define SIZE_MAX
Definition gmlscan.c:347
#define UINT16_MAX
Definition gmlscan.c:340
#define INT8_MAX
Definition gmlscan.c:328
node NULL
Definition grammar.y:181
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:204
void agwarningf(const char *fmt,...)
Definition agerror.c:175
void agerrorf(const char *fmt,...)
Definition agerror.c:167
int agerr(agerrlevel_t level, const char *fmt,...)
Definition agerror.c:157
@ AGPREV
Definition cgraph.h:951
#define GD_charset(g)
Definition types.h:367
#define GD_gvc(g)
Definition types.h:355
replacements for ctype.h functions
static char gv_tolower(int c)
Definition gv_ctype.h:81
agxbput(xb, staging)
int htmllex(union HTMLSTYPE *htmllval, htmlscan_t *scanner)
Definition htmllex.c:1054
static UNUSED void agxbput_move(agxbuf *dst, const char *src)
agxbput, but assume that source and destination may overlap
Definition htmllex.c:789
static void error_context(htmllexstate_t *ctx)
Definition htmllex.c:44
int clearHTMLlexer(htmlscan_t *scanner)
Definition htmllex.c:774
static uint64_t htmllineno(htmllexstate_t *ctx)
Definition htmllex.c:912
int initHTMLlexer(htmlscan_t *scanner, char *src, agxbuf *xb, htmlenv_t *env)
Definition htmllex.c:741
void htmlerror(htmlscan_t *scanner, const char *msg)
Definition htmllex.c:52
#define T_n_sup
Definition htmlparse.h:138
#define T_end_row
Definition htmlparse.h:126
#define T_end_table
Definition htmlparse.h:129
#define T_br
Definition htmlparse.h:148
#define T_vr
Definition htmlparse.h:145
#define T_error
Definition htmlparse.h:133
#define T_n_s
Definition htmlparse.h:140
#define T_end_cell
Definition htmlparse.h:130
#define T_n_sub
Definition htmlparse.h:139
#define T_n_bold
Definition htmlparse.h:135
#define T_html
Definition htmlparse.h:127
#define T_BR
Definition htmlparse.h:147
#define T_underline
Definition htmlparse.h:156
#define T_sup
Definition htmlparse.h:158
#define T_row
Definition htmlparse.h:125
#define T_table
Definition htmlparse.h:151
#define T_end_vr
Definition htmlparse.h:146
#define T_end_html
Definition htmlparse.h:128
#define T_IMG
Definition htmlparse.h:149
#define T_VR
Definition htmlparse.h:144
#define T_bold
Definition htmlparse.h:155
#define T_end_img
Definition htmlparse.h:124
#define T_sub
Definition htmlparse.h:159
#define T_s
Definition htmlparse.h:160
#define T_n_italic
Definition htmlparse.h:134
#define T_end_font
Definition htmlparse.h:131
#define T_overline
Definition htmlparse.h:157
#define T_hr
Definition htmlparse.h:142
#define T_font
Definition htmlparse.h:153
#define T_italic
Definition htmlparse.h:154
#define T_end_br
Definition htmlparse.h:123
#define T_n_underline
Definition htmlparse.h:136
#define T_cell
Definition htmlparse.h:152
#define T_end_hr
Definition htmlparse.h:143
#define T_string
Definition htmlparse.h:132
#define T_img
Definition htmlparse.h:150
#define T_HR
Definition htmlparse.h:141
#define T_n_overline
Definition htmlparse.h:137
cleanup & scanner
Definition htmlparse.y:289
#define PAD_SET
Definition htmltable.h:35
#define BORDER_RIGHT
Definition htmltable.h:42
#define BORDER_TOP
Definition htmltable.h:41
#define HALIGN_TEXT
Definition htmltable.h:30
#define UNSET_ALIGN
Definition htmltable.h:46
#define HALIGN_LEFT
Definition htmltable.h:28
#define VALIGN_BOTTOM
Definition htmltable.h:32
#define BALIGN_RIGHT
Definition htmltable.h:37
#define BALIGN_LEFT
Definition htmltable.h:38
#define BORDER_BOTTOM
Definition htmltable.h:43
static void free_ritem(row_t *p)
Free row. This closes and frees row’s list, then the item itself is freed.
Definition htmltable.h:119
#define SPACE_SET
Definition htmltable.h:36
#define BORDER_SET
Definition htmltable.h:34
#define BORDER_LEFT
Definition htmltable.h:40
#define BORDER_MASK
Definition htmltable.h:44
#define HALIGN_RIGHT
Definition htmltable.h:27
#define VALIGN_TOP
Definition htmltable.h:31
#define FIXED_FLAG
Definition htmltable.h:26
char * charsetToStr(int c)
Given an internal charset value, return a canonical string representation.
Definition input.c:806
static bool startswith(const char *s, const char *prefix)
does the string s begin with the string prefix?
Definition startswith.h:11
platform abstraction for case-insensitive string functions
Dt_t * textfont_dt
Definition gvcint.h:108
result of partitioning available space, part of maze
Definition grid.h:33
uint16_t rowspan
Definition htmltable.h:160
uint16_t colspan
Definition htmltable.h:159
char * bgcolor
Definition htmltable.h:89
unsigned char border
Definition htmltable.h:93
char * target
Definition htmltable.h:86
char * id
Definition htmltable.h:88
signed char space
Definition htmltable.h:92
unsigned short width
Definition htmltable.h:97
unsigned short height
Definition htmltable.h:98
int gradientangle
Definition htmltable.h:91
char * port
Definition htmltable.h:85
unsigned short flags
Definition htmltable.h:96
char * href
Definition htmltable.h:84
unsigned char pad
Definition htmltable.h:94
char * pencolor
Definition htmltable.h:90
htmlstyle_t style
Definition htmltable.h:99
char * title
Definition htmltable.h:87
graph_t * g
Definition htmltable.h:173
char * scale
Definition htmltable.h:72
char * src
Definition htmltable.h:71
HTMLSTYPE * htmllval
Definition htmlparse.h:229
agxbuf * xb
Definition htmlparse.h:220
strview_t prevtok
Definition htmlparse.h:227
strview_t currtok
Definition htmlparse.h:226
bool dashed
Definition htmltable.h:80
bool dotted
Definition htmltable.h:79
bool rounded
Definition htmltable.h:77
bool radial
Definition htmltable.h:76
bool invisible
Definition htmltable.h:78
bool vrule
vertical rule
Definition htmltable.h:145
size_t row_count
number of rows
Definition htmltable.h:141
bool hrule
horizontal rule
Definition htmltable.h:144
htmldata_t data
Definition htmltable.h:127
rows_t rows
cells
Definition htmltable.h:135
int8_t cellborder
Definition htmltable.h:138
Definition utils.c:699
a non-owning string reference
Definition strview.h:20
const char * data
start of the pointed to string
Definition strview.h:21
size_t size
extent of the string in bytes
Definition strview.h:22
char * color
Definition textspan.h:57
char * name
Definition textspan.h:56
unsigned int flags
Definition textspan.h:61
double size
Definition textspan.h:59
state for an in-progress string tokenization
Definition tokenize.h:36
Non-owning string references.
static bool strview_case_str_eq(strview_t a, const char *b)
compare a string reference to a string for case insensitive equality
Definition strview.h:62
#define HTML_OL
Definition textspan.h:37
#define HTML_IF
Definition textspan.h:32
#define HTML_UL
Definition textspan.h:33
#define HTML_BF
Definition textspan.h:31
#define HTML_SUP
Definition textspan.h:34
#define HTML_S
Definition textspan.h:36
#define GV_TEXTFONT_FLAGS_WIDTH
Definition textspan.h:26
#define HTML_SUB
Definition textspan.h:35
String tokenization.
static strview_t tok_get(const tok_t *t)
get the current token
Definition tokenize.h:76
static tok_t tok(const char *input, const char *separators)
begin tokenization of a new string
Definition tokenize.h:43
static bool tok_end(const tok_t *t)
is this tokenizer exhausted?
Definition tokenize.h:68
static void tok_next(tok_t *t)
advance to the next token in the string being scanned
Definition tokenize.h:85
htmltbl_t * tbl
Definition htmlparse.h:171
htmlcell_t * cell
Definition htmlparse.h:170
textfont_t * font
Definition htmlparse.h:172
htmlimg_t * img
Definition htmlparse.h:173
Definition grammar.c:90
abstraction for squashing compiler warnings for unused symbols
#define UNUSED
Definition unused.h:25