annotate macro.c @ 64:f50b4ea6cbfe

Prune single-line comments from (most) directive lines. Also, don't pass the string length to the directive processing functions, as half of them weren't honoring it. Instead, ensure that the directive line is terminated at the place the directive processing functions should stop looking at it.
author David A. Holland
date Sun, 31 Mar 2013 02:04:56 -0400
parents 2e25e55dba6b
children bbbf71859a21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
1 /*-
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
2 * Copyright (c) 2010 The NetBSD Foundation, Inc.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
3 * All rights reserved.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
4 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
6 * by David A. Holland.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
7 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
8 * Redistribution and use in source and binary forms, with or without
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
9 * modification, are permitted provided that the following conditions
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
10 * are met:
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
12 * notice, this list of conditions and the following disclaimer.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
15 * documentation and/or other materials provided with the distribution.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
16 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
28 */
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
29
33
a5acb7049e4c need stdint.h for uint16_t
David A. Holland
parents: 30
diff changeset
30 #include <stdint.h>
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
31 #include <stdlib.h>
76da41da923f added macro table
David A. Holland
parents:
diff changeset
32 #include <string.h>
76da41da923f added macro table
David A. Holland
parents:
diff changeset
33
76da41da923f added macro table
David A. Holland
parents:
diff changeset
34 #include "array.h"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
35 #include "mode.h"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
36 #include "place.h"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
37 #include "macro.h"
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
38 #include "output.h"
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
39
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
40 struct expansionitem {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
41 bool isstring;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
42 union {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
43 char *string;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
44 unsigned param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
45 };
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
46 };
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 42
diff changeset
47 DECLARRAY(expansionitem, static __unused);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 42
diff changeset
48 DEFARRAY(expansionitem, static);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
49
76da41da923f added macro table
David A. Holland
parents:
diff changeset
50 struct macro {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
51 struct place defplace;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
52 struct place expansionplace;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
53 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
54 char *name;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
55 bool hasparams;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
56 struct stringarray params;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
57 struct expansionitemarray expansion;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
58 bool inuse;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
59 };
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 42
diff changeset
60 DECLARRAY(macro, static __unused);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 42
diff changeset
61 DEFARRAY(macro, static);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 42
diff changeset
62 DECLARRAY(macroarray, static __unused);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 42
diff changeset
63 DEFARRAY(macroarray, static);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
64
76da41da923f added macro table
David A. Holland
parents:
diff changeset
65 static struct macroarrayarray macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
66 static unsigned total_macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
67 static unsigned hashmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
68
76da41da923f added macro table
David A. Holland
parents:
diff changeset
69 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
70 // macro structure ops
76da41da923f added macro table
David A. Holland
parents:
diff changeset
71
76da41da923f added macro table
David A. Holland
parents:
diff changeset
72 static
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
73 struct expansionitem *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
74 expansionitem_create_string(const char *string)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
75 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
76 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
77
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
78 ei = domalloc(sizeof(*ei));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
79 ei->isstring = true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
80 ei->string = dostrdup(string);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
81 return ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
82 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
83
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
84 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
85 struct expansionitem *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
86 expansionitem_create_stringlen(const char *string, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
87 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
88 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
89
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
90 ei = domalloc(sizeof(*ei));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
91 ei->isstring = true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
92 ei->string = dostrndup(string, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
93 return ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
94 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
95
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
96 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
97 struct expansionitem *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
98 expansionitem_create_param(unsigned param)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
99 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
100 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
101
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
102 ei = domalloc(sizeof(*ei));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
103 ei->isstring = false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
104 ei->param = param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
105 return ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
106 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
107
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
108 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
109 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
110 expansionitem_destroy(struct expansionitem *ei)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
111 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
112 if (ei->isstring) {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
113 dostrfree(ei->string);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
114 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
115 dofree(ei, sizeof(*ei));
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
116 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
117
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
118 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
119 bool
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
120 expansionitem_eq(const struct expansionitem *ei1,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
121 const struct expansionitem *ei2)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
122 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
123 if (ei1->isstring != ei2->isstring) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
124 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
125 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
126 if (ei1->isstring) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
127 if (strcmp(ei1->string, ei2->string) != 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
128 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
129 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
130 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
131 if (ei1->param != ei2->param) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
132 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
133 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
134 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
135 return true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
136 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
137
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
138 static
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
139 struct macro *
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
140 macro_create(struct place *p1, const char *name, unsigned hash,
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
141 struct place *p2)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
142 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
143 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
144
76da41da923f added macro table
David A. Holland
parents:
diff changeset
145 m = domalloc(sizeof(*m));
76da41da923f added macro table
David A. Holland
parents:
diff changeset
146 m->defplace = *p1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
147 m->expansionplace = *p2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
148 m->hash = hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
149 m->name = dostrdup(name);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
150 m->hasparams = false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
151 stringarray_init(&m->params);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
152 expansionitemarray_init(&m->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
153 m->inuse = false;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
154 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
155 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
156
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
157 DESTROYALL_ARRAY(expansionitem, );
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
158
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
159 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
160 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
161 macro_destroy(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
162 {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
163 expansionitemarray_destroyall(&m->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
164 expansionitemarray_cleanup(&m->expansion);
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
165 dostrfree(m->name);
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
166 dofree(m, sizeof(*m));
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
167 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
168
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
169 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
170 bool
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
171 macro_eq(const struct macro *m1, const struct macro *m2)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
172 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
173 unsigned num1, num2, i;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
174 struct expansionitem *ei1, *ei2;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
175 const char *p1, *p2;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
176
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
177 if (strcmp(m1->name, m2->name) != 0) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
178 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
179 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
180
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
181 if (m1->hasparams != m2->hasparams) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
182 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
183 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
184
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
185 num1 = expansionitemarray_num(&m1->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
186 num2 = expansionitemarray_num(&m2->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
187 if (num1 != num2) {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
188 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
189 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
190
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
191 for (i=0; i<num1; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
192 ei1 = expansionitemarray_get(&m1->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
193 ei2 = expansionitemarray_get(&m2->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
194 if (!expansionitem_eq(ei1, ei2)) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
195 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
196 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
197 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
198
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
199 num1 = stringarray_num(&m1->params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
200 num2 = stringarray_num(&m2->params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
201 if (num1 != num2) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
202 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
203 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
204
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
205 for (i=0; i<num1; i++) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
206 p1 = stringarray_get(&m1->params, i);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
207 p2 = stringarray_get(&m2->params, i);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
208 if (strcmp(p1, p2) != 0) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
209 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
210 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
211 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
212 return true;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
213 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
214
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
215 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
216 // macro table
76da41da923f added macro table
David A. Holland
parents:
diff changeset
217
76da41da923f added macro table
David A. Holland
parents:
diff changeset
218 /*
76da41da923f added macro table
David A. Holland
parents:
diff changeset
219 * Unless I've screwed up, this is something called Fletcher's Checksum
76da41da923f added macro table
David A. Holland
parents:
diff changeset
220 * that showed up in Dr. Dobbs in, according to my notes, May 1992. The
76da41da923f added macro table
David A. Holland
parents:
diff changeset
221 * implementation is new.
76da41da923f added macro table
David A. Holland
parents:
diff changeset
222 */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
223 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
224 unsigned
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
225 hashfunc(const char *s, size_t len)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
226 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
227 uint16_t x1, x2, a;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
228 size_t i;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
229
76da41da923f added macro table
David A. Holland
parents:
diff changeset
230 x1 = (uint16_t) (len >> 16);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
231 x2 = (uint16_t) (len);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
232 if (x1==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
233 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
234 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
235 if (x2==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
236 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
237 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
238
76da41da923f added macro table
David A. Holland
parents:
diff changeset
239 for (i=0; i<len; i+=2) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
240 if (i==len-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
241 a = (unsigned char)s[i];
76da41da923f added macro table
David A. Holland
parents:
diff changeset
242 /* don't run off the end of the array */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
243 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
244 else {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
245 a = (unsigned char)s[i] +
76da41da923f added macro table
David A. Holland
parents:
diff changeset
246 ((uint16_t)(unsigned char)s[i+1] << 8);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
247 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
248 x1 += a;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
249 if (x1 < a) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
250 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
251 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
252 x2 += x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
253 if (x2 < x1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
254 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
255 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
256 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
257
76da41da923f added macro table
David A. Holland
parents:
diff changeset
258 x1 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
259 x2 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
260 return ((uint32_t)x2)*65535U + x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
261 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
262
76da41da923f added macro table
David A. Holland
parents:
diff changeset
263 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
264 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
265 macrotable_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
266 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
267 unsigned i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
268
76da41da923f added macro table
David A. Holland
parents:
diff changeset
269 macroarrayarray_init(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
270 macroarrayarray_setsize(&macros, 4);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
271 for (i=0; i<4; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
272 macroarrayarray_set(&macros, i, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
273 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
274 total_macros = 0;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
275 hashmask = 0x3;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
276 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
277
76da41da923f added macro table
David A. Holland
parents:
diff changeset
278 DESTROYALL_ARRAY(macro, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
279
76da41da923f added macro table
David A. Holland
parents:
diff changeset
280 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
281 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
282 macrotable_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
283 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
284 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
285 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
286
76da41da923f added macro table
David A. Holland
parents:
diff changeset
287 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
288 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
289 bucket = macroarrayarray_get(&macros, i);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
290 if (bucket != NULL) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
291 macroarray_destroyall(bucket);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
292 macroarray_destroy(bucket);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
293 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
294 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
295 macroarrayarray_setsize(&macros, 0);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
296 macroarrayarray_cleanup(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
297 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
298
76da41da923f added macro table
David A. Holland
parents:
diff changeset
299 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
300 struct macro *
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
301 macrotable_findlen(const char *name, size_t len, bool remove)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
302 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
303 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
304 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
305 struct macro *m, *m2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
306 unsigned i, num;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
307 size_t mlen;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
308
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
309 hash = hashfunc(name, len);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
310 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
311 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
312 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
313 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
314 num = macroarray_num(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
315 for (i=0; i<num; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
316 m = macroarray_get(bucket, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
317 if (hash != m->hash) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
318 continue;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
319 }
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
320 mlen = strlen(m->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
321 if (len == mlen && !memcmp(name, m->name, len)) {
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
322 if (remove) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
323 if (i < num-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
324 m2 = macroarray_get(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
325 macroarray_set(bucket, i, m2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
326 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
327 macroarray_setsize(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
328 total_macros--;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
329 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
330 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
331 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
332 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
333 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
334 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
335
76da41da923f added macro table
David A. Holland
parents:
diff changeset
336 static
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
337 struct macro *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
338 macrotable_find(const char *name, bool remove)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
339 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
340 return macrotable_findlen(name, strlen(name), remove);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
341 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
342
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
343 static
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
344 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
345 macrotable_rehash(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
346 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
347 struct macroarray *newbucket, *oldbucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
348 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
349 unsigned newmask, tossbit;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
350 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
351 unsigned oldnum, j, k;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
352
76da41da923f added macro table
David A. Holland
parents:
diff changeset
353 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
354 macroarrayarray_setsize(&macros, numbuckets*2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
355
76da41da923f added macro table
David A. Holland
parents:
diff changeset
356 assert(hashmask == numbuckets - 1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
357 newmask = (hashmask << 1) | 1U;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
358 tossbit = newmask && ~hashmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
359 hashmask = newmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
360
76da41da923f added macro table
David A. Holland
parents:
diff changeset
361 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
362 newbucket = NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
363 oldbucket = macroarrayarray_get(&macros, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
364 oldnum = macroarray_num(oldbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
365 for (j=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
366 m = macroarray_get(oldbucket, j);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
367 if (m->hash & tossbit) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
368 if (newbucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
369 newbucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
370 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
371 macroarray_set(oldbucket, j, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
372 macroarray_add(newbucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
373 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
374 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
375 for (j=k=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
376 m = macroarray_get(oldbucket, j);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
377 if (m != NULL && k < j) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
378 macroarray_set(oldbucket, k++, m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
379 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
380 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
381 macroarray_setsize(oldbucket, k);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
382 macroarrayarray_set(&macros, numbuckets + i, newbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
383 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
384 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
385
76da41da923f added macro table
David A. Holland
parents:
diff changeset
386 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
387 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
388 macrotable_add(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
389 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
390 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
391 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
392 unsigned numbuckets;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
393
76da41da923f added macro table
David A. Holland
parents:
diff changeset
394 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
395 if (total_macros > 0 && total_macros / numbuckets > 9) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
396 macrotable_rehash();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
397 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
398
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
399 hash = hashfunc(m->name, strlen(m->name));
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
400 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
401 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
402 bucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
403 macroarrayarray_set(&macros, hash & hashmask, bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
404 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
405 macroarray_add(bucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
406 total_macros++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
407 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
408
76da41da923f added macro table
David A. Holland
parents:
diff changeset
409 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
410 // external macro definition interface
76da41da923f added macro table
David A. Holland
parents:
diff changeset
411
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
412 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
413 struct macro *
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
414 macro_define_common_start(struct place *p1, const char *macro,
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
415 struct place *p2)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
416 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
417 struct macro *m;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
418 unsigned hash;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
419
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
420 if (!is_identifier(macro)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
421 complain(p1, "Invalid macro name %s", macro);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
422 complain_fail();
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
423 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
424
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
425 hash = hashfunc(macro, strlen(macro));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
426 m = macro_create(p1, macro, hash, p2);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
427 return m;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
428 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
429
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
430 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
431 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
432 macro_define_common_end(struct macro *m)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
433 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
434 struct macro *oldm;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
435 bool ok;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
436
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
437 oldm = macrotable_find(m->name, false);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
438 if (oldm != NULL) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
439 ok = macro_eq(m, oldm);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
440 if (ok) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
441 complain(&m->defplace,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
442 "Warning: redefinition of %s", m->name);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
443 if (mode.werror) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
444 complain_fail();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
445 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
446 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
447 complain(&m->defplace,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
448 "Redefinition of %s is not identical",
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
449 m->name);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
450 complain_fail();
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
451 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
452 complain(&oldm->defplace, "Previous definition was here");
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
453 macro_destroy(m);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
454 return;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
455 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
456 macrotable_add(m);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
457 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
458
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
459 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
460 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
461 macro_parse_parameters(struct macro *m, struct place *p, const char *params)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
462 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
463 size_t len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
464 const char *s;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
465 char *param;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
466
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
467 while (params != NULL) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
468 len = strspn(params, ws);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
469 params += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
470 p->column += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
471 s = strchr(params, ',');
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
472 if (s) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
473 len = s-params;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
474 param = dostrndup(params, len);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
475 s++;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
476 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
477 len = strlen(params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
478 param = dostrndup(params, len);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
479 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
480 notrailingws(param, strlen(param));
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
481 if (!is_identifier(param)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
482 complain(p, "Invalid macro parameter name %s", param);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
483 complain_fail();
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
484 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
485 stringarray_add(&m->params, param, NULL);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
486 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
487 params = s;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
488 p->column += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
489 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
490 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
491
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
492 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
493 bool
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
494 isparam(struct macro *m, const char *name, size_t len, unsigned *num_ret)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
495 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
496 unsigned num, i;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
497 const char *param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
498
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
499 num = stringarray_num(&m->params);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
500 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
501 param = stringarray_get(&m->params, i);
27
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 25
diff changeset
502 if (strlen(param) == len && !memcmp(name, param, len)) {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
503 *num_ret = i;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
504 return true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
505 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
506 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
507 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
508 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
509
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
510 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
511 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
512 macro_parse_expansion(struct macro *m, const char *buf)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
513 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
514 size_t blockstart, wordstart, pos;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
515 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
516 unsigned param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
517
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
518 pos = blockstart = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
519 while (buf[pos] != '\0') {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
520 pos += strspn(buf+pos, ws);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
521 if (strchr(alnum, buf[pos])) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
522 wordstart = pos;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
523 pos += strspn(buf+pos, alnum);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
524 if (isparam(m, buf+wordstart, pos-wordstart, &param)) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
525 if (pos > blockstart) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
526 ei = expansionitem_create_stringlen(
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
527 buf + blockstart,
27
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 25
diff changeset
528 wordstart - blockstart);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
529 expansionitemarray_add(&m->expansion,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
530 ei, NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
531 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
532 ei = expansionitem_create_param(param);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
533 expansionitemarray_add(&m->expansion, ei,NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
534 blockstart = pos;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
535 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
536 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
537 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
538 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
539 pos++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
540 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
541 if (pos > blockstart) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
542 ei = expansionitem_create_stringlen(buf + blockstart,
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
543 pos - blockstart);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
544 expansionitemarray_add(&m->expansion, ei, NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
545 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
546 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
547
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
548 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
549 macro_define_plain(struct place *p1, const char *macro,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
550 struct place *p2, const char *expansion)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
551 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
552 struct macro *m;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
553 struct expansionitem *ei;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
554
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
555 m = macro_define_common_start(p1, macro, p2);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
556 ei = expansionitem_create_string(expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
557 expansionitemarray_add(&m->expansion, ei, NULL);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
558 macro_define_common_end(m);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
559 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
560
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
561 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
562 macro_define_params(struct place *p1, const char *macro,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
563 struct place *p2, const char *params,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
564 struct place *p3, const char *expansion)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
565 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
566 struct macro *m;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
567
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
568 m = macro_define_common_start(p1, macro, p3);
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
569 m->hasparams = true;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
570 macro_parse_parameters(m, p2, params);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
571 macro_parse_expansion(m, expansion);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
572 macro_define_common_end(m);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
573 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
574
76da41da923f added macro table
David A. Holland
parents:
diff changeset
575 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
576 macro_undef(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
577 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
578 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
579
76da41da923f added macro table
David A. Holland
parents:
diff changeset
580 m = macrotable_find(macro, true);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
581 if (m) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
582 macro_destroy(m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
583 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
584 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
585
76da41da923f added macro table
David A. Holland
parents:
diff changeset
586 bool
76da41da923f added macro table
David A. Holland
parents:
diff changeset
587 macro_isdefined(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
588 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
589 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
590
76da41da923f added macro table
David A. Holland
parents:
diff changeset
591 m = macrotable_find(macro, false);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
592 return m != NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
593 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
594
76da41da923f added macro table
David A. Holland
parents:
diff changeset
595 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
596 // macro expansion
76da41da923f added macro table
David A. Holland
parents:
diff changeset
597
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
598 struct expstate {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
599 bool honordefined;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
600 enum { ES_NORMAL, ES_WANTLPAREN, ES_NOARG, ES_HAVEARG } state;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
601 struct macro *curmacro;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
602 struct stringarray args;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
603 unsigned argparens;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
604
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
605 bool tobuf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
606 char *buf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
607 size_t bufpos, bufmax;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
608 };
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
609
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
610 static struct expstate mainstate;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
611
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
612 static void doexpand(struct expstate *es, struct place *p,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
613 char *buf, size_t len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
614
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
615 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
616 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
617 expstate_init(struct expstate *es, bool tobuf, bool honordefined)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
618 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
619 es->honordefined = honordefined;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
620 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
621 es->curmacro = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
622 stringarray_init(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
623 es->argparens = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
624 es->tobuf = tobuf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
625 es->buf = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
626 es->bufpos = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
627 es->bufmax = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
628 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
629
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
630 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
631 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
632 expstate_cleanup(struct expstate *es)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
633 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
634 assert(es->state == ES_NORMAL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
635 stringarray_cleanup(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
636 if (es->buf) {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
637 dofree(es->buf, es->bufmax);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
638 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
639 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
640
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
641 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
642 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
643 expstate_destroyargs(struct expstate *es)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
644 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
645 unsigned i, num;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
646
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
647 num = stringarray_num(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
648 for (i=0; i<num; i++) {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
649 dostrfree(stringarray_get(&es->args, i));
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
650 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
651 stringarray_setsize(&es->args, 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
652 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
653
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
654 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
655 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
656 expand_send(struct expstate *es, struct place *p, const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
657 {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
658 size_t oldmax;
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
659
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
660 if (es->tobuf) {
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 33
diff changeset
661 assert(es->bufpos <= es->bufmax);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
662 if (es->bufpos + len > es->bufmax) {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
663 oldmax = es->bufmax;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
664 if (es->bufmax == 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
665 es->bufmax = 64;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
666 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
667 while (es->bufpos + len > es->bufmax) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
668 es->bufmax *= 2;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
669 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
670 es->buf = dorealloc(es->buf, oldmax, es->bufmax);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
671 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
672 memcpy(es->buf + es->bufpos, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
673 es->bufpos += len;
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 33
diff changeset
674 assert(es->bufpos <= es->bufmax);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
675 } else {
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
676 output(p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
677 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
678 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
679
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
680 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
681 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
682 expand_send_eof(struct expstate *es, struct place *p)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
683 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
684 if (es->tobuf) {
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
685 expand_send(es, p, "", 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
686 es->bufpos--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
687 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
688 output_eof();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
689 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
690 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
691
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
692 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
693 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
694 expand_newarg(struct expstate *es, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
695 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
696 char *text;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
697
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
698 text = dostrndup(buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
699 stringarray_add(&es->args, text, NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
700 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
701
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
702 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
703 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
704 expand_appendarg(struct expstate *es, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
705 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
706 unsigned num;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
707 char *text;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
708 size_t oldlen;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
709
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
710 num = stringarray_num(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
711 assert(num > 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
712
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
713 text = stringarray_get(&es->args, num - 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
714 oldlen = strlen(text);
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
715 text = dorealloc(text, oldlen + 1, oldlen + len + 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
716 memcpy(text + oldlen, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
717 text[oldlen+len] = '\0';
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
718 stringarray_set(&es->args, num - 1, text);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
719 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
720
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
721 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
722 char *
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
723 expand_substitute(struct place *p, struct expstate *es)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
724 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
725 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
726 unsigned i, num;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
727 size_t len;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
728 char *arg;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
729 char *ret;
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
730 unsigned numargs, numparams;
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
731
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
732 numargs = stringarray_num(&es->args);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
733 numparams = stringarray_num(&es->curmacro->params);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
734
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
735 if (numargs == 0 && numparams == 1) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
736 /* no arguments <=> one empty argument */
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
737 stringarray_add(&es->args, dostrdup(""), NULL);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
738 numargs++;
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
739 }
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
740 if (numargs != numparams) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
741 complain(p, "Wrong number of arguments for macro %s; "
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
742 "found %u, expected %u",
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
743 es->curmacro->name, numargs, numparams);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
744 complain_fail();
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
745 while (numargs < numparams) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
746 stringarray_add(&es->args, dostrdup(""), NULL);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
747 numargs++;
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
748 }
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
749 }
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
750
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
751 len = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
752 num = expansionitemarray_num(&es->curmacro->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
753 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
754 ei = expansionitemarray_get(&es->curmacro->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
755 if (ei->isstring) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
756 len += strlen(ei->string);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
757 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
758 arg = stringarray_get(&es->args, ei->param);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
759 len += strlen(arg);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
760 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
761 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
762
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
763 ret = domalloc(len+1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
764 *ret = '\0';
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
765 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
766 ei = expansionitemarray_get(&es->curmacro->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
767 if (ei->isstring) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
768 strcat(ret, ei->string);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
769 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
770 arg = stringarray_get(&es->args, ei->param);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
771 strcat(ret, arg);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
772 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
773 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
774
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
775 return ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
776 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
777
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
778 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
779 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
780 expand_domacro(struct expstate *es, struct place *p)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
781 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
782 struct macro *m;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
783 char *newbuf, *newbuf2;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
784
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
785 if (es->curmacro == NULL) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
786 /* defined() */
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
787 if (stringarray_num(&es->args) != 1) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
788 complain(p, "Too many arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
789 complain_fail();
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
790 expand_send(es, p, "0", 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
791 return;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
792 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
793 m = macrotable_find(stringarray_get(&es->args, 0), false);
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
794 expand_send(es, p, (m != NULL) ? "1" : "0", 1);
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
795 expstate_destroyargs(es);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
796 return;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
797 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
798
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
799 assert(es->curmacro->inuse == false);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
800 es->curmacro->inuse = true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
801
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
802 newbuf = expand_substitute(p, es);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
803 newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false);
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
804 dostrfree(newbuf);
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
805 expstate_destroyargs(es);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
806 doexpand(es, p, newbuf2, strlen(newbuf2));
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
807 dostrfree(newbuf2);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
808
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
809 es->curmacro->inuse = false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
810 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
811
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
812 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
813 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
814 expand_got_ws(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
815 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
816 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
817 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
818 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
819 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
820 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
821 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
822 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
823 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
824 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
825 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
826 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
827 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
828 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
829
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
830 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
831 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
832 expand_got_word(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
833 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
834 struct macro *m;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
835 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
836 char *newbuf;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
837
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
838 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
839 case ES_NORMAL:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
840 if (es->honordefined &&
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
841 len == 7 && !memcmp(buf, "defined", 7)) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
842 es->curmacro = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
843 es->state = ES_WANTLPAREN;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
844 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
845 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
846 m = macrotable_findlen(buf, len, false);
42
ad7763329eba Don't crash if a macro tries to expand itself recursively.
David A. Holland
parents: 40
diff changeset
847 if (m == NULL || m->inuse) {
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
848 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
849 } else if (!m->hasparams) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
850 m->inuse = true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
851 assert(expansionitemarray_num(&m->expansion) == 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
852 ei = expansionitemarray_get(&m->expansion, 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
853 assert(ei->isstring);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
854 newbuf = macroexpand(p, ei->string,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
855 strlen(ei->string), false);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
856 doexpand(es, p, newbuf, strlen(newbuf));
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
857 dostrfree(newbuf);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
858 m->inuse = false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
859 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
860 es->curmacro = m;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
861 es->state = ES_WANTLPAREN;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
862 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
863 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
864 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
865 if (es->curmacro != NULL) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
866 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
867 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
868 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
869 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
870 /* "defined foo" means "defined(foo)" */
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
871 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
872 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
873 expand_domacro(es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
874 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
875 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
876 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
877 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
878 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
879 es->state = ES_HAVEARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
880 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
881 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
882 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
883 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
884 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
885 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
886
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
887 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
888 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
889 expand_got_lparen(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
890 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
891 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
892 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
893 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
894 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
895 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
896 es->state = ES_NOARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
897 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
898 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
899 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
900 es->state = ES_HAVEARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
901 es->argparens++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
902 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
903 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
904 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
905 es->argparens++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
906 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
907 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
908 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
909
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
910 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
911 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
912 expand_got_rparen(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
913 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
914 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
915 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
916 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
917 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
918 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
919 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
920 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
921 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
922 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
923 complain(p, "Expected arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
924 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
925 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
926 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
927 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
928 assert(es->argparens == 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
929 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
930 expand_domacro(es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
931 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
932 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
933 if (es->argparens > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
934 es->argparens--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
935 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
936 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
937 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
938 expand_domacro(es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
939 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
940 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
941 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
942 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
943
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
944 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
945 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
946 expand_got_comma(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
947 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
948 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
949 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
950 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
951 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
952 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
953 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
954 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
955 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
956 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
957 complain(p, "Expected arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
958 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
959 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
960 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
961 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
962 assert(es->argparens == 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
963 expand_newarg(es, buf, 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
964 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
965 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
966 if (es->argparens > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
967 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
968 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
969 es->state = ES_NOARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
970 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
971 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
972 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
973 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
974
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
975 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
976 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
977 expand_got_other(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
978 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
979 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
980 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
981 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
982 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
983 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
984 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
985 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
986 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
987 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
988 complain(p, "Expected arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
989 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
990 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
991 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
992 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
993 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
994 es->state = ES_HAVEARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
995 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
996 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
997 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
998 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
999 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1000 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1001
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1002 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1003 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1004 expand_got_eof(struct expstate *es, struct place *p)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1005 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1006 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1007 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
1008 expand_send_eof(es, p);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1009 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1010 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1011 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1012 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1013 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1014 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1015 complain(p, "Expected arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1016 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1017 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1018 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1019 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1020 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1021 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1022 complain(p, "Unclosed argument list for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1023 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1024 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1025 complain(p, "Unclosed argument list for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1026 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1027 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1028 expstate_destroyargs(es);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1029 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1030 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1031 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1032 es->curmacro = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1033 es->argparens = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1034 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1035
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1036 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1037 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1038 doexpand(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1039 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1040 size_t x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1041
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1042 while (len > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1043 x = strspn(buf, ws);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1044 if (x > len) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1045 /* XXX gross, need strnspn */
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1046 x = len;
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1047 }
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1048
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1049 if (x > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1050 expand_got_ws(es, p, buf, x);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1051 buf += x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1052 len -= x;
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1053 continue;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1054 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1055
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1056 x = strspn(buf, alnum);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1057 if (x > len) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1058 /* XXX gross, need strnspn */
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1059 x = len;
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1060 }
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1061
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1062 if (x > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1063 expand_got_word(es, p, buf, x);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1064 buf += x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1065 len -= x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1066 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1067 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1068
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1069 if (buf[0] == '(') {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1070 expand_got_lparen(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1071 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1072 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1073 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1074 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1075
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1076 if (buf[0] == ')') {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1077 expand_got_rparen(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1078 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1079 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1080 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1081 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1082
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1083 if (buf[0] == ',') {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1084 expand_got_comma(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1085 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1086 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1087 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1088 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1089
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1090 expand_got_other(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1091 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1092 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1093 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1094 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1095
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1096 char *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1097 macroexpand(struct place *p, char *buf, size_t len, bool honordefined)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1098 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1099 struct expstate es;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1100 char *ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1101
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1102 expstate_init(&es, true, honordefined);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1103 doexpand(&es, p, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1104 expand_got_eof(&es, p);
40
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1105
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1106 /* trim to fit, so the malloc debugging won't complain */
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1107 es.buf = dorealloc(es.buf, es.bufmax, strlen(es.buf) + 1);
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1108
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1109 ret = es.buf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1110 es.buf = NULL;
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
1111 es.bufpos = es.bufmax = 0;
40
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1112
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1113 expstate_cleanup(&es);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1114
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1115 return ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1116 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1117
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1118 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1119 macro_sendline(struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1120 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1121 doexpand(&mainstate, p, buf, len);
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
1122 output(p, "\n", 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1123 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1124
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1125 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1126 macro_sendeof(struct place *p)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1127 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1128 expand_got_eof(&mainstate, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1129 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1130
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1131 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1132 // module initialization
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1133
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1134 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1135 macros_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1136 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1137 macrotable_init();
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1138 expstate_init(&mainstate, false, false);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1139 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1140
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1141 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1142 macros_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1143 {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1144 expstate_cleanup(&mainstate);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1145 macrotable_cleanup();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1146 }