1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <string.h>
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <unistd.h>
31*7c478bd9Sstevel@tonic-gate #include <libgen.h>
32*7c478bd9Sstevel@tonic-gate #include <errno.h>
33*7c478bd9Sstevel@tonic-gate #include "parser.h"
34*7c478bd9Sstevel@tonic-gate #include "errlog.h"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate static int find_fun(char *key, char *value, char *parentfun);
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * handles the extends clause of the 'function' keyword
40*7c478bd9Sstevel@tonic-gate  * Returns the number of errors encountered
41*7c478bd9Sstevel@tonic-gate  * This function is recursive.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate int
do_extends(const Meta_info parentM,const Translator_info * T_info,char * value)44*7c478bd9Sstevel@tonic-gate do_extends(const Meta_info parentM, const Translator_info *T_info, char *value)
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate 	static int extends_count = 0;
47*7c478bd9Sstevel@tonic-gate 	char funname[BUFSIZ], filename[MAXPATHLEN], parentfun[BUFSIZ],
48*7c478bd9Sstevel@tonic-gate 	    buf[BUFSIZ], key[20];
49*7c478bd9Sstevel@tonic-gate 	char *ifilename, *f, *p;
50*7c478bd9Sstevel@tonic-gate 	char *localvalue = NULL, *buf2 = NULL;
51*7c478bd9Sstevel@tonic-gate 	FILE *efp;
52*7c478bd9Sstevel@tonic-gate 	Meta_info M;
53*7c478bd9Sstevel@tonic-gate 	int found = 0, errors = 0, ki = 0;
54*7c478bd9Sstevel@tonic-gate 	int retval;
55*7c478bd9Sstevel@tonic-gate 	int scan;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	++extends_count;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	if (extends_count > MAX_EXTENDS) {
60*7c478bd9Sstevel@tonic-gate 		errlog(ERROR, "\"%s\", line %d: Error: Too many levels of "
61*7c478bd9Sstevel@tonic-gate 		    "extends\n", parentM.mi_filename, parentM.mi_line_number);
62*7c478bd9Sstevel@tonic-gate 		++errors;
63*7c478bd9Sstevel@tonic-gate 		goto ret;
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	scan = sscanf(value, "%s %s %s %s", funname, buf, filename, parentfun);
67*7c478bd9Sstevel@tonic-gate 	switch (scan) {
68*7c478bd9Sstevel@tonic-gate 	case 0: /* funname not set */
69*7c478bd9Sstevel@tonic-gate 	case 1: /* buf not set, though ignored */
70*7c478bd9Sstevel@tonic-gate 	case 2: /* filename not set */
71*7c478bd9Sstevel@tonic-gate 		errlog(ERROR, "\"%s\", line %d: Error: Couldn't parse "
72*7c478bd9Sstevel@tonic-gate 		    "'data' or 'function' line\n",
73*7c478bd9Sstevel@tonic-gate 		    parentM.mi_filename, parentM.mi_line_number);
74*7c478bd9Sstevel@tonic-gate 		++errors;
75*7c478bd9Sstevel@tonic-gate 		goto ret;
76*7c478bd9Sstevel@tonic-gate 		break;
77*7c478bd9Sstevel@tonic-gate 	case 3:
78*7c478bd9Sstevel@tonic-gate 		(void) strncpy(parentfun, funname, BUFSIZ);
79*7c478bd9Sstevel@tonic-gate 		parentfun[BUFSIZ-1] = '\0';
80*7c478bd9Sstevel@tonic-gate 		break;
81*7c478bd9Sstevel@tonic-gate 	default:
82*7c478bd9Sstevel@tonic-gate 		break;
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	/* All info is from parent file - extends */
86*7c478bd9Sstevel@tonic-gate 	M.mi_ext_cnt = extends_count;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	if (T_info->ti_verbosity >= TRACING) {
89*7c478bd9Sstevel@tonic-gate 		errlog(TRACING, "Extending file %s\nExtending function %s\n"
90*7c478bd9Sstevel@tonic-gate 		    "SPEC's from %s\n", filename, parentfun,
91*7c478bd9Sstevel@tonic-gate 		    T_info->ti_dash_I);
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	f = pathfind(T_info->ti_dash_I, filename, "f");
95*7c478bd9Sstevel@tonic-gate 	if (f == NULL) {
96*7c478bd9Sstevel@tonic-gate 		errlog(ERROR, "\"%s\", line %d: Error: Unable to find spec "
97*7c478bd9Sstevel@tonic-gate 		    "file \"%s\"\n", parentM.mi_filename,
98*7c478bd9Sstevel@tonic-gate 		    parentM.mi_line_number, filename);
99*7c478bd9Sstevel@tonic-gate 		++errors;
100*7c478bd9Sstevel@tonic-gate 		goto ret;
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 	ifilename = strdup(f);
103*7c478bd9Sstevel@tonic-gate 	if (ifilename == NULL) {
104*7c478bd9Sstevel@tonic-gate 		errlog(ERROR | FATAL, "Error: strdup() of filename failed\n");
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate 	efp = fopen(ifilename, "r");
107*7c478bd9Sstevel@tonic-gate 	if (efp == NULL) {
108*7c478bd9Sstevel@tonic-gate 		errlog(ERROR, "\"%s\", line %d: Error: Unable to open "
109*7c478bd9Sstevel@tonic-gate 		    "file \"%s\"\n", parentM.mi_filename,
110*7c478bd9Sstevel@tonic-gate 		    parentM.mi_line_number, ifilename);
111*7c478bd9Sstevel@tonic-gate 		free(ifilename);
112*7c478bd9Sstevel@tonic-gate 		++errors;
113*7c478bd9Sstevel@tonic-gate 		goto ret;
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	(void) strncpy(M.mi_filename, ifilename, MAXPATHLEN);
117*7c478bd9Sstevel@tonic-gate 	M.mi_line_number = 0;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	/* search for begin function */
120*7c478bd9Sstevel@tonic-gate 	while (M.mi_nlines = readline(&buf2, efp)) {
121*7c478bd9Sstevel@tonic-gate 		M.mi_line_number += M.mi_nlines;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 		if (!non_empty(buf2)) {	 /* is line non empty */
124*7c478bd9Sstevel@tonic-gate 			free(buf2);
125*7c478bd9Sstevel@tonic-gate 			buf2 = NULL;
126*7c478bd9Sstevel@tonic-gate 			continue;
127*7c478bd9Sstevel@tonic-gate 		}
128*7c478bd9Sstevel@tonic-gate 		p = realloc(localvalue, sizeof (char)*(strlen(buf2)+1));
129*7c478bd9Sstevel@tonic-gate 		if (p == NULL) {
130*7c478bd9Sstevel@tonic-gate 			errlog(ERROR | FATAL, "Error (do_extends): "
131*7c478bd9Sstevel@tonic-gate 			    "Unable to allocate memory\n");
132*7c478bd9Sstevel@tonic-gate 		}
133*7c478bd9Sstevel@tonic-gate 		localvalue = p;
134*7c478bd9Sstevel@tonic-gate 		split(buf2, key, localvalue);
135*7c478bd9Sstevel@tonic-gate 		if ((found = find_fun(key, localvalue, parentfun))) {
136*7c478bd9Sstevel@tonic-gate 			/* check if architecture matches */
137*7c478bd9Sstevel@tonic-gate 			if (found = arch_match(efp, T_info->ti_archtoken))
138*7c478bd9Sstevel@tonic-gate 				break;
139*7c478bd9Sstevel@tonic-gate 		}
140*7c478bd9Sstevel@tonic-gate 		free(buf2);
141*7c478bd9Sstevel@tonic-gate 		buf2 = NULL;
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	if (found) {
145*7c478bd9Sstevel@tonic-gate 		int extends_err = 0;
146*7c478bd9Sstevel@tonic-gate 		static int extends_warn = 0;
147*7c478bd9Sstevel@tonic-gate 		extends_err = check4extends(ifilename, localvalue,
148*7c478bd9Sstevel@tonic-gate 		T_info->ti_archtoken, efp);
149*7c478bd9Sstevel@tonic-gate 		switch (extends_err) {
150*7c478bd9Sstevel@tonic-gate 		case -1:	/* Error */
151*7c478bd9Sstevel@tonic-gate 			errlog(ERROR, "\"%s\", line %d: Error occurred while "
152*7c478bd9Sstevel@tonic-gate 			    "checking for extends clause\n",
153*7c478bd9Sstevel@tonic-gate 			    M.mi_filename, M.mi_line_number);
154*7c478bd9Sstevel@tonic-gate 			++errors;
155*7c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
156*7c478bd9Sstevel@tonic-gate 		case 0:		/* No Extends */
157*7c478bd9Sstevel@tonic-gate 			break;
158*7c478bd9Sstevel@tonic-gate 		case 1:		/* Extends */
159*7c478bd9Sstevel@tonic-gate 			/*
160*7c478bd9Sstevel@tonic-gate 			 * Warning on more then one level of extends
161*7c478bd9Sstevel@tonic-gate 			 * but only warn once.
162*7c478bd9Sstevel@tonic-gate 			 */
163*7c478bd9Sstevel@tonic-gate 			if (extends_count == 1) {
164*7c478bd9Sstevel@tonic-gate 				extends_warn = 1;
165*7c478bd9Sstevel@tonic-gate 			}
166*7c478bd9Sstevel@tonic-gate 			if ((extends_err = do_extends(M, T_info, localvalue))
167*7c478bd9Sstevel@tonic-gate 			    != 0) {
168*7c478bd9Sstevel@tonic-gate 				if (extends_count == 1) {
169*7c478bd9Sstevel@tonic-gate 					errlog(ERROR, "\"%s\", line %d: "
170*7c478bd9Sstevel@tonic-gate 					    "Error occurred while "
171*7c478bd9Sstevel@tonic-gate 					    "processing 'extends'\n",
172*7c478bd9Sstevel@tonic-gate 					    parentM.mi_filename,
173*7c478bd9Sstevel@tonic-gate 					    parentM.mi_line_number);
174*7c478bd9Sstevel@tonic-gate 				}
175*7c478bd9Sstevel@tonic-gate 				errors += extends_err;
176*7c478bd9Sstevel@tonic-gate 			}
177*7c478bd9Sstevel@tonic-gate 			if (extends_warn == 1 && extends_count == 1) {
178*7c478bd9Sstevel@tonic-gate 				errlog(ERROR, "\"%s\", line %d: "
179*7c478bd9Sstevel@tonic-gate 				    "Warning: \"%s\" does not extend "
180*7c478bd9Sstevel@tonic-gate 				    "a base specification",
181*7c478bd9Sstevel@tonic-gate 				    parentM.mi_filename,
182*7c478bd9Sstevel@tonic-gate 				    parentM.mi_line_number,
183*7c478bd9Sstevel@tonic-gate 				    funname);
184*7c478bd9Sstevel@tonic-gate 			}
185*7c478bd9Sstevel@tonic-gate 			break;
186*7c478bd9Sstevel@tonic-gate 		default:	/* Programmer Error */
187*7c478bd9Sstevel@tonic-gate 			errlog(ERROR | FATAL,
188*7c478bd9Sstevel@tonic-gate 			    "Error: invalid return from "
189*7c478bd9Sstevel@tonic-gate 			    "check4extends: %d\n", extends_err);
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 		free(buf2);
193*7c478bd9Sstevel@tonic-gate 		buf2 = NULL;
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 		while (M.mi_nlines = readline(&buf2, efp)) {
196*7c478bd9Sstevel@tonic-gate 			M.mi_line_number += M.mi_nlines;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 			if (!non_empty(buf2)) { /* is line non empty */
199*7c478bd9Sstevel@tonic-gate 				free(buf2);
200*7c478bd9Sstevel@tonic-gate 				buf2 = NULL;
201*7c478bd9Sstevel@tonic-gate 				continue;
202*7c478bd9Sstevel@tonic-gate 			}
203*7c478bd9Sstevel@tonic-gate 			p = realloc(localvalue, sizeof (char)*(strlen(buf2)+1));
204*7c478bd9Sstevel@tonic-gate 			if (p == NULL) {
205*7c478bd9Sstevel@tonic-gate 				p = realloc(NULL,
206*7c478bd9Sstevel@tonic-gate 				    sizeof (char)*(strlen(buf2)+1));
207*7c478bd9Sstevel@tonic-gate 				if (p == NULL) {
208*7c478bd9Sstevel@tonic-gate 					errlog(ERROR | FATAL,
209*7c478bd9Sstevel@tonic-gate 					    "Error: unable to "
210*7c478bd9Sstevel@tonic-gate 					    "allocate memory\n");
211*7c478bd9Sstevel@tonic-gate 				}
212*7c478bd9Sstevel@tonic-gate 			}
213*7c478bd9Sstevel@tonic-gate 			localvalue = p;
214*7c478bd9Sstevel@tonic-gate 			split(buf2, key, localvalue);
215*7c478bd9Sstevel@tonic-gate 			ki = interesting_keyword(keywordlist, key);
216*7c478bd9Sstevel@tonic-gate 			switch (ki) {
217*7c478bd9Sstevel@tonic-gate 			case XLATOR_KW_END:
218*7c478bd9Sstevel@tonic-gate 				goto end;
219*7c478bd9Sstevel@tonic-gate 				break;
220*7c478bd9Sstevel@tonic-gate 			case XLATOR_KW_FUNC:
221*7c478bd9Sstevel@tonic-gate 			case XLATOR_KW_DATA:
222*7c478bd9Sstevel@tonic-gate 				errlog(ERROR, "\"%s\", line %d: "
223*7c478bd9Sstevel@tonic-gate 				    "Error: Interface is missing \"end\"\n"
224*7c478bd9Sstevel@tonic-gate 				    "\"%s\", line %d: Error while processing "
225*7c478bd9Sstevel@tonic-gate 				    "%s\n", M.mi_filename, M.mi_line_number,
226*7c478bd9Sstevel@tonic-gate 				    parentM.mi_filename,
227*7c478bd9Sstevel@tonic-gate 				    parentM.mi_line_number, ifilename);
228*7c478bd9Sstevel@tonic-gate 				++errors;
229*7c478bd9Sstevel@tonic-gate 				goto end;
230*7c478bd9Sstevel@tonic-gate 				break;
231*7c478bd9Sstevel@tonic-gate 			case XLATOR_KW_NOTFOUND:
232*7c478bd9Sstevel@tonic-gate 				if (T_info->ti_verbosity >= TRACING)
233*7c478bd9Sstevel@tonic-gate 					errlog(STATUS,
234*7c478bd9Sstevel@tonic-gate 					    "uninteresting keyword: %s\n", key);
235*7c478bd9Sstevel@tonic-gate 				break;
236*7c478bd9Sstevel@tonic-gate 			default:
237*7c478bd9Sstevel@tonic-gate 				retval = xlator_take_kvpair(M, ki, localvalue);
238*7c478bd9Sstevel@tonic-gate 				if (retval) {
239*7c478bd9Sstevel@tonic-gate 					if (T_info->ti_verbosity >= STATUS)
240*7c478bd9Sstevel@tonic-gate 						errlog(STATUS,
241*7c478bd9Sstevel@tonic-gate 						    "Error in "
242*7c478bd9Sstevel@tonic-gate 						    "xlator_take_kvpair\n");
243*7c478bd9Sstevel@tonic-gate 					++errors;
244*7c478bd9Sstevel@tonic-gate 				}
245*7c478bd9Sstevel@tonic-gate 			}
246*7c478bd9Sstevel@tonic-gate 			free(buf2);
247*7c478bd9Sstevel@tonic-gate 			buf2 = NULL;
248*7c478bd9Sstevel@tonic-gate 		}
249*7c478bd9Sstevel@tonic-gate 	} else {
250*7c478bd9Sstevel@tonic-gate 		errlog(ERROR, "\"%s\", line %d: Error: Unable to find "
251*7c478bd9Sstevel@tonic-gate 		    "function %s in %s\n", parentM.mi_filename,
252*7c478bd9Sstevel@tonic-gate 		    parentM.mi_line_number, parentfun, ifilename);
253*7c478bd9Sstevel@tonic-gate 		++errors;
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate end:
256*7c478bd9Sstevel@tonic-gate 	(void) fclose(efp);
257*7c478bd9Sstevel@tonic-gate 	free(localvalue);
258*7c478bd9Sstevel@tonic-gate 	free(ifilename);
259*7c478bd9Sstevel@tonic-gate 	free(buf2);
260*7c478bd9Sstevel@tonic-gate ret:
261*7c478bd9Sstevel@tonic-gate 	extends_count--;
262*7c478bd9Sstevel@tonic-gate 	return (errors);
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate /*
266*7c478bd9Sstevel@tonic-gate  * find_fun()
267*7c478bd9Sstevel@tonic-gate  *    given a key value pair, and the name of the function you are
268*7c478bd9Sstevel@tonic-gate  *    searching for in the SPEC source file, this function returns 1
269*7c478bd9Sstevel@tonic-gate  *    if the beginning of the function in the SPEC source file is found.
270*7c478bd9Sstevel@tonic-gate  *    returns 0 otherwise.
271*7c478bd9Sstevel@tonic-gate  */
272*7c478bd9Sstevel@tonic-gate static int
find_fun(char * key,char * value,char * parentfun)273*7c478bd9Sstevel@tonic-gate find_fun(char *key, char *value, char *parentfun)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	char pfun[BUFSIZ];
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	if (strcasecmp(key, "function") != 0 &&
278*7c478bd9Sstevel@tonic-gate 		strcasecmp(key, "data") != 0) {
279*7c478bd9Sstevel@tonic-gate 		return (0);
280*7c478bd9Sstevel@tonic-gate 	}
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	(void) sscanf(value, "%1023s", pfun);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	if (strcmp(pfun, parentfun) == 0) {
285*7c478bd9Sstevel@tonic-gate 		return (1);
286*7c478bd9Sstevel@tonic-gate 	}
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	return (0);
289*7c478bd9Sstevel@tonic-gate }
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate /*
292*7c478bd9Sstevel@tonic-gate  * arch_match(FILE *fp, int arch)
293*7c478bd9Sstevel@tonic-gate  * This function takes a FILE pointer, and an architecture token
294*7c478bd9Sstevel@tonic-gate  * The FILE pointer is assumed to point at the beginning of a Function
295*7c478bd9Sstevel@tonic-gate  * or Data specification (specifically at the first line of spec AFTER
296*7c478bd9Sstevel@tonic-gate  * the Function or Data line)
297*7c478bd9Sstevel@tonic-gate  * It reads all the way to the "End" line.
298*7c478bd9Sstevel@tonic-gate  * If it finds an "arch" keyword along the way, it is checked to see if
299*7c478bd9Sstevel@tonic-gate  * it matches the architecture currently being generated and returns
300*7c478bd9Sstevel@tonic-gate  * 1 if a match is found.  If a match is not found, it returns a
301*7c478bd9Sstevel@tonic-gate  * 0. If no "arch" keyword is found, it returns 1.
302*7c478bd9Sstevel@tonic-gate  *
303*7c478bd9Sstevel@tonic-gate  * XXX - the algorithm in arch_match is very inefficient. it read through
304*7c478bd9Sstevel@tonic-gate  * the file to find "arch" and rewinds before returning.
305*7c478bd9Sstevel@tonic-gate  * Later all the data that was skipped while searching for "arch" may
306*7c478bd9Sstevel@tonic-gate  * be needed and it is re-read from the disk. It would be nice to just
307*7c478bd9Sstevel@tonic-gate  * read the data once.
308*7c478bd9Sstevel@tonic-gate  */
309*7c478bd9Sstevel@tonic-gate int
arch_match(FILE * fp,int arch)310*7c478bd9Sstevel@tonic-gate arch_match(FILE *fp, int arch)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	off_t offset;
313*7c478bd9Sstevel@tonic-gate 	char key[20], buf[BUFSIZ], *buf2 = NULL, *localvalue = NULL, *p;
314*7c478bd9Sstevel@tonic-gate 	int len;
315*7c478bd9Sstevel@tonic-gate 	int has_arch = 0;
316*7c478bd9Sstevel@tonic-gate 	int archset = 0;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	offset = ftello(fp);
319*7c478bd9Sstevel@tonic-gate 	if (offset == -1) {
320*7c478bd9Sstevel@tonic-gate 		errlog(ERROR|FATAL, "Unable to determine file position\n");
321*7c478bd9Sstevel@tonic-gate 	}
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp)) {
324*7c478bd9Sstevel@tonic-gate 		/* replace comments with single whitespace */
325*7c478bd9Sstevel@tonic-gate 		remcomment(buf);
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 		/* get complete line */
328*7c478bd9Sstevel@tonic-gate 		buf2 = line_to_buf(buf2, buf); /* append buf to buf2 */
329*7c478bd9Sstevel@tonic-gate 		len = strlen(buf);
330*7c478bd9Sstevel@tonic-gate 		if (len > 1) {
331*7c478bd9Sstevel@tonic-gate 			while (buf[len-2] == '\\') {
332*7c478bd9Sstevel@tonic-gate 				if (!fgets(buf, BUFSIZ, fp)) {
333*7c478bd9Sstevel@tonic-gate 					buf2 = line_to_buf(buf2, buf);
334*7c478bd9Sstevel@tonic-gate 					break;
335*7c478bd9Sstevel@tonic-gate 				}
336*7c478bd9Sstevel@tonic-gate 				len = strlen(buf);
337*7c478bd9Sstevel@tonic-gate 				buf2 = line_to_buf(buf2, buf);
338*7c478bd9Sstevel@tonic-gate 			}
339*7c478bd9Sstevel@tonic-gate 		} /* end of 'get complete line' */
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 		if (!non_empty(buf2)) { /* is line non empty */
342*7c478bd9Sstevel@tonic-gate 			free(buf2);
343*7c478bd9Sstevel@tonic-gate 			buf2 = NULL;
344*7c478bd9Sstevel@tonic-gate 			continue;
345*7c478bd9Sstevel@tonic-gate 		}
346*7c478bd9Sstevel@tonic-gate 		p = realloc(localvalue, sizeof (char)*(strlen(buf2)+1));
347*7c478bd9Sstevel@tonic-gate 		if (p == NULL) {
348*7c478bd9Sstevel@tonic-gate 			p = realloc(NULL,
349*7c478bd9Sstevel@tonic-gate 				sizeof (char)*(strlen(buf2)+1));
350*7c478bd9Sstevel@tonic-gate 			if (p == NULL) {
351*7c478bd9Sstevel@tonic-gate 				errlog(ERROR | FATAL,
352*7c478bd9Sstevel@tonic-gate 					"Error: unable to "
353*7c478bd9Sstevel@tonic-gate 					"allocate memory\n");
354*7c478bd9Sstevel@tonic-gate 			}
355*7c478bd9Sstevel@tonic-gate 		}
356*7c478bd9Sstevel@tonic-gate 		localvalue = p;
357*7c478bd9Sstevel@tonic-gate 		split(buf2, key, localvalue);
358*7c478bd9Sstevel@tonic-gate 		if (strcasecmp(key, "arch") == 0) {
359*7c478bd9Sstevel@tonic-gate 			char *alist = localvalue, *a;
360*7c478bd9Sstevel@tonic-gate 			has_arch = 1;
361*7c478bd9Sstevel@tonic-gate 			while ((a = strtok(alist, " ,\n")) != NULL) {
362*7c478bd9Sstevel@tonic-gate 				archset = arch_strtoi(a);
363*7c478bd9Sstevel@tonic-gate 				if (arch & archset) {
364*7c478bd9Sstevel@tonic-gate 					free(buf2);
365*7c478bd9Sstevel@tonic-gate 					free(p);
366*7c478bd9Sstevel@tonic-gate 					if (fseeko(fp, offset, SEEK_SET) < 0) {
367*7c478bd9Sstevel@tonic-gate 						errlog(ERROR|FATAL,
368*7c478bd9Sstevel@tonic-gate 						    "%s", strerror(errno));
369*7c478bd9Sstevel@tonic-gate 					}
370*7c478bd9Sstevel@tonic-gate 					return (1);
371*7c478bd9Sstevel@tonic-gate 				}
372*7c478bd9Sstevel@tonic-gate 				alist = NULL;
373*7c478bd9Sstevel@tonic-gate 			}
374*7c478bd9Sstevel@tonic-gate 		} else if (strcasecmp(key, "end") == 0) {
375*7c478bd9Sstevel@tonic-gate 			break;
376*7c478bd9Sstevel@tonic-gate 		}
377*7c478bd9Sstevel@tonic-gate 		free(buf2);
378*7c478bd9Sstevel@tonic-gate 		buf2 = NULL;
379*7c478bd9Sstevel@tonic-gate 	}
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate end:
382*7c478bd9Sstevel@tonic-gate 	free(buf2);
383*7c478bd9Sstevel@tonic-gate 	free(p);
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	if (fseeko(fp, offset, SEEK_SET) < 0) {
386*7c478bd9Sstevel@tonic-gate 		errlog(ERROR|FATAL, "%s", strerror(errno));
387*7c478bd9Sstevel@tonic-gate 	}
388*7c478bd9Sstevel@tonic-gate 	if (has_arch == 0)
389*7c478bd9Sstevel@tonic-gate 		return (1);
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 	return (0);
392*7c478bd9Sstevel@tonic-gate }
393