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(register ss_request_table *rqtbl, int argc,
86 char *argv[], int sci_idx)
89 struct _ss_request_entry *request;
91 register ss_request_entry *request;
93 register ss_data *info;
94 register char const * const * name;
95 char *string = argv[0];
98 info = ss_info(sci_idx);
101 for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) {
102 for (name = request->command_names; *name; name++)
103 if (!strcmp(*name, string)) {
104 info->current_request = request->command_names[0];
105 (request->function)(argc, (const char *const *) argv,
106 sci_idx,info->info_ptr);
107 info->current_request = (char *)NULL;
111 return(SS_ET_COMMAND_NOT_FOUND);
115 * really_execute_command(sci_idx, argc, argv)
118 * Fills in the argc, argv values in the subsystem entry and
119 * call the appropriate routine.
122 * ss-internal index for subsystem control info structure
124 * number of arguments in argument list
126 * pointer to parsed argument list (may be reallocated
127 * on abbrev expansion)
131 * Zero if successful, ss_et_command_not_found otherwise.
135 static int really_execute_command(int sci_idx, int argc, char **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(int sci_idx, register char *argv[])
167 register int i, argc;
171 for (argp = argv; *argp; argp++)
173 argp = (char **)malloc((argc+1)*sizeof(char *));
174 for (i = 0; i <= argc; i++)
176 i = really_execute_command(sci_idx, argc, &argp);
182 * ss_execute_line(sci_idx, line_ptr)
185 * Parses and executes a command line within a subsystem.
188 * ss-internal index for subsystem control info structure
190 * Pointer to command line to be parsed.
197 int ss_execute_line(int sci_idx, char *line_ptr)
202 /* flush leading whitespace */
203 while (line_ptr[0] == ' ' || line_ptr[0] == '\t')
206 /* check if it should be sent to operating system for execution */
207 if (*line_ptr == '!') {
208 if (ss_info(sci_idx)->flags.escape_disabled)
209 return SS_ET_ESCAPE_DISABLED;
212 return (system(line_ptr) < 0) ? errno : 0;
217 argv = ss_parse(sci_idx, line_ptr, &argc);
223 /* look it up in the request tables, execute if found */
224 ret = really_execute_command (sci_idx, argc, &argv);