35#define XML_USE_MSC_EXTENSIONS 1
40#ifndef XML_STATUS_ERROR
41#define XML_STATUS_ERROR 0
79typedef int (*attrFn) (
void *,
char *);
80typedef int (*bcmpfn) (
const void *,
const void *);
88#define ISIZE (sizeof(attr_item))
93static int icmp(
const void *name,
const void *
item) {
94 const attr_item *j =
item;
95 return strcasecmp(name, j->name);
118 unsigned short flags = 0;
136 agwarningf(
"Unrecognized character '%c' (%d) in sides attribute\n", c, c);
147 p->
title = strdup(v);
174 agwarningf(
"Illegal value %.*s for STYLE - ignored\n", (
int)tk.
size,
201static int doInt(
char *v,
char *
s,
int min,
int max,
long *ul)
205 long b = strtol(v, &ep, 10);
208 agwarningf(
"Improper %s value %s - ignored",
s, v);
210 }
else if (b > max) {
211 agwarningf(
"%s value %s > %d - too large - ignored",
s, v, max);
213 }
else if (b < min) {
214 agwarningf(
"%s value %s < %d - too small - ignored",
s, v, min);
222static int gradientanglefn(
htmldata_t * p,
char *v)
226 if (doInt(v,
"GRADIENTANGLE", 0, 360, &u))
237 if (doInt(v,
"BORDER", 0, UCHAR_MAX, &u))
239 p->
border = (
unsigned char) u;
244static int cellpaddingfn(
htmldata_t * p,
char *v)
248 if (doInt(v,
"CELLPADDING", 0, UCHAR_MAX, &u))
250 p->
pad = (
unsigned char) u;
255static int cellspacingfn(
htmldata_t * p,
char *v)
259 if (doInt(v,
"CELLSPACING", SCHAR_MIN, SCHAR_MAX, &u))
261 p->
space = (
signed char) u;
266static int cellborderfn(
htmltbl_t * p,
char *v)
270 if (doInt(v,
"CELLBORDER", 0,
INT8_MAX, &u))
276static int columnsfn(
htmltbl_t * p,
char *v)
279 agwarningf(
"Unknown value %s for COLUMNS - ignored\n", v);
289 agwarningf(
"Unknown value %s for ROWS - ignored\n", v);
296static int fixedsizefn(
htmldata_t * p,
char *v)
299 if (!strcasecmp(v,
"TRUE"))
301 else if (strcasecmp(v,
"FALSE")) {
302 agwarningf(
"Illegal value %s for FIXEDSIZE - ignored\n", v);
311 if (!strcasecmp(v,
"BOTTOM"))
313 else if (!strcasecmp(v,
"TOP"))
315 else if (strcasecmp(v,
"MIDDLE")) {
316 agwarningf(
"Illegal value %s for VALIGN - ignored\n", v);
325 if (!strcasecmp(v,
"LEFT"))
327 else if (!strcasecmp(v,
"RIGHT"))
329 else if (strcasecmp(v,
"CENTER")) {
330 agwarningf(
"Illegal value %s for ALIGN - ignored\n", v);
336static int cell_halignfn(
htmldata_t * p,
char *v)
339 if (!strcasecmp(v,
"LEFT"))
341 else if (!strcasecmp(v,
"RIGHT"))
343 else if (!strcasecmp(v,
"TEXT"))
345 else if (strcasecmp(v,
"CENTER"))
348 agwarningf(
"Illegal value %s for ALIGN in TD - ignored\n", v);
355 if (!strcasecmp(v,
"LEFT"))
357 else if (!strcasecmp(v,
"RIGHT"))
359 else if (strcasecmp(v,
"CENTER"))
362 agwarningf(
"Illegal value %s for BALIGN in TD - ignored\n", v);
370 if (doInt(v,
"HEIGHT", 0, USHRT_MAX, &u))
372 p->
height = (
unsigned short) u;
380 if (doInt(v,
"WIDTH", 0, USHRT_MAX, &u))
382 p->
width = (
unsigned short) u;
393 agwarningf(
"ROWSPAN value cannot be 0 - ignored\n");
407 agwarningf(
"COLSPAN value cannot be 0 - ignored\n");
414static int fontcolorfn(
textfont_t * p,
char *v)
430 if (doInt(v,
"POINT-SIZE", 0, UCHAR_MAX, &u))
432 p->
size = (double) u;
442static int scalefn(
htmlimg_t * p,
char *v)
444 p->
scale = strdup(v);
448static int alignfn(
int *p,
char *v)
451 if (!strcasecmp(v,
"RIGHT"))
453 else if (!strcasecmp(v,
"LEFT"))
455 else if (!strcasecmp(v,
"CENTER"))
458 agwarningf(
"Illegal value %s for ALIGN - ignored\n", v);
465static attr_item tbl_items[] = {
466 {
"align", (attrFn) halignfn},
467 {
"bgcolor", (attrFn) bgcolorfn},
468 {
"border", (attrFn) borderfn},
469 {
"cellborder", (attrFn) cellborderfn},
470 {
"cellpadding", (attrFn) cellpaddingfn},
471 {
"cellspacing", (attrFn) cellspacingfn},
472 {
"color", (attrFn) pencolorfn},
473 {
"columns", (attrFn) columnsfn},
474 {
"fixedsize", (attrFn) fixedsizefn},
475 {
"gradientangle", (attrFn) gradientanglefn},
476 {
"height", (attrFn) heightfn},
477 {
"href", (attrFn) hreffn},
478 {
"id", (attrFn) idfn},
479 {
"port", (attrFn) portfn},
480 {
"rows", (attrFn) rowsfn},
481 {
"sides", (attrFn) sidesfn},
482 {
"style", (attrFn) stylefn},
483 {
"target", (attrFn) targetfn},
484 {
"title", (attrFn) titlefn},
485 {
"tooltip", (attrFn) titlefn},
486 {
"valign", (attrFn) valignfn},
487 {
"width", (attrFn) widthfn},
490static attr_item cell_items[] = {
491 {
"align", (attrFn) cell_halignfn},
492 {
"balign", (attrFn) balignfn},
493 {
"bgcolor", (attrFn) bgcolorfn},
494 {
"border", (attrFn) borderfn},
495 {
"cellpadding", (attrFn) cellpaddingfn},
496 {
"cellspacing", (attrFn) cellspacingfn},
497 {
"color", (attrFn) pencolorfn},
498 {
"colspan", (attrFn) colspanfn},
499 {
"fixedsize", (attrFn) fixedsizefn},
500 {
"gradientangle", (attrFn) gradientanglefn},
501 {
"height", (attrFn) heightfn},
502 {
"href", (attrFn) hreffn},
503 {
"id", (attrFn) idfn},
504 {
"port", (attrFn) portfn},
505 {
"rowspan", (attrFn) rowspanfn},
506 {
"sides", (attrFn) sidesfn},
507 {
"style", (attrFn) stylefn},
508 {
"target", (attrFn) targetfn},
509 {
"title", (attrFn) titlefn},
510 {
"tooltip", (attrFn) titlefn},
511 {
"valign", (attrFn) valignfn},
512 {
"width", (attrFn) widthfn},
515static attr_item font_items[] = {
516 {
"color", (attrFn) fontcolorfn},
517 {
"face", (attrFn) facefn},
518 {
"point-size", (attrFn) ptsizefn},
521static attr_item img_items[] = {
522 {
"scale", (attrFn) scalefn},
523 {
"src", (attrFn) srcfn},
526static attr_item br_items[] = {
527 {
"align", (attrFn) alignfn},
538static void doAttrs(
htmllexstate_t *ctx,
void *tp, attr_item *items,
size_t nel,
char **atts,
544 while ((name = *atts++) !=
NULL) {
546 ip = bsearch(name, items, nel, ISIZE, icmp);
548 ctx->
warn |= ip->action(tp, val);
550 agwarningf(
"Illegal attribute %s in %s - ignored\n", name,
560 doAttrs(ctx, &ctx->
htmllval->
i, br_items,
sizeof(br_items) / ISIZE, atts,
"<BR>");
567 doAttrs(ctx, img, img_items,
sizeof(img_items) / ISIZE, atts,
"<IMG>");
577 assert(
flags <= FLAGS_MAX);
578 tf.
flags = (
unsigned char)(
flags & FLAGS_MAX);
580 doAttrs(ctx, &tf, font_items,
sizeof(font_items) / ISIZE, atts,
"<FONT>");
591 doAttrs(ctx,
cell, cell_items,
sizeof(cell_items) / ISIZE, atts,
"<TD>");
602 doAttrs(ctx, tbl, tbl_items,
sizeof(tbl_items) / ISIZE, atts,
"<TABLE>");
607static void startElement(
void *user,
const char *name,
char **atts)
611 if (strcasecmp(name,
"TABLE") == 0) {
615 }
else if (strcasecmp(name,
"TR") == 0 || strcasecmp(name,
"TH") == 0) {
618 }
else if (strcasecmp(name,
"TD") == 0) {
622 }
else if (strcasecmp(name,
"FONT") == 0) {
625 }
else if (strcasecmp(name,
"B") == 0) {
628 }
else if (strcasecmp(name,
"S") == 0) {
631 }
else if (strcasecmp(name,
"U") == 0) {
634 }
else if (strcasecmp(name,
"O") == 0) {
637 }
else if (strcasecmp(name,
"I") == 0) {
640 }
else if (strcasecmp(name,
"SUP") == 0) {
643 }
else if (strcasecmp(name,
"SUB") == 0) {
646 }
else if (strcasecmp(name,
"BR") == 0) {
649 }
else if (strcasecmp(name,
"HR") == 0) {
651 }
else if (strcasecmp(name,
"VR") == 0) {
653 }
else if (strcasecmp(name,
"IMG") == 0) {
656 }
else if (strcasecmp(name,
"HTML") == 0) {
663static void endElement(
void *user,
const char *name)
667 if (strcasecmp(name,
"TABLE") == 0) {
670 }
else if (strcasecmp(name,
"TR") == 0 || strcasecmp(name,
"TH") == 0) {
672 }
else if (strcasecmp(name,
"TD") == 0) {
675 }
else if (strcasecmp(name,
"HTML") == 0) {
677 }
else if (strcasecmp(name,
"FONT") == 0) {
679 }
else if (strcasecmp(name,
"B") == 0) {
681 }
else if (strcasecmp(name,
"U") == 0) {
683 }
else if (strcasecmp(name,
"O") == 0) {
685 }
else if (strcasecmp(name,
"I") == 0) {
687 }
else if (strcasecmp(name,
"SUP") == 0) {
689 }
else if (strcasecmp(name,
"SUB") == 0) {
691 }
else if (strcasecmp(name,
"S") == 0) {
693 }
else if (strcasecmp(name,
"BR") == 0) {
698 }
else if (strcasecmp(name,
"HR") == 0) {
703 }
else if (strcasecmp(name,
"VR") == 0) {
708 }
else if (strcasecmp(name,
"IMG") == 0) {
725static void characterData(
void *user,
const char *
s,
int length)
733 for (i = length; i; i--) {
761 XML_SetUserData(ctx->parser, ctx);
762 XML_SetElementHandler(ctx->parser,
763 (XML_StartElementHandler) startElement,
765 XML_SetCharacterDataHandler(ctx->parser, characterData);
771 "Not built with libexpat. Table formatting is not available.\n");
783 XML_ParserFree(ctx->parser);
813 while (depth && (c = *
s++)) {
841 t = eatComment(ctx, t + 3);
843 while (*t && *t !=
'>')
846 agwarningf(
"Label closed before end of HTML element\n");
852 while ((c = *t) && c !=
'<') {
853 if (c ==
'&' && *(t+1) !=
'#') {
881static void protect_rsqb(
agxbuf *xb) {
890 size_t size = strlen(
data);
892 if (
data[size - 1] !=
']') {
898 data[size - 1] =
'\0';
913 return XML_GetCurrentLineNumber(ctx->parser);
1043 const char *token_text =
agxbuse(ctx->
xb);
1044 fprintf(stderr,
"%s \"%s\"\n",
s, token_text);
1047 fprintf(stderr,
"%s\n",
s);
1055 static char *begin_html =
"<HTML>";
1056 static char *end_html =
"</HTML>";
1069 if (ctx->
mode == 0) {
1081 endp = findNext(ctx,
s,&ctx->
lb);
1082 len = (size_t)(endp -
s);
1086 protect_rsqb(&ctx->
lb);
1091 assert(llen <= (
size_t)INT_MAX &&
"XML token too long for expat API");
1092 rv = XML_Parse(ctx->parser,
agxbuse(&ctx->
lb), (
int)llen, 0);
1094 assert(
len <= (
size_t)INT_MAX &&
"XML token too long for expat API");
1095 rv = XML_Parse(ctx->parser,
s, (
int)
len,
len ? 0 : 1);
1108 }
while (ctx->
tok == 0);
1110 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 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 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