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