38#ifndef XML_STATUS_ERROR
39#define XML_STATUS_ERROR 0
78static int icmp(
const void *name,
const void *
item) {
79 const char *
const *
const j =
item;
80 return strcasecmp(name, *j);
83static int bgcolorfn(
htmldata_t *p,
const char *v) {
88static int pencolorfn(
htmldata_t *p,
const char *v) {
93static int hreffn(
htmldata_t *p,
const char *v) {
98static int sidesfn(
htmldata_t *p,
const char *v) {
99 unsigned short flags = 0;
117 agwarningf(
"Unrecognized character '%c' (%d) in sides attribute\n", c, c);
126static int titlefn(
htmldata_t *p,
const char *v) {
127 p->
title = strdup(v);
131static int portfn(
htmldata_t *p,
const char *v) {
138static 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) {
177static int doInt(
const char *v,
char *
s,
int min,
int max,
long *ul) {
180 long b = strtol(v, &ep, 10);
183 agwarningf(
"Improper %s value %s - ignored",
s, v);
185 }
else if (b > max) {
186 agwarningf(
"%s value %s > %d - too large - ignored",
s, v, max);
188 }
else if (b < min) {
189 agwarningf(
"%s value %s < %d - too small - ignored",
s, v, min);
197static int gradientanglefn(
htmldata_t *p,
const char *v) {
200 if (doInt(v,
"GRADIENTANGLE", 0, 360, &u))
207static int borderfn(
htmldata_t *p,
const char *v) {
210 if (doInt(v,
"BORDER", 0, UCHAR_MAX, &u))
212 p->
border = (
unsigned char) u;
217static int cellpaddingfn(
htmldata_t *p,
const char *v) {
220 if (doInt(v,
"CELLPADDING", 0, UCHAR_MAX, &u))
222 p->
pad = (
unsigned char) u;
227static int cellspacingfn(
htmldata_t *p,
const char *v) {
230 if (doInt(v,
"CELLSPACING", SCHAR_MIN, SCHAR_MAX, &u))
232 p->
space = (
signed char) u;
237static int cellborderfn(
htmldata_t *data,
const char *v) {
241 if (doInt(v,
"CELLBORDER", 0,
INT8_MAX, &u))
247static int columnsfn(
htmldata_t *data,
const char *v) {
250 agwarningf(
"Unknown value %s for COLUMNS - ignored\n", v);
257static 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) {
357 agwarningf(
"ROWSPAN value cannot be 0 - ignored\n");
364static int colspanfn(
htmldata_t *data,
const char *v) {
371 agwarningf(
"COLSPAN value cannot be 0 - ignored\n");
378static int fontcolorfn(
textfont_t *p,
const char *v) {
380 p->
color = (
char *)v;
384static int facefn(
textfont_t *p,
const char *v) {
390static int ptsizefn(
textfont_t *p,
const char *v) {
393 if (doInt(v,
"POINT-SIZE", 0, UCHAR_MAX, &u))
395 p->
size = (double) u;
399static int srcfn(
htmlimg_t *p,
const char *v) {
404static int scalefn(
htmlimg_t *p,
const char *v) {
405 p->
scale = strdup(v);
409static int alignfn(
int *p,
const char *v) {
411 if (!strcasecmp(v,
"RIGHT"))
413 else if (!strcasecmp(v,
"LEFT"))
415 else if (!strcasecmp(v,
"CENTER"))
418 agwarningf(
"Illegal value %s for ALIGN - ignored\n", v);
430static html_item_t tbl_items[] = {
432 {
"bgcolor", bgcolorfn},
433 {
"border", borderfn},
434 {
"cellborder", cellborderfn},
435 {
"cellpadding", cellpaddingfn},
436 {
"cellspacing", cellspacingfn},
437 {
"color", pencolorfn},
438 {
"columns", columnsfn},
439 {
"fixedsize", fixedsizefn},
440 {
"gradientangle", gradientanglefn},
441 {
"height", heightfn},
448 {
"target", targetfn},
450 {
"tooltip", titlefn},
451 {
"valign", valignfn},
455static html_item_t cell_items[] = {
456 {
"align", cell_halignfn},
457 {
"balign", balignfn},
458 {
"bgcolor", bgcolorfn},
459 {
"border", borderfn},
460 {
"cellpadding", cellpaddingfn},
461 {
"cellspacing", cellspacingfn},
462 {
"color", pencolorfn},
463 {
"colspan", colspanfn},
464 {
"fixedsize", fixedsizefn},
465 {
"gradientangle", gradientanglefn},
466 {
"height", heightfn},
470 {
"rowspan", rowspanfn},
473 {
"target", targetfn},
475 {
"tooltip", titlefn},
476 {
"valign", valignfn},
485static font_item_t font_items[] = {
486 {
"color", fontcolorfn},
488 {
"point-size", ptsizefn},
493 int (*action)(
htmlimg_t *,
const char *);
496static img_item_t img_items[] = {
503 int (*action)(
int *,
const char *);
506static br_item_t br_items[] = {
514#define CALL_ACTION(list, elem, tp, val) \
515 (_Generic((list), html_item_t * \
516 : (html_item_t *)(elem), font_item_t * \
517 : (font_item_t *)(elem), img_item_t * \
518 : (img_item_t *)(elem), br_item_t * \
519 : (br_item_t *)(elem)) \
520 ->action((tp), (val)))
530#define doAttrs(ctx, tp, items, nel, atts, s) do { \
533 while ((name = *(atts)++) != NULL) { \
534 const char *val = *(atts)++; \
535 void *const ip = bsearch(name, (items), (nel), sizeof((items)[0]), icmp); \
537 (ctx)->warn |= CALL_ACTION((items), ip, (tp), val); \
539 agwarningf("Illegal attribute %s in %s - ignored\n", name, \
548 doAttrs(ctx, &ctx->
htmllval->
i, br_items,
sizeof(br_items) /
sizeof(br_items[0]), atts,
"<BR>");
554 doAttrs(ctx, img, img_items,
sizeof(img_items) /
sizeof(img_items[0]), atts,
"<IMG>");
564 assert(
flags <= FLAGS_MAX);
565 tf.
flags = (
unsigned char)(
flags & FLAGS_MAX);
567 doAttrs(ctx, &tf, font_items,
sizeof(font_items) /
sizeof(font_items[0]), atts,
"<FONT>");
577 doAttrs(ctx, &
cell->data, cell_items,
sizeof(cell_items) /
sizeof(cell_items[0]), atts,
"<TD>");
588 doAttrs(ctx, &tbl->
data, tbl_items,
sizeof(tbl_items) /
sizeof(tbl_items[0]), atts,
"<TABLE>");
593static void startElement(
void *user,
const char *name,
const char **atts) {
596 if (strcasecmp(name,
"TABLE") == 0) {
600 }
else if (strcasecmp(name,
"TR") == 0 || strcasecmp(name,
"TH") == 0) {
603 }
else if (strcasecmp(name,
"TD") == 0) {
607 }
else if (strcasecmp(name,
"FONT") == 0) {
610 }
else if (strcasecmp(name,
"B") == 0) {
613 }
else if (strcasecmp(name,
"S") == 0) {
616 }
else if (strcasecmp(name,
"U") == 0) {
619 }
else if (strcasecmp(name,
"O") == 0) {
622 }
else if (strcasecmp(name,
"I") == 0) {
625 }
else if (strcasecmp(name,
"SUP") == 0) {
628 }
else if (strcasecmp(name,
"SUB") == 0) {
631 }
else if (strcasecmp(name,
"BR") == 0) {
634 }
else if (strcasecmp(name,
"HR") == 0) {
636 }
else if (strcasecmp(name,
"VR") == 0) {
638 }
else if (strcasecmp(name,
"IMG") == 0) {
641 }
else if (strcasecmp(name,
"HTML") == 0) {
648static void endElement(
void *user,
const char *name)
652 if (strcasecmp(name,
"TABLE") == 0) {
655 }
else if (strcasecmp(name,
"TR") == 0 || strcasecmp(name,
"TH") == 0) {
657 }
else if (strcasecmp(name,
"TD") == 0) {
660 }
else if (strcasecmp(name,
"HTML") == 0) {
662 }
else if (strcasecmp(name,
"FONT") == 0) {
664 }
else if (strcasecmp(name,
"B") == 0) {
666 }
else if (strcasecmp(name,
"U") == 0) {
668 }
else if (strcasecmp(name,
"O") == 0) {
670 }
else if (strcasecmp(name,
"I") == 0) {
672 }
else if (strcasecmp(name,
"SUP") == 0) {
674 }
else if (strcasecmp(name,
"SUB") == 0) {
676 }
else if (strcasecmp(name,
"S") == 0) {
678 }
else if (strcasecmp(name,
"BR") == 0) {
683 }
else if (strcasecmp(name,
"HR") == 0) {
688 }
else if (strcasecmp(name,
"VR") == 0) {
693 }
else if (strcasecmp(name,
"IMG") == 0) {
710static void characterData(
void *user,
const char *
s,
int length)
718 for (i =
length; i; i--) {
746 XML_SetUserData(ctx->parser, ctx);
747 XML_SetElementHandler(ctx->parser, startElement, endElement);
748 XML_SetCharacterDataHandler(ctx->parser, characterData);
756 static atomic_flag first;
757 if (!atomic_flag_test_and_set(&first)) {
759 "Not built with libexpat. Table formatting is not available.\n");
770 XML_ParserFree(ctx->parser);
802 while (depth && (c = *
s++)) {
830 t = eatComment(ctx, t + 3);
832 while (*t && *t !=
'>')
835 agwarningf(
"Label closed before end of HTML element\n");
841 while ((c = *t) && c !=
'<') {
842 if (c ==
'&' && *(t+1) !=
'#') {
870static void protect_rsqb(
agxbuf *xb) {
879 size_t size = strlen(data);
881 if (data[size - 1] !=
']') {
887 data[size - 1] =
'\0';
902 return XML_GetCurrentLineNumber(ctx->parser);
1034 const char *token_text =
agxbuse(ctx->
xb);
1035 fprintf(stderr,
"%s \"%s\"\n",
s, token_text);
1038 fprintf(stderr,
"%s\n",
s);
1046 static char *begin_html =
"<HTML>";
1047 static char *end_html =
"</HTML>";
1060 if (ctx->
mode == 0) {
1072 endp = findNext(ctx,
s,&ctx->
lb);
1073 len = (size_t)(endp -
s);
1077 protect_rsqb(&ctx->
lb);
1082 assert(llen <= INT_MAX &&
"XML token too long for expat API");
1083 rv = XML_Parse(ctx->parser,
agxbuse(&ctx->
lb), (
int)llen, 0);
1085 assert(
len <= INT_MAX &&
"XML token too long for expat API");
1086 rv = XML_Parse(ctx->parser,
s, (
int)
len,
len ? 0 : 1);
1099 }
while (ctx->
tok == 0);
1101 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