1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 3*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 4*7c478bd9Sstevel@tonic-gate */ 5*7c478bd9Sstevel@tonic-gate 6*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 7*7c478bd9Sstevel@tonic-gate 8*7c478bd9Sstevel@tonic-gate /* 9*7c478bd9Sstevel@tonic-gate * Copyright 1987, 1988, 1989 by Massachusetts Institute of Technology 10*7c478bd9Sstevel@tonic-gate * 11*7c478bd9Sstevel@tonic-gate * For copyright info, see copyright.h. 12*7c478bd9Sstevel@tonic-gate */ 13*7c478bd9Sstevel@tonic-gate 14*7c478bd9Sstevel@tonic-gate #include "ss_internal.h" 15*7c478bd9Sstevel@tonic-gate #include "copyright.h" 16*7c478bd9Sstevel@tonic-gate #include <stdio.h> 17*7c478bd9Sstevel@tonic-gate 18*7c478bd9Sstevel@tonic-gate 19*7c478bd9Sstevel@tonic-gate /* 20*7c478bd9Sstevel@tonic-gate * get_request(tbl, idx) 21*7c478bd9Sstevel@tonic-gate * 22*7c478bd9Sstevel@tonic-gate * Function: 23*7c478bd9Sstevel@tonic-gate * Gets the idx'th request from the request table pointed to 24*7c478bd9Sstevel@tonic-gate * by tbl. 25*7c478bd9Sstevel@tonic-gate * Arguments: 26*7c478bd9Sstevel@tonic-gate * tbl (ss_request_table *) 27*7c478bd9Sstevel@tonic-gate * pointer to request table 28*7c478bd9Sstevel@tonic-gate * idx (int) 29*7c478bd9Sstevel@tonic-gate * index into table 30*7c478bd9Sstevel@tonic-gate * Returns: 31*7c478bd9Sstevel@tonic-gate * (ss_request_entry *) 32*7c478bd9Sstevel@tonic-gate * pointer to request table entry 33*7c478bd9Sstevel@tonic-gate * Notes: 34*7c478bd9Sstevel@tonic-gate * Has been replaced by a macro. 35*7c478bd9Sstevel@tonic-gate */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #ifdef __SABER__ 38*7c478bd9Sstevel@tonic-gate /* sigh. saber won't deal with pointer-to-const-struct */ 39*7c478bd9Sstevel@tonic-gate static struct _ss_request_entry * get_request (tbl, idx) 40*7c478bd9Sstevel@tonic-gate ss_request_table * tbl; 41*7c478bd9Sstevel@tonic-gate int idx; 42*7c478bd9Sstevel@tonic-gate { 43*7c478bd9Sstevel@tonic-gate struct _ss_request_table *tbl1 = (struct _ss_request_table *) tbl; 44*7c478bd9Sstevel@tonic-gate struct _ss_request_entry *e = (struct _ss_request_entry *) tbl1->requests; 45*7c478bd9Sstevel@tonic-gate return e + idx; 46*7c478bd9Sstevel@tonic-gate } 47*7c478bd9Sstevel@tonic-gate #else 48*7c478bd9Sstevel@tonic-gate #define get_request(tbl,idx) ((tbl) -> requests + (idx)) 49*7c478bd9Sstevel@tonic-gate #endif 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate /* 52*7c478bd9Sstevel@tonic-gate * check_request_table(rqtbl, argc, argv, sci_idx) 53*7c478bd9Sstevel@tonic-gate * 54*7c478bd9Sstevel@tonic-gate * Function: 55*7c478bd9Sstevel@tonic-gate * If the command string in argv[0] is in the request table, execute 56*7c478bd9Sstevel@tonic-gate * the commands and return error code 0. Otherwise, return error 57*7c478bd9Sstevel@tonic-gate * code ss_et_command_not_found. 58*7c478bd9Sstevel@tonic-gate * Arguments: 59*7c478bd9Sstevel@tonic-gate * rqtbl (ss_request_table *) 60*7c478bd9Sstevel@tonic-gate * pointer to request table 61*7c478bd9Sstevel@tonic-gate * argc (int) 62*7c478bd9Sstevel@tonic-gate * number of elements in argv[] 63*7c478bd9Sstevel@tonic-gate * argv (char *[]) 64*7c478bd9Sstevel@tonic-gate * argument string array 65*7c478bd9Sstevel@tonic-gate * sci_idx (int) 66*7c478bd9Sstevel@tonic-gate * ss-internal index for subsystem control info structure 67*7c478bd9Sstevel@tonic-gate * Returns: 68*7c478bd9Sstevel@tonic-gate * (int) 69*7c478bd9Sstevel@tonic-gate * zero if command found, ss_et_command_not_found otherwise 70*7c478bd9Sstevel@tonic-gate * Notes: 71*7c478bd9Sstevel@tonic-gate */ 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate static int check_request_table (rqtbl, argc, argv, sci_idx) 74*7c478bd9Sstevel@tonic-gate register ss_request_table *rqtbl; 75*7c478bd9Sstevel@tonic-gate int argc; 76*7c478bd9Sstevel@tonic-gate char *argv[]; 77*7c478bd9Sstevel@tonic-gate int sci_idx; 78*7c478bd9Sstevel@tonic-gate { 79*7c478bd9Sstevel@tonic-gate #ifdef __SABER__ 80*7c478bd9Sstevel@tonic-gate struct _ss_request_entry *request; 81*7c478bd9Sstevel@tonic-gate #else 82*7c478bd9Sstevel@tonic-gate register ss_request_entry *request; 83*7c478bd9Sstevel@tonic-gate #endif 84*7c478bd9Sstevel@tonic-gate register ss_data *info; 85*7c478bd9Sstevel@tonic-gate register char const * const * name; 86*7c478bd9Sstevel@tonic-gate char *string = argv[0]; 87*7c478bd9Sstevel@tonic-gate int i; 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate info = ss_info(sci_idx); 90*7c478bd9Sstevel@tonic-gate info->argc = argc; 91*7c478bd9Sstevel@tonic-gate info->argv = argv; 92*7c478bd9Sstevel@tonic-gate for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) { 93*7c478bd9Sstevel@tonic-gate for (name = request->command_names; *name; name++) 94*7c478bd9Sstevel@tonic-gate if (!strcmp(*name, string)) { 95*7c478bd9Sstevel@tonic-gate info->current_request = request->command_names[0]; 96*7c478bd9Sstevel@tonic-gate (request->function)(argc, (const char *const *) argv, 97*7c478bd9Sstevel@tonic-gate sci_idx,info->info_ptr); 98*7c478bd9Sstevel@tonic-gate info->current_request = (char *)NULL; 99*7c478bd9Sstevel@tonic-gate return(0); 100*7c478bd9Sstevel@tonic-gate } 101*7c478bd9Sstevel@tonic-gate } 102*7c478bd9Sstevel@tonic-gate return(SS_ET_COMMAND_NOT_FOUND); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate /* 106*7c478bd9Sstevel@tonic-gate * really_execute_command(sci_idx, argc, argv) 107*7c478bd9Sstevel@tonic-gate * 108*7c478bd9Sstevel@tonic-gate * Function: 109*7c478bd9Sstevel@tonic-gate * Fills in the argc, argv values in the subsystem entry and 110*7c478bd9Sstevel@tonic-gate * call the appropriate routine. 111*7c478bd9Sstevel@tonic-gate * Arguments: 112*7c478bd9Sstevel@tonic-gate * sci_idx (int) 113*7c478bd9Sstevel@tonic-gate * ss-internal index for subsystem control info structure 114*7c478bd9Sstevel@tonic-gate * argc (int) 115*7c478bd9Sstevel@tonic-gate * number of arguments in argument list 116*7c478bd9Sstevel@tonic-gate * argv (char **[]) 117*7c478bd9Sstevel@tonic-gate * pointer to parsed argument list (may be reallocated 118*7c478bd9Sstevel@tonic-gate * on abbrev expansion) 119*7c478bd9Sstevel@tonic-gate * 120*7c478bd9Sstevel@tonic-gate * Returns: 121*7c478bd9Sstevel@tonic-gate * (int) 122*7c478bd9Sstevel@tonic-gate * Zero if successful, ss_et_command_not_found otherwise. 123*7c478bd9Sstevel@tonic-gate * Notes: 124*7c478bd9Sstevel@tonic-gate */ 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate static int really_execute_command (sci_idx, argc, argv) 127*7c478bd9Sstevel@tonic-gate int sci_idx; 128*7c478bd9Sstevel@tonic-gate int argc; 129*7c478bd9Sstevel@tonic-gate char **argv[]; 130*7c478bd9Sstevel@tonic-gate { 131*7c478bd9Sstevel@tonic-gate register ss_request_table **rqtbl; 132*7c478bd9Sstevel@tonic-gate register ss_data *info; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate info = ss_info(sci_idx); 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate for (rqtbl = info->rqt_tables; *rqtbl; rqtbl++) { 137*7c478bd9Sstevel@tonic-gate if (check_request_table (*rqtbl, argc, *argv, sci_idx) == 0) 138*7c478bd9Sstevel@tonic-gate return(0); 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate return(SS_ET_COMMAND_NOT_FOUND); 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* 144*7c478bd9Sstevel@tonic-gate * ss_execute_command(sci_idx, argv) 145*7c478bd9Sstevel@tonic-gate * 146*7c478bd9Sstevel@tonic-gate * Function: 147*7c478bd9Sstevel@tonic-gate * Executes a parsed command list within the subsystem. 148*7c478bd9Sstevel@tonic-gate * Arguments: 149*7c478bd9Sstevel@tonic-gate * sci_idx (int) 150*7c478bd9Sstevel@tonic-gate * ss-internal index for subsystem control info structure 151*7c478bd9Sstevel@tonic-gate * argv (char *[]) 152*7c478bd9Sstevel@tonic-gate * parsed argument list 153*7c478bd9Sstevel@tonic-gate * Returns: 154*7c478bd9Sstevel@tonic-gate * (int) 155*7c478bd9Sstevel@tonic-gate * Zero if successful, ss_et_command_not_found otherwise. 156*7c478bd9Sstevel@tonic-gate * Notes: 157*7c478bd9Sstevel@tonic-gate */ 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate int 160*7c478bd9Sstevel@tonic-gate ss_execute_command(sci_idx, argv) 161*7c478bd9Sstevel@tonic-gate int sci_idx; 162*7c478bd9Sstevel@tonic-gate register char *argv[]; 163*7c478bd9Sstevel@tonic-gate { 164*7c478bd9Sstevel@tonic-gate register int i, argc; 165*7c478bd9Sstevel@tonic-gate char **argp; 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate argc = 0; 168*7c478bd9Sstevel@tonic-gate for (argp = argv; *argp; argp++) 169*7c478bd9Sstevel@tonic-gate argc++; 170*7c478bd9Sstevel@tonic-gate argp = (char **)malloc((argc+1)*sizeof(char *)); 171*7c478bd9Sstevel@tonic-gate for (i = 0; i <= argc; i++) 172*7c478bd9Sstevel@tonic-gate argp[i] = argv[i]; 173*7c478bd9Sstevel@tonic-gate i = really_execute_command(sci_idx, argc, &argp); 174*7c478bd9Sstevel@tonic-gate free(argp); 175*7c478bd9Sstevel@tonic-gate return(i); 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate /* 179*7c478bd9Sstevel@tonic-gate * ss_execute_line(sci_idx, line_ptr) 180*7c478bd9Sstevel@tonic-gate * 181*7c478bd9Sstevel@tonic-gate * Function: 182*7c478bd9Sstevel@tonic-gate * Parses and executes a command line within a subsystem. 183*7c478bd9Sstevel@tonic-gate * Arguments: 184*7c478bd9Sstevel@tonic-gate * sci_idx (int) 185*7c478bd9Sstevel@tonic-gate * ss-internal index for subsystem control info structure 186*7c478bd9Sstevel@tonic-gate * line_ptr (char *) 187*7c478bd9Sstevel@tonic-gate * Pointer to command line to be parsed. 188*7c478bd9Sstevel@tonic-gate * Returns: 189*7c478bd9Sstevel@tonic-gate * (int) 190*7c478bd9Sstevel@tonic-gate * Error code. 191*7c478bd9Sstevel@tonic-gate * Notes: 192*7c478bd9Sstevel@tonic-gate */ 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate int ss_execute_line (sci_idx, line_ptr) 195*7c478bd9Sstevel@tonic-gate int sci_idx; 196*7c478bd9Sstevel@tonic-gate char *line_ptr; 197*7c478bd9Sstevel@tonic-gate { 198*7c478bd9Sstevel@tonic-gate char **argv; 199*7c478bd9Sstevel@tonic-gate int argc; 200*7c478bd9Sstevel@tonic-gate int rc; 201*7c478bd9Sstevel@tonic-gate 202*7c478bd9Sstevel@tonic-gate /* flush leading whitespace */ 203*7c478bd9Sstevel@tonic-gate while (line_ptr[0] == ' ' || line_ptr[0] == '\t') 204*7c478bd9Sstevel@tonic-gate line_ptr++; 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate /* check if it should be sent to operating system for execution */ 207*7c478bd9Sstevel@tonic-gate if (*line_ptr == '!') { 208*7c478bd9Sstevel@tonic-gate if (ss_info(sci_idx)->flags.escape_disabled) 209*7c478bd9Sstevel@tonic-gate return SS_ET_ESCAPE_DISABLED; 210*7c478bd9Sstevel@tonic-gate else { 211*7c478bd9Sstevel@tonic-gate line_ptr++; 212*7c478bd9Sstevel@tonic-gate system(line_ptr); 213*7c478bd9Sstevel@tonic-gate return 0; 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate /* parse it */ 218*7c478bd9Sstevel@tonic-gate argv = ss_parse(sci_idx, line_ptr, &argc); 219*7c478bd9Sstevel@tonic-gate if (argc == 0) 220*7c478bd9Sstevel@tonic-gate return 0; 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate /* look it up in the request tables, execute if found */ 223*7c478bd9Sstevel@tonic-gate rc = really_execute_command (sci_idx, argc, &argv); 224*7c478bd9Sstevel@tonic-gate 225*7c478bd9Sstevel@tonic-gate free(argv); 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate return (rc); 228*7c478bd9Sstevel@tonic-gate } 229