~arivigo/sline

ref: 18b4d61f6508689a81dfdb772035c7575e83d990 sline/sline.c -rw-r--r-- 10.1 KiB
18b4d61fAriadna Vigo sline 0.6.3 3 months ago
                                                                                
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* See LICENSE for copyright and license details. */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include "history.h"
#include "sline.h"
#include "strlcpy.h"

#define CURSOR_BUF_SIZE 16 /* Used for cursor movement directives */
#define SLINE_PROMPT_DEFAULT "> " 
#define SLINE_PROMPT_SIZE 32
#define UTF8_BYTES 4

enum {
	VT_DEF,
	VT_CHR,
	VT_BKSPC,
	VT_DLT,
	VT_EOF,
	VT_RET,
	VT_UP,
	VT_DWN,
	VT_LFT,
	VT_RGHT,
	VT_HOME,
	VT_END
};

static char *buf_slice(char *src, int pivot, size_t size);
static size_t cursor_end_pos(const char *buf);
static void ln_buf_replace(char *buf, size_t size, const char *src);
static void ln_redraw(const char *str, size_t nbytes);
static int utf8_nbytes(const char *utf8);
static int utf8_nbytes_r(const char *utf8);

static int term_key(char *utf8);
static int term_esc(char *seq);

static void key_up(char *buf, size_t size);
static void key_down(char *buf, size_t size);
static void key_left(char *buf);
static void key_right(char *buf);
static void key_home(void);
static void key_end(char *buf);

static void chr_delete(char *buf, size_t size, int bsmode);
static void chr_ins(char *buf, size_t size, const char *utf8);
static void chr_return(void);

static char sline_prompt[SLINE_PROMPT_SIZE];
static size_t pos, buf_i;
static int sline_history = 1; /* History feature on by default */
static struct termios old, term;

int sline_err = SLINE_ERR_DEF;

/* Auxiliary VT100 related subroutines */

static char *
buf_slice(char *src, int pivot, size_t size)
{
	char *suff, *ptr;
	size_t ptr_size;

	ptr = src + pivot;
	ptr_size = strlen(ptr) + 1;

	if ((suff = calloc(size, sizeof(char))) == NULL)
		return NULL;

	strlcpy(suff, ptr, ptr_size);
	memset(ptr, 0, ptr_size);

	return suff;
}

static size_t
cursor_end_pos(const char *buf)
{
	int i;
	size_t end_pos;

	i = 0;
	end_pos = 0;
	while (buf[i] != '\0') {
		++end_pos;
		i += utf8_nbytes(&buf[i]);
	}

	return end_pos;
}

static void
ln_buf_replace(char *buf, size_t size, const char *src)
{
	memset(buf, 0, size);
	strlcpy(buf, src, size);

	key_home();
	write(STDOUT_FILENO, "\x1b[0K", 4);
	write(STDOUT_FILENO, src, strlen(src));

	buf_i = strlen(buf);
	pos = cursor_end_pos(buf);
}

static void
ln_redraw(const char *str, size_t nbytes)
{
	write(STDOUT_FILENO, "\x1b[0K", 4);
	write(STDOUT_FILENO, "\x1b", 1);
	write(STDOUT_FILENO, "7", 1); /* ESC 7: portable save cursor */
	write(STDOUT_FILENO, str, nbytes);
	write(STDOUT_FILENO, "\x1b", 1);
	write(STDOUT_FILENO, "8", 1); /* ESC 8: portable restore cursor */
}

static int
utf8_nbytes(const char *utf8)
{
	if (utf8[0] >= '\xc0' && utf8[0] <= '\xdf')
		return 2;
	else if (utf8[0] >= '\xe0' && utf8[0] <= '\xef')
		return 3;
	else if (utf8[0] >= '\xf0' && utf8[0] <= '\xf7')
		return 4;

	return 1;
}

static int
utf8_nbytes_r(const char *utf8)
{
	const char *ptr;
	int nbytes;

	for (ptr = utf8; (nbytes = utf8_nbytes(ptr)) == 1; --ptr) {
		/* We stop if we're an ASCII char */
		if ((unsigned char)ptr[0] <= '\x7f')
			return 1;
	}

	return nbytes;
}

static int
term_esc(char *seq)
{
	if (read(STDIN_FILENO, &seq[0], 1) != 1)
		return -1;
	if (read(STDIN_FILENO, &seq[1], 1) != 1)
		return -1;

	if (seq[0] != '[')
		return -1;

	if (seq[1] >= '0' && seq[1] <= '9') {
		if (read(STDIN_FILENO, &seq[2], 1) != 1)
			return -1;
	}

	return 0;
}

static int
term_key(char *utf8)
{
	char key;
	char seq[3];
	int nread, nbytes;

	while ((nread = read(STDIN_FILENO, &key, 1)) != 1) {
		if (nread == -1)
			return -1;
	}

	if (key == '\x1b') {
		if (term_esc(seq) < 0)
			return VT_DEF;

		if (seq[1] == '3' && seq[2] == '~')
			return VT_DLT;

		if ((seq[1] == '1' || seq[1] == '7') && seq[2] == '~')
			return VT_HOME;

		if ((seq[1] == '4' || seq[1] == '8') && seq[2] == '~')
			return VT_END;

		switch (seq[1]) {
		case 'A':
			return VT_UP;
		case 'B':
			return VT_DWN;
		case 'C':
			return VT_RGHT;
		case 'D':
			return VT_LFT;
		case 'H':
			return VT_HOME;
		case 'F':
			return VT_END;
		default:
			return VT_DEF;
		}
	} else if (key == '\x7f') {
		return VT_BKSPC;
	} else if (key == '\x03' || key == '\x04') {
		return VT_EOF;
	} else if (key == '\x0a') {
		return VT_RET;
	} else if ((nbytes = utf8_nbytes(&key)) > 1) {
		utf8[0] = key;
		read(STDIN_FILENO, utf8 + 1, nbytes - 1);
		return VT_CHR;
	} else {
		utf8[0] = key;
		return VT_CHR;
	}
}

static void
key_up(char *buf, size_t size)
{
	const char *hist;

	if (sline_history == 0)
		return;

	if (hist_pos > 0)
		--hist_pos;

	if ((hist = history_get(hist_pos)) == NULL)
		return;

	ln_buf_replace(buf, size, hist);
}

static void
key_down(char *buf, size_t size)
{
	const char *hist;

	if (sline_history == 0)
		return;

	if (hist_pos < hist_top)
		++hist_pos;
	else
		hist_pos = hist_top;

	if ((hist = history_get(hist_pos)) == NULL)
		return;

	ln_buf_replace(buf, size, hist);
}

static void
key_left(char *buf)
{
	int nbytes;

	if (pos == 0)
		return;

	nbytes = utf8_nbytes_r(&buf[buf_i - 1]);
	buf_i -= nbytes;

	write(STDOUT_FILENO, "\x1b[D", 3);
	--pos;
}

static void
key_right(char *buf)
{
	int nbytes;

	if (pos == cursor_end_pos(buf))
		return;

	nbytes = utf8_nbytes(&buf[buf_i]);
	buf_i += nbytes;

	write(STDOUT_FILENO, "\x1b[C", 3);
	++pos;
}

static void
key_home(void)
{
	char cmd[CURSOR_BUF_SIZE];

	if (pos == 0)
		return;

	buf_i = 0;

	snprintf(cmd, CURSOR_BUF_SIZE, "\x1b[%zdD", pos);
	write(STDOUT_FILENO, cmd, strlen(cmd));
	pos = 0;
}


static void
key_end(char *buf)
{
	int i;
	size_t end_pos;
	char cmd[CURSOR_BUF_SIZE];

	if (buf[buf_i] == '\0')
		return;

	i = 0;
	end_pos = 0;
	while (buf[i] != '\0') {
		++end_pos;
		i += utf8_nbytes(&buf[i]);
	}

	buf_i = strlen(buf);

	snprintf(cmd, CURSOR_BUF_SIZE, "\x1b[%zdC", end_pos - pos);
	write(STDOUT_FILENO, cmd, strlen(cmd));
	pos = end_pos;
}

static void
chr_delete(char *buf, size_t size, int bsmode)
{
	char *suff, *suff_new;
	int nbytes;
	size_t len;

	if (bsmode > 0) {
		if (buf_i == 0)
			return;

		nbytes = utf8_nbytes_r(&buf[buf_i - 1]);
		buf_i -= nbytes;
	} else {
		nbytes = utf8_nbytes_r(&buf[buf_i]);
	}

	if ((suff = buf_slice(buf, buf_i, size)) == NULL)
		return;

	suff_new = suff + nbytes; /* Deleting character from suff; way safer */
	len = strlen(suff_new);
	strlcpy(&buf[strlen(buf)], suff_new, len + 1);

	if (bsmode > 0) {
		write(STDOUT_FILENO, "\b", 1);
		--pos;
	}
	ln_redraw(suff_new, len);

	free(suff);

	if (buf_i == 0)
		history_del_top();
	else
		history_set(hist_top, buf);
}

static void
chr_ins(char *buf, size_t size, const char *utf8)
{
	char *suff;
	size_t len;
	int i, nbytes;

	if (pos >= size)
		return;

	if ((suff = buf_slice(buf, buf_i, size)) == NULL)
		return;

	len = strlen(suff);
	nbytes = utf8_nbytes(utf8);
	for (i = 0; i < nbytes; ++i)
		buf[strlen(buf)] = utf8[i];

	strlcpy(&buf[strlen(buf)], suff, len + 1);
	buf_i += nbytes;

	write(STDOUT_FILENO, utf8, nbytes);
	++pos;
	ln_redraw(suff, len);

	free(suff);

	history_set(hist_top, buf);
}

static void
chr_return(void)
{
	write(STDOUT_FILENO, "\n", 1);
	if (sline_history > 0)
		history_next();
}

/* Public sline API subroutines follow */

int
sline(char *buf, int size, const char *init)
{
	char utf8[UTF8_BYTES];
	int key;
	size_t wsize;

	write(STDOUT_FILENO, sline_prompt, SLINE_PROMPT_SIZE);

	/* 
	 * We're always writing one less, so together with the memset() call
	 * below, using wsize will guarantee the last character in buf to 
	 * always be '\0'.
	 */
	wsize = size - 1; 
	memset(buf, 0, size);
	pos = 0;
	buf_i = 0;
	if (init != NULL) {
		/* 
		 * Using size instead of wsize because we're already given a
		 * null terminated string.
		 */
		strlcpy(buf, init, size);
		write(STDOUT_FILENO, buf, size);
		pos = cursor_end_pos(buf);
		buf_i = strlen(buf);
	}

	memset(utf8, 0, UTF8_BYTES);
	hist_pos = hist_top;
	while ((key = term_key(utf8)) != -1) {
		switch (key) {
		case VT_BKSPC:
			chr_delete(buf, size, 1);
			hist_pos = hist_top;
			break;
		case VT_DLT:
			chr_delete(buf, size, 0);
			hist_pos = hist_top;
			break;
		case VT_EOF:
			write(STDOUT_FILENO, "\n", 1);
			sline_err = SLINE_ERR_EOF;
			return -1;
		case VT_RET:
			chr_return();
			return 0;
		case VT_UP:
			key_up(buf, wsize);
			break;
		case VT_DWN:
			key_down(buf, wsize);
			break;
		case VT_LFT:
			key_left(buf);
			break;
		case VT_RGHT:
			key_right(buf);
			break;
		case VT_HOME:
			key_home();
			break;
		case VT_END:
			key_end(buf);
			break;
		case VT_CHR:
			chr_ins(buf, wsize, utf8);
			hist_pos = hist_top;
			break;
		default:
			/* Silently ignore everything that isn't caught. */
			break;
		}

		memset(utf8, 0, UTF8_BYTES);
	}

	/* If we reach this, then term_key() returned -1 */
	sline_err = SLINE_ERR_IO;

	return -1;
}

void
sline_end(void)
{
	int i;

	if (sline_history == 0 || hist_top < 0)
		goto termios;

	for (i = 0; i < HISTORY_SIZE; ++i) {
		if (history[i] != NULL)
			free(history[i]);
	}

termios:
	tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
}

const char *
sline_errmsg(void)
{
	switch (sline_err) {
	case SLINE_ERR_EOF:
		return "EOF caught.";
	case SLINE_ERR_IO:
		return "I/O error.";
	case SLINE_ERR_MEMORY:
		return "could not allocate internal memory.";
	case SLINE_ERR_TERMIOS_GET:
		return "could not read attributes.";
	case SLINE_ERR_TERMIOS_SET:
		return "could not set attributes.";
	default:
		return "unknown error.";
	}
}

int
sline_setup(int entry_size)
{
	int i;

	sline_set_prompt(SLINE_PROMPT_DEFAULT);

	if (entry_size <= 0) {
		sline_history = 0; /* Disabling history */
		goto termios;
	}

	hist_entry_size = entry_size;
	for (i = 0; i < HISTORY_SIZE; ++i) {
		history[i] = calloc(hist_entry_size, sizeof(char));
		if (history[i] == NULL) {
			sline_err = SLINE_ERR_MEMORY;
			return -1;
		}
	}

termios:
	if (tcgetattr(STDIN_FILENO, &old) < 0) {
		sline_err = SLINE_ERR_TERMIOS_GET;
		return -1;
	}

	term = old;
	term.c_lflag &= ~(ICANON | ECHO | ISIG);
	term.c_cc[VMIN] = 0;
	term.c_cc[VTIME] = 1;
	if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) < 0) {
		sline_err = SLINE_ERR_TERMIOS_SET;
		return -1;
	}

	return 0;
}

void
sline_set_prompt(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);

	/* vsnprintf() is ISO C99 */
	vsnprintf(sline_prompt, SLINE_PROMPT_SIZE, fmt, ap);

	va_end(ap);
}

const char *
sline_version(void)
{
	return VERSION;
}