annotate utils.c @ 26:ac45dcc57ff9

add some simple tests
author David A. Holland
date Mon, 20 Dec 2010 05:01:07 -0500
parents 40748b097655
children b156910b59b2
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
bfa97d43197e support code
David A. Holland
parents:
diff changeset
72 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
73 dostrdup(const char *s)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
74 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
75 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
76 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
77
bfa97d43197e support code
David A. Holland
parents:
diff changeset
78 len = strlen(s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
79 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
80 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
81 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
82 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
83
bfa97d43197e support code
David A. Holland
parents:
diff changeset
84 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
85 dostrdup2(const char *s, const char *t)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
86 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
87 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
88 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
89
bfa97d43197e support code
David A. Holland
parents:
diff changeset
90 len = strlen(s) + strlen(t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
91 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
92 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
93 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
94 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
95 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
96
bfa97d43197e support code
David A. Holland
parents:
diff changeset
97 char *
bfa97d43197e support code
David A. Holland
parents:
diff changeset
98 dostrdup3(const char *s, const char *t, const char *u)
bfa97d43197e support code
David A. Holland
parents:
diff changeset
99 {
bfa97d43197e support code
David A. Holland
parents:
diff changeset
100 char *ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
101 size_t len;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
102
bfa97d43197e support code
David A. Holland
parents:
diff changeset
103 len = strlen(s) + strlen(t) + strlen(u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
104 ret = domalloc(len+1);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
105 strcpy(ret, s);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
106 strcat(ret, t);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
107 strcat(ret, u);
bfa97d43197e support code
David A. Holland
parents:
diff changeset
108 return ret;
bfa97d43197e support code
David A. Holland
parents:
diff changeset
109 }
bfa97d43197e support code
David A. Holland
parents:
diff changeset
110
20
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
111 char *
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
112 dostrndup(const char *s, size_t len)
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
113 {
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
114 char *ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
115
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
116 ret = domalloc(len+1);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
117 memcpy(ret, s, len);
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
118 ret[len] = '\0';
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
119 return ret;
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
120 }
40748b097655 add output.
David A. Holland
parents: 18
diff changeset
121
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
122 size_t
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
123 notrailingws(char *buf, size_t len)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
124 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
125 while (len > 0 && strchr(ws, buf[len-1])) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
126 buf[--len] = '\0';
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
127 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
128 return len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
129 }
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 bool
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
132 is_identifier(const char *str)
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 size_t 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 len = strlen(str);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
137 if (len != strspn(str, alnum)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
138 return false;
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 if (str[0] >= '0' && str[0] <= '9') {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
141 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
142 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
143 return true;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
144 }