aboutsummaryrefslogtreecommitdiffstats
path: root/sysmoOCTSIM/command.c
blob: 9502f055b856c1e5bf35fc8a06a45f2e9ec066df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <string.h>
#include <stdint.h>
#include <stdio.h>

#include <utils.h>

#include "atmel_start.h"
#include "command.h"

struct cmd_state {
	const char *prompt;
	char buf[128];
	unsigned int buf_idx;
	const struct command_fn *cmd[32];
	unsigned int cmd_idx;
};

static struct cmd_state g_cmds;

int command_register(const struct command_fn *cmd)
{
	if (g_cmds.cmd_idx >= ARRAY_SIZE(g_cmds.cmd))
		return -1;
	g_cmds.cmd[g_cmds.cmd_idx++] = cmd;
	return 0;
}

DEFUN(help, help_cmd, "help", "Print command reference")
{
	unsigned int i;
	printf("Help:\r\n");
	printf(" Command          Help\r\n");
	printf(" ---------------- ----\r\n");
	for (i = 0; i < g_cmds.cmd_idx; i++)
		printf(" %-16s %s\r\n", g_cmds.cmd[i]->command, g_cmds.cmd[i]->help);
}

static void cmd_execute()
{
	char *argv[16];
	unsigned int i;
	int argc = 0;
	char *cur;

	printf("\r\n");
	memset(argv, 0, sizeof(argv));

	for (cur = strtok(g_cmds.buf, " "); cur; cur = strtok(NULL, " ")) {
		if (argc >= ARRAY_SIZE(argv))
			break;
		argv[argc++] = cur;
	}

	for (i = 0; i < g_cmds.cmd_idx; i++) {
		if (!strcmp(g_cmds.cmd[i]->command, argv[0])) {
			g_cmds.cmd[i]->fn(argc, argv);
			return;
		}
	}
	printf("Unknown command: '%s'\r\n", argv[0]);
}

static void cmd_buf_reset(void)
{
	memset(g_cmds.buf, 0, sizeof(g_cmds.buf));
	g_cmds.buf_idx = 0;
}

static void cmd_buf_append(char c)
{
	g_cmds.buf[g_cmds.buf_idx++] = c;
}

void command_try_recv(void)
{
	unsigned int i = 0;

	/* yield CPU after maximum of 10 received characters */
	while (usart_sync_is_rx_not_empty(&UART_debug) && (i < 10)) {
		int c = getchar();
		if (c < 0)
			return;
		putchar(c);
		if (c == '\r' || c == '\n' || g_cmds.buf_idx >= sizeof(g_cmds.buf)-1) {
			cmd_execute();
			cmd_buf_reset();
			printf(g_cmds.prompt);
			return;
		}
		/* append character */
		cmd_buf_append(c);

		i++;
	}
}

void command_init(const char *prompt)
{
	g_cmds.prompt = prompt;
	command_register(&help_cmd);
}