annotate utils.c @ 164:f14f5352956c

Merge upstream into Joerg's changes.
author David A. Holland
date Fri, 12 Jun 2015 01:00:38 -0400
parents d6e6b3940780
children 4ea0ce804d22
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
bfa97d43197e support code
David A. Holland
parents:
diff changeset
34 #include "utils.h"
bfa97d43197e support code
David A. Holland
parents:
diff changeset
35
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
36 #define MALLOCDEBUG
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
37
16
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
38 const char ws[] =
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
39 " \t\f\v"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
40 ;
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
41 const char alnum[] =
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
42 "0123456789"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
43 "abcdefghijklmnopqrstuvwxyz"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
45 "_"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
46 ;
160
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 143
diff changeset
47 const char digits[] =
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 143
diff changeset
48 "0123456789"
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 143
diff changeset
49 ;
16
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
50
41
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 // malloc
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 #define ROUNDUP(len, size) ((size) * (((len) + (size) - 1) / (size)))
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 #ifdef MALLOCDEBUG
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
57
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
58 struct mallocheader {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
59 struct mallocheader *self;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
60 size_t len;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
61 };
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
62
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
63 static
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
64 size_t
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
65 adjustsize(size_t len)
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
66 {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
67 const size_t sz = sizeof(struct mallocheader);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
68 return ROUNDUP(len, sz) + 2*sz;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
69 }
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
70
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
71 static
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
72 void *
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
73 placeheaders(void *block, size_t len)
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
74 {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
75 struct mallocheader *bothdr, *tophdr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
76 size_t roundedlen;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
77 void *ret;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
78
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
79 roundedlen = ROUNDUP(len, sizeof(struct mallocheader));
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
80 bothdr = block;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
81 bothdr->len = len;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
82 bothdr->self = block;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
83 ret = bothdr + 1;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
84 tophdr = (void *)(((unsigned char *)ret) + roundedlen);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
85 tophdr->len = len;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
86 tophdr->self = bothdr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
87 return ret;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
88 }
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
89
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
90 static
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
91 void *
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
92 checkheaders(void *block, size_t len)
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
93 {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
94 struct mallocheader *bothdr, *tophdr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
95 size_t roundedlen;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
96
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
97 if (block == NULL) {
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
98 assert(len == 0);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
99 return block;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
100 }
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
101
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
102 roundedlen = ROUNDUP(len, sizeof(struct mallocheader));
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
103 bothdr = block;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
104 bothdr--;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
105 assert(bothdr->self == bothdr);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
106 assert(bothdr->len == len);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
107 tophdr = (void *)(((unsigned char *)(bothdr + 1)) + roundedlen);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
108 assert(tophdr->self == bothdr);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
109 assert(tophdr->len == len);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
110 return bothdr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
111 }
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 #else
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
114
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
115 #define adjustsize(len) (len)
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
116 #define placeheaders(block, len) ((void)(len), (block))
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
117 #define checkheaders(ptr, len) ((void)(len), (ptr))
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
118
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
119 #endif /* MALLOCDEBUG */
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
120
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
121 void *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
122 domalloc(size_t len)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
123 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
124 void *ret;
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
125 size_t blocklen;
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
126
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
127 blocklen = adjustsize(len);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
128 ret = malloc(blocklen);
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
129 if (ret == NULL) {
143
ed45f2d8d3bc Don't use the <err.h> functions.
David A. Holland
parents: 99
diff changeset
130 complain(NULL, "Out of memory");
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
131 die();
bfa97d43197e support code
David A. Holland
parents:
diff changeset
132 }
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
133
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
134 return placeheaders(ret, len);
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
135 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
136
bfa97d43197e support code
David A. Holland
parents:
diff changeset
137 void *
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
138 dorealloc(void *ptr, size_t oldlen, size_t newlen)
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
139 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
140 void *ret;
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
141 void *blockptr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
142 size_t newblocklen;
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
143
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
144 blockptr = checkheaders(ptr, oldlen);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
145 newblocklen = adjustsize(newlen);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
146
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
147 ret = realloc(blockptr, newblocklen);
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
148 if (ret == NULL) {
143
ed45f2d8d3bc Don't use the <err.h> functions.
David A. Holland
parents: 99
diff changeset
149 complain(NULL, "Out of memory");
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
150 die();
bfa97d43197e support code
David A. Holland
parents:
diff changeset
151 }
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
152
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
153 return placeheaders(ret, newlen);
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
154 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
155
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
156 void
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
157 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
158 {
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
159 void *blockptr;
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
160
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
161 blockptr = checkheaders(ptr, len);
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
162 free(blockptr);
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
163 }
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
164
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
165 ////////////////////////////////////////////////////////////
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
166 // string allocators
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
167
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
168 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
169 dostrdup(const char *s)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
170 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
171 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
172 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
173
bfa97d43197e support code
David A. Holland
parents:
diff changeset
174 len = strlen(s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
175 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
176 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
177 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
178 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
179
bfa97d43197e support code
David A. Holland
parents:
diff changeset
180 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
181 dostrdup2(const char *s, const char *t)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
182 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
183 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
184 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
185
bfa97d43197e support code
David A. Holland
parents:
diff changeset
186 len = strlen(s) + strlen(t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
187 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
188 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
189 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
190 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
191 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
192
bfa97d43197e support code
David A. Holland
parents:
diff changeset
193 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
194 dostrdup3(const char *s, const char *t, const char *u)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
195 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
196 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
197 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
198
bfa97d43197e support code
David A. Holland
parents:
diff changeset
199 len = strlen(s) + strlen(t) + strlen(u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
200 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
201 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
202 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
203 strcat(ret, u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
204 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
205 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
206
20
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
207 char *
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
208 dostrndup(const char *s, size_t len)
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
209 {
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
210 char *ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
211
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
212 ret = domalloc(len+1);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
213 memcpy(ret, s, len);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
214 ret[len] = '\0';
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
215 return ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
216 }
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
217
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
218 void
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
219 dostrfree(char *s)
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 dofree(s, strlen(s)+1);
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
222 }
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
223
41
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
224 ////////////////////////////////////////////////////////////
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
225 // other stuff
ef8bedab8a4b Add malloc debug code.
David A. Holland
parents: 39
diff changeset
226
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
227 size_t
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
228 notrailingws(char *buf, size_t len)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
229 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
230 while (len > 0 && strchr(ws, buf[len-1])) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
231 buf[--len] = '\0';
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 return len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
234 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
235
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
236 bool
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
237 is_identifier(const char *str)
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 size_t len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
240
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
241 len = strlen(str);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
242 if (len != strspn(str, alnum)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
243 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
244 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
245 if (str[0] >= '0' && str[0] <= '9') {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
246 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
247 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
248 return true;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
249 }