annotate utils.c @ 39:337110e7240a

Pass the size to free; it makes debug checking easier.
author David A. Holland
date Sat, 30 Mar 2013 21:17:47 -0400
parents b156910b59b2
children ef8bedab8a4b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
1 /*-
bfa97d43197e support code
David A. Holland
parents:
diff changeset
2 * Copyright (c) 2010 The NetBSD Foundation, Inc.
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>
bfa97d43197e support code
David A. Holland
parents:
diff changeset
32 #include <err.h>
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
16
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
36 const char ws[] =
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
37 " \t\f\v"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
38 ;
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
39 const char alnum[] =
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
40 "0123456789"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
41 "abcdefghijklmnopqrstuvwxyz"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
42 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
43 "_"
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
44 ;
9dda765ee85c expression evaluator
David A. Holland
parents: 3
diff changeset
45
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
46 void *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
47 domalloc(size_t len)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
48 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
49 void *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
50
bfa97d43197e support code
David A. Holland
parents:
diff changeset
51 ret = malloc(len);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
52 if (ret == NULL) {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
53 warnx("Out of memory");
bfa97d43197e support code
David A. Holland
parents:
diff changeset
54 die();
bfa97d43197e support code
David A. Holland
parents:
diff changeset
55 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
56 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
57 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
58
bfa97d43197e support code
David A. Holland
parents:
diff changeset
59 void *
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
60 dorealloc(void *ptr, size_t oldlen, size_t newlen)
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
61 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
62 void *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
63
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
64 (void)oldlen;
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
65 ret = realloc(ptr, newlen);
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
66 if (ret == NULL) {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
67 warnx("Out of memory");
bfa97d43197e support code
David A. Holland
parents:
diff changeset
68 die();
bfa97d43197e support code
David A. Holland
parents:
diff changeset
69 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
70 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
71 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
72
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
73 void
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
74 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
75 {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
76 (void)len;
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
77 free(ptr);
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
78 }
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
79
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
80 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
81 dostrdup(const char *s)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
82 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
83 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
84 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
85
bfa97d43197e support code
David A. Holland
parents:
diff changeset
86 len = strlen(s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
87 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
88 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
89 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
90 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
91
bfa97d43197e support code
David A. Holland
parents:
diff changeset
92 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
93 dostrdup2(const char *s, const char *t)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
94 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
95 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
96 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
97
bfa97d43197e support code
David A. Holland
parents:
diff changeset
98 len = strlen(s) + strlen(t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
99 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
100 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
101 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
102 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
103 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
104
bfa97d43197e support code
David A. Holland
parents:
diff changeset
105 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
106 dostrdup3(const char *s, const char *t, const char *u)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
107 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
108 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
109 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
110
bfa97d43197e support code
David A. Holland
parents:
diff changeset
111 len = strlen(s) + strlen(t) + strlen(u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
112 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
113 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
114 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
115 strcat(ret, u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
116 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
117 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
118
20
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
119 char *
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
120 dostrndup(const char *s, size_t len)
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
121 {
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
122 char *ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
123
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
124 ret = domalloc(len+1);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
125 memcpy(ret, s, len);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
126 ret[len] = '\0';
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
127 return ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
128 }
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
129
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
130 void
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
131 dostrfree(char *s)
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
132 {
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
133 dofree(s, strlen(s)+1);
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
134 }
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
135
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
136 size_t
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
137 notrailingws(char *buf, size_t len)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
138 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
139 while (len > 0 && strchr(ws, buf[len-1])) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
140 buf[--len] = '\0';
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
141 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
142 return len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
143 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
144
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
145 bool
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
146 is_identifier(const char *str)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
147 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
148 size_t len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
149
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
150 len = strlen(str);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
151 if (len != strspn(str, alnum)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
152 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
153 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
154 if (str[0] >= '0' && str[0] <= '9') {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
155 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
156 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
157 return true;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
158 }