-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathctemplate.c
More file actions
1550 lines (1340 loc) · 40.4 KB
/
Copy pathctemplate.c
File metadata and controls
1550 lines (1340 loc) · 40.4 KB
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
/*
* C Template: template expander library based on the perl
* HTML::Template package.
*
* Version: 1.0
*
* Author: Stephen C. Losen, University of Virginia
*
* Copyright 2009 Stephen C. Losen. Distributed under the terms
* of the GNU General Public License (GPL)
*
* A template consists of text sequences, comments and template tags.
* Anything that is not a tag and not a comment is considered text.
* The tags include:
*
* <TMPL_VAR name = "varname" default = "value" fmt = "fmtname">
* <TMPL_INCLUDE name = "filename">
* <TMPL_LOOP name = "loopname">
* <TMPL_BREAK level = N>
* <TMPL_CONTINUE level = N>
* </TMPL_LOOP>
* <TMPL_IF name = "varname" value = "testvalue">
* <TMPL_ELSIF name = "varname" value = "testvalue">
* <TMPL_ELSE>
* </TMPL_IF>
*
* The "name =" attribute is required, and the "value =", "fmt =",
* "default =", and "level =" attributes are optional.
*
* A comment is any text enclosed by <* and *>
*
* We read the entire template into memory, scan it, parse it, and
* build a parse tree. To generate the output, we walk the parse
* tree and output the tree nodes. A list of variables and values
* determines what tree nodes we visit and what we output.
*
* The scanner splits the template into text sequences and tags.
* Each call of scan() returns a tagnode struct representing the
* next text sequence or tag. The parser uses recursive descent
* to link the tagnodes into a parse tree.
*/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdarg.h>
#include "ctemplate.h"
/* To prevent infinite TMPL_INCLUDE cycles, we limit the depth */
#define MAX_INCLUDE_DEPTH 30
/* template tag kinds (used in bitmaps) */
typedef enum {
tag_text = 0x001, /* text sequence */
tag_var = 0x002, /* TMPL_VAR */
tag_if = 0x004, /* TMPL_IF */
tag_elsif = 0x008, /* TMPL_ELSIF */
tag_else = 0x010, /* <TMPL_ELSE> */
tag_endif = 0x020, /* </TMPL_IF> */
tag_include = 0x040, /* TMPL_INCLUDE */
tag_loop = 0x080, /* TMPL_LOOP */
tag_break = 0x100, /* TMPL_BREAK */
tag_cont = 0x200, /* TMPL_CONTINUE */
tag_endloop = 0x400 /* </TMPL_LOOP> */
} tag_kind;
typedef struct tagnode tagnode;
typedef struct template template;
/* The parse tree consists of tagnodes */
struct tagnode {
tag_kind kind;
tagnode *next;
union {
/* text sequence */
struct {
const char *start;
int len;
}
text;
/* TMPL_VAR tag */
struct {
const char *varname, *dfltval;
TMPL_fmtfunc fmtfunc;
}
var;
/* TMPL_IF tag or TMPL_ELSIF tag */
struct {
const char *varname, *testval;
tagnode *tbranch, *fbranch;
}
ifelse;
/* TMPL_LOOP tag */
struct {
const char *loopname;
tagnode *body;
}
loop;
/* TMPL_BREAK tag or TMPL_CONTINUE tag */
struct {
int level;
}
breakcont;
/* TMPL_INCLUDE tag */
struct {
const char *filename;
template *tmpl;
}
include;
}
tag;
};
/* template information */
struct template {
const char *filename; /* name of template file */
const char *tmplstr; /* contents of template file */
FILE *out; /* template output file pointer */
FILE *errout; /* error output file pointer */
tagnode *roottag; /* root of parse tree */
const TMPL_fmtlist
*fmtlist; /* list of format functions */
/* scanner and parser state variables */
const char *scanptr; /* next character to be scanned */
tagnode *nexttag; /* next tag to be returned by scanner */
tagnode *curtag; /* current tagnode being parsed */
int linenum; /* current template line number */
int tagline; /* line number of current tag's name */
int error; /* error indicator */
int include_depth; /* avoids TMPL_INCLUDE cycles */
int loop_depth; /* current loop nesting depth */
int break_level; /* for processing a TMPL_BREAK tag */
int cont_level; /* for processing a TMPL_CONTINUE tag */
tagnode reusable; /* reusable storage for simple tags */
};
/*
* TMPL_fmtlist is a list of format functions, which are passed to
* a template. A TMPL_VAR tag can specify a format function for
* outputting the variable with the fmt="fmtname" attribute.
*/
struct TMPL_fmtlist {
TMPL_fmtlist *next; /* next list member */
TMPL_fmtfunc fmtfunc; /* pointer to format function */
char name[1]; /* name of format function */
};
/*
* variables are passed to a template in a tree consisting of
* TMPL_var, TMPL_varlist and TMPL_loop nodes.
*
* TMPL_var is a simple variable (name and value)
*/
typedef struct TMPL_var TMPL_var;
struct TMPL_var {
TMPL_var *next; /* next simple variable on list */
const char *name;
char value[1]; /* value and name stored here */
};
/*
* TMPL_varlist is a variable list of simple variables and/or
* loop variables
*/
struct TMPL_varlist {
TMPL_varlist *next; /* next variable list on a list */
TMPL_var *var; /* list of my simple variables */
TMPL_loop *loop; /* list of my loop variables */
TMPL_loop *parent; /* my parent loop variable (if any) */
};
/* TMPL_loop is a loop variable, which is a list of variable lists */
struct TMPL_loop {
TMPL_loop *next; /* next loop variable on a list */
const char *name; /* my name */
TMPL_varlist *varlist; /* list of my variable lists */
TMPL_varlist *tail; /* tail of "varlist" */
TMPL_varlist *parent; /* my parent variable list */
};
/* mymalloc() is a malloc wrapper that exits on failure */
static void *
mymalloc(size_t size) {
void *ret = malloc(size);
if (ret == 0) {
fputs("C Template library: out of memory\n", stderr);
exit(1);
}
return ret;
}
/*
* newtemplate() creates a new template struct and reads the template
* file "filename" into memory. If "tmplstr" is non-null then it is
* the template, so we do not read "filename".
*/
static template *
newtemplate(const char *filename, const char *tmplstr,
const TMPL_fmtlist *fmtlist, FILE *out, FILE *errout)
{
template *t;
FILE *fp;
char *buf = 0;
struct stat stb;
if (tmplstr == 0 && filename == 0) {
if (errout != 0) {
fputs("C Template library: no template specified\n", errout);
}
return 0;
}
if (tmplstr == 0) {
if ((fp = fopen(filename, "r")) != 0 &&
fstat(fileno(fp), &stb) == 0 &&
S_ISREG(stb.st_mode) != 0 &&
(buf = (char *) mymalloc(stb.st_size + 1)) != 0 &&
(stb.st_size == 0 ||
fread(buf, 1, stb.st_size, fp) == stb.st_size))
{
fclose(fp);
buf[stb.st_size] = 0;
}
else {
if (errout != 0) {
fprintf(errout, "C Template library: failed to read "
"template from file \"%s\"\n", filename);
}
if (buf != 0) {
free(buf);
}
if (fp != 0) {
fclose(fp);
}
return 0;
}
}
t = (template *) mymalloc(sizeof(*t));
t->filename = filename != 0 ? filename : "(none)";
t->tmplstr = tmplstr != 0 ? tmplstr : buf;
t->fmtlist = fmtlist;
t->scanptr = t->tmplstr;
t->roottag = t->curtag = t->nexttag = 0;
t->out = out;
t->errout = errout;
t->linenum = 1;
t->error = 0;
t->include_depth = 0;
t->loop_depth = 0;
t->break_level = t->cont_level = 0;
return t;
}
/* newtag() allocates a new tagnode */
static tagnode *
newtag(template *t, tag_kind kind) {
tagnode *ret;
switch(kind) {
/*
* The following tags are simple parse tokens that are
* never linked into the parse tree so they share storage.
*/
case tag_else:
case tag_endif:
case tag_endloop:
ret = &t->reusable;
break;
default:
ret = (tagnode *) mymalloc(sizeof(*ret));
break;
}
ret->kind = kind;
ret->next = 0;
return ret;
}
/*
* freetag() recursively frees parse tree tagnodes. We do not free
* the text in a tag_text tagnode because it points to memory where
* the input template is stored, which we free elsewhere.
*/
static void
freetag(tagnode *tag) {
template *t;
if (tag == 0) {
return;
}
switch(tag->kind) {
case tag_var:
free((void *) tag->tag.var.varname);
if (tag->tag.var.dfltval != 0) {
free((void *) tag->tag.var.dfltval);
}
break;
case tag_if:
case tag_elsif:
free((void *) tag->tag.ifelse.varname);
if (tag->tag.ifelse.testval != 0) {
free((void *) tag->tag.ifelse.testval);
}
freetag(tag->tag.ifelse.tbranch);
freetag(tag->tag.ifelse.fbranch);
break;
case tag_loop:
free((void *) tag->tag.loop.loopname);
freetag(tag->tag.loop.body);
break;
case tag_include:
free((void *) tag->tag.include.filename);
if ((t = tag->tag.include.tmpl) != 0) {
free((void *) t->filename);
free((void *) t->tmplstr);
freetag(t->roottag);
free(t);
}
break;
}
freetag(tag->next);
free(tag);
}
/* map tag_kind to a human readable string */
static const char *
tagname(tag_kind kind) {
switch(kind) {
case tag_var:
return "TMPL_VAR";
case tag_if:
return "TMPL_IF";
case tag_elsif:
return "TMPL_ELSIF";
case tag_else:
return "TMPL_ELSE";
case tag_endif:
return "/TMPL_IF";
case tag_include:
return "TMPL_INCLUDE";
case tag_loop:
return "TMPL_LOOP";
case tag_break:
return "TMPL_BREAK";
case tag_cont:
return "TMPL_CONTINUE";
case tag_endloop:
return "/TMPL_LOOP";
}
return "unknown";
}
/*
* SCANNER FUNCTIONS
*
* scanspaces() scans white space
*/
static const char *
scanspaces(template *t, const char *p) {
while (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') {
if (*p++ == '\n') {
t->linenum++;
}
}
return p;
}
/*
* scancomment() scans a comment delimited by <* and *>. If we find a
* comment then we advance t->scanptr to the first character after the
* comment and return 1. Otherwise we return 0.
*/
static int
scancomment(template *t, const char *p) {
int linenum = t->linenum;
if (p[0] != '<' || p[1] != '*') {
return 0;
}
for (p += 2; *p != 0; p++) {
if (*p == '\n') {
t->linenum++;
}
if (p[0] == '*' && p[1] == '>') {
t->scanptr = p + 2;
return 1;
}
}
/* end of template, comment not terminated */
if (t->errout != 0) {
fprintf(t->errout, "\"<*\" in file \"%s\" line %d "
"has no \"*>\"\n", t->filename, linenum);
}
t->error = 1;
return 0;
}
/*
* scanattr() scans an attribute such as: name="value". If
* successful we advance t->scanptr to the character after the
* attribute and return a copy of the attribute value. Otherwise
* we return null. We accept double or single quotes around "value".
* We accept no quotes if "value" contains only letters, digits,
* '.' or '-'.
*/
static char *
scanattr(template *t, const char *attrname, const char *p) {
int i = strlen(attrname);
int quote = 0;
char *ret;
if (strncasecmp(p, attrname, i) != 0) {
return 0;
}
p = scanspaces(t, p + i);
if (*p++ != '=') {
return 0;
}
p = scanspaces(t, p);
if (*p == '"' || *p == '\'') {
quote = *p++;
}
/* p now points to the start of the attribute value */
if (quote != 0) {
for (i = 0; p[i] != quote && p[i] != '\n' && p[i] != 0; i++)
;
if (p[i] != quote) {
return 0;
}
t->scanptr = p + i + 1;
}
else {
for (i = 0; isalnum(p[i]) || p[i] == '.' || p[i] == '-'; i++)
;
if (i == 0) {
return 0;
}
t->scanptr = p + i;
}
/* i is now the length of the attribute value */
ret = (char *) mymalloc(i + 1);
memcpy(ret, p, i);
ret[i] = 0;
return ret;
}
/*
* findfmt() looks up a format function by name. If successful
* we return a pointer to the function, otherwise we return null.
*/
static TMPL_fmtfunc
findfmt(const TMPL_fmtlist *fmtlist, const char *name) {
for (; fmtlist != 0; fmtlist = fmtlist->next) {
if (strcmp(fmtlist->name, name) == 0) {
return fmtlist->fmtfunc;
}
}
return 0;
}
/*
* scantag() scans a template tag. If successful we return a tagnode
* for the tag and advance t->scanptr to the first character after the
* tag. Otherwise we clean up and return null.
*/
static tagnode *
scantag(template *t, const char *p) {
tag_kind kind;
int commentish = 0; /* true if tag enclosed by <!-- and --> */
int hasname = 0; /* true if tag has name= attribute */
int container = 0; /* true if tag may not end with /> */
tagnode *tag;
int linenum = t->linenum;
int len, level;
char *name = 0, *value = 0, *fmt = 0;
TMPL_fmtfunc func;
const char *err = "";
if (*p++ != '<') {
return 0;
}
if (p[0] == '!' && p[1] == '-' && p[2] == '-') {
commentish = 1;
p = scanspaces(t, p + 3);
}
t->tagline = t->linenum; /* tag name is on this line */
if (strncasecmp(p, "TMPL_VAR", len = 8) == 0) {
kind = tag_var;
hasname = 1;
}
else if (strncasecmp(p, "TMPL_INCLUDE", len = 12) == 0) {
kind = tag_include;
hasname = 1;
}
else if (strncasecmp(p, "TMPL_IF", len = 7) == 0) {
kind = tag_if;
hasname = 1;
container = 1;
}
else if (strncasecmp(p, "TMPL_ELSIF", len = 10) == 0) {
kind = tag_elsif;
hasname = 1;
}
else if (strncasecmp(p, "TMPL_ELSE", len = 9) == 0) {
kind = tag_else;
}
else if (strncasecmp(p, "/TMPL_IF", len = 8) == 0) {
kind = tag_endif;
container = 1;
}
else if (strncasecmp(p, "TMPL_LOOP", len = 9) == 0) {
kind = tag_loop;
hasname = 1;
container = 1;
}
else if (strncasecmp(p, "/TMPL_LOOP", len = 10) == 0) {
kind = tag_endloop;
container = 1;
}
else if (strncasecmp(p, "TMPL_BREAK", len = 10) == 0) {
kind = tag_break;
}
else if (strncasecmp(p, "TMPL_CONTINUE", len = 13) == 0) {
kind = tag_cont;
}
else {
kind = 0;
goto failure;
}
t->scanptr = p + len;
/* white space required between tag name and attributes */
p = scanspaces(t, t->scanptr);
if (hasname != 0 && p == t->scanptr) {
goto failure;
}
/*
* These tags require one "name =" attribute. The TMPL_VAR tag
* may have optional "fmt =" and "default =" attributes. The
* TMPL_IF and TMPL_ELSIF tags may have an optional "value ="
* attribute. Attributes can come in any order.
*/
switch(kind) {
case tag_include:
case tag_loop:
if ((name = scanattr(t, "name", p)) != 0) {
p = scanspaces(t, t->scanptr);
}
break;
case tag_var:
while ((name == 0 && (name = scanattr(t, "name", p)) != 0) ||
(fmt == 0 && (fmt = scanattr(t, "fmt", p)) != 0) ||
(value == 0 && (value = scanattr(t, "default", p)) != 0))
{
p = scanspaces(t, t->scanptr);
}
break;
case tag_if:
case tag_elsif:
while ((name == 0 && (name = scanattr(t, "name", p)) != 0) ||
(value == 0 && (value = scanattr(t, "value", p)) != 0))
{
p = scanspaces(t, t->scanptr);
}
break;
/*
* These tags may have an optional "level =" attribute, which
* must be preceded by white space.
*/
case tag_break:
case tag_cont:
if (p != t->scanptr &&
(value = scanattr(t, "level", p)) != 0)
{
p = scanspaces(t, t->scanptr);
}
break;
}
/* check for end of tag */
if (commentish == 0 && p[0] == '>') {
t->scanptr = p + 1;
}
else if (commentish == 0 && container == 0 &&
p[0] == '/' && p[1] == '>')
{
t->scanptr = p + 2;
}
else if (commentish != 0 && p[0] == '-' &&
p[1] == '-' && p[2] == '>')
{
t->scanptr = p + 3;
}
else {
goto failure;
}
/* check attributes and build tag node */
if (hasname != 0 && name == 0) {
err = "(missing \"name=\" attribute) ";
goto failure;
}
switch(kind) {
case tag_var:
func = 0;
if (fmt != 0) {
if ((func = findfmt(t->fmtlist, fmt)) == 0) {
err = "(bad \"fmt=\" attribute) ";
goto failure;
}
free(fmt);
}
tag = newtag(t, kind);
tag->tag.var.varname = name;
tag->tag.var.dfltval = value;
tag->tag.var.fmtfunc = func;
break;
case tag_include:
if (t->include_depth >= MAX_INCLUDE_DEPTH) {
err = "(check for include cycle) ";
goto failure;
}
tag = newtag(t, kind);
tag->tag.include.filename = name;
tag->tag.include.tmpl = 0;
break;
case tag_loop:
tag = newtag(t, kind);
tag->tag.loop.loopname = name;
tag->tag.loop.body = 0;
break;
case tag_break:
case tag_cont:
if (t->loop_depth < 1) {
err = "(not inside a loop) ";
goto failure;
}
level = 1;
if (value != 0) {
if ((level = atoi(value)) < 1 || level > t->loop_depth) {
err = "(bad \"level=\" attribute) ";
goto failure;
}
free(value);
}
tag = newtag(t, kind);
tag->tag.breakcont.level = level;
break;
case tag_if:
case tag_elsif:
tag = newtag(t, kind);
tag->tag.ifelse.varname = name;
tag->tag.ifelse.testval = value;
tag->tag.ifelse.tbranch = 0;
tag->tag.ifelse.fbranch = 0;
break;
default:
tag = newtag(t, kind);
break;
}
return tag;
failure:
/* restore line number, clean up and return null */
t->linenum = linenum;
if (name != 0) {
free(name);
}
if (value != 0) {
free(value);
}
if (fmt != 0) {
free(fmt);
}
if (kind != 0 && t->errout != 0) {
fprintf(t->errout, "Ignoring bad %s tag %sin file \"%s\" line %d\n",
tagname(kind), err, t->filename, t->tagline);
}
return 0;
}
/*
* scan() is the main scanner function. We return the next text sequence
* or template tag in t->curtag or we set it to null at the end of the
* template. We start scanning at t->scanptr and when we are done,
* t->scanptr points to where the next call to scan() should
* start scanning. We scan text until we find a tag or comment. If we
* find a comment, then we return the text. If we find a tag, then we
* save it in t->nexttag and return the text. We will return t->nexttag
* the next time scan() is called. If we find a tag with no preceding
* text, then we return the tag. If we find a comment with no preceding
* text, then we try again.
*/
static void
scan(template *t) {
tagnode *tag = 0;
const char *p;
int i;
if (t->nexttag != 0) { /* return tag from previous call */
t->curtag = t->nexttag;
t->nexttag = 0;
return;
}
/* scan text until we find a tag or a comment or null */
p = t->scanptr;
for (i = 0; p[i] != 0; i++) {
if (p[i] == '\n') {
t->linenum++;
}
if (p[i] != '<') {
continue;
}
/* we found a '<' so we look for a comment or tag */
if (scancomment(t, p + i) != 0) {
if (i == 0) {
scan(t); /* no text so try again */
return;
}
break;
}
if ((tag = scantag(t, p + i)) != 0) {
break;
}
}
/*
* At this point p is where we started scanning and p[i] is
* the first character of the tag or comment that ended this
* scan or else p[i] is the null at the end of the template.
*/
if (p[i] == 0) {
t->scanptr = p + i;
}
if (i > 0) {
t->nexttag = tag; /* save the tag (if any) */
tag = newtag(t, tag_text); /* return the text sequence */
tag->tag.text.start = p;
tag->tag.text.len = i;
}
t->curtag = tag;
}
/*
* PARSER FUNCTIONS
*
* forward declaration for recursive calls
*/
static tagnode *parselist(template *t, int stop);
/*
* parseif() parses a TMPL_IF statement, which looks like this:
*
* <TMPL_IF name = "varname" value = "testvalue" >
* template-list
* <TMPL_ELSIF name = "varname" value = "testvalue" >
* template-list
* <TMPL_ELSE>
* template-list
* </TMPL_IF>
*
* A template-list is any sequence (including an empty sequence)
* of text, template tags, if statements or loop statements. There
* can be zero or more TMPL_ELSIF tags followed by zero or one
* TMPL_ELSE tag. There must be a final /TMPL_IF tag.
*
* "iftag" is a TMPL_IF tagnode, which has pointers for a true branch
* and a false branch. We construct a parse tree for the if statement
* with "iftag" at the root. When we are done t->curtag points to
* the tag that follows the /TMPL_IF tag for this statement.
*/
static void
parseif(template *t, int stop) {
tagnode *iftag = t->curtag;
int linenum = t->tagline;
int mystop = stop | tag_else | tag_elsif | tag_endif;
iftag->tag.ifelse.tbranch = parselist(t, mystop);
while (t->curtag != 0 && t->curtag->kind == tag_elsif) {
iftag->tag.ifelse.fbranch = t->curtag;
iftag = t->curtag;
iftag->tag.ifelse.tbranch = parselist(t, mystop);
}
if (t->curtag != 0 && t->curtag->kind == tag_else) {
iftag->tag.ifelse.fbranch = parselist(t, stop | tag_endif);
}
if (t->curtag != 0 && t->curtag->kind == tag_endif) {
scan(t); /* success, scan next tag */
}
else {
if (t->errout != 0) {
fprintf(t->errout, "TMPL_IF tag in file \"%s\" line %d "
"has no /TMPL_IF tag\n", t->filename, linenum);
}
t->error = 1;
}
}
/*
* parseloop() parses a TMPL_LOOP statement which looks like this:
*
* <TMPL_LOOP name = "loopname">
* template-list
* </TMPL_LOOP>
*
* "looptag" is a TMPL_LOOP tagnode, which has a pointer for the
* loop body. We construct a parse tree for the loop statement
* with "looptag" at the root. When we are done, t->curtag points
* to the tag that follows the /TMPL_LOOP tag for this statement.
*/
static void
parseloop(template *t, int stop) {
tagnode *looptag = t->curtag;
int linenum = t->tagline;
t->loop_depth++;
looptag->tag.loop.body = parselist(t, stop | tag_endloop);
t->loop_depth--;
if (t->curtag != 0 && t->curtag->kind == tag_endloop) {
scan(t); /* success, scan next tag */
}
else {
if (t->errout != 0) {
fprintf(t->errout, "TMPL_LOOP tag in file \"%s\" line %d "
"has no /TMPL_LOOP tag\n", t->filename, linenum);
}
t->error = 1;
}
}
/*
* parselist() is the top level parser function. It parses a
* template-list which is any sequence of text, TMPL_VAR tags,
* TMPL_INCLUDE tags, if statements or loop statements.
* We return a parse tree which is a linked list of tagnodes. The
* "stop" parameter is a bitmap of tag kinds that we expect to end
* this list. For example, if we are parsing the template-list
* following a TMPL_IF tag, then we expect the list to end with a
* TMPL_ELSIF tag or TMPL_ELSE tag or /TMPL_IF tag. If we are parsing the
* template-list that comprises the entire template, then "stop" is zero
* so that we keep going to the end of the template. When we are done,
* t->curtag is the tag that caused us to stop, or null at the end of
* the template.
*/
static tagnode *
parselist(template *t, int stop) {
tagnode *list = 0, *tail, *tag;
scan(t);
while ((tag = t->curtag) != 0) {
switch(tag->kind) {
case tag_elsif: /* check for terminator tag */
case tag_else:
case tag_endif:
case tag_endloop:
if ((tag->kind & stop) != 0) {
return list;
}
/* unexpected terminator tag -- keep going */
if (t->errout != 0) {
fprintf(t->errout, "Unexpected %s tag in file \"%s\" "
"line %d\n", tagname(tag->kind), t->filename,
t->tagline);
}
t->error = 1;
scan(t);
if (tag->kind == tag_elsif) {
break; /* tag linked to list to be freed later */
}
continue; /* tag not linked to list */
case tag_if:
parseif(t, stop);
break;
case tag_loop:
parseloop(t, stop);
break;
default:
scan(t);
break;
}
/* link the tag into the list of tags */
tag->next = 0;
if (list == 0) {
list = tail = tag;
}
else {
tail->next = tag;
tail = tag;
}
}
return list;
}
/*
* PARSE TREE WALKING FUNCTIONS
*
* valueof() looks up a variable by name and returns its value
* or returns null if not found. We search "varlist" and any
* enclosing variable lists. The parent of "varlist" is a
* loop variable, whose parent is a variable list that encloses
* "varlist".
*/
static char *
valueof(const char *varname, const TMPL_varlist *varlist) {
TMPL_var *var;
while(varlist != 0) {
for (var = varlist->var; var != 0; var = var->next) {
if (strcmp(varname, var->name) == 0) {
return var->value;
}
}
varlist = varlist->parent == 0 ? 0 : varlist->parent->parent;