-
Notifications
You must be signed in to change notification settings - Fork 13
/
lib_cli.c
384 lines (339 loc) · 6.64 KB
/
lib_cli.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* File name: lib_cli.c
* Date first: 02/19/2018
* Date last: 02/23/2018
*
* Description: STM8 Library for handling commands from input source.
*
* Author: Richard Hodges
*
* Copyright (C) 2018 Richard Hodges. All rights reserved.
* Permission is hereby granted for any use.
*
******************************************************************************
*
*/
#include "lib_cli.h"
#define CMD_CR 0x0d
#define CMD_BELL 0x07
#define CMD_BS 0x08
#define CMD_CRLF "\r\n"
static char cmpkey(char *, char *); /* compare keyword */
static char toupper(char);
static char tolower(char);
static COMMAND_TAB *command_tab;
static char command_len;
static char *buf_in;
static char buf_len;
static char *cmd_ptr;
static char cmd_len;
static char (*getc)(void);
static void (*putc)(char);
static char (*rxct)(void);
/******************************************************************************
*
* Command init
* in: COMMAND_TAB, tab size, command buffer, buffer size
*/
void cli_init(COMMAND_CTX *ctx)
{
command_tab = ctx->tab;
buf_in = ctx->buf_in;
buf_len = ctx->buf_len - 1;
getc = ctx->getc;
putc = ctx->putc;
rxct = ctx->size;
cmd_ptr = buf_in;
cmd_len = 0;
*cmd_ptr = 0;
}
/******************************************************************************
*
* Poll for command characters
* out: non-zero = line ready
*/
char cli_poll(void)
{
char cmdchr;
while (rxct()) {
cmdchr = getc();
if (cmdchr == CMD_CR)
return 1;
if (cmdchr == CMD_BS) {
if (!cmd_len)
continue;
cmd_len--;
cmd_ptr--;
*cmd_ptr = 0;
putc(CMD_BS);
putc(' ');
putc(CMD_BS);
continue;
}
if (cmd_len == buf_len) {
putc(CMD_BELL);
continue;
}
putc(cmdchr);
*cmd_ptr = cmdchr;
cmd_len++;
cmd_ptr++;
*cmd_ptr = 0;
}
return 0;
}
/******************************************************************************
*
* Get keyword number
* out: command number or zero (invalid)
*/
char cli_keynum(char *str)
{
COMMAND_TAB *tab;
tab = command_tab;
while (tab->cmd_num) {
if (cmpkey(str, tab->cmd_name) == 0)
return tab->cmd_num;
tab++;
}
return 0;
}
/******************************************************************************
*
* Show available commands
*/
static void puts(char *str)
{
while (*str)
putc(*str++);
}
void cli_help(void)
{
COMMAND_TAB *tab;
tab = command_tab;
while (tab->cmd_num) {
puts(tab->cmd_name);
putc(9);
puts(tab->cmd_info);
putc(0x0d);
putc(0x0a);
tab++;
}
}
/******************************************************************************
*
* Reset command line
*/
void cli_reset(void)
{
cmd_ptr = buf_in;
cmd_len = 0;
*cmd_ptr = 0;
}
/******************************************************************************
*
* Function using inline assembly follow.
* Remove compiler warnings that functions "must return value"
*/
#pragma disable_warning 59
/******************************************************************************
*
* Trim whitespace from string
* out: final string length
*/
char cli_trim (char *s)
{
s;
__asm
ldw x, (3, sp)
ldw y, (3, sp)
push #0 ; not in quotes
push #0 ; final length
push #' ' ; pretend previous space
00001$:
ld a, (x)
jreq 00090$
incw x
tnz (3, sp) ; inside quotes?
jrne 00009$ ; do not trim inside quote
cp a, #' '
jrne 00009$
tnz (1, sp)
jrne 00001$
ld (1, sp),a
ld (y), a
incw y
inc (2, sp)
jra 00001$
00009$:
ld (y), a
incw y
inc (2, sp)
clr (1, sp)
cp a, #'"'
jrne 00001$
cpl (3, sp)
jra 00001$
00090$:
tnz (1, sp) ; trailing space?
jreq 00091$
tnz (2, sp) ; only spaces?
jreq 00091$
decw y
00091$:
clr (y)
ld a, (2, sp)
addw sp, #3
__endasm;
}
/******************************************************************************
*
* Tokenize command line
* in: command line, token pointer list, max tokens
* out: token count
*/
char cli_tokenize(char *inbuf, char **toklist, char tokmax)
{
inbuf, toklist, tokmax;
__asm
ldw x, (3, sp)
ldw y, (5, sp)
ld a, (7, sp)
push #0 ; (2, sp) token count
push #0 ; (1, sp) quote flag
00001$:
ld a, (x)
jreq 00090$
cp a, #'"' ; does token start with quote?
jrne 00003$
incw x
cpl (1, sp)
00003$:
ldw (y), x
incw y
incw y
inc (2, sp)
dec (9, sp)
jreq 00090$
00005$:
ld a, (x)
jreq 00090$
incw x
cp a, #'"'
jreq 00010$
tnz (1, sp) ; inside quote?
jrne 00005$ ; leave quoted token alone
cp a, #' ' ; space ends token
jrne 00005$
decw x
clr (x) ; terminate token
incw x
jra 00001$
00010$:
decw x
clr (x) ; quote is now end of token
incw x
cpl (1, sp)
jrne 00001$ ; starting new token inside quote
ld a, (x)
cp a, #' ' ; end quote followed by space?
jrne 00001$
incw x ; skip space
jra 00001$
00090$:
ld a, (2, sp)
addw sp, #2
__endasm;
}
/******************************************************************************
*
* Remove comment from string (comment char inside double quotes ignored)
* in: string, 4 character string with comment chars (eg, "#;;;")
*/
void cli_comment(char *buf, char *comment)
{
buf, comment;
__asm
ldw y, (3, sp)
ldw x, (5, sp)
push #0 ; quotes flag
00001$:
ld a, (y)
jreq 00095$
incw y
cp a, #'"'
jreq 00010$
tnz (1, sp) ; inside quotes?
jrne 00001$
cp a, (x)
jreq 00005$
cp a, (1, x)
jreq 00005$
cp a, (2, x)
jreq 00005$
cp a, (3, x)
jrne 00001$
00005$:
decw y
clr (y)
jra 00095$
00010$:
cpl (1, sp)
jra 00001$
00095$:
addw sp, #1
__endasm;
}
/******************************************************************************
*
* Make uppercase
*/
static char toupper(char c)
{
c;
__asm
cp a, #'a'
jrc 00001$
cp a, #'z' + 1
jrnc 00001$
sub a, #0x20
00001$:
__endasm;
}
static char tolower(char c)
{
c;
__asm
cp a, #'A'
jrc 00001$
cp a, #'Z' + 1
jrnc 00001$
add a, #0x20
00001$:
__endasm;
}
/******************************************************************************
*
* Compare keywords
*
* in: word 1 (upper/lower), word 2 (lowercase)
* out: zero = match
*/
static char cmpkey(char *in, char *list)
{
in, list;
__asm
ldw x, (3, sp)
ldw y, (5, sp)
00001$:
ld a, (y)
jreq 00090$
incw y
call _tolower
sub a, (x)
jrne 00090$
incw x
jra 00001$
00090$:
__endasm;
}