Advertisement
Guest User

processline

a guest
Nov 29th, 2012
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. void processline (char *line, int inFD, int outFD, plFlags FLAGS) //, int mainargc, char **mainargv, char **shiftedMArgV)
  2. {
  3.     int    status;
  4.     char expanded_line[EXPANDEDLEN];
  5.     char **args;
  6.     char *line_itr = line;
  7.     int command_count;
  8.     int pix;
  9.     int fd_a[2];
  10.     int fd_b[2];
  11.     int line_len = strlen(line);
  12.     plFlags pl_flags = {0U, 0U, 0U};
  13.    
  14.     if(FLAGS.EXPAND)
  15.     {
  16.         if(expand(line, expanded_line, EXPANDEDLEN)) return; //, mainargc, mainargv, shiftedMArgV)) return;
  17.         line_itr = expanded_line;
  18.         line_len = strlen(expanded_line);
  19.     }
  20.  
  21.     if((pix = findUnquotChar(line_itr, '|')))
  22.     {
  23.         line_itr[pix++] = 0;
  24.         if(pipe (fd_a) < 0) perror("pipe");
  25.         processline(line_itr, inFD, fd_a[1], pl_flags);
  26.         line_itr = &(line_itr[pix]);
  27.  
  28.         while((pix = findUnquotChar(line_itr, '|')) && pix < line_len)
  29.         {
  30.             close(fd_a[1]);
  31.             line_itr[pix++] = 0;
  32.             if(pipe (fd_b) < 0) perror("pipe");
  33.             processline(line_itr, fd_a[0], fd_b[1] pl_flags);
  34.             close(fd_a[0]);
  35.             close(fd_b[1]);
  36.             line_itr = &(line_itr[pix]);
  37.         }
  38.         return;
  39.     }
  40.    
  41.     command_count = arg_parse(expanded_line, &args);
  42.     /* print_args(args); */
  43.  
  44.     if(!command_count) return;
  45.  
  46.     if(!exec_if_built_in(args, outFD))
  47.     {  
  48.         /* Start a new process to do the job. */
  49.         cpid = fork();
  50.         if (cpid < 0) {
  51.             perror ("fork");
  52.             free(args);
  53.             return;
  54.         }
  55.        
  56.         /* Check for who we are! */
  57.         if (cpid == 0) {
  58.           /* We are the child! */
  59.    
  60.             /* Turning stdout into pipe output if needed */
  61.             if((dup2(outFD, 1)) < 0)
  62.             {
  63.                 perror("dup");
  64.                 return;
  65.             }
  66.            
  67.             execvp(args[0], args);
  68.             perror ("exec");
  69.             exit (127);
  70.             last_exit_status = 127;
  71.         }
  72.  
  73.         /* Have the parent wait for child to complete */
  74.         if(FLAGS.WAIT)
  75.         {
  76.             if(wait(&status) < 0) perror("wait");
  77.             last_exit_status = ((WEXITSTATUS(status) || !WIFEXITED(status)) ? 127 : 0);
  78.         }
  79.     }
  80.  
  81.     free(args);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement