Permalink
Browse files

Fix batch mode and extend batch test file

  • Loading branch information...
1 parent 17d57de commit 5882da0623f1bc7227208517b1b25000dabe81fb @koblenski committed Nov 29, 2016
Showing with 245 additions and 37 deletions.
  1. +6 −1 batch.txt
  2. +194 −0 golden_output.txt
  3. +45 −36 shell.cpp
View
@@ -1,3 +1,8 @@
/bin/ls -a
-/usr/bin/gvim
+cat batch.txt
+this is an invalid command
+
+
+echo 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
+exit invalid
exit
View
@@ -0,0 +1,194 @@
+.
+..
+batch.txt
+.batch.txt.swp
+.git
+.gitignore
+golden_output.txt
+shell
+shell.cpp
+/bin/ls -a
+cat batch.txt
+this is an invalid command
+
+
+echo 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
+exit invalid
+exit
+koblensk> Command Line : /bin/ls -a
+ 0 : /bin/ls
+ 1 : -a
+ 0 arg /bin/ls
+ 1 arg -a
+/bin/ls
+/bin/ls
+koblensk> Command Line : cat batch.txt
+ 0 : cat
+ 1 : batch.txt
+ 0 arg cat
+ 1 arg batch.txt
+cat
+cat
+koblensk> Command Line : this is an invalid command
+ 0 : this
+ 1 : is
+ 2 : an
+ 3 : invalid
+ 4 : command
+ 0 arg this
+ 1 arg is
+ 2 arg an
+ 3 arg invalid
+ 4 arg command
+this
+this
+Doing execv this
+ 0 this
+ 1 is
+ 2 an
+ 3 invalid
+ 4 command
+Doing execv /usr/bin/this
+ 0 this
+ 1 is
+ 2 an
+ 3 invalid
+ 4 command
+Doing execv /bin/this
+ 0 this
+ 1 is
+ 2 an
+ 3 invalid
+ 4 command
+
+Command Not Found.
+Please check your path and filename.
+Note: programs needing user input cannot be executed with this shell.
+
+koblensk> Command Line : /bin/ls -a
+ 0 : /bin/ls
+ 1 : -a
+ 0 arg /bin/ls
+ 1 arg -a
+/bin/ls
+/bin/ls
+koblensk> Command Line : cat batch.txt
+ 0 : cat
+ 1 : batch.txt
+ 0 arg cat
+ 1 arg batch.txt
+cat
+cat
+koblensk> Command Line : this is an invalid command
+ 0 : this
+ 1 : is
+ 2 : an
+ 3 : invalid
+ 4 : command
+ 0 arg this
+ 1 arg is
+ 2 arg an
+ 3 arg invalid
+ 4 arg command
+this
+this
+koblensk> Command Line :
+koblensk> Command Line :
+koblensk> Command Line : echo 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+Input exceeds valid command length.
+Input must be at most 512 characters.
+ 0 :
+901234567890
+
+ 0 arg
+901234567890
+
+
+901234567890
+
+
+901234567890
+
+Doing execv
+901234567890
+
+ 0
+901234567890
+
+Doing execv /usr/bin/
+901234567890
+
+ 0
+901234567890
+
+Doing execv /bin/
+901234567890
+
+ 0
+901234567890
+
+
+Command Not Found.
+Please check your path and filename.
+Note: programs needing user input cannot be executed with this shell.
+
+koblensk> Command Line : /bin/ls -a
+ 0 : /bin/ls
+ 1 : -a
+ 0 arg /bin/ls
+ 1 arg -a
+/bin/ls
+/bin/ls
+koblensk> Command Line : cat batch.txt
+ 0 : cat
+ 1 : batch.txt
+ 0 arg cat
+ 1 arg batch.txt
+cat
+cat
+koblensk> Command Line : this is an invalid command
+ 0 : this
+ 1 : is
+ 2 : an
+ 3 : invalid
+ 4 : command
+ 0 arg this
+ 1 arg is
+ 2 arg an
+ 3 arg invalid
+ 4 arg command
+this
+this
+koblensk> Command Line :
+koblensk> Command Line :
+koblensk> Command Line : echo 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+Input exceeds valid command length.
+Input must be at most 512 characters.
+ 0 :
+901234567890
+
+ 0 arg
+901234567890
+
+
+901234567890
+
+
+901234567890
+
+koblensk> Command Line : exit invalid
+ 0 : exit
+ 1 : invalid
+ 0 arg exit
+ 1 arg invalid
+exit
+exit
+
+exit has too many arguments.
+Type "exit" or press Ctrl-D to exit.
+
+koblensk> Command Line : exit
+ 0 : exit
+ 0 arg exit
+exit
+exit
View
@@ -21,6 +21,14 @@ bool is_empty(const char *str) {
return true;
}
+void rstrip(char *str) {
+ char *end = str + strlen(str) - 1;
+ while (end > str && isspace(*end)) {
+ *end = '\0';
+ end--;
+ }
+}
+
int main (int argc, char *argv[]){
//variable initialization
struct command_t *command; //Holds parsed command
@@ -68,6 +76,39 @@ int main (int argc, char *argv[]){
strcpy(shellPath, getenv("SHELLPATH"));
} //else
+ //Find the full pathname for the file and execute command
+ int pathCount = 0;
+
+ if (shellPath != NULL) {
+
+ commandPath[0] = (char *) \
+ malloc(LENGTH*sizeof(char));
+ char *pathTmp = strtok(shellPath, ":");
+ if (pathTmp) {
+ strcpy(commandPath[0], pathTmp);
+ } else {
+ commandPath[0] = NULL;
+ printf("OOPS\n");
+ exit(1);
+ }
+
+ while (commandPath[pathCount] != NULL) {
+ pathCount++;
+
+ char *sTmp = strtok(NULL, ":");
+ if (sTmp) {
+ commandPath[pathCount] = (char *) malloc(256*sizeof(char));
+ strcpy(commandPath[pathCount], sTmp);
+ } else {
+ commandPath[pathCount] = NULL;
+ }
+ } //while
+
+ //temp = strlen(commandPath[pathCount-1]) - 1;
+ //commandPath[pathCount-1][temp] = '\0';
+ commandPath[pathCount] = NULL;
+ } //if
+
//Main Loop
while (TRUE) {
printf("%s", PROMPT);
@@ -76,14 +117,15 @@ int main (int argc, char *argv[]){
commandLine = (char *) malloc(LENGTH*sizeof(char));
checkEOF=fgets(commandLine, LENGTH, inputSrc);
printf("Command Line : %s", commandLine);
- if (is_empty(commandLine)) continue;
- commandLine[strlen(commandLine) - 1] = '\0';
+ rstrip(commandLine);
if (checkEOF == NULL){
printf("\n");
exit(0);
- } else if (strlen(commandLine) >= (unsigned)LENGTH-1) {
+ }
+ if (is_empty(commandLine)) continue;
+ if (strlen(commandLine) >= (unsigned)LENGTH-1) {
puts("\nInput exceeds valid command length.");
puts("Input must be at most 512 characters.");
while(strlen(fgets(commandLine, LENGTH, inputSrc)) >= (unsigned)LENGTH-1);
@@ -156,39 +198,6 @@ int main (int argc, char *argv[]){
} //else
} //if
- //Find the full pathname for the file and execute command
- int pathCount = 0;
-
- if (shellPath != NULL) {
-
- commandPath[0] = (char *) \
- malloc(LENGTH*sizeof(char));
- char *pathTmp = strtok(shellPath, ":");
- if (pathTmp) {
- strcpy(commandPath[0], pathTmp);
- } else {
- commandPath[0] = NULL;
- printf("OOPS\n");
- exit(1);
- }
-
- while (commandPath[pathCount] != NULL) {
- pathCount++;
-
- char *sTmp = strtok(NULL, ":");
- if (sTmp) {
- commandPath[pathCount] = (char *) malloc(256*sizeof(char));
- strcpy(commandPath[pathCount], sTmp);
- } else {
- commandPath[pathCount] = NULL;
- }
- } //while
-
- //temp = strlen(commandPath[pathCount-1]) - 1;
- //commandPath[pathCount-1][temp] = '\0';
- commandPath[pathCount] = NULL;
- } //if
-
if (execute) {
int i = 0;
int pid, rc;

0 comments on commit 5882da0

Please sign in to comment.