68 agerrorf(
"Unknown HTML element <%s> on line %" PRIu64
" \n", name,
73static int icmp(
const void *name,
const void *
item) {
74 const char *
const *
const j =
item;
75 return strcasecmp(name, *j);
78static int bgcolorfn(
htmldata_t *p,
const char *v) {
83static int pencolorfn(
htmldata_t *p,
const char *v) {
88static int hreffn(
htmldata_t *p,
const char *v) {
93static int sidesfn(
htmldata_t *p,
const char *v) {
94 unsigned short flags = 0;
112 agwarningf(
"Unrecognized character '%c' (%d) in sides attribute\n", c, c);
121static int titlefn(
htmldata_t *p,
const char *v) {
122 p->
title = strdup(v);
126static int portfn(
htmldata_t *p,
const char *v) {
133static int stylefn(
htmldata_t *p,
const char *v) {
152 agwarningf(
"Illegal value %.*s for STYLE - ignored\n", (
int)tk.
size,
160static int targetfn(
htmldata_t *p,
const char *v) {
165static int idfn(
htmldata_t *p,
const char *v) {
176static int doInt(
const char *v,
char *
s,
int min,
int max,
long *ul) {
179 long b = strtol(v, &ep, 10);
182 agwarningf(
"Improper %s value %s - ignored",
s, v);
184 }
else if (b > max) {
185 agwarningf(
"%s value %s > %d - too large - ignored",
s, v, max);
187 }
else if (b < min) {
188 agwarningf(
"%s value %s < %d - too small - ignored",
s, v, min);
195static int gradientanglefn(
htmldata_t *p,
const char *v) {
198 if (doInt(v,
"GRADIENTANGLE", 0, 360, &u))
204static int borderfn(
htmldata_t *p,
const char *v) {
207 if (doInt(v,
"BORDER", 0, UCHAR_MAX, &u))
209 p->
border = (
unsigned char)u;
214static int cellpaddingfn(
htmldata_t *p,
const char *v) {
217 if (doInt(v,
"CELLPADDING", 0, UCHAR_MAX, &u))
219 p->
pad = (
unsigned char)u;
224static int cellspacingfn(
htmldata_t *p,
const char *v) {
227 if (doInt(v,
"CELLSPACING", SCHAR_MIN, SCHAR_MAX, &u))
229 p->
space = (
signed char)u;
234static int cellborderfn(
htmldata_t *data,
const char *v) {
239 if (doInt(v,
"CELLBORDER", 0,
INT8_MAX, &u))
245static int columnsfn(
htmldata_t *data,
const char *v) {
249 agwarningf(
"Unknown value %s for COLUMNS - ignored\n", v);
256static int rowsfn(
htmldata_t *data,
const char *v) {
260 agwarningf(
"Unknown value %s for ROWS - ignored\n", v);
267static int fixedsizefn(
htmldata_t *p,
const char *v) {
269 if (!strcasecmp(v,
"TRUE"))
271 else if (strcasecmp(v,
"FALSE")) {
272 agwarningf(
"Illegal value %s for FIXEDSIZE - ignored\n", v);
278static int valignfn(
htmldata_t *p,
const char *v) {
280 if (!strcasecmp(v,
"BOTTOM"))
282 else if (!strcasecmp(v,
"TOP"))
284 else if (strcasecmp(v,
"MIDDLE")) {
285 agwarningf(
"Illegal value %s for VALIGN - ignored\n", v);
291static int halignfn(
htmldata_t *p,
const char *v) {
293 if (!strcasecmp(v,
"LEFT"))
295 else if (!strcasecmp(v,
"RIGHT"))
297 else if (strcasecmp(v,
"CENTER")) {
298 agwarningf(
"Illegal value %s for ALIGN - ignored\n", v);
304static int cell_halignfn(
htmldata_t *p,
const char *v) {
306 if (!strcasecmp(v,
"LEFT"))
308 else if (!strcasecmp(v,
"RIGHT"))
310 else if (!strcasecmp(v,
"TEXT"))
312 else if (strcasecmp(v,
"CENTER"))
315 agwarningf(
"Illegal value %s for ALIGN in TD - ignored\n", v);
319static int balignfn(
htmldata_t *p,
const char *v) {
321 if (!strcasecmp(v,
"LEFT"))
323 else if (!strcasecmp(v,
"RIGHT"))
325 else if (strcasecmp(v,
"CENTER"))
328 agwarningf(
"Illegal value %s for BALIGN in TD - ignored\n", v);
332static int heightfn(
htmldata_t *p,
const char *v) {
335 if (doInt(v,
"HEIGHT", 0, USHRT_MAX, &u))
337 p->
height = (
unsigned short)u;
341static int widthfn(
htmldata_t *p,
const char *v) {
344 if (doInt(v,
"WIDTH", 0, USHRT_MAX, &u))
346 p->
width = (
unsigned short)u;
350static int rowspanfn(
htmldata_t *data,
const char *v) {
358 agwarningf(
"ROWSPAN value cannot be 0 - ignored\n");
365static int colspanfn(
htmldata_t *data,
const char *v) {
373 agwarningf(
"COLSPAN value cannot be 0 - ignored\n");
380static int fontcolorfn(
textfont_t *p,
const char *v) {
382 p->
color = (
char *)v;
386static int facefn(
textfont_t *p,
const char *v) {
392static int ptsizefn(
textfont_t *p,
const char *v) {
395 if (doInt(v,
"POINT-SIZE", 0, UCHAR_MAX, &u))
401static int srcfn(
htmlimg_t *p,
const char *v) {
406static int scalefn(
htmlimg_t *p,
const char *v) {
407 p->
scale = strdup(v);
411static int alignfn(
int *p,
const char *v) {
413 if (!strcasecmp(v,
"RIGHT"))
415 else if (!strcasecmp(v,
"LEFT"))
417 else if (!strcasecmp(v,
"CENTER"))
420 agwarningf(
"Illegal value %s for ALIGN - ignored\n", v);
433static const html_item_t tbl_items[] = {
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},
451 {
"target", targetfn},
453 {
"tooltip", titlefn},
454 {
"valign", valignfn},
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},
473 {
"rowspan", rowspanfn},
476 {
"target", targetfn},
478 {
"tooltip", titlefn},
479 {
"valign", valignfn},
489static const font_item_t font_items[] = {
490 {
"color", fontcolorfn},
492 {
"point-size", ptsizefn},
501static const img_item_t img_items[] = {
508 int (*action)(
int *,
const char *);
511static const br_item_t br_items[] = {
519#define CALL_ACTION(list, elem, tp, val) \
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)))
535#define doAttrs(ctx, tp, items, nel, atts, s) \
539 while ((name = *(atts)++) != NULL) { \
540 const char *val = *(atts)++; \
542 bsearch(name, (items), (nel), sizeof((items)[0]), icmp); \
544 (ctx)->warn |= CALL_ACTION((items), ip, (tp), val); \
546 agwarningf("Illegal attribute %s in %s - ignored\n", name, (s)); \
554 doAttrs(ctx, &ctx->
htmllval->
i, br_items,
555 sizeof(br_items) /
sizeof(br_items[0]), atts,
"<BR>");
561 doAttrs(ctx, img, img_items,
sizeof(img_items) /
sizeof(img_items[0]), atts,
568 unsigned char flags) {
573 assert(
flags <= FLAGS_MAX);
574 tf.
flags = (
unsigned char)(
flags & FLAGS_MAX);
576 doAttrs(ctx, &tf, font_items,
sizeof(font_items) /
sizeof(font_items[0]),
587 doAttrs(ctx, &
cell->data, cell_items,
588 sizeof(cell_items) /
sizeof(cell_items[0]), atts,
"<TD>");
599 doAttrs(ctx, &tbl->
data, tbl_items,
sizeof(tbl_items) /
sizeof(tbl_items[0]),
605static void startElement(
void *user,
const char *name,
const char **atts) {
608 if (strcasecmp(name,
"TABLE") == 0) {
612 }
else if (strcasecmp(name,
"TR") == 0 || strcasecmp(name,
"TH") == 0) {
615 }
else if (strcasecmp(name,
"TD") == 0) {
619 }
else if (strcasecmp(name,
"FONT") == 0) {
622 }
else if (strcasecmp(name,
"B") == 0) {
625 }
else if (strcasecmp(name,
"S") == 0) {
628 }
else if (strcasecmp(name,
"U") == 0) {
631 }
else if (strcasecmp(name,
"O") == 0) {
634 }
else if (strcasecmp(name,
"I") == 0) {
637 }
else if (strcasecmp(name,
"SUP") == 0) {
640 }
else if (strcasecmp(name,
"SUB") == 0) {
643 }
else if (strcasecmp(name,
"BR") == 0) {
646 }
else if (strcasecmp(name,
"HR") == 0) {
648 }
else if (strcasecmp(name,
"VR") == 0) {
650 }
else if (strcasecmp(name,
"IMG") == 0) {
653 }
else if (strcasecmp(name,
"HTML") == 0) {
660static void endElement(
void *user,
const char *name) {
663 if (strcasecmp(name,
"TABLE") == 0) {
666 }
else if (strcasecmp(name,
"TR") == 0 || strcasecmp(name,
"TH") == 0) {
668 }
else if (strcasecmp(name,
"TD") == 0) {
671 }
else if (strcasecmp(name,
"HTML") == 0) {
673 }
else if (strcasecmp(name,
"FONT") == 0) {
675 }
else if (strcasecmp(name,
"B") == 0) {
677 }
else if (strcasecmp(name,
"U") == 0) {
679 }
else if (strcasecmp(name,
"O") == 0) {
681 }
else if (strcasecmp(name,
"I") == 0) {
683 }
else if (strcasecmp(name,
"SUP") == 0) {
685 }
else if (strcasecmp(name,
"SUB") == 0) {
687 }
else if (strcasecmp(name,
"S") == 0) {
689 }
else if (strcasecmp(name,
"BR") == 0) {
694 }
else if (strcasecmp(name,
"HR") == 0) {
699 }
else if (strcasecmp(name,
"VR") == 0) {
704 }
else if (strcasecmp(name,
"IMG") == 0) {
721static void characterData(
void *user,
const char *
s,
int length) {
728 for (i =
length; i; i--) {
756 XML_SetUserData(ctx->parser, ctx);
757 XML_SetElementHandler(ctx->parser, startElement, endElement);
758 XML_SetCharacterDataHandler(ctx->parser, characterData);
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");
778 XML_ParserFree(ctx->parser);
809 while (depth && (c = *
s++)) {
836 t = eatComment(ctx, t + 3);
838 while (*t && *t !=
'>')
841 agwarningf(
"Label closed before end of HTML element\n");
847 while ((c = *t) && c !=
'<') {
848 if (c ==
'&' && *(t + 1) !=
'#') {
875static void protect_rsqb(
agxbuf *xb) {
884 size_t size = strlen(data);
886 if (data[size - 1] !=
']') {
892 data[size - 1] =
'\0';
901static uint64_t expat_get_lineno(XML_Parser parser) {
904#if XML_MAJOR_VERSION > 2 || (XML_MAJOR_VERSION == 2 && XML_MINOR_VERSION >= 9)
905 return XML_GetCurrentLineNumber64(parser);
907 return XML_GetCurrentLineNumber(parser);
914 return expat_get_lineno(ctx->parser);
1045 const char *token_text =
agxbuse(ctx->
xb);
1046 fprintf(stderr,
"%s \"%s\"\n",
s, token_text);
1049 fprintf(stderr,
"%s\n",
s);
1056 static char *begin_html =
"<HTML>";
1057 static char *end_html =
"</HTML>";
1070 if (ctx->
mode == 0) {
1082 endp = findNext(ctx,
s, &ctx->
lb);
1083 len = (size_t)(endp -
s);
1087 protect_rsqb(&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);
1095 assert(
len <= INT_MAX &&
"XML token too long for expat API");
1096 rv = XML_Parse(ctx->parser,
s, (
int)
len,
len ? 0 : 1);
1098 if (rv == XML_STATUS_ERROR) {
1100 agerrorf(
"%s in line %" PRIu64
" \n",
1101 XML_ErrorString(XML_GetErrorCode(ctx->parser)),
1110 }
while (ctx->
tok == 0);
1112 printTok(ctx, ctx->
tok);
static void agxbfree(agxbuf *xb)
free any malloced resources
static WUR char * agxbuse(agxbuf *xb)
static size_t agxblen(const agxbuf *xb)
return number of characters currently stored
static int agxbputc(agxbuf *xb, char c)
add character to buffer
Memory allocation wrappers that exit on failure.
static char * gv_strdup(const char *original)
static void * gv_alloc(size_t size)
char * scanEntity(char *t, agxbuf *xb)
static Extype_t length(Exid_t *rhs, Exdisc_t *disc)
static double len(glCompPoint p)
static int cnt(Dict_t *d, Dtlink_t **set)
void agwarningf(const char *fmt,...)
void agerrorf(const char *fmt,...)
int agerr(agerrlevel_t level, const char *fmt,...)
replacements for ctype.h functions
static char gv_tolower(int c)
int htmllex(union HTMLSTYPE *htmllval, htmlscan_t *scanner)
static UNUSED void agxbput_move(agxbuf *dst, const char *src)
agxbput, but assume that source and destination may overlap
static void error_context(htmllexstate_t *ctx)
int clearHTMLlexer(htmlscan_t *scanner)
static uint64_t htmllineno(htmllexstate_t *ctx)
int initHTMLlexer(htmlscan_t *scanner, char *src, agxbuf *xb, htmlenv_t *env)
void htmlerror(htmlscan_t *scanner, const char *msg)
static void free_ritem(row_t *p)
Free row. This closes and frees row’s list, then the item itself is freed.
static bool startswith(const char *s, const char *prefix)
does the string s begin with the string prefix?
platform abstraction for case-insensitive string functions
result of partitioning available space, part of maze
size_t row_count
number of rows
bool hrule
horizontal rule
a non-owning string reference
const char * data
start of the pointed to string
size_t size
extent of the string in bytes
state for an in-progress string tokenization
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
#define GV_TEXTFONT_FLAGS_WIDTH
static strview_t tok_get(const tok_t *t)
get the current token
static tok_t tok(const char *input, const char *separators)
begin tokenization of a new string
static bool tok_end(const tok_t *t)
is this tokenizer exhausted?
static void tok_next(tok_t *t)
advance to the next token in the string being scanned
abstraction for squashing compiler warnings for unused symbols