summaryrefslogtreecommitdiff
path: root/src/ui_readline.c
blob: f47485f0a75df1354c10ab33c426985da0e04fbe (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
/*
Copyright (c) 2008-2014
	Lars-Dominik Braun <lars@6xq.net>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include "ui_readline.h"
#include "main.h"

/*	return size of previous UTF-8 character
 */
static size_t BarReadlinePrevUtf8 (char *ptr) {
	size_t i = 0;

	do {
		++i;
		--ptr;
	} while ((*ptr & (1 << 7)) && !(*ptr & (1 << 6)));

	return i;
}

/*	readline replacement
 *	@param buffer
 *	@param buffer size
 *	@param accept these characters
 *	@param input fds
 *	@param flags
 *	@param timeout (seconds) or -1 (no timeout)
 *	@return number of bytes read from stdin
 */
size_t BarReadline (char *buf, const size_t bufSize, const char *mask,
		BarReadlineFds_t *input, const BarReadlineFlags_t flags, int timeout) {
	size_t bufLen = 0;
	unsigned char escapeState = 0;
	fd_set set;
	const bool echo = !(flags & BAR_RL_NOECHO);
	bool done = false;

	assert (buf != NULL);
	assert (bufSize > 0);
	assert (input != NULL);

	/* not actually used here. just stops the player from receiving the
	 * signal */
	sig_atomic_t *prevInt = interrupted, localInt = 0;
	if (!(flags & BAR_RL_NOINT)) {
		interrupted = &localInt;
	}

	memset (buf, 0, bufSize);

	/* if fd is a fifo fgetc will always return EOF if nobody writes to
	 * it, stdin will block */
	while (!done) {
		int curFd = -1;
		unsigned char chr;
		struct timeval timeoutstruct;

		/* select modifies set and timeout */
		memcpy (&set, &input->set, sizeof (set));
		timeoutstruct.tv_sec = timeout;
		timeoutstruct.tv_usec = 0;

		if (select (input->maxfd, &set, NULL, NULL,
				(timeout == -1) ? NULL : &timeoutstruct) <= 0) {
			/* timeout or interrupted */
			bufLen = 0;
			break;
		}

		assert (sizeof (input->fds) / sizeof (*input->fds) == 2);
		if (FD_ISSET(input->fds[0], &set)) {
			curFd = input->fds[0];
		} else if (input->fds[1] != -1 && FD_ISSET(input->fds[1], &set)) {
			curFd = input->fds[1];
		}
		if (read (curFd, &chr, sizeof (chr)) <= 0) {
			/* select() is going wild if fdset contains EOFed stdin, only check
			 * for stdin, fifo is "reopened" as soon as another writer is
			 * available
			 * FIXME: ugly */
			if (curFd == STDIN_FILENO) {
				FD_CLR (curFd, &input->set);
			}
			continue;
		}
		switch (chr) {
			/* EOT */
			case 4:
			/* return */
			case 10:
				done = true;
				break;

			/* clear line */
			case 21:
				if (echo) {
					while (bufLen > 0) {
						const size_t moveSize = BarReadlinePrevUtf8 (&buf[bufLen]);
						assert (bufLen >= moveSize);

						/* move caret and delete character */
						fputs ("\033[D\033[K", stdout);
						bufLen -= moveSize;
					}
					fflush (stdout);
				}
				bufLen = 0;
				break;

			/* escape */
			case 27:
				escapeState = 1;
				break;

			/* del */
			case 126:
				break;

			/* backspace */
			case 8: /* ASCII BS */
			case 127: /* ASCII DEL */
				if (bufLen > 0) {
					size_t moveSize = BarReadlinePrevUtf8 (&buf[bufLen]);
					assert (bufLen >= moveSize);
					memmove (&buf[bufLen-moveSize], &buf[bufLen], moveSize);

					bufLen -= moveSize;

					/* move caret back and delete last character */
					if (echo) {
						fputs ("\033[D\033[K", stdout);
						fflush (stdout);
					}
				}
				break;

			default:
				/* ignore control/escape characters */
				if (chr <= 0x1F) {
					break;
				}
				if (escapeState == 2) {
					escapeState = 0;
					break;
				}
				if (escapeState == 1 && chr == '[') {
					escapeState = 2;
					break;
				}
				/* don't accept chars not in mask */
				if (mask != NULL && !strchr (mask, chr)) {
					break;
				}
				/* don't write beyond buffer's limits */
				if (bufLen < bufSize-1) {
					buf[bufLen] = chr;
					++bufLen;
					if (echo) {
						putchar (chr);
						fflush (stdout);
					}
					/* buffer full => return if requested */
					if (bufLen >= bufSize-1 && (flags & BAR_RL_FULLRETURN)) {
						done = true;
					}
				}
				break;
		} /* end switch */
	} /* end while */

	if (echo) {
		fputs ("\n", stdout);
	}

	interrupted = prevInt;

	buf[bufLen] = '\0';
	return bufLen;
}

/*	Read string from stdin
 *	@param buffer
 *	@param buffer size
 *	@return number of bytes read from stdin
 */
size_t BarReadlineStr (char *buf, const size_t bufSize,
		BarReadlineFds_t *input, const BarReadlineFlags_t flags) {
	return BarReadline (buf, bufSize, NULL, input, flags, -1);
}

/*	Read int from stdin
 *	@param write result into this variable
 *	@return number of bytes read from stdin
 */
size_t BarReadlineInt (int *ret, BarReadlineFds_t *input) {
	int rlRet = 0;
	char buf[16];

	rlRet = BarReadline (buf, sizeof (buf), "0123456789", input,
			BAR_RL_DEFAULT, -1);
	*ret = atoi ((char *) buf);

	return rlRet;
}

/*	Yes/No?
 *	@param default (user presses enter)
 */
bool BarReadlineYesNo (bool def, BarReadlineFds_t *input) {
	char buf[2];
	BarReadline (buf, sizeof (buf), "yYnN", input, BAR_RL_FULLRETURN, -1);
	if (*buf == 'y' || *buf == 'Y' || (def == true && *buf == '\0')) {
		return true;
	} else {
		return false;
	}
}