annotate utils.c @ 204:bdc672634010

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