File: template.c

package info (click to toggle)
prayer 1.3.5-dfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,596 kB
  • sloc: ansic: 43,163; makefile: 817; sh: 445; perl: 166
file content (231 lines) | stat: -rw-r--r-- 4,772 bytes parent folder | download | duplicates (6)
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
/* $Cambridge: hermes/src/prayer/lib/template.c,v 1.6 2008/09/16 09:59:57 dpc22 Exp $ */

/************************************************
 *    Prayer - a Webmail Interface              *
 ************************************************/

/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */

#include "lib.h"

/* Support routines for locating and parsing templates */

/* token is \S+ */

#define MAX_TOKEN (63)

char *
template_gettoken(char **sp, struct pool *pool)
{
    static char buf[MAX_TOKEN+1];
    char *s = *sp;
    char *start;
    int len;

    while (*s && Uisspace(*s))
        s++;

    start = s;
    while (*s && !Uisspace(*s))
        s++;

    if ((len = s-start) <= MAX_TOKEN) {
        memcpy(buf, start, len);
        buf[len] = '\0';
    } else
        buf[0] = '\0';
    
    while (*s && Uisspace(*s))
        s++;
    *sp = s;

    return((pool) ? pool_strdup(pool, buf) : buf);
}


/* Example Variable names:
 *  $a
 *  $a->b
 *  $a->{b}
 *  ${a->{b}}
 *  $list[3]->foo
 *
 */

#define MAX_VAR_LEN (63)

char *
template_getvar(char **sp, char *quotep, struct pool *pool)
{
    static char buf[MAX_VAR_LEN+1];
    char *s = *sp;
    int depth = 0;
    int len = 0;

    /* Quoted variable: |n : none, |c : canon, |u : url */
    if (quotep)
        *quotep = 'n';

    while (*s && Uisspace(*s))
        s++;

    if (!(s && (*s == '$')))
        return(NIL);

    buf[len++] = *s++;
    do {
        if (s[0] == '[') {
            s++;
            buf[0] = '@';
            buf[len++] = '-';
            while (isdigit(*s) && (len < MAX_VAR_LEN))
                buf[len++] = *s++;
            if (*s++ != ']')
                return(NIL);
            continue;
        }
        if ((s[0] == '-') && (s[1] == '>')) {
            buf[len++] = '-';
            s += 2;
            continue;
        }
        if (*s == '{') {
            s++;
            depth++;
        } else if (s[0] == '|' && s[1] != '\0'  && s[2] == '}') {
            if (quotep)
                *quotep = s[1];
            s += 3;
            if (depth <= 0)
                return(NIL);
            if (--depth == 0)
                break;
        } else if (*s == '}') {
            s++;
            if (depth <= 0)
                return(NIL);
            if (--depth == 0)
                break;
        } else if (isalnum(*s)) {
            buf[len++] = *s++;
        } else if (*s == '_') {
            buf[len++] = *s++;
        } else
            break;
    } while (*s && (len < MAX_VAR_LEN));
    
    if ((len == 0) || (len >= MAX_VAR_LEN) || (depth < 0))
        return(NIL);

    *sp = s;

    buf[len] = '\0';
    s = (buf[0] == '$') ? (buf+1) : buf;

    return((pool) ? pool_strdup(pool, s) : s);
}

char *
template_getlist(char **sp, struct pool *pool)
{
    static char buf[MAX_VAR_LEN+1];
    char *s = *sp;
    int len = 0;

    while (*s && Uisspace(*s))
        s++;

    if (!(s && (*s == '@')))
        return(NIL);

    buf[len++] = *s++;
    while ((isalnum(*s) || (*s == '_')) && (len < MAX_VAR_LEN)) {
        buf[len++] = *s++;
    }

    *sp = s;

    buf[len] = '\0';
    return((pool) ? pool_strdup(pool, buf) : buf);
}

/* expr is token or "quoted text" */

char *
template_getexpr(char **sp, struct pool *pool)
{
    char *result;
    char *s = *sp;
    char *start;
    int len;

    while (*s && Uisspace(*s))
        s++;

    if (*s == '"') {
        start = ++s;
        while (*s && (*s != '"'))
            s++;
    } else {
        start = s;
        while (*s && !Uisspace(*s))
            s++;
    }

    len = s-start;
    result = pool_alloc(pool, len+1);
    memcpy(result, start, len);
    result[len] = '\0';
    
    if (*s == '"')
        s++;

    while (*s && Uisspace(*s))
        s++;
    *sp = s;

    return(result);
}

/* ====================================================================== */

extern struct template_map_index template_map_index[];

struct template *
template_find(char *set, char *name, struct pool *pool)
{
    struct template_map_index *tmi = &template_map_index[0];
    struct template_map *tm;
    unsigned long count;
    unsigned long first, last, middle;
    int rc;

    while (tmi->name && (strcmp(tmi->name, set) != 0))
        tmi++;

    if (!tmi->name)
        return(NIL);

    tm    = tmi->template_map;
    count = *(tmi->count);

    first = 0;
    last = count;

    /* Binary chop */
    while (first < last) {
        middle = (first + last) / 2;
        rc = strcmp(tm[middle].name, name);

        if (rc == 0)
            return(tm[middle].template);
        else if (rc < 0)
            first = middle + 1;
        else
            last = middle;
    }
    /* Not found */
    return (NIL);
}