1 /*
2 Copyright (c) 2008-2014
3 	Lars-Dominik Braun <lars@6xq.net>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <assert.h>
29 
30 #include "ui_readline.h"
31 #include "main.h"
32 
33 /*	return size of previous UTF-8 character
34  */
BarReadlinePrevUtf8(char * ptr)35 static size_t BarReadlinePrevUtf8 (char *ptr) {
36 	size_t i = 0;
37 
38 	do {
39 		++i;
40 		--ptr;
41 	} while ((*ptr & (1 << 7)) && !(*ptr & (1 << 6)));
42 
43 	return i;
44 }
45 
46 /*	readline replacement
47  *	@param buffer
48  *	@param buffer size
49  *	@param accept these characters
50  *	@param input fds
51  *	@param flags
52  *	@param timeout (seconds) or -1 (no timeout)
53  *	@return number of bytes read from stdin
54  */
BarReadline(char * buf,const size_t bufSize,const char * mask,BarReadlineFds_t * input,const BarReadlineFlags_t flags,int timeout)55 size_t BarReadline (char *buf, const size_t bufSize, const char *mask,
56 		BarReadlineFds_t *input, const BarReadlineFlags_t flags, int timeout) {
57 	size_t bufLen = 0;
58 	unsigned char escapeState = 0;
59 	fd_set set;
60 	const bool echo = !(flags & BAR_RL_NOECHO);
61 	bool done = false;
62 
63 	assert (buf != NULL);
64 	assert (bufSize > 0);
65 	assert (input != NULL);
66 
67 	/* not actually used here. just stops the player from receiving the
68 	 * signal */
69 	sig_atomic_t *prevInt = interrupted, localInt = 0;
70 	if (!(flags & BAR_RL_NOINT)) {
71 		interrupted = &localInt;
72 	}
73 
74 	memset (buf, 0, bufSize);
75 
76 	/* if fd is a fifo fgetc will always return EOF if nobody writes to
77 	 * it, stdin will block */
78 	while (!done) {
79 		int curFd = -1;
80 		unsigned char chr;
81 		struct timeval timeoutstruct;
82 
83 		/* select modifies set and timeout */
84 		memcpy (&set, &input->set, sizeof (set));
85 		timeoutstruct.tv_sec = timeout;
86 		timeoutstruct.tv_usec = 0;
87 
88 		if (select (input->maxfd, &set, NULL, NULL,
89 				(timeout == -1) ? NULL : &timeoutstruct) <= 0) {
90 			/* timeout or interrupted */
91 			bufLen = 0;
92 			break;
93 		}
94 
95 		assert (sizeof (input->fds) / sizeof (*input->fds) == 2);
96 		if (FD_ISSET(input->fds[0], &set)) {
97 			curFd = input->fds[0];
98 		} else if (input->fds[1] != -1 && FD_ISSET(input->fds[1], &set)) {
99 			curFd = input->fds[1];
100 		}
101 		if (read (curFd, &chr, sizeof (chr)) <= 0) {
102 			/* select() is going wild if fdset contains EOFed stdin, only check
103 			 * for stdin, fifo is "reopened" as soon as another writer is
104 			 * available
105 			 * FIXME: ugly */
106 			if (curFd == STDIN_FILENO) {
107 				FD_CLR (curFd, &input->set);
108 			}
109 			continue;
110 		}
111 		switch (chr) {
112 			/* EOT */
113 			case 4:
114 			/* return */
115 			case 10:
116 				done = true;
117 				break;
118 
119 			/* clear line */
120 			case 21:
121 				if (echo) {
122 					while (bufLen > 0) {
123 						const size_t moveSize = BarReadlinePrevUtf8 (&buf[bufLen]);
124 						assert (bufLen >= moveSize);
125 
126 						/* move caret and delete character */
127 						fputs ("\033[D\033[K", stdout);
128 						bufLen -= moveSize;
129 					}
130 					fflush (stdout);
131 				}
132 				bufLen = 0;
133 				break;
134 
135 			/* escape */
136 			case 27:
137 				escapeState = 1;
138 				break;
139 
140 			/* del */
141 			case 126:
142 				break;
143 
144 			/* backspace */
145 			case 8: /* ASCII BS */
146 			case 127: /* ASCII DEL */
147 				if (bufLen > 0) {
148 					size_t moveSize = BarReadlinePrevUtf8 (&buf[bufLen]);
149 					assert (bufLen >= moveSize);
150 					memmove (&buf[bufLen-moveSize], &buf[bufLen], moveSize);
151 
152 					bufLen -= moveSize;
153 
154 					/* move caret back and delete last character */
155 					if (echo) {
156 						fputs ("\033[D\033[K", stdout);
157 						fflush (stdout);
158 					}
159 				}
160 				break;
161 
162 			default:
163 				/* ignore control/escape characters */
164 				if (chr <= 0x1F) {
165 					break;
166 				}
167 				if (escapeState == 2) {
168 					escapeState = 0;
169 					break;
170 				}
171 				if (escapeState == 1 && chr == '[') {
172 					escapeState = 2;
173 					break;
174 				}
175 				/* don't accept chars not in mask */
176 				if (mask != NULL && !strchr (mask, chr)) {
177 					break;
178 				}
179 				/* don't write beyond buffer's limits */
180 				if (bufLen < bufSize-1) {
181 					buf[bufLen] = chr;
182 					++bufLen;
183 					if (echo) {
184 						putchar (chr);
185 						fflush (stdout);
186 					}
187 					/* buffer full => return if requested */
188 					if (bufLen >= bufSize-1 && (flags & BAR_RL_FULLRETURN)) {
189 						done = true;
190 					}
191 				}
192 				break;
193 		} /* end switch */
194 	} /* end while */
195 
196 	if (echo) {
197 		fputs ("\n", stdout);
198 	}
199 
200 	interrupted = prevInt;
201 
202 	buf[bufLen] = '\0';
203 	return bufLen;
204 }
205 
206 /*	Read string from stdin
207  *	@param buffer
208  *	@param buffer size
209  *	@return number of bytes read from stdin
210  */
BarReadlineStr(char * buf,const size_t bufSize,BarReadlineFds_t * input,const BarReadlineFlags_t flags)211 size_t BarReadlineStr (char *buf, const size_t bufSize,
212 		BarReadlineFds_t *input, const BarReadlineFlags_t flags) {
213 	return BarReadline (buf, bufSize, NULL, input, flags, -1);
214 }
215 
216 /*	Read int from stdin
217  *	@param write result into this variable
218  *	@return number of bytes read from stdin
219  */
BarReadlineInt(int * ret,BarReadlineFds_t * input)220 size_t BarReadlineInt (int *ret, BarReadlineFds_t *input) {
221 	int rlRet = 0;
222 	char buf[16];
223 
224 	rlRet = BarReadline (buf, sizeof (buf), "0123456789", input,
225 			BAR_RL_DEFAULT, -1);
226 	*ret = atoi ((char *) buf);
227 
228 	return rlRet;
229 }
230 
231 /*	Yes/No?
232  *	@param default (user presses enter)
233  */
BarReadlineYesNo(bool def,BarReadlineFds_t * input)234 bool BarReadlineYesNo (bool def, BarReadlineFds_t *input) {
235 	char buf[2];
236 	BarReadline (buf, sizeof (buf), "yYnN", input, BAR_RL_FULLRETURN, -1);
237 	if (*buf == 'y' || *buf == 'Y' || (def == true && *buf == '\0')) {
238 		return true;
239 	} else {
240 		return false;
241 	}
242 }
243 
244