Advertisement
Guest User

msh.c

a guest
Nov 18th, 2012
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include "proto.h"
  9.  
  10. /* Constants */
  11.  
  12. #define LINELEN 1024
  13. #define EXPANDEDLEN 200000
  14.  
  15. /* Globals */
  16.  int last_exit_status = 0;
  17.  int m_argc;
  18.  int m_shifted_argc;
  19.  char **m_argv;
  20.  char **m_shifted_argv;
  21.  
  22. /* Prototypes */
  23.  
  24. void processline (char *line, int outFD, int waitFlag);
  25. void print_args(char **args);
  26.  
  27. /* Shell main */
  28.  
  29. int main (int mainargc, char **mainargv)
  30. {
  31.     // Defining Locals
  32.     char *promptStr;
  33.     char   buffer [LINELEN];
  34.     int    len;
  35.     FILE *fp;
  36.  
  37.     m_argc = mainargc;
  38.     m_shifted_argc = mainargc;
  39.     m_argv = mainargv;
  40.     m_shifted_argv = mainargv;
  41.  
  42.     if(m_argc > 1) fp = fopen(m_argv[1], "r");
  43.     else fp = stdin;
  44.  
  45.     if(fp == NULL)
  46.     {
  47.         fprintf(stderr, "./msh: Can not open file %s\n", m_argv[1]);
  48.         exit(127);
  49.     }
  50.  
  51.     while(1)
  52.     {
  53.         if(fp == stdin)
  54.         {
  55.             if(!(promptStr = getenv("P1"))) fprintf(stderr, "%% ");
  56.             else fprintf(stderr, promptStr);
  57.         }
  58.        
  59.         if(fgets(buffer, LINELEN, fp) != buffer) break;
  60.  
  61.         len = strlen(buffer);
  62.         if (buffer[len-1] == '\n') buffer[len-1] = 0;
  63.  
  64.         processline (buffer, 1, 1); //, mainargc, mainargv, shiftedMArgV);
  65.     }
  66.  
  67.     if (!feof(fp)) perror ("read");
  68.     if(fclose(fp) != 0) perror("close");
  69.     return 0;       /* Also known as exit (0); */
  70. }
  71.  
  72.  
  73. void processline (char *line, int outFD, int waitFlag) //, int mainargc, char **mainargv, char **shiftedMArgV)
  74. {
  75.     pid_t  cpid;
  76.     int    status;
  77.     /* All the parsed arguments, will be passed to exec */
  78.     /* int expanded_len = 2048;*/
  79.     char expanded_line[EXPANDEDLEN];
  80.     char **args;
  81.     int command_count;
  82.     /* int index = 0; */
  83.    
  84.     if(expand(line, expanded_line, EXPANDEDLEN)) return; //, mainargc, mainargv, shiftedMArgV)) return;
  85.  
  86.     fprintf(stderr, "strlen(expanded) = %d\n", (int)strlen(expanded_line));
  87.    
  88.     command_count = arg_parse(expanded_line, &args);
  89.  
  90.     if(!command_count) return;
  91.  
  92.     if(!exec_if_built_in(args, outFD))
  93.     {  
  94.         /* Start a new process to do the job. */
  95.         cpid = fork();
  96.         if (cpid < 0) {
  97.             perror ("fork");
  98.             free(args);
  99.             return;
  100.         }
  101.        
  102.         /* Check for who we are! */
  103.         if (cpid == 0) {
  104.           /* We are the child! */
  105.    
  106.             /* Turning stdout into pipe output if needed */
  107.             if((dup2(outFD, 1)) < 0)
  108.             {
  109.                 perror("dup");
  110.                 return;
  111.             }
  112.            
  113.             execvp(args[0], args);
  114.             perror ("exec");
  115.             exit (127);
  116.             last_exit_status = 127;
  117.         }
  118.        
  119.         /* Have the parent wait for child to complete */
  120.         if(waitFlag)
  121.         {
  122.             if (wait (&status) < 0) perror ("wait");
  123.             last_exit_status = ((WEXITSTATUS(status) || !WIFEXITED(status)) ? 127 : 0);
  124.         }
  125.     }
  126.     free(args);
  127. }
  128.  
  129.  
  130. /* Procedure for testing purposes,
  131. prints the arguments of a command array */
  132. void print_args(char **args)
  133. {
  134.     int i = 0;
  135.     char** cmd = args;
  136.    
  137.     printf("COMMANDS: ");
  138.     while(cmd[i] != 0)
  139.     {
  140.         printf("'%s'\n", cmd[i]);
  141.         i++;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement