File: main.c

package info (click to toggle)
libexplain 0.52.D002-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 34,656 kB
  • sloc: ansic: 138,224; makefile: 36,642; sh: 15,448; yacc: 1,442; awk: 246
file content (183 lines) | stat: -rw-r--r-- 4,565 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
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
/*
 * libexplain - Explain errno values returned by libc functions
 * Copyright (C) 2008-2011 Peter Miller
 * Written by Peter Miller <pmiller@opensource.org.au>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <libexplain/ac/assert.h>
#include <libexplain/ac/getopt.h>
#include <libexplain/ac/stdio.h>
#include <libexplain/ac/stdlib.h>
#include <libexplain/ac/string.h>
#include <libexplain/ac/unistd.h>

#include <libexplain/output.h>
#include <libexplain/version_print.h>

#include <codegen/catalogue.h>
#include <codegen/elastic_buffer.h>
#include <codegen/generate.h>
#include <codegen/gram.h>
#include <codegen/ioctl_scan.h>


static void
usage(void)
{
    fprintf(stderr, "Usage: codegen <declaration>\n");
    fprintf(stderr, "       codegen -i <include-file>\n");
    fprintf(stderr, "       codegen -V\n");
    exit(EXIT_FAILURE);
}


#ifdef HAVE_GETOPT_LONG

static const struct option options[] =
{
    { "ioctl-scan-include", 1, 0, 'i' },
    { "ioctl-scan-generate", 1, 0, 'I' },
    { "lisp", 0, 0, 'l' },
    { "specific", 1, 0, 'g' },
    { "version", 0, 0, 'V' },
    { 0, 0, 0, 0 }
};

#endif


int
main(int argc, char **argv)
{
    const char      *decl;
    node_t          *np;
    int             lisp;
    catalogue_t     *cp;
    int             save_at_end;

    lisp = 0;
    for (;;)
    {
#ifdef HAVE_GETOPT_LONG
        int c = getopt_long(argc, argv, "g:I:i:lV", options, 0);
#else
        int c = getopt(argc, argv, "g:I:i:lV");
#endif
        if (c < 0)
            break;
        switch (c)
        {
        case 'g':
            generate_specific(optarg);
            break;

        case 'I':
            ioctl_scan_generate(optarg);
            return 0;

        case 'i':
            ioctl_scan_include(optarg);
            return 0;

        case 'l':
            lisp = 1;
            break;

        case 'V':
            explain_version_print();
            return 0;

        default:
            usage();
            /* NOTREACHED */
        }
    }
    if (optind + 1 != argc)
        usage();
    decl = argv[optind];
    save_at_end = !!strchr(decl, '(');
    if (!save_at_end)
    {
        char            catpath[300];

        if (strchr(decl, '/'))
        {
            explain_output_error_and_die("did you mean \"-g %s\" ?!?", decl);
        }
        snprintf(catpath, sizeof(catpath), "catalogue/%s", decl);
        /* using prototype from message catalogue */
        cp = catalogue_open(catpath);
        decl = catalogue_get(cp, "Prototype");
        if (!decl)
        {
            explain_output_error_and_die("catalogue has no Prototype");
        }
        np = grammar(decl);
        if (lisp)
        {
            node_print_lisp(np, stdout);
            return 0;
        }
    }
    else
    {
        char            catpath[300];

        np = grammar(decl);
        if (lisp)
        {
            node_print_lisp(np, stdout);
            return 0;
        }
        node_print(np, stdout);
        putchar('\n');

        /*
         * Make there is only one name declared.
         */
        assert(node_is(np, "declaration"));
        assert(np->nchild >= 1);
        if (np->nchild != 3)
        {
            explain_output_error_and_die("no names declared");
        }
        assert(node_is(np->child[0], "declaration_specifiers"));
        assert(node_is(np->child[1], "init_declarator_list"));
        if (np->child[1]->nchild != 1)
        {
            explain_output_error_and_die("too many names declared");
        }

        snprintf
        (
            catpath,
            sizeof(catpath),
            "catalogue/%s",
            find_function_name(np)
        );
        cp = catalogue_creat(catpath);
        catalogue_set(cp, "Prototype", decl);
        catalogue_save(cp);

        /* don't actually generate anything */
        generate_specific("garbage");
    }

    generate(np, cp);
    if (save_at_end)
        catalogue_save(cp);
    return 0;
}