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