summaryrefslogtreecommitdiffstats
path: root/src/btparser/utils.c
blob: 1de329a7e69dedf7670cab87094a0b7907c3bb6b (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
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
/*
    utils.c

    Copyright (C) 2010  Red Hat, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "utils.h"
#include "location.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <regex.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

bool btp_debug_parser = false;

void *
btp_malloc(size_t size)
{
    void *ptr = malloc(size);
    if (ptr == NULL)
    {
        fprintf(stderr, "btp: out of memory");
        exit(1);
    }
    return ptr;
}

char *
btp_vasprintf(const char *format, va_list p)
{
    int r;
    char *string_ptr;

#if 1
    // GNU extension
    r = vasprintf(&string_ptr, format, p);
#else
    // Bloat for systems that haven't got the GNU extension.
    va_list p2;
    va_copy(p2, p);
    r = vsnprintf(NULL, 0, format, p);
    string_ptr = xmalloc(r+1);
    r = vsnprintf(string_ptr, r+1, format, p2);
    va_end(p2);
#endif

    if (r < 0)
    {
        fprintf(stderr, "btp: out of memory");
        exit(1);
    }

    return string_ptr;
}

char *
btp_strdup(const char *s)
{
    return btp_strndup(s, strlen(s));
}

char *
btp_strndup(const char *s, size_t n)
{
    char *result = strndup(s, n);
    if (result == NULL)
    {
        fprintf(stderr, "btp: out of memory");
        exit(1);
    }
    return result;
}

int
btp_strcmp0(const char *s1, const char *s2)
{
    if (!s1)
    {
        if (s2)
            return -1;
        return 0;
    }
    else
    {
        if (!s2)
            return 1;
        /* Both are non-null. */
        return strcmp(s1, s2);
    }
}

char *
btp_strchr_location(const char *s, int c, int *line, int *column)
{
    *line = 1;
    *column = 0;

    /* Scan s for the character.  When this loop is finished,
       s will either point to the end of the string or the
       character we were looking for.  */
    while (*s != '\0' && *s != (char)c)
    {
        btp_location_eat_char_ext(line, column, *s);
        ++s;
    }
    return ((*s == c) ? (char*)s : NULL);
}

char *
btp_strstr_location(const char *haystack,
                    const char *needle,
                    int *line,
                    int *column)
{
    *line = 1;
    *column = 0;
    size_t needlelen;

    /* Check for the null needle case.  */
    if (*needle == '\0')
        return (char*)haystack;

    needlelen = strlen(needle);
    int chrline, chrcolumn;
    for (;(haystack = btp_strchr_location(haystack, *needle, &chrline, &chrcolumn)) != NULL; ++haystack)
    {
        btp_location_add_ext(line, column, chrline, chrcolumn);

        if (strncmp(haystack, needle, needlelen) == 0)
            return (char*)haystack;

        btp_location_eat_char_ext(line, column, *haystack);
    }
    return NULL;
}

size_t
btp_strspn_location(const char *s,
                    const char *accept,
                    int *line,
                    int *column)
{
    *line = 1;
    *column = 0;
    const char *sc;
    for (sc = s; *sc != '\0'; ++sc)
    {
        if (strchr(accept, *sc) == NULL)
            return (sc - s);

        btp_location_eat_char_ext(line, column, *sc);
    }
    return sc - s; /* terminating nulls don't match */
}

char *
btp_file_to_string(const char *filename)
{
    /* Open input file, and parse it. */
    int fd = open(filename, O_RDONLY | O_LARGEFILE);
    if (fd < 0)
    {
        fprintf(stderr, "Unable to open '%s': %s.\n",
                filename, strerror(errno));
        return NULL;
    }

    off_t size = lseek(fd, 0, SEEK_END);
    if (size < 0) /* EOVERFLOW? */
    {
        fprintf(stderr, "Unable to seek in '%s': %s.\n",
                filename, strerror(errno));
    }

    lseek(fd, 0, SEEK_SET); /* No reason to fail. */

    static const size_t FILE_SIZE_LIMIT = 20000000; /* ~ 20 MB */
    if (size > FILE_SIZE_LIMIT)
    {
        fprintf(stderr, "Input file too big (%lld). Maximum size is %zd.\n",
                (long long)size, FILE_SIZE_LIMIT);
        close(fd);
        return NULL;
    }

    char *contents = btp_malloc(size + 1);
    if (size != read(fd, contents, size))
    {
        fprintf(stderr, "Unable to read from '%s'.\n", filename);
        close(fd);
        free(contents);
        return NULL;
    }

    /* Just reading, so no need to check the returned value. */
    close(fd);

    contents[size] = '\0';
    return contents;
}

bool
btp_skip_char(char **input, char c)
{
    if (**input != c)
        return false;
    ++*input;
    return true;
}

bool
btp_skip_char_limited(char **input, const char *allowed)
{
    if (strchr(allowed, **input) == NULL)
        return false;
    ++*input;
    return true;
}

bool
btp_parse_char_limited(char **input, const char *allowed, char *result)
{
    if (**input == '\0')
        return false;
    if (strchr(allowed, **input) == NULL)
        return false;
    *result = **input;
    ++*input;
    return true;
}

int
btp_skip_char_sequence(char **input, char c)
{
    int count = 0;

    /* Skip all the occurences of c. */
    while (btp_skip_char(input, c))
        ++count;

    return count;
}

int
btp_skip_char_span(char **input, const char *chars)
{
    size_t count = strspn(*input, chars);
    if (0 == count)
        return count;
    *input += count;
    return count;
}

int
btp_skip_char_span_location(char **input,
                            const char *chars,
                            int *line,
                            int *column)
{
    size_t count = btp_strspn_location(*input, chars, line, column);
    if (0 == count)
        return count;
    *input += count;
    return count;
}

int
btp_parse_char_span(char **input, const char *accept, char **result)
{
    size_t count = strspn(*input, accept);
    if (count == 0)
        return 0;
    *result = btp_strndup(*input, count);
    *input += count;
    return count;
}

bool
btp_parse_char_cspan(char **input, const char *reject, char **result)
{
    size_t count = strcspn(*input, reject);
    if (count == 0)
        return false;
    *result = btp_strndup(*input, count);
    *input += count;
    return true;
}

int
btp_skip_string(char **input, const char *string)
{
    char *local_input = *input;
    const char *local_string = string;
    while (*local_string && *local_input && *local_input == *local_string)
    {
        ++local_input;
        ++local_string;
    }
    if (*local_string != '\0')
        return 0;
    int count = local_input - *input;
    *input = local_input;
    return count;
}

bool
btp_parse_string(char **input, const char *string, char **result)
{
    char *local_input = *input;
    const char *local_string = string;
    while (*local_string && *local_input && *local_input == *local_string)
    {
        ++local_input;
        ++local_string;
    }
    if (*local_string != '\0')
        return false;
    *result = btp_strndup(string, local_input - *input);
    *input = local_input;
    return true;
}

char
btp_parse_digit(char **input)
{
    char digit = **input;
    if (digit < '0' || digit > '9')
        return '\0';
    ++*input;
    return digit;
}

int
btp_skip_unsigned_integer(char **input)
{
    return btp_skip_char_span(input, "0123456789");
}

int
btp_parse_unsigned_integer(char **input, unsigned *result)
{
    char *local_input = *input;
    char *numstr;
    int length = btp_parse_char_span(&local_input,
                                     "0123456789",
                                     &numstr);
    if (0 == length)
        return 0;

    char *endptr;
    errno = 0;
    unsigned long r = strtoul(numstr, &endptr, 10);
    bool failure = (errno || numstr == endptr || *endptr != '\0'
                    || r > UINT_MAX);
    free(numstr);
    if (failure) /* number too big or some other error */
        return 0;
    *result = r;
    *input = local_input;
    return length;
}

int
btp_skip_hexadecimal_number(char **input)
{
    char *local_input = *input;
    if (!btp_skip_char(&local_input, '0'))
        return 0;
    if (!btp_skip_char(&local_input, 'x'))
        return 0;
    int count = 2;
    count += btp_skip_char_span(&local_input, "abcdef0123456789");
    if (2 == count) /* btp_skip_char_span returned 0 */
        return 0;
    *input = local_input;
    return count;
}

int
btp_parse_hexadecimal_number(char **input, uint64_t *result)
{
    char *local_input = *input;
    if (!btp_skip_char(&local_input, '0'))
        return 0;
    if (!btp_skip_char(&local_input, 'x'))
        return 0;
    int count = 2;
    char *numstr;
    count += btp_parse_char_span(&local_input,
                                 "abcdef0123456789",
                                 &numstr);

    if (2 == count) /* btp_parse_char_span returned 0 */
        return 0;
    char *endptr;
    errno = 0;
    unsigned long long r = strtoull(numstr, &endptr, 16);
    bool failure = (errno || numstr == endptr || *endptr != '\0');
    free(numstr);
    if (failure) /* number too big or some other error */
        return 0;
    *result = r;
    *input = local_input;
    return count;
}