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
111
112
115
116
117
118
119
120
121
122
123
124
125
126
127
128
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
204
205
206
207
208
209
210
213
214
215
218
219
220
223
224
225
226
227
228
229
233
234
235
236
239
240
241
242
243
244
245
246
248
249
250
253
254
257
258
259
260
261
266
283
284
285
286
287
288
291
292
293
294
296
297
298
299
300
301
302
303
305
306
307
308
309
310
311
312
313
316
317
319
322
323
324
326
327
330
331
332
333
334
335
336
339
340
341
342
345
346
347
348
349
350
351
352
353
354
358
359
361
365
366
367
368
369
376
377
383
384
385
386
387
388
389
390
393
394
395
396
397
398
399
400
401
402
403
404
405
406
409
410
411
415
416
435
436
437
438
439
440
441
442
443
446
447
449
452
453
455
458
459
463
464
465
466
467
468
469
470
471
475
476
477
478
479
480
481
485
489
490
494
495
496
497
498
499
500
501
502
503
504
507
508
510
511
512
513
514
515
516
517
518
519
520
524
525
526
527
528
529
530
531
532
533
537
538
539
540
541
542
543
544
545
546
551
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
596
597
602
603
604
609
610
611
612
613
614
615
616
617
618
619
620
621
622
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
666
672
673
676
677
678
679
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
716
717
718
719
720
721
722
728
729
730
736
737
738
744
745
746
752
753
755
756
757
758
759
761
762
763
764
765
772
773
774
775
776
777
778
779
781
789
790
791
800
801
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
822
823
824
825
826
827
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
873
874
875
878
879
880
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
912
913
916
917
918
919
920
921
922
923
924
925
926
939
942
943
946
956
965
968
971
974
977
978
979
982
983
984
985
998
999
1002
1005
1008
1011
1014
1017
1018
1019
1020
1021
1022
1032
1033
1036
1041
1046
1049
1052
1056
1059
1060
1061
1062
1063
1064
1065
1068
1069
1070
1071
1072
1073
1074
1075
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1094
1098
1099
1100
1101
1102
1103
1106
1109
1110
1111
1112
1113
1114
1115
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1136
1137
1138
1139
1140
1141
1142
1143
1147
1148
1156
1157
1158
1159
1160
1161
1162
1163
1164
1174
1175
1176
1177
1178
1179
1182
1187
1196
1197
1198
1199
1203
1206
1207
1208
1209
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1237
1238
1239
1240
1241
1243
1244
1245
1250
1251
1252
1253
1254
1255
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1306
1307
1308
1311
1312
1313
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1336
/* ... */
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "sdkconfig.h"5 includes
#if !CONFIG_IDF_TARGET_LINUX
#include <stdio_ext.h> /* ... */
#endif
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include "linenoise.h"11 includes
#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
#define LINENOISE_DEFAULT_MAX_LINE 4096
#define LINENOISE_MINIMAL_MAX_LINE 64
#define LINENOISE_COMMAND_MAX_LEN 32
#define LINENOISE_PASTE_KEY_DELAY 30 5 defines
static linenoiseCompletionCallback *completionCallback = NULL;
static linenoiseHintsCallback *hintsCallback = NULL;
static linenoiseFreeHintsCallback *freeHintsCallback = NULL;
static size_t max_cmdline_length = LINENOISE_DEFAULT_MAX_LINE;
static int mlmode = 0;
static int dumbmode = 0;
static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
static int history_len = 0;
static char **history = NULL;
static bool allow_empty = true;
/* ... */
struct linenoiseState {
char *buf;
size_t buflen;
const char *prompt;
size_t plen;
size_t pos;
size_t oldpos;
size_t len;
size_t cols;
size_t maxrows;
int history_index;
}{ ... };
enum KEY_ACTION{
KEY_NULL = 0,
CTRL_A = 1,
CTRL_B = 2,
CTRL_C = 3,
CTRL_D = 4,
CTRL_E = 5,
CTRL_F = 6,
CTRL_H = 8,
TAB = 9,
CTRL_K = 11,
CTRL_L = 12,
ENTER = 10,
CTRL_N = 14,
CTRL_P = 16,
CTRL_T = 20,
CTRL_U = 21,
CTRL_W = 23,
ESC = 27,
UNIT_SEP = 31,
BACKSPACE = 127
}{ ... };
int linenoiseHistoryAdd(const char *line);
static void refreshLine(struct linenoiseState *l);
#if 0
FILE *lndebug_fp = NULL;
#define lndebug(...) \
do { \
if (lndebug_fp == NULL) { \
lndebug_fp = fopen("/tmp/lndebug.txt","a"); \
fprintf(lndebug_fp, \
"[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \
(int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \
(int)l->maxrows,old_rows); \
}{...} \
fprintf(lndebug_fp, ", " __VA_ARGS__); \
fflush(lndebug_fp); \
}{...} while (0)...
/* ... */#else
#define lndebug(fmt, ...)
#endif
void linenoiseSetMultiLine(int ml) {
mlmode = ml;
}{ ... }
void linenoiseSetDumbMode(int set) {
dumbmode = set;
}{ ... }
bool linenoiseIsDumbMode(void) {
return dumbmode;
}{ ... }
static void flushWrite(void) {
#if !CONFIG_IDF_TARGET_LINUX
if (__fbufsize(stdout) > 0) {
fflush(stdout);
}{...}
#endif/* ... */
fsync(fileno(stdout));
}{ ... }
/* ... */
static int getCursorPosition(void) {
char buf[LINENOISE_COMMAND_MAX_LEN] = { 0 };
int cols = 0;
int rows = 0;
int i = 0;
const int out_fd = fileno(stdout);
const int in_fd = fileno(stdin);
/* ... */
const char get_cursor_cmd[] = "\x1b[6n";
/* ... */
int num_written = write(out_fd, get_cursor_cmd, sizeof(get_cursor_cmd));
if (num_written != sizeof(get_cursor_cmd)) {
return -1;
}{...}
flushWrite();
/* ... */
while (i < sizeof(buf)-1) {
/* ... */
if (read(in_fd, buf + i, 1) != 1 || buf[i] == 'R') {
/* ... */
break;
}{...}
/* ... */
if (buf[i] != '\n') {
i++;
}{...}
}{...}
buf[i] = '\0';
if (buf[0] != ESC || buf[1] != '[' || sscanf(buf+2,"%d;%d",&rows,&cols) != 2) {
return -1;
}{...}
return cols;
}{ ... }
/* ... */
static int getColumns(void) {
int start = 0;
int cols = 0;
int written = 0;
char seq[LINENOISE_COMMAND_MAX_LEN] = { 0 };
const int fd = fileno(stdout);
/* ... */
const char move_cursor_right[] = "\x1b[999C";
const size_t cmd_len = sizeof(move_cursor_right);
const char set_cursor_pos[] = "\x1b[%dD";
start = getCursorPosition();
if (start == -1) {
goto failed;
}{...}
/* ... */
if (write(fd, move_cursor_right, cmd_len) != cmd_len) {
goto failed;
}{...}
flushWrite();
/* ... */
cols = getCursorPosition();
if (cols == -1) {
goto failed;
}{...}
if (cols > start) {
written = snprintf(seq, LINENOISE_COMMAND_MAX_LEN, set_cursor_pos, cols-start);
/* ... */
assert (written < LINENOISE_COMMAND_MAX_LEN);
if (write(fd, seq, written) == -1) {
}{...}
flushWrite();
}{...}
return cols;
failed:
return 80;
}{ ... }
void linenoiseClearScreen(void) {
fprintf(stdout,"\x1b[H\x1b[2J");
flushWrite();
}{ ... }
/* ... */
static void linenoiseBeep(void) {
fprintf(stdout, "\x7");
flushWrite();
}{ ... }
static void freeCompletions(linenoiseCompletions *lc) {
size_t i;
for (i = 0; i < lc->len; i++)
free(lc->cvec[i]);
if (lc->cvec != NULL)
free(lc->cvec);
}{ ... }
/* ... */
static int completeLine(struct linenoiseState *ls) {
linenoiseCompletions lc = { 0, NULL };
int nread, nwritten;
char c = 0;
int in_fd = fileno(stdin);
completionCallback(ls->buf,&lc);
if (lc.len == 0) {
linenoiseBeep();
}{...} else {
size_t stop = 0, i = 0;
while(!stop) {
if (i < lc.len) {
struct linenoiseState saved = *ls;
ls->len = ls->pos = strlen(lc.cvec[i]);
ls->buf = lc.cvec[i];
refreshLine(ls);
ls->len = saved.len;
ls->pos = saved.pos;
ls->buf = saved.buf;
}{...} else {
refreshLine(ls);
}{...}
nread = read(in_fd, &c, 1);
if (nread <= 0) {
freeCompletions(&lc);
return -1;
}{...}
switch(c) {
case TAB:
i = (i+1) % (lc.len+1);
if (i == lc.len) linenoiseBeep();
break;...
case ESC:
if (i < lc.len) refreshLine(ls);
stop = 1;
break;...
default:
if (i < lc.len) {
nwritten = snprintf(ls->buf,ls->buflen,"%s",lc.cvec[i]);
ls->len = ls->pos = nwritten;
}{...}
stop = 1;
break;...
}{...}
}{...}
}{...}
freeCompletions(&lc);
return c;
}{ ... }
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) {
completionCallback = fn;
}{ ... }
/* ... */
void linenoiseSetHintsCallback(linenoiseHintsCallback *fn) {
hintsCallback = fn;
}{ ... }
/* ... */
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *fn) {
freeHintsCallback = fn;
}{ ... }
/* ... */
void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
size_t len = strlen(str);
char *copy, **cvec;
copy = malloc(len+1);
if (copy == NULL) return;
memcpy(copy,str,len+1);
cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1));
if (cvec == NULL) {
free(copy);
return;
}{...}
lc->cvec = cvec;
lc->cvec[lc->len++] = copy;
}{ ... }
/* ... */
struct abuf {
char *b;
int len;
}{ ... };
static void abInit(struct abuf *ab) {
ab->b = NULL;
ab->len = 0;
}{ ... }
static void abAppend(struct abuf *ab, const char *s, int len) {
char *new = realloc(ab->b,ab->len+len);
if (new == NULL) return;
memcpy(new+ab->len,s,len);
ab->b = new;
ab->len += len;
}{ ... }
static void abFree(struct abuf *ab) {
free(ab->b);
}{ ... }
/* ... */
void refreshShowHints(struct abuf *ab, struct linenoiseState *l, int plen) {
char seq[64];
if (hintsCallback && plen+l->len < l->cols) {
int color = -1, bold = 0;
char *hint = hintsCallback(l->buf,&color,&bold);
if (hint) {
int hintlen = strlen(hint);
int hintmaxlen = l->cols-(plen+l->len);
if (hintlen > hintmaxlen) hintlen = hintmaxlen;
if (bold == 1 && color == -1) color = 37;
if (color != -1 || bold != 0) {
snprintf(seq,64,"\033[%d;%d;49m",bold,color);
abAppend(ab,seq,strlen(seq));
}{...}
abAppend(ab,hint,hintlen);
if (color != -1 || bold != 0)
abAppend(ab,"\033[0m",4);
if (freeHintsCallback) freeHintsCallback(hint);
}{...}
}{...}
}{ ... }
/* ... */
static void refreshSingleLine(struct linenoiseState *l) {
char seq[64];
size_t plen = l->plen;
int fd = fileno(stdout);
char *buf = l->buf;
size_t len = l->len;
size_t pos = l->pos;
struct abuf ab;
while((plen+pos) >= l->cols) {
buf++;
len--;
pos--;
}{...}
while (plen+len > l->cols) {
len--;
}{...}
abInit(&ab);
snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq));
abAppend(&ab,l->prompt,strlen(l->prompt));
abAppend(&ab,buf,len);
refreshShowHints(&ab,l,plen);
snprintf(seq,64,"\x1b[0K");
abAppend(&ab,seq,strlen(seq));
snprintf(seq,64,"\r\x1b[%dC", (int)(pos+plen));
abAppend(&ab,seq,strlen(seq));
if (write(fd, ab.b, ab.len) == -1) {}
flushWrite();
abFree(&ab);
}{ ... }
/* ... */
static void refreshMultiLine(struct linenoiseState *l) {
char seq[64];
int plen = l->plen;
int rows = (plen+l->len+l->cols-1)/l->cols;
int rpos = (plen+l->oldpos+l->cols)/l->cols;
int rpos2;
int col;
int old_rows = l->maxrows;
int j;
int fd = fileno(stdout);
struct abuf ab;
if (rows > (int)l->maxrows) l->maxrows = rows;
/* ... */
abInit(&ab);
if (old_rows-rpos > 0) {
lndebug("go down %d", old_rows-rpos);
snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
abAppend(&ab,seq,strlen(seq));
}{...}
for (j = 0; j < old_rows-1; j++) {
lndebug("clear+up");
snprintf(seq,64,"\r\x1b[0K\x1b[1A");
abAppend(&ab,seq,strlen(seq));
}{...}
lndebug("clear");
snprintf(seq,64,"\r\x1b[0K");
abAppend(&ab,seq,strlen(seq));
abAppend(&ab,l->prompt,strlen(l->prompt));
abAppend(&ab,l->buf,l->len);
refreshShowHints(&ab,l,plen);
/* ... */
if (l->pos &&
l->pos == l->len &&
(l->pos+plen) % l->cols == 0)
{
lndebug("<newline>");
abAppend(&ab,"\n",1);
snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq));
rows++;
if (rows > (int)l->maxrows) l->maxrows = rows;
}{...}
rpos2 = (plen+l->pos+l->cols)/l->cols;
lndebug("rpos2 %d", rpos2);
if (rows-rpos2 > 0) {
lndebug("go-up %d", rows-rpos2);
snprintf(seq,64,"\x1b[%dA", rows-rpos2);
abAppend(&ab,seq,strlen(seq));
}{...}
col = (plen+(int)l->pos) % (int)l->cols;
lndebug("set col %d", 1+col);
if (col)
snprintf(seq,64,"\r\x1b[%dC", col);
else
snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq));
lndebug("\n");
l->oldpos = l->pos;
if (write(fd,ab.b,ab.len) == -1) {}
flushWrite();
abFree(&ab);
}{ ... }
/* ... */
static void refreshLine(struct linenoiseState *l) {
if (mlmode)
refreshMultiLine(l);
else
refreshSingleLine(l);
}{ ... }
/* ... */
int linenoiseEditInsert(struct linenoiseState *l, char c) {
int fd = fileno(stdout);
if (l->len < l->buflen) {
if (l->len == l->pos) {
l->buf[l->pos] = c;
l->pos++;
l->len++;
l->buf[l->len] = '\0';
if ((!mlmode && l->plen+l->len < l->cols && !hintsCallback)) {
/* ... */
if (write(fd, &c,1) == -1) {
return -1;
}{...}
flushWrite();
}{...} else {
refreshLine(l);
}{...}
}{...} else {
memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos);
l->buf[l->pos] = c;
l->len++;
l->pos++;
l->buf[l->len] = '\0';
refreshLine(l);
}{...}
}{...}
return 0;
}{ ... }
int linenoiseInsertPastedChar(struct linenoiseState *l, char c) {
int fd = fileno(stdout);
if (l->len < l->buflen && l->len == l->pos) {
l->buf[l->pos] = c;
l->pos++;
l->len++;
l->buf[l->len] = '\0';
if (write(fd, &c,1) == -1) {
return -1;
}{...}
flushWrite();
}{...}
return 0;
}{ ... }
void linenoiseEditMoveLeft(struct linenoiseState *l) {
if (l->pos > 0) {
l->pos--;
refreshLine(l);
}{...}
}{ ... }
void linenoiseEditMoveRight(struct linenoiseState *l) {
if (l->pos != l->len) {
l->pos++;
refreshLine(l);
}{...}
}{ ... }
void linenoiseEditMoveHome(struct linenoiseState *l) {
if (l->pos != 0) {
l->pos = 0;
refreshLine(l);
}{...}
}{ ... }
void linenoiseEditMoveEnd(struct linenoiseState *l) {
if (l->pos != l->len) {
l->pos = l->len;
refreshLine(l);
}{...}
}{ ... }
/* ... */
#define LINENOISE_HISTORY_NEXT 0
#define LINENOISE_HISTORY_PREV 1
void linenoiseEditHistoryNext(struct linenoiseState *l, int dir) {
if (history_len > 1) {
/* ... */
free(history[history_len - 1 - l->history_index]);
history[history_len - 1 - l->history_index] = strdup(l->buf);
l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1;
if (l->history_index < 0) {
l->history_index = 0;
return;
}{...} else if (l->history_index >= history_len) {
l->history_index = history_len-1;
return;
}{...}
strncpy(l->buf,history[history_len - 1 - l->history_index],l->buflen);
l->buf[l->buflen-1] = '\0';
l->len = l->pos = strlen(l->buf);
refreshLine(l);
}{...}
}{ ... }
/* ... */
void linenoiseEditDelete(struct linenoiseState *l) {
if (l->len > 0 && l->pos < l->len) {
memmove(l->buf+l->pos,l->buf+l->pos+1,l->len-l->pos-1);
l->len--;
l->buf[l->len] = '\0';
refreshLine(l);
}{...}
}{ ... }
void linenoiseEditBackspace(struct linenoiseState *l) {
if (l->pos > 0 && l->len > 0) {
memmove(l->buf+l->pos-1,l->buf+l->pos,l->len-l->pos);
l->pos--;
l->len--;
l->buf[l->len] = '\0';
refreshLine(l);
}{...}
}{ ... }
/* ... */
void linenoiseEditDeletePrevWord(struct linenoiseState *l) {
size_t old_pos = l->pos;
size_t diff;
while (l->pos > 0 && l->buf[l->pos-1] == ' ')
l->pos--;
while (l->pos > 0 && l->buf[l->pos-1] != ' ')
l->pos--;
diff = old_pos - l->pos;
memmove(l->buf+l->pos,l->buf+old_pos,l->len-old_pos+1);
l->len -= diff;
refreshLine(l);
}{ ... }
uint32_t getMillis(void) {
struct timeval tv = { 0 };
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}{ ... }
static inline size_t prompt_len_ignore_escape_seq(const char *prompt) {
size_t plen = 0;
bool in_escape_sequence = false;
for (; *prompt != '\0'; ++prompt) {
if (*prompt == '\033') {
in_escape_sequence = true;
}{...} else if (in_escape_sequence && *prompt == 'm') {
in_escape_sequence = false;
}{...} else if (!in_escape_sequence) {
++plen;
}{...}
}{...}
return plen;
}{ ... }
/* ... */
static int linenoiseEdit(char *buf, size_t buflen, const char *prompt)
{
uint32_t t1 = 0;
struct linenoiseState l;
int out_fd = fileno(stdout);
int in_fd = fileno(stdin);
/* ... */
l.buf = buf;
l.buflen = buflen;
l.prompt = prompt;
l.plen = strlen(prompt);
l.oldpos = l.pos = 0;
l.len = 0;
l.cols = getColumns();
l.maxrows = 0;
l.history_index = 0;
l.buf[0] = '\0';
l.buflen--;
/* ... */
linenoiseHistoryAdd("");
if (write(out_fd, prompt,l.plen) == -1) {
return -1;
}{...}
flushWrite();
/* ... */
l.plen = prompt_len_ignore_escape_seq(prompt);
while(1) {
char c;
int nread;
char seq[3];
/* ... */
t1 = getMillis();
nread = read(in_fd, &c, 1);
if (nread <= 0) return l.len;
if ( (getMillis() - t1) < LINENOISE_PASTE_KEY_DELAY && c != ENTER) {
/* ... */
if (linenoiseInsertPastedChar(&l,c)) {
return -1;
}{...}
continue;
}{...}
/* ... */
if (c == 9 && completionCallback != NULL) {
int c2 = completeLine(&l);
if (c2 < 0) return l.len;
if (c2 == 0) continue;
c = c2;
}{...}
switch(c) {
case ENTER:
history_len--;
free(history[history_len]);
if (mlmode) linenoiseEditMoveEnd(&l);
if (hintsCallback) {
/* ... */
linenoiseHintsCallback *hc = hintsCallback;
hintsCallback = NULL;
refreshLine(&l);
hintsCallback = hc;
}{...}
return (int)l.len;...
case CTRL_C:
errno = EAGAIN;
return -1;...
case BACKSPACE:
case 8:
linenoiseEditBackspace(&l);
break;...
case CTRL_D:
/* ... */
if (l.len > 0) {
linenoiseEditDelete(&l);
}{...} else {
history_len--;
free(history[history_len]);
return -1;
}{...}
break;...
case CTRL_T:
if (l.pos > 0 && l.pos < l.len) {
int aux = buf[l.pos-1];
buf[l.pos-1] = buf[l.pos];
buf[l.pos] = aux;
if (l.pos != l.len-1) l.pos++;
refreshLine(&l);
}{...}
break;...
case CTRL_B:
linenoiseEditMoveLeft(&l);
break;...
case CTRL_F:
linenoiseEditMoveRight(&l);
break;...
case CTRL_P:
linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
break;...
case CTRL_N:
linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
break;...
case ESC:
if (read(in_fd, seq, 2) < 2) {
break;
}{...}
if (seq[0] == '[') {
if (seq[1] >= '0' && seq[1] <= '9') {
if (read(in_fd, seq+2, 1) == -1) {
break;
}{...}
if (seq[2] == '~') {
switch(seq[1]) {
case '3':
linenoiseEditDelete(&l);
break;...
}{...}
}{...}
}{...} else {
switch(seq[1]) {
case 'A':
linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
break;...
case 'B':
linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
break;...
case 'C':
linenoiseEditMoveRight(&l);
break;...
case 'D':
linenoiseEditMoveLeft(&l);
break;...
case 'H':
linenoiseEditMoveHome(&l);
break;...
case 'F':
linenoiseEditMoveEnd(&l);
break;...
}{...}
}{...}
}{...}
else if (seq[0] == 'O') {
switch(seq[1]) {
case 'H':
linenoiseEditMoveHome(&l);
break;...
case 'F':
linenoiseEditMoveEnd(&l);
break;...
}{...}
}{...}
break;...
default:
if (linenoiseEditInsert(&l,c)) return -1;
break;...
case CTRL_U:
buf[0] = '\0';
l.pos = l.len = 0;
refreshLine(&l);
break;...
case CTRL_K:
buf[l.pos] = '\0';
l.len = l.pos;
refreshLine(&l);
break;...
case CTRL_A:
linenoiseEditMoveHome(&l);
break;...
case CTRL_E:
linenoiseEditMoveEnd(&l);
break;...
case CTRL_L:
linenoiseClearScreen();
refreshLine(&l);
break;...
case CTRL_W:
linenoiseEditDeletePrevWord(&l);
break;...
}{...}
flushWrite();
}{...}
return l.len;
}{ ... }
void linenoiseAllowEmpty(bool val) {
allow_empty = val;
}{ ... }
int linenoiseProbe(void) {
int stdin_fileno = fileno(stdin);
int flags = fcntl(stdin_fileno, F_GETFL);
flags |= O_NONBLOCK;
int res = fcntl(stdin_fileno, F_SETFL, flags);
if (res != 0) {
return -1;
}{...}
fprintf(stdout, "\x1b[5n");
flushWrite();
int timeout_ms = 500;
const int retry_ms = 10;
size_t read_bytes = 0;
while (timeout_ms > 0 && read_bytes < 4) {
usleep(retry_ms * 1000);
timeout_ms -= retry_ms;
char c;
int cb = read(stdin_fileno, &c, 1);
if (cb < 0) {
continue;
}{...}
if (read_bytes == 0 && c != ESC) {
continue;
}{...}
read_bytes += cb;
}{...}
flags &= ~O_NONBLOCK;
res = fcntl(stdin_fileno, F_SETFL, flags);
if (res != 0) {
return -1;
}{...}
if (read_bytes < 4) {
return -2;
}{...}
return 0;
}{ ... }
static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {
int count;
if (buflen == 0) {
errno = EINVAL;
return -1;
}{...}
count = linenoiseEdit(buf, buflen, prompt);
fputc('\n', stdout);
flushWrite();
return count;
}{ ... }
static int linenoiseDumb(char* buf, size_t buflen, const char* prompt) {
fputs(prompt, stdout);
flushWrite();
size_t count = 0;
while (count < buflen) {
int c = fgetc(stdin);
if (c == '\n') {
break;
}{...} else if (c == BACKSPACE || c == CTRL_H) {
if (count > 0) {
buf[count - 1] = 0;
count--;
fputs("\x08 ", stdout);
flushWrite();
}{...} else {
continue;
}{...}
}{...} else if (c <= UNIT_SEP) {
/* ... */
continue;
}{...} else {
buf[count] = c;
++count;
}{...}
fputc(c, stdout);
flushWrite();
}{...}
fputc('\n', stdout);
flushWrite();
return count;
}{ ... }
static void sanitize(char* src) {
char* dst = src;
for (int c = *src; c != 0; src++, c = *src) {
if (isprint(c)) {
*dst = c;
++dst;
}{...}
}{...}
*dst = 0;
}{ ... }
char *linenoise(const char *prompt) {
char *buf = calloc(1, max_cmdline_length);
int count = 0;
if (buf == NULL) {
return NULL;
}{...}
if (!dumbmode) {
count = linenoiseRaw(buf, max_cmdline_length, prompt);
}{...} else {
count = linenoiseDumb(buf, max_cmdline_length, prompt);
}{...}
if (count > 0) {
sanitize(buf);
count = strlen(buf);
}{...} else if (count == 0 && allow_empty) {
}{...} else {
free(buf);
return NULL;
}{...}
return buf;
}{ ... }
/* ... */
void linenoiseFree(void *ptr) {
free(ptr);
}{ ... }
void linenoiseHistoryFree(void) {
if (history) {
for (int j = 0; j < history_len; j++) {
free(history[j]);
}{...}
free(history);
}{...}
history = NULL;
}{ ... }
/* ... */
int linenoiseHistoryAdd(const char *line) {
char *linecopy;
if (history_max_len == 0) return 0;
if (history == NULL) {
history = malloc(sizeof(char*)*history_max_len);
if (history == NULL) return 0;
memset(history,0,(sizeof(char*)*history_max_len));
}{...}
if (history_len && !strcmp(history[history_len-1], line)) return 0;
/* ... */
linecopy = strdup(line);
if (!linecopy) return 0;
if (history_len == history_max_len) {
free(history[0]);
memmove(history,history+1,sizeof(char*)*(history_max_len-1));
history_len--;
}{...}
history[history_len] = linecopy;
history_len++;
return 1;
}{ ... }
/* ... */
int linenoiseHistorySetMaxLen(int len) {
char **new;
if (len < 1) return 0;
if (history) {
int tocopy = history_len;
new = malloc(sizeof(char*)*len);
if (new == NULL) return 0;
if (len < tocopy) {
int j;
for (j = 0; j < tocopy-len; j++) free(history[j]);
tocopy = len;
}{...}
memset(new,0,sizeof(char*)*len);
memcpy(new,history+(history_len-tocopy), sizeof(char*)*tocopy);
free(history);
history = new;
}{...}
history_max_len = len;
if (history_len > history_max_len)
history_len = history_max_len;
return 1;
}{ ... }
/* ... */
int linenoiseHistorySave(const char *filename) {
FILE *fp;
int j;
fp = fopen(filename,"w");
if (fp == NULL) return -1;
for (j = 0; j < history_len; j++)
fprintf(fp,"%s\n",history[j]);
fclose(fp);
return 0;
}{ ... }
/* ... */
int linenoiseHistoryLoad(const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
return -1;
}{...}
char *buf = calloc(1, max_cmdline_length);
if (buf == NULL) {
fclose(fp);
return -1;
}{...}
while (fgets(buf, max_cmdline_length, fp) != NULL) {
char *p;
p = strchr(buf,'\r');
if (!p) p = strchr(buf,'\n');
if (p) *p = '\0';
linenoiseHistoryAdd(buf);
}{...}
free(buf);
fclose(fp);
return 0;
}{ ... }
/* ... */
int linenoiseSetMaxLineLen(size_t len) {
if (len < LINENOISE_MINIMAL_MAX_LINE) {
return -1;
}{...}
max_cmdline_length = len;
return 0;
}{ ... }