aboutsummaryrefslogtreecommitdiff
path: root/tl.c
blob: 6f7ac34daad9e8f60ae60aa12da1cfdb482ba496 (plain)
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
#include "tl.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * public list functions
 */
struct tl_list *tl_new(const char     *name,
                       const char     *data,
                       struct tl_list *list,
                       struct tl_list *next)
{
    struct tl_list *res = malloc(sizeof(*res));
    res->name           = name;
    res->data           = data;
    res->list           = list;
    res->next           = next;
    return res;
}

struct tl_list *tl_plain_list(struct tl_list *list, struct tl_list *next)
{
    return tl_new(0, 0, list, next);
}

struct tl_list *tl_named_list(const char     *name,
                              struct tl_list *list,
                              struct tl_list *next)
{
    return tl_new(name, 0, list, next);
}

struct tl_list *tl_named_data(const char     *name,
                              const char     *data,
                              struct tl_list *next)
{
    return tl_new(name, data, 0, next);
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * internal list functions
 */
static struct tl_list *tl_list_union(struct tl_list *a, struct tl_list *b)
{  /* Copy a appending copy of b. */
    if (!a) return b ? tl_list_union(b, 0) : 0;
    return tl_new(a->name, a->data,
                  tl_list_union(a->list, 0),
                  tl_list_union(a->next, b));
}

static struct tl_list *tl_list_get(struct tl_list *l, char *name)
{
    if (!l) return 0;
    if (!strcmp(l->name, name)) return l;
    return tl_list_get(l->next, name);
}

static void tl_list_free(struct tl_list *l)
{
    if (l->list) tl_list_free(l->list);
    if (l->next) tl_list_free(l->next);
    free(l);
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * string
 * Strings are represented as usual char * null-terminated string.
 * Size and length are stored before the string (so it is a fat pointer).
 */
static int *tl_string_size(char *str)  { return ((int *) str) - 2; }
static int *tl_string_len(char *str)   { return ((int *) str) - 1; }

static void tl_string_add(char **str, char c)
{
    static const int shift = 2 * sizeof(int);
    if (!*str) {
        *str  = malloc(shift + 1);
        *str += shift;
        *tl_string_size(*str) = 1;
        *tl_string_len(*str)  = 0;
        (*str)[0] = 0;
    }
    ++*tl_string_len(*str);
    if (*tl_string_size(*str) == *tl_string_len(*str)) {
        *tl_string_size(*str) *= 2;
        *str  = realloc(*str - shift, shift + *tl_string_size(*str));
        *str += shift;
    }
    (*str)[*tl_string_len(*str) - 1] = c;
    (*str)[*tl_string_len(*str)]     = 0;
}

static void tl_string_free(char *str)
{
    if (str) free(str - 2 * sizeof(int));
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * template language translator as automata
 * Automata states are functions, consuming a character, changing automata,
 * returning new state and outputting substituted text.
 */
struct tl {
    struct tl_list *variables;
    char           *name;
    char           *body;
    int             opened;
};
typedef void *(*tl_state)(struct tl *t, char c);

static void tl_free(struct tl *t)
{
    if (t->variables) tl_list_free(t->variables);
    if (t->name)      tl_string_free(t->name);
    if (t->body)      tl_string_free(t->body);
}

static void *tl_state_text(struct tl *t, char c);
void tl_translate(struct tl_list *l)
{
    int c;
    tl_state  state    = tl_state_text;
    struct tl automata = { l, 0, 0, 0 };
    while ((c = getchar()) != EOF) {
        state = state(&automata, c);
    }
    tl_free(&automata);
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * automata states
 */
static void *tl_state_backslash(struct tl *t, char c);
static void *tl_state_at(struct tl *t, char c);
static void *tl_state_subs(struct tl *t, char c);
static void *tl_state_text(struct tl *t, char c)
{  /* Normal state is to just copy input, switching state on specials. */
    if (c == '\\') return tl_state_backslash;
    if (c == '@') return tl_state_at;
    if (c == '{') return tl_state_subs;
    putchar(c);
    return tl_state_text;
}

static void *tl_state_subs(struct tl *t, char c)
{  /* Read name until } and output value from variables of this name. */
    if (c == '}') {
        struct tl_list *var = tl_list_get(t->variables, t->name);
        if (var && var->data) {
            fputs(var->data, stdout);
        }
        tl_string_free(t->name);
        t->name = 0;
        return tl_state_text;
    }
    tl_string_add(&t->name, c);
    return tl_state_subs;
}

static void *tl_state_body(struct tl *t, char c);
static void *tl_state_at(struct tl *t, char c)
{  /* Read name until { and start reading body after it. */
    if (c == '{') return tl_state_body;
    tl_string_add(&t->name, c);
    return tl_state_at;
}

static void tl_at(struct tl *t)
{  /* Repeat the body substitution in context of readed name. */
    struct tl_list *var = tl_list_get(t->variables, t->name);
    for (var = var->list; var; var = var->next) {
        char *c;
        tl_state  state    = tl_state_text;
        struct tl automata = { 0, 0, 0, 0 };
        automata.variables = tl_list_union(var->list, t->variables);
        for (c = t->body; *c; ++c) {
            state = state(&automata, *c);
        }
        tl_free(&automata);
    }
}

static void *tl_state_body(struct tl *t, char c)
{  /* Read the body until } respecting nesting and call tl_at. */
    if (c == '{') ++t->opened;
    else if (c == '}' && !t->opened--) {
        tl_at(t);
        t->opened = 0;
        tl_string_free(t->name);
        t->name = 0;
        tl_string_free(t->body);
        t->body = 0;
        return tl_state_text;
    }
    tl_string_add(&t->body, c);
    return tl_state_body;
}

static void *tl_state_backslash(struct tl *t, char c)
{  /* Output newline for \n, nothing for \ and newline, and copy the rest. */
    if (c == 'n') putchar('\n');
    else if (c != '\n') putchar(c);
    return tl_state_text;
}