/
target.c
146 lines (112 loc) · 3.21 KB
/
target.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef NO_VERB
#define LOG(fmt, ...) (void)fmt
#else
#define LOG(fmt, ...) fprintf(stderr, " DBG: " fmt "\n", ## __VA_ARGS__)
#endif
#include "t.c"
int valid_uid(const char* uid) {
size_t i;
char c;
// check that the uid is at least UID_LEN long
// check that the uid contains only valid UID characters
// A-Z or a-z or - or _
for (i = 0; i < UID_LEN; i++) {
c = uid[i];
if (c == '\0' || c == '}') {
LOG("UID too short!");
return -1;
}
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '-' || c == '_'))) {
LOG("Invalid character");
return -1;
}
}
return 0;
}
char* str_append(char* out, size_t* outend, size_t* outsz, const char* in, size_t amt)
{
size_t newsz = (*outend + amt);
if (newsz >= *outsz) {
LOG("Growing allocation from %zu to contain %zu", *outsz, newsz);
while (newsz >= *outsz) {
*outsz = *outsz << 1;
}
out = realloc(out, *outsz + 1);
}
memcpy(&out[*outend], in, amt);
*outend = newsz;
out[*outend] = '\0';
return out;
}
char* process_line(const char* in) {
const char* cursor = in;
const char* next = in;
const char* name = NULL;
char* out = NULL;
size_t outsz = 0;
size_t outend = 0;
outsz = strlen(in) + 1;
out = malloc(outsz + 1);
for (cursor=in; *cursor != '\0'; cursor++) {
// find {{...}}
if (cursor[0] == '{' && cursor[1] == '{') {
if (valid_uid(&cursor[2])) {
LOG("Invalid User Identifier");
goto ERR;
}
LOG("Found UID signifier");
// first copy from next to cursor
out = str_append(out, &outend, &outsz, next, cursor - next);
// then copy in the name
name = uid_to_name(&cursor[2]);
if (name == NULL) {
LOG("Bad User Identifier");
goto ERR;
}
out = str_append(out, &outend, &outsz, name, strlen(name));
// then move cursor and next to the end '}}' or '\0'
while (cursor[0] != '\0' && !(cursor[0] == '}' && cursor[1] == '}')) {
cursor++;
}
if (cursor[0] == '\0') {
next = cursor;
break;
} else {
next = &cursor[2];
}
}
}
// copy out rest
out = str_append(out, &outend, &outsz, next, cursor - next);
return out;
ERR:
free(out);
return NULL;
}
int main(int argc, char** argv) {
(void)argc; (void)argv;
char* line = NULL;
char* rendered = NULL;
size_t linesz = 0;
ssize_t amt = 0;
size_t count = 1;
for (;;count++) {
amt = getline(&line, &linesz, stdin);
if (amt <= 0) {
break;
}
rendered = process_line(line);
if (rendered == NULL) {
LOG("Error rendering line %zu", count);
break;
}
fputs(rendered, stdout);
free(rendered);
LOG("Line %zu rendered", count);
}
free(line);
return 0;
}