annotate utils.c @ 135:eaae8014a94a

Don't assert and leak memory if failing during argument collection. Once you have a -D option or a -include foo option, you need to clean up the arrays those are accumulated in if you die before they're collected and handled.
author David A. Holland
date Tue, 09 Jul 2013 13:35:40 -0400
parents 60184aa42604
children ed45f2d8d3bc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
1 /*-
99
60184aa42604 add 2013 to copyrights where it seems warranted
David A. Holland
parents: 41
diff changeset
2 * Copyright (c) 2010, 2013 The NetBSD Foundation, Inc.
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
3 * All rights reserved.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
4 *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
bfa97d43197e support code
David A. Holland
parents:
diff changeset
6 * by David A. Holland.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
7 *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
bfa97d43197e support code
David A. Holland
parents:
diff changeset
9 * modification, are permitted provided that the following conditions
bfa97d43197e support code
David A. Holland
parents:
diff changeset
10 * are met:
bfa97d43197e support code
David A. Holland
parents:
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
bfa97d43197e support code
David A. Holland
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
bfa97d43197e support code
David A. Holland
parents:
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
bfa97d43197e support code
David A. Holland
parents:
diff changeset
15 * documentation and/or other materials provided with the distribution.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
16 *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
bfa97d43197e support code
David A. Holland
parents:
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
bfa97d43197e support code
David A. Holland
parents:
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
bfa97d43197e support code
David A. Holland
parents:
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
bfa97d43197e support code
David A. Holland
parents:
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
bfa97d43197e support code
David A. Holland
parents:
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
bfa97d43197e support code
David A. Holland
parents:
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
bfa97d43197e support code
David A. Holland
parents:
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
bfa97d43197e support code
David A. Holland
parents:
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
bfa97d43197e support code
David A. Holland
parents:
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
bfa97d43197e support code
David A. Holland
parents:
diff changeset
28 */
bfa97d43197e support code
David A. Holland
parents:
diff changeset
29
bfa97d43197e support code
David A. Holland
parents:
diff changeset
30 #include <stdlib.h>
bfa97d43197e support code
David A. Holland
parents:
diff changeset
31 #include <string.h>
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
32 #include <assert.h>
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
33 #include <err.h>
bfa97d43197e support code
David A. Holland
parents:
diff changeset
34
bfa97d43197e support code
David A. Holland
parents:
diff changeset
35 #include "utils.h"
bfa97d43197e support code
David A. Holland
parents:
diff changeset
36
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
37 #define MALLOCDEBUG
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
38
16
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
39 const char ws[] =
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
40 " \t\f\v"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
41 ;
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
42 const char alnum[] =
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
43 "0123456789"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
44 "abcdefghijklmnopqrstuvwxyz"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
46 "_"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
47 ;
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
48
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
49 ////////////////////////////////////////////////////////////
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
50 // malloc
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
51
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
52 #define ROUNDUP(len, size) ((size) * (((len) + (size) - 1) / (size)))
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
53
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
54 #ifdef MALLOCDEBUG
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
55
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
56 struct mallocheader {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
57 struct mallocheader *self;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
58 size_t len;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
59 };
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
60
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
61 static
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
62 size_t
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
63 adjustsize(size_t len)
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
64 {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
65 const size_t sz = sizeof(struct mallocheader);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
66 return ROUNDUP(len, sz) + 2*sz;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
67 }
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
68
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
69 static
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
70 void *
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
71 placeheaders(void *block, size_t len)
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
72 {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
73 struct mallocheader *bothdr, *tophdr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
74 size_t roundedlen;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
75 void *ret;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
76
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
77 roundedlen = ROUNDUP(len, sizeof(struct mallocheader));
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
78 bothdr = block;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
79 bothdr->len = len;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
80 bothdr->self = block;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
81 ret = bothdr + 1;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
82 tophdr = (void *)(((unsigned char *)ret) + roundedlen);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
83 tophdr->len = len;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
84 tophdr->self = bothdr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
85 return ret;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
86 }
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
87
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
88 static
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
89 void *
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
90 checkheaders(void *block, size_t len)
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
91 {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
92 struct mallocheader *bothdr, *tophdr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
93 size_t roundedlen;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
94
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
95 if (block == NULL) {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
96 assert(len == 0);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
97 return block;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
98 }
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
99
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
100 roundedlen = ROUNDUP(len, sizeof(struct mallocheader));
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
101 bothdr = block;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
102 bothdr--;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
103 assert(bothdr->self == bothdr);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
104 assert(bothdr->len == len);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
105 tophdr = (void *)(((unsigned char *)(bothdr + 1)) + roundedlen);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
106 assert(tophdr->self == bothdr);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
107 assert(tophdr->len == len);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
108 return bothdr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
109 }
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
110
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
111 #else
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
112
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
113 #define adjustsize(len) (len)
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
114 #define placeheaders(block, len) ((void)(len), (block))
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
115 #define checkheaders(ptr, len) ((void)(len), (ptr))
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
116
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
117 #endif /* MALLOCDEBUG */
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
118
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
119 void *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
120 domalloc(size_t len)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
121 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
122 void *ret;
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
123 size_t blocklen;
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
124
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
125 blocklen = adjustsize(len);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
126 ret = malloc(blocklen);
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
127 if (ret == NULL) {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
128 warnx("Out of memory");
bfa97d43197e support code
David A. Holland
parents:
diff changeset
129 die();
bfa97d43197e support code
David A. Holland
parents:
diff changeset
130 }
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
131
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
132 return placeheaders(ret, len);
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
133 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
134
bfa97d43197e support code
David A. Holland
parents:
diff changeset
135 void *
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
136 dorealloc(void *ptr, size_t oldlen, size_t newlen)
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
137 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
138 void *ret;
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
139 void *blockptr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
140 size_t newblocklen;
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
141
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
142 blockptr = checkheaders(ptr, oldlen);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
143 newblocklen = adjustsize(newlen);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
144
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
145 ret = realloc(blockptr, newblocklen);
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
146 if (ret == NULL) {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
147 warnx("Out of memory");
bfa97d43197e support code
David A. Holland
parents:
diff changeset
148 die();
bfa97d43197e support code
David A. Holland
parents:
diff changeset
149 }
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
150
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
151 return placeheaders(ret, newlen);
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
152 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
153
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
154 void
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
155 dofree(void *ptr, size_t len)
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
156 {
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
157 void *blockptr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
158
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
159 blockptr = checkheaders(ptr, len);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
160 free(blockptr);
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
161 }
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
162
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
163 ////////////////////////////////////////////////////////////
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
164 // string allocators
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
165
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
166 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
167 dostrdup(const char *s)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
168 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
169 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
170 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
171
bfa97d43197e support code
David A. Holland
parents:
diff changeset
172 len = strlen(s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
173 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
174 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
175 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
176 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
177
bfa97d43197e support code
David A. Holland
parents:
diff changeset
178 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
179 dostrdup2(const char *s, const char *t)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
180 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
181 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
182 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
183
bfa97d43197e support code
David A. Holland
parents:
diff changeset
184 len = strlen(s) + strlen(t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
185 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
186 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
187 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
188 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
189 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
190
bfa97d43197e support code
David A. Holland
parents:
diff changeset
191 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
192 dostrdup3(const char *s, const char *t, const char *u)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
193 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
194 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
195 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
196
bfa97d43197e support code
David A. Holland
parents:
diff changeset
197 len = strlen(s) + strlen(t) + strlen(u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
198 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
199 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
200 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
201 strcat(ret, u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
202 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
203 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
204
20
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
205 char *
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
206 dostrndup(const char *s, size_t len)
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
207 {
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
208 char *ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
209
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
210 ret = domalloc(len+1);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
211 memcpy(ret, s, len);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
212 ret[len] = '\0';
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
213 return ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
214 }
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
215
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
216 void
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
217 dostrfree(char *s)
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
218 {
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
219 dofree(s, strlen(s)+1);
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
220 }
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
221
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
222 ////////////////////////////////////////////////////////////
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
223 // other stuff
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
224
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
225 size_t
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
226 notrailingws(char *buf, size_t len)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
227 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
228 while (len > 0 && strchr(ws, buf[len-1])) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
229 buf[--len] = '\0';
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
230 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
231 return len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
232 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
233
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
234 bool
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
235 is_identifier(const char *str)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
236 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
237 size_t len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
238
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
239 len = strlen(str);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
240 if (len != strspn(str, alnum)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
241 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
242 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
243 if (str[0] >= '0' && str[0] <= '9') {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
244 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
245 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
246 return true;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
247 }