void commands_loop() { char input[256]; int index = 0; while (1) { char key = keyboard_read(); console_putchar(key); if (key == 13 || key == 10) { // execute command input[index] = '\0'; commands_run(input); memset(input, 0, 256); index = 0; } else { input[index] = key; index++; } } }
void dsi_command(globus_gfs_operation_t Operation, globus_gfs_command_info_t * CommandInfo, void * UserArg) { commands_run(Operation, CommandInfo, UserArg, globus_gridftp_server_finished_command); }
/** * @short Runs a specific subcommand, NOT replacing current process. */ int commands_run_no_exec(const char *subcommand, int argc, char **argv){ subcommand_t *command=subcommand_find(subcommand); if (!command){ fprintf(stderr, "Invalid command: %s\n", subcommand); return -1; } if (command->type==SC_EXTERNAL){ // If external, will do execv, do fork here pid_t pid; if ( (pid=fork()) == 0){ commands_run(subcommand, argc, argv); exit(1); // If got here, command was not run properly. } int status=0; waitpid(pid,&status,0); if (!WIFEXITED(status)){ fprintf(stderr, "Error executing command %s.\n", subcommand); } return WEXITSTATUS(status); } else{ // If not, jsut run. return commands_run(subcommand, argc, argv); } }
int main(int argc, char **argv){ if (argc==2 && strcmp(argv[1],"--one-line-help")==0){ printf("Executes a restricted shell\n"); return 0; } #ifdef DEFAULT_COMMANDS_PATH setenv(COMMANDS_PATH, DEFAULT_COMMANDS_PATH, 1); #else setenv(COMMANDS_PATH, getenv("PATH"), 1); #endif // Must be set at COMMANDS_NAME envvar. commands_name=getenv("COMMANDS_NAME"); if (!commands_name){ char *tmp; #ifdef COMMAND_NAME tmp=strdup(COMMAND_NAME); #else tmp=strdup(argv[0]); #endif commands_name=strdup(basename(tmp)); free(tmp); // Use first part of my own name char *dash=strchr(commands_name,'-'); if (dash) *dash=0; } else commands_name=strdup(commands_name); commands_name_length=strlen(commands_name); commands_config_parse(); update_allowed_commands(); if (argc>1){ // May set one command as arguments. commands_run(argv[1], argc-1, argv+1); exit(0); } int running=1; char line[2048]; memset(line, 0, sizeof(line)); char *margv[256]; unsigned long n=1; while(running){ printf("%s:%ld> ", commands_name, n++); fflush(stdout); memset(line, 0, sizeof(line)); if ( !fgets(line, sizeof(line), stdin) ){ running=0; break; } if (!strchr(line,'\0')){ fprintf(stderr,"Statement too long. Ignoring."); } else{ char *I=line, *endI=line+strnlen(line, sizeof(line))-1; *(endI)=0; int margc=1; margv[0]=I; for(;I!=endI && *I;++I){ if (isspace(*I)){ *I++=0; while (isspace(*I)) ++I; margv[margc++]=I; } } margv[margc]=NULL; if (strcmp(margv[0],"exit")==0){ printf("Bye.\n"); running=0; } else commands_run_no_exec(margv[0], margc, margv); } } printf("\n"); subcommand_list_free(); free(commands_name); return 0; }