File: getbool.c

package info (click to toggle)
nasm 3.01-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,660 kB
  • sloc: ansic: 129,101; asm: 40,471; perl: 8,238; sh: 4,146; makefile: 1,281; xml: 726; python: 582; lisp: 578; sed: 11
file content (76 lines) | stat: -rw-r--r-- 1,748 bytes parent folder | download
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
/* SPDX-License-Identifier: BSD-2-Clause */
/* Copyright 2025 The NASM Authors - All Rights Reserved */

#include "compiler.h"
#include "nasm.h"
#include "asmutil.h"
#include "stdscan.h"
#include "eval.h"

/*
 * 1. An expression (true if nonzero 0)
 * 2. The keywords true, on, yes for true
 * 3. The keywords false, off, no for false
 * 4. An empty line, for true
 *
 * This is equivalent to pp_get_boolean_option() outside of the
 * preprocessor.
 *
 * On error, return defval (usually the previous value)
 *
 * If str is NULL, return NULL without changing *val.
 */
char *get_boolean_option(const char *str, bool *val)
{
    static const char * const noyes[] = {
        "no", "yes",
        "false", "true",
        "off", "on"
    };
    struct tokenval tokval;
    expr *evalresult;
    char *p;
    int tt;

    if (!str)
        return NULL;

    str = nasm_skip_spaces(str);
    p = nasm_strdup(str);

    tokval.t_type  = TOKEN_INVALID;
    tokval.t_start = str;
    stdscan_reset(p);

    tt = stdscan(NULL, &tokval);
    if (tt == TOKEN_EOS || tt == ']' || tt == ',') {
        *val = true;
        goto done;
    }

    if (tt == TOKEN_ID) {
        size_t i;
        for (i = 0; i < ARRAY_SIZE(noyes); i++)
            if (!nasm_stricmp(tokval.t_charptr, noyes[i])) {
                *val = i & 1;
                goto done;
            }
    }

    evalresult = evaluate(stdscan, NULL, &tokval, NULL, true, NULL);

    if (!evalresult)
        goto done;

    if (!is_really_simple(evalresult)) {
        nasm_nonfatal("boolean flag expression must be a constant");
        goto done;
    }

    *val = reloc_value(evalresult) != 0;

done:
    str += nasm_skip_spaces(tokval.t_start) - p;
    nasm_free(p);
    return (char *)str;
}