2 * Copyright 1987, 1988, 1989 by Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose is hereby granted, provided that
6 * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
7 * advertising or publicity pertaining to distribution of the software
8 * without specific, written prior permission. M.I.T. and the
9 * M.I.T. S.I.P.B. make no representations about the suitability of
10 * this software for any purpose. It is provided "as is" without
11 * express or implied warranty.
17 #include "ss_internal.h"
20 static int check_request_table PROTOTYPE((ss_request_table *rqtbl, int argc,
21 char *argv[], int sci_idx));
22 static int really_execute_command PROTOTYPE((int sci_idx, int argc,
26 * get_request(tbl, idx)
29 * Gets the idx'th request from the request table pointed to
32 * tbl (ss_request_table *)
33 * pointer to request table
37 * (ss_request_entry *)
38 * pointer to request table entry
40 * Has been replaced by a macro.
44 /* sigh. saber won't deal with pointer-to-const-struct */
45 static struct _ss_request_entry * get_request (tbl, idx)
46 ss_request_table * tbl;
49 struct _ss_request_table *tbl1 = (struct _ss_request_table *) tbl;
50 struct _ss_request_entry *e = (struct _ss_request_entry *) tbl1->requests;
54 #define get_request(tbl,idx) ((tbl) -> requests + (idx))
58 * check_request_table(rqtbl, argc, argv, sci_idx)
61 * If the command string in argv[0] is in the request table, execute
62 * the commands and return error code 0. Otherwise, return error
63 * code ss_et_command_not_found.
65 * rqtbl (ss_request_table *)
66 * pointer to request table
68 * number of elements in argv[]
70 * argument string array
72 * ss-internal index for subsystem control info structure
75 * zero if command found, ss_et_command_not_found otherwise
79 static int check_request_table (rqtbl, argc, argv, sci_idx)
80 register ss_request_table *rqtbl;
86 struct _ss_request_entry *request;
88 register ss_request_entry *request;
90 register ss_data *info;
91 register char const * const * name;
92 char *string = argv[0];
95 info = ss_info(sci_idx);
98 for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) {
99 for (name = request->command_names; *name; name++)
100 if (!strcmp(*name, string)) {
101 info->current_request = request->command_names[0];
102 (request->function)(argc, (const char *const *) argv,
103 sci_idx,info->info_ptr);
104 info->current_request = (char *)NULL;
108 return(SS_ET_COMMAND_NOT_FOUND);
112 * really_execute_command(sci_idx, argc, argv)
115 * Fills in the argc, argv values in the subsystem entry and
116 * call the appropriate routine.
119 * ss-internal index for subsystem control info structure
121 * number of arguments in argument list
123 * pointer to parsed argument list (may be reallocated
124 * on abbrev expansion)
128 * Zero if successful, ss_et_command_not_found otherwise.
132 static int really_execute_command (sci_idx, argc, argv)
137 register ss_request_table **rqtbl;
138 register ss_data *info;
140 info = ss_info(sci_idx);
142 for (rqtbl = info->rqt_tables; *rqtbl; rqtbl++) {
143 if (check_request_table (*rqtbl, argc, *argv, sci_idx) == 0)
146 return(SS_ET_COMMAND_NOT_FOUND);
150 * ss_execute_command(sci_idx, argv)
153 * Executes a parsed command list within the subsystem.
156 * ss-internal index for subsystem control info structure
158 * parsed argument list
161 * Zero if successful, ss_et_command_not_found otherwise.
165 int ss_execute_command(sci_idx, argv)
167 register char *argv[];
169 register int i, argc;
173 for (argp = argv; *argp; argp++)
175 argp = (char **)malloc((argc+1)*sizeof(char *));
176 for (i = 0; i <= argc; i++)
178 i = really_execute_command(sci_idx, argc, &argp);
184 * ss_execute_line(sci_idx, line_ptr)
187 * Parses and executes a command line within a subsystem.
190 * ss-internal index for subsystem control info structure
192 * Pointer to command line to be parsed.
199 int ss_execute_line (sci_idx, line_ptr)
206 /* flush leading whitespace */
207 while (line_ptr[0] == ' ' || line_ptr[0] == '\t')
210 /* check if it should be sent to operating system for execution */
211 if (*line_ptr == '!') {
212 if (ss_info(sci_idx)->flags.escape_disabled)
213 return SS_ET_ESCAPE_DISABLED;
222 argv = ss_parse(sci_idx, line_ptr, &argc);
229 /* look it up in the request tables, execute if found */
230 ret = really_execute_command (sci_idx, argc, &argv);