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