-
Notifications
You must be signed in to change notification settings - Fork 80
/
console.c
120 lines (104 loc) · 3.12 KB
/
console.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
/*
Copyright (C) 2014 Serval Project Inc.
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include "cli.h"
#include "mem.h"
#include "net.h"
#include "str.h"
#include "fdqueue.h"
#include "console.h"
struct command_state{
struct sched_ent alarm;
char line_buff[1024];
size_t line_pos;
struct cli_schema *cli_commands;
};
struct profile_total stdin_profile={
.name="command_handler",
};
static void process_command(char *line, struct cli_schema *cli_commands){
char *argv[16];
int argc = parse_argv(line, ' ', argv, 16);
struct cli_parsed parsed;
switch (cli_parse(argc, (const char *const*)argv, cli_commands, NULL, &parsed)) {
case 0:
cli_invoke(&parsed, NULL);
break;
case 1:
printf("Unknown command, try help\n");
fflush(stdout);
break;
case 2:
printf("Ambiguous command, try help\n");
fflush(stdout);
break;
default:
printf("Error\n");
fflush(stdout);
break;
}
}
static void read_lines(struct sched_ent *alarm){
struct command_state *state=(struct command_state *)alarm;
ssize_t bytes = read(alarm->poll.fd, state->line_buff + state->line_pos, sizeof(state->line_buff) - state->line_pos);
if (bytes<=0){
// EOF?
unwatch(alarm);
alarm->poll.fd=-1;
return;
}
size_t i = state->line_pos;
size_t processed=0;
state->line_pos+=bytes;
char *line_start=state->line_buff;
for (;i<state->line_pos;i++){
if (state->line_buff[i]=='\n'){
state->line_buff[i]=0;
if (*line_start)
process_command(line_start, state->cli_commands);
processed=i+1;
line_start = state->line_buff + processed;
}
}
if (processed){
// squash unprocessed data back to the start of the buffer
state->line_pos -= processed;
bcopy(state->line_buff, line_start, state->line_pos);
}
}
struct command_state *command_register(struct cli_schema *commands, int fd){
struct command_state *ret = emalloc_zero(sizeof(struct command_state));
if (!ret)
return NULL;
ret->alarm.poll.fd=fd;
ret->alarm.poll.events=POLLIN;
ret->alarm.function=read_lines;
ret->alarm.stats=&stdin_profile;
ret->cli_commands = commands;
watch(&ret->alarm);
return ret;
}
uint8_t is_command_closed(struct command_state *state){
return state->alarm.poll.fd==-1;
}
void command_close(struct command_state *state){
if (is_watching(&state->alarm))
unwatch(&state->alarm);
state->alarm.poll.fd=-1;
}
void command_free(struct command_state *state){
command_close(state);
free(state);
}