@@ -653,19 +653,87 @@ void free_string_array_token(string_array_token simple_commands) {
653653// free(file_info.merge_filenames);
654654// }
655655
656+ void resetIO (int * tmpin , int * tmpout , int * tmperr ) {
657+ dup2 (* tmpin , 0 );
658+ dup2 (* tmpout , 1 );
659+ dup2 (* tmperr , 2 );
660+ close (* tmpin );
661+ close (* tmpout );
662+ close (* tmperr );
663+ * tmpin = dup (0 );
664+ * tmpout = dup (1 );
665+ * tmperr = dup (2 );
666+ }
667+
668+ void outputRedirection (file_redirection_data file_info , int pd [2 ], int * fdout , int * fdin , int tmpout , int i ,
669+ string_array_token simple_commands_arr ) {
670+ if (file_info .output_filename != NULL ) {
671+ * fdout = file_info .output_append ? open (file_info .output_filename , O_RDWR | O_CREAT | O_APPEND , 0666 )
672+ : open (file_info .output_filename , O_RDWR | O_CREAT | O_TRUNC , 0666 );
673+ } else if (i < simple_commands_arr .len - 1 && simple_commands_arr .token_arr [i + 1 ] == PIPE_CMD ) {
674+ pipe (pd );
675+ * fdout = pd [1 ];
676+ * fdin = pd [0 ];
677+ } else {
678+ * fdout = dup (tmpout );
679+ }
680+ }
681+
682+ void errorRedirection (file_redirection_data file_info , int * fderr , int tmperr ) {
683+ if (file_info .stderr_filename != NULL ) {
684+ * fderr = file_info .stderr_append ? open (file_info .stderr_filename , O_RDWR | O_CREAT | O_APPEND , 0666 )
685+ : open (file_info .stderr_filename , O_RDWR | O_CREAT | O_TRUNC , 0666 );
686+ } else {
687+ * fderr = dup (tmperr );
688+ }
689+ }
690+
691+ void mergeRedirection (file_redirection_data file_info , int * fdout ) {
692+ * fdout = file_info .merge_append ? open (file_info .merge_filename , O_RDWR | O_CREAT | O_APPEND , 0666 )
693+ : open (file_info .merge_filename , O_RDWR | O_CREAT | O_TRUNC , 0666 );
694+ dup2 (* fdout , STDOUT_FILENO );
695+ dup2 (* fdout , STDERR_FILENO );
696+ close (* fdout );
697+ }
698+
699+ string_array getPathBins () {
700+ string_array PATH_ARR = splitString (getenv ("PATH" ), ':' );
701+ string_array all_files_in_dir = getAllFilesInDir (& PATH_ARR );
702+ free_string_array (& PATH_ARR );
703+ string_array removed_dots = removeDots (& all_files_in_dir );
704+
705+ return removeDuplicates (& removed_dots );
706+ }
707+
708+ bool wildcardLogic (string_array_token simple_commands_arr , int * fdout , int * fderr , int tmpout , int tmperr , int i ) {
709+ if (strchr (simple_commands_arr .values [i ], '*' ) != NULL || strchr (simple_commands_arr .values [i ], '?' ) != NULL ) {
710+ if (!replaceWildcards (& simple_commands_arr .values [i ])) {
711+ * fdout = dup (tmpout );
712+ * fderr = dup (tmperr );
713+ dup2 (* fderr , STDERR_FILENO );
714+ close (* fderr );
715+ dup2 (* fdout , STDOUT_FILENO );
716+ close (* fdout );
717+ printf ("psh: no wildcard matches found\n" );
718+ return false;
719+ }
720+ }
721+ return true;
722+ }
723+
724+ bool foundBuiltin (string_array splitted_line , builtins_array BUILTINS , int * builtin_index ) {
725+ return (splitted_line .len > 0 && (* builtin_index = isBuiltin (splitted_line .values [0 ], BUILTINS )) != -1 ) ? true
726+ : false;
727+ }
728+
656729#ifndef TEST
657730
658731int main (int argc , char * argv []) {
659732 char * line ;
660- // string_array splitted_line;
661733 char dir [PATH_MAX ];
662734 bool loop = true;
663735 string_array command_history = {.len = 0 , .values = calloc (HISTORY_SIZE , sizeof (char * ))};
664- string_array PATH_ARR = splitString (getenv ("PATH" ), ':' );
665- string_array all_files_in_dir = getAllFilesInDir (& PATH_ARR );
666- free_string_array (& PATH_ARR );
667- string_array removed_dots = removeDots (& all_files_in_dir );
668- string_array PATH_BINS = removeDuplicates (& removed_dots );
736+ string_array PATH_BINS = getPathBins ();
669737 string_array global_command_history = getAllHistoryCommands ();
670738
671739 char * current_dir = getcwd (dir , sizeof (dir ));
@@ -703,19 +771,8 @@ int main(int argc, char* argv[]) {
703771 int fdin = open (0 , O_RDONLY );
704772
705773 for (int i = 0 ; i < simple_commands_arr .len ; i ++ ) {
706- if (strchr (simple_commands_arr .values [i ], '*' ) != NULL ||
707- strchr (simple_commands_arr .values [i ], '?' ) != NULL ) {
708- if (!replaceWildcards (& simple_commands_arr .values [i ])) {
709- fdout = dup (tmpout );
710- fderr = dup (tmperr );
711- dup2 (fderr , STDERR_FILENO );
712- close (fderr );
713- dup2 (fdout , STDOUT_FILENO );
714- close (fdout );
715- printf ("psh: no wildcard matches found\n" );
716- // free_string_array_token(simple_commands_arr);
717- continue ;
718- }
774+ if (!wildcardLogic (simple_commands_arr , & fdout , & fderr , tmpout , tmperr , i )) {
775+ continue ;
719776 }
720777 token_index_arr token = tokenizeLine (simple_commands_arr .values [i ]);
721778 removeWhitespaceTokens (& token );
@@ -734,20 +791,11 @@ int main(int argc, char* argv[]) {
734791 continue ;
735792 }
736793 } else if (simple_commands_arr .token_arr [i ] == AMP_CMD ) {
737- // reset all io
738- dup2 (tmpin , 0 );
739- dup2 (tmpout , 1 );
740- dup2 (tmperr , 2 );
741- close (tmpin );
742- close (tmpout );
743- close (tmperr );
744- tmpin = dup (0 );
745- tmpout = dup (1 );
746- tmperr = dup (2 );
794+ resetIO (& tmpin , & tmpout , & tmperr );
747795 }
748- if (splitted_line .len > 0 && (builtin_index = isBuiltin (splitted_line .values [0 ], BUILTINS )) != -1 ) {
796+
797+ if (foundBuiltin (splitted_line , BUILTINS , & builtin_index )) {
749798 if (!callBuiltin (splitted_line , BUILTINS .array , builtin_index )) {
750- // free_string_array(&simple_commands_arr);
751799 free_string_array (& splitted_line );
752800 loop = false;
753801 break ;
@@ -765,42 +813,25 @@ int main(int argc, char* argv[]) {
765813 dup2 (fdin , STDIN_FILENO );
766814 close (fdin );
767815 if (file_info .merge_filename != NULL ) {
768- fdout = file_info .merge_append ? open (file_info .merge_filename , O_RDWR | O_CREAT | O_APPEND , 0666 )
769- : open (file_info .merge_filename , O_RDWR | O_CREAT | O_TRUNC , 0666 );
770- dup2 (fdout , STDOUT_FILENO );
771- dup2 (fdout , STDERR_FILENO );
772- close (fdout );
816+ mergeRedirection (file_info , & fdout );
773817 } else {
774- if (file_info .output_filename != NULL ) {
775- fdout = file_info .output_append ? open (file_info .output_filename , O_RDWR | O_CREAT | O_APPEND , 0666 )
776- : open (file_info .output_filename , O_RDWR | O_CREAT | O_TRUNC , 0666 );
777- } else if (i < simple_commands_arr .len - 1 && simple_commands_arr .token_arr [i + 1 ] == PIPE_CMD ) {
778- pipe (pd );
779- fdout = pd [1 ];
780- fdin = pd [0 ];
781- } else {
782- fdout = dup (tmpout );
783- }
784- if (file_info .stderr_filename != NULL ) {
785- fderr = file_info .stderr_append ? open (file_info .stderr_filename , O_RDWR | O_CREAT | O_APPEND , 0666 )
786- : open (file_info .stderr_filename , O_RDWR | O_CREAT | O_TRUNC , 0666 );
787- } else {
788- fderr = dup (tmperr );
789- }
818+ outputRedirection (file_info , pd , & fdout , & fdin , tmpout , i , simple_commands_arr );
819+ errorRedirection (file_info , & fderr , tmperr );
820+
790821 dup2 (fderr , STDERR_FILENO );
791822 close (fderr );
792823 dup2 (fdout , STDOUT_FILENO );
793824 close (fdout );
794825 }
795- if (splitted_line .len > 0 ) {
796- pid = fork ();
797826
798- if (pid == 0 ) {
827+ if (splitted_line .len > 0 ) {
828+ if ((pid = fork ()) == 0 ) {
799829 int error = execvp (splitted_line .values [0 ], splitted_line .values );
800830 if (error ) {
801831 printf ("couldn't find command %s\n" , splitted_line .values [0 ]);
802832 }
803833 }
834+
804835 if (waitpid (pid , & w_status , WUNTRACED | WCONTINUED ) == -1 ) {
805836 exit (EXIT_FAILURE );
806837 }
0 commit comments