295
|
1 /*
|
|
2 lwcc/cpp.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 <stdarg.h>
|
|
23 #include <stdio.h>
|
|
24 #include <stdlib.h>
|
|
25 #include <string.h>
|
|
26
|
|
27 #include <lw_alloc.h>
|
|
28 #include <lw_string.h>
|
|
29
|
|
30 #include "cpp.h"
|
|
31
|
|
32 struct token *preproc_lex_next_token(struct preproc_info *);
|
|
33
|
|
34 struct preproc_info *preproc_init(const char *fn)
|
|
35 {
|
|
36 FILE *fp;
|
|
37 struct preproc_info *pp;
|
|
38
|
|
39 if (!fn || (fn[0] == '-' && fn[1] == '0'))
|
|
40 {
|
|
41 fp = stdin;
|
|
42 }
|
|
43 else
|
|
44 {
|
|
45 fp = fopen(fn, "rb");
|
|
46 }
|
|
47 if (!fp)
|
|
48 return NULL;
|
|
49
|
|
50 pp = lw_alloc(sizeof(struct preproc_info));
|
|
51 memset(pp, 0, sizeof(struct preproc_info));
|
|
52 pp -> fn = lw_strdup(fn);
|
|
53 pp -> fp = fp;
|
|
54 pp -> ra = CPP_NOUNG;
|
296
|
55 pp -> ppeolseen = 1;
|
295
|
56 return pp;
|
|
57 }
|
|
58
|
|
59 struct token *preproc_next_token(struct preproc_info *pp)
|
|
60 {
|
|
61 struct token *t;
|
|
62
|
296
|
63 if (pp -> curtok)
|
|
64 token_free(pp -> curtok);
|
|
65
|
|
66 /*
|
|
67 If there is a list of tokens to process, move it to the "unget" queue
|
|
68 with an EOF marker at the end of it.
|
|
69 */
|
|
70 if (pp -> sourcelist)
|
|
71 {
|
|
72 for (t = pp -> sourcelist; t -> next; t = t -> next)
|
|
73 /* do nothing */ ;
|
|
74 t -> next = token_create(TOK_EOF, NULL, -1, -1, "");
|
|
75 t -> next -> next = pp -> tokqueue;
|
|
76 pp -> tokqueue = pp -> sourcelist;
|
|
77 pp -> sourcelist = NULL;
|
|
78 }
|
|
79 again:
|
295
|
80 if (pp -> tokqueue)
|
|
81 {
|
|
82 t = pp -> tokqueue;
|
|
83 pp -> tokqueue = t -> next;
|
|
84 if (pp -> tokqueue)
|
|
85 pp -> tokqueue -> prev = NULL;
|
|
86 t -> next = NULL;
|
|
87 t -> prev = NULL;
|
296
|
88 pp -> curtok = t;
|
|
89 goto ret;
|
295
|
90 }
|
296
|
91 pp -> curtok = preproc_lex_next_token(pp);
|
|
92 t = pp -> curtok;
|
|
93 ret:
|
|
94 if (t -> ttype == TOK_ENDEXPAND)
|
|
95 {
|
|
96 struct expand_e *e;
|
|
97 e = pp -> expand_list;
|
|
98 pp -> expand_list = e -> next;
|
|
99 lw_free(e);
|
|
100 goto again;
|
|
101 }
|
|
102 return t;
|
|
103 }
|
|
104
|
|
105 void preproc_unget_token(struct preproc_info *pp, struct token *t)
|
|
106 {
|
|
107 t -> next = pp -> tokqueue;
|
|
108 pp -> tokqueue = t;
|
|
109 if (pp -> curtok == t)
|
|
110 pp -> curtok = NULL;
|
295
|
111 }
|
|
112
|
|
113 void preproc_finish(struct preproc_info *pp)
|
|
114 {
|
|
115 lw_free((void *)(pp -> fn));
|
|
116 fclose(pp -> fp);
|
296
|
117 if (pp -> curtok)
|
|
118 token_free(pp -> curtok);
|
|
119 while (pp -> tokqueue)
|
|
120 {
|
|
121 preproc_next_token(pp);
|
|
122 token_free(pp -> curtok);
|
|
123 }
|
295
|
124 lw_free(pp);
|
|
125 }
|
|
126
|
|
127 void preproc_register_error_callback(struct preproc_info *pp, void (*cb)(const char *))
|
|
128 {
|
|
129 pp -> errorcb = cb;
|
|
130 }
|
|
131
|
|
132 void preproc_register_warning_callback(struct preproc_info *pp, void (*cb)(const char *))
|
|
133 {
|
|
134 pp -> warningcb = cb;
|
|
135 }
|
|
136
|
|
137 static void preproc_throw_error_default(const char *m)
|
|
138 {
|
|
139 fprintf(stderr, "ERROR: %s\n", m);
|
|
140 }
|
|
141
|
|
142 static void preproc_throw_warning_default(const char *m)
|
|
143 {
|
|
144 fprintf(stderr, "WARNING: %s\n", m);
|
|
145 }
|
|
146
|
|
147 static void preproc_throw_message(void (*cb)(const char *), const char *m, va_list args)
|
|
148 {
|
|
149 int s;
|
|
150 char *b;
|
|
151
|
|
152 s = vsnprintf(NULL, 0, m, args);
|
|
153 b = lw_alloc(s + 1);
|
|
154 vsnprintf(b, s + 1, m, args);
|
|
155 (*cb)(b);
|
|
156 lw_free(b);
|
|
157 }
|
|
158
|
|
159 void preproc_throw_error(struct preproc_info *pp, const char *m, ...)
|
|
160 {
|
|
161 va_list args;
|
|
162 va_start(args, m);
|
|
163 preproc_throw_message(pp -> errorcb ? pp -> errorcb : preproc_throw_error_default, m, args);
|
|
164 va_end(args);
|
|
165 exit(1);
|
|
166 }
|
|
167
|
|
168 void preproc_throw_warning(struct preproc_info *pp, const char *m, ...)
|
|
169 {
|
|
170 va_list args;
|
|
171 va_start(args, m);
|
|
172 preproc_throw_message(pp -> warningcb ? pp -> warningcb : preproc_throw_warning_default, m, args);
|
|
173 va_end(args);
|
|
174 }
|