void build_depend_list(command_t comm, token_stream_t token_stream) { //command_stream_t cmd_stream = command_stream_head; if(comm == NULL) printf("NUll command\n"); else { //the sub_head is t->head //token_t token_now = sub_head->next; token_t curr_node = token_stream->head->next; while(curr_node != NULL) { if(curr_node->t_type == RIGHT_DERCTION) { add_out_list (curr_node->next->text, comm); curr_node = curr_node->next; } else if(curr_node->text != NULL) { add_filename_to_dependlist(curr_node->text, comm); } curr_node = curr_node->next; } //token_stream = token_stream->next; //cmd_stream = cmd_stream->next; } }
EDGE * GRAPH::new_edge_c(VERTEX * from, VERTEX * to) { EDGE * e = m_e_free_list.get_free_elem(); if (e == NULL) { e = (EDGE*)_xmalloc(sizeof(EDGE)); } EDGE_from(e) = from; EDGE_to(e) = to; add_in_list(to, e); add_out_list(from, e); return e; }
command_t make_command_tree(token_stream_t t) { token_t sub_head = t->head;//the sub_head is t->head token_t token_now = sub_head->next; Stack_t operators =initStack(); Stack_t operands =initStack();//build two stacks to hold operands and operators command_t comm;//one command command_t prev = NULL; int line = token_now->line; int counter=0; //if(token_now->next == NULL) // printf("Heere\n"); while(token_now!=NULL) { counter++; // printf("the %d time loop, the type is %d", counter,token_now->t_type); if( !(token_now->t_type == LEFT_DERCTION || token_now->t_type == RIGHT_DERCTION) ) { // make new command comm = (command_t)checked_malloc(sizeof( struct command )); // add_filename_to_dependlist(token_now->text,comm); } switch(token_now->t_type) { case AND: comm->type = AND_COMMAND; while ( !is_empty(operators) &&( top(operators)->type == PIPE_COMMAND || top(operators)->type == OR_COMMAND || top(operators)->type == AND_COMMAND )) { command_t pop_ops = pop(operators);//pop until the top ops has smaller presendence than if (! (pop_ops, operands)) { error(2, 0, "Line %d: Syntax error. Not enough children to create new tree.", line); return NULL; } } push(operators,comm);//push ADD command to ops token_now = token_now->next; break; case OR: comm->type = OR_COMMAND; while ( !is_empty(operators) &&( top(operators)->type == PIPE_COMMAND || top(operators)->type == OR_COMMAND || top(operators)->type == AND_COMMAND )) { command_t pop_ops = pop(operators);//pop until the top ops has smaller presendence than if (!make_command_branch(pop_ops, operands)) { error(2, 0, "Line %d: Syntax error. Not enough children to create new tree.", line); return NULL; } } push(operators,comm);//push ADD command to ops token_now = token_now->next; break; // push OR to ops push(operators,comm); break; case PIPELINE: comm->type = PIPE_COMMAND; while ( !is_empty(operators) && top(operators)->type == PIPE_COMMAND ) { command_t pop_ops = pop(operators);//pop until the top ops has smaller presendence than if (!make_command_branch(pop_ops, operands)) { error(2, 0, "Line %d: Syntax error. Not enough children to create new tree.", line); return NULL; } } push(operators,comm);//push PIPE command to ops token_now = token_now->next; break; case SEMICOLON: comm->type = SEQUENCE_COMMAND; // always pop since SEMICOLON <= all ops while (!is_empty(operators)) { command_t pop_ops = pop(operators); if (!make_command_branch(pop_ops, operands)) { error(2, 0, "Line %d: Syntax error. Not enough children to create new tree.", line); return NULL; } } // push SEMICOLON to ops push(operators,comm); token_now = token_now->next; break; case WORD: comm->type = SIMPLE_COMMAND; //seprated by word int num_words = 1; token_t count = token_now; while(count!=NULL&&count->next!=NULL&&count->next->t_type==WORD)//while next is still word { count = count ->next; num_words++; } comm->u.word = (char**)checked_malloc((num_words+1)*sizeof(char**)); comm->u.word[0] = token_now->text; int i =1; while(i< num_words) { token_now = token_now->next; comm->u.word[i] = token_now->text; // add_filename_to_dependlist(comm->u.word[i],comm); i++; //if(i == num_words) } comm->u.word[num_words] = NULL; push(operands,comm); token_now = token_now->next; break; case SUBSHELL: comm->type = SUBSHELL_COMMAND; // process subshell command tree token_stream_t subshell_token = convert_buffer_to_token_stream(token_now->text, strlen(token_now->text)); command_t subshell_command = make_command_tree(subshell_token); comm->u.subshell_command = subshell_command; // push SUBSHELL tree to operands push(operands, comm); token_now = token_now->next; break; case LEFT_DERCTION: // check that previous command is a subshell or word if (prev== NULL || !(prev->type == SIMPLE_COMMAND || prev->type == SUBSHELL_COMMAND)) { error(2, 0, "Line %d: Syntax error. ONLY words and SUBSHELL can follow redirection", line); return NULL; } /* else if (prev->output != NULL) { error(2, 0, "Line %d: Syntax error. Previous command already has output. ", line); return NULL; }*/ else if (prev->input != NULL) { error(2, 0, "Line %d: Syntax error. Previous command already has input.", line); return NULL; } token_now= token_now->next; if (token_now->t_type == WORD) // followed by a word { //which is valid input prev->input = token_now->text; // add_filename_to_dependlist(prev->input,comm); } else { error(2, 0, "Line %d: Syntax error. Redirects must be followed by words.", line); return NULL; } token_now= token_now->next; // no pushing required break; case RIGHT_DERCTION: // check that previous command is a subshell or word if (prev== NULL || !(prev->type == SIMPLE_COMMAND || prev->type == SUBSHELL_COMMAND)) { error(2, 0, "Line %d: Syntax error. ONLY words and SUBSHELL can follow redirection", line); return NULL; } else if (prev->output != NULL) { error(2, 0, "Line %d: Syntax error. Previous command already has output. ", line); return NULL; } /*else if (prev->input != NULL) { //error(2, 0, "Line %d: Syntax error. Previous command already has input.", line); return NULL; }*/ token_now= token_now->next; if (token_now->t_type == WORD) // followed by a word { //which is valid output prev->output = token_now->text; //add output word to outfile_list outfile_t new_outfile = add_out_list (prev); prev->outfile_ptr = new_outfile; } else { error(2, 0, "Line %d: Syntax error. Redirects must be followed by words.", line); return NULL; } // no pushing required token_now= token_now->next; break; default: token_now = token_now->next; break; }; prev = comm; // printf("the previous command is: %d\n", prev->type); } //printf("end first while"); while(size(operators) > 0) { command_t pop_ops = pop(operators); if (!make_command_branch(pop_ops, operands)) { error(2, 0, "Line %d: Syntax error. Not enough children to create new tree.", line); return NULL; } } // check for single root if (size(operands) != 1) { error(2, 0, "Line %d: Syntax error. Tree did not converge into single root.", line); return NULL; } command_t root = pop(operands); // the root should be the final tree left in operands return root; }