forked from biot/tokenline
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tokenline.c
1205 lines (1113 loc) · 28.6 KB
/
tokenline.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
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
* Copyright (C) 2019 Karim SUDKI
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "tokenline.h"
#define INDENT " "
#define NO_HELP "No help available."NL
#define NL "\r\n"
#define HYDRABUS_SPECIAL_CHARS "[]{}/\\_-!^&%~"
static void line_clear(t_tokenline *tl);
static void line_backspace(t_tokenline *tl);
static void set_line(t_tokenline *tl, char *line);
static void add_char(t_tokenline *tl, int c);
static void add_char_silent(t_tokenline *tl, int c);
static char space[] = " ";
static void unsplit_line(t_tokenline *tl)
{
int quoted, i;
quoted = FALSE;
for (i = 0; i < tl->buf_len; i++) {
if (tl->buf[i] == '"') {
quoted = TRUE;
continue;
}
if (!tl->buf[i]) {
if (quoted) {
tl->buf[i] = '"';
quoted = FALSE;
} else {
tl->buf[i] = ' ';
}
}
}
tl->buf[tl->buf_len] = 0;
}
static int split_line(t_tokenline *tl, int *words, int *num_words, int silent)
{
int state, quoted, tokened, i, x;
state = 1;
quoted = FALSE;
tokened = FALSE;
*num_words = 0;
for (i = 0; i < tl->buf_len && *num_words < TL_MAX_WORDS; i++) {
switch (state) {
case 1:
/* Looking for a new word. */
if (tl->buf[i] == ' ')
continue;
if (tl->buf[i] == '"')
quoted = TRUE;
if (!quoted && strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i])) {
if(tl->buf[i+1] != ' ' && tl->buf[i+1] != 0 && tl->buf[i+1] != ':' && i < tl->buf_len) {
if((tl->buf_len + 1 < TL_MAX_LINE_LEN) && (*num_words + 1 < TL_MAX_WORDS)) {
tl->pos=i+1;
add_char_silent(tl, ' ');
} else {
if (!silent)
tl->print(tl->user, "Uncompressed form too big"NL);
unsplit_line(tl);
return FALSE;
}
}
}
words[(*num_words)++] = i ;
state = 2;
break;
case 2:
if(!quoted && tl->buf[i] != ' '){
tokened = FALSE;
for (x = 1; tl->token_dict[x].token; x++) {
if (!strncmp(tl->buf+words[(*num_words-1)], tl->token_dict[x].tokenstr,
i-words[(*num_words-1)]+1) && strlen(tl->token_dict[x].tokenstr) > 1)
{
tokened = TRUE;
}
}
if (!tokened && (strchr(HYDRABUS_SPECIAL_CHARS, tl->buf[i]) || tl->buf[i] == '"')){
if (tl->buf[i-1] != ' ' && tl->buf[i-1] != 0 && tl->buf[i-1] != ':') {
if((tl->buf_len + 1 < TL_MAX_LINE_LEN) && (*num_words + 1 < TL_MAX_WORDS)){
tl->pos=i;
add_char_silent(tl, ' ');
} else {
if (!silent)
tl->print(tl->user, "Uncompressed form too big"NL);
unsplit_line(tl);
return FALSE;
}
}
}
}
/* In a word. */
if (quoted && tl->buf[i] == '"') {
quoted = FALSE;
tl->buf[i] = 0;
if (tl->buf[i+1] != ' ') {
if((tl->buf_len + 1 < TL_MAX_LINE_LEN) && (*num_words + 1 < TL_MAX_WORDS)){
tl->pos=i+1;
add_char_silent(tl, ' ');
} else {
if (!silent)
tl->print(tl->user, "Uncompressed form too big"NL);
unsplit_line(tl);
return FALSE;
}
}
state = 1;
} else if (!quoted && tl->buf[i] == ' ') {
tl->buf[i] = 0;
state = 1;
}
break;
}
}
if (quoted) {
if (!silent)
tl->print(tl->user, "Unmatched quote."NL);
unsplit_line(tl);
return FALSE;
}
if (*num_words > TL_MAX_WORDS - 1) {
if (!silent)
tl->print(tl->user, "Too many words."NL);
unsplit_line(tl);
return FALSE;
}
tl->pos=tl->buf_len;
return TRUE;
}
static int history_previous(t_tokenline *tl, int entry)
{
int prev;
if (entry == tl->hist_begin)
return -1;
/* Onto the terminating zero of the previous entry. */
if (--entry < 0)
entry = TL_MAX_HISTORY_SIZE - 1;
/* Onto the last byte of the previous entry. */
prev = entry;
if (--entry < 0)
entry = TL_MAX_HISTORY_SIZE - 1;
while (tl->hist_buf[entry]) {
prev = entry;
if (--entry < 0)
entry = TL_MAX_HISTORY_SIZE - 1;
}
return prev;
}
static void history_up(t_tokenline *tl)
{
int entry;
if (tl->hist_step == -1)
entry = history_previous(tl, tl->hist_end);
else
entry = history_previous(tl, tl->hist_step);
if (entry == -1)
return;
line_clear(tl);
if (tl->hist_begin != 0 && TL_MAX_HISTORY_SIZE == strlen(tl->hist_buf + entry) + entry) {
set_line(tl, tl->hist_buf + entry);
set_line(tl, tl->hist_buf);
} else {
set_line(tl, tl->hist_buf + entry);
}
tl->hist_step = entry;
}
static void history_down(t_tokenline *tl)
{
int i;
if (tl->hist_step == -1)
return;
line_clear(tl);
if (tl->hist_step == tl->hist_end) {
tl->hist_step = -1;
return;
}
i = tl->hist_step;
while (tl->hist_buf[i]) {
if (++i == TL_MAX_HISTORY_SIZE)
i = 0;
}
if (++i == TL_MAX_HISTORY_SIZE)
i = 0;
set_line(tl, tl->hist_buf + i);
tl->hist_step = i;
}
static void history_show(t_tokenline *tl)
{
int entry, i;
/* Skip the 'history' command itself. */
entry = history_previous(tl, tl->hist_end);
for (entry = history_previous(tl, entry); entry != -1;
entry = history_previous(tl, entry)) {
tl->print(tl->user, tl->hist_buf + entry);
/* Did we drop off the end of the buffer? */
for (i = entry; i < TL_MAX_HISTORY_SIZE; i++) {
if (!tl->hist_buf[i])
break;
}
if (i == TL_MAX_HISTORY_SIZE)
/* Yes we did */
tl->print(tl->user, tl->hist_buf);
tl->print(tl->user, NL);
}
}
/* Free at least size, but also fully zero out any deleted entry. */
static int history_delete(t_tokenline *tl, int entry, int size)
{
int freed, i;
freed = 0;
i = entry;
while (freed < size || tl->hist_buf[i]) {
tl->hist_buf[i] = 0;
freed++;
if (++i == TL_MAX_HISTORY_SIZE)
i = 0;
}
tl->hist_buf[i] = 0;
/*
* Skip at least over the terminating zero, and any after that,
* to the next entry.
*/
do {
if (++i == TL_MAX_HISTORY_SIZE)
i = 0;
} while (!tl->hist_buf[i] && i < entry);
return i;
}
static void history_add(t_tokenline *tl)
{
int size, entry, part1;
size = strlen(tl->buf) + 1;
if (tl->hist_begin == tl->hist_end) {
/* First entry, both are 0. */
memcpy(tl->hist_buf, tl->buf, size);
} else if (tl->hist_begin < tl->hist_end) {
if (TL_MAX_HISTORY_SIZE - tl->hist_end >= size) {
memcpy(tl->hist_buf + tl->hist_end, tl->buf, size);
} else {
part1 = TL_MAX_HISTORY_SIZE - tl->hist_end;
entry = tl->hist_begin;
tl->hist_begin = history_delete(tl, entry, size - part1);
memcpy(tl->hist_buf + tl->hist_end, tl->buf, part1);
memcpy(tl->hist_buf, tl->buf + part1, size - part1);
}
} else {
part1 = tl->hist_begin - tl->hist_end;
if (part1 <= size) {
/* Not enough room between end and begin. */
tl->hist_begin = history_delete(tl, tl->hist_begin, size - part1);
part1 = TL_MAX_HISTORY_SIZE - tl->hist_end;
if (part1 < size) {
memcpy(tl->hist_buf + tl->hist_end, tl->buf, part1);
memcpy(tl->hist_buf, tl->buf + part1, size - part1);
} else {
memcpy(tl->hist_buf + tl->hist_end, tl->buf, size);
}
} else {
/* Enough room between end and begin. */
memcpy(tl->hist_buf + tl->hist_end, tl->buf, size);
}
}
tl->hist_end += size;
if (tl->hist_end >= TL_MAX_HISTORY_SIZE)
tl->hist_end -= TL_MAX_HISTORY_SIZE;
if (tl->hist_begin == tl->hist_end)
tl->hist_begin = history_delete(tl, tl->hist_begin,
strlen(tl->hist_buf + tl->hist_begin));
}
/*
* Converts string to uint32_t. Takes decimal, hex prefixed with 0x,
* binary prefixed with 0b and octal prefixed with 0. Returns FALSE
* if conversion in any of these fails.
* endptr: Reference to an object of type char*, whose value is set by
the function to the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
*/
static int str_to_uint(char *s, uint32_t *out, char **endptr)
{
int i;
char *suffix;
char character;
if (endptr != 0)
*endptr = NULL;
if (strncmp(s, "0b", 2)) {
*out = strtoul(s, &suffix, 0);
character = *suffix;
if (character == 'k' ||
character == 'm' ||
character == 'g' )
{
if (endptr != 0)
*endptr = suffix;
suffix++;
}
if (*suffix != '\0')
return FALSE;
} else {
*out = 0;
for (i = 2; s[i]; i++) {
if (s[i] != '0' && s[i] != '1')
return FALSE;
*out <<= 1;
*out |= s[i] - 0x30;
}
}
return TRUE;
}
static char *arg_type_to_string(int arg_type)
{
if (arg_type == T_ARG_UINT)
return "<integer>";
else if (arg_type == T_ARG_FLOAT)
return "<float>";
else if (arg_type == T_ARG_STRING)
return "<string>";
return NULL;
}
static int find_token(t_token *tokens, t_token_dict *token_dict, char *word)
{
uint32_t arg_uint;
int token, partial, i;
/* Find exact match. */
for (i = 0; tokens[i].token; i++) {
token = tokens[i].token;
if (token == T_ARG_UINT) {
if (str_to_uint(word, &arg_uint, NULL))
return i;
} else if (token > T_ARG_UINT) {
continue;
} else {
if (!strcmp(word, token_dict[token].tokenstr))
return i;
}
}
/* Find partial match. */
partial = -1;
for (i = 0; tokens[i].token; i++) {
token = tokens[i].token;
if (token >= T_ARG_UINT)
continue;
if (strlen(word) >= strlen(token_dict[token].tokenstr))
continue;
if (!strncmp(word, token_dict[token].tokenstr, strlen(word))) {
if (partial != -1)
/* Not unique. */
return -1;
else
partial = i;
}
}
return partial;
}
/*
* Tokenize the current set of NULL-terminated words, allowing for
* one token sublevel starting from the current token level.
*/
static int tokenize(t_tokenline *tl, int *words, int num_words,
t_token **complete_tokens, int *complete_arg)
{
t_token *token_stack[8], *arg_tokens;
t_tokenline_parsed *p;
float arg_float;
uint32_t arg_uint, suffix_uint;
int done, arg_needed, w, t, t_idx, size;
int cur_tsp, cur_tp, cur_bufsize, i;
char *word, *suffix;
done = FALSE;
p = &tl->parsed;
token_stack[0] = tl->token_levels[tl->token_level];
cur_tsp = 0;
cur_tp = 0;
cur_bufsize = 0;
arg_needed = 0;
arg_tokens = NULL;
for (w = 0; w < num_words; w++) {
word = tl->buf + words[w];
if (done) {
if (!complete_tokens)
tl->print(tl->user, "Too many arguments."NL);
return FALSE;
} else if (!arg_needed) {
/* Token needed. */
if ((suffix = strchr(word, TL_TOKEN_DELIMITER))) {
*suffix++ = 0;
if (!str_to_uint(suffix, &suffix_uint, NULL)) {
tl->print(tl->user, "Invalid number."NL);
return FALSE;
}
} else {
suffix_uint = 0;
}
if ((t_idx = find_token(token_stack[cur_tsp], tl->token_dict, word)) > -1 && word[0] != '"') {
t = token_stack[cur_tsp][t_idx].token;
if (!(cur_tp + 1 < TL_MAX_WORDS)){
tl->print(tl->user, "Too many words."NL);
return FALSE;
}
p->tokens[cur_tp++] = t;
if (t == T_ARG_UINT) {
/* Integer token. */
str_to_uint(word, &arg_uint, NULL);
if (!(cur_tp + 1 < TL_MAX_WORDS)){
tl->print(tl->user, "Too many words."NL);
return FALSE;
}
p->tokens[cur_tp++] = cur_bufsize;
memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t));
cur_bufsize += sizeof(uint32_t);
}
if (suffix) {
if (!(token_stack[cur_tsp][t_idx].flags &
T_FLAG_SUFFIX_TOKEN_DELIM_INT)) {
if (!complete_tokens)
tl->print(tl->user, "Token suffix not allowed."NL);
return FALSE;
}
if (suffix_uint > 1) {
if (!(cur_tp + 2 < TL_MAX_WORDS)){
tl->print(tl->user, "Too many words."NL);
return FALSE;
}
p->tokens[cur_tp++] = T_ARG_TOKEN_SUFFIX_INT;
p->tokens[cur_tp++] = cur_bufsize;
memcpy(p->buf + cur_bufsize, &suffix_uint, sizeof(uint32_t));
cur_bufsize += sizeof(uint32_t);
*(suffix-1) = TL_TOKEN_DELIMITER;
}
}
p->last_token_entry = &token_stack[cur_tsp][t_idx];
if (token_stack[cur_tsp][t_idx].arg_type == T_ARG_HELP) {
/* Nothing to do, just keep cur_tsp from increasing. */
} else if (token_stack[cur_tsp][t_idx].arg_type) {
/* Token needs an argument */
arg_needed = token_stack[cur_tsp][t_idx].arg_type;
if (arg_needed == T_ARG_TOKEN)
/* Argument is one of these subtokens. */
arg_tokens = token_stack[cur_tsp][t_idx].subtokens;
} else if (token_stack[cur_tsp][t_idx].subtokens) {
/* Switch to a new token set. */
token_stack[cur_tsp + 1] = token_stack[cur_tsp][t_idx].subtokens;
cur_tsp++;
} else {
/* Not expecting any more arguments or tokens. */
done = tl->one_command_per_line;
}
} else {
/*
* No matching token found, but maybe the token
* set allows freeform strings?
*/
for (i = 0; token_stack[cur_tsp][i].token; i++) {
if (token_stack[cur_tsp][i].token == T_ARG_STRING)
break;
}
if (token_stack[cur_tsp][i].token) {
/* Add it in as a token. */
if (word[0] == '"' && word[1] != 0) {
if (!(cur_tp + 2 < TL_MAX_WORDS)){
tl->print(tl->user, "Too many words."NL);
return FALSE;
}
p->tokens[cur_tp++] = T_ARG_STRING;
p->tokens[cur_tp++] = cur_bufsize;
size = strlen(word + 1) + 1;
memcpy(p->buf + cur_bufsize, word + 1, size);
cur_bufsize += size;
p->buf[cur_bufsize] = 0;
} else if (word[0] == '"' && word[1] == 0){
cur_bufsize += 2;
} else {
tl->print(tl->user, "Invalid command."NL);
for (i = 0; i < num_words; i++) {
tl->print(tl->user, tl->buf + words[i]);
if (*(tl->buf + words[i]) == '"') {
tl->print(tl->user, "\"");
}
tl->print(tl->user, " ");
}
tl->print(tl->user, NL);
for(i = 0; i < words[w]; i++) {
tl->print(tl->user, "-");
}
tl->print(tl->user, "^"NL);
return FALSE;
}
} else {
if (!complete_tokens)
tl->print(tl->user, "Invalid command."NL);
return FALSE;
}
}
} else {
/* Parse word as the type in arg_needed */
switch (arg_needed) {
case T_ARG_UINT:
if( str_to_uint(word, &arg_uint, &suffix) == FALSE)
{
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
if (suffix) {
switch(*suffix)
{
case 'k':
arg_uint *= 1000;
break;
case 'm':
arg_uint *= 1000000;
break;
case 'g':
arg_uint *= 1000000000L;
break;
default:
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
}
if (!(cur_tp + 2 < TL_MAX_WORDS)){
tl->print(tl->user, "Too many words."NL);
return FALSE;
}
p->tokens[cur_tp++] = T_ARG_UINT;
p->tokens[cur_tp++] = cur_bufsize;
memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t));
cur_bufsize += sizeof(uint32_t);
break;
case T_ARG_FLOAT:
arg_float = strtof(word, &suffix);
if( arg_float == 0.0F )
{
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
if (*suffix) {
if( strlen(suffix) > 1 )
{
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
switch(*suffix)
{
case 'k':
arg_float *= 1000;
break;
case 'm':
arg_float *= 1000000;
break;
case 'g':
arg_float *= 1000000000L;
break;
default:
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
}
if (!(cur_tp + 2 < TL_MAX_WORDS)){
tl->print(tl->user, "Too many words."NL);
return FALSE;
}
p->tokens[cur_tp++] = T_ARG_FLOAT;
p->tokens[cur_tp++] = cur_bufsize;
memcpy(p->buf + cur_bufsize, &arg_float, sizeof(float));
cur_bufsize += sizeof(float);
break;
case T_ARG_STRING:
if (!(cur_tp + 2 < TL_MAX_WORDS)){
tl->print(tl->user, "Too many words."NL);
return FALSE;
}
p->tokens[cur_tp++] = T_ARG_STRING;
if (word[0] != '"') {
p->tokens[cur_tp++] = cur_bufsize;
size = strlen(word) + 1;
memcpy(p->buf + cur_bufsize, word, size);
} else {
p->tokens[cur_tp++] = cur_bufsize + 1;
size = strlen(word + 1) + 1;
memcpy(p->buf + cur_bufsize + 1, word + 1, size);
}
cur_bufsize += size;
p->buf[cur_bufsize] = 0;
break;
case T_ARG_TOKEN:
if ((t_idx = find_token(arg_tokens, tl->token_dict, word)) > -1) {
if (!(cur_tp + 1 < TL_MAX_WORDS)){
tl->print(tl->user, "Too many words."NL);
return FALSE;
}
p->tokens[cur_tp++] = arg_tokens[t_idx].token;
p->last_token_entry = &arg_tokens[t_idx];
} else {
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
break;
}
arg_needed = 0;
done = tl->one_command_per_line;
}
}
if (arg_needed && !complete_tokens) {
tl->print(tl->user, "Missing argument."NL);
return FALSE;
}
p->tokens[cur_tp] = 0;
if (complete_tokens) {
if (done) {
/* Nothing to add. */
*complete_tokens = NULL;
} else {
/* Fill in the completion token list. */
if (arg_needed == T_ARG_TOKEN)
*complete_tokens = arg_tokens;
else
*complete_tokens = token_stack[cur_tsp];
}
}
if (complete_arg)
*complete_arg = arg_needed;
return TRUE;
}
static void show_help(t_tokenline *tl, int *words, int num_words)
{
t_token *tokens;
int i;
char *s;
(void)words;
if (tl->parsed.last_token_entry) {
if (tl->parsed.last_token_entry->help_full) {
tl->print(tl->user, tl->parsed.last_token_entry->help_full);
tl->print(tl->user, NL);
} else if (tl->parsed.last_token_entry->help) {
tl->print(tl->user, tl->parsed.last_token_entry->help);
tl->print(tl->user, NL);
}
}
if (num_words == 1) {
/* Just "help" -- global command overview. */
tokens = tl->token_levels[tl->token_level];
} else {
if (tl->parsed.last_token_entry) {
tokens = tl->parsed.last_token_entry->subtokens;
} else {
tokens = NULL;
}
}
if (tokens) {
for (i = 0; tokens[i].token; i++) {
tl->print(tl->user, INDENT);
if (tokens[i].token < T_ARG_UINT)
s = tl->token_dict[tokens[i].token].tokenstr;
else
s = arg_type_to_string(tokens[i].token);
tl->print(tl->user, s);
if (tokens[i].help) {
tl->print(tl->user, space + strlen(s));
tl->print(tl->user, tokens[i].help);
}
tl->print(tl->user, NL);
}
}
if ((!tl->parsed.last_token_entry || !tl->parsed.last_token_entry->help)
&& !tokens)
tl->print(tl->user, NO_HELP);
}
static void process_line(t_tokenline *tl)
{
t_token *tokens;
int words[TL_MAX_WORDS], num_words, i;
tl->print(tl->user, NL);
do {
if (!tl->buf_len)
break;
history_add(tl);
if (!split_line(tl, words, &num_words, FALSE))
break;
if (!num_words)
break;
if (!strcmp(tl->buf + words[0], "help")) {
if (num_words == 1) {
/*
* Nothing to tokenize: find the help entry
* if any so its help text can be shown.
*/
tokens = tl->token_levels[tl->token_level];
for (i = 0; tokens[i].token; i++) {
if (tokens[i].arg_type == T_ARG_HELP) {
tl->parsed.last_token_entry = &tokens[i];
break;
}
}
} else {
/* Tokenize with errors turned off. */
tokenize(tl, words + 1, num_words - 1, &tokens, NULL);
}
show_help(tl, words, num_words);
} else if (!strcmp(tl->buf + words[0], "history")) {
history_show(tl);
} else {
if (!tokenize(tl, words, num_words, NULL, NULL))
break;
if (tl->callback)
tl->callback(tl->user, &tl->parsed);
}
} while (FALSE);
tl->buf[0] = 0;
tl->buf_len = 0;
tl->escape_len = 0;
tl->pos = 0;
tl->hist_step = -1;
tl->print(tl->user, tl->prompt);
}
static void add_char_silent(t_tokenline *tl, int c)
{
if (tl->pos == tl->buf_len) {
tl->buf[tl->buf_len++] = c;
tl->buf[tl->buf_len] = 0;
tl->pos++;
} else {
memmove(tl->buf + tl->pos + 1, tl->buf + tl->pos,
tl->buf_len - tl->pos + 1);
tl->buf[tl->pos] = c;
tl->buf_len++;
tl->pos++;
}
}
static void add_char(t_tokenline *tl, int c)
{
int i;
if (tl->pos == tl->buf_len) {
tl->buf[tl->buf_len++] = c;
tl->buf[tl->buf_len] = 0;
tl->print(tl->user, tl->buf + tl->buf_len - 1);
tl->pos++;
} else {
memmove(tl->buf + tl->pos + 1, tl->buf + tl->pos,
tl->buf_len - tl->pos + 1);
tl->buf[tl->pos] = c;
tl->print(tl->user, tl->buf + tl->pos);
for (i = 0; i < tl->buf_len - tl->pos; i++)
tl->print(tl->user, "\x1b\x5b\x44");
tl->buf_len++;
tl->pos++;
}
}
static void set_line(t_tokenline *tl, char *line)
{
int size;
size = strlen(line);
if (size > TL_MAX_LINE_LEN - 1) {
add_char(tl, '!');
return;
}
if (tl->pos == tl->buf_len) {
tl->print(tl->user, line);
tl->pos += size;
memcpy(tl->buf + tl->buf_len, line, size);
tl->buf_len += size;
tl->buf[tl->buf_len] = 0;
}
}
static void print_token_and_help(t_tokenline *tl, t_token *token)
{
char *s;
tl->print(tl->user, INDENT);
if (token->token < T_ARG_UINT)
s = tl->token_dict[token->token].tokenstr;
else
s = arg_type_to_string(token->token);
tl->print(tl->user, s);
if (token->help) {
tl->print(tl->user, space + strlen(s));
tl->print(tl->user, token->help);
}
}
static void complete(t_tokenline *tl)
{
t_token *tokens, *partial;
unsigned int i;
int words[TL_MAX_WORDS], num_words, arg_needed, multiple;
int reprompt, t;
char *word;
reprompt = FALSE;
if (!tl->pos) {
/* Tab on an empty line: show all commmands. */
tl->print(tl->user, NL);
tokens = tl->token_levels[tl->token_level];
for (i = 0; tokens[i].token; i++) {
print_token_and_help(tl, &tokens[i]);
tl->print(tl->user, NL);
}
reprompt = TRUE;
} else if (tl->buf[tl->pos - 1] != ' ') {
/* Try to complete the current word. */
if (!split_line(tl, words, &num_words, TRUE) || !num_words)
return;
if (tokenize(tl, words, num_words - 1, &tokens, NULL)) {
if (tokens) {
word = tl->buf + words[num_words - 1];
partial = NULL;
multiple = FALSE;
for (t = 0; tokens[t].token; t++) {
if (tokens[t].token >= T_ARG_UINT)
continue;
if (!strncmp(word, tl->token_dict[tokens[t].token].tokenstr, strlen(word))) {
if (partial) {
/* Not the first match, print previous one. */
multiple = TRUE;
tl->print(tl->user, NL);
print_token_and_help(tl, partial);
}
partial = &tokens[t];
}
}
if (partial) {
if (multiple) {
/* Last partial match. */
tl->print(tl->user, NL);
print_token_and_help(tl, partial);
tl->print(tl->user, NL);
reprompt = TRUE;
} else {
for (i = strlen(word); i < strlen(tl->token_dict[partial->token].tokenstr); i++)
add_char(tl, tl->token_dict[partial->token].tokenstr[i]);
if(tl->pos+1 < TL_MAX_LINE_LEN - 1)
add_char(tl, ' ');
}
}
}
}
} else {
/* List all possible tokens from this point. */
if (!split_line(tl, words, &num_words, TRUE) || !num_words)
return;
if (tokenize(tl, words, num_words, &tokens, &arg_needed)) {
if (arg_needed && arg_needed != T_ARG_TOKEN) {
tl->print(tl->user, INDENT NL);
tl->print(tl->user, arg_type_to_string(arg_needed));
tl->print(tl->user, NL);
reprompt = TRUE;
} else if (tokens) {
tl->print(tl->user, NL);
for (t = 0; tokens[t].token; t++) {
print_token_and_help(tl, &tokens[t]);
tl->print(tl->user, NL);
reprompt = TRUE;
}
}
}
}
unsplit_line(tl);
if (reprompt) {
tl->print(tl->user, tl->prompt);
tl->print(tl->user, tl->buf);
}
}
static void line_clear(t_tokenline *tl)
{
while (tl->pos < tl->buf_len) {
tl->pos++;
tl->print(tl->user, "\x1b\x5b\x43");
}
while (tl->pos)
line_backspace(tl);
}
static void line_backspace(t_tokenline *tl)
{
int i;
if (tl->pos == tl->buf_len) {
tl->buf[tl->buf_len - 1] = 0;
tl->print(tl->user, "\x1b\x5b\x44 \x1b\x5b\x44");
} else {
memmove(tl->buf + tl->pos - 1, tl->buf + tl->pos,
tl->buf_len - tl->pos + 1);
tl->print(tl->user, "\x1b\x5b\x44");
tl->print(tl->user, tl->buf + tl->pos - 1);
tl->print(tl->user, " ");
for (i = 0; i < tl->buf_len - tl->pos + 1; i++)
tl->print(tl->user, "\x1b\x5b\x44");
}
tl->buf_len--;
tl->pos--;
}
static void line_home(t_tokenline *tl)
{
while (tl->pos) {
tl->pos--;
tl->print(tl->user, "\x1b\x5b\x44");
}
}
static void line_end(t_tokenline *tl)
{
while (tl->pos < tl->buf_len) {
tl->pos++;
tl->print(tl->user, "\x1b\x5b\x43");
}
}
static void line_delete_char(t_tokenline *tl)
{
int i;
if (tl->pos < tl->buf_len) {
memmove(tl->buf + tl->pos, tl->buf + tl->pos + 1,