forked from cjlano/tinysh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinysh.c
688 lines (634 loc) · 15.3 KB
/
tinysh.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
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
/*
* tinysh.c
*
* Minimal portable shell
*
* Copyright (C) 2001 Michel Gutierrez <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "tinysh.h"
#ifndef BUFFER_SIZE
#define BUFFER_SIZE 256
#endif
#ifndef HISTORY_DEPTH
#define HISTORY_DEPTH 16
#endif
#ifndef MAX_ARGS
#define MAX_ARGS 16
#endif
#ifndef PROMPT_SIZE
#define PROMPT_SIZE 16
#endif
#ifndef TOPCHAR
#define TOPCHAR '/'
#endif
typedef unsigned char uchar;
/* redefine some useful and maybe missing utilities to avoid conflicts */
#define strlen tinysh_strlen
#define puts tinysh_puts
#define putchar tinysh_char_out
static void help_fnt(int argc, char **argv);
static tinysh_cmd_t help_cmd={
0,"help","display help","<cr>",help_fnt,0,0,0 };
static uchar input_buffers[HISTORY_DEPTH][BUFFER_SIZE+1]={0};
static uchar trash_buffer[BUFFER_SIZE+1]={0};
static int cur_buf_index=0;
static uchar context_buffer[BUFFER_SIZE+1]={0};
static int cur_context=0;
static int cur_index=0;
static int echo=1;
static char prompt[PROMPT_SIZE+1]="$ ";
static tinysh_cmd_t *root_cmd=&help_cmd;
static tinysh_cmd_t *cur_cmd_ctx=0;
static void *tinysh_arg=0;
/* few useful utilities that may be missing */
static int strlen(uchar *s)
{
int i;
for(i=0;*s;s++,i++);
return i;
}
static void puts(char *s)
{
while(*s)
putchar(*s++);
}
/* callback for help function
*/
static void help_fnt(int argc, char **argv)
{
puts("? display help on given or available commands\n");
puts("<TAB> auto-completion\n");
puts("<cr> execute command line\n");
puts("CTRL-P recall previous input line\n");
puts("CTRL-N recall next input line\n");
puts("<any> treat as input character\n");
}
/*
*/
enum { NULLMATCH,FULLMATCH,PARTMATCH,UNMATCH,MATCH,AMBIG };
/* verify if the non-spaced part of s2 is included at the begining
* of s1.
* return FULLMATCH if s2 equal to s1, PARTMATCH if s1 starts with s2
* but there are remaining chars in s1, UNMATCH if s1 does not start with
* s2
*/
int strstart(uchar *s1, uchar *s2)
{
while(*s1 && *s1==*s2) { s1++; s2++; }
if(*s2==' ' || *s2==0)
{
if(*s1==0)
return FULLMATCH; /* full match */
else
return PARTMATCH; /* partial match */
}
else
return UNMATCH; /* no match */
}
/*
* check commands at given level with input string.
* _cmd: point to first command at this level, return matched cmd
* _str: point to current unprocessed input, return next unprocessed
*/
static int parse_command(tinysh_cmd_t **_cmd, uchar **_str)
{
uchar *str=*_str;
tinysh_cmd_t *cmd;
int matched_len=0;
tinysh_cmd_t *matched_cmd=0;
/* first eliminate first blanks */
while(*str==' ') str++;
if(!*str)
{
*_str=str;
return NULLMATCH; /* end of input */
}
/* first pass: count matches */
for(cmd=*_cmd;cmd;cmd=cmd->next)
{
int ret=strstart(cmd->name,str);
if(ret==FULLMATCH)
{
/* found full match */
while(*str && *str!=' ') str++;
while(*str==' ') str++;
*_str=str;
*_cmd=cmd;
return MATCH;
}
else if (ret==PARTMATCH)
{
if(matched_cmd)
{
*_cmd=matched_cmd;
return AMBIG;
}
else
{
matched_cmd=cmd;
}
}
else /* UNMATCH */
{
}
}
if(matched_cmd)
{
while(*str && *str!=' ') str++;
while(*str==' ') str++;
*_cmd=matched_cmd;
*_str=str;
return MATCH;
}
else
return UNMATCH;
}
/* create a context from current input line
*/
static void do_context(tinysh_cmd_t *cmd, uchar *str)
{
while(*str)
context_buffer[cur_context++]=*str++;
context_buffer[cur_context]=0;
cur_cmd_ctx=cmd;
}
/* execute the given command by calling callback with appropriate
* arguments
*/
static void exec_command(tinysh_cmd_t *cmd, uchar *str)
{
char *argv[MAX_ARGS];
int argc=0;
int i;
/* copy command line to preserve it for history */
for(i=0;i<BUFFER_SIZE;i++)
trash_buffer[i]=str[i];
str=trash_buffer;
/* cut into arguments */
argv[argc++]=cmd->name;
while(*str && argc<MAX_ARGS)
{
while(*str==' ') str++;
if(*str==0)
break;
argv[argc++]=str;
while(*str!=' ' && *str) str++;
if(!*str) break;
*str++=0;
}
/* call command function if present */
if(cmd->function)
{
tinysh_arg=cmd->arg;
cmd->function(argc,&argv[0]);
}
}
/* try to execute the current command line
*/
static int exec_command_line(tinysh_cmd_t *cmd, uchar *_str)
{
uchar *str=_str;
while(1)
{
int ret;
ret=parse_command(&cmd,&str);
if(ret==MATCH) /* found unique match */
{
if(cmd)
{
if(!cmd->child) /* no sub-command, execute */
{
exec_command(cmd,str);
return 0;
}
else
{
if(*str==0) /* no more input, this is a context */
{
do_context(cmd,_str);
return 0;
}
else /* process next command word */
{
cmd=cmd->child;
}
}
}
else /* cmd == 0 */
{
return 0;
}
}
else if(ret==AMBIG)
{
puts("ambiguity: ");
puts(str);
putchar('\n');
return 0;
}
else if(ret==UNMATCH) /* UNMATCH */
{
puts("no match: ");
puts(str);
putchar('\n');
return 0;
}
else /* NULLMATCH */
return 0;
}
}
/* display help for list of commands
*/
static void display_child_help(tinysh_cmd_t *cmd)
{
tinysh_cmd_t *cm;
int len=0;
putchar('\n');
for(cm=cmd;cm;cm=cm->next)
if(len<strlen(cm->name))
len=strlen(cm->name);
for(cm=cmd;cm;cm=cm->next)
if(cm->help)
{
int i;
puts(cm->name);
for(i=strlen(cm->name);i<len+2;i++)
putchar(' ');
puts(cm->help);
putchar('\n');
}
}
/* try to display help for current comand line
*/
static int help_command_line(tinysh_cmd_t *cmd, uchar *_str)
{
uchar *str=_str;
while(1)
{
int ret;
ret=parse_command(&cmd,&str);
if(ret==MATCH && *str==0) /* found unique match or empty line */
{
tinysh_cmd_t *cm;
int len=0;
if(cmd->child) /* display sub-commands help */
{
display_child_help(cmd->child);
return 0;
}
else /* no sub-command, show single help */
{
if(*(str-1)!=' ')
putchar(' ');
if(cmd->usage)
puts(cmd->usage);
puts(": ");
if(cmd->help)
puts(cmd->help);
else
puts("no help available");
putchar('\n');
}
return 0;
}
else if(ret==MATCH && *str)
{ /* continue processing the line */
cmd=cmd->child;
}
else if(ret==AMBIG)
{
puts("\nambiguity: ");
puts(str);
putchar('\n');
return 0;
}
else if(ret==UNMATCH)
{
puts("\nno match: ");
puts(str);
putchar('\n');
return 0;
}
else /* NULLMATCH */
{
if(cur_cmd_ctx)
display_child_help(cur_cmd_ctx->child);
else
display_child_help(root_cmd);
return 0;
}
}
}
/* try to complete current command line
*/
static int complete_command_line(tinysh_cmd_t *cmd, uchar *_str)
{
uchar *str=_str;
while(1)
{
int ret;
int common_len=BUFFER_SIZE;
int _str_len;
int i;
uchar *__str=str;
tinysh_cmd_t *_cmd=cmd;
ret=parse_command(&cmd,&str);
for(_str_len=0;__str[_str_len]&&__str[_str_len]!=' ';_str_len++);
if(ret==MATCH && *str)
{
cmd=cmd->child;
}
else if(ret==AMBIG || ret==MATCH || ret==NULLMATCH)
{
tinysh_cmd_t *cm;
tinysh_cmd_t *matched_cmd=0;
int nb_match=0;
for(cm=cmd;cm;cm=cm->next)
{
int r=strstart(cm->name,__str);
if(r==FULLMATCH)
{
for(i=_str_len;cmd->name[i];i++)
tinysh_char_in(cmd->name[i]);
if(*(str-1)!=' ')
tinysh_char_in(' ');
if(!cmd->child)
{
if(cmd->usage)
{
puts(cmd->usage);
putchar('\n');
return 1;
}
else
return 0;
}
else
{
cmd=cmd->child;
break;
}
}
else if(r==PARTMATCH)
{
nb_match++;
if(!matched_cmd)
{
matched_cmd=cm;
common_len=strlen(cm->name);
}
else
{
for(i=_str_len;cm->name[i] && i<common_len &&
cm->name[i]==matched_cmd->name[i];i++);
if(i<common_len)
common_len=i;
}
}
}
if(cm)
continue;
if(matched_cmd)
{
if(_str_len==common_len)
{
putchar('\n');
for(cm=cmd;cm;cm=cm->next)
{
int r=strstart(cm->name,__str);
if(r==FULLMATCH || r==PARTMATCH)
{
puts(cm->name);
putchar('\n');
}
}
return 1;
}
else
{
for(i=_str_len;i<common_len;i++)
tinysh_char_in(matched_cmd->name[i]);
if(nb_match==1)
tinysh_char_in(' ');
}
}
return 0;
}
else /* UNMATCH */
{
return 0;
}
}
}
/* start a new line
*/
static void start_of_line()
{
/* display start of new line */
puts(prompt);
if(cur_context)
{
puts(context_buffer);
puts("> ");
}
cur_index=0;
}
/* character input
*/
static void _tinysh_char_in(uchar c)
{
uchar *line=input_buffers[cur_buf_index];
if(c=='\n' || c=='\r') /* validate command */
{
tinysh_cmd_t *cmd;
int context=0;
/* first, echo the newline */
if(echo)
putchar(c);
while(*line && *line==' ') line++;
if(*line) /* not empty line */
{
cmd=cur_cmd_ctx?cur_cmd_ctx->child:root_cmd;
exec_command_line(cmd,line);
cur_buf_index=(cur_buf_index+1)%HISTORY_DEPTH;
cur_index=0;
input_buffers[cur_buf_index][0]=0;
}
start_of_line();
}
else if(c==TOPCHAR) /* return to top level */
{
if(echo)
putchar(c);
cur_context=0;
cur_cmd_ctx=0;
}
else if(c==8 || c==127) /* backspace */
{
if(cur_index>0)
{
puts("\b \b");
cur_index--;
line[cur_index]=0;
}
}
else if(c==16) /* CTRL-P: back in history */
{
int prevline=(cur_buf_index+HISTORY_DEPTH-1)%HISTORY_DEPTH;
if(input_buffers[prevline][0])
{
line=input_buffers[prevline];
/* fill the rest of the line with spaces */
while(cur_index-->strlen(line))
puts("\b \b");
putchar('\r');
start_of_line();
puts(line);
cur_index=strlen(line);
cur_buf_index=prevline;
}
}
else if(c==14) /* CTRL-N: next in history */
{
int nextline=(cur_buf_index+1)%HISTORY_DEPTH;
if(input_buffers[nextline][0])
{
line=input_buffers[nextline];
/* fill the rest of the line with spaces */
while(cur_index-->strlen(line))
puts("\b \b");
putchar('\r');
start_of_line();
puts(line);
cur_index=strlen(line);
cur_buf_index=nextline;
}
}
else if(c=='?') /* display help */
{
tinysh_cmd_t *cmd;
cmd=cur_cmd_ctx?cur_cmd_ctx->child:root_cmd;
help_command_line(cmd,line);
start_of_line();
puts(line);
cur_index=strlen(line);
}
else if(c==9 || c=='!') /* TAB: autocompletion */
{
tinysh_cmd_t *cmd;
cmd=cur_cmd_ctx?cur_cmd_ctx->child:root_cmd;
if(complete_command_line(cmd,line))
{
start_of_line();
puts(line);
}
cur_index=strlen(line);
}
else /* any input character */
{
if(cur_index<BUFFER_SIZE)
{
if(echo)
putchar(c);
line[cur_index++]=c;
line[cur_index]=0;
}
}
}
/* new character input */
void tinysh_char_in(uchar c)
{
/*
* filter characters here
*/
_tinysh_char_in(c);
}
/* add a new command */
void tinysh_add_command(tinysh_cmd_t *cmd)
{
tinysh_cmd_t *cm;
if(cmd->parent)
{
cm=cmd->parent->child;
if(!cm)
{
cmd->parent->child=cmd;
}
else
{
while(cm->next) cm=cm->next;
cm->next=cmd;
}
}
else if(!root_cmd)
{
root_cmd=cmd;
}
else
{
cm=root_cmd;
while(cm->next) cm=cm->next;
cm->next=cmd;
}
}
/* modify shell prompt
*/
void tinysh_set_prompt(char *str)
{
int i;
for(i=0;str[i] && i<PROMPT_SIZE;i++)
prompt[i]=str[i];
prompt[i]=0;
/* force prompt display by generating empty command */
tinysh_char_in('\r');
}
/* return current command argument
*/
void *tinysh_get_arg()
{
return tinysh_arg;
}
/* string to decimal/hexadecimal conversion
*/
unsigned long tinysh_atoxi(char *s)
{
int ishex=0;
unsigned long res=0;
if(*s==0) return 0;
if(*s=='0' && *(s+1)=='x')
{
ishex=1;
s+=2;
}
while(*s)
{
if(ishex)
res*=16;
else
res*=10;
if(*s>='0' && *s<='9')
res+=*s-'0';
else if(ishex && *s>='a' && *s<='f')
res+=*s+10-'a';
else if(ishex && *s>='A' && *s<='F')
res+=*s+10-'A';
else
break;
s++;
}
return res;
}