Esempio n. 1
0
bool
bot_main_loop(struct bot *bot)
{
	struct command_ctx ctx;

	if (bot_hand_shake(bot) == false)
		return false;

	do {
		if (bot_get_next_cmd(bot, &ctx) == false) {
			DEBUG(LOG_DEFAULT, "sock error\n");
			goto err;
		}
		if (ctx.cmd == CMD_UNKNOWN)
			goto err;
		command_run(bot, &ctx);
		//need check connection
		DEBUG(LOG_VERBOSE, "next main loop iter\n");
	} while (ctx.cmd != CMD_BYE && bot->socket->is_closed != 1);


	return true;
err:
	return false;
}
Esempio n. 2
0
static void acm_parse(int32_t _amt)
{
	int i = 0;
	char **argv;
	int argc;
	char scratch[ACM_BUFFER_SIZE];

	for(; i < _amt; i++)
	{
		if(acm_recv_buffer[i] == '\n'
				|| acm_recv_buffer[i] == '\r')
		{
			_amt = i;
			break;
		}
	}

	acm_recv_buffer[_amt] = 0;

	memcpy(scratch, acm_recv_buffer, _amt+1);
	argv = command_parse(scratch, &argc);

	if(argc >= 3 && strcmp(argv[0], "sendfile") == 0)
	{
		acm_busy = TRUE;

		acm_file_ptr = (char*)parseNumber(argv[1]);
		acm_file_recv_left = parseNumber(argv[2]);
		received_file_size = acm_file_recv_left;
		bufferPrintf("ACM: Started receiving file at 0x%08x - 0x%08x (%d bytes).\n", acm_file_ptr, acm_file_ptr + acm_file_recv_left, acm_file_recv_left);
	}
	else if(argc >= 3 && strcmp(argv[0], "recvfile") == 0)
	{
		acm_busy = TRUE;

		acm_file_ptr = (char*)parseNumber(argv[1]);
		acm_file_send_left = parseNumber(argv[2]);

		bufferPrintf("ACM: Started sending file at 0x%08x - 0x%08x (%d bytes).\n", acm_file_ptr, acm_file_ptr + acm_file_send_left, acm_file_send_left);

		int amt = sprintf(acm_send_buffer, "ACM: Starting File: %d %d\n", (uint32_t)acm_file_ptr, acm_file_send_left);
		usb_send_bulk(ACM_EP_SEND, acm_send_buffer, amt);
	}
	else
	{
		bufferPrintf("ACM: Starting %s\n", acm_recv_buffer);
		if(command_run(argc, argv) == 0)
			bufferPrintf("ACM: Done: %s\n", acm_recv_buffer);
		else
			bufferPrintf("ACM: Unknown command: %s\n", argv[0]);
	}

	free(argv);

	EnterCriticalSection(); // Deliberately unended.
	usb_receive_bulk(ACM_EP_RECV, acm_recv_buffer, acm_usb_mps);
	task_stop();
}
Esempio n. 3
0
void
command_process(char* command)
{
	// Create array of arguments from line
	char** array = calloc(1000, sizeof(char*));
//	char* array[10];
	int numTokens = tokenize(command, array, 1000);

	// run command line
	command_run(numTokens, array);

	// Free array
	free(array);
}
Esempio n. 4
0
File: ui.c Progetto: bcl/parted
int
interactive_mode (PedDevice** dev, PedDisk** disk, Command* cmd_list[])
{
        StrList*    list;
        StrList*    command_names = command_get_names (cmd_list);

        commands = cmd_list;    /* FIXME yucky, nasty, evil hack */

        fputs (prog_name, stdout);

        print_using_dev (*dev);

        list = str_list_create (_(banner_msg), NULL);
        str_list_print_wrap (list, screen_width (), 0, 0, stdout);
        str_list_destroy (list);

        while (1) {
                char*       word;
                Command*    cmd;

                while (!command_line_get_word_count ()) {
                        if (got_ctrl_c) {
                                putchar ('\n');
                                return 1;
                        }
                        command_line_prompt_words ("(parted)", NULL,
                                                   command_names, 1);
                }

                word = command_line_pop_word ();
                if (word) {
                        cmd = command_get (commands, word);
                        free (word);
                        if (cmd) {
                                if (!command_run (cmd, dev, disk)) {
                                        command_line_flush ();

                                        if (*disk) {
                                                ped_disk_destroy (*disk);
                                                *disk = 0;
                                        }
                                }
                        } else
                                print_commands_help ();
                }
        }

        return 1;
}
Esempio n. 5
0
void serial_proto_got_char(char ch) {
	// Each time we get a char we perform the following:
	// Append the char to the buffer
	// Check that we have a valid command in the buffer
	//  * Since the first byte is cmd_id it should always be valid at this
	//  * stage.
	//  * If command is not valid reset buffer and return.
	// If we have a full command in buffer:
	//  * Check end CR + LF
	//  * * If invalid then reset
	//  * Run command handler

	// Append char to current buffer and increment buff ndx
	char_buffer_append(&serial_buffer, &ch, 1);

	// Check to see if we got a full command
	struct command_info *cmd = serial_proto_cmd_info();
	if(!cmd) {
		// Invalid command, reset buffer
		serial_proto_reset();
		return;
	}

	// Length of command in buffer when completed
	uint8_t cmd_len = serial_proto_cmd_length(cmd);
	uint8_t buff_len = char_buffer_length(&serial_buffer);

	// If we have a complete command in the buffer
	if(buff_len == cmd_len) {
		// Verify end tag
		if(serial_buffer.data[buff_len-2] != '\r' ||
		   serial_buffer.data[buff_len-2] != '\n') {
			// Invalid end tag
			// Reset and return
			serial_proto_reset();
			return;
		}

		// Null out start of end tag so args end in '\0'
		serial_buffer.data[buff_len-2] = '\0';

		// Run command
		command_run((uint8_t)serial_buffer.data[0], serial_buffer.data+1, buff_len-3);

		// Reset
		serial_proto_reset();
	}
}
Esempio n. 6
0
static void console_client_read(evutil_socket_t fd, short bedrock_attribute_unused events, void *data)
{
	struct console_client *client = data;
	int i;
	struct command_source source;

	if (client->in_buffer_len == sizeof(client->in_buffer))
	{
		bedrock_log(LEVEL_INFO, "Receive queue exceeded for console client - dropping client");

		io_disable(&client->fd.event_read);
		io_disable(&client->fd.event_write);

		return;
	}

	i = recv(fd, client->in_buffer + client->in_buffer_len, sizeof(client->in_buffer) - client->in_buffer_len, 0);
	if (i <= 0)
	{
		if (bedrock_list_has_data(&exiting_client_list, client) == false)
			bedrock_log(LEVEL_INFO, "Lost connection from console client");

		io_disable(&client->fd.event_read);
		io_disable(&client->fd.event_write);

		console_exit(client);
		return;
	}
	client->in_buffer_len += i;

	source.user = NULL;
	source.console = client;

	while (io_is_pending(&client->fd.event_read, EV_READ) && (i = mem_find(client->in_buffer, client->in_buffer_len, '\n')) > 0)
	{
		client->in_buffer[i] = 0;

		if (client->in_buffer[i - 1] == '\r')
			client->in_buffer[i - 1] = 0;

		command_run(&source, (char *) client->in_buffer);

		memmove(client->in_buffer, client->in_buffer + i + 1, client->in_buffer_len - i - 1);
		client->in_buffer_len -= i + 1;
	}
}
Esempio n. 7
0
/*** Process a command typed by the user ***/
uint8_t process_command(char *cmd_buffer, uint16_t cmd_length) {
	char *cmd = cmd_buffer;
	char *args = cmd_buffer;
	uint16_t i;
	uint8_t status = 0; // success

	// separate command and arguments (if any)
	for (i=0; args[0]!=' ' && args[0]!=0; i++,args++);
	*args = 0; // command word ends here in the buffer
	if (i<cmd_length) args++; // arguments start next, if any

	// help
	if (strcmp(cmd,"help")==0) {
		if (*args != 0) puts("No such help available.\n");
		else puts("You are running a really Simple-OS.\n");
	}
	// cls: clear the screen
	else if (strcmp(cmd,"cls")==0) {
		if (*args != 0) puts("cls: What to do with the arguments?\n");
		else cls();
	}
	// shutdown
	else if (strcmp(cmd,"shutdown")==0) {
		if (*args != 0) puts("shutdown: What to do with the arguments?\n");
		else {
			puts("You really had to do that...SYSTEM HALTED!!\n");
			status = 0xFF; // halt the system
		}	
	}
	// diskdump: see disk content on screen
	else if (strcmp(cmd,"diskdump")==0) {
		command_diskdump(args);	
	}
	// run: run a program sequentially
	else if (strcmp(cmd,"run")==0) {
		command_run(args);	
	}
	// unknown command
	else if (cmd[0]!=0) {
		sys_printf("%s: Command not found.\n",cmd);
	}
	return status;
}
Esempio n. 8
0
File: ui.c Progetto: bcl/parted
int
non_interactive_mode (PedDevice** dev, PedDisk **disk, Command* cmd_list[],
                      int argc, char* argv[])
{
        int         i;
        Command*    cmd;

        commands = cmd_list;    /* FIXME yucky, nasty, evil hack */

        for (i = 0; i < argc; i++)
                command_line_push_line (argv [i], 1);

        while (command_line_get_word_count ()) {
                char*    word;

                word = command_line_pop_word ();
                if (!word)
                        break;

                cmd = command_get (commands, word);
                free (word);
                if (!cmd) {
                        help_msg ();
                        goto error;
                }
                if (!(cmd->non_interactive)) {
                        fputs(_("This command does not make sense in "
                                "non-interactive mode.\n"), stdout);
                        exit(EXIT_FAILURE);
                        goto error;
                }

                if (!command_run (cmd, dev, disk))
                        goto error;
        }
        return 1;

error:
        return 0;
}
Esempio n. 9
0
File: xmpp.c Progetto: grouzen/xinb
LmHandlerResult xmpp_receive_command(LmMessageHandler *handler,
                        LmConnection *conn, LmMessage *m, gpointer udata)
{
    Xinb *x = udata;
    LmMessageNode *body;
    gchar *jid;
    const gchar *from;
    
    if(x->state != LM_CONNECTION_STATE_AUTHENTICATED) {
        log_record(x, LOGS_ERR,
                   "Unable to receive message: not authenticated");
        goto out;
    }
    
    from = lm_message_node_get_attribute(m->node, "from");
    jid = xmpp_get_jid(from);
    if(g_strcmp0(jid, g_hash_table_lookup(x->config, "owner"))) {
        log_record(x, LOGS_ERR, "To command may only owner: '%s'", from);
        x->to = jid;
        x->message = g_strdup("You don't have permission to command me");
        xmpp_send_message(x, LM_MESSAGE_SUB_TYPE_CHAT);
        g_free(x->message);
        goto out;
    }
    
    body = lm_message_node_find_child(m->node, "body");
    if(lm_message_get_sub_type(m) != LM_MESSAGE_SUB_TYPE_CHAT) {
        log_record(x, LOGS_ERR, "Invalid subtype of the command");
        goto out;
    }
    
    if(command_run(x, body->value)) {
        log_record(x, LOGS_INFO, "The command was successfully executed");
    }

    out:
    g_free(jid);
    lm_message_unref(m);
    return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
Esempio n. 10
0
File: main.c Progetto: chrippa/xmms2
void
command_run (cli_infos_t *infos, gchar *input)
{
	while (input && *input == ' ') ++input;

	if (input == NULL) {
		if (infos->status != CLI_ACTION_STATUS_ALIAS) {
			/* End of stream, quit */
			cli_infos_loop_stop (infos);
			g_printf ("\n");
		}
	} else if (*input != 0) {
		gint argc;
		gchar **argv, *listop;
		GError *error = NULL;

		if ((listop = strchr (input, ';'))) {
			*listop++ = '\0';
		}

		if (g_shell_parse_argv (input, &argc, &argv, &error)) {
			if (infos->status != CLI_ACTION_STATUS_ALIAS) {
				add_history (input);
			}
			command_dispatch (infos, argc, argv);
			g_strfreev (argv);
			if (listop && *listop) {
				g_printf ("\n");
				command_run (infos, listop);
			}
		} else {
			g_printf (_("Error: %s\n"), error->message);
			g_error_free (error);
		}
	}
}
Esempio n. 11
0
/* Parse the command and execute it */
command_result interface_handle_command(GIOChannel* chan, gchar* command){
    GError* err = NULL;
    gint argc;
    gchar** argv_;
    gchar** argv;
    size_t i;

    /* Parse the command in a shell-like fashion */
    if (!g_shell_parse_argv(g_strstrip(command), &argc, &argv_, &err)) {
        g_debug("Command parser error: %s", err->message);
        interface_write(chan, "{ \"error\": \"invalid command\" }\n");
        return CR_OK;
    }

    /* Copy argv_ to the stack so it's easier to use... */
    argv = g_newa(gchar*, argc);
    for(i=0; i < argc; i++) {
        argv[i] = g_newa(gchar, strlen(argv_[i]+1));
        strcpy(argv[i], argv_[i]);
    }
    g_strfreev(argv_);

    g_debug("Command: [%s] with %d parameter(s)", argv[0], argc-1);

    /* Now execute the command */
    command_full_descriptor* cmd_desc = NULL;

    for (i=0; g_commands[i].name != NULL; i++) {
        int nb_args = 0;
        while ((nb_args < MAX_CMD_ARGS) && (g_commands[i].desc.args[nb_args] != CA_NONE))
            nb_args += 1;
        if ((strcmp(g_commands[i].name, argv[0]) == 0) && (nb_args == argc-1)) {
            cmd_desc = &(g_commands[i]);
            break;
        }
    }
    if (!cmd_desc) {
        interface_write(chan, "{ \"error\": \"unknown command\" }\n");
        return CR_OK;
    }

    /* Handle "normal" and "special" commands separately. */
    switch (cmd_desc->type) {
    case CT_FUNC: {
        gboolean ret;

        ret = command_run((command_finalize_func) interface_finalize, chan, &(cmd_desc->desc), argc, argv);
        return (ret ? CR_OK : CR_DEFERED);
    }

    case CT_BYE:
        interface_write(chan, "Bye bye!\n");
        return CR_CLOSE;

    case CT_QUIT:
        g_message("Got a quit command, exiting...");
        exit(0);

    case CT_IDLE:
        return CR_IDLE;
    }

    return CR_OK;
}
Esempio n. 12
0
/*
 * Parse a line in a batch file and do the task accordingly.
 * @param command: the command to be sent to the console
 * return LINE_PARSE_GROUND for ground station messages.
 * return LINE_PARSE_SATELLITE for ground station messages.
 * return LINE_PARSE_ERROR for any errors.
 */
int parse_file(const char * batch_path) {

	char line[50];
	memset(line, 0, sizeof(line));
	char * pc_start;
	char * pc_end;
	int pos = 0;
	int replyed;
	struct stat statbuf;
	stat(batch_path, &statbuf);

	char * pc_batch_buf = (char * ) calloc(10240, 1);
	char * current_line = (char * ) calloc(128, 1);
	char * response = (char * ) calloc(128, 1);
	pc_start = pc_batch_buf;
	pc_end = pc_batch_buf;

	int i = 1;

	// Open file
	FILE * fp = fopen(batch_path, "r");
	if (!fp) {
		printf("Cannot open %s\n", batch_path);
		return -1;
	}


	if (((size_t) statbuf.st_size) > 10240)
	{
		printf("File %s too big.\n", batch_path);
		goto err;
	}


	if (!pc_batch_buf) {
		printf("malloc error!\n");
		goto err;
	}

	// Read file into buffer
	if (fread(pc_batch_buf, 1, statbuf.st_size, fp) != (size_t) statbuf.st_size) {
		printf("Failed to read complete file");
		goto err;
	}

	fclose(fp);
	fp = NULL;

	/* Finished loading to buffer. Main program starts from here. */


	while (strchr(pc_end + 1, '\n')) {
		pc_start = pc_batch_buf + pos;  //
		pc_end = strchr(pc_start, '\n');
		if (!(pc_end))
			break;
		memset(current_line, 0, 128);
		memset(response, 0, 128);
		memcpy(current_line, pc_start, (pc_end - pc_start));
		int ret = parse_line(current_line);
		switch (ret) {
		case LINE_PARSE_GROUND:
			printf("[%03d] \"%s\" to ground station.\n", i , current_line);
			command_run(current_line);
			usleep(10000);
			break;
		case LINE_PARSE_SATELLITE:
			printf("[%03d] \"%s\" to satellite.\n", i , current_line);
			send_remote_shell_command(current_line, response, &replyed);
			printf("%s, replyed: %d\n", response, replyed);
			usleep(10000);
			break;
		case LINE_PARSE_ERROR:
			printf("Error when processing line %d: %s\n", i, current_line);
			break;
		case LINE_PARSE_SYNTAX:
			printf("Please check the syntax of %d: %s\n", i, current_line);
			break;
		case LINE_PARSE_COMMENT:
		case LINE_PARSE_EMPTY:
		default:
			break;
		}
		pos = (pc_end - pc_batch_buf) + 1;
		i++;
	}

	if (current_line)
		free(current_line);
	if (response)
		free(response);
	if (pc_batch_buf)
		free(pc_batch_buf);
	return i;

err:
	if (current_line)
		free(current_line);
	if (response)
		free(response);
	if (pc_batch_buf)
		free(pc_batch_buf);
	if (fp)
		fclose(fp);

	return -1;
}
Esempio n. 13
0
int main(int argc, char **argv)
{
	int rval = 0;

	lookfile = init_args(argc, argv);

	if(lookfile == stdin){
		printf(APPTITLE " >:O MUHAHAHA. READY. SET. GO!\n");
		printf("LINE NUMBERS BETWEEN %d AND %d!\n",LINENUM_MIN,LINENUM_MAX);
		printf("\n");

		printf(INPUTLINE_START);
		fflush(stdout);

		getlook();

		for(;;){

			skipwhite();

			if(look == EOF)
				break;

			if(isdigit(look)){		/* BASIC program line */
				parse_BASIC_line();
			}else if(isalpha(look)){	/* BASIC command */
				parse_BASIC_command();
			}else{				/* Syntax error */
				read_to_eol();
				error("syntax error");
			}
			printf(INPUTLINE_START);
			fflush(stdout);


			skip_crlf();
		}
	}else{
		getlook();

		for(;;){

			skipwhite();

			if(look == EOF)
				break;

			if(!isdigit(look)){
				error("no line number");
				rval = 1;
				break;
			}

			parse_BASIC_line();
			skip_crlf();
		}
		if(rval == 0){
			command_run();
		}

		fclose(lookfile);
	}

	lineset_free();

	printf("\n");
	
	return rval;
}
Esempio n. 14
0
int command_step(debugger_state_t *state, int argc, char **argv) {
	char *_argv[] = { "run", "1" };
	return command_run(state, 2, _argv);
}
Esempio n. 15
0
static void acm_parse(int32_t _amt)
{
	int start = 0;
	int i = 0;

	if(acm_file_ptr != NULL && acm_file_recv_left > 0)
	{
		if(_amt >= acm_file_recv_left)
		{
			memcpy(acm_file_ptr, acm_recv_buffer, acm_file_recv_left);
			i = acm_file_recv_left;
			start = i;

			bufferPrintf("ACM: Received file (finished at 0x%08x)!\n", acm_file_ptr + acm_file_recv_left);

			acm_file_ptr = NULL;
			acm_file_recv_left = 0;
		}
		else
		{

			memcpy(acm_file_ptr, acm_recv_buffer, _amt);
			acm_file_ptr += _amt;
			acm_file_recv_left -= _amt;
			//bufferPrintf("ACM: Got %d of file (%d remain).\n", _amt, acm_file_recv_left);

			EnterCriticalSection(); // Deliberately unended.
			usb_receive_bulk(ACM_EP_RECV, acm_recv_buffer, acm_usb_mps);
			return;
		}
	}

	for(; i < _amt; i++)
	{
		if(acm_recv_buffer[i] == '\n')
		{
			acm_recv_buffer[i] = 0;
			if(i > 0)
				if(acm_recv_buffer[i-1] == '\r')
					acm_recv_buffer[i-1] = 0;

			char safeCommand[ACM_BUFFER_SIZE];
			char *command = &acm_recv_buffer[start];
			strcpy(safeCommand, command);
			int argc;
			char** argv = command_parse(command, &argc);

			if(argc >= 3 && strcmp(argv[0], "sendfile") == 0)
			{
				acm_file_ptr = (char*)parseNumber(argv[1]);
				acm_file_recv_left = parseNumber(argv[2]);
				received_file_size = acm_file_recv_left;
				bufferPrintf("ACM: Started receiving file at 0x%08x - 0x%08x (%d bytes).\n", acm_file_ptr, acm_file_ptr + acm_file_recv_left, acm_file_recv_left);
				i = _amt;
				start = i;
			}
			else if(argc >= 3 && strcmp(argv[0], "recvfile") == 0)
			{
				acm_busy = TRUE;

				acm_file_ptr = (char*)parseNumber(argv[1]);
				acm_file_send_left = parseNumber(argv[2]);

				bufferPrintf("ACM: Started sending file at 0x%08x - 0x%08x (%d bytes).\n", acm_file_ptr, acm_file_ptr + acm_file_send_left, acm_file_send_left);

				int amt = sprintf(acm_send_buffer, "ACM: Starting File: %d %d\n", (uint32_t)acm_file_ptr, acm_file_send_left);
				usb_send_bulk(ACM_EP_SEND, acm_send_buffer, amt+1);

				i = _amt;
				start = i;
			}
			else
			{
				bufferPrintf("ACM: Starting %s\n", safeCommand);
				if(command_run(argc, argv) == 0)
					bufferPrintf("ACM: Done: %s\n", safeCommand);
				else
					bufferPrintf("ACM: Unknown command: %s\n", command);
				
				start = i+1;
			}

			free(argv);
		}
	}

	EnterCriticalSection(); // Deliberately unended.

	if(start < _amt)
	{
		if(acm_unprocessed > 0)
		{
			bufferPrintf("ACM: command too long, discarding...\n");
			acm_unprocessed = 0;
			usb_receive_bulk(ACM_EP_RECV, acm_recv_buffer, acm_usb_mps);
			task_stop();
			return;
		}
		else
			memcpy(acm_recv_buffer, acm_recv_buffer+start, _amt-start);
	}

	acm_unprocessed = _amt-start;
	usb_receive_bulk(ACM_EP_RECV, acm_recv_buffer+acm_unprocessed, acm_usb_mps);
	task_stop();
}