35#ifndef XML_STATUS_ERROR
36#define XML_STATUS_ERROR 0
74typedef int (*attrFn) (
void *,
char *);
75typedef int (*bcmpfn) (
const void *,
const void *);
83#define ISIZE (sizeof(attr_item))
88static int icmp(
const void *name,
const void *
item) {
89 const attr_item *j =
item;
90 return strcasecmp(name, j->name);
113 unsigned short flags = 0;
131 agwarningf(
"Unrecognized character '%c' (%d) in sides attribute\n", c, c);
142 p->
title = strdup(v);
169 agwarningf(
"Illegal value %.*s for STYLE - ignored\n", (
int)tk.
size,
196static int doInt(
char *v,
char *
s,
int min,
int max,
long *ul)
200 long b = strtol(v, &ep, 10);
203 agwarningf(
"Improper %s value %s - ignored",
s, v);
205 }
else if (b > max) {
206 agwarningf(
"%s value %s > %d - too large - ignored",
s, v, max);
208 }
else if (b < min) {
209 agwarningf(
"%s value %s < %d - too small - ignored",
s, v, min);
217static int gradientanglefn(
htmldata_t * p,
char *v)
221 if (doInt(v,
"GRADIENTANGLE", 0, 360, &u))
232 if (doInt(v,
"BORDER", 0, UCHAR_MAX, &u))
234 p->
border = (
unsigned char) u;
239static int cellpaddingfn(
htmldata_t * p,
char *v)
243 if (doInt(v,
"CELLPADDING", 0, UCHAR_MAX, &u))
245 p->
pad = (
unsigned char) u;
250static int cellspacingfn(
htmldata_t * p,
char *v)
254 if (doInt(v,
"CELLSPACING", SCHAR_MIN, SCHAR_MAX, &u))
256 p->
space = (
signed char) u;
261static int cellborderfn(
htmltbl_t * p,
char *v)
265 if (doInt(v,
"CELLBORDER", 0,
INT8_MAX, &u))
271static int columnsfn(
htmltbl_t * p,
char *v)
274 agwarningf(
"Unknown value %s for COLUMNS - ignored\n", v);
284 agwarningf(
"Unknown value %s for ROWS - ignored\n", v);
291static int fixedsizefn(
htmldata_t * p,
char *v)
294 if (!strcasecmp(v,
"TRUE"))
296 else if (strcasecmp(v,
"FALSE")) {
297 agwarningf(
"Illegal value %s for FIXEDSIZE - ignored\n", v);
306 if (!strcasecmp(v,
"BOTTOM"))
308 else if (!strcasecmp(v,
"TOP"))
310 else if (strcasecmp(v,
"MIDDLE")) {
311 agwarningf(
"Illegal value %s for VALIGN - ignored\n", v);
320 if (!strcasecmp(v,
"LEFT"))
322 else if (!strcasecmp(v,
"RIGHT"))
324 else if (strcasecmp(v,
"CENTER")) {
325 agwarningf(
"Illegal value %s for ALIGN - ignored\n", v);
331static int cell_halignfn(
htmldata_t * p,
char *v)
334 if (!strcasecmp(v,
"LEFT"))
336 else if (!strcasecmp(v,
"RIGHT"))
338 else if (!strcasecmp(v,
"TEXT"))
340 else if (strcasecmp(v,
"CENTER"))
343 agwarningf(
"Illegal value %s for ALIGN in TD - ignored\n", v);
350 if (!strcasecmp(v,
"LEFT"))
352 else if (!strcasecmp(v,
"RIGHT"))
354 else if (strcasecmp(v,
"CENTER"))
357 agwarningf(
"Illegal value %s for BALIGN in TD - ignored\n", v);
365 if (doInt(v,
"HEIGHT", 0, USHRT_MAX, &u))
367 p->
height = (
unsigned short) u;
375 if (doInt(v,
"WIDTH", 0, USHRT_MAX, &u))
377 p->
width = (
unsigned short) u;
388 agwarningf(
"ROWSPAN value cannot be 0 - ignored\n");
402 agwarningf(
"COLSPAN value cannot be 0 - ignored\n");
409static int fontcolorfn(
textfont_t * p,
char *v)
425 if (doInt(v,
"POINT-SIZE", 0, UCHAR_MAX, &u))
427 p->
size = (double) u;
437static int scalefn(
htmlimg_t * p,
char *v)
439 p->
scale = strdup(v);
443static int alignfn(
int *p,
char *v)
446 if (!strcasecmp(v,
"RIGHT"))
448 else if (!strcasecmp(v,
"LEFT"))
450 else if (!strcasecmp(v,
"CENTER"))
453 agwarningf(
"Illegal value %s for ALIGN - ignored\n", v);
460static attr_item tbl_items[] = {
461 {
"align", (attrFn) halignfn},
462 {
"bgcolor", (attrFn) bgcolorfn},
463 {
"border", (attrFn) borderfn},
464 {
"cellborder", (attrFn) cellborderfn},
465 {
"cellpadding", (attrFn) cellpaddingfn},
466 {
"cellspacing", (attrFn) cellspacingfn},
467 {
"color", (attrFn) pencolorfn},
468 {
"columns", (attrFn) columnsfn},
469 {
"fixedsize", (attrFn) fixedsizefn},
470 {
"gradientangle", (attrFn) gradientanglefn},
471 {
"height", (attrFn) heightfn},
472 {
"href", (attrFn) hreffn},
473 {
"id", (attrFn) idfn},
474 {
"port", (attrFn) portfn},
475 {
"rows", (attrFn) rowsfn},
476 {
"sides", (attrFn) sidesfn},
477 {
"style", (attrFn) stylefn},
478 {
"target", (attrFn) targetfn},
479 {
"title", (attrFn) titlefn},
480 {
"tooltip", (attrFn) titlefn},
481 {
"valign", (attrFn) valignfn},
482 {
"width", (attrFn) widthfn},
485static attr_item cell_items[] = {
486 {
"align", (attrFn) cell_halignfn},
487 {
"balign", (attrFn) balignfn},
488 {
"bgcolor", (attrFn) bgcolorfn},
489 {
"border", (attrFn) borderfn},
490 {
"cellpadding", (attrFn) cellpaddingfn},
491 {
"cellspacing", (attrFn) cellspacingfn},
492 {
"color", (attrFn) pencolorfn},
493 {
"colspan", (attrFn) colspanfn},
494 {
"fixedsize", (attrFn) fixedsizefn},
495 {
"gradientangle", (attrFn) gradientanglefn},
496 {
"height", (attrFn) heightfn},
497 {
"href", (attrFn) hreffn},
498 {
"id", (attrFn) idfn},
499 {
"port", (attrFn) portfn},
500 {
"rowspan", (attrFn) rowspanfn},
501 {
"sides", (attrFn) sidesfn},
502 {
"style", (attrFn) stylefn},
503 {
"target", (attrFn) targetfn},
504 {
"title", (attrFn) titlefn},
505 {
"tooltip", (attrFn) titlefn},
506 {
"valign", (attrFn) valignfn},
507 {
"width", (attrFn) widthfn},
510static attr_item font_items[] = {
511 {
"color", (attrFn) fontcolorfn},
512 {
"face", (attrFn) facefn},
513 {
"point-size", (attrFn) ptsizefn},
516static attr_item img_items[] = {
517 {
"scale", (attrFn) scalefn},
518 {
"src", (attrFn) srcfn},
521static attr_item br_items[] = {
522 {
"align", (attrFn) alignfn},
533static void doAttrs(
htmllexstate_t *ctx,
void *tp, attr_item *items,
size_t nel,
char **atts,
539 while ((name = *atts++) !=
NULL) {
541 ip = bsearch(name, items, nel, ISIZE, icmp);
543 ctx->
warn |= ip->action(tp, val);
545 agwarningf(
"Illegal attribute %s in %s - ignored\n", name,
555 doAttrs(ctx, &ctx->
htmllval->
i, br_items,
sizeof(br_items) / ISIZE, atts,
"<BR>");
562 doAttrs(ctx, img, img_items,
sizeof(img_items) / ISIZE, atts,
"<IMG>");
572 assert(
flags <= FLAGS_MAX);
573 tf.
flags = (
unsigned char)(
flags & FLAGS_MAX);
575 doAttrs(ctx, &tf, font_items,
sizeof(font_items) / ISIZE, atts,
"<FONT>");
586 doAttrs(ctx,
cell, cell_items,
sizeof(cell_items) / ISIZE, atts,
"<TD>");
598 doAttrs(ctx, tbl, tbl_items,
sizeof(tbl_items) / ISIZE, atts,
"<TABLE>");
603static void startElement(
void *user,
const char *name,
char **atts)
607 if (strcasecmp(name,
"TABLE") == 0) {
611 }
else if (strcasecmp(name,
"TR") == 0 || strcasecmp(name,
"TH") == 0) {
614 }
else if (strcasecmp(name,
"TD") == 0) {
618 }
else if (strcasecmp(name,
"FONT") == 0) {
621 }
else if (strcasecmp(name,
"B") == 0) {
624 }
else if (strcasecmp(name,
"S") == 0) {
627 }
else if (strcasecmp(name,
"U") == 0) {
630 }
else if (strcasecmp(name,
"O") == 0) {
633 }
else if (strcasecmp(name,
"I") == 0) {
636 }
else if (strcasecmp(name,
"SUP") == 0) {
639 }
else if (strcasecmp(name,
"SUB") == 0) {
642 }
else if (strcasecmp(name,
"BR") == 0) {
645 }
else if (strcasecmp(name,
"HR") == 0) {
647 }
else if (strcasecmp(name,
"VR") == 0) {
649 }
else if (strcasecmp(name,
"IMG") == 0) {
652 }
else if (strcasecmp(name,
"HTML") == 0) {
659static 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)
729 for (i =
length; i; i--) {
757 XML_SetUserData(ctx->parser, ctx);
758 XML_SetElementHandler(ctx->parser,
759 (XML_StartElementHandler) startElement,
761 XML_SetCharacterDataHandler(ctx->parser, characterData);
772 "Not built with libexpat. Table formatting is not available.\n");
784 XML_ParserFree(ctx->parser);
816 while (depth && (c = *
s++)) {
844 t = eatComment(ctx, t + 3);
846 while (*t && *t !=
'>')
849 agwarningf(
"Label closed before end of HTML element\n");
855 while ((c = *t) && c !=
'<') {
856 if (c ==
'&' && *(t+1) !=
'#') {
884static void protect_rsqb(
agxbuf *xb) {
893 size_t size = strlen(data);
895 if (data[size - 1] !=
']') {
901 data[size - 1] =
'\0';
916 return XML_GetCurrentLineNumber(ctx->parser);
1048 const char *token_text =
agxbuse(ctx->
xb);
1049 fprintf(stderr,
"%s \"%s\"\n",
s, token_text);
1052 fprintf(stderr,
"%s\n",
s);
1060 static char *begin_html =
"<HTML>";
1061 static char *end_html =
"</HTML>";
1074 if (ctx->
mode == 0) {
1086 endp = findNext(ctx,
s,&ctx->
lb);
1087 len = (size_t)(endp -
s);
1091 protect_rsqb(&ctx->
lb);
1096 assert(llen <= INT_MAX &&
"XML token too long for expat API");
1097 rv = XML_Parse(ctx->parser,
agxbuse(&ctx->
lb), (
int)llen, 0);
1099 assert(
len <= INT_MAX &&
"XML token too long for expat API");
1100 rv = XML_Parse(ctx->parser,
s, (
int)
len,
len ? 0 : 1);
1113 }
while (ctx->
tok == 0);
1115 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
unsigned long htmllineno(htmlscan_t *scanner)
static void error_context(htmllexstate_t *ctx)
int clearHTMLlexer(htmlscan_t *scanner)
int initHTMLlexer(htmlscan_t *scanner, char *src, agxbuf *xb, htmlenv_t *env)
void htmlerror(htmlscan_t *scanner, const char *msg)
static unsigned long htmllineno_ctx(htmllexstate_t *ctx)
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