aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.c
blob: 0e91510d75b4898d3bfa5c3a39b23f40e58abb36 (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
#include "common.h"
#include "parse.h"
#include "obj.h"
#include <stdlib.h>
#include <unistd.h>

struct option ccopt;

static void
flushstd(void)
{
    ioflush(&bstdout);
    ioflush(&bstderr);
}

/* parse an argument of the form 'opt=abcd'
 * e.g.  arg="foo=bar123"; opt="foo"; returns "bar123" */
static const char *
optval(const char *arg, const char *opt)
{
    uint n1 = strlen(arg), n2 = strlen(opt);
    if (n1 < n2+1 || memcmp(arg, opt, n2) != 0 || arg[n2] != '=')
        return NULL;
    return arg + n2 + 1;
}

static void
optparse(const char **file, const char **targ, char **args)
{
    const char *arg, *x;
    *file = *targ = NULL;
    while ((arg = *++args)) {
        if (*arg++ != '-') {
            *file = arg-1;
            continue;
        }
        if ((x = optval(arg, "std"))) {
            if (!strcmp(x, "c89") || !strcmp(x, "c90")) ccopt.cstd = STDC89;
            else if (!strcmp(x, "c99")) ccopt.cstd = STDC99;
            else if (!strcmp(x, "c11")) ccopt.cstd = STDC11;
            else if (!strcmp(x, "c2x")) ccopt.cstd = STDC23;
            else if (!strcmp(x, "c23")) ccopt.cstd = STDC23;
            else goto Bad;
        } else if (!strcmp(arg, "pedantic")) {
            ccopt.pedant = 1;
        } else if (*arg == 'd' && arg[1]) {
            /* see common.h§struct option */
            while (*++arg) switch (*arg | 32) {
            case 'p': ccopt.dbg.p = 1; break;
            case 'a': ccopt.dbg.a = 1; break;
            case 'r': ccopt.dbg.r = 1; break;
            default: warn(NULL, "-d: invalid debug flag %'c", *arg);
            }
        } else Bad: warn(NULL, "invalid option: %'s", arg-1);
    }
}

static void
detectcolor(void)
{
    const char *s;
    if (!isatty(STDERR_FILENO)
     || ((s = getenv("NO_COLOR")) && *s)
     || ((s = getenv("TERM")) && !strcmp(s, "dumb")))
        ccopt.nocolor = 1;
}

int
main(int argc, char **argv)
{
    struct parser pr;
    const char *file, *targ;

    atexit(flushstd);
    detectcolor();
    ccopt.cstd = STDC99; /* C99 by default */
    optparse(&file, &targ, argv);
    if (!file) {
        efmt("usage: %s [options] <file>\n", *argv);
        return 1;
    }

    targ_init(targ ? targ : "amd64-sysv");
    objini("a.out");
    initparser(&pr, file);
    parse(&pr);
    if (!nerror) objfini();
}