int command_type() { if(*current_command == '@') { current_command_type = A_COMMAND; return A_COMMAND; } else if(search_command(current_command, '(') > 0 || search_command(current_command, ')') > 0) { current_command_type = L_COMMAND; return L_COMMAND; } else if(!isdigit(*current_command)){ current_command_type = C_COMMAND; return C_COMMAND; } else if(isdigit(*current_command)){ current_command_type = C_COMMAND; return C_COMMAND; } else { int i = find_line_num(); line_notification(i); exit_error(7, "Incorrect Command Type"); return -1; } }
//========================================================================================================== // Go over the file line by line. If you see control flow keywords (if, else, etc.) call the appropriate // handler. Otherwise check if the line contains a command call, and if so execute it. //========================================================================================================== void ScriptReader::interpret(string filename) { string filepath = MServer::inst.get_value(SCENARIO_DIR) + "/" + filename; ifstream file(filepath); if(!file.is_open()) { throw string("File " + filepath + " not found"); } //------------------------------------------------------------------------------------------------------ // Go over the lines of the file and execute commands //------------------------------------------------------------------------------------------------------ for(string line; getline(file, line);) { trim(line); if(line.empty()) { continue; } search_command(line, file); } //------------------------------------------------------------------------------------------------------ // Check that control flow structures (if-else) are closed properly //------------------------------------------------------------------------------------------------------ bool non_closed_if = !if_stack.empty(); while(!if_stack.empty()) { delete if_stack.top(); if_stack.pop(); } if(non_closed_if) { throw string("If statement not closed"); } }
int parse_and_execute (char *line) { char *cmd = NULL; char * args [MAX_TOKENS]; cmdnode_t *cmd_node = NULL; int error_code = -1; if (!(cmd = check_alias (line))) { cmd = line; } if (!(cmd_node = search_command (cmd, &args, &error_code))) { write_string (cli_error[error_code]); return -1; } if (!(get_curr_priv_level () & cmd_node->priv_mode)) { write_string (cli_error[ERR_INVALID_COMMAND]); return 0; } execute_cmd (cmd_node, args); return 0; }
void system(const char * cmdline) { struct command_t * cmd; char **args; char *p, *buf, *pos; size_t len; int n, ret; if(!cmdline) return; len = strlen(cmdline); buf = malloc(len + 2); if(!buf) return; memcpy(buf, cmdline, len); memcpy(buf + len, " ", 2); p = buf; while(*p) { if(parser(p, &n, &args, &pos)) { if(n > 0) { cmd = search_command(args[0]); if(cmd) { ret = cmd->exec(n, args); if(ret != 0) { /* * if having other command which waitting be exec, abort. */ if(pos) { printf(" when exec \'%s\' command return an error code (%ld).\r\n", args[0], ret); free(args[0]); free(args); break; } } } else { printf(" could not found \'%s\' command \r\n", args[0]); printf(" if you want to kown available commands, type 'help'.\r\n"); /* * if having other command which waitting be exec, abort. */ if(pos) { free(args[0]); free(args); break; } } } free(args[0]); free(args); } if(!pos) *p = 0; else p = pos; } free(buf); }