annotate lwcc/cpp-main.c @ 560:dba08c7dff96

Fix off by one handling MOD directive optional arguments Fixes an off by one in handling the optional arguments to the MOD directive as of changeset 928c033c0cd0. Thanks to Alex Evans <varmfskii@gmail.com> for reporting the problem and a suggested fix. This fix is different but should be more stable should the rest of the parsing code be refactored in the future.
author William Astle <lost@l-w.ca>
date Fri, 22 Sep 2023 12:15:09 -0600
parents a38542cf4cc6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/cpp-main.c
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
3
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
5
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
7
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
12
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
17
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
21
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <errno.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <stdarg.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
24 #include <stdio.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <stdlib.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
26 #include <string.h>
313
73b2bfa17ab0 Checkpoint
William Astle <lost@l-w.ca>
parents: 307
diff changeset
27 #include <lw_alloc.h>
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include <lw_stringlist.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
29 #include <lw_cmdline.h>
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
30 #include <lw_string.h>
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
31
496
a38542cf4cc6 Make lwcc stuff compile (on Linux anyway)
William Astle <lost@l-w.ca>
parents: 313
diff changeset
32 #include <version.h>
a38542cf4cc6 Make lwcc stuff compile (on Linux anyway)
William Astle <lost@l-w.ca>
parents: 313
diff changeset
33
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
34 #include "cpp.h"
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
35
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
36 int process_file(const char *);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
37 static void do_error(const char *f, ...);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
38
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
39 /* command line option handling */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
40 #define PROGVER "lwcc-cpp from " PACKAGE_STRING
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
41 char *program_name;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
42
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
43 /* input files */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
44 lw_stringlist_t input_files;
306
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
45 lw_stringlist_t includedirs;
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
46 lw_stringlist_t sysincludedirs;
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
47 lw_stringlist_t macrolist;
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
48
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
49 /* various flags */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
50 int trigraphs = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
51 char *output_file = NULL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
52 FILE *output_fp = NULL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
53
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
54 static struct lw_cmdline_options options[] =
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
55 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
56 { "output", 'o', "FILE", 0, "Output to FILE"},
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
57 { "include", 'i', "FILE", 0, "Pre-include FILE" },
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
58 { "includedir", 'I', "PATH", 0, "Add entry to the user include path" },
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
59 { "sincludedir", 'S', "PATH", 0, "Add entry to the system include path" },
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
60 { "define", 'D', "SYM[=VAL]",0, "Automatically define SYM to be VAL (or 1)"},
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
61 { "trigraphs", 0x100, NULL, 0, "Enable interpretation of trigraphs" },
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
62 { 0 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
63 };
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
64
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
65 static int parse_opts(int key, char *arg, void *state)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
66 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
67 switch (key)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
68 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
69 case 'o':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
70 if (output_file)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
71 do_error("Output file specified more than once.");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
72 output_file = arg;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
73 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
74
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
75 case 0x100:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
76 trigraphs = 1;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
77 break;
306
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
78
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
79 case 'I':
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
80 lw_stringlist_addstring(includedirs, arg);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
81 break;
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
82
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
83 case 'S':
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
84 lw_stringlist_addstring(sysincludedirs, arg);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
85 break;
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
86
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
87 case 'D':
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
88 lw_stringlist_addstring(macrolist, arg);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
89 break;
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
90
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
91 case lw_cmdline_key_end:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
92 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
93
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
94 case lw_cmdline_key_arg:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
95 lw_stringlist_addstring(input_files, arg);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
96 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
97
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
98 default:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
99 return lw_cmdline_err_unknown;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
100 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
101 return 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
102 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
103
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
104 static struct lw_cmdline_parser cmdline_parser =
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
105 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
106 options,
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
107 parse_opts,
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
108 "INPUTFILE",
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
109 "lwcc-cpp - C preprocessor for lwcc",
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
110 PROGVER
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
111 };
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
112
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
113 int main(int argc, char **argv)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
114 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
115 program_name = argv[0];
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
116 int retval = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
117
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
118 input_files = lw_stringlist_create();
306
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
119 includedirs = lw_stringlist_create();
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
120 sysincludedirs = lw_stringlist_create();
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
121 macrolist = lw_stringlist_create();
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
122
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
123 /* parse command line arguments */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
124 lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
125
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
126 /* set up output file */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
127 if (output_file == NULL || strcmp(output_file, "-") == 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
128 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
129 output_fp = stdout;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
130 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
131 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
132 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
133 output_fp = fopen(output_file, "wb");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
134 if (output_fp == NULL)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
135 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
136 do_error("Failed to create output file %s: %s", output_file, strerror(errno));
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
137 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
138 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
139
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
140 if (lw_stringlist_nstrings(input_files) == 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
141 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
142 /* if no input files, work on stdin */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
143 retval = process_file("-");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
144 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
145 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
146 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
147 char *s;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
148 lw_stringlist_reset(input_files);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
149 for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files))
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
150 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
151 retval = process_file(s);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
152 if (retval != 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
153 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
154 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
155 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
156 lw_stringlist_destroy(input_files);
306
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
157 lw_stringlist_destroy(includedirs);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
158 lw_stringlist_destroy(sysincludedirs);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
159 lw_stringlist_destroy(macrolist);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
160 exit(retval);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
161 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
162
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
163 static void print_line_marker(FILE *fp, int line, const char *fn, int flag)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
164 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
165 fprintf(fp, "\n# %d \"", line);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
166 while (*fn)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
167 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
168 if (*fn < 32 || *fn == 34 || *fn > 126)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
169 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
170 fprintf(fp, "\\%03o", *fn);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
171 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
172 else
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
173 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
174 fprintf(fp, "%c", *fn);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
175 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
176 fn++;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
177 }
307
9e342c4e4b66 Added support for __LINE__, __FILE__, __DATE__, and __TIME__
William Astle <lost@l-w.ca>
parents: 306
diff changeset
178 fprintf(fp, "\" %d", flag);
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
179 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
180
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
181 int process_file(const char *fn)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
182 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
183 struct preproc_info *pp;
304
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents: 295
diff changeset
184 struct token *tok = NULL;
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
185 int last_line = 0;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
186 char *last_fn = NULL;
306
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
187 char *tstr;
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
188
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
189 pp = preproc_init(fn);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
190 if (!pp)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
191 return -1;
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
192
306
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
193 /* set up the include paths */
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
194 lw_stringlist_reset(includedirs);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
195 for (tstr = lw_stringlist_current(includedirs); tstr; tstr = lw_stringlist_next(includedirs))
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
196 {
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
197 preproc_add_include(pp, tstr, 0);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
198 }
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
199
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
200 lw_stringlist_reset(sysincludedirs);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
201 for (tstr = lw_stringlist_current(sysincludedirs); tstr; tstr = lw_stringlist_next(sysincludedirs))
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
202 {
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
203 preproc_add_include(pp, tstr, 1);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
204 }
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
205
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
206 /* set up pre-defined macros */
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
207 lw_stringlist_reset(macrolist);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
208 for (tstr = lw_stringlist_current(macrolist); tstr; tstr = lw_stringlist_next(macrolist))
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
209 {
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
210 preproc_add_macro(pp, tstr);
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
211 }
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
212
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
213 print_line_marker(output_fp, 1, fn, 1);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
214 last_fn = lw_strdup(fn);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
215 for (;;)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
216 {
304
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents: 295
diff changeset
217 tok = preproc_next(pp);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
218 if (tok -> ttype == TOK_EOF)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
219 break;
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
220 if (strcmp(tok -> fn, last_fn) != 0)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
221 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
222 int lt = 1;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
223 if (tok -> lineno != 1)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
224 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
225 lt = 2;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
226 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
227 lw_free(last_fn);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
228 last_fn = lw_strdup(tok -> fn);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
229 last_line = tok -> lineno;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
230 print_line_marker(output_fp, last_line, last_fn, lt);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
231 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
232 else
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
233 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
234 while (tok -> lineno > last_line)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
235 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
236 fprintf(output_fp, "\n");
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
237 last_line++;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
238 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
239 }
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
240 token_print(tok, output_fp);
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
241 if (tok -> ttype == TOK_EOL)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
242 last_line++;
304
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents: 295
diff changeset
243 token_free(tok);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
244 }
304
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents: 295
diff changeset
245 token_free(tok);
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
246 lw_free(last_fn);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
247 // symtab_dump(pp);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
248 preproc_finish(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
249 return 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
250 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
251
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
252 static void do_error(const char *f, ...)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
253 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
254 va_list args;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
255 va_start(args, f);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
256 fprintf(stderr, "ERROR: ");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
257 vfprintf(stderr, f, args);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
258 va_end(args);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
259 fprintf(stderr, "\n");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
260 exit(1);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
261 }