xref: /titanic_51/usr/src/lib/efcode/packages/parse.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <strings.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <fcode/private.h>
34*7c478bd9Sstevel@tonic-gate #include <fcode/log.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate void
39*7c478bd9Sstevel@tonic-gate byte_loadfile(fcode_env_t *env)
40*7c478bd9Sstevel@tonic-gate {
41*7c478bd9Sstevel@tonic-gate 	int len;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 	load_file(env);
44*7c478bd9Sstevel@tonic-gate 	len = (int) POP(DS);
45*7c478bd9Sstevel@tonic-gate 	if (len) {
46*7c478bd9Sstevel@tonic-gate 		void *ptr = (void *) TOS;
47*7c478bd9Sstevel@tonic-gate 		PUSH(DS, 1);
48*7c478bd9Sstevel@tonic-gate 		byte_load(env);
49*7c478bd9Sstevel@tonic-gate 		FREE(ptr);
50*7c478bd9Sstevel@tonic-gate 	} else {
51*7c478bd9Sstevel@tonic-gate 		drop(env);
52*7c478bd9Sstevel@tonic-gate 	}
53*7c478bd9Sstevel@tonic-gate }
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate void
56*7c478bd9Sstevel@tonic-gate define_hook(fcode_env_t *env, char *name, int len, char *fcimage)
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	static void (*byteload_ptr)(fcode_env_t *env) = byte_loadfile;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	header(env, name, len, 0);
61*7c478bd9Sstevel@tonic-gate 	COMPILE_TOKEN(&do_colon);
62*7c478bd9Sstevel@tonic-gate 	env->state |= 1;
63*7c478bd9Sstevel@tonic-gate 	PUSH(DS, (fstack_t) fcimage);
64*7c478bd9Sstevel@tonic-gate 	PUSH(DS, strlen(fcimage));
65*7c478bd9Sstevel@tonic-gate 	compile_string(env);
66*7c478bd9Sstevel@tonic-gate 	COMPILE_TOKEN(&byteload_ptr);
67*7c478bd9Sstevel@tonic-gate 	semi(env);
68*7c478bd9Sstevel@tonic-gate }
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate  * simple parser for builtin-driver matching.
72*7c478bd9Sstevel@tonic-gate  *
73*7c478bd9Sstevel@tonic-gate  * Consists of alias:target<CR>
74*7c478bd9Sstevel@tonic-gate  * where alias is:
75*7c478bd9Sstevel@tonic-gate  *	<Key>[;<key>[;<key>]]
76*7c478bd9Sstevel@tonic-gate  *
77*7c478bd9Sstevel@tonic-gate  * and target is:
78*7c478bd9Sstevel@tonic-gate  *	<path to fcode image>
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate #define	PARSE_LINE	256
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static void
84*7c478bd9Sstevel@tonic-gate line_error(char *where, int line, char *msg)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	log_message(MSG_ERROR, "%s:%d: %s\n", where, line, msg);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate void
90*7c478bd9Sstevel@tonic-gate make_builtin_hooks(fcode_env_t *env, char *where)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	FILE *fd;
93*7c478bd9Sstevel@tonic-gate 	int lnum = 0, len;
94*7c478bd9Sstevel@tonic-gate 	char *buffer, *line, *target, *next;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	if (where == NULL)
97*7c478bd9Sstevel@tonic-gate 		where = "/fcode/aliases";
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	if ((fd = fopen(where, "r")) == NULL) {
100*7c478bd9Sstevel@tonic-gate 		return;
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	buffer = MALLOC(PARSE_LINE+1);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	while ((line = fgets(buffer, PARSE_LINE, fd)) != NULL) {
106*7c478bd9Sstevel@tonic-gate 		lnum++;
107*7c478bd9Sstevel@tonic-gate 		if ((next = strpbrk(line, " \t#\n")) != NULL)
108*7c478bd9Sstevel@tonic-gate 			*next = '\0';
109*7c478bd9Sstevel@tonic-gate 		if (strlen(line) == 0)
110*7c478bd9Sstevel@tonic-gate 			continue;
111*7c478bd9Sstevel@tonic-gate 		if ((target = strchr(line, ':')) == NULL) {
112*7c478bd9Sstevel@tonic-gate 			line_error(where, lnum, "Badly formed line");
113*7c478bd9Sstevel@tonic-gate 			continue;
114*7c478bd9Sstevel@tonic-gate 		}
115*7c478bd9Sstevel@tonic-gate 		*target++ = 0;
116*7c478bd9Sstevel@tonic-gate 		if (strlen(line) == 0) {
117*7c478bd9Sstevel@tonic-gate 			line_error(where, lnum, "Badly formed alias");
118*7c478bd9Sstevel@tonic-gate 			continue;
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 		if (strlen(target) == 0) {
121*7c478bd9Sstevel@tonic-gate 			line_error(where, lnum, "Badly formed target");
122*7c478bd9Sstevel@tonic-gate 			continue;
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 		for (; line; line = next) {
125*7c478bd9Sstevel@tonic-gate 			if ((next = strchr(line, ';')) != NULL)
126*7c478bd9Sstevel@tonic-gate 				*next++ = '\0';
127*7c478bd9Sstevel@tonic-gate 			if (strlen(line) == 0)
128*7c478bd9Sstevel@tonic-gate 				line_error(where, lnum, "Null key in alias");
129*7c478bd9Sstevel@tonic-gate 			else
130*7c478bd9Sstevel@tonic-gate 				define_hook(env, line, strlen(line), target);
131*7c478bd9Sstevel@tonic-gate 		}
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 	FREE(buffer);
134*7c478bd9Sstevel@tonic-gate 	fclose(fd);
135*7c478bd9Sstevel@tonic-gate }
136