annotate lwcc/cpp/main.c @ 292:40ecbd5da481 ccdev

Part one of the C preprocessor This is part one of the C preprocessor. It finds and then fails to intepret directives. Also handles line splicing and trigraphs.
author William Astle <lost@l-w.ca>
date Sun, 08 Sep 2013 21:58:12 -0600
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
292
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/cpp/main.c
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
3
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
5
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
7
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
12
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
17
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
21
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <errno.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <stdio.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
24 #include <stdlib.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <string.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
26
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
27 #include <lw_stringlist.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include <lw_cmdline.h>
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
29
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
30 #include "cpp.h"
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
31
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
32 /* command line option handling */
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
33 #define PROGVER "lwcc-cpp from " PACKAGE_STRING
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
34 char *program_name;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
35
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
36 /* input files */
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
37 lw_stringlist_t input_files;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
38
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
39 /* various flags */
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
40 int trigraphs = 0;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
41 char *output_file = NULL;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
42 FILE *output_fp = NULL;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
43
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
44 static struct lw_cmdline_options options[] =
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
45 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
46 { "output", 'o', "FILE", 0, "Output to FILE"},
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
47 { "include", 'i', "FILE", 0, "Pre-include FILE" },
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
48 { "includedir", 'I', "PATH", 0, "Add entry to the user include path" },
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
49 { "sincludedir", 'S', "PATH", 0, "Add entry to the system include path" },
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
50 { "define", 'D', "SYM[=VAL]",0, "Automatically define SYM to be VAL (or 1)"},
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
51 { "trigraphs", 0x100, NULL, 0, "Enable interpretation of trigraphs" },
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
52 { 0 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
53 };
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
54
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
55
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
56 static int parse_opts(int key, char *arg, void *state)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
57 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
58 switch (key)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
59 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
60 case 'o':
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
61 if (output_file)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
62 do_error("Output file specified more than once.");
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
63 output_file = arg;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
64 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
65
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
66 case 0x100:
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
67 trigraphs = 1;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
68 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
69
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
70 case lw_cmdline_key_end:
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
71 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
72
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
73 case lw_cmdline_key_arg:
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
74 lw_stringlist_addstring(input_files, arg);
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
75 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
76
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
77 default:
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
78 return lw_cmdline_err_unknown;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
79 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
80 return 0;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
81 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
82
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
83 static struct lw_cmdline_parser cmdline_parser =
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
84 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
85 options,
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
86 parse_opts,
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
87 "INPUTFILE",
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
88 "lwcc-cpp - C preprocessor for lwcc",
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
89 PROGVER
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
90 };
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
91
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
92 int main(int argc, char **argv)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
93 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
94 program_name = argv[0];
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
95 int retval = 0;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
96
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
97 input_files = lw_stringlist_create();
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
98
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
99 /* parse command line arguments */
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
100 lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL);
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
101
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
102 /* set up output file */
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
103 if (output_file == NULL || strcmp(output_file, "-") == 0)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
104 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
105 output_fp = stdout;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
106 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
107 else
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
108 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
109 output_fp = fopen(output_file, "wb");
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
110 if (output_fp == NULL)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
111 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
112 do_error("Failed to create output file %s: %s", output_file, strerror(errno));
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
113 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
114 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
115
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
116 if (lw_stringlist_nstrings(input_files) == 0)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
117 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
118 /* if no input files, work on stdin */
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
119 retval = process_file("-");
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
120 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
121 else
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
122 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
123 char *s;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
124 lw_stringlist_reset(input_files);
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
125 for (s = lw_stringlist_current(input_files); s; s = lw_stringlist_next(input_files))
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
126 {
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
127 retval = process_file(s);
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
128 if (retval != 0)
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
129 break;
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
130 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
131 }
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
132 lw_stringlist_destroy(input_files);
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
133 exit(retval);
40ecbd5da481 Part one of the C preprocessor
William Astle <lost@l-w.ca>
parents:
diff changeset
134 }