Facebook
From Trivial Giraffe, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 49
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<unistd.h>
  5. #include<termios.h>
  6. #include<sys/types.h>
  7. #include<sys/wait.h>
  8.  
  9.  
  10.  
  11. #define MAXLENGTH 1000 // max dlugosc lini
  12. #define MAXCOMMANDS 100 // max liczba parametrow
  13. #define MAXHISTORY 50 // max l. el. w historii
  14. #define MAXUSERNAMELENGTH 50
  15.  
  16.  
  17. static struct termios oldt, newt;
  18.  
  19. int countHistory;
  20. char * username;
  21. char * cwd;
  22.  
  23. void clear(){
  24. printf("\033c");//\033 == \x1B == 27 == ESC
  25. }
  26.    
  27. void printHelp()
  28. {
  29.         printf("\n");
  30.         printf("MyShell help(). Usage:\n");
  31.         printf("ls {path}\t Prints content of a directory\n");
  32.         printf("\t Default directory is \".\"\n");
  33.         printf("cd {path}\n");
  34.         printf("cp [src] [dst]\t Copies file from source to destination\n");
  35.         printf("exec [programname]\tStarts a program\n");
  36.         printf("mkdir [name]\tCreates new directory\n");
  37.         printf("\n");
  38.         printf("\n");
  39.         printf("\n");
  40.         printf("\n");
  41.         printf("\n");
  42.         printf("\n");
  43.        
  44. }
  45.        
  46. void init()
  47. {
  48.         //wylaczenie buforu terminala
  49.         tcgetattr( STDIN_FILENO, &oldt);
  50.         newt = oldt;
  51.         newt.c_lflag &= ~(ICANON);          
  52.         tcsetattr( STDIN_FILENO, TCSANOW, &newt);  
  53.  
  54.        
  55.         clear();
  56.         sleep(1);
  57.         username = getenv("USER");
  58.         printf("Witaj, %s!\n", username);
  59.                 sleep(1);
  60.                 clear();
  61. }
  62.  //
  63. int prompt(){
  64.         if ( getcwd( cwd, MAXLENGTH * sizeof(char) ) == NULL )
  65.         {
  66.                 printf("GETCWD ERROR !");
  67.                 return 0;
  68.         }
  69.         else
  70.         {
  71.         printf("[%s] %s> $",cwd, username);
  72.                 return 1;
  73.         }
  74. }
  75.  
  76. int readInput(char *input)
  77. {
  78.         char *buf = malloc( MAXLENGTH + 1 );
  79.         int c;
  80.         int count = 0;
  81.        
  82.         while(1)
  83.         {
  84.                 c = getchar();
  85.                 if ( (c == "\n") || (c==EOF) )
  86.                 {
  87.                         buf[count] = "\0";
  88.                         strcpy(input,buf);
  89.                         free (buf);
  90.                         return count;
  91.                 }
  92.         }
  93. }
  94.  
  95. int findPipes(char *input, char *toParse, char *cmdRest)
  96. {
  97.  
  98.         char * b = strtok(input, "|");
  99.        
  100.         char * c = strtok(NULL, "");
  101.  
  102.         strcpy(toParse,input);
  103.         strcpy(toParse,b);
  104.  
  105.         if (strcmp(toParse,"")) return -1;
  106.         else if (strcmp(cmdRest,"")) return 0;
  107.         else return 1;
  108.  
  109.  
  110. }
  111.  
  112. int parseSpace(char *input, char **parsed)
  113. {
  114.         int i;
  115.         char * token;
  116.         for (i = 0; i < MAXCOMMANDS; i++)
  117.         {
  118.                 while ((token = strtok(input," "))!=NULL){
  119.                 strcpy(parsed[i],token);       
  120.         }
  121. }
  122.  
  123. return 0;
  124. }
  125.  
  126. int parseInput(char *input, char **parsed, char *cmdRest)
  127. {
  128.  
  129.         int isPiped = 0;
  130.         char *toParse = calloc((MAXLENGTH+1)*sizeof(char),sizeof(char));
  131.  
  132.         isPiped = findPipes(input, toParse, cmdRest);
  133.  
  134.         printf("Input: %s \n toParse: %s \n cmdRest: %s \n",input,toParse,cmdRest);
  135.  
  136.         if(isPiped == 1)
  137.         {
  138.                 printf("FOUND PIPE!\n");
  139.  
  140.         }
  141.         parseSpace(toParse, parsed);
  142.        
  143.                
  144.  
  145.         return 0;
  146. }
  147.  
  148.  
  149.  
  150. int main()
  151. {
  152.         int i,cArgs,c;
  153.         char *inputLine = malloc(MAXLENGTH + 1);
  154.         char *cmdRest = malloc(MAXLENGTH + 1);
  155.         username = malloc(MAXLENGTH + 1);
  156.         cwd = malloc(MAXLENGTH + 1);
  157.         char **parsedCmds = malloc(MAXCOMMANDS*sizeof(char*));
  158.         for (i=0;i<MAXLENGTH;i++) parsedCmds[i] = malloc(MAXLENGTH + 1) ;
  159.        
  160.  
  161.        
  162.  
  163.        
  164.        
  165.        
  166.  
  167.         init();
  168.        
  169.        
  170.         while(1)
  171.         {
  172.                 if ( !prompt() ) return 1;
  173.                 else
  174.                 {
  175.                         c = readInput(inputLine);
  176.                         if ( c )
  177.                         {
  178.                                 printf("\n (%d)%s \n",c,inputLine);
  179.                                 cArgs = parseInput(inputLine, parsedCmds, cmdRest);
  180.                                 for
  181.                                 (
  182.                                         i = 0,printf("\nDEBUG\n");
  183.                                         i < cArgs;
  184.                                         printf("Arg[%d]=%s\n",i, parsedCmds[i]),i++
  185.                                 );
  186.  
  187.                         }
  188.                 }
  189.  
  190.  
  191.         }
  192.  
  193.         tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
  194.         return 0;
  195. }
  196.