This repository has been archived by the owner on Mar 27, 2021. It is now read-only.
forked from Crest/swdcom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineinput.c
66 lines (55 loc) · 1.39 KB
/
lineinput.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
// Use Linenoise input from console
#ifdef USE_LINENOISE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdint.h>
#include "linenoise/linenoise.h"
#define MAX_HISTORY 2000
const char *SWD_HISTORY = ".swd.history";
char * line;
void completion(const char *buf, linenoiseCompletions *lc) {
if (buf[0] == 'h') {
linenoiseAddCompletion(lc,"hello");
linenoiseAddCompletion(lc,"hello there");
}
}
char *hints(const char *buf, int *color, int *bold) {
if (!strcasecmp(buf,"hello")) {
*color = 35;
*bold = 0;
return " World";
}
return NULL;
}
void linput_init() {
linenoiseSetCompletionCallback(completion);
linenoiseSetHintsCallback(hints);
linenoiseSetMultiLine(1);
linenoiseHistoryLoad(SWD_HISTORY);
linenoiseHistorySetMaxLen(MAX_HISTORY);
}
uint8_t linput_readline(uint8_t*buffer, unsigned int buffer_size) {
if (line) {
free(line);
line = NULL;
}
line = linenoise("input> ");
if (line) {
linenoiseHistoryAdd(line);
// reuse the fixed buffer, reserv latest 2 bytes for \n and 0
strncpy((char*)buffer, line, buffer_size-2);
buffer[buffer_size-1]=0;
unsigned int len = strlen((char*)buffer);
buffer[len]='\n'; // inject newline
return len+1;
}
// empty line
buffer[0]=0;
return 0;
}
void linput_save() {
linenoiseHistorySave(SWD_HISTORY);
}
#endif