Пример #1
0
cmdnode_t *command_lookup (char *command, char * (*args)[10], int *error_code) 
{
	unsigned int key = command[0];
	struct list_head  *pnode = NULL;
	struct list_head  *head = NULL;
	cmdnode_t *match = NULL, *cmd = NULL;
	int nref = 0;
	char * token[MAX_TOKENS];
	char tmp[MAX_CMD_NAME];
	cmd_t  *p = cmd_tree_walk (&cmd_root, key);

	if (!p) {
		*error_code = ERR_INVALID_COMMAND;
		return NULL;
	}

	memset (token, 0, sizeof(token));
	memset (tmp, 0, sizeof(tmp));

	head = &p->head;

	memcpy (tmp, command, strlen(command) + 1);

	build_token (command, &token);

	list_for_each (pnode, head) {

		cmd= list_entry (pnode, cmdnode_t, np);

		if (match_command (cmd, token, args)) {
			++nref;
			match = cmd;
		}
	}
Пример #2
0
console_cmd console_get_command_handler(const char *commandstr)
{
    const cmd *command = match_command(commandstr, CMD_AVAIL_NORMAL);

    if (command)
        return command->cmd_callback;
    else
        return NULL;
}
Пример #3
0
int
vt100_parser(struct cmdline_vt100 *vt, char ch)
{
	unsigned int size;
	uint8_t c = (uint8_t) ch;

	if (!vt)
		return -1;

	if (vt->bufpos >= CMDLINE_VT100_BUF_SIZE) {
		vt->state = CMDLINE_VT100_INIT;
		vt->bufpos = 0;
	}

	vt->buf[vt->bufpos++] = c;
	size = vt->bufpos;

	switch (vt->state) {
	case CMDLINE_VT100_INIT:
		if (c == 033) {
			vt->state = CMDLINE_VT100_ESCAPE;
		}
		else {
			vt->bufpos = 0;
			goto match_command;
		}
		break;

	case CMDLINE_VT100_ESCAPE:
		if (c == 0133) {
			vt->state = CMDLINE_VT100_ESCAPE_CSI;
		}
		else if (c >= 060 && c <= 0177) { /* XXX 0177 ? */
			vt->bufpos = 0;
			vt->state = CMDLINE_VT100_INIT;
			goto match_command;
		}
		break;

	case CMDLINE_VT100_ESCAPE_CSI:
		if (c >= 0100 && c <= 0176) {
			vt->bufpos = 0;
			vt->state = CMDLINE_VT100_INIT;
			goto match_command;
		}
		break;

	default:
		vt->bufpos = 0;
		break;
	}

	return -2;

 match_command:
	return match_command(vt->buf, size);
}
Пример #4
0
int console_run_command(const char *string)
{
	const cmd *command;

	ASSERT(string != NULL);

	command = match_command(string);
	if (!command)
		return -1;

	int result = command->cmd_callback(0, NULL);

	return result;
}
Пример #5
0
/* Execute a single command. */
static int
execute_command(int argc, char * const *argv)
{
	const command *c;
	int found = 0;

	/* Nothing to do. */
	if (argc == 0)
		return 0;

	for (c = commands; c->c_name; ++c)
	{
		if (match_command(c->c_name, argv[0]))
		{
			found = 1;
			break;
		}
	}

	if (found)
	{
		int result;

		/* Reset getopt for command proc. */
		optind = 1;
		optreset = 1;

		if (do_verbose)
		{
			int ix;

			fprintf(stderr, "%s", c->c_name);
			for (ix = 1; ix < argc; ++ix)
				fprintf(stderr, " \"%s\"", argv[ix]);
			fprintf(stderr, "\n");
		}

		result = c->c_func(argc, argv);
		if (result == 2)
			fprintf(stderr, "Usage: %s %s\n        %s\n", c->c_name, c->c_usage, c->c_help);

		return result;
	}
	else
	{
		sec_error("unknown command \"%s\"", argv[0]);
		return 1;
	}
}
struct command *
parse_command(int fd, int argc, char **argv)
{
	struct command	*start = first_level;
	struct command	*match = NULL;
	const char *errstring = "ERROR:\tvalid commands are:\n";

	while (argc >= 0) {
		match = match_command(start, *argv);
		if (match == NULL) {
			errstring = "ERROR:\tNo match found.\n";
			goto error3;
		}

		argc--;
		argv++;

		if (argc > 0 && match->next == NULL) {
			errstring = "ERROR:\textra arguments passed.\n";
			goto error3;
		}
		if (argc < 0 && match->type != NON) {
			if (match->next != NULL)
				start = match->next;
			errstring = "ERROR:\tincomplete command.\n";
			goto error3;
		}
		if (argc == 0 && *argv == NULL && match->type != NON) {
			if (match->next != NULL)
				start = match->next;
			errstring = "ERROR:\tincomplete command.\n";
			goto error3;
		}

		if ( match->next == NULL)
			break;

		start = match->next;	
	}

	return (match);
error3:
	write(fd, errstring, strlen(errstring));
	show_command_list(fd, start);
	return (NULL);
}
Пример #7
0
static int console_loop(void)
{
	cmd_args args[16];
	char buffer[256];

	printf("entering main console loop\n");

	for (;;) {
		puts("] ");

		int len = read_line(buffer, sizeof(buffer));
		if (len == 0)
			continue;

//		printf("line = '%s'\n", buffer);

		/* tokenize the line */
		int argc = tokenize_command(buffer, args, 16);
		if (argc < 0) {
			printf("syntax error\n");
			continue;
		} else if (argc == 0) {
			continue;
		}

//		printf("after tokenize: argc %d\n", argc);
//		for (int i = 0; i < argc; i++)
//			printf("%d: '%s'\n", i, args[i].str);

		/* convert the args */
		convert_args(argc, args);

		/* try to match the command */
		const cmd *command = match_command(args[0].str);
		if (!command) {
			printf("command not found\n");
			continue;
		}

		int result = command->cmd_callback(argc, args);
                return result;
		// XXX do something with the result
	}
}
Пример #8
0
/*
 * Runs the shell
 */
                void start_shell(){
                    const char *path[] = {
                        "./",
                        "/usr/bin/",
                        "/bin/",
                        NULL
                    };
                    char buff[PATH_BUFFER_SIZE];
                    char * cwd;
                    cwd = getcwd(buff,PATH_BUFFER_SIZE); 
                    bool running = true;
                    struct CommandBuffer cmb = command_buffer_factory();
                    while(running){
                        printf("%s > ",cwd);
                        get_command(&cmb,stdin);
                        running = match_command(&cmb,&cwd,path);
                    }
                    delete_command_buffer(&cmb);
                    printf("Exiting shell\n");
                }
Пример #9
0
void panic_shell_start(void)
{
    dprintf(INFO, "entering panic shell loop\n");
    char input_buffer[PANIC_LINE_LEN];
    cmd_args args[MAX_NUM_ARGS];

    // panic_fd allows us to do I/O using the polling drivers.
    // These drivers function even if interrupts are disabled.
    FILE _panic_fd = get_panic_fd();
    FILE *panic_fd = &_panic_fd;

    for (;;) {
        fputs("! ", panic_fd);
        read_line_panic(input_buffer, PANIC_LINE_LEN, panic_fd);

        int argc;
        char* tok = strtok(input_buffer, WHITESPACE);
        for (argc = 0; argc < MAX_NUM_ARGS; argc++) {
            if (tok == NULL) {
                break;
            }
            args[argc].str = tok;
            tok = strtok(NULL, WHITESPACE);
        }

        if (argc == 0) {
            continue;
        }

        convert_args(argc, args);

        const cmd* command = match_command(args[0].str, CMD_AVAIL_PANIC);
        if (!command) {
            fputs("command not found\n", panic_fd);
            continue;
        }

        command->cmd_callback(argc, args);
    }
}
Пример #10
0
/* The help command. */
static int
help(int argc, char * const *argv)
{
	const command *c;

	if (argc > 1)
	{
		char * const *arg;
		for (arg = argv + 1; *arg; ++arg)
		{
			int found = 0;

			for (c = commands; c->c_name; ++c)
			{
				if (match_command(c->c_name, *arg))
				{
					found = 1;
					break;
				}
			}

			if (found)
				printf("Usage: %s %s\n", c->c_name, c->c_usage);
			else
			{
				sec_error("%s: no such command: %s", argv[0], *arg);
				return 1;
			}
		}
	}
	else
	{
		for (c = commands; c->c_name; ++c)
			printf("    %-36s %s\n", c->c_name, c->c_help);
	}

	return 0;
}
Пример #11
0
static status_t command_loop(int (*get_line)(const char **, void *), void *get_line_cookie, bool showprompt, bool locked)
{
    bool exit;
#if WITH_LIB_ENV
    bool report_result;
#endif
    cmd_args *args = NULL;
    const char *buffer;
    const char *continuebuffer;
    char *outbuf = NULL;

    args = (cmd_args *) malloc (MAX_NUM_ARGS * sizeof(cmd_args));
    if (unlikely(args == NULL)) {
        goto no_mem_error;
    }

    const size_t outbuflen = 1024;
    outbuf = malloc(outbuflen);
    if (unlikely(outbuf == NULL)) {
        goto no_mem_error;
    }

    exit = false;
    continuebuffer = NULL;
    while (!exit) {
        // read a new line if it hadn't been split previously and passed back from tokenize_command
        if (continuebuffer == NULL) {
            if (showprompt)
                fputs("] ", stdout);

            int len = get_line(&buffer, get_line_cookie);
            if (len < 0)
                break;
            if (len == 0)
                continue;
        } else {
            buffer = continuebuffer;
        }

//      dprintf("line = '%s'\n", buffer);

        /* tokenize the line */
        int argc = tokenize_command(buffer, &continuebuffer, outbuf, outbuflen,
                                    args, MAX_NUM_ARGS);
        if (argc < 0) {
            if (showprompt)
                printf("syntax error\n");
            continue;
        } else if (argc == 0) {
            continue;
        }

//      dprintf("after tokenize: argc %d\n", argc);
//      for (int i = 0; i < argc; i++)
//          dprintf("%d: '%s'\n", i, args[i].str);

        /* convert the args */
        convert_args(argc, args);

        /* try to match the command */
        const cmd *command = match_command(args[0].str, CMD_AVAIL_NORMAL);
        if (!command) {
            if (showprompt)
                printf("command not found\n");
            continue;
        }

        if (!locked)
            mutex_acquire(command_lock);

        abort_script = false;
        lastresult = command->cmd_callback(argc, args);

#if WITH_LIB_ENV
        bool report_result;
        env_get_bool("reportresult", &report_result, false);
        if (report_result) {
            if (lastresult < 0)
                printf("FAIL %d\n", lastresult);
            else
                printf("PASS %d\n", lastresult);
        }
#endif

#if WITH_LIB_ENV
        // stuff the result in an environment var
        env_set_int("?", lastresult, true);
#endif

        // someone must have aborted the current script
        if (abort_script)
            exit = true;
        abort_script = false;

        if (!locked)
            mutex_release(command_lock);
    }

    free(outbuf);
    free(args);
    return NO_ERROR;

no_mem_error:
    if (outbuf)
        free(outbuf);

    if (args)
        free(args);

    dprintf(INFO, "%s: not enough memory\n", __func__);
    return ERR_NO_MEMORY;
}
Пример #12
0
void svr_ipcctrl::execute(void *sthread) {

	if ( sfd == -1 )
		return;

	int len;

	struct timeval last_action;
	gettimeofday(&last_action, NULL);

	send(sfd, hello, sizeof(hello), 0);

	while(sthread_isterminated(sthread) == 0) {
		eh_wait(eh, 1000000);

		if ( (len = recv(sfd, buffer, sizeof(buffer), 0)) != 0 ) {

			if ( len > 0 ) {

				buffer[255] = 0;

				if ( match_command(cmd_is_iodev_connected, len) ) {

					int UserID = 0;
					int DeviceID = 0;
					sscanf (&buffer[strlen(cmd_is_iodev_connected)], "%i,%i", &UserID, &DeviceID);

					if ( UserID
						 && DeviceID
						 && supla_user::is_device_online(UserID, DeviceID) ) {
						send_result("CONNECTED:",DeviceID);
					} else {
						send_result("DISCONNECTED:",DeviceID);
					}
				} else if ( match_command(cmd_user_reconnect, len) ) {

					int UserID = 0;
					sscanf (&buffer[strlen(cmd_user_reconnect)], "%i", &UserID);

					if ( UserID
						 && supla_user::reconnect(UserID) ) {
						send_result("OK:", UserID);
					} else {
						send_result("USER_UNKNOWN:", UserID);
					}
				} else if ( match_command(cmd_get_double_value, len) ) {

					int UserID = 0;
					int DeviceID = 0;
					int ChannelID = 0;
					double Value;

					sscanf (&buffer[strlen(cmd_get_double_value)], "%i,%i,%i", &UserID, &DeviceID, &ChannelID);

					if ( UserID
						 && DeviceID
						 && ChannelID
						 && supla_user::get_channel_double_value(UserID, DeviceID, ChannelID, &Value) ) {

						send_result("VALUE:", Value);

					} else {
						send_result("UNKNOWN:", ChannelID);
					}
				}


			}

		} else {
			sthread_terminate(sthread);
		}


		struct timeval now;
		gettimeofday(&now, NULL);

		if ( now.tv_sec-last_action.tv_sec >= 5 ) {
			sthread_terminate(sthread);
			break;
		}
	};

}
Пример #13
0
static void command_loop(int (*get_line)(const char **, void *), void *get_line_cookie, bool showprompt, bool locked)
{
	bool exit;
	bool report_result;
	cmd_args args[16];
	const char *buffer;
	const char *continuebuffer;
	char *outbuf;

	const size_t outbuflen = 1024;
	outbuf = malloc(outbuflen);

	exit = false;
	continuebuffer = NULL;
	while (!exit) {
		// read a new line if it hadn't been split previously and passed back from tokenize_command
		if (continuebuffer == NULL) {
			if (showprompt)
				puts("] ");

			int len = get_line(&buffer, get_line_cookie);
			if (len < 0)
				break;
			if (len == 0)
				continue;
		} else {
			buffer = continuebuffer;
		}

//		dprintf("line = '%s'\n", buffer);

		/* tokenize the line */
		int argc = tokenize_command(buffer, &continuebuffer, outbuf, outbuflen, args, 16);
		if (argc < 0) {
			if (showprompt)
				printf("syntax error\n");
			continue;
		} else if (argc == 0) {
			continue;
		}

//		dprintf("after tokenize: argc %d\n", argc);
//		for (int i = 0; i < argc; i++)
//			dprintf("%d: '%s'\n", i, args[i].str);

		/* convert the args */
		convert_args(argc, args);

		/* try to match the command */
		const cmd *command = match_command(args[0].str);
		if (!command) {
			if (showprompt)
				printf("command not found\n");
			continue;
		}

		if (!locked)
			mutex_acquire(command_lock);

		abort_script = false;
		lastresult = command->cmd_callback(argc, args);

#if WITH_LIB_ENV
		if ((env_get_bool("reportresult", &report_result, false) >= 0) &&
			(report_result))
		{
			if (lastresult < 0)
				printf("FAIL %d\n", lastresult);
			else
				printf("PASS %d\n", lastresult);
		}
#endif

#if WITH_LIB_ENV
		// stuff the result in an environment var
		env_set_int("?", lastresult, true);
#endif

		// someone must have aborted the current script
		if (abort_script)
			exit = true;
		abort_script = false;

		if (!locked)
			mutex_release(command_lock);
	}

	free(outbuf);
}
Пример #14
0
/* event parsing loop freely stolen from Zarf's glk example code */
void debug_monitor() {
    char commandbuf[256], lastcommand[256];
    char *cx, *cmd;
    int gotline, len, monitor, match;
    event_t ev;
    char *parser = "^([cfghlnoqsx])( +([0-9a-f]+))?";
    // char *parser = "\\([cghlnqsx]\\)\\( 0\\)?";
    char *matched;
    regex_t preg;
    size_t nmatch = 4;
    regmatch_t pmatch[4];
    
    monitor = TRUE;
    while(monitor) {
        glk_put_string("\nmonitor>");
        // if (cmd)
        //     strncpy(lastcommand, cmd, 256);
        glk_request_line_event(mainwin, commandbuf, 255, 0);
        gotline = FALSE;
        while (!gotline) {
            glk_select(&ev);
            if (ev.type == evtype_LineInput)
                gotline = TRUE;
        }

        len = ev.val1;
        commandbuf[len] = '\0';

        for (cx = commandbuf; *cx; cx++) { 
            *cx = glk_char_to_lower(*cx);
        }
        
        /* strip whitespace */
        for (cx = commandbuf; *cx == ' '; cx++) { };
        cmd = cx;
        for (cx = commandbuf+len-1; cx >= cmd && *cx == ' '; cx--) { };
        *(cx+1) = '\0';
        
        if (*cmd == '\0') {
            monitor = FALSE;
            continue;
        }
        
        if ((match = regcomp(&preg, parser, REG_EXTENDED | REG_ICASE)) != 0) {
            fatal_error("Bad regex\n");
        }

        if ((match = regexec(&preg, cmd, nmatch, pmatch, 0)) != 0) {
            glk_put_string("pardon? - try 'h' for help\n");
        } else {
            if(match_command("c")) {
                monitor = FALSE;
            } else if (match_command("f")) {
                match_args_and_call(debug_print_callstack, 0xffff)
            } else if (match_command("g")) {
                match_args_and_call(debug_print_global, 0xff)
            } else if (match_command("h")) {
                debug_print_help();
            } else if (match_command("l")) {
                match_args_and_call(debug_print_local, 0xff)
            } else if (match_command("n")) {
                monitor = FALSE;                
            } else if (match_command("o")) {
                match_args_and_call(debug_print_object, 1);         
            } else if (match_command("x")) {
                match_args_and_call(debug_print_memory, 0xdeadbeef)
            } else if (match_command("")) {
                match_args_and_call(debug_print_zstring, 0)
            } else if (match_command("q")) {
                fatal_error("Debugger terminated game.");
            } else if (match_command("s")) {
                debug_print_stack();
            } else if (match_command("x")) {
                match_args_and_call(debug_print_memory, 0xdeadbeef)
            }
        }
        regfree(&preg);
    }
}
Пример #15
0
void svr_ipcctrl::execute(void *sthread) {
  if (sfd == -1) return;

  int len;

  struct timeval last_action;
  gettimeofday(&last_action, NULL);

  send(sfd, hello, sizeof(hello), 0);

  while (sthread_isterminated(sthread) == 0) {
    eh_wait(eh, 1000000);

    if ((len = recv(sfd, buffer, sizeof(buffer), 0)) != 0) {
      if (len > 0) {
        buffer[255] = 0;

        if (match_command(cmd_channel_get_hivalue, len)) {
          char hi;
          int number = 255;
          sscanf(&buffer[strlen(cmd_channel_get_hivalue)], "%i", &number);

          if (channelio_get_hi_value(number, &hi) == 1) {
            send_result("HIVALUE:", hi == 1 ? 1 : 0);
          } else {
            send_result("ERR", number);
          }

        } else if (match_command(cmd_channel_get_type, len)) {
          int number = 255;
          sscanf(&buffer[strlen(cmd_channel_get_type)], "%i", &number);
          send_result("TYPE:", channelio_get_type(number));

        } else if (match_command(cmd_channel_set_hivalue, len)) {
          int hi = 0;
          int number = 255;
          unsigned int timems = 0;

          sscanf(&buffer[strlen(cmd_channel_set_hivalue)], "%i,%i,%i", &number,
                 &hi, &timems);

          if (channelio_set_hi_value(number, hi == 1 ? 1 : 0, timems) == 1) {
            send_result("OK");
          } else {
            send_result("ERR", number);
          }
        }
      }

    } else {
      sthread_terminate(sthread);
    }

    struct timeval now;
    gettimeofday(&now, NULL);

    if (now.tv_sec - last_action.tv_sec >= 5) {
      sthread_terminate(sthread);
      break;
    }
  }
}