forked from sschweiz/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cliui.c
327 lines (264 loc) · 5.24 KB
/
cliui.c
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
/*
* cliui.c - control functions for the curses based cli user interface
* (C) 2010-2011 Stephen Schweizer <schweizer@alumni.cmu.edu>
*
* 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 3 of the License, or
* (at your option) any later version.
*/
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "cliui.h"
#include "clibase.h"
void cli_ui_init(cli_ui *ui)
{
initscr();
keypad(stdscr, true);
noecho();
cbreak();
pthread_mutex_init(&ui->mutex, NULL);
getmaxyx(stdscr, ui->h, ui->w);
// setscrreg(0, ui->h - 1);
idlok(stdscr, true);
scrollok(stdscr, true);
ui->first = NULL;
ui->cur = ui->first;
ui->cpos = 0;
ui->input_len = 0;
ui->hwm = 0;
}
void cli_ui_transpose(cli_ui *ui)
{
int pos = 0;
struct input_key *s, *cur = ui->first;
memset(ui->cmd, 0, 256);
while (cur != NULL) {
s = cur;
cur = cur->n;
ui->cmd[pos] = s->val;
pos++;
free(s);
}
ui->first = NULL;
ui->input_len = 0;
ui->hwm = 0;
ui->cpos = 0;
}
int cli_ui_handle_keyleft(cli_ui *ui)
{
int ret = 0;
if (ui->cur != NULL) {
ui->cur = ui->cur->p;
ui->cpos--;
ret = 1;
}
return ret;
}
int cli_ui_handle_keyright(cli_ui *ui)
{
int ret = 0;
if (ui->cur == NULL) {
if (ui->first != NULL) {
ui->cur = ui->first;
ui->cpos++;
ret = 1;
}
} else {
if (ui->cur->n != NULL) {
ui->cur = ui->cur->n;
ui->cpos++;
ret = 1;
}
}
return ret;
}
void cli_ui_replace(cli_ui *ui, const char *cmd)
{
int i;
struct input_key *s, *cur = ui->first;
memset(ui->cmd, 0, CLI_DEFAULT_BUFFER);
while (cur != NULL) {
s = cur;
cur = cur->n;
free(s);
}
ui->first = NULL;
ui->input_len = 0;
for (i = 0; i < CLI_DEFAULT_BUFFER; i++) {
if (isprint(cmd[i])) {
s = (struct input_key *)malloc(sizeof(struct input_key));
s->val = cmd[i];
s->n = NULL;
if (ui->first == NULL) {
s->p = NULL;
ui->first = s;
} else {
s->p = cur;
cur->n = s;
}
cur = s;
ui->input_len++;
}
}
i = 0;
cur = ui->first;
while (cur != NULL) {
i++;
if (i == ui->input_len) break;
cur = cur->n;
}
ui->cur = cur;
ui->cpos = ui->input_len;
if (ui->input_len > ui->hwm) { ui->hwm = ui->input_len; }
}
void cli_ui_history_next(cli_ui *ui)
{
char tmp[CLI_DEFAULT_BUFFER];
if (ui->hoffset < ui->hsize) {
ui->hoffset++;
fseek(ui->history, ui->hoffset * CLI_DEFAULT_BUFFER, SEEK_SET);
if (fgets(tmp, CLI_DEFAULT_BUFFER, ui->history) != NULL) {
cli_ui_replace(ui, tmp);
}
}
}
void cli_ui_history_prev(cli_ui *ui)
{
char tmp[CLI_DEFAULT_BUFFER];
if (ui->hoffset > 0) {
ui->hoffset--;
fseek(ui->history, ui->hoffset * CLI_DEFAULT_BUFFER, SEEK_SET);
if (fgets(tmp, CLI_DEFAULT_BUFFER, ui->history) != NULL) {
cli_ui_replace(ui, tmp);
}
}
}
void cli_ui_handle_keypress(cli_ui *ui, int key)
{
struct input_key *n, *p;
switch (key) {
case KEY_BACKSPACE:
if (ui->cur != NULL) {
p = ui->cur->p;
if (p != NULL) {
p->n = ui->cur->n;
} else {
ui->first = ui->cur->n;
}
if (ui->cur->n != NULL) { ui->cur->n->p = p; }
free(ui->cur);
ui->cur = p;
ui->input_len--;
ui->cpos--;
}
break;
default:
if (!isprint(key)) break;
p = (struct input_key *)malloc(sizeof(struct input_key));
p->val = key;
p->n = NULL;
p->p = NULL;
if (ui->first == NULL) {
ui->first = p;
ui->cur = p;
} else if (ui->cur == NULL) {
p->n = ui->first;
ui->first->p = p;
ui->first = p;
ui->cur = p;
} else {
n = ui->cur->n;
p->p = ui->cur;
p->n = ui->cur->n;
if (n != NULL) { n->p = p; }
ui->cur->n = p;
ui->cur = p;
}
ui->input_len++;
ui->cpos++;
if (ui->input_len > ui->hwm) { ui->hwm = ui->input_len; }
break;
case KEY_LEFT:
cli_ui_handle_keyleft(ui);
break;
case KEY_RIGHT:
cli_ui_handle_keyright(ui);
break;
case KEY_END:
while (cli_ui_handle_keyright(ui)) { }
break;
case KEY_HOME:
while (cli_ui_handle_keyleft(ui)) { }
break;
case KEY_UP:
cli_ui_history_prev(ui);
break;
case KEY_DOWN:
cli_ui_history_next(ui);
break;
}
if ((ui->cur == NULL) && (ui->input_len == 0)) { ui->first = NULL; }
}
void cli_ui_draw_cmd(cli_ui *ui, int x, int y)
{
struct input_key *cur = ui->first;
int i;
move(y, x);
for (i = 0; i < ui->hwm; i++) {
addch(' ');
}
move(y, x);
while (cur != NULL) {
addch(cur->val);
cur = cur->n;
}
move(y, x + ui->cpos);
refresh();
}
void cli_ui_exit(cli_ui *ui)
{
pthread_mutex_destroy(&ui->mutex);
cli_ui_transpose(ui);
endwin();
}
int cli_ui_capture(cli_ui *ui)
{
int c;
int x, y;
refresh();
getyx(stdscr, y, x);
pthread_mutex_lock(&ui->mutex);
ui->irq = 0;
pthread_mutex_unlock(&ui->mutex);
do {
c = getch();
if (c == ERR) continue;
pthread_mutex_lock(&ui->mutex);
if (ui->irq) {
pthread_mutex_unlock(&ui->mutex);
return 1;
}
cli_ui_handle_keypress(ui, c);
cli_ui_draw_cmd(ui, x, y);
pthread_mutex_unlock(&ui->mutex);
} while ((c != '\n') && (c != '\r'));
cli_ui_transpose(ui);
addch('\n');
return 0;
}
#ifdef CLIUI_STANDALONE
int main(int argc, char **argv)
{
cli_ui ui;
cli_ui_init(&ui);
while (1) {
printw("cli> ");
cli_ui_capture(&ui);
}
cli_ui_exit(&ui);
return 0;
}
#endif