summaryrefslogtreecommitdiff
path: root/Radio/HW/BladeRF/common/src/parse.c
blob: dd6abe1c519ec7348360e7d11a0f7b07eb157b9b (plain) (blame)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * Copyright (c) 2017 Nuand LLC.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "parse.h"
#include "conversions.h"
#include "log.h"

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

static char **add_arg(
    char **argv, int argc, const char *buf, int start, int end, int quote_count)
{
    char **rv;
    char *d_ptr;
    int i;
    int len;

    char c;
    char quote_char;

    quote_char = 0;

    rv = (char **)realloc(argv, sizeof(char *) * (argc + 1));
    if (rv == NULL) {
        return NULL;
    }

    rv[argc] = NULL;

    len = end - start + 1;

    d_ptr = (char *)malloc(len + 1 - quote_count * 2);
    if (d_ptr == NULL) {
        free(rv);
        return NULL;
    }

    rv[argc - 1] = d_ptr;

    for (i = 0; i < len; i++) {
        c = buf[start + i];

        if (!quote_char) {
            /* We are not in a quote, copy everything but quote chars */
            if (c == '"' || c == '\'') {
                quote_char = c;
            } else {
                *d_ptr++ = c;
            }
        } else {
            /* We are in a quote, copy everything but the current quote char */
            if (c == quote_char) {
                quote_char = 0;
            } else {
                *d_ptr++ = c;
            }
        }
    }
    *d_ptr = '\0';

    return rv;
}

int str2args(const char *line, char comment_char, char ***argv)
{
    char **rv;
    int argc;

    unsigned i;
    size_t len;

    bool in_arg;
    char c;
    char quote_char;

    int arg_start;
    int quote_count;

    rv   = NULL;
    argc = 0;

    quote_char = 0;

    arg_start   = 0;
    quote_count = 0;

    len = strlen(line);

    in_arg = false;

    for (i = 0; i < len; i++) {
        c = line[i];

        if (in_arg) {
            /* Found the end of a quote! */
            if (quote_char) {
                if (quote_char == c) {
                    quote_char = 0;
                }
                continue;
            }

            /* Found the beginning of a quote! */
            if (c == '\'' || c == '"') {
                quote_count++;
                quote_char = c;
                continue;
            }

            /* Found whitespace outside of quote */
            if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
                in_arg = false;
                argc++;
                rv = add_arg(rv, argc, line, arg_start, i - 1, quote_count);
                if (rv == NULL)
                    return -1;
            }
        } else {
            if (c == comment_char) {
                break;
            }
            /* Enter a new argument */
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                /* If first argument is a tick it means we're in a quote */
                if (c == '\'' || c == '"') {
                    quote_char = c;
                } else {
                    quote_char = 0;
                }
                quote_count = 0;
                arg_start   = i;
                in_arg      = true;
            }
            /* else this is still whitespace */
        }
    }

    /* reached the end of the line, check to see if current arg needs to
     * be closed */
    if (in_arg) {
        if (quote_char) {
            free_args(argc, rv);
            return -2;
        } else {
            argc++;
            rv = add_arg(rv, argc, line, arg_start, i - 1, quote_count);
            if (rv == NULL) {
                return -1;
            }
        }
    }

    *argv = rv;

    return argc;
}

void free_args(int argc, char **argv)
{
    int i;
    for (i = 0; i < argc; i++) {
        free(argv[i]);
    }
    free(argv);
}

static struct config_options *add_opt(
    struct config_options *optv, int optc, char *key, char *val, int lineno)
{
    struct config_options *rv;
    char *ptr1, *ptr2;
    rv = (struct config_options *)realloc(optv,
                                          sizeof(struct config_options) * optc);
    if (rv == NULL) {
        return NULL;
    }

    ptr1 = (char *)malloc(strlen(key) + 1);
    if (ptr1 == NULL) {
        free(rv);
        return NULL;
    }
    strcpy(ptr1, key);
    rv[optc - 1].key = ptr1;

    ptr2 = (char *)malloc(strlen(val) + 1);
    if (ptr2 == NULL) {
        free(ptr1);
        free(rv);
        return NULL;
    }
    strcpy(ptr2, val);
    rv[optc - 1].value = ptr2;

    rv[optc - 1].lineno = lineno;

    return rv;
}

bool update_match(struct bladerf *dev, char *str)
{
    size_t len;
    int status;
    struct bladerf_devinfo info;
    bladerf_fpga_size fpga_size;

    status = bladerf_get_devinfo(dev, &info);
    if (status < 0)
        return false;

    bladerf_get_fpga_size(dev, &fpga_size);
    if (status < 0)
        return false;

    str++;
    len = strlen(str);
    if (str[len - 1] == ']')
        str[len - 1] = '\0';

    if (!strcmp(str, "x40")) {
        return fpga_size == BLADERF_FPGA_40KLE;
    } else if (!strcmp(str, "x115")) {
        return fpga_size == BLADERF_FPGA_115KLE;
    }

    status = bladerf_devstr_matches(str, &info);

    return status == 1;
}

int str2options(struct bladerf *dev,
                const char *buf,
                size_t buf_sz,
                struct config_options **opts)
{
    char *line;
    char *d_ptr;
    int line_num;
    char c;
    unsigned i;

    struct config_options *optv;
    int optc;

    char **line_argv;
    int line_argc;

    bool match;

    match = true;

    optv = NULL;
    optc = 0;

    line_num = 1;

    line = malloc(buf_sz + 1);
    if (!line)
        return BLADERF_ERR_MEM;

    d_ptr = line;

    for (i = 0; i < buf_sz; i++) {
        c = buf[i];
        if (c == '\n') {
            /* deal with the old line */
            *d_ptr    = 0;
            line_argc = str2args(line, '#', &line_argv);
            if (line_argc < 0)
                goto out;

            /* handle line */
            if (line_argc > 3) {
                log_error("Too many arguments in bladeRF.conf on line %d\n",
                          line_num);
                goto out;
            } else if (match && line_argc == 2) {
                optc++;
                optv =
                    add_opt(optv, optc, line_argv[0], line_argv[1], line_num);
                if (!optv) {
                    optc = -1;
                    goto out;
                }
            } else if (line_argc == 1) {
                if (*line_argv[0] != '[') {
                    log_error("Expecting scoping line (requires [ and ]) on "
                              "line %d\n",
                              line_num);
                }
                match = update_match(dev, line_argv[0]);
            }

            /* free line */
            free_args(line_argc, line_argv);

            /* setup to capture the next line */
            line_num++;
            d_ptr = line;
        } else {
            *d_ptr++ = c;
        }
    }

    if (opts)
        *opts = optv;

out:
    free(line);
    return optc;
}

void free_opts(struct config_options *optv, int optc)
{
    int i;

    for (i = 0; i < optc; i++) {
        free(optv[i].key);
        free(optv[i].value);
    }
    free(optv);
}

int csv2int(const char *line, int ***args)
{
    const char delim[]   = " \r\n\t,.:"; /* supported delimiters */
    const size_t MAXLEN  = 128;  /* max line length (with newline and null) */
    static size_t arglen = 2;    /* tunable: initial expected column count */
    char *myline         = NULL; /* local copy of 'line' */
    char *parsestr       = NULL; /* ptr to 'myline' on first strtok_r */
    char *saveptr        = NULL; /* strtok_r state pointer */
    int **argout         = NULL; /* array of output values */
    size_t count         = 0;    /* count of tokens extracted */
    size_t i;

    // Validity check
    if (NULL == line) {
        log_debug("line is null\n");
        return 0;
    }

    if (NULL == args) {
        log_error("args is null\n");
        goto fail;
    }

    // strtok_r doesn't respect const, so make a copy of 'line'
    myline = calloc(MAXLEN, 1);
    if (NULL == myline) {
        log_error("could not calloc myline\n");
        goto fail;
    }

    myline = strncpy(myline, line, MAXLEN - 1);

    // Initial allocation of argout
    argout = malloc(arglen * sizeof(int *));
    if (NULL == argout) {
        log_error("could not malloc argout\n");
        goto fail;
    }

    // Loop over input until strtok_r returns a NULL
    for (i = 0, parsestr = myline; true; ++i, parsestr = NULL) {
        char *token = NULL; /* return token from strtok_r */
        bool ok;

        token = strtok_r(parsestr, delim, &saveptr);
        if (NULL == token) {
            break;
        }

        // Expand argout if necessary
        if (i >= arglen) {
            arglen *= 2;
            log_verbose("expanding allocation to %zu column(s)\n", arglen);
            int **newargout = realloc(argout, arglen * sizeof(int *));
            if (NULL == newargout) {
                log_error("could not realloc(argout,%zu)\n", arglen);
                goto fail;
            }
            argout = newargout;
        }

        // Allocate memory for this value
        argout[i] = malloc(sizeof(int));
        if (NULL == argout[i]) {
            log_error("could not malloc argout[%zu]\n", i);
            goto fail;
        }

        // Update the count now, in case str2int fails and we need to dealloc
        ++count;

        // Parse token into an integer value
        *argout[i] = str2int(token, INT32_MIN, INT32_MAX, &ok);
        if (!ok) {
            log_error("str2int failed on '%s'\n", token);
            goto fail;
        }
    }

    // Success!
    *args = argout;
    free(myline);

    // If arglen is too big, cut it in half next time...
    if (count <= (arglen / 2)) {
        arglen /= 2;
        log_verbose("decreasing future arglen to %zu\n", arglen);
    }

    return (int)count;

fail:
    // Deallocate everything...
    free(myline);
    free_csv2int((int)count, argout);
    return -1;
}

void free_csv2int(int argc, int **args)
{
    int i;

    if (NULL == args) {
        return;
    }

    for (i = 0; i < argc; ++i) {
        free(args[i]);
    }

    free(args);
}