comparison lwcc/cc-main.c @ 311:7957e90d0a35 ccdev

Add skeleton compiler target to build
author William Astle <lost@l-w.ca>
date Sat, 21 Sep 2013 13:53:18 -0600
parents
children 41118fb0a8f2
comparison
equal deleted inserted replaced
309:65ed8dde5331 311:7957e90d0a35
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 #include <lw_string.h>
31
32 #include "cpp.h"
33
34 int process_file(const char *);
35 static void do_error(const char *f, ...);
36
37 /* command line option handling */
38 #define PROGVER "lwcc-cc from " PACKAGE_STRING
39 char *program_name;
40
41 /* input files */
42 lw_stringlist_t input_files;
43 lw_stringlist_t includedirs;
44 lw_stringlist_t sysincludedirs;
45 lw_stringlist_t macrolist;
46
47 /* various flags */
48 int trigraphs = 0;
49 char *output_file = NULL;
50 FILE *output_fp = NULL;
51
52 static struct lw_cmdline_options options[] =
53 {
54 { "output", 'o', "FILE", 0, "Output to FILE"},
55 { "include", 'i', "FILE", 0, "Pre-include FILE" },
56 { "includedir", 'I', "PATH", 0, "Add entry to the user include path" },
57 { "sincludedir", 'S', "PATH", 0, "Add entry to the system include path" },
58 { "define", 'D', "SYM[=VAL]",0, "Automatically define SYM to be VAL (or 1)"},
59 { "trigraphs", 0x100, NULL, 0, "Enable interpretation of trigraphs" },
60 { 0 }
61 };
62
63 static int parse_opts(int key, char *arg, void *state)
64 {
65 switch (key)
66 {
67 case 'o':
68 if (output_file)
69 do_error("Output file specified more than once.");
70 output_file = arg;
71 break;
72
73 case 0x100:
74 trigraphs = 1;
75 break;
76
77 case 'I':
78 lw_stringlist_addstring(includedirs, arg);
79 break;
80
81 case 'S':
82 lw_stringlist_addstring(sysincludedirs, arg);
83 break;
84
85 case 'D':
86 lw_stringlist_addstring(macrolist, arg);
87 break;
88
89 case lw_cmdline_key_end:
90 break;
91
92 case lw_cmdline_key_arg:
93 lw_stringlist_addstring(input_files, arg);
94 break;
95
96 default:
97 return lw_cmdline_err_unknown;
98 }
99 return 0;
100 }
101
102 static struct lw_cmdline_parser cmdline_parser =
103 {
104 options,
105 parse_opts,
106 "INPUTFILE",
107 "lwcc-cc - C compiler for lwcc",
108 PROGVER
109 };
110
111 int main(int argc, char **argv)
112 {
113 program_name = argv[0];
114 int retval = 0;
115
116 input_files = lw_stringlist_create();
117 includedirs = lw_stringlist_create();
118 sysincludedirs = lw_stringlist_create();
119 macrolist = lw_stringlist_create();
120
121 /* parse command line arguments */
122 lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL);
123
124 /* set up output file */
125 if (output_file == NULL || strcmp(output_file, "-") == 0)
126 {
127 output_fp = stdout;
128 }
129 else
130 {
131 output_fp = fopen(output_file, "wb");
132 if (output_fp == NULL)
133 {
134 do_error("Failed to create output file %s: %s", output_file, strerror(errno));
135 }
136 }
137
138 if (lw_stringlist_nstrings(input_files) == 0)
139 {
140 /* if no input files, work on stdin */
141 retval = process_file("-");
142 }
143 else
144 {
145 char *s;
146 lw_stringlist_reset(input_files);
147 for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files))
148 {
149 retval = process_file(s);
150 if (retval != 0)
151 break;
152 }
153 }
154 lw_stringlist_destroy(input_files);
155 lw_stringlist_destroy(includedirs);
156 lw_stringlist_destroy(sysincludedirs);
157 lw_stringlist_destroy(macrolist);
158 exit(retval);
159 }
160
161 static void print_line_marker(FILE *fp, int line, const char *fn, int flag)
162 {
163 fprintf(fp, "\n# %d \"", line);
164 while (*fn)
165 {
166 if (*fn < 32 || *fn == 34 || *fn > 126)
167 {
168 fprintf(fp, "\\%03o", *fn);
169 }
170 else
171 {
172 fprintf(fp, "%c", *fn);
173 }
174 fn++;
175 }
176 fprintf(fp, "\" %d", flag);
177 }
178
179 int process_file(const char *fn)
180 {
181 struct preproc_info *pp;
182 struct token *tok = NULL;
183 int last_line = 0;
184 char *last_fn = NULL;
185 char *tstr;
186
187 pp = preproc_init(fn);
188 if (!pp)
189 return -1;
190
191 /* set up the include paths */
192 lw_stringlist_reset(includedirs);
193 for (tstr = lw_stringlist_current(includedirs); tstr; tstr = lw_stringlist_next(includedirs))
194 {
195 preproc_add_include(pp, tstr, 0);
196 }
197
198 lw_stringlist_reset(sysincludedirs);
199 for (tstr = lw_stringlist_current(sysincludedirs); tstr; tstr = lw_stringlist_next(sysincludedirs))
200 {
201 preproc_add_include(pp, tstr, 1);
202 }
203
204 /* set up pre-defined macros */
205 lw_stringlist_reset(macrolist);
206 for (tstr = lw_stringlist_current(macrolist); tstr; tstr = lw_stringlist_next(macrolist))
207 {
208 preproc_add_macro(pp, tstr);
209 }
210
211 print_line_marker(output_fp, 1, fn, 1);
212 last_fn = lw_strdup(fn);
213 for (;;)
214 {
215 tok = preproc_next(pp);
216 if (tok -> ttype == TOK_EOF)
217 break;
218 if (strcmp(tok -> fn, last_fn) != 0)
219 {
220 int lt = 1;
221 if (tok -> lineno != 1)
222 {
223 lt = 2;
224 }
225 lw_free(last_fn);
226 last_fn = lw_strdup(tok -> fn);
227 last_line = tok -> lineno;
228 print_line_marker(output_fp, last_line, last_fn, lt);
229 }
230 else
231 {
232 while (tok -> lineno > last_line)
233 {
234 fprintf(output_fp, "\n");
235 last_line++;
236 }
237 }
238 token_print(tok, output_fp);
239 if (tok -> ttype == TOK_EOL)
240 last_line++;
241 token_free(tok);
242 }
243 token_free(tok);
244 lw_free(last_fn);
245 // symtab_dump(pp);
246 preproc_finish(pp);
247 return 0;
248 }
249
250 static void do_error(const char *f, ...)
251 {
252 va_list args;
253 va_start(args, f);
254 fprintf(stderr, "ERROR: ");
255 vfprintf(stderr, f, args);
256 va_end(args);
257 fprintf(stderr, "\n");
258 exit(1);
259 }