2
2
#include < stdlib.h>
3
3
#include < string.h>
4
4
#include < sys/select.h>
5
+
5
6
#include " linenoise.h"
6
7
7
- static void completion (const char *buf, linenoiseCompletions *lc) {
8
+ static void completion (const char * buf, linenoiseCompletions * lc) {
8
9
if (buf[0 ] == ' h' ) {
9
- linenoiseAddCompletion (lc," hello" );
10
- linenoiseAddCompletion (lc," hello there" );
11
- linenoiseAddCompletion (lc," hello 😀" );
10
+ linenoiseAddCompletion (lc, " hello" );
11
+ linenoiseAddCompletion (lc, " hello there" );
12
+ linenoiseAddCompletion (lc, " hello 😀" );
12
13
}
13
14
14
15
const auto こ = " こ" ;
15
16
if (!strncmp (buf, こ, strlen (こ))) {
16
- linenoiseAddCompletion (lc," こんにちは hello" );
17
- linenoiseAddCompletion (lc," こんにちは hello there" );
18
- linenoiseAddCompletion (lc," こんにちは hello 😀" );
17
+ linenoiseAddCompletion (lc, " こんにちは hello" );
18
+ linenoiseAddCompletion (lc, " こんにちは hello there" );
19
+ linenoiseAddCompletion (lc, " こんにちは hello 😀" );
19
20
}
20
21
}
21
22
22
- static const char *hints (const char *buf, int *color, int *bold ) {
23
- if (!strcasecmp (buf," hello" )) {
23
+ static const char * hints (const char * buf, int * color, int * bold ) {
24
+ if (!strcasecmp (buf, " hello" )) {
24
25
*color = 35 ;
25
- *bold = 0 ;
26
+ *bold = 0 ;
26
27
return " World" ;
27
28
}
28
- if (!strcasecmp (buf," こんにちは" )) {
29
+ if (!strcasecmp (buf, " こんにちは" )) {
29
30
*color = 35 ;
30
- *bold = 0 ;
31
+ *bold = 0 ;
31
32
return " 世界" ;
32
33
}
33
34
return NULL ;
34
35
}
35
36
36
- int main (int argc, char **argv) {
37
- const char *line;
38
- char *prgname = argv[0 ];
39
- int async = 0 ;
37
+ int main (int argc, char ** argv) {
38
+ const char * line;
39
+ char * prgname = argv[0 ];
40
+ int async = 0 ;
40
41
41
42
/* Parse options, with --multiline we enable multi line editing. */
42
- while (argc > 1 ) {
43
+ while (argc > 1 ) {
43
44
argc--;
44
45
argv++;
45
- if (!strcmp (*argv," --multiline" )) {
46
+ if (!strcmp (*argv, " --multiline" )) {
46
47
linenoiseSetMultiLine (1 );
47
48
printf (" Multi-line mode enabled.\n " );
48
- } else if (!strcmp (*argv," --keycodes" )) {
49
+ } else if (!strcmp (*argv, " --keycodes" )) {
49
50
linenoisePrintKeyCodes ();
50
51
exit (0 );
51
- } else if (!strcmp (*argv," --async" )) {
52
+ } else if (!strcmp (*argv, " --async" )) {
52
53
async = 1 ;
53
54
} else {
54
55
fprintf (stderr, " Usage: %s [--multiline] [--keycodes] [--async]\n " , prgname);
@@ -72,28 +73,30 @@ int main(int argc, char **argv) {
72
73
* The typed string is returned as a malloc() allocated string by
73
74
* linenoise, so the user needs to free() it. */
74
75
75
- while (1 ) {
76
+ while (1 ) {
76
77
if (!async) {
77
78
line = linenoise (" 😀 \033 [32mhello\x1b [0m> " );
78
- if (line == NULL ) break ;
79
+ if (line == NULL ) {
80
+ break ;
81
+ }
79
82
} else {
80
83
/* Asynchronous mode using the multiplexing API: wait for
81
84
* data on stdin, and simulate async data coming from some source
82
85
* using the select(2) timeout. */
83
86
struct linenoiseState ls;
84
- char buf[1024 ];
85
- linenoiseEditStart (&ls,-1 ,-1 ,buf,sizeof (buf)," hello> " );
86
- while (1 ) {
87
- fd_set readfds;
87
+ char buf[1024 ];
88
+ linenoiseEditStart (&ls, -1 , -1 , buf, sizeof (buf), " hello> " );
89
+ while (1 ) {
90
+ fd_set readfds;
88
91
struct timeval tv;
89
- int retval;
92
+ int retval;
90
93
91
94
FD_ZERO (&readfds);
92
95
FD_SET (ls.ifd , &readfds);
93
- tv.tv_sec = 1 ; // 1 sec timeout
96
+ tv.tv_sec = 1 ; // 1 sec timeout
94
97
tv.tv_usec = 0 ;
95
98
96
- retval = select (ls.ifd + 1 , &readfds, NULL , NULL , &tv);
99
+ retval = select (ls.ifd + 1 , &readfds, NULL , NULL , &tv);
97
100
if (retval == -1 ) {
98
101
perror (" select()" );
99
102
exit (1 );
@@ -102,7 +105,9 @@ int main(int argc, char **argv) {
102
105
/* A NULL return means: line editing is continuing.
103
106
* Otherwise the user hit enter or stopped editing
104
107
* (CTRL+C/D). */
105
- if (line != linenoiseEditMore) break ;
108
+ if (line != linenoiseEditMore) {
109
+ break ;
110
+ }
106
111
} else {
107
112
// Timeout occurred
108
113
static int counter = 0 ;
@@ -112,17 +117,19 @@ int main(int argc, char **argv) {
112
117
}
113
118
}
114
119
linenoiseEditStop (&ls);
115
- if (line == NULL ) exit (0 ); /* Ctrl+D/C. */
120
+ if (line == NULL ) {
121
+ exit (0 ); /* Ctrl+D/C. */
122
+ }
116
123
}
117
124
118
125
/* Do something with the string. */
119
126
if (line[0 ] != ' \0 ' && line[0 ] != ' /' ) {
120
127
printf (" echo: '%s'\n " , line);
121
- linenoiseHistoryAdd (line); /* Add to the history. */
128
+ linenoiseHistoryAdd (line); /* Add to the history. */
122
129
linenoiseHistorySave (" history.txt" ); /* Save the history on disk. */
123
- } else if (!strncmp (line," /historylen" ,11 )) {
130
+ } else if (!strncmp (line, " /historylen" , 11 )) {
124
131
/* The "/historylen" command will change the history len. */
125
- int len = atoi (line+ 11 );
132
+ int len = atoi (line + 11 );
126
133
linenoiseHistorySetMaxLen (len);
127
134
} else if (!strncmp (line, " /mask" , 5 )) {
128
135
linenoiseMaskModeEnable ();
@@ -131,7 +138,8 @@ int main(int argc, char **argv) {
131
138
} else if (line[0 ] == ' /' ) {
132
139
printf (" Unrecognized command: %s\n " , line);
133
140
}
134
- free ((void *) line);
141
+ free ((void *) line);
135
142
}
136
143
return 0 ;
137
144
}
145
+
0 commit comments