295
|
1 /*
|
|
2 lwcc/cpp-main.c
|
|
3
|
|
4 Copyright © 2013 William Astle
|
|
5
|
|
6 This file is part of LWTOOLS.
|
|
7
|
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
|
|
9 terms of the GNU General Public License as published by the Free Software
|
|
10 Foundation, either version 3 of the License, or (at your option) any later
|
|
11 version.
|
|
12
|
|
13 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
16 more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License along with
|
|
19 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include <errno.h>
|
|
23 #include <stdarg.h>
|
|
24 #include <stdio.h>
|
|
25 #include <stdlib.h>
|
|
26 #include <string.h>
|
|
27
|
|
28 #include <lw_stringlist.h>
|
|
29 #include <lw_cmdline.h>
|
|
30
|
|
31 #include "cpp.h"
|
|
32
|
|
33 int process_file(const char *);
|
|
34 static void do_error(const char *f, ...);
|
|
35
|
|
36 /* command line option handling */
|
|
37 #define PROGVER "lwcc-cpp from " PACKAGE_STRING
|
|
38 char *program_name;
|
|
39
|
|
40 /* input files */
|
|
41 lw_stringlist_t input_files;
|
|
42
|
|
43 /* various flags */
|
|
44 int trigraphs = 0;
|
|
45 char *output_file = NULL;
|
|
46 FILE *output_fp = NULL;
|
|
47
|
|
48 static struct lw_cmdline_options options[] =
|
|
49 {
|
|
50 { "output", 'o', "FILE", 0, "Output to FILE"},
|
|
51 { "include", 'i', "FILE", 0, "Pre-include FILE" },
|
|
52 { "includedir", 'I', "PATH", 0, "Add entry to the user include path" },
|
|
53 { "sincludedir", 'S', "PATH", 0, "Add entry to the system include path" },
|
|
54 { "define", 'D', "SYM[=VAL]",0, "Automatically define SYM to be VAL (or 1)"},
|
|
55 { "trigraphs", 0x100, NULL, 0, "Enable interpretation of trigraphs" },
|
|
56 { 0 }
|
|
57 };
|
|
58
|
|
59 static int parse_opts(int key, char *arg, void *state)
|
|
60 {
|
|
61 switch (key)
|
|
62 {
|
|
63 case 'o':
|
|
64 if (output_file)
|
|
65 do_error("Output file specified more than once.");
|
|
66 output_file = arg;
|
|
67 break;
|
|
68
|
|
69 case 0x100:
|
|
70 trigraphs = 1;
|
|
71 break;
|
|
72
|
|
73 case lw_cmdline_key_end:
|
|
74 break;
|
|
75
|
|
76 case lw_cmdline_key_arg:
|
|
77 lw_stringlist_addstring(input_files, arg);
|
|
78 break;
|
|
79
|
|
80 default:
|
|
81 return lw_cmdline_err_unknown;
|
|
82 }
|
|
83 return 0;
|
|
84 }
|
|
85
|
|
86 static struct lw_cmdline_parser cmdline_parser =
|
|
87 {
|
|
88 options,
|
|
89 parse_opts,
|
|
90 "INPUTFILE",
|
|
91 "lwcc-cpp - C preprocessor for lwcc",
|
|
92 PROGVER
|
|
93 };
|
|
94
|
|
95 int main(int argc, char **argv)
|
|
96 {
|
|
97 program_name = argv[0];
|
|
98 int retval = 0;
|
|
99
|
|
100 input_files = lw_stringlist_create();
|
|
101
|
|
102 /* parse command line arguments */
|
|
103 lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL);
|
|
104
|
|
105 /* set up output file */
|
|
106 if (output_file == NULL || strcmp(output_file, "-") == 0)
|
|
107 {
|
|
108 output_fp = stdout;
|
|
109 }
|
|
110 else
|
|
111 {
|
|
112 output_fp = fopen(output_file, "wb");
|
|
113 if (output_fp == NULL)
|
|
114 {
|
|
115 do_error("Failed to create output file %s: %s", output_file, strerror(errno));
|
|
116 }
|
|
117 }
|
|
118
|
|
119 if (lw_stringlist_nstrings(input_files) == 0)
|
|
120 {
|
|
121 /* if no input files, work on stdin */
|
|
122 retval = process_file("-");
|
|
123 retval = 1;
|
|
124 }
|
|
125 else
|
|
126 {
|
|
127 char *s;
|
|
128 lw_stringlist_reset(input_files);
|
|
129 for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files))
|
|
130 {
|
|
131 retval = process_file(s);
|
|
132 if (retval != 0)
|
|
133 break;
|
|
134 }
|
|
135 }
|
|
136 lw_stringlist_destroy(input_files);
|
|
137
|
|
138 // symbol_dump();
|
|
139 exit(retval);
|
|
140 }
|
|
141
|
|
142 int process_file(const char *fn)
|
|
143 {
|
|
144 struct preproc_info *pp;
|
|
145 struct token *tok;
|
|
146
|
|
147 pp = preproc_init(fn);
|
|
148 if (!pp)
|
|
149 return -1;
|
|
150
|
|
151 for (;;)
|
|
152 {
|
|
153 tok = preproc_next_token(pp);
|
|
154 if (tok -> ttype == TOK_EOF)
|
|
155 break;
|
|
156 token_print(tok, output_fp);
|
|
157 }
|
|
158 preproc_finish(pp);
|
|
159 return 0;
|
|
160 }
|
|
161
|
|
162 static void do_error(const char *f, ...)
|
|
163 {
|
|
164 va_list args;
|
|
165 va_start(args, f);
|
|
166 fprintf(stderr, "ERROR: ");
|
|
167 vfprintf(stderr, f, args);
|
|
168 va_end(args);
|
|
169 fprintf(stderr, "\n");
|
|
170 exit(1);
|
|
171 }
|