xref: /onnv-gate/usr/src/lib/efcode/interpreter/interpreter.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 2000-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
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 /*
30*0Sstevel@tonic-gate  * Embedded Fcode Interpreter
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * Process cmd line args and invoke Fcode engine.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/stat.h>
36*0Sstevel@tonic-gate #include <sys/wait.h>
37*0Sstevel@tonic-gate #include <fcntl.h>
38*0Sstevel@tonic-gate #include <unistd.h>
39*0Sstevel@tonic-gate #include <stdio.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <strings.h>
42*0Sstevel@tonic-gate #include <stropts.h>
43*0Sstevel@tonic-gate #include <ctype.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <fcode/engine.h>
46*0Sstevel@tonic-gate #include <fcode/log.h>
47*0Sstevel@tonic-gate #include <fcode/debug.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #define	MSG_ERRLOG_DEFAULT	(MSG_FATAL|MSG_ERROR|MSG_WARN|MSG_INFO|\
52*0Sstevel@tonic-gate 				    MSG_DEBUG|MSG_FC_DEBUG)
53*0Sstevel@tonic-gate #define	MSG_SYSLOG_DEFAULT	(MSG_FATAL|MSG_ERROR|MSG_WARN)
54*0Sstevel@tonic-gate #define	DEBUG_FC_LIST		(DEBUG_COMMA|DEBUG_EXEC_TRACE|\
55*0Sstevel@tonic-gate 				    DEBUG_EXEC_DUMP_RS|DEBUG_EXEC_DUMP_RS|\
56*0Sstevel@tonic-gate 				    DEBUG_EXEC_SHOW_VITALS|DEBUG_TRACING|\
57*0Sstevel@tonic-gate 				    DEBUG_BYTELOAD_DS|DEBUG_BYTELOAD_RS|\
58*0Sstevel@tonic-gate 				    DEBUG_BYTELOAD_TOKENS|DEBUG_SHOW_RS|\
59*0Sstevel@tonic-gate 				    DEBUG_SHOW_STACK)
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate common_data_t common;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate void *fc_env;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate void
usage(char * argv[])66*0Sstevel@tonic-gate usage(char *argv[])
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	log_message(MSG_ERROR, "Usage: %s <flags>\n", argv[0]);
69*0Sstevel@tonic-gate 	log_message(MSG_ERROR,
70*0Sstevel@tonic-gate 	    "    -D                        fcode_debug = true\n");
71*0Sstevel@tonic-gate 	log_message(MSG_ERROR,
72*0Sstevel@tonic-gate 	    "    -d <level>                set debug level\n");
73*0Sstevel@tonic-gate 	log_message(MSG_ERROR,
74*0Sstevel@tonic-gate 	    "    -f <file>                 interpret fcode/source <file>\n");
75*0Sstevel@tonic-gate 	log_message(MSG_ERROR,
76*0Sstevel@tonic-gate 	    "    -i                        go 'interactive'\n");
77*0Sstevel@tonic-gate 	log_message(MSG_ERROR,
78*0Sstevel@tonic-gate 	    "    -s <string>               interpret <string> as forth\n");
79*0Sstevel@tonic-gate 	log_message(MSG_ERROR,
80*0Sstevel@tonic-gate 	    "    -a                        FCODE image has a.out header\n");
81*0Sstevel@tonic-gate 	log_message(MSG_ERROR,
82*0Sstevel@tonic-gate 	    "    -e [<msglvl>:]<errorlog>  Set error log file\n");
83*0Sstevel@tonic-gate 	log_message(MSG_ERROR,
84*0Sstevel@tonic-gate 	    "    -l <msglvl>               Set syslog message level\n");
85*0Sstevel@tonic-gate 	log_message(MSG_ERROR,
86*0Sstevel@tonic-gate 	    "    -k                        Toggle OBP page kludge\n");
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate fcode_env_t *env;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate int
main(int argc,char * argv[])92*0Sstevel@tonic-gate main(int argc, char *argv[])
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	extern char *optarg;
95*0Sstevel@tonic-gate 	extern int optind, opterr, optopt;
96*0Sstevel@tonic-gate 	int c, aout = 0;
97*0Sstevel@tonic-gate 	char *fcode_file = NULL;
98*0Sstevel@tonic-gate 	char *forthstr = NULL;
99*0Sstevel@tonic-gate 	int debug = 0;
100*0Sstevel@tonic-gate 	int syslog_flags = MSG_SYSLOG_DEFAULT;
101*0Sstevel@tonic-gate 	int lflag = 0;
102*0Sstevel@tonic-gate 	int error_log_flags;
103*0Sstevel@tonic-gate 	char *errlog = NULL;
104*0Sstevel@tonic-gate 	extern void run_one_efdaemon_request(fcode_env_t *);
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	common.Progname = argv[0];
107*0Sstevel@tonic-gate 	common.search_path = getenv("FC_SEARCH_PATH");
108*0Sstevel@tonic-gate 	common.fcode_fd = -1;
109*0Sstevel@tonic-gate 	env = fc_env  = clone_environment(NULL, &common);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ad:e:f:l:iDs:k")) != EOF) {
112*0Sstevel@tonic-gate 		switch (c) {
113*0Sstevel@tonic-gate 		case 'a':
114*0Sstevel@tonic-gate 			aout = 1;
115*0Sstevel@tonic-gate 			break;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 		case 'd':
118*0Sstevel@tonic-gate 			debug = debug_flags_to_mask(optarg);
119*0Sstevel@tonic-gate 			set_interpreter_debug_level(debug);
120*0Sstevel@tonic-gate 			if (debug)
121*0Sstevel@tonic-gate 				env->fcode_debug = 1;
122*0Sstevel@tonic-gate 			break;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 		case 'e':
125*0Sstevel@tonic-gate 			if ((errlog = strchr(optarg, ':')) != NULL) {
126*0Sstevel@tonic-gate 				*errlog++ = '\0';
127*0Sstevel@tonic-gate 				error_log_flags = parse_msg_flags(optarg);
128*0Sstevel@tonic-gate 			} else {
129*0Sstevel@tonic-gate 				errlog = optarg;
130*0Sstevel@tonic-gate 				error_log_flags = MSG_ERRLOG_DEFAULT;
131*0Sstevel@tonic-gate 			}
132*0Sstevel@tonic-gate 			open_error_log(errlog, error_log_flags);
133*0Sstevel@tonic-gate 			break;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 		case 'l':
136*0Sstevel@tonic-gate 			syslog_flags = parse_msg_flags(optarg);
137*0Sstevel@tonic-gate 			lflag++;
138*0Sstevel@tonic-gate 			break;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		case 'D':
141*0Sstevel@tonic-gate 			env->fcode_debug = 1;
142*0Sstevel@tonic-gate 			break;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		case 'f':
145*0Sstevel@tonic-gate 			fcode_file = optarg;
146*0Sstevel@tonic-gate 			break;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		case 'i':
149*0Sstevel@tonic-gate 			forthstr = "interact";
150*0Sstevel@tonic-gate 			env->fcode_debug = 1;
151*0Sstevel@tonic-gate 			break;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 		case 's':
154*0Sstevel@tonic-gate 			forthstr = optarg;
155*0Sstevel@tonic-gate 			break;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 		case '?':
158*0Sstevel@tonic-gate 			usage(argv);
159*0Sstevel@tonic-gate 			exit(1);
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	if (forthstr) {
164*0Sstevel@tonic-gate 		run_fcode(env, (uchar_t *)forthstr, strlen(forthstr));
165*0Sstevel@tonic-gate 	} else if (fcode_file) {
166*0Sstevel@tonic-gate 		run_fcode_from_file(env, fcode_file, aout);
167*0Sstevel@tonic-gate 	} else {
168*0Sstevel@tonic-gate 		if ((debug & DEBUG_FC_LIST) != 0 &&
169*0Sstevel@tonic-gate 		    ((error_log_flags | syslog_flags) & MSG_FC_DEBUG) == 0) {
170*0Sstevel@tonic-gate 			log_message(MSG_WARN, "Warning, verbose debug flag(s)"
171*0Sstevel@tonic-gate 			    " on, but syslog/errlog not enabled for verbose"
172*0Sstevel@tonic-gate 			    " debug\n");
173*0Sstevel@tonic-gate 			if (errlog)
174*0Sstevel@tonic-gate 				error_log_flags |= MSG_FC_DEBUG;
175*0Sstevel@tonic-gate 			else
176*0Sstevel@tonic-gate 				syslog_flags |= MSG_FC_DEBUG;
177*0Sstevel@tonic-gate 		}
178*0Sstevel@tonic-gate 		if ((debug & ~DEBUG_FC_LIST) != 0 &&
179*0Sstevel@tonic-gate 		    ((error_log_flags | syslog_flags) & MSG_DEBUG) == 0) {
180*0Sstevel@tonic-gate 			log_message(MSG_WARN, "Warning, debug flag(s) on, but"
181*0Sstevel@tonic-gate 			    " syslog/errlog not enabled for debug\n");
182*0Sstevel@tonic-gate 			if (errlog)
183*0Sstevel@tonic-gate 				error_log_flags |= MSG_DEBUG;
184*0Sstevel@tonic-gate 			else
185*0Sstevel@tonic-gate 				syslog_flags |= MSG_DEBUG;
186*0Sstevel@tonic-gate 		}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		if (errlog == NULL || lflag) {
189*0Sstevel@tonic-gate 			if (syslog_flags & MSG_FC_DEBUG)
190*0Sstevel@tonic-gate 				log_message(MSG_WARN, "Warning, verbose debug"
191*0Sstevel@tonic-gate 				    " not recommended for syslog\n");
192*0Sstevel@tonic-gate 			open_syslog_log("interpreter", syslog_flags);
193*0Sstevel@tonic-gate 		}
194*0Sstevel@tonic-gate 		run_one_efdaemon_request(env);
195*0Sstevel@tonic-gate 	}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	return (0);
198*0Sstevel@tonic-gate }
199