dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / krb5 / ss / execute_cmd.c
blobfc0d07bc6c35d3cf8e5636f9beec3d5ff15d1bba
1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /*
7 * Copyright 1987, 1988, 1989 by Massachusetts Institute of Technology
9 * For copyright info, see copyright.h.
12 #include "ss_internal.h"
13 #include "copyright.h"
14 #include <stdio.h>
18 * get_request(tbl, idx)
20 * Function:
21 * Gets the idx'th request from the request table pointed to
22 * by tbl.
23 * Arguments:
24 * tbl (ss_request_table *)
25 * pointer to request table
26 * idx (int)
27 * index into table
28 * Returns:
29 * (ss_request_entry *)
30 * pointer to request table entry
31 * Notes:
32 * Has been replaced by a macro.
35 #ifdef __SABER__
36 /* sigh. saber won't deal with pointer-to-const-struct */
37 static struct _ss_request_entry * get_request (tbl, idx)
38 ss_request_table * tbl;
39 int idx;
41 struct _ss_request_table *tbl1 = (struct _ss_request_table *) tbl;
42 struct _ss_request_entry *e = (struct _ss_request_entry *) tbl1->requests;
43 return e + idx;
45 #else
46 #define get_request(tbl,idx) ((tbl) -> requests + (idx))
47 #endif
50 * check_request_table(rqtbl, argc, argv, sci_idx)
52 * Function:
53 * If the command string in argv[0] is in the request table, execute
54 * the commands and return error code 0. Otherwise, return error
55 * code ss_et_command_not_found.
56 * Arguments:
57 * rqtbl (ss_request_table *)
58 * pointer to request table
59 * argc (int)
60 * number of elements in argv[]
61 * argv (char *[])
62 * argument string array
63 * sci_idx (int)
64 * ss-internal index for subsystem control info structure
65 * Returns:
66 * (int)
67 * zero if command found, ss_et_command_not_found otherwise
68 * Notes:
71 static int check_request_table (rqtbl, argc, argv, sci_idx)
72 register ss_request_table *rqtbl;
73 int argc;
74 char *argv[];
75 int sci_idx;
77 #ifdef __SABER__
78 struct _ss_request_entry *request;
79 #else
80 register ss_request_entry *request;
81 #endif
82 register ss_data *info;
83 register char const * const * name;
84 char *string = argv[0];
85 int i;
87 info = ss_info(sci_idx);
88 info->argc = argc;
89 info->argv = argv;
90 for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) {
91 for (name = request->command_names; *name; name++)
92 if (!strcmp(*name, string)) {
93 info->current_request = request->command_names[0];
94 (request->function)(argc, (const char *const *) argv,
95 sci_idx,info->info_ptr);
96 info->current_request = NULL;
97 return(0);
100 return(SS_ET_COMMAND_NOT_FOUND);
104 * really_execute_command(sci_idx, argc, argv)
106 * Function:
107 * Fills in the argc, argv values in the subsystem entry and
108 * call the appropriate routine.
109 * Arguments:
110 * sci_idx (int)
111 * ss-internal index for subsystem control info structure
112 * argc (int)
113 * number of arguments in argument list
114 * argv (char **[])
115 * pointer to parsed argument list (may be reallocated
116 * on abbrev expansion)
118 * Returns:
119 * (int)
120 * Zero if successful, ss_et_command_not_found otherwise.
121 * Notes:
124 static int really_execute_command (sci_idx, argc, argv)
125 int sci_idx;
126 int argc;
127 char **argv[];
129 register ss_request_table **rqtbl;
130 register ss_data *info;
132 info = ss_info(sci_idx);
134 for (rqtbl = info->rqt_tables; *rqtbl; rqtbl++) {
135 if (check_request_table (*rqtbl, argc, *argv, sci_idx) == 0)
136 return(0);
138 return(SS_ET_COMMAND_NOT_FOUND);
142 * ss_execute_command(sci_idx, argv)
144 * Function:
145 * Executes a parsed command list within the subsystem.
146 * Arguments:
147 * sci_idx (int)
148 * ss-internal index for subsystem control info structure
149 * argv (char *[])
150 * parsed argument list
151 * Returns:
152 * (int)
153 * Zero if successful, ss_et_command_not_found otherwise.
154 * Notes:
158 ss_execute_command(sci_idx, argv)
159 int sci_idx;
160 register char *argv[];
162 register int i, argc;
163 char **argp;
165 argc = 0;
166 for (argp = argv; *argp; argp++)
167 argc++;
168 argp = (char **)malloc((argc+1)*sizeof(char *));
169 for (i = 0; i <= argc; i++)
170 argp[i] = argv[i];
171 i = really_execute_command(sci_idx, argc, &argp);
172 free(argp);
173 return(i);
177 * ss_execute_line(sci_idx, line_ptr)
179 * Function:
180 * Parses and executes a command line within a subsystem.
181 * Arguments:
182 * sci_idx (int)
183 * ss-internal index for subsystem control info structure
184 * line_ptr (char *)
185 * Pointer to command line to be parsed.
186 * Returns:
187 * (int)
188 * Error code.
189 * Notes:
192 int ss_execute_line (sci_idx, line_ptr)
193 int sci_idx;
194 char *line_ptr;
196 char **argv;
197 int argc, ret;
199 /* flush leading whitespace */
200 while (line_ptr[0] == ' ' || line_ptr[0] == '\t')
201 line_ptr++;
203 /* check if it should be sent to operating system for execution */
204 if (*line_ptr == '!') {
205 if (ss_info(sci_idx)->flags.escape_disabled)
206 return SS_ET_ESCAPE_DISABLED;
207 else {
208 line_ptr++;
209 system(line_ptr);
210 return 0;
214 /* parse it */
215 /* Solaris Kerberos */
216 (void) ss_parse(sci_idx, line_ptr, &argc, &argv, 0);
217 if (argc == 0)
218 return 0;
220 /* look it up in the request tables, execute if found */
221 ret = really_execute_command (sci_idx, argc, &argv);
223 free(argv);
225 return(ret);