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.
22 #include "ss_internal.h"
25 static int check_request_table PROTOTYPE((ss_request_table *rqtbl, int argc,
26 char *argv[], int sci_idx));
27 static int really_execute_command PROTOTYPE((int sci_idx, int argc,
31 * get_request(tbl, idx)
34 * Gets the idx'th request from the request table pointed to
37 * tbl (ss_request_table *)
38 * pointer to request table
42 * (ss_request_entry *)
43 * pointer to request table entry
45 * Has been replaced by a macro.
49 /* sigh. saber won't deal with pointer-to-const-struct */
50 static struct _ss_request_entry * get_request (tbl, idx)
51 ss_request_table * tbl;
54 struct _ss_request_table *tbl1 = (struct _ss_request_table *) tbl;
55 struct _ss_request_entry *e = (struct _ss_request_entry *) tbl1->requests;
59 #define get_request(tbl,idx) ((tbl) -> requests + (idx))
63 * check_request_table(rqtbl, argc, argv, sci_idx)
66 * If the command string in argv[0] is in the request table, execute
67 * the commands and return error code 0. Otherwise, return error
68 * code ss_et_command_not_found.
70 * rqtbl (ss_request_table *)
71 * pointer to request table
73 * number of elements in argv[]
75 * argument string array
77 * ss-internal index for subsystem control info structure
80 * zero if command found, ss_et_command_not_found otherwise
84 static int check_request_table (rqtbl, argc, argv, sci_idx)
85 register ss_request_table *rqtbl;
91 struct _ss_request_entry *request;
93 register ss_request_entry *request;
95 register ss_data *info;
96 register char const * const * name;
97 char *string = argv[0];
100 info = ss_info(sci_idx);
103 for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) {
104 for (name = request->command_names; *name; name++)
105 if (!strcmp(*name, string)) {
106 info->current_request = request->command_names[0];
107 (request->function)(argc, (const char *const *) argv,
108 sci_idx,info->info_ptr);
109 info->current_request = (char *)NULL;
113 return(SS_ET_COMMAND_NOT_FOUND);
117 * really_execute_command(sci_idx, argc, argv)
120 * Fills in the argc, argv values in the subsystem entry and
121 * call the appropriate routine.
124 * ss-internal index for subsystem control info structure
126 * number of arguments in argument list
128 * pointer to parsed argument list (may be reallocated
129 * on abbrev expansion)
133 * Zero if successful, ss_et_command_not_found otherwise.
137 static int really_execute_command (sci_idx, argc, argv)
142 register ss_request_table **rqtbl;
143 register ss_data *info;
145 info = ss_info(sci_idx);
147 for (rqtbl = info->rqt_tables; *rqtbl; rqtbl++) {
148 if (check_request_table (*rqtbl, argc, *argv, sci_idx) == 0)
151 return(SS_ET_COMMAND_NOT_FOUND);
155 * ss_execute_command(sci_idx, argv)
158 * Executes a parsed command list within the subsystem.
161 * ss-internal index for subsystem control info structure
163 * parsed argument list
166 * Zero if successful, ss_et_command_not_found otherwise.
170 int ss_execute_command(sci_idx, argv)
172 register char *argv[];
174 register int i, argc;
178 for (argp = argv; *argp; argp++)
180 argp = (char **)malloc((argc+1)*sizeof(char *));
181 for (i = 0; i <= argc; i++)
183 i = really_execute_command(sci_idx, argc, &argp);
189 * ss_execute_line(sci_idx, line_ptr)
192 * Parses and executes a command line within a subsystem.
195 * ss-internal index for subsystem control info structure
197 * Pointer to command line to be parsed.
204 int ss_execute_line (sci_idx, line_ptr)
211 /* flush leading whitespace */
212 while (line_ptr[0] == ' ' || line_ptr[0] == '\t')
215 /* check if it should be sent to operating system for execution */
216 if (*line_ptr == '!') {
217 if (ss_info(sci_idx)->flags.escape_disabled)
218 return SS_ET_ESCAPE_DISABLED;
221 return (system(line_ptr) < 0) ? errno : 0;
226 argv = ss_parse(sci_idx, line_ptr, &argc);
232 /* look it up in the request tables, execute if found */
233 ret = really_execute_command (sci_idx, argc, &argv);