Refactoring: editing state is now a structure.
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
diff --git a/linenoise.c b/linenoise.c
index 4632f7d..b04398e 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -109,6 +109,21 @@ static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
static int history_len = 0;
char **history = NULL;
+/* The linenoiseState structure represents the state during line editing.
+ * We pass this state to functions implementing specific editing
+ * functionalities. */
+struct linenoiseState {
+ int fd; /* Terminal file descriptor. */
+ char *buf; /* Edited line buffer. */
+ size_t buflen; /* Edited line buffer size. */
+ const char *prompt; /* Prompt to display. */
+ size_t plen; /* Prompt length. */
+ size_t pos; /* Current cursor position. */
+ size_t len; /* Current edited line length. */
+ size_t cols; /* Number of columns in terminal. */
+ int history_index; /* The history index we are currently editing. */
+};
+
static void linenoiseAtExit(void);
int linenoiseHistoryAdd(const char *line);
@@ -186,7 +201,9 @@ static int getColumns(void) {
return ws.ws_col;
}
-static void refreshLine(int fd, const char *prompt, char *buf, size_t len, size_t pos, size_t cols) {
+static void refreshLineRaw(int fd, const char *prompt, char *buf, size_t len,
+ size_t pos, size_t cols)
+{
char seq[64];
size_t plen = strlen(prompt);
@@ -213,6 +230,10 @@ static void refreshLine(int fd, const char *prompt, char *buf, size_t len, size_
if (write(fd,seq,strlen(seq)) == -1) return;
}
+static void refreshLine(struct linenoiseState *l) {
+ refreshLineRaw(l->fd, l->prompt, l->buf, l->len, l->pos, l->cols);
+}
+
static void beep() {
fprintf(stderr, "\x7");
fflush(stderr);
@@ -226,12 +247,12 @@ static void freeCompletions(linenoiseCompletions *lc) {
free(lc->cvec);
}
-static int completeLine(int fd, const char *prompt, char *buf, size_t buflen, size_t *len, size_t *pos, size_t cols) {
+static int completeLine(struct linenoiseState *ls) {
linenoiseCompletions lc = { 0, NULL };
int nread, nwritten;
char c = 0;
- completionCallback(buf,&lc);
+ completionCallback(ls->buf,&lc);
if (lc.len == 0) {
beep();
} else {
@@ -242,12 +263,12 @@ static int completeLine(int fd, const char *prompt, char *buf, size_t buflen, si
/* Show completion or original buffer */
if (i < lc.len) {
clen = strlen(lc.cvec[i]);
- refreshLine(fd,prompt,lc.cvec[i],clen,clen,cols);
+ refreshLineRaw(ls->fd,ls->prompt,lc.cvec[i],clen,clen,ls->cols);
} else {
- refreshLine(fd,prompt,buf,*len,*pos,cols);
+ refreshLine(ls);
}
- nread = read(fd,&c,1);
+ nread = read(ls->fd,&c,1);
if (nread <= 0) {
freeCompletions(&lc);
return -1;
@@ -260,16 +281,14 @@ static int completeLine(int fd, const char *prompt, char *buf, size_t buflen, si
break;
case 27: /* escape */
/* Re-show original buffer */
- if (i < lc.len) {
- refreshLine(fd,prompt,buf,*len,*pos,cols);
- }
+ if (i < lc.len) refreshLine(ls);
stop = 1;
break;
default:
/* Update buffer and return */
if (i < lc.len) {
- nwritten = snprintf(buf,buflen,"%s",lc.cvec[i]);
- *len = *pos = nwritten;
+ nwritten = snprintf(ls->buf,ls->buflen,"%s",lc.cvec[i]);
+ ls->len = ls->pos = nwritten;
}
stop = 1;
break;
@@ -287,15 +306,25 @@ void linenoiseClearScreen(void) {
}
}
-static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt) {
- size_t plen = strlen(prompt);
- size_t pos = 0;
- size_t len = 0;
- size_t cols = getColumns();
- int history_index = 0;
+static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt)
+{
+ struct linenoiseState l;
+
+ /* Populate the linenoise state that we pass to functions implementing
+ * specific editing functionalities. */
+ l.fd = fd;
+ l.buf = buf;
+ l.buflen = buflen;
+ l.prompt = prompt;
+ l.plen = strlen(prompt);
+ l.pos = 0;
+ l.len = 0;
+ l.cols = getColumns();
+ l.history_index = 0;
size_t old_pos;
size_t diff;
+ /* Buffer starts empty. */
buf[0] = '\0';
buflen--; /* Make sure there is always space for the nulterm */
@@ -303,22 +332,22 @@ static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt)
* initially is just an empty string. */
linenoiseHistoryAdd("");
- if (write(fd,prompt,plen) == -1) return -1;
+ if (write(fd,prompt,l.plen) == -1) return -1;
while(1) {
char c;
int nread;
char seq[2], seq2[2];
nread = read(fd,&c,1);
- if (nread <= 0) return len;
+ if (nread <= 0) return l.len;
/* Only autocomplete when the callback is set. It returns < 0 when
* there was an error reading from fd. Otherwise it will return the
* character that should be handled next. */
if (c == 9 && completionCallback != NULL) {
- c = completeLine(fd,prompt,buf,buflen,&len,&pos,cols);
+ c = completeLine(&l);
/* Return on errors */
- if (c < 0) return len;
+ if (c < 0) return l.len;
/* Read next character when 0 */
if (c == 0) continue;
}
@@ -327,39 +356,39 @@ static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt)
case 13: /* enter */
history_len--;
free(history[history_len]);
- return (int)len;
+ return (int)l.len;
case 3: /* ctrl-c */
errno = EAGAIN;
return -1;
case 127: /* backspace */
case 8: /* ctrl-h */
- if (pos > 0 && len > 0) {
- memmove(buf+pos-1,buf+pos,len-pos);
- pos--;
- len--;
- buf[len] = '\0';
- refreshLine(fd,prompt,buf,len,pos,cols);
+ if (l.pos > 0 && l.len > 0) {
+ memmove(buf+l.pos-1,buf+l.pos,l.len - l.pos);
+ l.pos--;
+ l.len--;
+ buf[l.len] = '\0';
+ refreshLine(&l);
}
break;
case 4: /* ctrl-d, remove char at right of cursor */
- if (len > 1 && pos < (len-1)) {
- memmove(buf+pos,buf+pos+1,len-pos);
- len--;
- buf[len] = '\0';
- refreshLine(fd,prompt,buf,len,pos,cols);
- } else if (len == 0) {
+ if (l.len > 1 && l.pos < (l.len-1)) {
+ memmove(buf+l.pos,buf+l.pos+1,l.len - l.pos);
+ l.len--;
+ buf[l.len] = '\0';
+ refreshLine(&l);
+ } else if (l.len == 0) {
history_len--;
free(history[history_len]);
return -1;
}
break;
case 20: /* ctrl-t */
- if (pos > 0 && pos < len) {
- int aux = buf[pos-1];
- buf[pos-1] = buf[pos];
- buf[pos] = aux;
- if (pos != len-1) pos++;
- refreshLine(fd,prompt,buf,len,pos,cols);
+ 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 2: /* ctrl-b */
@@ -378,16 +407,16 @@ static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt)
if (seq[0] == 91 && seq[1] == 68) {
left_arrow:
/* left arrow */
- if (pos > 0) {
- pos--;
- refreshLine(fd,prompt,buf,len,pos,cols);
+ if (l.pos > 0) {
+ l.pos--;
+ refreshLine(&l);
}
} else if (seq[0] == 91 && seq[1] == 67) {
right_arrow:
/* right arrow */
- if (pos != len) {
- pos++;
- refreshLine(fd,prompt,buf,len,pos,cols);
+ if (l.pos != l.len) {
+ l.pos++;
+ refreshLine(&l);
}
} else if (seq[0] == 91 && (seq[1] == 65 || seq[1] == 66)) {
up_down_arrow:
@@ -395,96 +424,96 @@ up_down_arrow:
if (history_len > 1) {
/* Update the current history entry before to
* overwrite it with tne next one. */
- free(history[history_len-1-history_index]);
- history[history_len-1-history_index] = strdup(buf);
+ free(history[history_len-1-l.history_index]);
+ history[history_len-1-l.history_index] = strdup(buf);
/* Show the new entry */
- history_index += (seq[1] == 65) ? 1 : -1;
- if (history_index < 0) {
- history_index = 0;
+ l.history_index += (seq[1] == 65) ? 1 : -1;
+ if (l.history_index < 0) {
+ l.history_index = 0;
break;
- } else if (history_index >= history_len) {
- history_index = history_len-1;
+ } else if (l.history_index >= history_len) {
+ l.history_index = history_len-1;
break;
}
- strncpy(buf,history[history_len-1-history_index],buflen);
+ strncpy(buf,history[history_len-1-l.history_index],buflen);
buf[buflen] = '\0';
- len = pos = strlen(buf);
- refreshLine(fd,prompt,buf,len,pos,cols);
+ l.len = l.pos = strlen(buf);
+ refreshLine(&l);
}
} else if (seq[0] == 91 && seq[1] > 48 && seq[1] < 55) {
/* extended escape */
if (read(fd,seq2,2) == -1) break;
if (seq[1] == 51 && seq2[0] == 126) {
/* delete */
- if (len > 0 && pos < len) {
- memmove(buf+pos,buf+pos+1,len-pos-1);
- len--;
- buf[len] = '\0';
- refreshLine(fd,prompt,buf,len,pos,cols);
+ if (l.len > 0 && l.pos < l.len) {
+ memmove(buf+l.pos,buf+l.pos+1,l.len-l.pos-1);
+ l.len--;
+ buf[l.len] = '\0';
+ refreshLine(&l);
}
}
}
break;
default:
- if (len < buflen) {
- if (len == pos) {
- buf[pos] = c;
- pos++;
- len++;
- buf[len] = '\0';
- if (plen+len < cols) {
+ if (l.len < buflen) {
+ if (l.len == l.pos) {
+ buf[l.pos] = c;
+ l.pos++;
+ l.len++;
+ buf[l.len] = '\0';
+ if (l.plen+l.len < l.cols) {
/* Avoid a full update of the line in the
* trivial case. */
if (write(fd,&c,1) == -1) return -1;
} else {
- refreshLine(fd,prompt,buf,len,pos,cols);
+ refreshLine(&l);
}
} else {
- memmove(buf+pos+1,buf+pos,len-pos);
- buf[pos] = c;
- len++;
- pos++;
- buf[len] = '\0';
- refreshLine(fd,prompt,buf,len,pos,cols);
+ memmove(buf+l.pos+1,buf+l.pos,l.len-l.pos);
+ buf[l.pos] = c;
+ l.len++;
+ l.pos++;
+ buf[l.len] = '\0';
+ refreshLine(&l);
}
}
break;
case 21: /* Ctrl+u, delete the whole line. */
buf[0] = '\0';
- pos = len = 0;
- refreshLine(fd,prompt,buf,len,pos,cols);
+ l.pos = l.len = 0;
+ refreshLine(&l);
break;
case 11: /* Ctrl+k, delete from current to end of line. */
- buf[pos] = '\0';
- len = pos;
- refreshLine(fd,prompt,buf,len,pos,cols);
+ buf[l.pos] = '\0';
+ l.len = l.pos;
+ refreshLine(&l);
break;
case 1: /* Ctrl+a, go to the start of the line */
- pos = 0;
- refreshLine(fd,prompt,buf,len,pos,cols);
+ l.pos = 0;
+ refreshLine(&l);
break;
case 5: /* ctrl+e, go to the end of the line */
- pos = len;
- refreshLine(fd,prompt,buf,len,pos,cols);
+ l.pos = l.len;
+ refreshLine(&l);
break;
case 12: /* ctrl+l, clear screen */
linenoiseClearScreen();
- refreshLine(fd,prompt,buf,len,pos,cols);
+ refreshLine(&l);
break;
case 23: /* ctrl+w, delete previous word */
- old_pos = pos;
- while (pos > 0 && buf[pos-1] == ' ')
- pos--;
- while (pos > 0 && buf[pos-1] != ' ')
- pos--;
- diff = old_pos - pos;
- memmove(&buf[pos], &buf[old_pos], len-old_pos+1);
- len -= diff;
- refreshLine(fd,prompt,buf,len,pos,cols);
+ old_pos = l.pos;
+ while (l.pos > 0 && buf[l.pos-1] == ' ')
+ l.pos--;
+ while (l.pos > 0 && buf[l.pos-1] != ' ')
+ l.pos--;
+ diff = old_pos - l.pos;
+ memmove(&buf[l.pos], &buf[old_pos], l.len-old_pos+1);
+ l.len -= diff;
+ refreshLine(&l);
break;
}
}
- return len;
+ return l.len;
}
static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {