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