xref: /onnv-gate/usr/src/lib/efcode/packages/parse.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <strings.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <fcode/private.h>
34*0Sstevel@tonic-gate #include <fcode/log.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate void
byte_loadfile(fcode_env_t * env)39*0Sstevel@tonic-gate byte_loadfile(fcode_env_t *env)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	int len;
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 	load_file(env);
44*0Sstevel@tonic-gate 	len = (int) POP(DS);
45*0Sstevel@tonic-gate 	if (len) {
46*0Sstevel@tonic-gate 		void *ptr = (void *) TOS;
47*0Sstevel@tonic-gate 		PUSH(DS, 1);
48*0Sstevel@tonic-gate 		byte_load(env);
49*0Sstevel@tonic-gate 		FREE(ptr);
50*0Sstevel@tonic-gate 	} else {
51*0Sstevel@tonic-gate 		drop(env);
52*0Sstevel@tonic-gate 	}
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate void
define_hook(fcode_env_t * env,char * name,int len,char * fcimage)56*0Sstevel@tonic-gate define_hook(fcode_env_t *env, char *name, int len, char *fcimage)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	static void (*byteload_ptr)(fcode_env_t *env) = byte_loadfile;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 	header(env, name, len, 0);
61*0Sstevel@tonic-gate 	COMPILE_TOKEN(&do_colon);
62*0Sstevel@tonic-gate 	env->state |= 1;
63*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t) fcimage);
64*0Sstevel@tonic-gate 	PUSH(DS, strlen(fcimage));
65*0Sstevel@tonic-gate 	compile_string(env);
66*0Sstevel@tonic-gate 	COMPILE_TOKEN(&byteload_ptr);
67*0Sstevel@tonic-gate 	semi(env);
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * simple parser for builtin-driver matching.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * Consists of alias:target<CR>
74*0Sstevel@tonic-gate  * where alias is:
75*0Sstevel@tonic-gate  *	<Key>[;<key>[;<key>]]
76*0Sstevel@tonic-gate  *
77*0Sstevel@tonic-gate  * and target is:
78*0Sstevel@tonic-gate  *	<path to fcode image>
79*0Sstevel@tonic-gate  */
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #define	PARSE_LINE	256
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate static void
line_error(char * where,int line,char * msg)84*0Sstevel@tonic-gate line_error(char *where, int line, char *msg)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	log_message(MSG_ERROR, "%s:%d: %s\n", where, line, msg);
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate void
make_builtin_hooks(fcode_env_t * env,char * where)90*0Sstevel@tonic-gate make_builtin_hooks(fcode_env_t *env, char *where)
91*0Sstevel@tonic-gate {
92*0Sstevel@tonic-gate 	FILE *fd;
93*0Sstevel@tonic-gate 	int lnum = 0, len;
94*0Sstevel@tonic-gate 	char *buffer, *line, *target, *next;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	if (where == NULL)
97*0Sstevel@tonic-gate 		where = "/fcode/aliases";
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	if ((fd = fopen(where, "r")) == NULL) {
100*0Sstevel@tonic-gate 		return;
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	buffer = MALLOC(PARSE_LINE+1);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	while ((line = fgets(buffer, PARSE_LINE, fd)) != NULL) {
106*0Sstevel@tonic-gate 		lnum++;
107*0Sstevel@tonic-gate 		if ((next = strpbrk(line, " \t#\n")) != NULL)
108*0Sstevel@tonic-gate 			*next = '\0';
109*0Sstevel@tonic-gate 		if (strlen(line) == 0)
110*0Sstevel@tonic-gate 			continue;
111*0Sstevel@tonic-gate 		if ((target = strchr(line, ':')) == NULL) {
112*0Sstevel@tonic-gate 			line_error(where, lnum, "Badly formed line");
113*0Sstevel@tonic-gate 			continue;
114*0Sstevel@tonic-gate 		}
115*0Sstevel@tonic-gate 		*target++ = 0;
116*0Sstevel@tonic-gate 		if (strlen(line) == 0) {
117*0Sstevel@tonic-gate 			line_error(where, lnum, "Badly formed alias");
118*0Sstevel@tonic-gate 			continue;
119*0Sstevel@tonic-gate 		}
120*0Sstevel@tonic-gate 		if (strlen(target) == 0) {
121*0Sstevel@tonic-gate 			line_error(where, lnum, "Badly formed target");
122*0Sstevel@tonic-gate 			continue;
123*0Sstevel@tonic-gate 		}
124*0Sstevel@tonic-gate 		for (; line; line = next) {
125*0Sstevel@tonic-gate 			if ((next = strchr(line, ';')) != NULL)
126*0Sstevel@tonic-gate 				*next++ = '\0';
127*0Sstevel@tonic-gate 			if (strlen(line) == 0)
128*0Sstevel@tonic-gate 				line_error(where, lnum, "Null key in alias");
129*0Sstevel@tonic-gate 			else
130*0Sstevel@tonic-gate 				define_hook(env, line, strlen(line), target);
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate 	FREE(buffer);
134*0Sstevel@tonic-gate 	fclose(fd);
135*0Sstevel@tonic-gate }
136