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.
23 #include "ss_internal.h"
26 static int check_request_table PROTOTYPE((ss_request_table *rqtbl, int argc,
27 char *argv[], int sci_idx));
28 static int really_execute_command PROTOTYPE((int sci_idx, int argc,
32 * get_request(tbl, idx)
35 * Gets the idx'th request from the request table pointed to
38 * tbl (ss_request_table *)
39 * pointer to request table
43 * (ss_request_entry *)
44 * pointer to request table entry
46 * Has been replaced by a macro.
50 /* sigh. saber won't deal with pointer-to-const-struct */
51 static struct _ss_request_entry * get_request (tbl, idx)
52 ss_request_table * tbl;
55 struct _ss_request_table *tbl1 = (struct _ss_request_table *) tbl;
56 struct _ss_request_entry *e = (struct _ss_request_entry *) tbl1->requests;
60 #define get_request(tbl,idx) ((tbl) -> requests + (idx))
64 * check_request_table(rqtbl, argc, argv, sci_idx)
67 * If the command string in argv[0] is in the request table, execute
68 * the commands and return error code 0. Otherwise, return error
69 * code ss_et_command_not_found.
71 * rqtbl (ss_request_table *)
72 * pointer to request table
74 * number of elements in argv[]
76 * argument string array
78 * ss-internal index for subsystem control info structure
81 * zero if command found, ss_et_command_not_found otherwise
85 static int check_request_table (rqtbl, argc, argv, sci_idx)
86 register ss_request_table *rqtbl;
92 struct _ss_request_entry *request;
94 register ss_request_entry *request;
96 register ss_data *info;
97 register char const * const * name;
98 char *string = argv[0];
101 info = ss_info(sci_idx);
104 for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) {
105 for (name = request->command_names; *name; name++)
106 if (!strcmp(*name, string)) {
107 info->current_request = request->command_names[0];
108 (request->function)(argc, (const char *const *) argv,
109 sci_idx,info->info_ptr);
110 info->current_request = (char *)NULL;
114 return(SS_ET_COMMAND_NOT_FOUND);
118 * really_execute_command(sci_idx, argc, argv)
121 * Fills in the argc, argv values in the subsystem entry and
122 * call the appropriate routine.
125 * ss-internal index for subsystem control info structure
127 * number of arguments in argument list
129 * pointer to parsed argument list (may be reallocated
130 * on abbrev expansion)
134 * Zero if successful, ss_et_command_not_found otherwise.
138 static int really_execute_command (sci_idx, argc, argv)
143 register ss_request_table **rqtbl;
144 register ss_data *info;
146 info = ss_info(sci_idx);
148 for (rqtbl = info->rqt_tables; *rqtbl; rqtbl++) {
149 if (check_request_table (*rqtbl, argc, *argv, sci_idx) == 0)
152 return(SS_ET_COMMAND_NOT_FOUND);
156 * ss_execute_command(sci_idx, argv)
159 * Executes a parsed command list within the subsystem.
162 * ss-internal index for subsystem control info structure
164 * parsed argument list
167 * Zero if successful, ss_et_command_not_found otherwise.
171 int ss_execute_command(sci_idx, argv)
173 register char *argv[];
175 register int i, argc;
179 for (argp = argv; *argp; argp++)
181 argp = (char **)malloc((argc+1)*sizeof(char *));
182 for (i = 0; i <= argc; i++)
184 i = really_execute_command(sci_idx, argc, &argp);
190 * ss_execute_line(sci_idx, line_ptr)
193 * Parses and executes a command line within a subsystem.
196 * ss-internal index for subsystem control info structure
198 * Pointer to command line to be parsed.
205 int ss_execute_line (sci_idx, line_ptr)
212 /* flush leading whitespace */
213 while (line_ptr[0] == ' ' || line_ptr[0] == '\t')
216 /* check if it should be sent to operating system for execution */
217 if (*line_ptr == '!') {
218 if (ss_info(sci_idx)->flags.escape_disabled)
219 return SS_ET_ESCAPE_DISABLED;
222 return (system(line_ptr) < 0) ? errno : 0;
227 argv = ss_parse(sci_idx, line_ptr, &argc);
233 /* look it up in the request tables, execute if found */
234 ret = really_execute_command (sci_idx, argc, &argv);