void CvrpFileReader::processNextLine() { QString l_line = m_file->readLine(); if (l_line.isNull()) { // qDebug() << "END OF FILE reached."; return; } QStringList l_wordList = splitLine(l_line); removeWhiteSpaces(l_wordList); if (l_wordList.isEmpty()) { // qDebug() << "Skipped empty line"; return; } processLine(l_wordList, l_line); }
int runPath(char *consoleinput) { if(strlen(consoleinput) == 4) //path without any args { if(pathlist!=NULL) { printList(pathlist); } else { printf("[path variable not set]"); } } else //path with args { char *args = strndup(consoleinput+4, strlen(consoleinput)-4); //remove "path" from input removeWhiteSpaces(args); char *newpath = strndup(args+1, strlen(args)-1); //remove + or - from input removeWhiteSpaces(newpath); if(args[0]=='+') { //add to linkedlist printf("\nAdding new path:[%s]\n", newpath); pathlist = addNode(pathlist, newpath); printList(pathlist); return 0; } else if(args[0]=='-') { //remove from linkedlist printf("\nDeleting path:[%s]\n", newpath); pathlist = deleteNode(pathlist, newpath); printList(pathlist); return 0; } else { free(args); free(newpath); return 22; } free(newpath); free(args); } return 12; }
int main(int argc, char **argv) { while(1) //shell runs till user enters exit { char * cwd = malloc(256); //find current working directory for prompt assert(cwd != NULL); getCurrentDir(cwd); printf("\nMyShell:%s$ ", cwd); //print shell prompt free(cwd); char *consoleinput = malloc(256); assert(consoleinput != NULL); //TO DO: Change buffer size to variable readCommand(consoleinput); //accept input removeNewLineChar(consoleinput); //remove '\n' character from input removeWhiteSpaces(consoleinput); //remove all spaces before and after command if(strncmp(consoleinput, SUPPORTED_COMMANDS[1], 4) == 0) //exit { free(consoleinput); printf("\nMyShell Terminated\n"); return 0; } else if(strncmp(consoleinput, SUPPORTED_COMMANDS[0], 2) == 0) //cd { int result = runChangeDir(consoleinput); if(result!=0) { perror("cd error:"); } } else if(strncmp(consoleinput, SUPPORTED_COMMANDS[2], 4) == 0) //path { int result = runPath(consoleinput); if(result!=0) { errno = result; perror("path error:"); } } else { //handle pipes char **commands = parseArgv(consoleinput, SPECIAL_CHARS[4]); //input destoyed int numcommands = 0; while( commands[numcommands]!=NULL ) { numcommands++; } //printf("\nNumber of commands:[%d]", numcommands); const int numpipes = 2*(numcommands-1); //printf("\nNumber of pipe file descriptors:[%d]", numpipes); /*read and write ends of pipes stay apart by 3 -increment open pipe indexes by 2 after every command -close all pipes */ int pipefds[numpipes]; int i=0; for(i=0; i<numpipes;i=i+2) { pipe(pipefds+i); } //printf("\npipe() call successful"); // for(i=0;i<numpipes;i++) // { // printf("[%d]", pipefds[i]); // } int pipe_w = 1; int pipe_r = pipe_w - 3; int curcommand = 0; while(curcommand < numcommands) { //printf("\nCommand number:[%d]", curcommand); //printf("\ninside pipe loop for command [%s]", commands[curcommand]); //Parse Command and Arguments into formatneeded by execv char **argv = parseArgv(commands[curcommand], SPECIAL_CHARS[0]); //printf("\nCurrent Command:[%s]", argv[0]); if(findPath(argv) == 0) { //executeCommand(argv); int child_pid = fork(); //int child_status; if(child_pid < 0) { //errno = 3; perror("fork error:"); } else if(child_pid == 0) //fork success { if(pipe_w < numpipes) { //open write end //printf("\nWrite pipe:[%d] to stdout", pipefds[pipe_w]); if(dup2(pipefds[pipe_w], 1) < 0) { perror("pipe write-end error: "); } } if((pipe_r >= 0)&&(pipe_r < numpipes)) { //open read end //printf("\nRead pipe:[%d] to stdin", pipefds[pipe_r]); if(dup2(pipefds[pipe_r], 0) < 0) { perror("pipe read-end error: "); } } for(i=0;i<numpipes;i++) //close off all pipes { //printf("\nclosing all pipes"); close(pipefds[i]); } if(execv(argv[0], argv) == -1) { perror("Bad command or filename:"); exit(0); //TODO: child hangs here } //fflush(stdin); } } else { printf("\nBad command or filename"); //TODO: ForkBomb occuring here //exit(0); } free(argv); //printf("\nIncrementing pipe ends, moving to next command."); curcommand++; pipe_w = pipe_w + 2; pipe_r = pipe_r + 2; } //int i=0; for(i=0;i<numpipes;i++) //close off all pipes { //printf("\nclosing all pipes"); close(pipefds[i]); } int status; for(i=0;i<numcommands;i++) { wait(&status); } free(commands); } free(consoleinput); } freeList(pathlist); }