Path: blob/main/crypto/krb5/src/util/ss/execute_cmd.c
34907 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 1987, 1988, 1989 by Massachusetts Institute of Technology3*4* For copyright info, see copyright.h.5*/67#include "ss_internal.h"8#include "copyright.h"9#include <stdio.h>101112/*13* get_request(tbl, idx)14*15* Function:16* Gets the idx'th request from the request table pointed to17* by tbl.18* Arguments:19* tbl (ss_request_table *)20* pointer to request table21* idx (int)22* index into table23* Returns:24* (ss_request_entry *)25* pointer to request table entry26* Notes:27* Has been replaced by a macro.28*/2930#define get_request(tbl,idx) ((tbl) -> requests + (idx))3132/*33* check_request_table(rqtbl, argc, argv, sci_idx)34*35* Function:36* If the command string in argv[0] is in the request table, execute37* the commands and return error code 0. Otherwise, return error38* code ss_et_command_not_found.39* Arguments:40* rqtbl (ss_request_table *)41* pointer to request table42* argc (int)43* number of elements in argv[]44* argv (char *[])45* argument string array46* sci_idx (int)47* ss-internal index for subsystem control info structure48* Returns:49* (int)50* zero if command found, ss_et_command_not_found otherwise51* Notes:52*/5354static int55check_request_table(ss_request_table *rqtbl, int argc, char *argv[],56int sci_idx)57{58ss_request_entry *request;59ss_data *info;60char const *const *name;61char *string = argv[0];62int i;6364info = ss_info(sci_idx);65info->argc = argc;66info->argv = argv;67for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) {68for (name = request->command_names; *name; name++)69if (!strcmp(*name, string)) {70info->current_request = request->command_names[0];71(request->function)(argc, (const char *const *) argv,72sci_idx,info->info_ptr);73info->current_request = (char *)NULL;74return(0);75}76}77return(SS_ET_COMMAND_NOT_FOUND);78}7980/*81* really_execute_command(sci_idx, argc, argv)82*83* Function:84* Fills in the argc, argv values in the subsystem entry and85* call the appropriate routine.86* Arguments:87* sci_idx (int)88* ss-internal index for subsystem control info structure89* argc (int)90* number of arguments in argument list91* argv (char **[])92* pointer to parsed argument list (may be reallocated93* on abbrev expansion)94*95* Returns:96* (int)97* Zero if successful, ss_et_command_not_found otherwise.98* Notes:99*/100101static int102really_execute_command(int sci_idx, int argc, char **argv[])103{104ss_request_table **rqtbl;105ss_data *info;106107info = ss_info(sci_idx);108109for (rqtbl = info->rqt_tables; *rqtbl; rqtbl++) {110if (check_request_table (*rqtbl, argc, *argv, sci_idx) == 0)111return(0);112}113return(SS_ET_COMMAND_NOT_FOUND);114}115116/*117* ss_execute_command(sci_idx, argv)118*119* Function:120* Executes a parsed command list within the subsystem.121* Arguments:122* sci_idx (int)123* ss-internal index for subsystem control info structure124* argv (char *[])125* parsed argument list126* Returns:127* (int)128* Zero if successful, ss_et_command_not_found otherwise.129* Notes:130*/131132int133ss_execute_command(int sci_idx, char *argv[])134{135unsigned int i, argc;136char **argp;137int ret;138139argc = 0;140for (argp = argv; *argp; argp++)141argc++;142argp = (char **)malloc((argc+1)*sizeof(char *));143if (argp == NULL)144return(ENOMEM);145for (i = 0; i <= argc; i++)146argp[i] = argv[i];147ret = really_execute_command(sci_idx, argc, &argp);148free(argp);149return(ret);150}151152/*153* ss_execute_line(sci_idx, line_ptr)154*155* Function:156* Parses and executes a command line within a subsystem.157* Arguments:158* sci_idx (int)159* ss-internal index for subsystem control info structure160* line_ptr (char *)161* Pointer to command line to be parsed.162* Returns:163* (int)164* Error code.165* Notes:166*/167168int169ss_execute_line(int sci_idx, char *line_ptr)170{171char **argv;172int argc, ret;173174/* flush leading whitespace */175while (line_ptr[0] == ' ' || line_ptr[0] == '\t')176line_ptr++;177178/* check if it should be sent to operating system for execution */179if (*line_ptr == '!') {180if (ss_info(sci_idx)->flags.escape_disabled)181return SS_ET_ESCAPE_DISABLED;182else {183line_ptr++;184system(line_ptr);185return 0;186}187}188189/* parse it */190argv = ss_parse(sci_idx, line_ptr, &argc);191if (argc == 0)192return 0;193194/* look it up in the request tables, execute if found */195ret = really_execute_command (sci_idx, argc, &argv);196197free(argv);198199return(ret);200}201202203