int main (int argc, char *argv[]) { UI.DisplayHeader (HEADER1 NAME); COMMAND command (argc, argv); // Распарсить команду if (command.ok) // Если парсинг был удачен и можно выполнить команду PROCESS (&command, &UI); // Выполнить разобранную команду printf (command.ok? (command.list_cmd()? "" : "All OK\n") : "Error(s) found\n"); return command.ok? EXIT_SUCCESS : FREEARC_EXIT_ERROR; }
/* * start_cmd_processing - main command processing loop. * */ void pagentd_cli_start ( void ) { char *cmdline; COMMAND *theCmd; BOOL retb; char cmdl[100]; puts(menuMsg); /* * the main command loop. */ while (1) { cmdline = rl_gets(); strcpy(cmdl, cmdline); theCmd = find_command(cmdline); if (!theCmd) printf("%s: No such command!\n", cmdline); else { retb = theCmd->cmdHandler(cmdl); } } }
void QueueManager::QueueCommand(const COMMAND& cmd) { CheckService(); QString listNodes = NodesList(); QStringList nodes = listNodes.split(" ", QString::SkipEmptyParts); if (cmd.channel == '*' || cmd.unit == 0) { Logger::Log(INFO, "Incoming Generic Command %s", qPrintable(cmd.commandStr())); char nodeChannel = 'A'; int nodeUnit = 0; bool bMatch = false; for (int i=0; i<nodes.count(); i++) { X10::sourceToAddress(nodes[i], nodeChannel, nodeUnit); if ( ((cmd.channel == '*' ) || (nodeChannel == cmd.channel)) && ((cmd.unit == 0 ) || (nodeUnit == cmd.unit)) ) { // match desired pattern E0 * *2 among known containers QueueCommand(COMMAND(nodeChannel, nodeUnit, cmd.command, cmd.value)); bMatch = true; } } if (!bMatch) { Logger::Log(INFO, "No source matching %s in nodes list (%s)", qPrintable(cmd.address()), qPrintable(listNodes) ); } return; } Logger::Log(INFO, "Incoming Command %s", qPrintable(cmd.commandStr())); const QMutexLocker locker(&m_mutex); //check existing commmand if (config.SMART_QUEUE) { for (std::list<COMMAND*>::iterator itr = m_queueCommands.begin(); itr != m_queueCommands.end();) { COMMAND* cmdit = *itr; // same channel / unit ? if ((cmd.channel == cmdit->channel) && (cmd.unit == cmdit->unit)) { if (cmd.command == CMD_SET) { // SET erase all other command // Logger::Log(DEBUG, "Incoming SET %s CANCELLED queued command %s", qPrintable(cmd.commandStr()), qPrintable(cmdit->commandStr())); itr = m_queueCommands.erase(itr); continue; } else if (cmd.command == CMD_OFF) { // OFF erase all other command // Logger::Log(DEBUG, "Incoming Stop %s CANCELLED queued command %s", qPrintable(cmd.commandStr()), qPrintable(cmdit->commandStr())); itr = m_queueCommands.erase(itr); continue; } else if (cmd.command == CMD_BRIGHT || cmd.command == CMD_DIM) { if (cmdit->command != cmd.command) // reverse direction, remove both commands { // Logger::Log(DEBUG, "Incoming command %s CANCELLED queued command %s", qPrintable(cmd.commandStr()), qPrintable(cmdit->commandStr())); itr = m_queueCommands.erase(itr); return; } } } ++itr; } // end for } // end smart queue // enqueue m_queueCommands.push_front(new COMMAND(cmd)); queueNotEmpty.wakeOne(); }
int QueueManager::GetNextCommand(const COMMAND& setCommand, COMMAND** outputCmd) { *outputCmd = 0; QString address = setCommand.address(); if (address.isEmpty()) return 0; CheckContainerExist(address); int curValue = Containers[address]->Value(); if (setCommand.command != CMD_SET)/* || (curValue == setCommand.value))*/ return 0; // force repeat off if (setCommand.value == -1) { *outputCmd = new COMMAND(setCommand.channel, setCommand.unit, CMD_OFF, -1); return 1; } if (curValue == setCommand.value) { Logger::Log(ERROR, "GetNextCommand outputs 0 sub cmds"); return 0; } if (curValue == -1) { if (setCommand.value == MAX_VALUE) { *outputCmd = new COMMAND(setCommand.channel, setCommand.unit, CMD_ON, MAX_VALUE); return 1; } else { *outputCmd = new COMMAND(setCommand.channel, setCommand.unit, CMD_DIM, MAX_VALUE - 1); return 1 + MAX_VALUE - setCommand.value; } } else // already on { if (curValue > setCommand.value) { *outputCmd = new COMMAND(setCommand.channel, setCommand.unit, CMD_DIM, curValue - 1); return (curValue - setCommand.value); } else { // shortcut ? off - on - down(s) may be faster if ((setCommand.value - curValue) > (2 + MAX_VALUE - setCommand.value)) { *outputCmd = new COMMAND(setCommand.channel, setCommand.unit, CMD_OFF, -1); return (2 + MAX_VALUE - setCommand.value); } *outputCmd = new COMMAND(setCommand.channel, setCommand.unit, CMD_BRIGHT, curValue + 1); return (setCommand.value - curValue); } } // return 0; // nothing to do }
void QueueManager::run() { SetStatus(QM_STARTING); m_isRunning = true; m_ruleManager.Load(config.RULES_LOCATION); SetStatus(QM_LOADING); m_controllerLoaded = ControllerLoad(); int res = 0; // flush queue commented because of cmd line (will wake up queue) // m_queueCommands.clear(); struct x10_ha_command x10CmdRecv; Logger::Log(DEBUG, "** QueueManager Entering loop", qPrintable(StatusStr())); while (m_isRunning) { QMutexLocker locker(&m_mutex); unsigned int nbErrors = 0; // while nothing to send while (m_isRunning && !m_queueCommands.size()) { /// Read input. if (m_controllerLoaded) { res = m_X10Controller->x10_recv(&x10CmdRecv); if (res > 0) { nbErrors = 0; Logger::Log(INFO, "X10 command received on house %c unit %d, cmd:%s res:%d", house_code_to_char(x10CmdRecv.house), unit_code_to_int(x10CmdRecv.unit), cmd_code_to_str(x10CmdRecv.cmd), res); // emit CommandReceived(*cmd); QString address = QString("%1%2") .arg(house_code_to_char(x10CmdRecv.house)) .arg(unit_code_to_int(x10CmdRecv.unit)) .toUpper(); CheckContainerExist(address); switch (x10CmdRecv.cmd) { case X10CMD_ON: Containers[address]->SetOn(true); break; case X10CMD_OFF: Containers[address]->SetOn(false); break; case X10CMD_BRIGHT: Containers[address]->PushDirection(true); break; case X10CMD_DIM: Containers[address]->PushDirection(false); break; case X10CMD_UP: case X10CMD_RIGHT: case X10CMD_DOWN: case X10CMD_LEFT: break; case X10CMD_INVALID: break; } emit CommandReceived(); } else if (res && res != -110 /*&& res != -1*/) // first res is -1 { // Logger::Log(ERROR, "res = -1 !!", res); nbErrors++; Logger::Log(DEBUG, "ERROR: (%d)", res); if (nbErrors > config.MAX_ERRORS) break; } } queueNotEmpty.wait(&m_mutex, 1000); } if (nbErrors > config.MAX_ERRORS) // reload controller { ControllerUnload(); if (!config.RELOAD_ON_ERROR) { Logger::Log(ERROR, "X10 Interface died, quitting QueueManager thread"); break; } nbErrors = 0; ControllerLoad(); continue; } COMMAND* cmd = 0; if (m_isRunning && m_queueCommands.size()) // not supposed to be empty at this point { cmd = m_queueCommands.back(); // when receiving it, it is this engine role to figure out how if (cmd->command == CMD_SET) { COMMAND* intermCmd; int nbSteps = GetNextCommand(*cmd, &intermCmd); // unique step or nothing to do if (nbSteps <= 1) m_queueCommands.remove(cmd); if (nbSteps) { if (!cmd->notified) { emit CommandCompleted(*cmd); cmd->notified = true; } intermCmd->notified = true; // not to be resent by subcmd } // Logger::Log(DEBUG, "** SET to %d produced %d steps", cmd->value, nbSteps); cmd = intermCmd; // null if nothing to do } else { // Logger::Log(DEBUG, "** SEND command"); m_queueCommands.remove(cmd); // 1 step per command } } locker.unlock(); if (!cmd) continue; // end marker x10_ha_command* x10cmd = new_x10_ha_command(parse_cmd_code(cmd->command), cmd->channel, cmd->unit); if (x10cmd) // else params out of bound { QString address = cmd->address(); CheckContainerExist(address); // for read int currentValue = Containers[address]->Value(); switch (cmd->command) { case CMD_SET: Containers[address]->SetValue(cmd->value); break; case CMD_ON: Containers[address]->SetValue(currentValue < 0 ? MAX_VALUE : currentValue); break; case CMD_OFF: Containers[address]->SetValue(-1); break; case CMD_DIM: if (currentValue == -1) currentValue = MAX_VALUE; Containers[address]->SetValue(currentValue ? currentValue - 1 : 0); break; case CMD_BRIGHT: if (currentValue == -1) currentValue = MAX_VALUE; Containers[address]->SetValue(currentValue >= MAX_VALUE ? MAX_VALUE : currentValue + 1); break; default: break; } if (m_controllerLoaded) { // emit CommandCompleted if not SET if (!cmd->notified) emit CommandCompleted(*cmd); m_X10Controller->x10_send(x10cmd); } else { Logger::Log(ERROR, "NO Controller to send Command %s", qPrintable(cmd->commandStr())); } del_x10_ha_command(x10cmd); } delete cmd; } ControllerUnload(); SetStatus(QM_STOPPED); }
void console_execute_line_stroked(int stroke, const char *str, int cid) { PARSE_RESULT result; COMMAND *command; char strokestr[2] = {'0', 0}; if(stroke) strokestr[0] = '1'; while(str) { const char *end = str; const char *next_part = 0; int in_string = 0; while(*end) { if(*end == '"') in_string ^= 1; else if(*end == '\\') /* escape sequences */ { if(end[1] == '"') end++; } else if(!in_string) { if(*end == ';') /* command separator */ { next_part = end+1; break; } else if(*end == '#') /* comment, no need to do anything more */ break; } end++; } if(console_parse_start(&result, str, (end-str) + 1) != 0) return; command = console_find_command(result.command); if(command) { int is_stroke_command = 0; if(result.command[0] == '+') { /* insert the stroke direction token */ result.args[result.num_args] = strokestr; result.num_args++; is_stroke_command = 1; } if(stroke || is_stroke_command) { if(console_parse_args(&result, command->params)) { char buf[256]; str_format(buf, sizeof(buf), "Invalid arguments... Usage: %s %s", command->name, command->params); server_send_rcon_line(cid, buf); } else command->callback(&result, command->user_data, cid); } } else { char buf[256]; str_format(buf, sizeof(buf), "No such command: %s.", result.command); server_send_rcon_line(cid, buf); } str = next_part; } }
int main(int argc, char *argv[]) { char *ip, *cp; char ttybuf[TTYLEN], cmd[CMDLEN]; COMMAND *cmdp; int i; config.gpibPresent = 1; gpibDebug = 0; vector_voltmeter.address = HP8508; vector_voltmeter2.address = HP8508+1; for (i=1; i<argc; i++) { if (present(argv[i],"-a")) { if (++i < argc) { sscanf(argv[i],"%d",&vector_voltmeter.address); } else { vvUsage(argv[0]); } } if (present(argv[i],"-b")) { if (++i < argc) { sscanf(argv[i],"%d",&vector_voltmeter2.address); } else { vvUsage(argv[0]); } } if (present(argv[i],"-h")) { vvUsage(argv[0]); } if (present(argv[i],"-d")) { gpibDebug = 1; } } readline_initialize_everything(); read_history(HIST_FILE); vector_voltmeter.present = 1; vector_voltmeter2.present = 1; /* find controller and setup the GPIB bus */ #if __Lynx__ brd0 = call_ibfind(GPIBDEVICE); #if !__powerpc__ call_ibsic(brd0); call_ibsre(brd0,1); #endif #endif /* open up the device */ WARN("opening GPIB device at address = %d....\n", vector_voltmeter.address); vector_voltmeter.dd = setupGPIBdevice(vector_voltmeter.address); if (vector_voltmeter.dd < 0) { WARN("Failed\n"); exit(0); } else { WARN("Succeeded. Descriptor = %d\n",vector_voltmeter.dd); clearVectorVoltmeter(vector_voltmeter.dd); } WARN("opening GPIB device at address = %d....\n", vector_voltmeter2.address); vector_voltmeter2.dd = setupGPIBdevice(vector_voltmeter2.address); if (vector_voltmeter2.dd < 0) { WARN("Failed\n"); exit(0); } else { WARN("Succeeded. Descriptor = %d\n",vector_voltmeter2.dd); clearVectorVoltmeter(vector_voltmeter2.dd); } whichVVMdd = vector_voltmeter.dd; whichVVM = 1; /* Now the main program is a loop, reading and executing commands */ for (;;) { sprintf(ttybuf,"vectorVoltmeter%d@addr%d> ",whichVVM, addressVVM(whichVVMdd)); ip = readline(ttybuf); add_history(ip); if (ip[0] == '!') { system(++ip); continue; } while((*ip == ' ' || *ip == '\t') && *ip != '\n') ip++; for(cp = cmd; cp < &cmd[CMDLEN]; cp++) { if((*cp = *ip++) == ' ' || *cp == ',' || *cp == '\t' || *cp == '\n' || *cp == 0) break; } *cp = 0; if (cp >= &cmd[CMDLEN]) { WARN( "cmd %s too long\n", cmd); continue; } if (cmd[0] == 0) continue; /* 'ip' now contains only the arguments to the command */ for (cmdp = cmds; cmdp < &cmds[NUMCMDS]; cmdp++) { /* look up the command */ if(*cmd == *cmdp->cmd && strcmp(cmd + 1, cmdp->cmd + 1) == 0) { cmdp->sub(ip); break; } } /* end of 'for' loop */ if (cmdp >= &cmds[NUMCMDS]) { WARN("Unrecognized cmd = %s\n", cmd); } #if __Lynx__ free(ip); #endif } /* end of infinite 'for' command loop */ call_ibloc(vector_voltmeter.dd); call_ibloc(vector_voltmeter2.dd); write_history(HIST_FILE); return(0); }
// p is a malloc'ed buffer // int handle_command(char* p) { char cmd[256]; int id; sscanf(p, "%s", cmd); if (!strcasecmp(cmd, "VERSION")) { printf("S "); print_version(); } else if (!strcasecmp(cmd, "COMMANDS")) { printf("S ASYNC_MODE_OFF ASYNC_MODE_ON BOINC_ABORT_JOBS BOINC_FETCH_OUTPUT BOINC_PING BOINC_QUERY_BATCHES BOINC_RETIRE_BATCH BOINC_SELECT_PROJECT BOINC_SUBMIT COMMANDS QUIT RESULTS VERSION\n"); } else if (!strcasecmp(cmd, "RESPONSE_PREFIX")) { printf("S\n"); strcpy(response_prefix, p+strlen("RESPONSE_PREFIX ")); } else if (!strcasecmp(cmd, "ASYNC_MODE_ON")) { printf("S\n"); } else if (!strcasecmp(cmd, "ASYNC_MODE_OFF")) { printf("S\n"); } else if (!strcasecmp(cmd, "QUIT")) { exit(0); } else if (!strcasecmp(cmd, "RESULTS")) { printf("S %d\n", n_results()); vector<COMMAND*>::iterator i = commands.begin(); while (i != commands.end()) { COMMAND *c2 = *i; if (c2->out) { printf("%s%d %s\n", response_prefix, c2->id, c2->out); free(c2->out); free(c2->in); free(c2); i = commands.erase(i); } else { i++; } } } else if (!strcasecmp(cmd, "BOINC_SELECT_PROJECT")) { int n = sscanf(p, "%s %s %s", cmd, project_url, authenticator); if (n ==3) { printf("S\n"); } else { printf("E\n"); } } else { // asynchronous commands go here // COMMAND *cp = new COMMAND(p); int retval = cp->parse_command(); if (retval) { printf("E\n"); delete cp; return 0; } if (debug_mode) { handle_command_aux(cp); printf("result: %s\n", cp->out); delete cp; } else { printf("S\n"); commands.push_back(cp); pthread_t thread_handle; pthread_attr_t thread_attrs; pthread_attr_init(&thread_attrs); pthread_attr_setstacksize(&thread_attrs, 32768); int retval = pthread_create( &thread_handle, &thread_attrs, &handle_command_aux, cp ); if (retval) { fprintf(stderr, "can't create thread\n"); return -1; } } } return 0; }
// p is a malloc'ed buffer // int handle_command(char* p) { char cmd[256]; int id; cmd[0] = '\0'; sscanf(p, "%s", cmd); if (!strcasecmp(cmd, "VERSION")) { print_version(false); } else if (!strcasecmp(cmd, "COMMANDS")) { BPRINTF("S ASYNC_MODE_OFF ASYNC_MODE_ON BOINC_ABORT_JOBS BOINC_FETCH_OUTPUT BOINC_PING BOINC_QUERY_BATCHES BOINC_RETIRE_BATCH BOINC_SELECT_PROJECT BOINC_SET_LEASE BOINC_SUBMIT COMMANDS QUIT RESULTS VERSION\n"); } else if (!strcasecmp(cmd, "RESPONSE_PREFIX")) { flockfile(stdout); BPRINTF("S\n"); strlcpy(response_prefix, p+strlen("RESPONSE_PREFIX "), sizeof(response_prefix)); funlockfile(stdout); } else if (!strcasecmp(cmd, "ASYNC_MODE_ON")) { flockfile(stdout); BPRINTF("S\n"); async_mode = true; funlockfile(stdout); } else if (!strcasecmp(cmd, "ASYNC_MODE_OFF")) { flockfile(stdout); BPRINTF("S\n"); async_mode = false; funlockfile(stdout); } else if (!strcasecmp(cmd, "QUIT")) { exit(0); } else if (!strcasecmp(cmd, "RESULTS")) { flockfile(stdout); int cnt = n_results(); BPRINTF("S %d\n", cnt); vector<COMMAND*>::iterator i = commands.begin(); int j = 0; while (i != commands.end() && j < cnt) { COMMAND *c2 = *i; if (c2->out) { BPRINTF("%d %s\n", c2->id, c2->out); delete c2; i = commands.erase(i); j++; } else { ++i; } } wrote_r = false; funlockfile(stdout); } else if (!strcasecmp(cmd, "BOINC_SELECT_PROJECT")) { int n = sscanf(p, "%s %s %s", cmd, project_url, authenticator); if (n ==3) { BPRINTF("S\n"); } else { BPRINTF("E\n"); } } else { // asynchronous commands go here // COMMAND *cp = new COMMAND(p); p = NULL; int retval = cp->parse_command(); if (retval) { BPRINTF("E\n"); delete cp; return 0; } if (debug_mode) { handle_command_aux(cp); BPRINTF("result: %s\n", cp->out); delete cp; } else { printf("S\n"); commands.push_back(cp); pthread_t thread_handle; pthread_attr_t thread_attrs; pthread_attr_init(&thread_attrs); pthread_attr_setstacksize(&thread_attrs, 256*1024); int retval = pthread_create( &thread_handle, &thread_attrs, &handle_command_aux, cp ); if (retval) { fprintf(stderr, "can't create thread\n"); return -1; } } } free(p); return 0; }