Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Welcome to GDB Online.
- GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
- C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
- Code, Compile, Run and Debug online from anywhere in world.
- *******************************************************************************/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef enum{false,true} bool;
- struct program{
- char name[256];
- char args[16][16];
- };
- struct AST{
- struct AST *left, *right, *parent;
- struct program prog;
- char file[32];
- char meta;
- };
- struct AST* find_root(struct AST* curr){
- if (curr->parent)
- return find_root(curr->parent);
- else
- return curr;
- }
- void destroyAST(struct AST* curr){
- if (curr == NULL)
- return;
- curr = find_root(curr);
- if (curr->left)
- curr->left->parent = NULL;
- if (curr->right)
- curr->right->parent = NULL;
- destroyAST(curr->right);
- destroyAST(curr->left);
- free(curr);
- }
- void printAST(struct AST* curr){
- if (curr == NULL)
- return;
- printAST(curr->left);
- switch(curr->meta){
- case 'f':
- printf("(%s)\n", curr->file);
- break;
- case 'p':
- printf("Program: (%s)\nArgs: ", curr->prog.name);
- for (int i = 0; i < 16; ++i)
- printf("(%s) ", curr->prog.args[i]);
- printf("%s\n", "");
- break;
- case 'e':
- printf("%s\n", "(end)");
- default: //meta char
- printf("%c\n", curr->meta);
- }
- printAST(curr->right);
- }
- bool isValidProgram(struct program* prog){
- return true;
- }
- bool isValidFile(char * filename){
- return true;
- }
- struct AST* split(struct AST* curr, char meta){
- if (curr->parent){
- return split(curr->parent, meta);
- }else{
- struct AST* new_root = malloc(sizeof(struct AST));
- memset(new_root, 0, sizeof(struct AST));
- new_root->meta = meta;
- new_root->left = curr;
- new_root->right = malloc(sizeof(struct AST));
- memset(new_root->right, 0, sizeof(struct AST));
- switch(meta){
- case '<':
- case '>':
- new_root->right->meta = 'f'; //for file
- break;
- case '|':
- new_root->right->meta = 'p'; //for program
- break;
- case '&':
- new_root->right->meta = 'e'; //for end of command
- break;
- default:
- printf("meta was %c\n", meta);
- new_root->right->meta = 'M';
- }
- new_root->left->parent = new_root;
- new_root->right->parent = new_root;
- return new_root->right;
- }
- }
- int main()
- {
- struct program programs[16];
- FILE *infile, *outfile;
- struct AST* curr_node = malloc(sizeof(struct AST));
- memset(curr_node, 0, sizeof(struct AST));
- memset(programs, 0, sizeof(programs));
- char str[] = "cmd1 arg1 arg2 arg3<file1 | cmd2 arg1 arg2 arg3 arg4 | cmd3 arg0 arg1 arg2 >file2";
- printf("Command: %s\n", str);
- char* p;
- char tokens[16][32];
- memset(tokens, 0, sizeof(tokens));
- p = strtok(str, " ");
- if (p)
- strcpy(tokens[0], p);
- for(int i=1; p!=NULL && i < 16;++i){
- p = strtok(NULL, " ");
- if (p)
- strcpy(tokens[i], p);
- }
- bool reading_prog, reading_args, reading_file;
- int a, n;
- reading_prog = true;
- reading_args = reading_file = false;
- a = 0;
- curr_node->meta = 'p';
- for(int i=0; i <16;++i){
- n = 0;
- //printf("%d%s\n", i,tokens[i]);
- for (int k = 0; k < strlen(tokens[i]); ++k){
- switch(tokens[i][k]){
- case '<':
- case '>':
- curr_node = split(curr_node, tokens[i][k]);
- a=0;
- n=0;
- reading_prog = false;
- reading_args = false;
- reading_file = true;
- break;
- case '|':
- curr_node = split(curr_node, tokens[i][k]);
- a=0;
- n=0;
- reading_prog = true;
- reading_args = false;
- reading_file = false;
- break;
- case '&':
- reading_prog = false;
- reading_args = false;
- reading_file = false;
- curr_node = split(curr_node, tokens[i][k]);
- break; //TODO handle this better lol
- default:
- if (reading_prog){
- curr_node->prog.name[n++] = tokens[i][k];
- }else if (reading_args){
- curr_node->prog.args[a][n++] = tokens[i][k];
- }else if (reading_file){
- curr_node->file[n++] = tokens[i][k];
- }else{
- printf("%s\n", "error parsing...");
- }
- }
- }
- if (reading_prog){
- if (strlen(curr_node->prog.name) > 0){
- reading_prog = false;
- reading_args = true;
- reading_file = false;
- a=0;
- }
- }
- if(reading_args){
- if (strlen(curr_node->prog.args[0]) > 0)
- ++a;
- }else{
- a = 0;
- }
- n=0;
- }
- if (curr_node){
- printAST(find_root(curr_node));
- }
- destroyAST(curr_node);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement