-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.c
More file actions
717 lines (584 loc) · 18.2 KB
/
cli.c
File metadata and controls
717 lines (584 loc) · 18.2 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/**
* Simple Command Line Interface (CLI) driver.
* Features:
* - Single- and double-quoting
* - Command line history
* - Auto-complete
* - UTF-8 support
*
* URL: <https://github.com/erkia/cli>
*
* Copyright (C) 2016 Erki Aring
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cli.h"
#define CLI_LINE_BUFSIZE 32 /**< Maximum length of the command line that can be handled */
#define CLI_ARGV_LEN 11 /**< Maximum number of allowed arguments, including command name */
#define CLI_HISTORY 8 /**< Number of command lines to remember */
#define CLI_HIST_IGNORE_DUP (1 << 0) /**< Flag to indicate, that duplicate entry should be discarded */
#define CLI_HIST_TEMPORARY (1 << 1) /**< Flag to indicate, that entry is temporary */
#define CLI_HIST_DIRTY (1 << 2) /**< Flag to indicate, that command line is modified */
#define CLI_HIST_CLEAR (1 << 3) /**< Flag to indicate, that history should be cleared */
#if (CLI_HISTORY > 0)
#define CLI_HAS_HISTORY /**< If defined, history functionality is included */
#endif
#define CLI_HAS_AUTOCOMPLETE /**< If defined, auto-complete functionality is included */
/**
* A structure containing information about the current command line.
*/
typedef struct {
char buf[CLI_LINE_BUFSIZE];
size_t len;
uint8_t ready;
} cli_line_t;
/**
* A structure containing information about the command line history.
*/
typedef struct {
char lines[CLI_HISTORY][CLI_LINE_BUFSIZE];
size_t pos;
size_t len;
int flags;
} cli_history_t;
/**
* A structure containing the state of the driver
*/
typedef struct {
cli_line_t line;
#ifdef CLI_HAS_HISTORY
cli_history_t history;
#endif
const cli_cmd_list_t *cmdlist;
uint8_t utf8_enabled;
} cli_control_t;
static cli_control_t _CliCtrl;
static cli_control_t *CliCtrl = &_CliCtrl;
static void CLI_AddChar (char c);
static void CLI_DelChar (void);
/**
* Write a buffer to the standard output.
*
* @param buf Buffer
* @param len Length of the buffer
*
* @return Number of bytes written
*/
static int CLI_Write (const void *buf, size_t len)
{
return write (1, buf, len);
}
/**
* Write a string to the standard output.
*
* @param str Null-terminated string
*
* @return Number of bytes written
*/
static int CLI_WriteStr (const char *str)
{
return CLI_Write (str, strlen (str));
}
/**
* Write out command prompt and current command.
*/
static void CLI_Prompt (void)
{
CLI_WriteStr ("\r\ncli> ");
CLI_Write (CliCtrl->line.buf, CliCtrl->line.len);
}
/**
* Erase the old command and replace it with a new one.
*
* @param line Command
* @param len Length of the command
*/
static void CLI_ReplaceCmd (const char *line, size_t len)
{
if (len < CLI_LINE_BUFSIZE) {
// Clear the old command
while (CliCtrl->line.len > 0){
CLI_DelChar ();
}
// Load the command from history
strncpy (CliCtrl->line.buf, line, len);
CliCtrl->line.buf[len] = '\0';
CliCtrl->line.len = len;
// Write the new command
CLI_Write (CliCtrl->line.buf, CliCtrl->line.len);
}
}
#ifdef CLI_HAS_AUTOCOMPLETE
/**
* Auto-complete a command or print out possible completions in case of ambiguity
*
* @param tab_count Number of times the tab is pressed consequently (counting from '0')
*/
static void CLI_AutoComplete (int tab_count)
{
size_t i;
int found = 0;
if (CliCtrl->cmdlist != NULL) {
// Find all matches
for (i = 0; i < CliCtrl->cmdlist->count; i++) {
if (!strncmp (CliCtrl->line.buf, CliCtrl->cmdlist->commands[i].name, CliCtrl->line.len)) {
found++;
}
}
if (found == 1) {
// One match found - use it for auto-completion
for (i = 0; i < CliCtrl->cmdlist->count; i++) {
if (!strncmp (CliCtrl->line.buf, CliCtrl->cmdlist->commands[i].name, CliCtrl->line.len)) {
CLI_ReplaceCmd (CliCtrl->cmdlist->commands[i].name, strlen (CliCtrl->cmdlist->commands[i].name));
CLI_AddChar (' ');
}
}
} else if (found > 1) {
// Multiple matches found - print out all possible completions on every second tab-press
if (tab_count % 2) {
for (i = 0; i < CliCtrl->cmdlist->count; i++) {
if (!strncmp (CliCtrl->line.buf, CliCtrl->cmdlist->commands[i].name, CliCtrl->line.len)) {
CLI_WriteStr ("\r\n");
CLI_WriteStr (CliCtrl->cmdlist->commands[i].name);
}
}
CLI_Prompt ();
}
}
}
}
#endif
#ifdef CLI_HAS_HISTORY
/**
* Set history related flags.
*
* @param flags Bitmask of flags to set
*/
static void CLI_HistorySetFlags (int flags)
{
CliCtrl->history.flags |= flags;
}
/**
* Clear history related flags.
*
* @param flags Bitmask of flags to clear
*/
static void CLI_HistoryClearFlags (int flags)
{
CliCtrl->history.flags &= ~flags;
}
/**
* Add a command to the history.
*
* @param flags Bitmask of flags
*/
static void CLI_HistoryAdd (int flags)
{
size_t i;
int last_non_temp;
// Ignore duplicates
if (flags & CLI_HIST_IGNORE_DUP) {
last_non_temp = CliCtrl->history.len - 1;
if (CliCtrl->history.flags & CLI_HIST_TEMPORARY) {
last_non_temp--;
}
if (last_non_temp >= 0) {
if (strncmp (CliCtrl->history.lines[last_non_temp], CliCtrl->line.buf, CLI_LINE_BUFSIZE) == 0) {
if (CliCtrl->history.flags & CLI_HIST_TEMPORARY) {
CliCtrl->history.len--;
CLI_HistoryClearFlags (CLI_HIST_TEMPORARY);
}
CliCtrl->history.pos = CliCtrl->history.len;
return;
}
}
}
// Find a place in history to add the new line
if (CliCtrl->history.flags & CLI_HIST_TEMPORARY) {
// Last entry is temporary, reuse it
if (CliCtrl->history.len > 0) {
CliCtrl->history.len--;
}
} else {
// If history is full, shift and make room
if (CliCtrl->history.len >= CLI_HISTORY) {
for (i = 0; i < CLI_HISTORY - 1; i++) {
strncpy (CliCtrl->history.lines[i], CliCtrl->history.lines[i + 1], CLI_LINE_BUFSIZE);
}
CliCtrl->history.len = CLI_HISTORY - 1;
}
}
// Add current line to the last position in history
strncpy (CliCtrl->history.lines[CliCtrl->history.len], CliCtrl->line.buf, CLI_LINE_BUFSIZE);
CliCtrl->history.len++;
CliCtrl->history.pos = CliCtrl->history.len;
// Remember the 'temporary' flag
if (flags & CLI_HIST_TEMPORARY) {
CLI_HistorySetFlags (CLI_HIST_TEMPORARY);
} else {
CLI_HistoryClearFlags (CLI_HIST_TEMPORARY);
}
}
/**
* Fetch a command from the history and place it on the command line.
*
* @param step Direction to step in history
*/
static void CLI_HistoryFetch (int step)
{
char *line;
int pos;
// If line buffer is modified, add it as a temporary entry to the history
if (CliCtrl->history.flags & CLI_HIST_DIRTY) {
if (CliCtrl->line.len < CLI_LINE_BUFSIZE) {
CliCtrl->line.buf[CliCtrl->line.len] = '\0';
CLI_HistoryAdd (CLI_HIST_TEMPORARY);
CliCtrl->history.pos--;
}
CLI_HistoryClearFlags (CLI_HIST_DIRTY);
}
// Walk the history
pos = CliCtrl->history.pos;
pos += step;
if (pos < 0) {
pos = 0;
} else if (pos > (CliCtrl->history.len - 1)) {
pos = (CliCtrl->history.len - 1);
}
CliCtrl->history.pos = pos;
// Load the command from history
line = CliCtrl->history.lines[CliCtrl->history.pos];
CLI_ReplaceCmd (line, strlen (line));
}
/**
* Marks history to be cleared in the next call to CLI_Parse().
*/
void CLI_HistoryClear (void)
{
CLI_HistorySetFlags (CLI_HIST_CLEAR);
}
#endif // CLI_HAS_HISTORY
/**
* Execute a command with given arguments.
*
* @param argc Number of arguments in argv
* @param argv Arguments, first argument is command name
*
* @return Result returned by the command or '127' in case command is not found
*/
static int CLI_Exec (int argc, char **argv)
{
size_t i;
CLI_WriteStr ("\r\n");
if (CliCtrl->cmdlist != NULL) {
for (i = 0; i < CliCtrl->cmdlist->count; i++) {
if (!strcmp (argv[0], CliCtrl->cmdlist->commands[i].name)) {
return CliCtrl->cmdlist->commands[i].callback (argc, argv);
}
}
}
CLI_WriteStr ("CLI_Exec: ");
CLI_WriteStr (argv[0]);
CLI_WriteStr (": command not found");
return 127;
}
/**
* Handle special keys (command line sequences)
*
* @param cmd Command identifier
*/
static void CLI_HandleCSI (uint8_t cmd)
{
switch (cmd) {
case 'A':
// UP
#ifdef CLI_HAS_HISTORY
CLI_HistoryFetch (-1);
#endif
break;
case 'B':
// DOWN
#ifdef CLI_HAS_HISTORY
CLI_HistoryFetch (1);
#endif
break;
case 'C':
// RIGHT
break;
case 'D':
// LEFT
break;
}
}
/**
* Add a character to the command line.
*
* @param c Character to add
*/
static void CLI_AddChar (char c)
{
uint8_t bits;
if (CliCtrl->line.len < CLI_LINE_BUFSIZE) {
// Add the byte to the command buffer
CliCtrl->line.buf[CliCtrl->line.len] = c;
CliCtrl->line.len++;
#ifdef CLI_HAS_HISTORY
CLI_HistorySetFlags (CLI_HIST_DIRTY);
#endif
} else {
// If it doesn't fit into buffer, then only count code points
bits = (c & 0xC0);
if (!CliCtrl->utf8_enabled || (bits != 0x80)) {
CliCtrl->line.len++;
}
}
CLI_Write (&c, 1);
}
/**
* Delete a character from the command line.
*/
static void CLI_DelChar (void)
{
uint8_t bits;
if (CliCtrl->line.len > 0) {
CLI_WriteStr ("\x08 \x08");
// Remove the whole code point from the buffer -
// no code point validation is done
while (CliCtrl->line.len > 0) {
CliCtrl->line.len--;
if (CliCtrl->line.len < CLI_LINE_BUFSIZE) {
bits = (CliCtrl->line.buf[CliCtrl->line.len] & 0xC0);
if (!CliCtrl->utf8_enabled || (bits != 0x80)) {
break;
}
} else {
break;
}
}
}
}
/**
* Parse incoming data and control the command line interface.
*
* @param ptr Buffer of incoming data
* @param len Length of the buffer
*
* @return Number of bytes parsed before the end of the buffer or the end
* of the current command was reached
*/
int CLI_Parse (const char *ptr, size_t len)
{
static uint8_t ignore_next_newline = 0;
static uint8_t esc = 0;
static uint8_t tab = 0;
size_t i;
// If CLI_Parse() is called before previous line is handled then drop it
if (CliCtrl->line.ready) {
CliCtrl->line.len = 0;
CliCtrl->line.ready = 0;
}
#ifdef CLI_HAS_HISTORY
// If CLI_HIST_CLEAR is set, then clear the history now
if (CliCtrl->history.flags & CLI_HIST_CLEAR) {
memset (&(CliCtrl->history), 0, sizeof (cli_history_t));
CLI_HistoryClearFlags (CLI_HIST_CLEAR);
}
#endif
for (i = 0; i < len; i++) {
// ANSI escape code
if (esc) {
if (esc == 1) {
if (ptr[i] >= 64 && ptr[i] <= 95) {
if (ptr[i] == '[') {
// Control Sequence Introducer (CSI)
esc = 2;
} else {
// Sequence complete
CLI_HandleCSI (ptr[i]);
esc = 0;
}
continue;
}
} else {
if (ptr[i] >= 64 && ptr[i] <= 126) {
// Sequence complete
CLI_HandleCSI (ptr[i]);
esc = 0;
continue;
}
}
esc = 0;
}
if (ptr[i] == '\r' || ptr[i] == '\n') {
if (!ignore_next_newline || ptr[i] != '\n') {
// Handle the received command
if (CliCtrl->line.len) {
CliCtrl->line.ready = 1;
return (i + 1);
} else {
CLI_Prompt ();
}
}
} else if (ptr[i] == '\b' || ptr[i] == 127) {
CLI_DelChar ();
#ifdef CLI_HAS_HISTORY
CLI_HistorySetFlags (CLI_HIST_DIRTY);
#endif
} else if (ptr[i] == '\t') {
#ifdef CLI_HAS_AUTOCOMPLETE
CLI_AutoComplete (tab);
#endif
tab++;
} else if (ptr[i] == 27) {
// ANSI ESC
esc = 1;
} else if (ptr[i] >= 32) {
CLI_AddChar (ptr[i]);
} else {
// Unrecognizable character
// printf ("%d\r\n", ptr[i]);
}
// Handle CRLF-s
if (ptr[i] == '\r') {
ignore_next_newline = 1;
} else {
ignore_next_newline = 0;
}
// Reset the TAB counter
if (ptr[i] != '\t') {
tab = 0;
}
}
return len;
}
/**
* Handle the last received command.
*
* @return The result of the executed command
*/
int CLI_HandleLine (void)
{
size_t i;
char c;
int res = -1;
if (CliCtrl->line.ready) {
// Leave room for terminating '\0'
if (CliCtrl->line.len < CLI_LINE_BUFSIZE) {
size_t argstart = 0;
size_t bufpos = 0;
char *argv[CLI_ARGV_LEN];
int argc = 0;
uint8_t is_squotes = 0;
uint8_t is_dquotes = 0;
uint8_t is_escape = 0;
CliCtrl->line.buf[CliCtrl->line.len] = '\0';
#ifdef CLI_HAS_HISTORY
CLI_HistoryAdd (CLI_HIST_IGNORE_DUP);
CLI_HistorySetFlags (CLI_HIST_DIRTY);
#endif
for (i = 0; i < CliCtrl->line.len; i++) {
c = CliCtrl->line.buf[i];
if (is_escape) {
CliCtrl->line.buf[bufpos++] = c;
is_escape = 0;
} else {
if (is_squotes) {
if (c == '\'') {
is_squotes = 0;
} else {
CliCtrl->line.buf[bufpos++] = c;
}
} else if (is_dquotes) {
if (c == '\"') {
is_dquotes = 0;
} else if (c == '\\') {
is_escape = 1;
} else {
CliCtrl->line.buf[bufpos++] = c;
}
} else {
if (c == '\\') {
is_escape = 1;
} else if (c == '\'') {
is_squotes = 1;
} else if (c == '\"') {
is_dquotes = 1;
} else if (c == ' ') {
if (bufpos - argstart > 0) {
CliCtrl->line.buf[bufpos++] = '\0';
if (argc < CLI_ARGV_LEN) {
argv[argc] = &(CliCtrl->line.buf[argstart]);
}
argc++;
}
argstart = bufpos;
} else {
CliCtrl->line.buf[bufpos++] = c;
}
}
}
}
if (is_escape || is_squotes || is_dquotes) {
CLI_WriteStr ("\r\nCLI_HandleLine: command parse error");
} else if ((argc > CLI_ARGV_LEN) || ((argc == CLI_ARGV_LEN) && (bufpos - argstart > 0))) {
CLI_WriteStr ("\r\nCLI_HandleLine: too many arguments");
} else {
// Handle last argument
if (bufpos - argstart > 0) {
CliCtrl->line.buf[bufpos++] = '\0';
argv[argc++] = &(CliCtrl->line.buf[argstart]);
}
if (argc > 0) {
// Set all unused arguments refer to empty string, to avoid accidental invalid dereferences
// by command handlers (in case they do not check the argument count)
char *empty_string = "";
for (i = argc; i < (sizeof (argv) / sizeof (char *)); i++) {
argv[i] = empty_string;
}
res = CLI_Exec (argc, argv);
}
}
} else {
CLI_WriteStr ("\r\nCLI_HandleLine: command too long");
}
CliCtrl->line.len = 0;
CliCtrl->line.ready = 0;
CLI_Prompt ();
}
return res;
}
/**
* Enable/disable UTF-8 support (only affects backspace).
*
* @param enable 0 - disable, 1 - enable
*/
void CLI_Utf8 (uint8_t enable)
{
CliCtrl->utf8_enabled = enable;
}
/**
* Initialize the driver.
*
* @param cmdlist List of available commands and their handlers
*/
void CLI_Init (const cli_cmd_list_t *cmdlist)
{
memset (CliCtrl, 0, sizeof (cli_control_t));
CliCtrl->cmdlist = cmdlist;
CLI_Utf8 (1);
CLI_Prompt ();
}