void LineParser::init( char* pszLine ) { DEBUG(("LineParser::init(%s)\n", pszLine)); clearTokens(); arrTokens = NULL; iNumberOfTokens = 0; iCurrentToken = 0; parse( pszLine ); }
uint16 Script::readLineToken(bool errorOnEOF) { char buf[200]; char *line = readLine(buf, 200); if (!line) { if (errorOnEOF) error("unexpected end of file while parsing"); else return 0; } clearTokens(); while (*line && _numTokens < MAX_TOKENS) { line = parseNextToken(line, _tokens[_numTokens], MAX_TOKEN_LEN, " "); line = Common::ltrim(line); _numTokens++; } return _numTokens; }
void LineParser::addToken(char* pszToken) { DEBUG(("LineParser::addToken(%s)\n", pszToken)); char **newarrTokens = new char*[ ++iNumberOfTokens ]; // check if the array could be allocated if (NULL == newarrTokens) return; // copy old content if (NULL != arrTokens) { memcpy(newarrTokens, arrTokens, sizeof( char* ) * (iNumberOfTokens - 1) ); clearTokens(); } arrTokens = newarrTokens; arrTokens[ iNumberOfTokens - 1] = pszToken; }
int main(int argc, char *argv[]) { // Input buffer and and commands char buffer[BUFFER_LEN] = { 0 }; char command[BUFFER_LEN] = { 0 }; char arg[BUFFER_LEN] = { 0 }; char fileN [256] = {0}; bool enterManual = true; // variable used to check pass bool check =false; // current working directory char cwd[PATH_MAX + 1]; // Get the argurment for the file. tokenize(argv[1]); // Checking through the files for the bat file check = foldercheck(tokens[0]); // copies the file into a tmp variable before we clear tokens strcpy(fileN, tokens[0]); clearTokens(); // If the file was found if(check) { // Load the file FILE *f; f=fopen(fileN, "r"); //sets it so you do not go into manual entry afterwords enterManual = false; if (getcwd(cwd, PATH_MAX + 1) != NULL) { printf("%s$ ", cwd); } // Enters a loop to get the inputs from the file while(fgets(buffer, BUFFER_LEN,f) != NULL) { // tokenizes the buffer tokenize(buffer); strcpy(command, tokens[0]); //Checks for commands if(strcmp(command, "dir") ==0) { directory(); } else if (strcmp(command, "cd") == 0) { changedir(tokens[1]); } else if(strcmp(command, "clr") ==0) { clear(); } else if(strcmp(command, "echo") == 0) { echo(tokens,i); } else if(strcmp(command, "pause") == 0) { pausing(); } else if(strcmp(command, "environ")==0) { environ(); } else if(strcmp(command, "help") ==0) { help(tokens[1]); } // quit command -- exit the shell else if (strcmp(command, "quit") == 0) { return EXIT_SUCCESS; } // If it is not a valid command checks if they are trying to start a program else { // Check if there is a file in the program check = false; check = foldercheck(command); // if program name is found if(check) { // create first part of program string char begin[250] = {"./"}; strcat (begin,tokens[0]); int count = 0; check = true; // Loops 7 times to get the max of seven parameter for(int i = 1; i < 7; i++) { // makes it so no blanks go into the checks if(strcmp(tokens[i]," ") != 0) { // checks for write symbol if(strcmp(tokens[i], ">") == 0) { // gets next token and sets it to write i++; freopen(tokens[i],"w",stdout); } // cbecks for append symbol else if (strcmp(tokens[i], ">>") == 0) { // gets next token and sets it to append i++; freopen(tokens[i],"a",stdout); } // checks for read input else if (strcmp(tokens[i], "<") == 0) { // gets next input for file name and checks if it is a file i++; if(foldercheck(tokens[i])) { // reads from file freopen(tokens[i],"r",stdin); }else { // not found sets it to terminal and writes i = 7; freopen("/dev/tty", "w", stdout); printf("Invalid file read entry \n"); check = false; } } // checks if two args have already pass else if(count < 2) { // appends the argument to the program call count++; char append [256] = {" "}; strcat (append,tokens[i]); strcat (begin, append); } // Invalid call has been passed else { freopen("/dev/tty", "w", stdout); printf("Invalid file program entry type \n"); check = false; i = 7; } } } // Runs if valid program call has been called if(check) { system(begin); } freopen("/dev/tty", "w", stdout); freopen("/dev/tty", "r", stdin); }// Command does not exist else { printf("Unsupported command, use help to display the manual. Please enter Manualy. %s \n", command); clearTokens(); enterManual = true; break; } } clearTokens(); // Fetches cwd and prints it out before input if (getcwd(cwd, PATH_MAX + 1) != NULL) { printf("%s$ ", cwd); } } } // Checks if the user has to enter manually for the shell if(enterManual) { if (getcwd(cwd, PATH_MAX + 1) != NULL) { printf("%s$ ", cwd); } while (fgets(buffer, BUFFER_LEN, stdin) != NULL) { // tokenizes the buffer tokenize(buffer); strcpy(command, tokens[0]); //Checks for commands if(strcmp(command, "dir") ==0) { directory(); } else if (strcmp(command, "cd") == 0) { changedir(tokens[1]); } else if(strcmp(command, "clr") ==0) { clear(); } else if(strcmp(command, "echo") == 0) { echo(tokens,i); } else if(strcmp(command, "pause") == 0) { pausing(); } else if(strcmp(command, "environ")==0) { environ(); } else if(strcmp(command, "help") ==0) { help(tokens[1]); } // quit command -- exit the shell else if (strcmp(command, "quit") == 0) { return EXIT_SUCCESS; } // If it is not a valid command checks if they are trying to start a program else { // Check if there is a file in the program check = false; check = foldercheck(command); // if the program is found if(check) { // create first part of program string char begin[250] = {"./"}; strcat (begin,tokens[0]); int count = 0; check = true; // Loops 7 times to get the max of seven parameter for(int i = 1; i < 7; i++) { // makes it so no blanks go into the checks if(strcmp(tokens[i]," ") != 0) { // checks for write symbol if(strcmp(tokens[i], ">") == 0) { // gets next token and sets it to write i++; freopen(tokens[i],"w",stdout); } // cbecks for append symbol else if (strcmp(tokens[i], ">>") == 0) { // gets next token and sets it to append i++; freopen(tokens[i],"a",stdout); } // checks for read input else if (strcmp(tokens[i], "<") == 0) { // gets next input for file name and checks if it is a file i++; if(foldercheck(tokens[i])) { // reads from file freopen(tokens[i],"r",stdin); }else { // not found sets it to terminal and writes i = 7; freopen("/dev/tty", "w", stdout); printf("Invalid file read entry \n"); check = false; } } // checks if two args have already pass else if(count < 2) { // appends the argument to the program call count++; char append [256] = {" "}; strcat (append,tokens[i]); strcat (begin, append); } // Invalid call has been passed else { freopen("/dev/tty", "w", stdout); printf("Invalid file program entry type \n"); check = false; i = 7; } } } // Runs if valid program call has been called if(check) { system(begin); } freopen("/dev/tty", "w", stdout); freopen("/dev/tty", "r", stdin); } else { printf("Unsupported command, use help to display the manual.%s \n", command); } } clearTokens(); // Fetches cwd and prints it out before input if (getcwd(cwd, PATH_MAX + 1) != NULL) { printf("%s$ ", cwd); } } } return EXIT_SUCCESS; }
Scanner::~Scanner() { clearTokens(); file.close(); }
LineParser::~LineParser() { DEBUG(("LineParser::~LineParser(%s)")); clearTokens(); }