summaryrefslogtreecommitdiff
path: root/usr/src/lib/efcode/interpreter/interpreter.c
blob: 73383e57044cd4746d062bb303d3e78022b172aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2000-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Embedded Fcode Interpreter
 *
 * Process cmd line args and invoke Fcode engine.
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <stropts.h>
#include <ctype.h>

#include <fcode/engine.h>
#include <fcode/log.h>
#include <fcode/debug.h>

#include <fcdriver/fcdriver.h>

#define	MSG_ERRLOG_DEFAULT	(MSG_FATAL|MSG_ERROR|MSG_WARN|MSG_INFO|\
				    MSG_DEBUG|MSG_FC_DEBUG)
#define	MSG_SYSLOG_DEFAULT	(MSG_FATAL|MSG_ERROR|MSG_WARN)
#define	DEBUG_FC_LIST		(DEBUG_COMMA|DEBUG_EXEC_TRACE|\
				    DEBUG_EXEC_DUMP_RS|DEBUG_EXEC_DUMP_RS|\
				    DEBUG_EXEC_SHOW_VITALS|DEBUG_TRACING|\
				    DEBUG_BYTELOAD_DS|DEBUG_BYTELOAD_RS|\
				    DEBUG_BYTELOAD_TOKENS|DEBUG_SHOW_RS|\
				    DEBUG_SHOW_STACK)

common_data_t common;

void *fc_env;

void
usage(char *argv[])
{
	log_message(MSG_ERROR, "Usage: %s <flags>\n", argv[0]);
	log_message(MSG_ERROR,
	    "    -D                        fcode_debug = true\n");
	log_message(MSG_ERROR,
	    "    -d <level>                set debug level\n");
	log_message(MSG_ERROR,
	    "    -f <file>                 interpret fcode/source <file>\n");
	log_message(MSG_ERROR,
	    "    -i                        go 'interactive'\n");
	log_message(MSG_ERROR,
	    "    -s <string>               interpret <string> as forth\n");
	log_message(MSG_ERROR,
	    "    -a                        FCODE image has a.out header\n");
	log_message(MSG_ERROR,
	    "    -e [<msglvl>:]<errorlog>  Set error log file\n");
	log_message(MSG_ERROR,
	    "    -l <msglvl>               Set syslog message level\n");
	log_message(MSG_ERROR,
	    "    -k                        Toggle OBP page kludge\n");
}

fcode_env_t *env;

int
main(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind, opterr, optopt;
	int c, aout = 0;
	char *fcode_file = NULL;
	char *forthstr = NULL;
	int debug = 0;
	int syslog_flags = MSG_SYSLOG_DEFAULT;
	int lflag = 0;
	int error_log_flags;
	char *errlog = NULL;
	extern void run_one_efdaemon_request(fcode_env_t *);

	common.Progname = argv[0];
	common.search_path = getenv("FC_SEARCH_PATH");
	common.fcode_fd = -1;
	env = fc_env  = clone_environment(NULL, &common);

	while ((c = getopt(argc, argv, "ad:e:f:l:iDs:k")) != EOF) {
		switch (c) {
		case 'a':
			aout = 1;
			break;

		case 'd':
			debug = debug_flags_to_mask(optarg);
			set_interpreter_debug_level(debug);
			if (debug)
				env->fcode_debug = 1;
			break;

		case 'e':
			if ((errlog = strchr(optarg, ':')) != NULL) {
				*errlog++ = '\0';
				error_log_flags = parse_msg_flags(optarg);
			} else {
				errlog = optarg;
				error_log_flags = MSG_ERRLOG_DEFAULT;
			}
			open_error_log(errlog, error_log_flags);
			break;

		case 'l':
			syslog_flags = parse_msg_flags(optarg);
			lflag++;
			break;

		case 'D':
			env->fcode_debug = 1;
			break;

		case 'f':
			fcode_file = optarg;
			break;

		case 'i':
			forthstr = "interact";
			env->fcode_debug = 1;
			break;

		case 's':
			forthstr = optarg;
			break;

		case '?':
			usage(argv);
			exit(1);
		}
	}

	if (forthstr) {
		run_fcode(env, (uchar_t *)forthstr, strlen(forthstr));
	} else if (fcode_file) {
		run_fcode_from_file(env, fcode_file, aout);
	} else {
		if ((debug & DEBUG_FC_LIST) != 0 &&
		    ((error_log_flags | syslog_flags) & MSG_FC_DEBUG) == 0) {
			log_message(MSG_WARN, "Warning, verbose debug flag(s)"
			    " on, but syslog/errlog not enabled for verbose"
			    " debug\n");
			if (errlog)
				error_log_flags |= MSG_FC_DEBUG;
			else
				syslog_flags |= MSG_FC_DEBUG;
		}
		if ((debug & ~DEBUG_FC_LIST) != 0 &&
		    ((error_log_flags | syslog_flags) & MSG_DEBUG) == 0) {
			log_message(MSG_WARN, "Warning, debug flag(s) on, but"
			    " syslog/errlog not enabled for debug\n");
			if (errlog)
				error_log_flags |= MSG_DEBUG;
			else
				syslog_flags |= MSG_DEBUG;
		}

		if (errlog == NULL || lflag) {
			if (syslog_flags & MSG_FC_DEBUG)
				log_message(MSG_WARN, "Warning, verbose debug"
				    " not recommended for syslog\n");
			open_syslog_log("interpreter", syslog_flags);
		}
		run_one_efdaemon_request(env);
	}

	return (0);
}