Mercurial > hg > index.cgi
annotate lwcc/cpp-main.c @ 304:d85d173ba120 ccdev
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
The preprocessor is currently runnable but doesn't actually do anything
useful. This is just a checkpoint.
author | William Astle <lost@l-w.ca> |
---|---|
date | Tue, 17 Sep 2013 19:33:41 -0600 |
parents | 4b17780f2777 |
children | 54f213c8fb81 |
rev | line source |
---|---|
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 } | |
124 else | |
125 { | |
126 char *s; | |
127 lw_stringlist_reset(input_files); | |
128 for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files)) | |
129 { | |
130 retval = process_file(s); | |
131 if (retval != 0) | |
132 break; | |
133 } | |
134 } | |
135 lw_stringlist_destroy(input_files); | |
136 | |
137 // symbol_dump(); | |
138 exit(retval); | |
139 } | |
140 | |
141 int process_file(const char *fn) | |
142 { | |
143 struct preproc_info *pp; | |
304
d85d173ba120
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
144 struct token *tok = NULL; |
295 | 145 |
146 pp = preproc_init(fn); | |
147 if (!pp) | |
148 return -1; | |
149 | |
150 for (;;) | |
151 { | |
304
d85d173ba120
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
152 tok = preproc_next(pp); |
295 | 153 if (tok -> ttype == TOK_EOF) |
154 break; | |
155 token_print(tok, output_fp); | |
304
d85d173ba120
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
156 token_free(tok); |
295 | 157 } |
304
d85d173ba120
Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
295
diff
changeset
|
158 token_free(tok); |
295 | 159 preproc_finish(pp); |
160 return 0; | |
161 } | |
162 | |
163 static void do_error(const char *f, ...) | |
164 { | |
165 va_list args; | |
166 va_start(args, f); | |
167 fprintf(stderr, "ERROR: "); | |
168 vfprintf(stderr, f, args); | |
169 va_end(args); | |
170 fprintf(stderr, "\n"); | |
171 exit(1); | |
172 } |