Exemple #1
0
void cli_loop(void)
{
	char buffer[CLI_BUFFER_SIZE];
	char c;

	int buffer_pos;

	uart_printf("%s", cli_prompt);

	buffer_pos = 0;

	while(1) {
		//wait until a character is available
		while((c = uart_getchar()) == 0) { ; }

		//new line means end of the command
		if(c == '\n') {
			//only process a command if something was entered
			if(buffer_pos) {
				buffer[buffer_pos] = '\0';
				cli_process_command(buffer);
			}

			buffer_pos = 0;
			memset(buffer, 0, CLI_BUFFER_SIZE);

			uart_printf("%s", cli_prompt);
		}
		//there is more to come...
		else if(isgraph(c) || isspace(c)) {
			buffer[buffer_pos++] = c;
		}
		//
		else if(c == ASCII_DEL) {
			if(buffer_pos) {
				buffer_pos--;
				buffer[buffer_pos] = '\0';
			}
		}
		//ignore for now
		else {
			continue;
		}

		//is the command too long?
		if(buffer_pos >= CLI_BUFFER_SIZE) {
			uart_printf("\n--Error--, command is too long. Command ignored.\n");
			
			buffer_pos = 0;
			memset(buffer, 0, CLI_BUFFER_SIZE);

			uart_printf("%s", cli_prompt);
		}
	}
}
Exemple #2
0
// Parse user-input and implement commands
int cli_service(void)
{
    int received_bytes = 0;
    char c;
    bool buffers_updated = false;
    // process received bytes from the ring buffer and move them into the
    // command buffer
    while ((received_bytes = serial_get_received_bytes(USB_COMM)) != g_receive_buffer.ring_buffer_position) {
        // add the current byte to the command buffer
        buffers_updated = true;
        c = g_receive_buffer.ring_buffer[g_receive_buffer.ring_buffer_position];
        LOG("%c", c);
        if (c == '\b') {
            if (g_receive_buffer.command_buffer_length) {
                LOG(" \b"); /* erase previous character from screen */
                g_receive_buffer.command_buffer_length--; /* backspace */
            } else {
                LOG(" "); /* fill back space */
            }
        } else {
            g_receive_buffer.command_buffer[g_receive_buffer.command_buffer_length++] = c;
        }
        if (g_receive_buffer.ring_buffer_position == sizeof(g_receive_buffer.ring_buffer)) {
            g_receive_buffer.ring_buffer_position = 0;
        } else {
            g_receive_buffer.ring_buffer_position++;
        }
    }

    // Scan for a "line" (that is words followed by \r\n or \r\r\n. If found,
    // record that we have a match and null terminate the string
    if (buffers_updated) {
        char *loc;
        int idx;
        int end_of_command = false;
        bool command_waiting = false;
        if ((loc = strstr(g_receive_buffer.command_buffer, "\r\n")) != NULL) {
            LOG("\r\n");
            idx = loc - g_receive_buffer.command_buffer;
            command_waiting = true;
            end_of_command = idx + 1;
            g_receive_buffer.command_buffer[idx] = '\0';
            g_receive_buffer.command_buffer[idx + 1] = '\0';
        } else if ((loc = strchr(g_receive_buffer.command_buffer, '\r')) != NULL) {
            LOG("\r\n");
            idx = loc - g_receive_buffer.command_buffer;
            command_waiting = true;
            end_of_command = idx;
            g_receive_buffer.command_buffer[idx] = '\0';
        }

        if (command_waiting) {
            // now, process the command and move any modify the contents of the
            // command_buffer to be in a valid state again
            cli_process_command(g_receive_buffer.command_buffer);
            LOG("#> ");
            memcpy(g_receive_buffer.command_buffer,
                   &g_receive_buffer.command_buffer[end_of_command + 1],
                   sizeof(g_receive_buffer.command_buffer) - end_of_command);
            g_receive_buffer.command_buffer_length -= (end_of_command + 1);
        }
    }
    return 0;

} // end menu()