/* Construct command */ command* construct_command(char** tokens) { /* Initialize a new command */ command *cmd = malloc(sizeof(command)); cmd->cmd1 = cmd->cmd2 = NULL; cmd->scmd = NULL; if (!is_complex_command(tokens)) { /* Simple command */ cmd->scmd = malloc(sizeof(simple_command)); if (is_builtin(tokens[0])) { cmd->scmd->builtin = 1; cmd->scmd->tokens = tokens; } else { cmd->scmd->builtin = 0; int err = extract_redirections(tokens, cmd->scmd); if (err == -1) { printf("Error extracting redirections!\n"); return NULL; } } } else { /* Complex command */ char **t1 = tokens, **t2; int i = 0; while(tokens[i]) { if(is_operator(tokens[i])) { strncpy(cmd->oper, tokens[i], 2); tokens[i] = 0; t2 = &(tokens[i+1]); break; } i++; } /* Recursively construct the rest of the commands */ cmd->cmd1 = construct_command(t1); cmd->cmd2 = construct_command(t2); } return cmd; }
boost::filesystem::path CMDGraphRenderer::render_graph(const path& src_path, const path& dst_path, const std::string& name, const std::string& img_ext, const std::string& engine) { assert(boost::filesystem::exists(src_path) && boost::filesystem::is_regular_file(src_path) && "ERROR: CMDGraphRenderer: source path does not exist or is not a file"); std::string command; std::string image_path; construct_command(src_path, dst_path, name, img_ext, engine, command, image_path); system(command.c_str()); return image_path; }
int main(int argc, char** argv) { char cwd[MAX_DIRNAME]; /* Current working directory */ char command_line[MAX_COMMAND]; /* The command */ char *tokens[MAX_TOKEN]; /* Command tokens (program name, * parameters, pipe, etc.) */ while (1) { /* Display prompt */ getcwd(cwd, MAX_DIRNAME-1); printf("%s> ", cwd); /* Read the command line */ fgets(command_line, MAX_COMMAND, stdin); /* Strip the new line character */ if (command_line[strlen(command_line) - 1] == '\n') { command_line[strlen(command_line) - 1] = '\0'; } /* Parse the command into tokens */ parse_line(command_line, tokens); /* Check for empty command */ if (!(*tokens)) { continue; } /* Construct chain of commands, if multiple commands */ command *cmd = construct_command(tokens); // print_command(cmd, 0); int exitcode = 0; if (cmd->scmd) { exitcode = execute_simple_command(cmd->scmd); if (exitcode == -1) { release_command(cmd); break; } } else { exitcode = execute_complex_command(cmd); if (exitcode == -1) { release_command(cmd); break; } } release_command(cmd); } return 0; }
int main(int argc, char** argv) { char cwd[MAX_DIRNAME]; /* Current working directory */ char command_line[MAX_COMMAND]; /* The command */ char *tokens[MAX_TOKEN]; /* Command tokens (program name, parameters, pipe, etc.) */ while (1) { /* Display prompt */ getcwd(cwd, MAX_DIRNAME-1); printf("%s> ", cwd); /* Read the command line */ gets(command_line); /* Parse the command into tokens */ parse_line(command_line, tokens); /* Empty command */ if (!(*tokens)) continue; /* Exit */ if (strcmp(tokens[0], "exit") == 0) exit(0); /* Construct chain of commands, if multiple commands */ command *cmd = construct_command(tokens); int exitcode = 0; if (cmd->scmd) { exitcode = execute_simple_command(cmd->scmd); if (exitcode == -1) break; } else { exitcode = execute_complex_command(cmd); if (exitcode == -1) break; } release_command(cmd); } return 0; }
command_t parse_command_string (char *comString, int line) { char *ii = comString; int endln = line; while (*ii) { if (*ii == '\n') endln++; ii++; } com_text_t comText = { .start = comString, .end = ii-1, .startln = line, .endln = endln }; return parse_ao_string(*strip_text(&comText)); } command_t parse_ao_string (com_text_t comText) { char *ii = comText.end; bool haveWord = false; int currln = comText.endln; int subDepth = 0; for ( ; ii >= comText.start; ii--) { // fprintf(stderr, "GOT HERE!"); if (*ii == '\n') currln--; if (*ii == ')') { subDepth++; haveWord = true; } if (*ii == '(') { subDepth--; if (subDepth < 0) { error(1, 0, "%d: unmatched '('", currln); } } if (subDepth != 0) continue; if (*ii == '&' && *(ii+1) == '&') { if (!haveWord) error(1, 0, "%d: expected command after '&&'", currln); *ii = 0; if (!anything_before(ii-1,comText.start)) error(1, 0, "%d: expected command before '&&'", currln); com_text_t subComms[2]= { { .start = comText.start, .end = ii-1, .startln = comText.startln, .endln = currln }, { .start = ii+2, .end = comText.end, .startln = currln, .endln = comText.endln } }; return construct_command(AND_COMMAND, NULL, NULL, parse_ao_string(*strip_text(&subComms[0])), parse_pipe_string(*strip_text(&subComms[1]))); } if (*ii == '|' && *(ii+1) == '|') { //fprintf(stderr, "GOT HERE!"); if (!haveWord) error(1, 0, "%d: expected command after '||'", currln); *ii = 0; if (!anything_before(ii-1,comText.start)) error(1, 0, "%d: expected command before '||'", currln); com_text_t subComms[2]= { { .start = comText.start, .end = ii-1, .startln = comText.startln, .endln = currln }, { .start = ii+2, .end = comText.end, .startln = currln, .endln = comText.endln } }; return construct_command(OR_COMMAND, NULL, NULL, parse_ao_string(*strip_text(&subComms[0])), parse_pipe_string(*strip_text(&subComms[1]))); }