int main(void){ struct token token = {0}; struct command command = {0}; boolean end = false; pid_t shell_pgid; int terminal; void *handle; terminal = STDIN_FILENO; while(tcgetpgrp(terminal) != (shell_pgid = getpgrp())){ kill(- shell_pgid,SIGTTIN); } shell_pgid = getpid(); if(setpgid(shell_pgid,shell_pgid) < 0){ printf("Erro"); exit(1); } tcsetpgrp(terminal,shell_pgid); terminateCommand ( & command ); signal( SIGCHLD , terminateHandler ); stopCommand( & command ); signal ( SIGTSTP , stopHandler ); handle = dlopen("./libparser.so", RTLD_LAZY); do{ ((void(*)(struct token*))(dlsym(handle,"getTokens")))(&token); ((void(*)(struct token*))(dlsym(handle,"identifyTokens")))(&token); if(!((boolean(*)(struct token*))(dlsym(handle,"checkSyntax")))(&token)) continue; if(ECHO_TOKENS) ((void(*)(struct token*))(dlsym(handle,"echoTokens")))(&token); if(ECHO_INPUT) ((void(*)(struct token*))(dlsym(handle,"echoInput")))(&token); while(((boolean(*)(struct token*,struct command*))(dlsym(handle,"getCommand")))(&token, &command)){ if(ECHO_COMMAND) ((void(*)(struct command))(dlsym(handle,"echoCommand")))(*(command.next)); end = executeCommand(command.next); } } while(!end); ((void(*)(struct command*))(dlsym(handle,"clearCommand")))(&command); ((void(*)(struct token*))(dlsym(handle,"clearToken")))(&token); dlclose(handle); return 0; }
void terminateHandler(int signalNumber){ terminateCommand(NULL); }
int commonInterface::runCommand(string command) { char commandStr[MAX_COMMAND_LEN]; memset(commandStr,0, MAX_COMMAND_LEN); sscanf(command.c_str(), "%s", commandStr); if(strlen(commandStr)<=0) return ERROR; string upperCaseCommand = commandStr; for(int i=0;i<upperCaseCommand.length();i++) { upperCaseCommand[i] = toupper(upperCaseCommand[i]); } if(strcmp(upperCaseCommand.c_str(), "CREATOR") == 0) { mWindow->displayCreator(); } else if(strcmp(upperCaseCommand.c_str(), "HELP") == 0) { mWindow->displayHelpMenu(); } else if(strcmp(upperCaseCommand.c_str(), "MYIP") == 0) { mWindow->displayMyIP(mMyIP); } else if(strcmp(upperCaseCommand.c_str(), "MYPORT") == 0) { mWindow->displayMyPort(mPort); } else if(strcmp(upperCaseCommand.c_str(), "REGISTER") == 0) { if(mMode == SERVER) { cout<<"\n Register command not available in server mode"<<endl; return true; } registerCommand(command); } else if(strcmp(upperCaseCommand.c_str(), "CONNECT") == 0) { connectCommand(command); return 1; } else if(strcmp(upperCaseCommand.c_str(), "LIST") == 0) { listCommand(); } else if(strcmp(upperCaseCommand.c_str(), "TERMINATE") == 0) { terminateCommand(command); } else if(strcmp(upperCaseCommand.c_str(), "EXIT") == 0) { exitCommand(); return EXIT_PROGRAM; } else if(strcmp(upperCaseCommand.c_str(), "UPLOAD") == 0) { uploadCommand(command); } else if(strcmp(upperCaseCommand.c_str(), "DOWNLOAD") == 0) { downloadCommand(command); } else if(strcmp(upperCaseCommand.c_str(), "STATISTICS") == 0) { statCommand(); } else { //Wrong command enter cout<<"\n Command not found. Please note the commands are case sensetive\n"; return ERROR; } }