1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
|
/*
* Copyright (c) Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#if HAVE_SYS_QUEUE
# include <sys/queue.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lowdown.h"
#include "extern.h"
TAILQ_HEAD(opq, op);
TAILQ_HEAD(op_resq, op_res);
TAILQ_HEAD(op_argq, op_arg);
enum op_type {
OP_FOR,
OP_IFDEF,
OP_ELSE,
OP_STR,
OP_EXPR,
OP_ROOT,
};
static const char *const op_types[OP_ROOT + 1] = {
"for",
"ifdef",
"else",
"str",
"expr",
"root",
};
/*
* Carry the result of an evaluation as an allocated string.
*/
struct op_res {
char *res;
TAILQ_ENTRY(op_res) entries;
};
/*
* An argument to a transformation.
*/
struct op_arg {
const char *arg;
size_t argsz;
TAILQ_ENTRY(op_arg) entries;
};
/*
* Operation for an opaque string.
*/
struct op_str {
const char *str; /* content */
size_t sz; /* length of content */
};
/*
* Operation for evaluating an expression.
*/
struct op_expr {
const char *expr; /* expression */
size_t sz; /* length of expression */
};
/*
* Operation for an ifdef conditional.
*/
struct op_ifdef {
const char *expr; /* expression */
size_t sz; /* length of expression */
const struct op *chain; /* "else" or NULL */
};
/*
* Operation for a for loop.
*/
struct op_for {
const char *expr;
size_t sz;
};
/*
* Operations are laid out in a tree under an OP_ROOT. Each block
* (OP_IFDEF, OP_ELSE, OP_FOR) introduces a sub-tree.
*/
struct op {
union {
struct op_for op_for;
struct op_ifdef op_ifdef;
struct op_str op_str;
struct op_expr op_expr;
};
enum op_type op_type; /* type */
struct opq children; /* if block, the children */
struct op *parent; /* parent (NULL for OP_ROOT) */
TAILQ_ENTRY(op) _siblings; /* siblings in block */
TAILQ_ENTRY(op) _all; /* queue of all operations */
};
struct op_out {
int debug;
size_t depth;
struct lowdown_buf *ob;
const struct lowdown_buf *content;
const struct lowdown_metaq *mq;
};
/* Forward declaration. */
static int op_exec(struct op_out *, const struct op *, const char *);
static struct op_resq *op_eval(struct op_out *, const char *, size_t,
const char *, const struct op_resq *);
static int op_debug(struct op_out *, const char *, ...)
__attribute__((format (printf, 2, 3)));
/*
* Allocate the generic members of "struct op". The caller should be
* initialising the type-specific members, e.g., "op_ifdef" for
* OP_IFDEF. Returns the pointer or NULL on allocation failure.
*/
static struct op *
op_alloc(struct opq *q, enum op_type type, struct op *cop)
{
struct op *op;
if ((op = calloc(1, sizeof(struct op))) == NULL)
return NULL;
TAILQ_INIT(&op->children);
op->op_type = type;
op->parent = cop;
TAILQ_INSERT_TAIL(q, op, _all);
if (cop != NULL)
TAILQ_INSERT_TAIL(&cop->children, op, _siblings);
return op;
}
/*
* Queue an expression-printing operation. Returns non-zero on success,
* zero on failure (allocation failure).
*/
static int
op_queue_expr(struct opq *q, struct op *cop, const char *expr,
size_t sz)
{
struct op *op;
if ((op = op_alloc(q, OP_EXPR, cop)) == NULL)
return 0;
op->op_expr.expr = expr;
op->op_expr.sz = sz;
return 1;
}
/*
* Queue an opaque string-printing operation. Returns non-zero on
* success, zero on failure (allocation failure).
*/
static int
op_queue_str(struct opq *q, struct op *cop, const char *str, size_t sz)
{
struct op *op;
if ((op = op_alloc(q, OP_STR, cop)) == NULL)
return 0;
op->op_str.str = str;
op->op_str.sz = sz;
return 1;
}
/*
* Queue the start of a conditional block. Returns non-zero on success,
* zero on failure (allocation failure).
*/
static int
op_queue_ifdef(struct opq *q, struct op **cop, const char *expr,
size_t sz)
{
struct op *op;
if ((op = op_alloc(q, OP_IFDEF, *cop)) == NULL)
return 0;
op->op_ifdef.expr = expr;
op->op_ifdef.sz = sz;
*cop = op;
return 1;
}
/*
* Queue the start of a for-loop block. Returns non-zero on success,
* zero on failure (allocation failure).
*/
static int
op_queue_for(struct opq *q, struct op **cop, const char *expr,
size_t sz)
{
struct op *op;
if ((op = op_alloc(q, OP_FOR, *cop)) == NULL)
return 0;
op->op_for.expr = expr;
op->op_for.sz = sz;
*cop = op;
return 1;
}
/*
* Queue the start of a conditional "else" block. If not currently in a
* conditional block, the "else" block will never execute. Returns
* non-zero on success, zero on failure (allocation failure).
*/
static int
op_queue_else(struct opq *q, struct op **cop)
{
struct op *op, *ifop = NULL;
if ((*cop)->op_type == OP_IFDEF) {
ifop = *cop;
*cop = (*cop)->parent;
}
if ((op = op_alloc(q, OP_ELSE, *cop)) == NULL)
return 0;
if (ifop != NULL) {
assert(ifop->op_ifdef.chain == NULL);
ifop->op_ifdef.chain = op;
}
*cop = op;
return 1;
}
/*
* Respond to ending a for-loop. If not currently in a loop block, does
* nothing. Returns non-zero always.
*/
static int
op_queue_endfor(struct op **cop)
{
if ((*cop)->op_type == OP_FOR)
*cop = (*cop)->parent;
return 1;
}
/*
* Respond to ending a conditional. If not currently in a conditional
* block ("ifdef" or "else"), does nothing. Returns non-zero always.
*/
static int
op_queue_endif(struct op **cop)
{
if ((*cop)->op_type == OP_IFDEF || (*cop)->op_type == OP_ELSE)
*cop = (*cop)->parent;
return 1;
}
/*
* Evaluate a statement as an expresion, conditional, etc. The current
* operation may be changed if entering or exiting a block. Returns
* zero on failure (memory allocation), non-zero on success.
*/
static int
op_queue(struct opq *q, struct op **cop, const char *str, size_t sz)
{
/* TODO: space before "(". */
if (sz > 6 && strncasecmp(str, "ifdef(", 6) == 0 &&
str[sz - 1] == ')')
return op_queue_ifdef(q, cop, str + 6, sz - 7);
if (sz > 4 && strncasecmp(str, "for(", 4) == 0 &&
str[sz - 1] == ')')
return op_queue_for(q, cop, str + 4, sz - 5);
if (sz == 4 && strncasecmp(str, "else", 4) == 0)
return op_queue_else(q, cop);
if (sz == 5 && strncasecmp(str, "endif", 5) == 0)
return op_queue_endif(cop);
if (sz == 6 && strncasecmp(str, "endfor", 6) == 0)
return op_queue_endfor(cop);
return op_queue_expr(q, *cop, str, sz);
}
/*
* If "debug" has been set, print out a message after a number of spaces
* proportionate to the debug depth.
* Return zero on failure (memory allocation), non-zero otherwise.
*/
static int
op_debug(struct op_out *out, const char *fmt, ...)
{
size_t i;
char buf[256];
int rc;
va_list ap;
if (!out->debug)
return 1;
for (i = 0; i < out->depth; i++)
if (!HBUF_PUTSL(out->ob, " "))
return 0;
va_start(ap, fmt);
rc = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (rc == -1)
return 0;
return hbuf_puts(out->ob, buf) && HBUF_PUTSL(out->ob, "\n");
}
static void
op_resq_free(struct op_resq *q)
{
struct op_res *res;
if (q == NULL)
return;
while ((res = TAILQ_FIRST(q)) != NULL) {
TAILQ_REMOVE(q, res, entries);
free(res->res);
free(res);
}
free(q);
}
static struct op_resq *
op_resq_clone(const struct op_resq *q, int trim)
{
struct op_resq *nq;
struct op_res *nres;
const struct op_res *res;
const char *start, *cp;
size_t sz;
if ((nq = malloc(sizeof(struct op_resq))) == NULL)
return NULL;
TAILQ_INIT(nq);
TAILQ_FOREACH(res, q, entries) {
sz = strlen(res->res);
start = res->res;
if (trim) {
for (cp = start; *cp != '\0'; cp++, sz--)
if (!isspace((unsigned char)*cp))
break;
if (*cp == '\0')
continue;
while (sz > 0 && isspace((unsigned char)cp[sz - 1]))
sz--;
if (sz == 0)
continue;
}
assert(sz > 0);
nres = calloc(1, sizeof(struct op_res));
if (nres == NULL) {
op_resq_free(nq);
return NULL;
}
TAILQ_INSERT_TAIL(nq, nres, entries);
nres->res = strndup(start, sz);
if (nres->res == NULL) {
op_resq_free(nq);
return NULL;
}
}
return nq;
}
static void
op_argq_free(struct op_argq *q)
{
struct op_arg *arg;
while ((arg = TAILQ_FIRST(q)) != NULL) {
TAILQ_REMOVE(q, arg, entries);
free(arg);
}
}
static int
op_argq_new(struct op_argq *q, const char *args, size_t argsz)
{
size_t i, start, substack = 0;
int inquot = 0;
struct op_arg *arg;
for (start = i = 0; i < argsz; i++) {
if (args[i] == '"') {
if (i == 0 || args[i - 1] != '\\')
inquot = !inquot;
continue;
} else if (args[i] == '(') {
substack++;
continue;
} else if (args[i] == ')') {
substack--;
continue;
} else if (args[i] != ',' || substack > 0 || inquot)
continue;
if ((arg = calloc(1, sizeof(struct op_arg))) == NULL)
return 0;
TAILQ_INSERT_TAIL(q, arg, entries);
arg->arg = &args[start];
arg->argsz = i - start;
start = i + 1;
}
if (i >= start) {
if ((arg = calloc(1, sizeof(struct op_arg))) == NULL)
return 0;
TAILQ_INSERT_TAIL(q, arg, entries);
arg->arg = &args[start];
arg->argsz = i - start;
start = i + 1;
}
return 1;
}
/*
* Split each input string along white-space boundaries. This trims
* white-space around all strings during the split. The result is a
* list of non-empty, non-only-whitespace strings. Returns NULL on
* allocation failure.
*/
static struct op_resq *
op_eval_function_split(struct op_out *out, const struct op_resq *input)
{
struct op_resq *nq = NULL;
struct op_res *nres, *nnres;
char *cp, *ncp;
if ((nq = op_resq_clone(input, 1)) == NULL)
goto err;
TAILQ_FOREACH(nres, nq, entries) {
for (cp = nres->res; *cp != '\0'; cp++)
if (!isspace((unsigned char)*cp))
break;
/* If this is only whitespace, ignore it. */
if (*cp == '\0')
continue;
/* Scan ahead until multiple whitespaces. */
for ( ; *cp != '\0'; cp++)
if (isspace((unsigned char)cp[0]) &&
isspace((unsigned char)cp[1]))
break;
/* Scan to next non-whitespace. */
for (ncp = cp; *ncp != '\0'; ncp++)
if (!isspace((unsigned char)*ncp))
break;
/* If no subsequent word, don't split. */
if (*ncp == '\0')
continue;
/* Split at white-space. */
*cp = '\0';
if ((nnres = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_AFTER(nq, nres, nnres, entries);
if ((nnres->res = strdup(ncp)) == NULL)
goto err;
}
return nq;
err:
op_resq_free(nq);
return NULL;
}
/*
* Escape all characters in all list items for HTML URL attributes.
* Returns NULL on allocation failure.
*/
static struct op_resq *
op_eval_function_escape_htmlurl(struct op_out *out,
const struct op_resq *input)
{
struct op_resq *nq = NULL;
struct op_res *nres;
const struct op_res *res;
struct lowdown_buf *buf = NULL;
if ((buf = hbuf_new(32)) == NULL)
goto err;
if ((nq = malloc(sizeof(struct op_resq))) == NULL)
goto err;
TAILQ_INIT(nq);
TAILQ_FOREACH(res, input, entries) {
hbuf_truncate(buf);
if (!lowdown_html_esc_href(buf, res->res, strlen(res->res)))
goto err;
if ((nres = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_TAIL(nq, nres, entries);
nres->res = strndup(buf->data, buf->size);
if (nres->res == NULL)
goto err;
}
hbuf_free(buf);
return nq;
err:
hbuf_free(buf);
op_resq_free(nq);
return NULL;
}
/*
* Escape all characters in all list items for HTML attributes.
* Returns NULL on allocation failure.
*/
static struct op_resq *
op_eval_function_escape_htmlattr(struct op_out *out,
const struct op_resq *input)
{
struct op_resq *nq = NULL;
struct op_res *nres;
const struct op_res *res;
struct lowdown_buf *buf = NULL;
if ((buf = hbuf_new(32)) == NULL)
goto err;
if ((nq = malloc(sizeof(struct op_resq))) == NULL)
goto err;
TAILQ_INIT(nq);
TAILQ_FOREACH(res, input, entries) {
hbuf_truncate(buf);
if (!lowdown_html_esc_attr(buf, res->res, strlen(res->res)))
goto err;
if ((nres = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_TAIL(nq, nres, entries);
nres->res = strndup(buf->data, buf->size);
if (nres->res == NULL)
goto err;
}
hbuf_free(buf);
return nq;
err:
hbuf_free(buf);
op_resq_free(nq);
return NULL;
}
/*
* HTML-escape (for general content) all characters in all list items.
* Returns NULL on allocation failure.
*/
static struct op_resq *
op_eval_function_escape_html(struct op_out *out,
const struct op_resq *input)
{
struct op_resq *nq = NULL;
struct op_res *nres;
const struct op_res *res;
struct lowdown_buf *buf = NULL;
if ((buf = hbuf_new(32)) == NULL)
goto err;
if ((nq = malloc(sizeof(struct op_resq))) == NULL)
goto err;
TAILQ_INIT(nq);
TAILQ_FOREACH(res, input, entries) {
hbuf_truncate(buf);
if (!lowdown_html_esc(buf, res->res, strlen(res->res),
1, 0, 0))
goto err;
if ((nres = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_TAIL(nq, nres, entries);
nres->res = strndup(buf->data, buf->size);
if (nres->res == NULL)
goto err;
}
hbuf_free(buf);
return nq;
err:
hbuf_free(buf);
op_resq_free(nq);
return NULL;
}
/*
* Escape all characters in all list items for general LaTeX content.
* Returns NULL on allocation failure.
*/
static struct op_resq *
op_eval_function_escape_latex(struct op_out *out,
const struct op_resq *input)
{
struct op_resq *nq = NULL;
struct op_res *nres;
const struct op_res *res;
struct lowdown_buf *buf = NULL;
if ((buf = hbuf_new(32)) == NULL)
goto err;
if ((nq = malloc(sizeof(struct op_resq))) == NULL)
goto err;
TAILQ_INIT(nq);
TAILQ_FOREACH(res, input, entries) {
hbuf_truncate(buf);
if (!lowdown_latex_esc(buf, res->res, strlen(res->res)))
goto err;
if ((nres = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_TAIL(nq, nres, entries);
nres->res = strndup(buf->data, buf->size);
if (nres->res == NULL)
goto err;
}
hbuf_free(buf);
return nq;
err:
hbuf_free(buf);
op_resq_free(nq);
return NULL;
}
/*
* Escape all characters in all list items for Gemini, either over
* multiple lines or with newlines removed for a single line.
* Returns NULL on allocation failure.
*/
static struct op_resq *
op_eval_function_escape_gemini(struct op_out *out,
const struct op_resq *input, int oneline)
{
struct op_resq *nq = NULL;
struct op_res *nres;
const struct op_res *res;
struct lowdown_buf *buf = NULL;
if ((buf = hbuf_new(32)) == NULL)
goto err;
if ((nq = malloc(sizeof(struct op_resq))) == NULL)
goto err;
TAILQ_INIT(nq);
TAILQ_FOREACH(res, input, entries) {
hbuf_truncate(buf);
if (!lowdown_gemini_esc(buf, res->res, strlen(res->res),
oneline))
goto err;
if ((nres = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_TAIL(nq, nres, entries);
nres->res = strndup(buf->data, buf->size);
if (nres->res == NULL)
goto err;
}
hbuf_free(buf);
return nq;
err:
hbuf_free(buf);
op_resq_free(nq);
return NULL;
}
/*
* Escape all characters in all list items for roff (ms/man), either
* over multiple lines or with newlines removed for a single line.
* Returns NULL on allocation failure.
*/
static struct op_resq *
op_eval_function_escape_roff(struct op_out *out,
const struct op_resq *input, int oneline)
{
struct op_resq *nq = NULL;
struct op_res *nres;
const struct op_res *res;
struct lowdown_buf *buf = NULL;
if ((buf = hbuf_new(32)) == NULL)
goto err;
if ((nq = malloc(sizeof(struct op_resq))) == NULL)
goto err;
TAILQ_INIT(nq);
TAILQ_FOREACH(res, input, entries) {
hbuf_truncate(buf);
if (!lowdown_nroff_esc(buf, res->res, strlen(res->res),
oneline, 0))
goto err;
if ((nres = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_TAIL(nq, nres, entries);
nres->res = strndup(buf->data, buf->size);
if (nres->res == NULL)
goto err;
}
hbuf_free(buf);
return nq;
err:
hbuf_free(buf);
op_resq_free(nq);
return NULL;
}
/*
* Lowercase or uppercase all characters in all list items. Returns
* NULL on allocation failure.
*/
static struct op_resq *
op_eval_function_case(struct op_out *out, const struct op_resq *input,
int lower)
{
struct op_resq *nq;
struct op_res *nres;
char *cp;
if ((nq = op_resq_clone(input, 0)) == NULL)
return NULL;
TAILQ_FOREACH(nres, nq, entries)
for (cp = nres->res; *cp != '\0'; cp++)
*cp = lower ?
tolower((unsigned char)*cp) :
toupper((unsigned char)*cp);
return nq;
}
/*
* Join all list items into a singleton, with two white-spaces
* delimiting the new string. If the input list is empty, produces an
* empty output. Returns NULL on allocation failure.
*/
static struct op_resq *
op_eval_function_join(struct op_out *out, const struct op_resq *input)
{
struct op_resq *nq = NULL;
struct op_res *nres;
const struct op_res *res;
size_t sz = 0;
void *p;
if ((nq = malloc(sizeof(struct op_resq))) == NULL)
goto err;
TAILQ_INIT(nq);
/* Empty list -> empty singleton. */
if (TAILQ_EMPTY(input))
return nq;
if ((nres = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_TAIL(nq, nres, entries);
TAILQ_FOREACH(res, input, entries) {
if (sz == 0) {
if ((nres->res = strdup(res->res)) == NULL)
goto err;
sz = strlen(nres->res) + 1;
} else {
sz += strlen(res->res) + 2;
if ((p = realloc(nres->res, sz)) == NULL)
goto err;
nres->res = p;
strlcat(nres->res, " ", sz);
strlcat(nres->res, res->res, sz);
}
}
assert(sz > 0);
return nq;
err:
op_resq_free(nq);
return NULL;
}
static struct op_resq *
op_eval_function(struct op_out *out, const char *expr, size_t exprsz,
const char *args, size_t argsz, const struct op_resq *input)
{
struct op_resq *nq;
if (!op_debug(out, "%s: %.*s", __func__, (int)exprsz, expr))
return NULL;
out->depth++;
if (exprsz == 9 && strncasecmp(expr, "uppercase", 9) == 0)
nq = op_eval_function_case(out, input, 0);
else if (exprsz == 9 && strncasecmp(expr, "lowercase", 9) == 0)
nq = op_eval_function_case(out, input, 1);
else if (exprsz == 5 && strncasecmp(expr, "split", 5) == 0)
nq = op_eval_function_split(out, input);
else if (exprsz == 4 && strncasecmp(expr, "join", 4) == 0)
nq = op_eval_function_join(out, input);
else if (exprsz == 4 && strncasecmp(expr, "trim", 4) == 0)
nq = op_resq_clone(input, 1);
else if (exprsz == 12 && strncasecmp(expr, "escapegemini", 12) == 0)
nq = op_eval_function_escape_gemini(out, input, 0);
else if (exprsz == 16 && strncasecmp(expr, "escapegeminiline", 16) == 0)
nq = op_eval_function_escape_gemini(out, input, 1);
else if (exprsz == 10 && strncasecmp(expr, "escapehtml", 10) == 0)
nq = op_eval_function_escape_html(out, input);
else if (exprsz == 14 && strncasecmp(expr, "escapehtmlattr", 14) == 0)
nq = op_eval_function_escape_htmlattr(out, input);
else if (exprsz == 13 && strncasecmp(expr, "escapehtmlurl", 13) == 0)
nq = op_eval_function_escape_htmlurl(out, input);
else if (exprsz == 11 && strncasecmp(expr, "escapelatex", 11) == 0)
nq = op_eval_function_escape_latex(out, input);
else if (exprsz == 10 && strncasecmp(expr, "escaperoff", 10) == 0)
nq = op_eval_function_escape_roff(out, input, -1);
else if (exprsz == 14 && strncasecmp(expr, "escaperoffline", 14) == 0)
nq = op_eval_function_escape_roff(out, input, 1);
else {
if (!op_debug(out, "transform not recognised"))
return NULL;
if ((nq = malloc(sizeof(struct op_resq))) != NULL)
TAILQ_INIT(nq);
}
out->depth--;
return nq;
}
/*
* The initial expression in an expression chain must resolve to a
* variable of some sort. This consists of metadata or "special"
* variables. Evaluate this variable to either a non-empty singleton or
* an empty list. Returns NULL on allocation failure.
*/
static struct op_resq *
op_eval_initial(struct op_out *out, const char *expr, size_t exprsz,
const char *args, size_t argsz, const char *this)
{
struct op_resq *q, *resq;
struct op_res *res;
struct op_argq argq;
struct op_arg *arg;
const struct lowdown_meta *m;
const char *v = NULL;
size_t vsz, i, j;
int rc;
if (!op_debug(out, "%s: %.*s", __func__, (int)exprsz, expr))
return NULL;
out->depth++;
if ((q = malloc(sizeof(struct op_resq))) == NULL)
return NULL;
TAILQ_INIT(&argq);
TAILQ_INIT(q);
if (exprsz > 1 && expr[0] == '"' && expr[exprsz - 1] == '"') {
v = &expr[1];
vsz = exprsz - 2;
if (vsz > 0) {
if ((res = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_TAIL(q, res, entries);
if ((res->res = malloc(vsz + 1)) == NULL)
goto err;
for (i = j = 0; i < vsz; i++)
if (!(v[i] == '\\' && v[i + 1] == '"'))
res->res[j++] = v[i];
res->res[j] = '\0';
op_argq_free(&argq);
out->depth--;
return q;
}
} else if (exprsz == 4 && strncasecmp(expr, "this", 4) == 0) {
/* Anaphoric keyword in current loop or NULL. */
v = this;
vsz = this == NULL ? 0 : strlen(this);
} else if (exprsz == 4 && strncasecmp(expr, "body", 4) == 0) {
/* Body of HTML document. */
v = out->content->data;
vsz = out->content->size;
} else if (exprsz == 3 && strncasecmp(expr, "not", 3) == 0) {
/* "NOT" of argument. The rest are ignored. */
resq = op_eval(out, args, argsz, this, NULL);
if (resq == NULL)
goto err;
rc = TAILQ_EMPTY(resq);
op_resq_free(resq);
if (rc == 1) {
v = "true";
vsz = 4;
}
} else if (exprsz == 2 && strncasecmp(expr, "or", 2) == 0) {
/* "OR" of all arguments. Short-circuit on TRUE. */
if (!op_argq_new(&argq, args, argsz))
goto err;
rc = 0;
TAILQ_FOREACH(arg, &argq, entries) {
resq = op_eval(out, arg->arg, arg->argsz, this,
NULL);
if (resq == NULL)
goto err;
rc = !TAILQ_EMPTY(resq);
op_resq_free(resq);
if (rc == 1)
break;
}
if (rc == 1) {
v = "true";
vsz = 4;
}
} else if (exprsz == 3 && strncasecmp(expr, "and", 3) == 0) {
/* "AND" of all arguments. Short-circuit on FALSE. */
if (!op_argq_new(&argq, args, argsz))
goto err;
rc = TAILQ_EMPTY(&argq) ? 0 : 1;
TAILQ_FOREACH(arg, &argq, entries) {
resq = op_eval(out, arg->arg, arg->argsz, this,
NULL);
if (resq == NULL)
goto err;
rc = !TAILQ_EMPTY(resq);
op_resq_free(resq);
if (rc == 0)
break;
}
if (rc != 0) {
v = "true";
vsz = 4;
}
} else {
/*
* If "meta", interpret argument as being a metadata
* key, allowing the use of the overridden names e.g.
* body.
*/
if (exprsz == 4 && strncasecmp(expr, "meta", 4) == 0 &&
args != NULL) {
if (!op_debug(out, "arg: %.*s",
(int)argsz, args))
goto err;
expr = args;
exprsz = argsz;
}
TAILQ_FOREACH(m, out->mq, entries)
if (strlen(m->key) == exprsz &&
strncasecmp(m->key, expr, exprsz) == 0) {
v = m->value;
vsz = strlen(m->value);
break;
}
}
op_argq_free(&argq);
out->depth--;
/* Invariant: non-empty or empty list. */
if (v == NULL || vsz == 0)
return q;
if ((res = calloc(1, sizeof(struct op_res))) == NULL)
goto err;
TAILQ_INSERT_TAIL(q, res, entries);
if ((res->res = strndup(v, vsz)) == NULL)
goto err;
return q;
err:
op_resq_free(q);
op_argq_free(&argq);
return NULL;
}
static struct op_resq *
op_eval(struct op_out *out, const char *expr, size_t sz,
const char *this, const struct op_resq *input)
{
size_t nextsz = 0, exprsz, argsz = 0, i;
const char *next = NULL, *args;
struct op_resq *q, *nextq;
int inquot = 0;
if (sz == 0) {
if ((q = malloc(sizeof(struct op_resq))) == NULL)
return NULL;
TAILQ_INIT(q);
return q;
}
/* Find next expression in chain. */
for (i = 0; next == NULL && i < sz; i++)
if (expr[i] == '"') {
if (i == 0 || expr[i - 1] != '\\')
inquot = !inquot;
} else if (expr[i] == '.' && !inquot)
next = &expr[i];
if (next != NULL) {
assert(next >= expr);
exprsz = (size_t)(next - expr);
next++;
assert(next > expr);
nextsz = sz - (size_t)(next - expr);
} else
exprsz = sz;
if (exprsz > 0 && expr[exprsz - 1] == ')' &&
((args = memchr(expr, '(', exprsz)) != NULL)) {
argsz = &expr[exprsz - 1] - args - 1;
exprsz = args - expr;
args++;
}
/*
* If input is NULL, this is the first of the chain, so it
* should resolve to a variable. Otherwise, it's a function.
*/
q = (input == NULL) ?
op_eval_initial(out, expr, exprsz, args, argsz, this) :
op_eval_function(out, expr, exprsz, args, argsz, input);
/* Return or pass to the next, then free current. */
if (next == NULL)
return q;
nextq = op_eval(out, next, nextsz, this, q);
op_resq_free(q);
return nextq;
}
/*
* Copy the string into the output. Returns zero on failure (memory
* allocation), non-zero on success.
*/
static int
op_exec_str(struct op_out *out, const struct op *op)
{
assert(op->op_type == OP_STR);
if (!op_debug(out, "length: %zu", op->op_str.sz))
return 0;
if (out->debug)
return 1;
return hbuf_put(out->ob, op->op_str.str, op->op_str.sz);
}
/*
* Copy the result running an expression into the output. Returns zero
* on failure (memory allocation), non-zero on success.
*/
static int
op_exec_expr(struct op_out *out, const struct op *op, const char *this)
{
struct op_resq *resq;
struct op_res *res;
int rc = 0, first = 1;
assert(op->op_type == OP_EXPR);
resq = op_eval(out, op->op_expr.expr, op->op_expr.sz, this,
NULL);
if (resq == NULL)
return 0;
if (!out->debug)
TAILQ_FOREACH(res, resq, entries) {
if (!first && !HBUF_PUTSL(out->ob, " "))
goto out;
if (!hbuf_puts(out->ob, res->res))
goto out;
first = 0;
}
rc = 1;
out:
op_resq_free(resq);
return rc;
}
/*
* Execute the body of a conditional or its "else", if found, depending
* on how the expression evaluates.
*/
static int
op_exec_for(struct op_out *out, const struct op *op, const char *this)
{
struct op_resq *resq;
struct op_res *res;
size_t loops = 0;
assert(op->op_type == OP_FOR);
/* Empty arguments evaluate to an empty list. */
if (op->op_for.sz == 0) {
if (!op_debug(out, "no loop expression"))
return 0;
return 1;
}
resq = op_eval(out, op->op_for.expr, op->op_for.sz, this, NULL);
if (resq == NULL)
return 0;
TAILQ_FOREACH(res, resq, entries) {
if (!op_debug(out, "loop iteration: %zu", ++loops))
return 0;
if (!op_exec(out, op, res->res)) {
op_resq_free(resq);
return 0;
}
}
if (loops == 0 && !op_debug(out, "no loop iterations"))
return 0;
op_resq_free(resq);
return 1;
}
/*
* Execute the body of a conditional or its "else", if found, depending
* on how the expression evaluates.
*/
static int
op_exec_ifdef(struct op_out *out, const struct op *op, const char *this)
{
struct op_resq *resq;
int rc;
assert(op->op_type == OP_IFDEF);
/* Empty arguments evaluate to an empty list. */
if (op->op_ifdef.sz > 0) {
resq = op_eval(out, op->op_ifdef.expr, op->op_ifdef.sz,
this, NULL);
if (resq == NULL)
return 0;
rc = !TAILQ_EMPTY(resq);
op_resq_free(resq);
} else
rc = 0;
if (!op_debug(out, "result: %s%s", rc ? "true" : "false",
rc || op->op_ifdef.chain == NULL ? "" :
" (taking else branch)"))
return 0;
return rc ? op_exec(out, op,this) :
op->op_ifdef.chain == NULL ? 1 :
op_exec(out, op->op_ifdef.chain, this);
}
static int
op_exec(struct op_out *out, const struct op *cop, const char *this)
{
const struct op *op;
out->depth++;
TAILQ_FOREACH(op, &cop->children, _siblings) {
if (!op_debug(out, "%s: %s", __func__,
op_types[op->op_type]))
return 0;
out->depth++;
switch (op->op_type) {
case OP_STR:
if (!op_exec_str(out, op))
return 0;
break;
case OP_EXPR:
if (!op_exec_expr(out, op, this))
return 0;
break;
case OP_IFDEF:
if (!op_exec_ifdef(out, op, this))
return 0;
break;
case OP_FOR:
if (!op_exec_for(out, op, this))
return 0;
break;
case OP_ELSE:
/*
* These are run if the condition in a prior
* "if" statement fails, otherwise the block is
* never run.
*/
break;
case OP_ROOT:
break;
}
out->depth--;
}
out->depth--;
return 1;
}
/*
* Fill in the output-specific template string "templ" with a document
* body of "content" into "ob".
*/
int
lowdown_template(const char *templ, const struct lowdown_buf *content,
struct lowdown_buf *ob, const struct lowdown_metaq *mq, int dbg)
{
char delim;
const char *cp, *nextcp, *savecp;
struct opq q;
struct op *op, *cop, *root;
int rc = 0, igneoln, inquot;
size_t sz;
struct op_out out;
TAILQ_INIT(&q);
if ((root = op_alloc(&q, OP_ROOT, NULL)) == NULL)
return 0;
/* Consume all template bytes. */
for (cop = root, cp = templ; *cp != '\0'; ) {
/*
* Scan ahead to the next variable delimiter. If none
* is found, stop processing.
*/
if ((nextcp = strchr(cp, '$')) == NULL)
break;
savecp = nextcp;
/* Output all text up to the delimiter. */
assert(nextcp >= cp);
if (!op_queue_str(&q, cop, cp, (size_t)(nextcp - cp)))
goto out;
cp = nextcp + 1;
/* Determine the closing delimiter. */
if (cp[0] == '{') {
delim = '}';
cp++;
} else
delim = '$';
/*
* If the closing delimiter was not found, revert to the
* beginning of the delimit sequence and bail out. Pass
* over any quoted materials inside of the sequence.
*/
inquot = 0;
for (nextcp = cp; *nextcp != '\0'; nextcp++)
if (*nextcp == '"') {
if (nextcp[-1] != '\\')
inquot = !inquot;
} else if (*nextcp == delim && !inquot)
break;
if (*nextcp == '\0') {
cp = savecp;
break;
}
/*
* A double-hypen before the end delimiter means that
* input must be consumed up to and including the eoln
* following the instruction.
*/
igneoln = nextcp > (cp + 2) &&
nextcp[-1] == '-' &&
nextcp[-2] == '-';
/*
* From "cp" to "nextcp" is the statement to evaluate.
* Trim the string.
*/
assert(nextcp >= cp);
for ( ; cp < nextcp; cp++)
if (*cp != ' ' && *cp != '\t')
break;
assert(nextcp >= cp);
sz = (size_t)(nextcp - cp);
/* Account for double-hyphens. */
if (igneoln)
sz -= 2;
while (sz > 0 &&
(cp[sz - 1] == ' ' || cp[sz - 1] == '\t'))
sz--;
/* Fully empty statements output a literal '$'. */
if (sz == 0) {
if (!op_queue_str(&q, cop, "$", 1))
goto out;
cp++;
continue;
}
/* Look up and process the operation. */
if (sz > 0 && !op_queue(&q, &cop, cp, sz))
goto out;
cp = nextcp + 1;
/* Special instruction that ignores til eoln/f. */
if (igneoln) {
while (*cp != '\0' && *cp != '\n')
cp++;
if (*cp == '\n')
cp++;
}
}
/* Mop up any remaining tokens. */
if (*cp != '\0' && !op_queue_str(&q, cop, cp, strlen(cp)))
goto out;
/* Execute the generation operation tree. */
out.debug = dbg;
out.ob = ob;
out.content = content;
out.mq = mq;
out.depth = 0;
rc = op_exec(&out, root, NULL);
out:
while ((op = TAILQ_FIRST(&q)) != NULL) {
TAILQ_REMOVE(&q, op, _all);
free(op);
}
return rc;
}
|