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
384
385
386
387
388
389
390
391
392
393
394
|
/*
* 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 (c) 1997-1999 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#include "parser.h"
#include "errlog.h"
static int find_fun(char *key, char *value, char *parentfun);
/*
* handles the extends clause of the 'function' keyword
* Returns the number of errors encountered
* This function is recursive.
*/
int
do_extends(const Meta_info parentM, const Translator_info *T_info, char *value)
{
static int extends_count = 0;
char funname[BUFSIZ], filename[MAXPATHLEN], parentfun[BUFSIZ],
buf[BUFSIZ], key[20];
char *ifilename, *f, *p;
char *localvalue = NULL, *buf2 = NULL;
FILE *efp;
Meta_info M;
int found = 0, errors = 0, ki = 0;
int retval;
int scan;
++extends_count;
if (extends_count > MAX_EXTENDS) {
errlog(ERROR, "\"%s\", line %d: Error: Too many levels of "
"extends\n", parentM.mi_filename, parentM.mi_line_number);
++errors;
goto ret;
}
scan = sscanf(value, "%s %s %s %s", funname, buf, filename, parentfun);
switch (scan) {
case 0: /* funname not set */
case 1: /* buf not set, though ignored */
case 2: /* filename not set */
errlog(ERROR, "\"%s\", line %d: Error: Couldn't parse "
"'data' or 'function' line\n",
parentM.mi_filename, parentM.mi_line_number);
++errors;
goto ret;
break;
case 3:
(void) strncpy(parentfun, funname, BUFSIZ);
parentfun[BUFSIZ-1] = '\0';
break;
default:
break;
}
/* All info is from parent file - extends */
M.mi_ext_cnt = extends_count;
if (T_info->ti_verbosity >= TRACING) {
errlog(TRACING, "Extending file %s\nExtending function %s\n"
"SPEC's from %s\n", filename, parentfun,
T_info->ti_dash_I);
}
f = pathfind(T_info->ti_dash_I, filename, "f");
if (f == NULL) {
errlog(ERROR, "\"%s\", line %d: Error: Unable to find spec "
"file \"%s\"\n", parentM.mi_filename,
parentM.mi_line_number, filename);
++errors;
goto ret;
}
ifilename = strdup(f);
if (ifilename == NULL) {
errlog(ERROR | FATAL, "Error: strdup() of filename failed\n");
}
efp = fopen(ifilename, "r");
if (efp == NULL) {
errlog(ERROR, "\"%s\", line %d: Error: Unable to open "
"file \"%s\"\n", parentM.mi_filename,
parentM.mi_line_number, ifilename);
free(ifilename);
++errors;
goto ret;
}
(void) strncpy(M.mi_filename, ifilename, MAXPATHLEN);
M.mi_line_number = 0;
/* search for begin function */
while (M.mi_nlines = readline(&buf2, efp)) {
M.mi_line_number += M.mi_nlines;
if (!non_empty(buf2)) { /* is line non empty */
free(buf2);
buf2 = NULL;
continue;
}
p = realloc(localvalue, sizeof (char)*(strlen(buf2)+1));
if (p == NULL) {
errlog(ERROR | FATAL, "Error (do_extends): "
"Unable to allocate memory\n");
}
localvalue = p;
split(buf2, key, localvalue);
if ((found = find_fun(key, localvalue, parentfun))) {
/* check if architecture matches */
if (found = arch_match(efp, T_info->ti_archtoken))
break;
}
free(buf2);
buf2 = NULL;
}
if (found) {
int extends_err = 0;
static int extends_warn = 0;
extends_err = check4extends(ifilename, localvalue,
T_info->ti_archtoken, efp);
switch (extends_err) {
case -1: /* Error */
errlog(ERROR, "\"%s\", line %d: Error occurred while "
"checking for extends clause\n",
M.mi_filename, M.mi_line_number);
++errors;
/*FALLTHRU*/
case 0: /* No Extends */
break;
case 1: /* Extends */
/*
* Warning on more then one level of extends
* but only warn once.
*/
if (extends_count == 1) {
extends_warn = 1;
}
if ((extends_err = do_extends(M, T_info, localvalue))
!= 0) {
if (extends_count == 1) {
errlog(ERROR, "\"%s\", line %d: "
"Error occurred while "
"processing 'extends'\n",
parentM.mi_filename,
parentM.mi_line_number);
}
errors += extends_err;
}
if (extends_warn == 1 && extends_count == 1) {
errlog(ERROR, "\"%s\", line %d: "
"Warning: \"%s\" does not extend "
"a base specification",
parentM.mi_filename,
parentM.mi_line_number,
funname);
}
break;
default: /* Programmer Error */
errlog(ERROR | FATAL,
"Error: invalid return from "
"check4extends: %d\n", extends_err);
}
free(buf2);
buf2 = NULL;
while (M.mi_nlines = readline(&buf2, efp)) {
M.mi_line_number += M.mi_nlines;
if (!non_empty(buf2)) { /* is line non empty */
free(buf2);
buf2 = NULL;
continue;
}
p = realloc(localvalue, sizeof (char)*(strlen(buf2)+1));
if (p == NULL) {
p = realloc(NULL,
sizeof (char)*(strlen(buf2)+1));
if (p == NULL) {
errlog(ERROR | FATAL,
"Error: unable to "
"allocate memory\n");
}
}
localvalue = p;
split(buf2, key, localvalue);
ki = interesting_keyword(keywordlist, key);
switch (ki) {
case XLATOR_KW_END:
goto end;
break;
case XLATOR_KW_FUNC:
case XLATOR_KW_DATA:
errlog(ERROR, "\"%s\", line %d: "
"Error: Interface is missing \"end\"\n"
"\"%s\", line %d: Error while processing "
"%s\n", M.mi_filename, M.mi_line_number,
parentM.mi_filename,
parentM.mi_line_number, ifilename);
++errors;
goto end;
break;
case XLATOR_KW_NOTFOUND:
if (T_info->ti_verbosity >= TRACING)
errlog(STATUS,
"uninteresting keyword: %s\n", key);
break;
default:
retval = xlator_take_kvpair(M, ki, localvalue);
if (retval) {
if (T_info->ti_verbosity >= STATUS)
errlog(STATUS,
"Error in "
"xlator_take_kvpair\n");
++errors;
}
}
free(buf2);
buf2 = NULL;
}
} else {
errlog(ERROR, "\"%s\", line %d: Error: Unable to find "
"function %s in %s\n", parentM.mi_filename,
parentM.mi_line_number, parentfun, ifilename);
++errors;
}
end:
(void) fclose(efp);
free(localvalue);
free(ifilename);
free(buf2);
ret:
extends_count--;
return (errors);
}
/*
* find_fun()
* given a key value pair, and the name of the function you are
* searching for in the SPEC source file, this function returns 1
* if the beginning of the function in the SPEC source file is found.
* returns 0 otherwise.
*/
static int
find_fun(char *key, char *value, char *parentfun)
{
char pfun[BUFSIZ];
if (strcasecmp(key, "function") != 0 &&
strcasecmp(key, "data") != 0) {
return (0);
}
(void) sscanf(value, "%1023s", pfun);
if (strcmp(pfun, parentfun) == 0) {
return (1);
}
return (0);
}
/*
* arch_match(FILE *fp, int arch)
* This function takes a FILE pointer, and an architecture token
* The FILE pointer is assumed to point at the beginning of a Function
* or Data specification (specifically at the first line of spec AFTER
* the Function or Data line)
* It reads all the way to the "End" line.
* If it finds an "arch" keyword along the way, it is checked to see if
* it matches the architecture currently being generated and returns
* 1 if a match is found. If a match is not found, it returns a
* 0. If no "arch" keyword is found, it returns 1.
*
* XXX - the algorithm in arch_match is very inefficient. it read through
* the file to find "arch" and rewinds before returning.
* Later all the data that was skipped while searching for "arch" may
* be needed and it is re-read from the disk. It would be nice to just
* read the data once.
*/
int
arch_match(FILE *fp, int arch)
{
off_t offset;
char key[20], buf[BUFSIZ], *buf2 = NULL, *localvalue = NULL, *p;
int len;
int has_arch = 0;
int archset = 0;
offset = ftello(fp);
if (offset == -1) {
errlog(ERROR|FATAL, "Unable to determine file position\n");
}
while (fgets(buf, BUFSIZ, fp)) {
/* replace comments with single whitespace */
remcomment(buf);
/* get complete line */
buf2 = line_to_buf(buf2, buf); /* append buf to buf2 */
len = strlen(buf);
if (len > 1) {
while (buf[len-2] == '\\') {
if (!fgets(buf, BUFSIZ, fp)) {
buf2 = line_to_buf(buf2, buf);
break;
}
len = strlen(buf);
buf2 = line_to_buf(buf2, buf);
}
} /* end of 'get complete line' */
if (!non_empty(buf2)) { /* is line non empty */
free(buf2);
buf2 = NULL;
continue;
}
p = realloc(localvalue, sizeof (char)*(strlen(buf2)+1));
if (p == NULL) {
p = realloc(NULL,
sizeof (char)*(strlen(buf2)+1));
if (p == NULL) {
errlog(ERROR | FATAL,
"Error: unable to "
"allocate memory\n");
}
}
localvalue = p;
split(buf2, key, localvalue);
if (strcasecmp(key, "arch") == 0) {
char *alist = localvalue, *a;
has_arch = 1;
while ((a = strtok(alist, " ,\n")) != NULL) {
archset = arch_strtoi(a);
if (arch & archset) {
free(buf2);
free(p);
if (fseeko(fp, offset, SEEK_SET) < 0) {
errlog(ERROR|FATAL,
"%s", strerror(errno));
}
return (1);
}
alist = NULL;
}
} else if (strcasecmp(key, "end") == 0) {
break;
}
free(buf2);
buf2 = NULL;
}
end:
free(buf2);
free(p);
if (fseeko(fp, offset, SEEK_SET) < 0) {
errlog(ERROR|FATAL, "%s", strerror(errno));
}
if (has_arch == 0)
return (1);
return (0);
}
|