summaryrefslogtreecommitdiff
path: root/rl_helper.c
blob: b066600c9938cdd10bde3df6640dd0de9a881a9a (plain)
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
/*
 * Line editing helper
 *
 * Copyright (C) 2013 Jefferson Delfes
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include "rl_helper.h"

#define MAX_LINE_BUFFER 512
#define MAX_SEQ 5

#define MIN(a, b) \
    ({ \
        __typeof__(a) _a = (a); \
        __typeof__(b) _b = (b); \
        _a < _b ? _a : _b; \
    })

typedef enum {
    K_EOT       = 0x04, /* end-of-transmission (ctrl-d) */
    K_TAB       = 0x09,
    K_ESC       = 0x1b,
    K_BACKSPACE = 0x7f,
    /* user defined codes (used for escape sequences */
    K_UP    = 0x100,
    K_DOWN  = 0x101,
    K_RIGHT = 0x102,
    K_LEFT  = 0x103,
    K_END   = 0x104,
    K_HOME  = 0x105,
    K_DELETE = 0x106,
} keys_t;

line_process_callback line_cb;
static tab_completer_callback tab_completer_cb = NULL;
char lnbuf[MAX_LINE_BUFFER]; /* buffer for our line editing */
size_t pos = 0;
char seq[MAX_SEQ]; /* sequence buffer (escape codes) */
size_t seq_pos = 0;
const char *prompt = "> ";

typedef struct {
    char sequence[MAX_SEQ];
    int code;
} char_sequence;

/* code sequence definitions */
const char_sequence seqs[] = {
    {
        .sequence = {K_ESC, '[', 'A'},
        .code = K_UP
    },
    {
        .sequence = {K_ESC, '[', 'B'},
        .code = K_DOWN
    },
    {
        .sequence = {K_ESC, '[', 'C'},
        .code = K_RIGHT
    },
    {
        .sequence = {K_ESC, '[', 'D'},
        .code = K_LEFT
    },
    {
        .sequence = {K_ESC, 'O', 'F'},
        .code = K_END
    },
    {
        .sequence = {K_ESC, 'O', 'H'},
        .code = K_HOME
    },
    {
        .sequence = {K_ESC, '[', '3', '~'},
        .code = K_DELETE
    },
};

void rl_clear_seq() {

    memset(seq, 0, sizeof(seq));
    seq_pos = 0;
}

void rl_clear() {

    rl_clear_seq();
    memset(lnbuf, 0, sizeof(lnbuf));
    pos = 0;
}

/* clear current line and returns to line begin */
void rl_clear_line() {

    printf("\x1b[2K\r");
}

void rl_reprint_prompt() {
    static size_t viewport_pos = 0;
    size_t terminal_cols = 80;
    size_t len = strlen(lnbuf);
    size_t viewport_size = terminal_cols - strlen(prompt) - 1;
    size_t viewport_end;

    rl_clear_line();

    if (pos < viewport_pos) /* cursor before viewport */
        viewport_pos = pos;
    if (pos > viewport_pos + viewport_size) /* cursor after viewport */
        viewport_pos = pos - viewport_size;

    printf("%s%.*s", prompt, MIN(viewport_size, len), lnbuf + viewport_pos);

    viewport_end = MIN(viewport_pos + viewport_size, len);
    while (viewport_end-- != pos)
        putchar('\b'); /* backspace */
    fflush(stdout);
}

void rl_init(line_process_callback cb) {
    struct termios settings;

    /* disable echo */
    tcgetattr(0, &settings); /* read settings from stdin (0) */
    settings.c_lflag &= ~(ICANON | ECHO); /* disable canonical and echo flags */
    tcsetattr(0, TCSANOW, &settings); /* store new settings */

    rl_clear();
    line_cb = cb;
}

void rl_set_prompt(const char *str) {

    prompt = str;
    rl_reprint_prompt();
}

void rl_set_tab_completer(tab_completer_callback cb) {

    tab_completer_cb = cb;
}

void rl_quit() {

    rl_clear();
    rl_clear_line();
}

/* returns 1 if char was consumed, 0 otherwise */
int rl_parse_seq(int *c) {

    if (seq_pos == 0) {
        /* starts a sequence of chars */
        if (*c == K_ESC) {
            seq[seq_pos++] = *c;
            return 1;
        } else
            return 0;
    } else {
        size_t i;
        if (seq_pos >= sizeof(seq)) {
            /* we lost the sequence */
            rl_clear_seq();
            return 0;
        }

        seq[seq_pos++] = *c;
        for (i = 0; i < sizeof(seqs) / sizeof(char_sequence); i++) {
            if (memcmp(seq, seqs[i].sequence, sizeof(seq)) == 0) {
                /* found sequence, return special code */
                *c = seqs[i].code;
                rl_clear_seq();
                return 0;
            }
        }
        return 1;
    }
}

bool rl_feed(int c) {

    if (rl_parse_seq(&c))
        return true;

    switch (c) {
        case K_EOT:
            putchar('\n');
            return false;
        case K_TAB:
            if (tab_completer_cb) {
                const char *str = tab_completer_cb(lnbuf, pos);
                if (str) {
                    int len = strlen(str);
                    memmove(lnbuf + pos + len, lnbuf + pos,
                            sizeof(lnbuf) - pos - len);
                    strncpy(lnbuf + pos, str, len);
                    pos += len;
                    rl_reprint_prompt();
                }
            }
            break;
        case '\r':
        case '\n':
            putchar('\n');
            line_cb(lnbuf);
            rl_clear();
            rl_reprint_prompt();
            break;
        case K_ESC:
            break;
        case K_BACKSPACE:
            if (pos > 0) {
                memmove(lnbuf + pos - 1, lnbuf + pos, sizeof(lnbuf) - pos);
                pos--;
                rl_reprint_prompt();
            }
            break;
        case K_UP:
        case K_DOWN:
            /* history handle */
            break;
        case K_RIGHT:
            if (pos < strlen(lnbuf)) {
                pos++;
                rl_reprint_prompt();
            }
            break;
        case K_LEFT:
            if (pos > 0) {
                pos--;
                rl_reprint_prompt();
            }
            break;
        case K_END:
            pos = strlen(lnbuf);
            rl_reprint_prompt();
            break;
        case K_HOME:
            pos = 0;
            rl_reprint_prompt();
            break;
        case K_DELETE:
            memmove(lnbuf + pos, lnbuf + pos + 1, sizeof(lnbuf) - pos - 1);
            rl_reprint_prompt();
            break;
        default:
            if (isprint(c)) {
                if (pos < (sizeof(lnbuf) - 1)) {
                    /* shift everything to right and insert char at pos */
                    memmove(lnbuf + pos + 1, lnbuf + pos,
                            sizeof(lnbuf) - pos - 1);
                    lnbuf[pos++] = c;
                    rl_reprint_prompt();
                }
            } else
                printf(" %x ", c);
            break;
    }

    return true;
}

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

    va_start(ap, fmt);

    rl_clear_line();
    vprintf(fmt, ap);
    va_end(ap);

    rl_reprint_prompt();
}