static void hostapd_cli_edit_cmd_cb(void *ctx, char *cmd) { char *argv[max_args]; int argc; argc = tokenize_cmd(cmd, argv); if (argc) wpa_request(ctrl_conn, argc, argv); }
int main(void){ char cmd[MAX_CMD_LENGTH]=""; //Command entered by the user including arguments,pipes,redirections,etc. char *tokens[MAX_CMD_LENGTH]={NULL}; //Stores the parsed command(Eg: token[0]=ls, token[1]=-l) char *commands[PATH_MAX]={NULL}; //Pathname of the actual commands. Commands may be more than one due to piping. char *target_cmd; int cpid,child_err_code; //Initialize PS1 content init_ps1(); while(1){ printf("%s",PS1); //Display prompt(PS1) if(!fgets(cmd,MAX_CMD_LENGTH,stdin)){ //Get input perror("fgets"); exit(EXIT_FAILURE); } if(strcmp(cmd,"\n") == 0) //If Empty command then new prompt again continue; cmd[strlen(cmd)-1] = '\0'; //Replace '\n' with a '\0' if(strcmp(cmd,"exit") == 0) //Exit code exit(0); //Tokenize the command if(tokenize_cmd(cmd,tokens) == -1){ fprintf(stderr,"Shell Error: Too may tokens in command.\n"); continue; } //Extract command names if(parse_cmd(tokens,commands) == -1){ fprintf(stderr,"Shell Error: Unable to parse command.\n"); continue; } target_cmd = search_cmd(commands[0]); //TODO....Modify this code for every commands //This is the core function of a shell. Rest are features. //Execute command by invoking the respective program for it using exec() //TODO.... cpid = fork(); if(cpid == -1){ perror("fork"); exit(EXIT_FAILURE); } if(cpid == 0){ //Child process if(execv(target_cmd,tokens) == -1){ //TODO...Extract command pathname and argument list perror("execv"); exit(EXIT_FAILURE); //Child exits if failed } } wait(&child_err_code); } return 0; }
static void wlantest_cli_edit_cmd_cb(void *ctx, char *cmd) { struct wlantest_cli *cli = ctx; char *argv[max_args]; int argc; argc = tokenize_cmd(cmd, argv); if (argc) { int ret = ctrl_command(cli->s, argc, argv); if (ret < 0) printf("FAIL\n"); } }