-
Notifications
You must be signed in to change notification settings - Fork 35
/
template.c
1340 lines (1169 loc) · 29.9 KB
/
template.c
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
/*
* Copyright (c) Kristaps Dzonsons <[email protected]>
*
* 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] == '"') {
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, 0);
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;
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 == 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 || v[0] == '\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;
const char *next, *args;
struct op_resq *q, *nextq;
if (sz == 0) {
if ((q = malloc(sizeof(struct op_resq))) == NULL)
return NULL;
TAILQ_INIT(q);
return q;
}