-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmplr.c
763 lines (677 loc) · 20.7 KB
/
tmplr.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
//'usr/bin/env' cc -xc -DSCRIPT -o tmplr.bin "$0" && exec ./tmplr.bin "$@"
/*
* Copyright (C) Huawei Technologies Co., Ltd. 2022-2025. All rights reserved.
* SPDX-License-Identifier: MIT
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <assert.h>
/*******************************************************************************
* tmplr - a template replacement tool
*
* tmplr is a simple tool to achieve a minimum level of genericity in libvsync
* without resorting to C preprocessor macros.
*
* ## Template blocks
*
* tmplr reads input files and replaces mappings in template blocks. Template
* blocks are marked with TMPL_BEGIN/TMPL_END commands (see "Template commands"
* below).
*
* For example:
*
* TMPL_BEGIN(key=value)
* The following word, key, will be replaced by value.
* TMPL_END
*
* ## Template mappings
*
* The mappings given to TMPL_BEGIN are called *template mappings*.
*
* Iteration mappings may take a single value as in keyA = value1 or multiple
* values as in keyA = [[value1; value2]]. The list of values is separated by
* semicolumn and optionally sorrounded by [[ ]]. The list of template mappings
* is separated by commas, for example:
*
* TMPL_BEGIN(keyA=[[val1;val2]], keyB=[[val3;val4]])
* ...
* TMPL_END
*
* ## Block iterations
*
* If template mappings contain multiple values, the template block is repeated
* for each combination of template mappings. Each such instance of template
* mappings is called *iteration mapping* in the source code.
*
* Consider this block example:
*
* TMPL_BEGIN(key=[[val1;val2]])
* Key --> key
* TMPL_END
*
* The template mapping consists of key=[[val1;val2]]. In the first iteration of
* the block, the iteration mapping is key=val1, in the second iteration, the
* mapping is key=val2.
*
* ## Persistent mappings
*
* Beyond template mappings, tmplr also support persistent mappings. Outside
* template blocks, the user can use the command TMPL_MAP(key, value) to add
* persistent mappings to the tmplr state.
*
* During the processing of each line in a block iteration, *after* exhausting
* the application of iteration mappings, tmplr applies any persistent mapping
* matches.
*
* ## Command line and mapping override
*
* tmplr is a CLI program and takes as input a list of files. It provides two
* flags: -v for verbose output and -D to select a single value for an iteration
* mapping. For example, -DkeyA=value1. Other values will be ignored.
*
* ## Valid keys and values
*
* tmplr **does not** tokenize the input. Hence, a key "two words" is a
* perfectly valid key. Characters such as $ can also be used in keys and
* values.
*
* The only restriction is that keys cannot contain
* - new line: \n
* - parenthesis: ( )
* - comma: ,
* - semicolon: ;
* - nor any tmplr commands
*
* Values cannot contain parenthesis, commas nor semicolon.
*
* Disclaimer:
* We are aware of similar, more powerful tools such as Jinja, Mustache and M4.
* tmplr follows three design principles:
*
* - simplicity: as simple and maintainable as possible
* - dependency freedom: no additonal language which will get deprecated
* - c-syntax transperency: annotation should not interfer with the LSP
* servers such as clangd
******************************************************************************/
/*******************************************************************************
* Template commands
******************************************************************************/
#define TMPL_MAP "_tmpl_map"
#define TMPL_BEGIN "_tmpl_begin"
#define TMPL_END "_tmpl_end"
#define TMPL_MUTE "_tmpl_mute"
#define TMPL_UNMUTE "_tmpl_unmute"
#define TMPL_ABORT "_tmpl_abort"
#define TMPL_SKIP "_tmpl_skip"
#define TMPL_DL "_tmpl_dl"
#define TMPL_NL "_tmpl_nl"
#define TMPL_UPCASE "_tmpl_upcase"
#define TMPL_HOOK "_tmpl_hook"
/*******************************************************************************
* Maximum line lengths and buffer sizes
******************************************************************************/
/* maximum length of a line */
#define MAX_SLEN 256
/* maximum number of lines in a block */
#define MAX_BLEN 100
/* maximum number of keys */
#define MAX_KEYS 1024
/* maximum length of a value */
#define MAX_VLEN 256
/* Buffer to hold the value */
#define V_BUF_LEN ((MAX_VLEN) + 1)
/* maximum length of a key */
#define MAX_KLEN 64
/* Buffer to hold the key */
#define K_BUF_LEN ((MAX_KLEN) + 1)
/* maximum number of replacements per line */
#define MAX_APPLY 32
/*******************************************************************************
* Type definitions
******************************************************************************/
/* pair_t is a key-value pair. Key and value are 0-terminated char arrays. */
typedef struct {
char key[K_BUF_LEN];
char val[V_BUF_LEN];
} pair_t;
/* err_t represents an error message */
typedef struct {
const char *msg;
} err_t;
#define NO_ERROR \
(err_t) \
{ \
0 \
}
#define ERROR(m) \
(err_t) \
{ \
.msg = m \
}
#define IS_ERROR(err) (err).msg != NULL
/*******************************************************************************
* Logging
******************************************************************************/
bool _verbose;
#define debugf(fmt, ...) \
do { \
if (_verbose) \
printf("// " fmt, ##__VA_ARGS__); \
} while (0)
/*******************************************************************************
* String functions
******************************************************************************/
void
trim(char *s, char c)
{
assert(s);
/* remove trailing space */
while (s[strlen(s) - 1] == c)
s[strlen(s) - 1] = '\0';
/* remove leading space */
while (s[0] == c)
/* use len of s to include \0 */
memmove(s, s + 1, strlen(s));
}
void
trims(char *s, char *chars)
{
for (char *c = chars; *c; c++)
trim(s, *c);
}
/*******************************************************************************
* Mappings
******************************************************************************/
/* template mappings: key -> value lists
*
* Passed as arguments to TMPL_BEGIN.
*/
pair_t template_map[MAX_KEYS];
/* template override mappings : key -> value
*
* Given via command line -D option. These overwrite template mapping values
*/
pair_t override_map[MAX_KEYS];
/* iteration mappings: key -> value
*
* These are the single values of the template mappings, potentially overriden
* by override mappings. They are set at each iteration of a template block.
*
* They precede the persistent mappings.
*/
pair_t iteration_map[MAX_KEYS];
/* persistent mappings: key -> value
*
* Given via TMPL_MAP(key, value) commands outside of template blocks.
*
* These succeed the iteration mappings.
*/
pair_t persistent_map[MAX_KEYS];
/* block hooks */
pair_t block_hooks[MAX_KEYS];
void
remap(pair_t *map, const char *key, const char *val)
{
if (key == NULL)
return;
for (int i = 0; i < MAX_KEYS; i++) {
pair_t *p = map + i;
if (!p->key[0] || strcmp(p->key, key) == 0) {
memset(p->key, 0, MAX_KLEN);
strcat(p->key, key);
trim(p->key, ' ');
memset(p->val, 0, MAX_VLEN);
strcat(p->val, val);
trim(p->val, ' ');
debugf("[REMAP] %s = %s\n", p->key, p->val);
return;
}
}
}
pair_t *
find(pair_t *map, const char *key)
{
for (int i = 0; i < MAX_KEYS; i++) {
pair_t *p = map + i;
if (strcmp(p->key, key) == 0)
return p;
}
return NULL;
}
void
unmap(pair_t *map, char *key)
{
pair_t *p = find(map, key);
if (p == NULL)
return;
memset(p->key, 0, MAX_KLEN);
memset(p->val, 0, MAX_VLEN);
}
void
show(pair_t *map, const char *name)
{
debugf("[SHOW MAP] %s\n", name);
for (int i = 0; i < MAX_KEYS; i++) {
pair_t *p = map + i;
if (p->key[0]) {
debugf("\t%s = %s\n", p->key, p->val);
}
}
}
void
clean(pair_t *map)
{
memset(map, 0, sizeof(pair_t) * MAX_KEYS);
}
/*******************************************************************************
* parse functions
******************************************************************************/
err_t
parse_assign(pair_t *p, char *start, char *end)
{
char key[K_BUF_LEN] = {0};
char val[V_BUF_LEN] = {0};
char *comma = strstr(start, ",");
if (comma == NULL)
return ERROR("expected ','");
start++;
strncat(key, start, comma - start);
comma++;
strncat(val, comma, end - comma);
remap(p, key, val);
return NO_ERROR;
}
err_t
parse_template_map(char *start, char *end)
{
char *next, *values;
start++;
*end = '\0';
again:
next = strstr(start, ",");
if (next) {
*next = '\0';
next++;
}
values = strstr(start, "=");
if (values == NULL)
return ERROR("expected '='");
*values = '\0';
values++;
char key[K_BUF_LEN] = {0};
strncat(key, start, MAX_KLEN);
char val[V_BUF_LEN] = {0};
strncat(val, values, strlen(values));
trims(val, " []");
remap(template_map, key, val);
if (next) {
start = next;
goto again;
}
return NO_ERROR;
}
/*******************************************************************************
* Line-based processing
******************************************************************************/
bool muted = false;
void
line_add_nl(char *line)
{
const size_t len = strlen(line);
if (line[len - 1] != '\n') {
assert(len + 1 < MAX_SLEN);
line[len] = '\n';
line[len + 1] = '\0';
}
}
bool
line_apply(char *line, const char *key, const char *val)
{
char *cur;
if (!key[0] || (cur = strstr(line, key)) == NULL)
return false;
const size_t vlen = strlen(val);
const size_t klen = strlen(key);
const size_t slen = strlen(cur);
const bool is_nl = strcmp(key, TMPL_NL) == 0;
if (!is_nl)
debugf("[APPLY] KEY: %s(%lu) VAL: %s(%lu)\n", key, klen, val, vlen);
/* make space for value */
if (!is_nl)
debugf("\tBEFORE: %s", line);
memmove(cur + vlen, cur + klen, slen);
memcpy(cur, val, vlen);
if (!is_nl)
debugf("\tAFTER: %s", line);
return true;
}
bool
process_block_line(char *line)
{
bool applied;
char *cur;
char buf[MAX_SLEN] = {0};
strcat(buf, line);
line_add_nl(buf);
debugf("[LINE] %s", buf);
int cnt = 0;
again:
applied = false;
for (int i = 0; i < MAX_KEYS && cnt < MAX_APPLY; i++) {
/* should delete line? */
if (strstr(buf, TMPL_DL)) {
strcpy(buf, "");
goto end;
}
if (strstr(buf, TMPL_SKIP))
return false;
const pair_t *pi = iteration_map + i;
const pair_t *pp = persistent_map + i;
if (!line_apply(buf, pi->key, pi->val) &&
!line_apply(buf, pp->key, pp->val))
continue;
applied = true;
cnt++;
/* if one mapping is applied, restart testing all mappings */
i = -1;
}
if (applied)
goto again;
end:
/* apply UPCASE */
while ((cur = strstr(buf, TMPL_UPCASE))) {
char *start = cur + strlen(TMPL_UPCASE);
char sep = start[0] == '(' ? ')' : start[0];
char ssep[] = {sep, 0};
char *end = strstr(start + 1, ssep);
assert(start && end && end > start);
char *ch = start + 1;
while (ch < end) {
if (*ch >= 'a' && *ch <= 'z')
*ch -= ('a' - 'A');
ch++;
}
size_t len = (end - start) - 1;
memmove(cur, start + 1, len);
/* include the end of line */
memmove(cur + len, end + 1, strlen(end));
}
/* apply NL */
while (line_apply(buf, TMPL_NL, "\n"))
;
/* output and return */
printf("%s", buf);
assert(cnt < MAX_APPLY);
return true;
}
/*******************************************************************************
* Block processing
******************************************************************************/
void
process_begin()
{
debugf("============================\n");
debugf("[BLOCK_BEGIN]\n");
show(persistent_map, "persistent_map");
show(block_hooks, "block_hooks");
show(template_map, "template_map");
debugf("----------------------------\n");
}
/*******************************************************************************
* Block buffer
*
* Inside a template block, we buffer the whole block and then output the
* content of hte buffer with mappings applied for each value of the mapping
* iterators.
******************************************************************************/
char save_block[MAX_BLEN][MAX_SLEN];
int save_k;
const char *
sticking(const char *key)
{
for (int i = 0; i < MAX_KEYS && strlen(override_map[i].key) != 0; i++)
if (strcmp(override_map[i].key, key) == 0)
return override_map[i].val;
return NULL;
}
void
process_block(int i, const int nvars)
{
pair_t *hook = NULL;
if (i == nvars) {
if ((hook = find(block_hooks, "begin")))
if (!process_block_line(hook->val))
return;
for (int k = 0; k < save_k && process_block_line(save_block[k]); k++)
;
if ((hook = find(block_hooks, "end")))
(void)process_block_line(hook->val);
return;
}
pair_t *p = template_map + i;
char val[V_BUF_LEN] = {0};
strcat(val, p->val);
const char *sval = sticking(p->key);
const char *sep = ";";
char *saveptr = NULL;
char *tok = strtok_r(val, sep, &saveptr);
int c = 0;
while (tok) {
trims(tok, " ");
if (sval == NULL || strcmp(sval, tok) == 0) {
(void)c;
remap(iteration_map, p->key, tok);
process_block(i + 1, nvars);
unmap(iteration_map, p->key);
}
tok = strtok_r(0, sep, &saveptr);
}
if (i == 0 && (hook = find(block_hooks, "final"))) {
(void)process_block_line(hook->val);
}
}
/*******************************************************************************
* File processing
******************************************************************************/
/* processing state */
enum state {
TEXT,
IGNORE_BLOCK,
BLOCK_BEGIN,
BLOCK_BEGIN_ARGS,
BLOCK_TEXT,
BLOCK_END,
MAP,
HOOK,
} S = TEXT;
err_t
process_line(char *line)
{
char *cur = NULL;
char *end = NULL;
err_t err;
again:
switch (S) {
case TEXT:
if (!muted && strstr(line, TMPL_ABORT)) {
fflush(stdout);
abort();
}
if (!muted && strstr(line, TMPL_BEGIN "(")) {
S = BLOCK_BEGIN;
goto again;
}
if (!muted && strstr(line, TMPL_MAP)) {
S = MAP;
goto again;
}
if (!muted && strstr(line, TMPL_HOOK)) {
S = HOOK;
goto again;
}
if (!muted && strstr(line, TMPL_MUTE)) {
debugf("[OUTPUT] muted\n");
muted = true;
break;
}
if (strstr(line, TMPL_UNMUTE)) {
debugf("[OUTPUT] unmuted\n");
muted = false;
break;
}
if (!muted && strstr(line, TMPL_DL) == NULL)
printf("%s", line);
break;
case BLOCK_BEGIN:
clean(template_map);
cur = strstr(line, "(");
if (cur == NULL)
return ERROR("expected '('");
S = BLOCK_BEGIN_ARGS;
line = cur;
goto again;
case BLOCK_BEGIN_ARGS:
if ((end = strstr(line, ")"))) {
err_t err = parse_template_map(line, end);
if (IS_ERROR(err))
return err;
S = BLOCK_TEXT;
process_begin();
} else {
if ((end = strstr(line, ",")) == NULL)
return ERROR("expected ','");
for (char *e = NULL; (e = strstr(end + 1, ","), e && e != end);
end = e)
;
parse_template_map(line, end);
}
break;
case BLOCK_TEXT:
cur = strstr(line, TMPL_END);
if (cur != NULL) {
S = BLOCK_END;
goto again;
}
if (save_k >= MAX_BLEN)
return ERROR("block too long");
memcpy(save_block[save_k++], line, strlen(line) + 1);
break;
case BLOCK_END:
/* consume */
{
int nvars = 0;
for (int i = 0; i < MAX_KEYS; i++)
if (template_map[i].key[0])
nvars++;
process_block(0, nvars);
}
save_k = 0;
S = TEXT;
break;
case MAP:
if ((cur = strstr(line, "(")) == NULL)
return ERROR("expected '('");
if ((end = strstr(cur, ")")) == NULL)
return ERROR("expected ')'");
for (char *e = NULL; (e = strstr(end + 1, ")"), e && e != end);
end = e)
;
err = parse_assign(persistent_map, cur, end);
if (IS_ERROR(err))
return err;
S = TEXT;
break;
case HOOK:
if ((cur = strstr(line, "(")) == NULL)
return ERROR("expected '('");
if ((end = strstr(cur, ")")) == NULL)
return ERROR("expected ')'");
err = parse_assign(block_hooks, cur, end);
if (IS_ERROR(err))
return err;
S = TEXT;
break;
default:
assert(0 && "invalid");
}
return NO_ERROR;
}
/*******************************************************************************
* File processing
******************************************************************************/
void
process_file(const char *fn)
{
FILE *fp = fopen(fn, "r+");
assert(fp);
char *line = NULL;
size_t len = 0;
ssize_t read;
err_t err;
int i = 0;
while ((read = getline(&line, &len, fp)) != -1) {
assert(line);
err = process_line(line);
if (IS_ERROR(err)) {
fprintf(stderr, "%s:%d: error: %s\n", fn, i + 1, err.msg);
abort();
}
if (line) {
free(line);
line = NULL;
}
i++;
}
if (line)
free(line);
fclose(fp);
}
/*******************************************************************************
* main function with options
*
* $0 [-v] <FILE> [FILE]...
******************************************************************************/
int
main(int argc, char *argv[])
{
debugf("vatomic generator\n");
int c;
char *k;
while ((c = getopt(argc, argv, "hvD:")) != -1) {
switch (c) {
case 'D':
k = strstr(optarg, "=");
*k++ = '\0';
remap(override_map, optarg, k);
break;
case 'v':
_verbose = true;
break;
case 'h':
printf("tmplr - a simple templating tool\n\n");
printf("Usage:\n\ttmplr [FLAGS] <FILE> [FILE ...]\n\n");
printf("Flags:\n");
printf("\t-v verbose\n");
printf(
"\t-Dkey=value override template map assignement of "
"key\n");
exit(0);
case '?':
printf("error");
exit(1);
default:
break;
}
}
for (int i = optind; i < argc; i++)
process_file(argv[i]);
// if started as script, remove tmplr file
#ifdef SCRIPT
return remove(argv[0]);
#else
return 0;
#endif
}