1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1997-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "parser.h"
#include "trace.h"
#include "util.h"
#include "errlog.h"
#define TABLE_INITIAL 50
#define TABLE_INCREMENT 50
/*
* String processing
*/
/*
* strnormalize -- combined tab-to-space and strtrim, plus removal
* of leading and trailing *$%$^@!!! semicolons.
* Not internationalized: TBD.
*/
char *
strnormalize(char *str)
{
char *p;
if (str == NULL || *str == '\0')
return (str);
for (p = str; *p != '\0'; p++) {
if (isspace(*p)) {
*p = ' ';
}
}
p--;
while (p >= str && (isspace(*p) || *p == ';'))
*p-- = '\0';
/* ERA - remove leading spaces */
while (isspace(*str))
str++;
return (str);
}
char *
strtrim(char *str)
{
char *p;
for (p = str; *p != '\0'; p++)
continue;
p--;
while (p >= str && isspace(*p))
*p-- = '\0';
return (str);
}
/*
* strlower -- make a string lower-case, destructively.
* Not internationalized: TBD.
*/
char *
strlower(char *str)
{
char *p;
for (p = str; *p != '\0'; p++) {
*p = tolower(*p);
}
return (str);
}
/*
* strset -- update a dynamically-allocated string or die trying.
*/
char *
strset(char *string, char *value)
{
size_t vlen;
assert(value != NULL, "passed a NULL value to strset");
vlen = strlen(value);
if (string == NULL) {
/* It was never allocated, so allocate it. */
if ((string = malloc(vlen+1)) == NULL) {
errlog(FATAL, "malloc ran out of space");
}
} else if (strlen(string) < vlen) {
/* Reallocate bigger. */
if ((string = realloc(string, vlen+1)) == NULL) {
errlog(FATAL, "realloc ran out of space", "", 0);
}
}
(void) strcpy(string, value);
return (string);
}
/*
* in_string_set --see if string matches any member of a space-separated
* set of strings.
*/
int
in_string_set(char *p, char *set)
{
char *q;
char save;
errlog(BEGIN, "in_string_set( p = \"%s\", set = \"%s\") {", p, set);
for (;;) {
set = skipb(set);
q = nextsep(set);
if (q == set) {
/* We've hit the end */
break;
}
save = *q;
*q = '\0';
if (strcmp(p, set) == 0) {
*q = save;
errlog(VERBOSE, "return YES");
errlog(END, "}");
return (YES);
}
*q = save;
set = q;
}
errlog(VERBOSE, "return NO");
errlog(END, "}");
return (NO);
}
char *
strend(char *p)
{
while (*p)
p++;
return (p);
}
char *
lastspace(char *p)
{
char *q;
q = strend(p);
q--;
while (q >= p && isspace(*q))
q--;
return (++q);
}
/*
* skipb -- skip over blanks (whitespace, actually), stopping
* on first non-blank.
*/
char *
skipb(char *p)
{
while (*p && isspace(*p))
p++;
return (p);
}
/*
* nextb -- skip over non-blanks (including operators!)
* stopping on first blank.
*/
char *
nextb(char *p)
{
while (*p && !isspace(*p))
p++;
return (p);
}
/*
* skipsep -- skip over separators (all but alnum and _),
* stopping on first non-separator.
*/
char *
skipsep(char *p)
{
errlog(BEGIN, "skipsep() {");
errlog(VERBOSE, "p (in) = %s", p);
while (*p && !(isalnum(*p) || *p == '_' || *p == '$'))
p++;
errlog(VERBOSE, "p (out) = %s", p);
errlog(END, "}");
return (p);
}
/*
* nextsep -- skip over non-separators (alnum and _, actually),
* stopping on first separator.
*/
char *
nextsep(char *p)
{
errlog(BEGIN, "nextsep() {");
errlog(VERBOSE, "p (in) = %s", p);
while (*p && isalnum(*p) || *p == '_' || *p == '$')
p++;
errlog(VERBOSE, "p (out) = %s", p);
errlog(END, "}");
return (p);
}
/*
* nextsep2 -- same as nextsep but also skips '.'
*/
char *
nextsep2(char *p)
{
errlog(BEGIN, "nextsep() {");
errlog(VERBOSE, "p (in) = %s", p);
while (*p && isalnum(*p) || *p == '_' || *p == '$' || *p == '.')
p++;
errlog(VERBOSE, "p (out) = %s", p);
errlog(END, "}");
return (p);
}
/*
* objectname -- basename was taken (in man3c), so...
*/
char *
objectname(char *name)
{
char *p;
static char basename[MAXLINE];
p = strrchr(name, '/');
while (p != NULL && *(p+1) == '\0') {
/* The / was at the end of the name. */
*p = '\0';
p = strrchr(name, '/');
}
(void) strlcpy(basename, p? p+1: name, MAXLINE);
if ((p = strstr(basename, ".c")) != NULL) {
*p = '\0';
}
return (strcat(basename, ".o"));
}
/*
* String tables
*/
table_t *
create_string_table(int size)
{
table_t *t;
errlog(BEGIN, "create_string_table() {");
if ((t = (table_t *)calloc((size_t)1,
(size_t)(sizeof (table_t) + (sizeof (char *)*size)))) == NULL) {
errlog(FATAL, "out of memory creating a string table");
}
t->nelem = size;
t->used = -1;
errlog(END, "}");
return (t);
}
table_t *
add_string_table(table_t *t, char *value)
{
table_t *t2;
int i;
if (t == NULL) {
errlog(FATAL, "programmer error: tried to add to "
"a NULL table");
}
if (in_string_table(t, value)) {
return (t);
}
t->used++;
if (t->used >= t->nelem) {
if ((t2 = realloc(t, (size_t)(sizeof (table_t)+(sizeof
(char *)*(t->nelem+TABLE_INCREMENT)))))
== NULL) {
errlog(FATAL, "out of memory extending string table");
}
t = t2;
t->nelem += TABLE_INCREMENT;
for (i = t->used; i < t->nelem; i++) {
t->elements[i] = NULL;
}
}
t->elements[t->used] = strset(t->elements[t->used], value);
return (t);
}
/*
* free_string_table -- really only mark it empty for reuse.
*/
table_t *
free_string_table(table_t *t)
{
errlog(BEGIN, "free_string_table() {");
if (t != NULL) {
t->used = -1;
}
errlog(END, "}");
return (t);
}
char *
get_string_table(table_t *t, int index)
{
if (t == NULL) {
return (NULL);
} else if (index > t->used) {
return (NULL);
} else {
return (t->elements[index]);
}
}
int
in_string_table(table_t *t, char *value)
{
int i;
size_t len = strlen(value);
if (t == NULL) {
return (0);
}
for (i = 0; i <= t->used; i++) {
if (strncmp(value, t->elements[i], len) == 0 &&
(t->elements[i][len] == '\0' ||
t->elements[i][len] == ','))
return (1);
}
return (0);
}
static int
compare(const void *p, const void *q)
{
return (strcmp((char *)(*(char **)p), (char *)(*(char **)q)));
}
void
sort_string_table(table_t *t)
{
if (t) {
qsort((char *)t->elements, (size_t)t->used,
sizeof (char *), compare);
}
}
|