Beispiel #1
0
void execute_one_command(void)
{
    int ret, i;
    char cmd_str[256] = "";

    if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
        cur_action = action_remove_queue_head();
        cur_command = NULL;
        if (!cur_action)
            return;
        INFO("processing action %p (%s)\n", cur_action, cur_action->name);
        cur_command = get_first_command(cur_action);
    } else {
        cur_command = get_next_command(cur_action, cur_command);
    }

    if (!cur_command)
        return;

    ret = cur_command->func(cur_command->nargs, cur_command->args);
    if (klog_get_level() >= KLOG_INFO_LEVEL) {
        for (i = 0; i < cur_command->nargs; i++) {
            strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
            if (i < cur_command->nargs - 1) {
                strlcat(cmd_str, " ", sizeof(cmd_str));
            }
        }
        INFO("command '%s' action=%s status=%d (%s:%d)\n",
             cmd_str, cur_action ? cur_action->name : "", ret, cur_command->filename,
             cur_command->line);
    }
}
Beispiel #2
0
void interactive(chaiscript::ChaiScript& chai)
{
  using_history();

  for (;;) {
    std::string input = get_next_command();
    try {
      // evaluate input
      chaiscript::Boxed_Value val = chai.eval(input);

      //Then, we try to print the result of the evaluation to the user
      if (!val.get_type_info().bare_equal(chaiscript::user_type<void>())) {
        try {
          std::cout << chai.eval<std::function<std::string(const chaiscript::Boxed_Value &bv)> >("to_string")(val) << std::endl;
        }
        catch (...) {} //If we can't, do nothing
      }
    }
    catch (const chaiscript::exception::eval_error &ee) {
      std::cout << ee.what();
      if (ee.call_stack.size() > 0) {
        std::cout << "during evaluation at (" << ee.call_stack[0]->start().line << ", " << ee.call_stack[0]->start().column << ")";
      }
      std::cout << std::endl;
    }
    catch (const std::exception &e) {
      std::cout << e.what();
      std::cout << std::endl;
    }
  }
}
Beispiel #3
0
/******************************************************************************
 *                                                                            *
 * Function: run_commands                                                     *
 *                                                                            *
 * Purpose: run remote commandlist for specific action                        *
 *                                                                            *
 * Parameters: trigger - trigger data                                         *
 *             action  - action data                                          *
 *                                                                            *
 * Author: Eugene Grigorjev                                                   *
 *                                                                            *
 * Comments: commands separated with newline                                  *
 *                                                                            *
 ******************************************************************************/
void	op_run_commands(char *cmd_list)
{
	DB_RESULT	result;
	DB_ROW		row;
	char		*alias, *alias_esc, *command;
	int		is_group;

	assert(cmd_list);

	zabbix_log(LOG_LEVEL_DEBUG, "In run_commands()");

	while (1 != get_next_command(&cmd_list, &alias, &is_group, &command))
	{
		if (!alias || *alias == '\0' || !command || *command == '\0')
			continue;

		if (is_group)
		{
			alias_esc = DBdyn_escape_string(alias);
			result = DBselect("select distinct h.host from hosts_groups hg,hosts h,groups g"
					" where hg.hostid=h.hostid and hg.groupid=g.groupid and g.name='%s'" DB_NODE,
					alias_esc,
					DBnode_local("h.hostid"));
			zbx_free(alias_esc);

			while (NULL != (row = DBfetch(result)))
				run_remote_command(row[0], command);

			DBfree_result(result);
		}
		else
			run_remote_command(alias, command);
	}
	zabbix_log( LOG_LEVEL_DEBUG, "End run_commands()");
}
int main(int /*argc*/, char * /*argv*/[]) {

  test myChai;


  std::string command = "";

  //
  // this loop increases memory usage, if RunFile is not called (just hitting enter)
  // as soon RunFile gets called, memory will be freed.
  //
  // scenario1 - RunFile gets called every Loop: memory usage does not change
  // scenario2 - RunFile gets never called (just hitting enter): memory usage increases every loop
  // scenario3 - RunFile gets in changing intervals: memory usage goes up and down, but never as
  //            low as in case 1 scenario3 :

  while(command != "quit")
  {
    for(int i = 1; i < 200; i++)
      myChai.ResetState();

    if(command == "runfile")
      myChai.RunFile("Test.chai");

    command = get_next_command();
  }
}
Beispiel #5
0
void loop_epoll(server_t *server)
{
    int epollfd = epoll_create1(0);
    if (epollfd == -1) err_exit("epoll_create1");

    add_to_epoll(epollfd, server->listen_fd);

    struct epoll_event *events = calloc(LOOP_EPOLL_MAXEVENTS, sizeof(struct epoll_event));

    while (!server->quit) {
        int n = epoll_wait(epollfd, events, LOOP_EPOLL_MAXEVENTS, -1);
        int i;
        for (i = 0; i < n; i++) {
            if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) {
                err_message("epoll error, closing %d\n", events[i].data.fd);
                close(events[i].data.fd);
                continue;
            }
            else if (server->listen_fd == events[i].data.fd) { /* incoming connections. */
                struct sockaddr addr;
                socklen_t in_len = sizeof addr;
                int newfd = accept(server->listen_fd, &addr, &in_len);
                printf("Accepted connection on descriptor %d ", newfd);
                add_to_epoll(epollfd, newfd);
            }
            else { /* incoming data */
                command_t command = get_next_command(events[i].data.fd);
                process_command(server, &command);
            }
        }
    }

    free(events);
    close(server->listen_fd);
}
void loop_fork_process(server_t *server)
{
    struct sigaction sa;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    sa.sa_handler = grim_reaper;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) syslog_exit("sigaction");

    while (1) {
        int cfd = accept(server->listen_fd, NULL, NULL);
        if (cfd == -1) syslog_exit("Failure in accept");

        switch (fork()) {
            case -1:
                syslog_nx("Can't create child");
                close(cfd);
                break;
            case 0: // this is child
                close(server->listen_fd);
                command_t command = get_next_command(cfd);
                process_command(server, &command);
                _exit(EXIT_SUCCESS);
            default:
                close(cfd);
                break;
        }
    }
}
Beispiel #7
0
static void
getinput(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);
	if (global_event == NULL)
		global_event = event;
	while (in_use) {
		get_next_command();
		if (ISC_LIST_HEAD(lookup_list) != NULL) {
			start_lookup();
			return;
		}
	}
	isc_app_shutdown();
}
std::vector<std::string> FlotillaDock::get_pending_commands(void){
	int channel_idx;
	std::vector<std::string> commands;

	for (channel_idx = 0; channel_idx < MAX_CHANNELS; channel_idx++){

		if (module[channel_idx].state != ModuleConnected) continue;

		std::string command = get_next_command(channel_idx);

		if (!command.empty()){
			commands.push_back(command);
		}
		
	}

	return commands;
}
void execute_one_command(void)
{
    int ret;

    if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
        cur_action = action_remove_queue_head();
        cur_command = NULL;
        if (!cur_action)
            return;
        INFO("processing action %p (%s)\n", cur_action, cur_action->name);
        cur_command = get_first_command(cur_action);
    } else {
        cur_command = get_next_command(cur_action, cur_command);
    }

    if (!cur_command)
        return;

    ret = cur_command->func(cur_command->nargs, cur_command->args);
    INFO("command '%s' r=%d\n", cur_command->args[0], ret);
}
Beispiel #10
0
void execute_one_command(void)
{
    int ret;

    if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
        cur_action = action_remove_queue_head();
        cur_command = NULL;
        if (!cur_action)
            return;
#ifdef INIT_ENG_BUILD
        ERROR("processing action %p (%s)\n", cur_action, cur_action->name);
#else
        INFO("processing action %p (%s)\n", cur_action, cur_action->name);
#endif
        cur_command = get_first_command(cur_action);
    } else {
        cur_command = get_next_command(cur_action, cur_command);
    }

    if (!cur_command)
        return;

    ret = cur_command->func(cur_command->nargs, cur_command->args);
#ifdef INIT_ENG_BUILD
    if(cur_command->nargs > 3)
    ERROR("command '%s %s %s %s' r=%d\n", cur_command->args[0], cur_command->args[1], cur_command->args[2], cur_command->args[3], ret);
    else if(cur_command->nargs > 2)
    ERROR("command '%s %s %s' r=%d\n", cur_command->args[0], cur_command->args[1], cur_command->args[2], ret);
    else if(cur_command->nargs > 1)
    ERROR("command '%s %s' r=%d\n", cur_command->args[0], cur_command->args[1], ret);
    else
    ERROR("command '%s' r=%d\n", cur_command->args[0], ret);
#else
    INFO("command '%s' r=%d\n", cur_command->args[0], ret);
#endif
}
Beispiel #11
0
int get_next_command(DemoScript *ds, DemoCommand *cmd, unsigned long time) {
	char *ptr;
	char *cmd_tok;
	int i;
	
	/* get next line if one is available */
	if(ds->line_buffer[0] == 0) {
		if(!fgets(ds->line_buffer, BUF_LEN, ds->file)) {
			return EOF;
		}
		ds->line++;
	}

	ptr = skip_spaces(ds->line_buffer);

	/* skip comments and empty lines */
	if(*ptr == '#' || *ptr == '\n') {
		ds->line_buffer[0] = 0;
		return get_next_command(ds, cmd, time);
	}

	/* retrieve command time */
	cmd->time = atoi(ptr);
	
	/* skip timestamp and following whitespace */
	while(*ptr && *ptr != '\n' && (isdigit(*ptr) || isspace(*ptr) || (isdigit(*(ptr-1)) && *ptr == 's'))) {
		if(*ptr == 's') cmd->time *= 1000;
		ptr++;
	}
	if(!*ptr || *ptr == '\n') {
		fprintf(stderr, "Skipping invalid line %ld: %s\n", ds->line, ds->line_buffer);
		ds->line_buffer[0] = 0;
		return get_next_command(ds, cmd, time);
	}
	
	if(cmd->time > time) {
		return 1;	/* time is in the future */
	}

	/* seperate command name substring (cmd_tok), ptr keeps the rest */
	cmd_tok = ptr;
	while(*ptr && !isspace(*ptr)) ptr++;
	*ptr++ = 0;

	/* make the command name upper-case */
	for(i=0; cmd_tok[i]; i++) {
		cmd_tok[i] = toupper(cmd_tok[i]);
	}

	/* match the command string with the available commands */
	cmd->type = (CommandType)UINT_MAX;
	for(i=0; cmd_symb[i]; i++) {
		if(!strcmp(cmd_tok, cmd_symb[i])) {
			cmd->type = i;
			break;
		}
	}
	
	if(cmd->type == (CommandType)UINT_MAX) {
		fprintf(stderr, "Skipping invalid line %ld: Unrecognized command %s\n", ds->line, cmd_tok);
		ds->line_buffer[0] = 0;
		return get_next_command(ds, cmd, time);
	}

	/* tokenize the rest of the arguments and put them into argv */
	cmd_tok = ptr = skip_spaces(ptr);
	cmd->argc = *ptr ? 1 : 0;

	while(*ptr && *ptr != '\n') {
		if(isspace(*ptr)) {
			ptr = skip_spaces(ptr);
			if(*ptr && *ptr != '\n') cmd->argc++;
		} else {
			ptr++;
		}
	}
	
	cmd->argv = malloc((cmd->argc + 1) * sizeof(char*));
	for(i=0; i<cmd->argc; i++) {
		ptr = strtok(i ? 0 : cmd_tok, " \t\n");
		assert(ptr);

		cmd->argv[i] = malloc(strlen(ptr) + 1);
		strcpy((char*)cmd->argv[i], ptr);
	}
	cmd->argv[i] = 0;
	
	/*
	if(!*ptr || *ptr == '\n') {
		cmd->args = 0;
	} else {
		unsigned int len = strlen(ptr);
		cmd->args = malloc(len + 1);
		strcpy(cmd->args, ptr);
		if(cmd->args[len - 1] == '\n') {
			cmd->args[len - 1] = 0;
		}
	}
	*/
	ds->line_buffer[0] = 0;
	
	return 0;
}
Beispiel #12
0
static void get_command(char *p_buffer)
{
  unsigned char input = 0;
  int pos = 0;
  memset(p_buffer, 0, 256);

  do
  {
    input = console_get_char();
    //it's for secureCRT.
    //if(input == 10)
    //  continue;
    
    // add support for backspace key
    if(input == KEY_BACKSPACE) 
    {
      if(pos >= 1)
      {
        p_buffer[--pos] = '\0';
        Console_PutChar(input);
      }
    }
#ifdef ENABLE_CMD_HISTORY
    else if((input == KEY_PRE) || (input == KEY_NEXT))
    {
      char *tmp = NULL;
      
      if(input == KEY_PRE)
        tmp = get_pre_command();
      else
        tmp = get_next_command();
      if(tmp != NULL)
      {
        memset(p_buffer, 0, 256);
        memcpy(p_buffer, tmp, 256);

        for(; pos > 0; pos--)
          Console_PutChar(KEY_BACKSPACE);
        
        pos = strlen(p_buffer);
        OS_PRINTK("%s", p_buffer);
      }
      else
      {
        memset(p_buffer, 0, 256);
        for(; pos > 0; pos--)
          Console_PutChar(KEY_BACKSPACE);
        pos = 0;
      }
    }
#endif
    else
    {
      if(pos >= CU_MAX_STR_LENGTH)
      {
        OS_PRINTK("Stop. Exceed the input buffer!!!\n");
      }
      else
      {
          p_buffer[pos++] = input;
      }
    }
    TESTFM_ASSERT(pos != CU_MAX_STR_LENGTH);
  } while(input != 10 && input != 13);
  //} while(input != 13);

  p_buffer[pos-1] = '\0';
}
Beispiel #13
0
command_stream_t make_command(token_t t)
{
  token_command_t c = get_next_command(t);
  command_stream_t operators = checked_malloc(sizeof(struct command_stream));
  operators->head = NULL;
  operators->tail = NULL;
  command_stream_t commands = checked_malloc(sizeof(struct command_stream));
  commands->head = NULL;
  commands->tail = NULL;
  command_stream_t stream = checked_malloc(sizeof(struct command_stream));
  stream->head = NULL;
  stream->tail = NULL;
  while(t != NULL)
  {
    //printf("%s %d\n", t->str, t->type);
    if(t->type == WORD)
    {
      //printf("simple\n");
      insert(commands, c);
    }
    if(t->type == OPEN_PARENTHESIS)
    {
      //printf("open paranthesis\n");
      insert(operators, c);
    }
    if(t->type == CLOSE_PARENTHESIS)
    {
      //printf("close paranthesis\n");
      while(operators->head->type != OPEN_PARENTHESIS)
      {
        //printf("combine operators %d %d\n", t->type, operators->head->type);
        struct command_node *t2 = pop(commands);
        struct command_node *t1 = pop(commands);
        struct command_node *oper = pop(operators);
        oper->command->u.command[0] = t1->command;
        oper->command->u.command[1] = t2->command;
        //print_command(oper->command);
        oper->next = commands->head;
        commands->head = oper;
        if(operators->head == NULL)
          break;
      }
      pop(operators);
      command_t s = checked_malloc(sizeof(struct command_node));
      struct command_node *n = checked_malloc(sizeof(struct command_node));
      s->type = SUBSHELL_COMMAND;
      s->status = 1;
      s->input = NULL;
      s->output = NULL;
      //printf("command:\n");
      s->u.subshell_command = pop(commands)->command;
      n->type = SUBSHELL;
      n->command = s;
      n->next = commands->head;
      commands->head = n;
      //print_command(s);
      //print_command(commands->head->command);
    }
    if(t->type == PIPE || t->type == SEQUENCE || t->type == AND || t->type == OR)
    {
      //printf("operator\n");
      if(operators->head == NULL)
      {
        //printf("new operator stack %d\n", t->type);
        insert(operators, c);
      }
      else if(((int)t->type - (int)operators->head->type) > 1)
      {
        //printf("%d > %d\n", t->type, operators->head->type);
        insert(operators, c);
      }
      else
      {
        //printf("combine operators %d %d\n", t->type, operators->head->type);
        while(operators->head->type != OPEN_PARENTHESIS && ((int)t->type - (int)operators->head->type) <= 1)
        {
          //printf("combine operators %d %d\n", t->type, operators->head->type);
          struct command_node *t2 = pop(commands);
          struct command_node *t1 = pop(commands);
          struct command_node *oper = pop(operators);
          oper->command->u.command[0] = t1->command;
          oper->command->u.command[1] = t2->command;
          oper->next = commands->head;
          commands->head = oper;
          if(operators->head == NULL)
            break;
        }
        insert(operators, c);
      }
    }
    if(t->type == OPEN_ANGLE)
    {
      //printf("i operator\n");
      commands->head->command->input = t->next->str;
      t = t->next;
    }
    if(t->type == CLOSE_ANGLE)
    {
      //printf("o operator\n");
      commands->head->command->output = t->next->str;
      t = t->next;
    }
    if(t->type == NEWLINE)
    {
      if(operators->head == NULL)
      {
        //printf("no operator newline\n");
        struct command_node *n = checked_malloc(sizeof(struct command_node));
        n->command = commands->head->command;
        n->next = stream->head;
        stream->head = n;
      }
      else
      {
        //printf("operator newline\n");
        while(operators->head != NULL)
        {
          //printf("operators %d\n", operators->head->type);
          struct command_node *t2 = pop(commands);
          struct command_node *t1 = pop(commands);
          struct command_node *oper = pop(operators);
          oper->command->u.command[0] = t1->command;
          oper->command->u.command[1] = t2->command;
          oper->next = commands->head;
          commands->head = oper;
        }
        commands->head->next = stream->head;
        stream->head = commands->head;
        pop(commands);
      }
    }
    t = t->next;
    c = get_next_command(t);
  }
  if(operators->head != NULL)
  {
    while(operators->head != NULL)
    {
      //printf("combine last two operators %d\n", operators->head->type);
      if(operators->head->type == SEQUENCE && operators->head->next == NULL)
        break;
      struct command_node *t2 = pop(commands);
      struct command_node *t1 = pop(commands);
      struct command_node *oper = pop(operators);
      oper->command->u.command[0] = t1->command;
      oper->command->u.command[1] = t2->command;
      oper->next = commands->head;
      commands->head = oper;
    }
    commands->head->next = stream->head;
    stream->head = commands->head;
  }
  else if(commands->head != NULL)
  {
    commands->head->next = stream->head;
    stream->head = commands->head;
  }
  //printf("return stream\n");
  return stream;
}