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