Esempio n. 1
0
void resulthandler<T>::operator=(std::string text)
{
	set_exitstatus(text);
}
Esempio n. 2
0
////
// main
//
//   The main function to the shell program.
//
int main (int argc, char **argv) {
   (void) argc;
   
   // variables
   set_execname (argv[0]);
   
   // loop until user exits
   while (TRUE) {   // repeat until user exits the shell
      char cmdline[1025]; // will only accept 1024
      bzero (cmdline, 1025);
      char parsebuffer[2024];
      bzero (parsebuffer, 2024);
      char *tokens[512];
      int toknum = 0;
//      bool pipein = FALSE;
//      bool pipeout = FALSE;
      
      print_prompt();    // print the prompt
      // get up to 1024 chars from the user
      if (get_cmdline (cmdline) == FALSE) continue;
      // break the line into tokens
      if (parseline (cmdline, parsebuffer, tokens, &toknum)) {
         // parseline returned an error
         eprintf ("%s: %s\n", "Error",
                  "parsing error caused by incorrect usage");
         continue;
      }

      // Check if the user wishes to exit the shell
      if (check_exit (tokens, toknum)) exit (get_exitstatus());
      

      // set the number of commands to be executed
      int numcmds = set_numofcmds (tokens, toknum);
      // the number of commands should not exceed 20
      if (numcmds > 20) {
         eprintf ("%s: %s\n", "ERROR", "cannot handle more than 20 commands");
         continue;
      }

      // initialize the current start index of the tokens
      int curstart;
      int nextstart = 0;
      
      int fd[2];
//      int fdin[2];
//      int fdout[2];
      // loop fork-exec for each command
      int i;
      for (i = 0; i < numcmds && nextstart < toknum; ++i) {
         // setup current command, update current start, and determine pipe
         curstart = nextstart;
         char *command = set_command (tokens, toknum, &nextstart);
         // a null command will cause an error
         if (command == NULL || command[0] == '\0') {
            eprintf ("%s: %s\n", "FATAL ERROR", "encountered a null command");
            break;
         }
         // make a new argv
         int newargc = get_newargc (tokens, curstart, nextstart);
         char *newargv[newargc + 1];
         newargv[0] = command;
         newargv[newargc] = NULL;
         int j = 1;
         int k;
         for (k = curstart; k < nextstart && j < newargc; ++k) {
            if (tokens[k][0] == '1') {
               newargv[j] = &tokens[k][1];
               ++j;
            }
         }

/*         // set pipe
         pipeout = checktopipe (tokens, curstart, nextstart);
         if (pipein) {
            fdin[0] = fdout[0];
            fdin[1] = fdout[1];
         }
         if (pipeout) {
            pipe (fdout);
         }
*/       
         // set redirection
         char *input_redirect = get_redirectarg (tokens, curstart, nextstart, '<');
         char *output_redirect = get_redirectarg (tokens, curstart, nextstart, '>');
         if (input_redirect != NULL) {
            // open file and set file descriptor
            fd[0] = open (input_redirect, O_RDONLY, 0755);
         }
         if (output_redirect != NULL) {
            // open file and set file descriptor
            fd[1] = open (output_redirect, O_CREAT | O_WRONLY | O_TRUNC, 0644);
         }


         // fork child process
         int status = 0;
         int pid = fork();
         if (pid != 0) {
            /* this is the PARENT PROCESS */
/*            // write to pipe from parent to child
            if (pipein) {
               close (fdin[0]);
               if (dup2 (fdin[1], STDOUT_FILENO) == -1)
                  eprintf ("%s: %s\n", "ERROR",
                           "could not open and write to pipe from parent to child");
               close (fdin[1]);
               pipein = FALSE;
               
            }
*/
            // wait for any child process to finish
            int waitstatus = waitpid (-1, &status, 0);

/*            // read pipe from child to parent
            if (pipeout) {
               close (fdout[1]);
               if (dup2 (fdout[0], STDIN_FILENO) == -1)
                  eprintf ("%s: %s\n", "ERROR",
                           "could not open and read pipe from child to parent");
               pipein = TRUE;
            }
*/          // close the file descriptors caused by redirection
            // close stdin
            if (input_redirect != NULL) close (fd[0]);
            // close stdout
            if (output_redirect != NULL) close (fd[1]);
            // determine errors
            if (waitstatus == 0) {
               eprintf ("%s: %s\n", "ERROR", "error waiting on child process");
               break;
            }else if (waitstatus == -1) {
               eprintf ("%s: %s\n", "ERROR", "error executing child process");
               break;
            }
            if (status != 0) {
               eprintf ("%s: %s: %s %d\n", "ERROR", command, "exit status of", status);
               break;
            }
         }else {
            /* this is the CHILD PROCESS */
            // set up pipe in from a previous command
/*            if (pipein) {
               close (fdin[1]);
               if (dup2 (fdin[0], STDIN_FILENO) == -1)
                  eprintf ("%s: %s\n", "ERROR",
                           "could not open and write to pipe from child to parent");
               close (fdin[0]);
            }
            // set up pipe out to the next command
            if (pipeout) {
               close (fdout[0]);
               if (dup2 (fdout[1], STDOUT_FILENO) == -1)
                  eprintf ("%s: %s\n", "ERROR",
                           "could not open and write to pipe from child to parent");
            }
*/            
            // set up redirection
            if (input_redirect != NULL) {
               // open file and set file descriptor
               // set file to stdin
               if (dup2 (fd[0], STDIN_FILENO) == -1)
                  eprintf ("%s: %s \"%s\" %s\n", "ERROR", "could not open",
                           input_redirect, "for input redirection");
            }
            if (output_redirect != NULL) {
               // open file and set file descriptor
               // set stdout to file
               if (dup2 (fd[1], STDOUT_FILENO) == -1)
                  eprintf ("%s: %s \"%s\" %s\n", "ERROR", "could not open",
                           output_redirect, "for output redirection");
            }

            // execute the command
            set_exitstatus (execvp (command, newargv));
            // if there was an error executing the command
            eprintf ("%s: %s: %s\n", "ERROR", command, "could not execute");
            return (get_exitstatus());
         }
         
      }

   }
   return get_exitstatus();
}
Esempio n. 3
0
void resulthandler<T>::operator=(const int id)
{
	set_exitstatus(id);
}