119
119
#include <sys/types.h>
120
120
#include <sys/ioctl.h>
121
121
#include "linenoise.h"
122
+ #include "utf8.h"
122
123
123
124
#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
124
125
#define LINENOISE_MAX_LINE 4096
@@ -140,7 +141,7 @@ static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
140
141
static int history_len = 0 ;
141
142
static char * * history = NULL ;
142
143
143
- enum KEY_ACTION {
144
+ enum KEY_ACTION {
144
145
KEY_NULL = 0 , /* NULL */
145
146
CTRL_A = 1 , /* Ctrl+a */
146
147
CTRL_B = 2 , /* Ctrl-b */
@@ -189,41 +190,52 @@ FILE *lndebug_fp = NULL;
189
190
#endif
190
191
191
192
/* ========================== Encoding functions ============================= */
193
+
194
+ typedef size_t (linenoisePrevCharLen )(const char * buf , size_t buf_len , size_t pos , size_t * col_len );
195
+ typedef size_t (linenoiseNextCharLen )(const char * buf , size_t buf_len , size_t pos , size_t * col_len );
196
+ typedef size_t (linenoiseReadCode )(int fd , char * buf , size_t buf_len , int * c );
197
+
198
+ /* Set default encoding functions */
199
+ static linenoisePrevCharLen * prevCharLen = linenoiseUtf8PrevCharLen ;
200
+ static linenoiseNextCharLen * nextCharLen = linenoiseUtf8NextCharLen ;
201
+ static linenoiseReadCode * readCode = linenoiseUtf8ReadCode ;
202
+
192
203
/* Get byte length and column length of the previous character */
193
- static size_t defaultPrevCharLen (const char * buf , size_t buf_len , size_t pos , size_t * col_len ) {
204
+ static size_t asciiPrevCharLen (const char * buf , size_t buf_len , size_t pos , size_t * col_len ) {
194
205
UNUSED (buf ); UNUSED (buf_len ); UNUSED (pos );
195
206
if (col_len != NULL ) * col_len = 1 ;
196
207
return 1 ;
197
208
}
198
209
199
210
/* Get byte length and column length of the next character */
200
- static size_t defaultNextCharLen (const char * buf , size_t buf_len , size_t pos , size_t * col_len ) {
211
+ static size_t asciiNextCharLen (const char * buf , size_t buf_len , size_t pos , size_t * col_len ) {
201
212
UNUSED (buf ); UNUSED (buf_len ); UNUSED (pos );
202
213
if (col_len != NULL ) * col_len = 1 ;
203
214
return 1 ;
204
215
}
205
216
206
217
/* Read bytes of the next character */
207
- static size_t defaultReadCode (int fd , char * buf , size_t buf_len , int * c ) {
218
+ static size_t asciiReadCode (int fd , char * buf , size_t buf_len , int * c ) {
208
219
if (buf_len < 1 ) return -1 ;
209
220
int nread = read (fd ,& buf [0 ],1 );
210
221
if (nread == 1 ) * c = buf [0 ];
211
222
return nread ;
212
223
}
213
224
214
- /* Set default encoding functions */
215
- static linenoisePrevCharLen * prevCharLen = defaultPrevCharLen ;
216
- static linenoiseNextCharLen * nextCharLen = defaultNextCharLen ;
217
- static linenoiseReadCode * readCode = defaultReadCode ;
218
-
219
- /* Set used defined encoding functions */
220
- void linenoiseSetEncodingFunctions (
221
- linenoisePrevCharLen * prevCharLenFunc ,
222
- linenoiseNextCharLen * nextCharLenFunc ,
223
- linenoiseReadCode * readCodeFunc ) {
224
- prevCharLen = prevCharLenFunc ;
225
- nextCharLen = nextCharLenFunc ;
226
- readCode = readCodeFunc ;
225
+ /* Set string encoding functions */
226
+ void linenoiseSetEncoding (enum STRING_ENCODING encoding ) {
227
+ switch (encoding ) {
228
+ case ASCII :
229
+ prevCharLen = asciiPrevCharLen ;
230
+ nextCharLen = asciiNextCharLen ;
231
+ readCode = asciiReadCode ;
232
+ break ;
233
+ case UTF8 :
234
+ prevCharLen = linenoiseUtf8PrevCharLen ;
235
+ nextCharLen = linenoiseUtf8NextCharLen ;
236
+ readCode = linenoiseUtf8ReadCode ;
237
+ break ;
238
+ }
227
239
}
228
240
229
241
/* Get column length from begining of buffer to current byte position */
0 commit comments