summaryrefslogtreecommitdiff
path: root/Master/tlpkg/archive/lib/C/tlp-src.c
blob: a67aacbb74fe1f63d0030a7a85c03fe8293a27ea (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
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
/* tlp-src.c - module for using tlpsrc files
 *
 * Copyright 2007 Jjgod Jiang
 * Derived from the Perl implementation by Norbert Preining
 *
 * This file is licensed under the GNU General Public License version 2
 * or any later version. */

#include <tlp-src.h>
#include <stdio.h>

/* internal function prototypes {{{ */

tlp_result tlp_src_process_line(tlp_src *src, tlp_str line, 
                                int *started, int *finished, 
                                const char *file);

tlp_result tlp_str_get_word(tlp_str str, tlp_str word, int max);

tlp_result tlp_src_get_name(tlp_str     line, 
                            tlp_src    *src, 
                            int        *started, 
                            const char *file);

tlp_result tlp_src_get_category(tlp_str     line, 
                                tlp_src    *src, 
                                int        *started, 
                                const char *file);

/* }}} */

/* internal data structures {{{ */

struct tlp_src_field {
    const char  *key;
    tlp_result (*func)(tlp_str     line, 
                       tlp_src    *src, 
                       int        *started, 
                       const char *file);
};

/* for lines start with "name", we use function tlp_src_get_name() to 
 * process it, and feed it with the current tlp_src instance. */

struct tlp_src_field tlp_src_fields[] = {
    { "name",          tlp_src_get_name        }, 
    { "category",      tlp_src_get_category    }, /*
    { "shortdesc",     tlp_src_get_shortdesc   }, 
    { "longdesc",      tlp_src_get_longdesc    },
    { "catalogue",     tlp_src_get_catalogue   },

    { "runpatterns",   tlp_src_get_runpatterns },
    { "srcpatterns",   tlp_src_get_srcpatterns },
    { "docpatterns",   tlp_src_get_docpatterns },
    { "binpatterns",   tlp_src_get_binpatterns },

    { "execute",       tlp_src_get_execute     },
    { "depend",        tlp_src_get_depend      },  */
};

#define TLP_SRC_TOTAL_FIELDS    (sizeof(tlp_src_fields) / sizeof(tlp_src_fields[0]))

/* }}} */

/* tlp_src related {{{ */

tlp_src *tlp_src_new(tlp_str name, 
                     tlp_str category, 
                     tlp_str shortdesc, 
                     tlp_str longdesc, 
                     tlp_str catalogue, 
                     tlp_pattern *runpatterns, 
                     tlp_pattern *srcpatterns, 
                     tlp_pattern *docpatterns,
                     tlp_pattern *binpatterns,
                     tlp_execute *executes, 
                     tlp_depend  *depends)
{
    tlp_src *src = tlp_new(tlp_src);
    if (! src)
        return NULL;

    src->name      = tlp_str_copy(name);
    src->category  = tlp_str_copy(category);
    src->shortdesc = tlp_str_copy(shortdesc);
    src->longdesc  = tlp_str_copy(longdesc);
    src->catalogue = tlp_str_copy(catalogue);

    src->runpatterns = runpatterns;
    src->srcpatterns = srcpatterns;
    src->docpatterns = docpatterns;
    src->binpatterns = binpatterns;

    src->executes = executes;
    src->depends  = depends;

    return src;
}

void tlp_src_free(tlp_src *src)
{
    if (! src)
        return;

    tlp_str_free(src->name);
    tlp_str_free(src->category);
    tlp_str_free(src->shortdesc);
    tlp_str_free(src->longdesc);
    tlp_str_free(src->catalogue);

    tlp_patterns_free(src->runpatterns);
    tlp_patterns_free(src->srcpatterns);
    tlp_patterns_free(src->docpatterns);
    tlp_patterns_free(src->binpatterns);

    tlp_executes_free(src->executes);
    tlp_depends_free(src->depends);

    free(src);
    src = NULL;
}

tlp_src *tlp_src_from_file(char *file)
{
    FILE    *fp;
    tlp_src *src;
    tlp_char line[TLP_SRC_LINE_MAX];
    int      started, finished;

    fp = fopen(file, "r");
    if (! fp)
        return NULL;

    src = tlp_new(tlp_src);

    /* TODO: if the argument is not readable as is, try looking 
     * for it in the hierarchy where we are.*/
    if (! src)
        return NULL;

    started = finished = 0;

    while (tlp_fgets(line, TLP_SRC_LINE_MAX, fp))
    {
        if (tlp_src_process_line(src, line, 
                                 &started, 
                                 &finished, 
                                 file) != TLP_OK)
            goto failed;
    }

    fclose(fp);
    return src;

failed:
    tlp_src_free(src);
    fclose(fp);
    return NULL;
}

tlp_result tlp_src_process_line(tlp_src *src, tlp_str line, 
                                int *started, int *finished, 
                                const char *file)
{
    int i;

    /* skip all leading spaces */
    for (i = 0; tlp_char_is_space(line[i]); i++)
        ;

    /* skip comment lines */
    if (line[i] == TLP_CHAR_COMMENT)
        return TLP_OK;

    if (tlp_char_is_eol(line[i]))
    {
        if (! *started || *finished)
            return TLP_OK;

        tlp_error_msg("%s: empty line not allowed in tlpsrc\n", file);
        return TLP_FAILED;
    }

    /* something like "   name ..." is not allowed */
    if (line[0] == ' ')
    {
        tlp_error_msg("%s: continuation line not allowed in tlpsrc: ", file);
        tlp_error_str(line);

        return TLP_FAILED;
    }

    for (i = 0; i < TLP_SRC_TOTAL_FIELDS; i++)
        if (tlp_str_start_with_bytes(line, tlp_src_fields[i].key))
            return tlp_src_fields[i].func(line, src, started, file);

    tlp_error_msg("%s: unknown tlpsrc directive, please fix: %s", 
                  file, line);
    return TLP_OK;
}

tlp_result tlp_src_get_name(tlp_str     line, 
                            tlp_src    *src, 
                            int        *started, 
                            const char *file)
{
    tlp_char name[TLP_SRC_NAME_MAX];

    if (*started)
    {
        tlp_error_msg("%s: tlpsrc cannot have two name directives", file);
        return TLP_FAILED;
    }

    if (tlp_str_get_word(line + tlp_str_len(TLP_STR("name")), 
                         name, TLP_SRC_NAME_MAX) == TLP_OK)
    {
        src->name = tlp_str_copy(name);
        *started = 1;

        return TLP_OK;
    }
    else
        return TLP_FAILED;
}

tlp_result tlp_src_get_category(tlp_str     line, 
                                tlp_src    *src, 
                                int        *started, 
                                const char *file)
{
    if (! *started)
    {
        tlp_error_msg("%s: first tlpsrc directive must be 'name'", file);
        return TLP_FAILED;
    }

    return TLP_OK;
}

/* }}} */

/* tlp_pattern related {{{ */

void tlp_pattern_free(tlp_pattern *pat)
{
    if (pat != NULL)
        return;

    switch (pat->type)
    {
    case TLP_PATTERN_T:
        tlp_str_list_free(pat->u.t);
        break;

    case TLP_PATTERN_F:
        tlp_str_free(pat->u.f);
        break;

    case TLP_PATTERN_D:
        tlp_str_free(pat->u.d);
        break;

    case TLP_PATTERN_R:
        tlp_str_free(pat->u.r);
        break;
    }

    free(pat);
    pat = NULL;
}

void tlp_patterns_free(tlp_pattern *pat)
{
    tlp_pattern *node = pat, *temp;

    while (node != NULL)
    {
        temp = node->next;
        tlp_pattern_free(node);
        node = temp;
    }

    pat = NULL;
}

/* }}} */

/* remaining stuff used in tlp_src processing {{{ */

tlp_result tlp_str_get_word(tlp_str str, tlp_str word, int max)
{
    int i, j;

    /* skip all leading spaces */
    for (i = 0; tlp_char_is_space(str[i]); i++)
        ;

    j = 0;
    for (; ! tlp_char_is_eol(str[i]); i++)
    {
        if (str[i] != TLP_CHAR('-') &&
            ! tlp_char_in_word(str[i]))
            return TLP_FAILED;

        if (j >= max)
            return TLP_FAILED;

        word[j++] = str[i];
    }

    if (j == 0)
        return TLP_FAILED;

    word[j] = TLP_CHAR_END;
    return TLP_OK;
}

/* }}} */

/* vim:set ts=4 sts=4 sw=4 expandtab foldmethod=marker: */