-
Notifications
You must be signed in to change notification settings - Fork 20
/
commands.c
269 lines (231 loc) · 4.48 KB
/
commands.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* 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 <stdlib.h>
#include "microcom.h"
static int cmd_speed(int argc, char *argv[])
{
unsigned long speed;
int ret;
if (argc < 2) {
printf("current speed: %lu\n", current_speed);
return 0;
}
speed = strtoul(argv[1], NULL, 0);
ret = ios->set_speed(ios, speed);
if (ret) {
fprintf(stderr, "invalid speed %lu\n", speed);
return ret;
}
current_speed = speed;
return 0;
}
static int cmd_flow(int argc, char *argv[])
{
char *flow;
if (argc < 2) {
switch (current_flow) {
default:
case FLOW_NONE:
flow = "none";
break;
case FLOW_SOFT:
flow = "soft";
break;
case FLOW_HARD:
flow = "hard";
break;
}
printf("current flow: %s\n", flow);
return 0;
}
switch (*argv[1]) {
case 'n':
current_flow = FLOW_NONE;
break;
case 's':
current_flow = FLOW_SOFT;
break;
case 'h':
current_flow = FLOW_HARD;
break;
default:
printf("unknown flow type \"%s\"\n", argv[1]);
return 1;
}
ios->set_flow(ios, current_flow);
return 0;
}
static int current_dtr = 0;
static int current_rts = 0;
static int cmd_set_handshake_line(int argc, char *argv[])
{
int enable;
int ret = 0;
int pin = 0;
if (!ios->set_handshake_line) {
printf("function not supported \"%s\"\n", argv[0]);
return 1;
}
if (!strncmp(argv[0], "dtr", 3))
pin = PIN_DTR;
else if (!strncmp(argv[0], "rts", 3))
pin = PIN_RTS;
if (!pin) {
printf("unknown pin \"%s\"\n", argv[0]);
return 1;
}
if (argc < 2) {
switch (pin) {
case PIN_DTR:
printf("current dtr: \"%d\"\n", current_dtr);
break;
case PIN_RTS:
printf("current rts: \"%d\"\n", current_rts);
break;
}
return 0;
}
if (!strncmp(argv[1], "1", strlen(argv[1]))) {
enable = 1;
} else if (!strncmp(argv[1], "0", strlen(argv[1]))) {
enable = 0;
} else {
printf("unknown dtr state \"%s\"\n", argv[1]);
return 1;
}
printf("setting %s: \"%d\"\n", argv[0], enable);
ret = ios->set_handshake_line(ios, pin, enable);
if (ret)
return ret;
switch (pin) {
case PIN_DTR:
current_dtr = enable;
break;
case PIN_RTS:
current_rts = enable;
break;
}
return 0;
}
static int cmd_exit(int argc, char *argv[])
{
return MICROCOM_CMD_START;
}
static int cmd_break(int argc, char *argv[])
{
ios->send_break(ios);
return MICROCOM_CMD_START;
}
static int cmd_quit(int argc, char *argv[])
{
microcom_exit(0);
return 0;
}
static int cmd_sendescape(int argc, char *argv[])
{
ios->write(ios, "\x1c", 1);
return 0;
}
static int cmd_help(int argc, char *argv[])
{
struct cmd *cmd;
if (argc == 1) {
for_each_command(cmd) {
if (cmd->info)
printf("%s - %s\n", cmd->name, cmd->info);
else
printf("%s\n", cmd->name);
}
} else {
microcom_cmd_usage(argv[1]);
}
return 0;
}
static int cmd_execute(int argc, char *argv[])
{
if (argc < 2)
return MICROCOM_CMD_USAGE;
return do_script(argv[1]);
}
static int cmd_log(int argc, char *argv[])
{
int ret;
if (argc < 2)
return MICROCOM_CMD_USAGE;
ret = logfile_open(argv[1]);
return ret;
}
static int cmd_comment(int argc, char *argv[])
{
return 0;
}
static struct cmd cmds[] = {
{
.name = "speed",
.fn = cmd_speed,
.info = "set terminal speed",
.help = "speed <newspeed>"
}, {
.name = "exit",
.fn = cmd_exit,
.info = "exit from command processing",
}, {
.name = "flow",
.fn = cmd_flow,
.info = "set flow control",
.help = "flow hard|soft|none",
}, {
.name = "dtr",
.fn = cmd_set_handshake_line,
.info = "set dtr value",
.help = "dtr 1|0",
}, {
.name = "rts",
.fn = cmd_set_handshake_line,
.info = "set rts value",
.help = "rts 1|0",
}, {
.name = "break",
.fn = cmd_break,
.info = "send break",
}, {
.name = "sendescape",
.fn = cmd_sendescape,
.info = "send a Ctrl-\\",
}, {
.name = "quit",
.fn = cmd_quit,
.info = "quit microcom",
}, {
.name = "help",
.fn = cmd_help,
.info = "show help",
}, {
.name = "x",
.fn = cmd_execute,
.info = "execute a script",
.help = "x <scriptfile>",
}, {
.name = "log",
.fn = cmd_log,
.info = "log to file",
.help = "log <logfile>",
}, {
.name = "#",
.fn = cmd_comment,
.info = "comment",
},
};
void commands_init(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(cmds); i++)
register_command(&cmds[i]);
}