annotate utils.c @ 38:b156910b59b2

Wrap free() in dofree() to allow instrumenting it for debugging.
author David A. Holland
date Sat, 30 Mar 2013 21:02:25 -0400
parents 40748b097655
children 337110e7240a
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 *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
60 dorealloc(void *ptr, size_t len)
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
bfa97d43197e support code
David A. Holland
parents:
diff changeset
64 ret = realloc(ptr, len);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
65 if (ret == NULL) {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
66 warnx("Out of memory");
bfa97d43197e support code
David A. Holland
parents:
diff changeset
67 die();
bfa97d43197e support code
David A. Holland
parents:
diff changeset
68 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
69 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
70 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
71
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
72 void
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
73 dofree(void *ptr)
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
74 {
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
75 free(ptr);
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
76 }
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 20
diff changeset
77
3
bfa97d43197e support code
David A. Holland
parents:
diff changeset
78 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
79 dostrdup(const char *s)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
80 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
81 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
82 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
83
bfa97d43197e support code
David A. Holland
parents:
diff changeset
84 len = strlen(s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
85 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
86 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
87 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
88 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
89
bfa97d43197e support code
David A. Holland
parents:
diff changeset
90 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
91 dostrdup2(const char *s, const char *t)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
92 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
93 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
94 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
95
bfa97d43197e support code
David A. Holland
parents:
diff changeset
96 len = strlen(s) + strlen(t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
97 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
98 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
99 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
100 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
101 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
102
bfa97d43197e support code
David A. Holland
parents:
diff changeset
103 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
104 dostrdup3(const char *s, const char *t, const char *u)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
105 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
106 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
107 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
108
bfa97d43197e support code
David A. Holland
parents:
diff changeset
109 len = strlen(s) + strlen(t) + strlen(u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
110 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
111 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
112 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
113 strcat(ret, u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
114 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
115 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
116
20
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
117 char *
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
118 dostrndup(const char *s, size_t len)
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
119 {
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
120 char *ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
121
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
122 ret = domalloc(len+1);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
123 memcpy(ret, s, len);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
124 ret[len] = '\0';
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
125 return ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
126 }
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
127
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
128 size_t
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
129 notrailingws(char *buf, size_t len)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
130 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
131 while (len > 0 && strchr(ws, buf[len-1])) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
132 buf[--len] = '\0';
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
133 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
134 return len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
135 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
136
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
137 bool
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
138 is_identifier(const char *str)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
139 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
140 size_t len;
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 len = strlen(str);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
143 if (len != strspn(str, alnum)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
144 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
145 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
146 if (str[0] >= '0' && str[0] <= '9') {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
147 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
148 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
149 return true;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
150 }