Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.78 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
  5. C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
  6. Code, Compile, Run and Debug online from anywhere in world.
  7.  
  8. *******************************************************************************/
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12.  
  13. typedef enum{false,true} bool;
  14.  
  15. struct program{
  16.     char name[256];
  17.     char args[16][16];
  18. };
  19.  
  20. struct AST{
  21.     struct AST *left, *right, *parent;
  22.     struct program prog;
  23.     char file[32];
  24.     char meta;
  25. };
  26.  
  27. struct AST* find_root(struct AST* curr){
  28.     if (curr->parent)
  29.         return find_root(curr->parent);
  30.     else
  31.         return curr;
  32. }
  33.  
  34. void destroyAST(struct AST* curr){
  35.     if (curr == NULL)
  36.         return;
  37.        
  38.     curr = find_root(curr);
  39.     if (curr->left)
  40.         curr->left->parent = NULL;
  41.     if (curr->right)
  42.         curr->right->parent = NULL;
  43.    
  44.     destroyAST(curr->right);
  45.     destroyAST(curr->left);
  46.     free(curr);
  47. }
  48.  
  49. void printAST(struct AST* curr){
  50.     if (curr == NULL)
  51.         return;
  52.     printAST(curr->left);
  53.     switch(curr->meta){
  54.         case 'f':
  55.             printf("(%s)\n", curr->file);
  56.             break;
  57.         case 'p':
  58.             printf("Program: (%s)\nArgs: ", curr->prog.name);
  59.             for (int i = 0; i < 16; ++i)
  60.                 printf("(%s) ", curr->prog.args[i]);
  61.             printf("%s\n", "");
  62.             break;
  63.         case 'e':
  64.             printf("%s\n", "(end)");
  65.         default: //meta char
  66.             printf("%c\n", curr->meta);
  67.     }
  68.     printAST(curr->right);
  69. }
  70.  
  71. bool isValidProgram(struct program* prog){
  72.     return true;
  73. }
  74.  
  75. bool isValidFile(char * filename){
  76.     return true;
  77. }
  78.  
  79.  
  80. struct AST* split(struct AST* curr, char meta){
  81.     if (curr->parent){
  82.         return split(curr->parent, meta);
  83.     }else{
  84.         struct AST* new_root = malloc(sizeof(struct AST));
  85.         memset(new_root, 0, sizeof(struct AST));
  86.         new_root->meta = meta;
  87.         new_root->left = curr;
  88.         new_root->right = malloc(sizeof(struct AST));
  89.         memset(new_root->right, 0, sizeof(struct AST));
  90.         switch(meta){
  91.             case '<':
  92.             case '>':
  93.                 new_root->right->meta = 'f'; //for file
  94.                 break;
  95.             case '|':
  96.                 new_root->right->meta = 'p'; //for program
  97.                 break;
  98.             case '&':
  99.                 new_root->right->meta = 'e'; //for end of command
  100.                 break;
  101.             default:
  102.                 printf("meta was %c\n", meta);
  103.                 new_root->right->meta = 'M';
  104.         }
  105.         new_root->left->parent = new_root;
  106.         new_root->right->parent = new_root;
  107.         return new_root->right;
  108.     }
  109. }
  110.  
  111. int main()
  112. {
  113.     struct program programs[16];
  114.     FILE *infile, *outfile;
  115.     struct AST* curr_node = malloc(sizeof(struct AST));
  116.     memset(curr_node, 0, sizeof(struct AST));
  117.     memset(programs, 0, sizeof(programs));
  118.     char str[] = "cmd1 arg1 arg2 arg3<file1 | cmd2 arg1 arg2 arg3 arg4 | cmd3 arg0 arg1 arg2 >file2";
  119.     printf("Command: %s\n", str);
  120.     char* p;
  121.     char tokens[16][32];
  122.     memset(tokens, 0, sizeof(tokens));
  123.     p = strtok(str, " ");
  124.     if (p)
  125.         strcpy(tokens[0], p);
  126.     for(int i=1; p!=NULL && i < 16;++i){
  127.         p = strtok(NULL, " ");
  128.         if (p)
  129.             strcpy(tokens[i], p);
  130.     }
  131.     bool reading_prog, reading_args, reading_file;
  132.     int a, n;
  133.     reading_prog = true;
  134.     reading_args = reading_file = false;
  135.     a = 0;
  136.     curr_node->meta = 'p';
  137.     for(int i=0; i <16;++i){
  138.         n = 0;
  139.         //printf("%d%s\n", i,tokens[i]);
  140.         for (int k = 0; k < strlen(tokens[i]); ++k){
  141.             switch(tokens[i][k]){
  142.                 case '<':
  143.                 case '>':
  144.                     curr_node = split(curr_node, tokens[i][k]);
  145.                     a=0;
  146.                     n=0;
  147.                     reading_prog = false;
  148.                     reading_args = false;
  149.                     reading_file = true;
  150.                     break;
  151.                 case '|':
  152.                     curr_node = split(curr_node, tokens[i][k]);
  153.                     a=0;
  154.                     n=0;
  155.                     reading_prog = true;
  156.                     reading_args = false;
  157.                     reading_file = false;
  158.                     break;
  159.                 case '&':
  160.                     reading_prog = false;
  161.                     reading_args = false;
  162.                     reading_file = false;
  163.                     curr_node = split(curr_node, tokens[i][k]);
  164.                     break; //TODO handle this better lol
  165.                 default:
  166.                     if (reading_prog){
  167.                         curr_node->prog.name[n++] = tokens[i][k];
  168.                     }else if (reading_args){
  169.                         curr_node->prog.args[a][n++] = tokens[i][k];
  170.                     }else if (reading_file){
  171.                         curr_node->file[n++] = tokens[i][k];
  172.                     }else{
  173.                         printf("%s\n", "error parsing...");
  174.                     }
  175.             }
  176.         }
  177.         if (reading_prog){
  178.             if (strlen(curr_node->prog.name) > 0){
  179.                 reading_prog = false;
  180.                 reading_args = true;
  181.                 reading_file = false;
  182.                 a=0;
  183.             }
  184.         }
  185.        
  186.         if(reading_args){
  187.             if (strlen(curr_node->prog.args[0]) > 0)
  188.                 ++a;
  189.         }else{
  190.             a = 0;
  191.         }
  192.        
  193.         n=0;
  194.     }
  195.     if (curr_node){
  196.         printAST(find_root(curr_node));
  197.     }
  198.     destroyAST(curr_node);
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement