37#ifndef XML_STATUS_ERROR
38#define XML_STATUS_ERROR 0
76typedef int (*attrFn) (
void *,
char *);
77typedef int (*bcmpfn) (
const void *,
const void *);
85#define ISIZE (sizeof(attr_item))
90static int icmp(
const void *name,
const void *
item) {
91 const attr_item *j =
item;
92 return strcasecmp(name, j->name);
115 unsigned short flags = 0;
133 agwarningf(
"Unrecognized character '%c' (%d) in sides attribute\n", c, c);
144 p->
title = strdup(v);
171 agwarningf(
"Illegal value %.*s for STYLE - ignored\n", (
int)tk.
size,
198static int doInt(
char *v,
char *
s,
int min,
int max,
long *ul)
202 long b = strtol(v, &ep, 10);
205 agwarningf(
"Improper %s value %s - ignored",
s, v);
207 }
else if (b > max) {
208 agwarningf(
"%s value %s > %d - too large - ignored",
s, v, max);
210 }
else if (b < min) {
211 agwarningf(
"%s value %s < %d - too small - ignored",
s, v, min);
219static int gradientanglefn(
htmldata_t * p,
char *v)
223 if (doInt(v,
"GRADIENTANGLE", 0, 360, &u))
234 if (doInt(v,
"BORDER", 0, UCHAR_MAX, &u))
236 p->
border = (
unsigned char) u;
241static int cellpaddingfn(
htmldata_t * p,
char *v)
245 if (doInt(v,
"CELLPADDING", 0, UCHAR_MAX, &u))
247 p->
pad = (
unsigned char) u;
252static int cellspacingfn(
htmldata_t * p,
char *v)
256 if (doInt(v,
"CELLSPACING", SCHAR_MIN, SCHAR_MAX, &u))
258 p->
space = (
signed char) u;
263static int cellborderfn(
htmltbl_t * p,
char *v)
267 if (doInt(v,
"CELLBORDER", 0,
INT8_MAX, &u))
273static int columnsfn(
htmltbl_t * p,
char *v)
276 agwarningf(
"Unknown value %s for COLUMNS - ignored\n", v);
286 agwarningf(
"Unknown value %s for ROWS - ignored\n", v);
293static int fixedsizefn(
htmldata_t * p,
char *v)
296 if (!strcasecmp(v,
"TRUE"))
298 else if (strcasecmp(v,
"FALSE")) {
299 agwarningf(
"Illegal value %s for FIXEDSIZE - ignored\n", v);
308 if (!strcasecmp(v,
"BOTTOM"))
310 else if (!strcasecmp(v,
"TOP"))
312 else if (strcasecmp(v,
"MIDDLE")) {
313 agwarningf(
"Illegal value %s for VALIGN - ignored\n", v);
322 if (!strcasecmp(v,
"LEFT"))
324 else if (!strcasecmp(v,
"RIGHT"))
326 else if (strcasecmp(v,
"CENTER")) {
327 agwarningf(
"Illegal value %s for ALIGN - ignored\n", v);
333static int cell_halignfn(
htmldata_t * p,
char *v)
336 if (!strcasecmp(v,
"LEFT"))
338 else if (!strcasecmp(v,
"RIGHT"))
340 else if (!strcasecmp(v,
"TEXT"))
342 else if (strcasecmp(v,
"CENTER"))
345 agwarningf(
"Illegal value %s for ALIGN in TD - ignored\n", v);
352 if (!strcasecmp(v,
"LEFT"))
354 else if (!strcasecmp(v,
"RIGHT"))
356 else if (strcasecmp(v,
"CENTER"))
359 agwarningf(
"Illegal value %s for BALIGN in TD - ignored\n", v);
367 if (doInt(v,
"HEIGHT", 0, USHRT_MAX, &u))
369 p->
height = (
unsigned short) u;
377 if (doInt(v,
"WIDTH", 0, USHRT_MAX, &u))
379 p->
width = (
unsigned short) u;
390 agwarningf(
"ROWSPAN value cannot be 0 - ignored\n");
404 agwarningf(
"COLSPAN value cannot be 0 - ignored\n");
411static int fontcolorfn(
textfont_t * p,
char *v)
427 if (doInt(v,
"POINT-SIZE", 0, UCHAR_MAX, &u))
429 p->
size = (double) u;
439static int scalefn(
htmlimg_t * p,
char *v)
441 p->
scale = strdup(v);
445static int alignfn(
int *p,
char *v)
448 if (!strcasecmp(v,
"RIGHT"))
450 else if (!strcasecmp(v,
"LEFT"))
452 else if (!strcasecmp(v,
"CENTER"))
455 agwarningf(
"Illegal value %s for ALIGN - ignored\n", v);
462static attr_item tbl_items[] = {
463 {
"align", (attrFn) halignfn},
464 {
"bgcolor", (attrFn) bgcolorfn},
465 {
"border", (attrFn) borderfn},
466 {
"cellborder", (attrFn) cellborderfn},
467 {
"cellpadding", (attrFn) cellpaddingfn},
468 {
"cellspacing", (attrFn) cellspacingfn},
469 {
"color", (attrFn) pencolorfn},
470 {
"columns", (attrFn) columnsfn},
471 {
"fixedsize", (attrFn) fixedsizefn},
472 {
"gradientangle", (attrFn) gradientanglefn},
473 {
"height", (attrFn) heightfn},
474 {
"href", (attrFn) hreffn},
475 {
"id", (attrFn) idfn},
476 {
"port", (attrFn) portfn},
477 {
"rows", (attrFn) rowsfn},
478 {
"sides", (attrFn) sidesfn},
479 {
"style", (attrFn) stylefn},
480 {
"target", (attrFn) targetfn},
481 {
"title", (attrFn) titlefn},
482 {
"tooltip", (attrFn) titlefn},
483 {
"valign", (attrFn) valignfn},
484 {
"width", (attrFn) widthfn},
487static attr_item cell_items[] = {
488 {
"align", (attrFn) cell_halignfn},
489 {
"balign", (attrFn) balignfn},
490 {
"bgcolor", (attrFn) bgcolorfn},
491 {
"border", (attrFn) borderfn},
492 {
"cellpadding", (attrFn) cellpaddingfn},
493 {
"cellspacing", (attrFn) cellspacingfn},
494 {
"color", (attrFn) pencolorfn},
495 {
"colspan", (attrFn) colspanfn},
496 {
"fixedsize", (attrFn) fixedsizefn},
497 {
"gradientangle", (attrFn) gradientanglefn},
498 {
"height", (attrFn) heightfn},
499 {
"href", (attrFn) hreffn},
500 {
"id", (attrFn) idfn},
501 {
"port", (attrFn) portfn},
502 {
"rowspan", (attrFn) rowspanfn},
503 {
"sides", (attrFn) sidesfn},
504 {
"style", (attrFn) stylefn},
505 {
"target", (attrFn) targetfn},
506 {
"title", (attrFn) titlefn},
507 {
"tooltip", (attrFn) titlefn},
508 {
"valign", (attrFn) valignfn},
509 {
"width", (attrFn) widthfn},
512static attr_item font_items[] = {
513 {
"color", (attrFn) fontcolorfn},
514 {
"face", (attrFn) facefn},
515 {
"point-size", (attrFn) ptsizefn},
518static attr_item img_items[] = {
519 {
"scale", (attrFn) scalefn},
520 {
"src", (attrFn) srcfn},
523static attr_item br_items[] = {
524 {
"align", (attrFn) alignfn},
535static void doAttrs(
htmllexstate_t *ctx,
void *tp, attr_item *items,
size_t nel,
char **atts,
541 while ((name = *atts++) !=
NULL) {
543 ip = bsearch(name, items, nel, ISIZE, icmp);
545 ctx->
warn |= ip->action(tp, val);
547 agwarningf(
"Illegal attribute %s in %s - ignored\n", name,
557 doAttrs(ctx, &ctx->
htmllval->
i, br_items,
sizeof(br_items) / ISIZE, atts,
"<BR>");
564 doAttrs(ctx, img, img_items,
sizeof(img_items) / ISIZE, atts,
"<IMG>");
574 assert(
flags <= FLAGS_MAX);
575 tf.
flags = (
unsigned char)(
flags & FLAGS_MAX);
577 doAttrs(ctx, &tf, font_items,
sizeof(font_items) / ISIZE, atts,
"<FONT>");
588 doAttrs(ctx,
cell, cell_items,
sizeof(cell_items) / ISIZE, atts,
"<TD>");
600 doAttrs(ctx, tbl, tbl_items,
sizeof(tbl_items) / ISIZE, atts,
"<TABLE>");
605static void startElement(
void *user,
const char *name,
char **atts)
609 if (strcasecmp(name,
"TABLE") == 0) {
613 }
else if (strcasecmp(name,
"TR") == 0 || strcasecmp(name,
"TH") == 0) {
616 }
else if (strcasecmp(name,
"TD") == 0) {
620 }
else if (strcasecmp(name,
"FONT") == 0) {
623 }
else if (strcasecmp(name,
"B") == 0) {
626 }
else if (strcasecmp(name,
"S") == 0) {
629 }
else if (strcasecmp(name,
"U") == 0) {
632 }
else if (strcasecmp(name,
"O") == 0) {
635 }
else if (strcasecmp(name,
"I") == 0) {
638 }
else if (strcasecmp(name,
"SUP") == 0) {
641 }
else if (strcasecmp(name,
"SUB") == 0) {
644 }
else if (strcasecmp(name,
"BR") == 0) {
647 }
else if (strcasecmp(name,
"HR") == 0) {
649 }
else if (strcasecmp(name,
"VR") == 0) {
651 }
else if (strcasecmp(name,
"IMG") == 0) {
654 }
else if (strcasecmp(name,
"HTML") == 0) {
661static void endElement(
void *user,
const char *name)
665 if (strcasecmp(name,
"TABLE") == 0) {
668 }
else if (strcasecmp(name,
"TR") == 0 || strcasecmp(name,
"TH") == 0) {
670 }
else if (strcasecmp(name,
"TD") == 0) {
673 }
else if (strcasecmp(name,
"HTML") == 0) {
675 }
else if (strcasecmp(name,
"FONT") == 0) {
677 }
else if (strcasecmp(name,
"B") == 0) {
679 }
else if (strcasecmp(name,
"U") == 0) {
681 }
else if (strcasecmp(name,
"O") == 0) {
683 }
else if (strcasecmp(name,
"I") == 0) {
685 }
else if (strcasecmp(name,
"SUP") == 0) {
687 }
else if (strcasecmp(name,
"SUB") == 0) {
689 }
else if (strcasecmp(name,
"S") == 0) {
691 }
else if (strcasecmp(name,
"BR") == 0) {
696 }
else if (strcasecmp(name,
"HR") == 0) {
701 }
else if (strcasecmp(name,
"VR") == 0) {
706 }
else if (strcasecmp(name,
"IMG") == 0) {
723static void characterData(
void *user,
const char *
s,
int length)
731 for (i =
length; i; i--) {
759 XML_SetUserData(ctx->parser, ctx);
760 XML_SetElementHandler(ctx->parser,
761 (XML_StartElementHandler) startElement,
763 XML_SetCharacterDataHandler(ctx->parser, characterData);
774 "Not built with libexpat. Table formatting is not available.\n");
786 XML_ParserFree(ctx->parser);
818 while (depth && (c = *
s++)) {
846 t = eatComment(ctx, t + 3);
848 while (*t && *t !=
'>')
851 agwarningf(
"Label closed before end of HTML element\n");
857 while ((c = *t) && c !=
'<') {
858 if (c ==
'&' && *(t+1) !=
'#') {
886static void protect_rsqb(
agxbuf *xb) {
895 size_t size = strlen(data);
897 if (data[size - 1] !=
']') {
903 data[size - 1] =
'\0';
918 return XML_GetCurrentLineNumber(ctx->parser);
1050 const char *token_text =
agxbuse(ctx->
xb);
1051 fprintf(stderr,
"%s \"%s\"\n",
s, token_text);
1054 fprintf(stderr,
"%s\n",
s);
1062 static char *begin_html =
"<HTML>";
1063 static char *end_html =
"</HTML>";
1076 if (ctx->
mode == 0) {
1088 endp = findNext(ctx,
s,&ctx->
lb);
1089 len = (size_t)(endp -
s);
1093 protect_rsqb(&ctx->
lb);
1098 assert(llen <= INT_MAX &&
"XML token too long for expat API");
1099 rv = XML_Parse(ctx->parser,
agxbuse(&ctx->
lb), (
int)llen, 0);
1101 assert(
len <= INT_MAX &&
"XML token too long for expat API");
1102 rv = XML_Parse(ctx->parser,
s, (
int)
len,
len ? 0 : 1);
1115 }
while (ctx->
tok == 0);
1117 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