annotate lwcc/cpp/lwcpp.c @ 260:c5b1dd523ac4

Make default build use -O3 Because it yields such an impressive speedup, make the default build use -O3 instead of -g. If you need the debugging stuff, you can set CFLAGS=-g before building.
author William Astle <lost@l-w.ca>
date Mon, 04 Feb 2013 20:35:47 -0700
parents 68f41eaf44f2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
193
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
1 /*
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
2
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
3 This is the main source for lwcpp, the C preprocessor
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
4
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
5 Copyright © 2012 William Astle
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
6
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
7 This file is part of LWTOOLS.
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
8
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
9 LWTOOLS is free software: you can redistribute it and/or modify it under the
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
10 terms of the GNU General Public License as published by the Free Software
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
11 Foundation, either version 3 of the License, or (at your option) any later
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
12 version.
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
13
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
14 This program is distributed in the hope that it will be useful, but WITHOUT
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
17 more details.
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
18
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
19 You should have received a copy of the GNU General Public License along with
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
20 this program. If not, see <http://www.gnu.org/licenses/>.
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
21
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
22 */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
23
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
24 #include <stdio.h>
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
25 #include <stdlib.h>
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
26 #include <string.h>
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
27
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
28 #include <lw_alloc.h>
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
29 #include <lw_string.h>
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
30
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
31 /* command line option handling */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
32 #define PROGVER "lwcpp from " PACKAGE_STRING
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
33 char *program_name;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
34
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
35 /* global state */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
36 char *output_file = NULL;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
37 int debug_level = 0;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
38
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
39 static void do_help(void);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
40 static void do_usage(void);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
41
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
42 /*
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
43 NOTE:
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
44
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
45 We can't actually use a standard option parser here due to a raft of weird
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
46 command line syntax and we want to be somewhat compatible with various
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
47 other tools. That means we have a built-in help text that is preformatted.
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
48
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
49 */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
50
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
51 #define OPTARG(dest,src) do { char *___s = (src); if (!*___s) { if (i < argc) ___s = argv[i++]; else { fprintf(stderr, "Option %s requires an argument\n", arg); } } (dest) = ___s; } while (0)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
52 static void parse_cmdline(int argc, char **argv)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
53 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
54 int i = 1;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
55 int eargs = 0;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
56 char *arg;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
57
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
58 while (i < argc)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
59 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
60 arg = argv[i++];
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
61 if (!eargs && arg[0] == '-' && arg[1] != 0)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
62 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
63 /* we have an option here */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
64 if (arg[1] == '-' && arg[2] == 0)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
65 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
66 eargs = 1;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
67 continue;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
68 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
69
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
70 /* consume the '-' */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
71 arg++;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
72 if (!strcmp(arg, "-help") || !strcmp(arg, "?"))
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
73 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
74 /* --help */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
75 do_help();
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
76 exit(0);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
77 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
78 else if (!strcmp(arg, "-usage"))
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
79 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
80 /* --usage */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
81 do_usage();
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
82 exit(0);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
83 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
84 else if (!strcmp(arg, "version") || !strcmp(arg, "-version"))
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
85 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
86 /* --version */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
87 printf("%s\n", PROGVER);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
88 exit(0);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
89 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
90
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
91 switch (*arg)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
92 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
93 case 'o':
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
94 if (output_file)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
95 lw_free(output_file);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
96 OPTARG(output_file, arg + 1);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
97 continue;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
98
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
99 case 'd':
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
100 if (!arg[1])
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
101 debug_level = 50;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
102 else
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
103 debug_level = atoi(arg + 1);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
104 continue;
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
105 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
106
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
107 fprintf(stderr, "Unknown option: %s\n", arg);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
108 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
109 else
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
110 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
111 /* we have an input file here */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
112 printf("Input file: %s\n", arg);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
113 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
114 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
115 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
116
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
117 /*
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
118 static struct lw_cmdline_parser cmdline_parser =
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
119 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
120 options,
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
121 parse_opts,
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
122 "INPUTFILE",
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
123 "lwcc, a HD6309 and MC6809 cross-compiler\vPlease report bugs to lost@l-w.ca.",
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
124 PROGVER
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
125 };
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
126 */
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
127 int main(int argc, char **argv)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
128 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
129 program_name = argv[0];
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
130
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
131 parse_cmdline(argc, argv);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
132
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
133 if (!output_file)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
134 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
135 output_file = lw_strdup("a.out");
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
136 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
137
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
138 exit(0);
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
139 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
140
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
141 void do_usage(void)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
142 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
143 printf(
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
144 "Usage: %1$s [options] <input file>\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
145 " %1$s --help\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
146 " %1$s --version\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
147 " %1$s --usage\n",
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
148 program_name
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
149 );
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
150 }
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
151
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
152 void do_help(void)
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
153 {
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
154 printf(
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
155 "Usage: %s [options] <input file>\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
156 "lwcpp, the lwtools C preprocessor\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
157 "\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
158 " -d[LEVEL] enable debug output, optionally set verbosity\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
159 " level to LEVEL\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
160 " -o FILE specify the output file name\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
161 " -?, --help give this help message\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
162 " --usage print a short usage message\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
163 " -version, --version print program version\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
164 "\n"
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
165 "Please report bugs to lost@l-w.ca.\n",
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
166 program_name
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
167 );
68f41eaf44f2 Added lwcc, lwcpp stubs
lost@l-w.ca
parents:
diff changeset
168 }