command_t make_compound_command(char *buffer, enum command_type type, command_t caller) { command_t compound_command = checked_malloc(sizeof(struct command)); compound_command->type = type; compound_command->status = -1; if(caller == NULL) compound_command->u.command[0] = make_simple_command(buffer); else if(caller->type == SUBSHELL_COMMAND || (type != PIPE_COMMAND && caller->type == PIPE_COMMAND) || (type == PIPE_COMMAND) == (caller->type == PIPE_COMMAND)) compound_command->u.command[0] = caller; else if(type == PIPE_COMMAND && caller->type != PIPE_COMMAND) compound_command->u.command[0] = caller->u.command[1]; enum command_type adjacent_type = scan(buffer); if(adjacent_type == SIMPLE_COMMAND || adjacent_type == SEQUENCE_COMMAND) { compound_command->u.command[1] = make_simple_command(buffer); return compound_command; } else if(adjacent_type == SUBSHELL_COMMAND) { command_t subshell = make_subshell_command(buffer); adjacent_type = scan(buffer); if(adjacent_type == SIMPLE_COMMAND) { compound_command->u.command[1] = subshell; return compound_command; } else if(type != PIPE_COMMAND && adjacent_type == PIPE_COMMAND) { compound_command->u.command[1] = subshell; compound_command->u.command[1] = make_compound_command(buffer, adjacent_type, compound_command); return compound_command; } else { compound_command->u.command[1] = subshell; command_t adjacent_command = make_compound_command(buffer, adjacent_type, compound_command); return adjacent_command; } } else if(type != PIPE_COMMAND && adjacent_type == PIPE_COMMAND) { compound_command->u.command[1] = make_simple_command(buffer); compound_command->u.command[1] = make_compound_command(buffer, adjacent_type, compound_command); return compound_command; } else { compound_command->u.command[1] = make_simple_command(buffer); command_t adjacent_command = make_compound_command(buffer, adjacent_type, compound_command); return adjacent_command; } }
command_t make_command(char* buffer, enum command_type type) { if(type == SIMPLE_COMMAND) return make_simple_command(buffer); else if(type == SUBSHELL_COMMAND) { command_t subshell = make_subshell_command(buffer); type = scan(buffer); if(type == SIMPLE_COMMAND) return subshell; else return make_compound_command(buffer, type, subshell); } else return make_compound_command(buffer, type, NULL); }
void make_command(token_t *token_array, command_t* out_cmd) { int i = 0; stack operator_stack= create_stack(); // printf("%lu\n", operator_stack); // printf("%lu\n", operator_stack); stack command_stack = create_stack(); // printf("%lu\n", command_stack); while (token_array[i]->line_num != -1) { char *token_origin = token_array[i]->word; char *token = eat_tail_white_space(token_origin); if (!strcmp(token, "&&") || !strcmp(token, "||") || !strcmp(token, "|") || !strcmp(token, ";") || !strcmp(token, "<") || !strcmp(token, ">") || !strcmp(token, "(") || !strcmp(token, ")")) { if (!strcmp(token, "<")) { char *word = eat_tail_white_space(token_array[i+1]->word); command_t tmp_cmd = (command_t)peer(command_stack); tmp_cmd->input = word; // free(word); i+=2; continue; } else if (!strcmp(token, ">")) { char *word = eat_tail_white_space(token_array[i+1]->word); command_t tmp_cmd = (command_t)peer(command_stack); tmp_cmd->output = word; // free(word); i+=2; continue; } if (!strcmp(token, ")")) { char *oper = (char*)pop(operator_stack); while (strcmp(oper, "(")) { pop(operator_stack); } command_t sub_cmd = checked_malloc(sizeof(struct command)); sub_cmd->line = token_array[0]->line_num; command_t subshell = (command_t)pop(command_stack); make_subshell_command(subshell, sub_cmd); push(sub_cmd, command_stack); } else if (is_empty(operator_stack)) push(token, operator_stack); else { char *oper = (char*)peer(operator_stack); while (strcmp(token, "(") && oper_cmp(token, oper) <= 0 && !is_empty(operator_stack)) { oper = (char*)pop(operator_stack); command_t cmd2 = (command_t)pop(command_stack); command_t cmd1 = (command_t)pop(command_stack); command_t new_cmd = checked_malloc(sizeof(struct command)); make_compound_command(cmd1, cmd2, oper, new_cmd); new_cmd->line = token_array[0]->line_num; push(new_cmd, command_stack); if (!is_empty(operator_stack)) oper = (char*)peer(operator_stack); } push(token, operator_stack); } // if (!strcmp(token, "&&")) } else { // command_t cmd = checked_malloc(sizeof(struct command)); if (i > 0 && !strcmp(token_array[i - 1]->word, "(")) { token_t *sub_token_array = checked_malloc(sizeof(struct token) * 128); tokenize(token, sub_token_array, token_array[i]->line_num); command_t cmd[1]; make_command(sub_token_array, cmd); cmd[0]->line = token_array[0]->line_num; push(cmd[0], command_stack); } else { command_t cmd = checked_malloc(sizeof(struct command)); cmd->line = token_array[0]->line_num; make_simple_command(token, cmd); push(cmd, command_stack); } // push(cmd, command_stack); } i++; } if (is_empty(operator_stack)) { out_cmd[0] = (command_t)pop(command_stack); out_cmd[0]->line = token_array[0]->line_num; } else { while (!is_empty(operator_stack)) { char *oper = (char*)pop(operator_stack); command_t cmd2 = (command_t)pop(command_stack); command_t cmd1 = (command_t)pop(command_stack); out_cmd[0] = checked_malloc(sizeof(struct command)); out_cmd[0]->line = token_array[0]->line_num; make_compound_command(cmd1, cmd2, oper, out_cmd[0]); push(out_cmd[0], command_stack); } } // free(operator_stack); // free(command_stack); }