-
Notifications
You must be signed in to change notification settings - Fork 20
/
parser.c
209 lines (165 loc) · 3.54 KB
/
parser.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (C) 2010 Sascha Hauer <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*/
#include "config.h"
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "microcom.h"
#define MAXARGS 64
static int parse_line(char *_line, int *argc, char *argv[])
{
char *line = _line;
int nargs = 0;
if (!line)
goto out;
while (nargs < MAXARGS) {
/* skip any white space */
while (*line == ' ' || *line == '\t')
++line;
if (*line == '\0' || *line == ';') /* end of line, no more args */
goto out;
argv[nargs] = line; /* begin of argument string */
if (*line == '\"') {
line++;
argv[nargs] = line;
while (*line && *line != '\"')
line++;
if (!*line) {
printf("could not find matching '\"'\n");
return -EINVAL;
} else
*line++ = '\0';
} else {
/* find end of string */
while (*line && *line != ' ' && *line != '\t' && *line != ';')
++line;
}
nargs++;
if (*line == '\0') /* end of line, no more args */
goto out;
if (*line == ';') {
*line = '\0';
goto out;
}
*line++ = '\0'; /* terminate current arg */
}
printf("Too many args (max. %d)\n", MAXARGS);
out:
argv[nargs] = NULL;
*argc = nargs;
return line - _line + 1;
}
struct cmd *commands;
int register_command(struct cmd *cmd)
{
struct cmd *tmp;
cmd->next = NULL;
if (!commands) {
commands = cmd;
return 0;
}
tmp = commands;
while (tmp->next)
tmp = tmp->next;
tmp->next = cmd;
return 0;
}
void microcom_cmd_usage(char *command)
{
struct cmd *cmd;
for_each_command(cmd) {
if (!strcmp(command, cmd->name)) {
char *str = NULL;
if (cmd->info)
str = cmd->info;
if (cmd->help)
str = cmd->help;
if (!str)
str = "no help available\n";
printf("usage:\n%s\n", str);
return;
}
}
printf("no such command\n");
}
static int __do_commandline(const char *prompt)
{
char *cmd;
char *argv[MAXARGS + 1];
int argc = 0, ret = 0, n, len;
while (1) {
struct cmd *command;
cmd = readline(prompt);
if (!cmd) {
ret = MICROCOM_CMD_START;
break;
}
if (!strlen(cmd))
goto done;
if (prompt)
add_history(cmd);
len = strlen(cmd);
n = 0;
while (n < len) {
int handled = 0;
ret = parse_line(cmd + n, &argc, argv);
if (ret < 0)
break;
n += ret;
if (!argv[0])
continue;
for_each_command(command) {
if (!strcmp(argv[0], command->name)) {
ret = command->fn(argc, argv);
if (ret == MICROCOM_CMD_START) {
free(cmd);
return ret;
}
if (ret == MICROCOM_CMD_USAGE)
microcom_cmd_usage(argv[0]);
handled = 1;
break;
}
}
if (!handled)
printf("unknown command \'%s\', try \'help\'\n", argv[0]);
}
done:
free(cmd);
}
if (cmd)
free(cmd);
return ret;
}
int do_commandline(void)
{
int ret;
restore_terminal();
printf("\nEnter command. Try \'help\' for a list of builtin commands\n");
do {
ret = __do_commandline("-> ");
} while (ret != MICROCOM_CMD_START);
printf("\n----------------------\n");
init_terminal();
return 0;
}
int do_script(char *script)
{
int fd = open(script, O_RDONLY);
int stdinfd = dup(1);
int ret;
if (fd < 0) {
printf("could not open %s: %s\n", script, strerror(errno));
return -1;
}
dup2(fd, 0);
ret = __do_commandline(NULL);
dup2(stdinfd, 0);
return ret;
}