aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-03-16 08:09:57 +0000
committerErik Andersen <andersen@codepoet.org>2000-03-16 08:09:57 +0000
commit13456d1fcd0122d8464c3c3e1c356d86a56e6c08 (patch)
tree0d037e1e4bdccc54b86db8f4b03e096804124904
parentd75af99529879e6cd38164fd110732052a9cdda4 (diff)
downloadbusybox-w32-13456d1fcd0122d8464c3c3e1c356d86a56e6c08.tar.gz
busybox-w32-13456d1fcd0122d8464c3c3e1c356d86a56e6c08.tar.bz2
busybox-w32-13456d1fcd0122d8464c3c3e1c356d86a56e6c08.zip
Forgot these files...
-Erik
-rw-r--r--cmdedit.c405
-rw-r--r--cmdedit.h17
-rw-r--r--coreutils/echo.c126
-rw-r--r--coreutils/test.c583
-rw-r--r--echo.c126
-rw-r--r--shell/cmdedit.c405
-rw-r--r--shell/cmdedit.h17
-rw-r--r--test.c583
8 files changed, 2262 insertions, 0 deletions
diff --git a/cmdedit.c b/cmdedit.c
new file mode 100644
index 000000000..d1604f1d1
--- /dev/null
+++ b/cmdedit.c
@@ -0,0 +1,405 @@
1/*
2 * Termios command line History and Editting for NetBSD sh (ash)
3 * Copyright (c) 1999
4 * Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
5 * Etc: Dave Cinege <dcinege@psychosis.com>
6 * Adjusted for busybox: Erik Andersen <andersee@debian.org>
7 *
8 * You may use this code as you wish, so long as the original author(s)
9 * are attributed in any redistributions of the source code.
10 * This code is 'as is' with no warranty.
11 * This code may safely be consumed by a BSD or GPL license.
12 *
13 * v 0.5 19990328 Initial release
14 *
15 * Future plans: Simple file and path name completion. (like BASH)
16 *
17 */
18
19/*
20 Usage and Known bugs:
21 Terminal key codes are not extensive, and more will probably
22 need to be added. This version was created on Debian GNU/Linux 2.x.
23 Delete, Backspace, Home, End, and the arrow keys were tested
24 to work in an Xterm and console. Ctrl-A also works as Home.
25 Ctrl-E also works as End. The binary size increase is <3K.
26
27 Editting will not display correctly for lines greater then the
28 terminal width. (more then one line.) However, history will.
29 */
30
31#include "internal.h"
32#ifdef BB_FEATURE_SH_COMMAND_EDITING
33
34#include <stdio.h>
35#include <errno.h>
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39#include <termio.h>
40#include <ctype.h>
41#include <signal.h>
42
43#include "cmdedit.h"
44
45
46#define MAX_HISTORY 15 /* Maximum length of the linked list for the command line history */
47
48#define ESC 27
49#define DEL 127
50
51static struct history *his_front = NULL; /* First element in command line list */
52static struct history *his_end = NULL; /* Last element in command line list */
53static struct termio old_term, new_term; /* Current termio and the previous termio before starting ash */
54
55static int history_counter = 0; /* Number of commands in history list */
56static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
57char *parsenextc; /* copy of parsefile->nextc */
58
59struct history {
60 char *s;
61 struct history *p;
62 struct history *n;
63};
64
65
66/* Version of write which resumes after a signal is caught. */
67int xwrite(int fd, char *buf, int nbytes)
68{
69 int ntry;
70 int i;
71 int n;
72
73 n = nbytes;
74 ntry = 0;
75 for (;;) {
76 i = write(fd, buf, n);
77 if (i > 0) {
78 if ((n -= i) <= 0)
79 return nbytes;
80 buf += i;
81 ntry = 0;
82 } else if (i == 0) {
83 if (++ntry > 10)
84 return nbytes - n;
85 } else if (errno != EINTR) {
86 return -1;
87 }
88 }
89}
90
91
92/* Version of ioctl that retries after a signal is caught. */
93int xioctl(int fd, unsigned long request, char *arg)
94{
95 int i;
96 while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
97 return i;
98}
99
100
101void cmdedit_reset_term(void)
102{
103 if (reset_term)
104 xioctl(fileno(stdin), TCSETA, (void *) &old_term);
105}
106
107void gotaSignal(int sig)
108{
109 cmdedit_reset_term();
110 fprintf(stdout, "\n");
111 exit(TRUE);
112}
113
114void input_home(int outputFd, int *cursor)
115{ /* Command line input routines */
116 while (*cursor > 0) {
117 xwrite(outputFd, "\b", 1);
118 --*cursor;
119 }
120}
121
122
123void input_delete(int outputFd, int cursor)
124{
125 int j = 0;
126
127 memmove(parsenextc + cursor, parsenextc + cursor + 1,
128 BUFSIZ - cursor - 1);
129 for (j = cursor; j < (BUFSIZ - 1); j++) {
130 if (!*(parsenextc + j))
131 break;
132 else
133 xwrite(outputFd, (parsenextc + j), 1);
134 }
135
136 xwrite(outputFd, " \b", 2);
137
138 while (j-- > cursor)
139 xwrite(outputFd, "\b", 1);
140}
141
142
143void input_end(int outputFd, int *cursor, int len)
144{
145 while (*cursor < len) {
146 xwrite(outputFd, "\033[C", 3);
147 ++*cursor;
148 }
149}
150
151
152void input_backspace(int outputFd, int *cursor, int *len)
153{
154 int j = 0;
155
156 if (*cursor > 0) {
157 xwrite(outputFd, "\b \b", 3);
158 --*cursor;
159 memmove(parsenextc + *cursor, parsenextc + *cursor + 1,
160 BUFSIZ - *cursor + 1);
161
162 for (j = *cursor; j < (BUFSIZ - 1); j++) {
163 if (!*(parsenextc + j))
164 break;
165 else
166 xwrite(outputFd, (parsenextc + j), 1);
167 }
168
169 xwrite(outputFd, " \b", 2);
170
171 while (j-- > *cursor)
172 xwrite(outputFd, "\b", 1);
173
174 --*len;
175 }
176}
177
178extern int cmdedit_read_input(int inputFd, int outputFd,
179 char command[BUFSIZ])
180{
181
182 int nr = 0;
183 int len = 0;
184 int j = 0;
185 int cursor = 0;
186 int break_out = 0;
187 int ret = 0;
188 char c = 0;
189 struct history *hp = his_end;
190
191 memset(command, 0, sizeof(command));
192 parsenextc = command;
193 if (!reset_term) {
194 xioctl(inputFd, TCGETA, (void *) &old_term);
195 memcpy(&new_term, &old_term, sizeof(struct termio));
196 new_term.c_cc[VMIN] = 1;
197 new_term.c_cc[VTIME] = 0;
198 new_term.c_lflag &= ~ICANON; /* unbuffered input */
199 new_term.c_lflag &= ~ECHO;
200 xioctl(inputFd, TCSETA, (void *) &new_term);
201 reset_term = 1;
202 } else {
203 xioctl(inputFd, TCSETA, (void *) &new_term);
204 }
205
206 memset(parsenextc, 0, BUFSIZ);
207
208 while (1) {
209
210 if ((ret = read(inputFd, &c, 1)) < 1)
211 return ret;
212
213 switch (c) {
214 case 1: /* Control-A Beginning of line */
215 input_home(outputFd, &cursor);
216 break;
217 case 5: /* Control-E EOL */
218 input_end(outputFd, &cursor, len);
219 break;
220 case 4: /* Control-D */
221 if (cursor != len) {
222 input_delete(outputFd, cursor);
223 len--;
224 }
225 break;
226 case '\b': /* Backspace */
227 case DEL:
228 input_backspace(outputFd, &cursor, &len);
229 break;
230 case '\n': /* Enter */
231 *(parsenextc + len++ + 1) = c;
232 xwrite(outputFd, &c, 1);
233 break_out = 1;
234 break;
235 case ESC: /* escape sequence follows */
236 if ((ret = read(inputFd, &c, 1)) < 1)
237 return ret;
238
239 if (c == '[') { /* 91 */
240 if ((ret = read(inputFd, &c, 1)) < 1)
241 return ret;
242
243 switch (c) {
244 case 'A':
245 if (hp && hp->p) { /* Up */
246 hp = hp->p;
247 goto hop;
248 }
249 break;
250 case 'B':
251 if (hp && hp->n && hp->n->s) { /* Down */
252 hp = hp->n;
253 goto hop;
254 }
255 break;
256
257 hop: /* hop */
258 len = strlen(parsenextc);
259
260 for (; cursor > 0; cursor--) /* return to begining of line */
261 xwrite(outputFd, "\b", 1);
262
263 for (j = 0; j < len; j++) /* erase old command */
264 xwrite(outputFd, " ", 1);
265
266 for (j = len; j > 0; j--) /* return to begining of line */
267 xwrite(outputFd, "\b", 1);
268
269 strcpy(parsenextc, hp->s); /* write new command */
270 len = strlen(hp->s);
271 xwrite(outputFd, parsenextc, len);
272 cursor = len;
273 break;
274 case 'C': /* Right */
275 if (cursor < len) {
276 xwrite(outputFd, "\033[C", 3);
277 cursor++;
278 }
279 break;
280 case 'D': /* Left */
281 if (cursor > 0) {
282 xwrite(outputFd, "\033[D", 3);
283 cursor--;
284 }
285 break;
286 case '3': /* Delete */
287 if (cursor != len) {
288 input_delete(outputFd, cursor);
289 len--;
290 }
291 break;
292 case '1': /* Home (Ctrl-A) */
293 input_home(outputFd, &cursor);
294 break;
295 case '4': /* End (Ctrl-E) */
296 input_end(outputFd, &cursor, len);
297 break;
298 }
299 if (c == '1' || c == '3' || c == '4')
300 if ((ret = read(inputFd, &c, 1)) < 1)
301 return ret; /* read 126 (~) */
302 }
303 if (c == 'O') { /* 79 */
304 if ((ret = read(inputFd, &c, 1)) < 1)
305 return ret;
306 switch (c) {
307 case 'H': /* Home (xterm) */
308 input_home(outputFd, &cursor);
309 break;
310 case 'F': /* End (xterm) */
311 input_end(outputFd, &cursor, len);
312 break;
313 }
314 }
315 c = 0;
316 break;
317
318 default: /* If it's regular input, do the normal thing */
319
320 if (!isprint(c)) /* Skip non-printable characters */
321 break;
322
323 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
324 break;
325
326 len++;
327
328 if (cursor == (len - 1)) { /* Append if at the end of the line */
329 *(parsenextc + cursor) = c;
330 } else { /* Insert otherwise */
331 memmove(parsenextc + cursor + 1, parsenextc + cursor,
332 len - cursor - 1);
333
334 *(parsenextc + cursor) = c;
335
336 for (j = cursor; j < len; j++)
337 xwrite(outputFd, parsenextc + j, 1);
338 for (; j > cursor; j--)
339 xwrite(outputFd, "\033[D", 3);
340 }
341
342 cursor++;
343 xwrite(outputFd, &c, 1);
344 break;
345 }
346
347 if (break_out) /* Enter is the command terminator, no more input. */
348 break;
349 }
350
351 nr = len + 1;
352 xioctl(inputFd, TCSETA, (void *) &old_term);
353 reset_term = 0;
354
355
356 if (*(parsenextc)) { /* Handle command history log */
357
358 struct history *h = his_end;
359
360 if (!h) { /* No previous history */
361 h = his_front = malloc(sizeof(struct history));
362 h->n = malloc(sizeof(struct history));
363 h->p = NULL;
364 h->s = strdup(parsenextc);
365
366 h->n->p = h;
367 h->n->n = NULL;
368 h->n->s = NULL;
369 his_end = h->n;
370 history_counter++;
371 } else { /* Add a new history command */
372
373 h->n = malloc(sizeof(struct history));
374
375 h->n->p = h;
376 h->n->n = NULL;
377 h->n->s = NULL;
378 h->s = strdup(parsenextc);
379 his_end = h->n;
380
381 if (history_counter >= MAX_HISTORY) { /* After max history, remove the last known command */
382
383 struct history *p = his_front->n;
384
385 p->p = NULL;
386 free(his_front->s);
387 free(his_front);
388 his_front = p;
389 } else {
390 history_counter++;
391 }
392 }
393 }
394
395 return nr;
396}
397
398extern void cmdedit_init(void)
399{
400 atexit(cmdedit_reset_term);
401 signal(SIGINT, gotaSignal);
402 signal(SIGQUIT, gotaSignal);
403 signal(SIGTERM, gotaSignal);
404}
405#endif /* BB_FEATURE_SH_COMMAND_EDITING */
diff --git a/cmdedit.h b/cmdedit.h
new file mode 100644
index 000000000..e776543d6
--- /dev/null
+++ b/cmdedit.h
@@ -0,0 +1,17 @@
1/*
2 * Termios command line History and Editting for NetBSD sh (ash)
3 * Copyright (c) 1999
4 * Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
5 * Etc: Dave Cinege <dcinege@psychosis.com>
6 * Adjusted for busybox: Erik Andersen <andersee@debian.org>
7 *
8 * You may use this code as you wish, so long as the original author(s)
9 * are attributed in any redistributions of the source code.
10 * This code is 'as is' with no warranty.
11 * This code may safely be consumed by a BSD or GPL license.
12 *
13 */
14
15extern int cmdedit_read_input(int inputFd, int outputFd, char command[BUFSIZ]);
16extern void cmdedit_init(void);
17
diff --git a/coreutils/echo.c b/coreutils/echo.c
new file mode 100644
index 000000000..91f17aa0f
--- /dev/null
+++ b/coreutils/echo.c
@@ -0,0 +1,126 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * echo implementation for busybox
4 *
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Original copyright notice is retained at the end of this file.
23 */
24
25#include "internal.h"
26#include <stdio.h>
27
28extern int
29echo_main(int argc, char** argv)
30{
31 register char **ap;
32 register char *p;
33 register char c;
34 int nflag = 0;
35 int eflag = 0;
36
37 ap = argv;
38 if (argc)
39 ap++;
40 while ((p = *ap) != NULL && *p == '-') {
41 if (strcmp(p, "-n")==0) {
42 nflag = 1;
43 } else if (strcmp(p, "-e")==0) {
44 eflag = 1;
45 } else if (strcmp(p, "-E")==0) {
46 eflag = 0;
47 }
48 else break;
49 ap++;
50 }
51 while ((p = *ap++) != NULL) {
52 while ((c = *p++) != '\0') {
53 if (c == '\\' && eflag) {
54 switch (c = *p++) {
55 case 'a': c = '\007'; break;
56 case 'b': c = '\b'; break;
57 case 'c': exit( 0); /* exit */
58 case 'f': c = '\f'; break;
59 case 'n': c = '\n'; break;
60 case 'r': c = '\r'; break;
61 case 't': c = '\t'; break;
62 case 'v': c = '\v'; break;
63 case '\\': break; /* c = '\\' */
64 case '0': case '1': case '2': case '3':
65 case '4': case '5': case '6': case '7':
66 c -= '0';
67 if (*p >= '0' && *p <= '7')
68 c = c * 8 + (*p++ - '0');
69 if (*p >= '0' && *p <= '7')
70 c = c * 8 + (*p++ - '0');
71 break;
72 default:
73 p--;
74 break;
75 }
76 }
77 putchar(c);
78 }
79 if (*ap)
80 putchar(' ');
81 }
82 if (! nflag)
83 putchar('\n');
84 fflush(stdout);
85 exit( 0);
86}
87
88/*-
89 * Copyright (c) 1991, 1993
90 * The Regents of the University of California. All rights reserved.
91 *
92 * This code is derived from software contributed to Berkeley by
93 * Kenneth Almquist.
94 *
95 * Redistribution and use in source and binary forms, with or without
96 * modification, are permitted provided that the following conditions
97 * are met:
98 * 1. Redistributions of source code must retain the above copyright
99 * notice, this list of conditions and the following disclaimer.
100 * 2. Redistributions in binary form must reproduce the above copyright
101 * notice, this list of conditions and the following disclaimer in the
102 * documentation and/or other materials provided with the distribution.
103 * 3. All advertising materials mentioning features or use of this software
104 * must display the following acknowledgement:
105 * This product includes software developed by the University of
106 * California, Berkeley and its contributors.
107 * 4. Neither the name of the University nor the names of its contributors
108 * may be used to endorse or promote products derived from this software
109 * without specific prior written permission.
110 *
111 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
112 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
113 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
114 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
115 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
116 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
117 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
118 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
119 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
120 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
121 * SUCH DAMAGE.
122 *
123 * @(#)echo.c 8.1 (Berkeley) 5/31/93
124 */
125
126
diff --git a/coreutils/test.c b/coreutils/test.c
new file mode 100644
index 000000000..85d06a83a
--- /dev/null
+++ b/coreutils/test.c
@@ -0,0 +1,583 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * echo implementation for busybox
4 *
5 * Copyright (c) by a whole pile of folks:
6 *
7 * test(1); version 7-like -- author Erik Baalbergen
8 * modified by Eric Gisin to be used as built-in.
9 * modified by Arnold Robbins to add SVR3 compatibility
10 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
11 * modified by J.T. Conklin for NetBSD.
12 * modified by Herbert Xu to be used as built-in in ash.
13 * modified by Erik Andersen <andersee@debian.org> to be used
14 * in busybox.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * Original copyright notice states:
31 * "This program is in the Public Domain."
32 */
33
34#include "internal.h"
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <unistd.h>
38#include <ctype.h>
39#include <errno.h>
40#include <stdlib.h>
41#include <string.h>
42
43/* test(1) accepts the following grammar:
44 oexpr ::= aexpr | aexpr "-o" oexpr ;
45 aexpr ::= nexpr | nexpr "-a" aexpr ;
46 nexpr ::= primary | "!" primary
47 primary ::= unary-operator operand
48 | operand binary-operator operand
49 | operand
50 | "(" oexpr ")"
51 ;
52 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
53 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
54
55 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
56 "-nt"|"-ot"|"-ef";
57 operand ::= <any legal UNIX file name>
58*/
59
60enum token {
61 EOI,
62 FILRD,
63 FILWR,
64 FILEX,
65 FILEXIST,
66 FILREG,
67 FILDIR,
68 FILCDEV,
69 FILBDEV,
70 FILFIFO,
71 FILSOCK,
72 FILSYM,
73 FILGZ,
74 FILTT,
75 FILSUID,
76 FILSGID,
77 FILSTCK,
78 FILNT,
79 FILOT,
80 FILEQ,
81 FILUID,
82 FILGID,
83 STREZ,
84 STRNZ,
85 STREQ,
86 STRNE,
87 STRLT,
88 STRGT,
89 INTEQ,
90 INTNE,
91 INTGE,
92 INTGT,
93 INTLE,
94 INTLT,
95 UNOT,
96 BAND,
97 BOR,
98 LPAREN,
99 RPAREN,
100 OPERAND
101};
102
103enum token_types {
104 UNOP,
105 BINOP,
106 BUNOP,
107 BBINOP,
108 PAREN
109};
110
111struct t_op {
112 const char *op_text;
113 short op_num, op_type;
114} const ops [] = {
115 {"-r", FILRD, UNOP},
116 {"-w", FILWR, UNOP},
117 {"-x", FILEX, UNOP},
118 {"-e", FILEXIST,UNOP},
119 {"-f", FILREG, UNOP},
120 {"-d", FILDIR, UNOP},
121 {"-c", FILCDEV,UNOP},
122 {"-b", FILBDEV,UNOP},
123 {"-p", FILFIFO,UNOP},
124 {"-u", FILSUID,UNOP},
125 {"-g", FILSGID,UNOP},
126 {"-k", FILSTCK,UNOP},
127 {"-s", FILGZ, UNOP},
128 {"-t", FILTT, UNOP},
129 {"-z", STREZ, UNOP},
130 {"-n", STRNZ, UNOP},
131 {"-h", FILSYM, UNOP}, /* for backwards compat */
132 {"-O", FILUID, UNOP},
133 {"-G", FILGID, UNOP},
134 {"-L", FILSYM, UNOP},
135 {"-S", FILSOCK,UNOP},
136 {"=", STREQ, BINOP},
137 {"!=", STRNE, BINOP},
138 {"<", STRLT, BINOP},
139 {">", STRGT, BINOP},
140 {"-eq", INTEQ, BINOP},
141 {"-ne", INTNE, BINOP},
142 {"-ge", INTGE, BINOP},
143 {"-gt", INTGT, BINOP},
144 {"-le", INTLE, BINOP},
145 {"-lt", INTLT, BINOP},
146 {"-nt", FILNT, BINOP},
147 {"-ot", FILOT, BINOP},
148 {"-ef", FILEQ, BINOP},
149 {"!", UNOT, BUNOP},
150 {"-a", BAND, BBINOP},
151 {"-o", BOR, BBINOP},
152 {"(", LPAREN, PAREN},
153 {")", RPAREN, PAREN},
154 {0, 0, 0}
155};
156
157char **t_wp;
158struct t_op const *t_wp_op;
159static gid_t *group_array = NULL;
160static int ngroups;
161
162static enum token t_lex();
163static int oexpr();
164static int aexpr();
165static int nexpr();
166static int binop();
167static int primary();
168static int filstat();
169static int getn();
170static int newerf();
171static int olderf();
172static int equalf();
173static void syntax();
174static int test_eaccess();
175static int is_a_group_member();
176static void initialize_group_array();
177
178extern int
179test_main(int argc, char** argv)
180{
181 int res;
182
183 if (strcmp(argv[0], "[") == 0) {
184 if (strcmp(argv[--argc], "]"))
185 fatalError("missing ]");
186 argv[argc] = NULL;
187 }
188
189 /* Implement special cases from POSIX.2, section 4.62.4 */
190 switch (argc) {
191 case 1:
192 exit( 1);
193 case 2:
194 exit (*argv[1] == '\0');
195 case 3:
196 if (argv[1][0] == '!' && argv[1][1] == '\0') {
197 exit (!(*argv[2] == '\0'));
198 }
199 break;
200 case 4:
201 if (argv[1][0] != '!' || argv[1][1] != '\0') {
202 if (t_lex(argv[2]),
203 t_wp_op && t_wp_op->op_type == BINOP) {
204 t_wp = &argv[1];
205 exit (binop() == 0);
206 }
207 }
208 break;
209 case 5:
210 if (argv[1][0] == '!' && argv[1][1] == '\0') {
211 if (t_lex(argv[3]),
212 t_wp_op && t_wp_op->op_type == BINOP) {
213 t_wp = &argv[2];
214 exit (!(binop() == 0));
215 }
216 }
217 break;
218 }
219
220 t_wp = &argv[1];
221 res = !oexpr(t_lex(*t_wp));
222
223 if (*t_wp != NULL && *++t_wp != NULL)
224 syntax(*t_wp, "unknown operand");
225
226 exit( res);
227}
228
229static void
230syntax(op, msg)
231 char *op;
232 char *msg;
233{
234 if (op && *op)
235 fatalError("%s: %s", op, msg);
236 else
237 fatalError("%s", msg);
238}
239
240static int
241oexpr(n)
242 enum token n;
243{
244 int res;
245
246 res = aexpr(n);
247 if (t_lex(*++t_wp) == BOR)
248 return oexpr(t_lex(*++t_wp)) || res;
249 t_wp--;
250 return res;
251}
252
253static int
254aexpr(n)
255 enum token n;
256{
257 int res;
258
259 res = nexpr(n);
260 if (t_lex(*++t_wp) == BAND)
261 return aexpr(t_lex(*++t_wp)) && res;
262 t_wp--;
263 return res;
264}
265
266static int
267nexpr(n)
268 enum token n; /* token */
269{
270 if (n == UNOT)
271 return !nexpr(t_lex(*++t_wp));
272 return primary(n);
273}
274
275static int
276primary(n)
277 enum token n;
278{
279 int res;
280
281 if (n == EOI)
282 syntax(NULL, "argument expected");
283 if (n == LPAREN) {
284 res = oexpr(t_lex(*++t_wp));
285 if (t_lex(*++t_wp) != RPAREN)
286 syntax(NULL, "closing paren expected");
287 return res;
288 }
289 if (t_wp_op && t_wp_op->op_type == UNOP) {
290 /* unary expression */
291 if (*++t_wp == NULL)
292 syntax(t_wp_op->op_text, "argument expected");
293 switch (n) {
294 case STREZ:
295 return strlen(*t_wp) == 0;
296 case STRNZ:
297 return strlen(*t_wp) != 0;
298 case FILTT:
299 return isatty(getn(*t_wp));
300 default:
301 return filstat(*t_wp, n);
302 }
303 }
304
305 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
306 return binop();
307 }
308
309 return strlen(*t_wp) > 0;
310}
311
312static int
313binop()
314{
315 const char *opnd1, *opnd2;
316 struct t_op const *op;
317
318 opnd1 = *t_wp;
319 (void) t_lex(*++t_wp);
320 op = t_wp_op;
321
322 if ((opnd2 = *++t_wp) == (char *)0)
323 syntax(op->op_text, "argument expected");
324
325 switch (op->op_num) {
326 case STREQ:
327 return strcmp(opnd1, opnd2) == 0;
328 case STRNE:
329 return strcmp(opnd1, opnd2) != 0;
330 case STRLT:
331 return strcmp(opnd1, opnd2) < 0;
332 case STRGT:
333 return strcmp(opnd1, opnd2) > 0;
334 case INTEQ:
335 return getn(opnd1) == getn(opnd2);
336 case INTNE:
337 return getn(opnd1) != getn(opnd2);
338 case INTGE:
339 return getn(opnd1) >= getn(opnd2);
340 case INTGT:
341 return getn(opnd1) > getn(opnd2);
342 case INTLE:
343 return getn(opnd1) <= getn(opnd2);
344 case INTLT:
345 return getn(opnd1) < getn(opnd2);
346 case FILNT:
347 return newerf (opnd1, opnd2);
348 case FILOT:
349 return olderf (opnd1, opnd2);
350 case FILEQ:
351 return equalf (opnd1, opnd2);
352 }
353 /* NOTREACHED */
354 return 1;
355}
356
357static int
358filstat(nm, mode)
359 char *nm;
360 enum token mode;
361{
362 struct stat s;
363 int i;
364
365 if (mode == FILSYM) {
366#ifdef S_IFLNK
367 if (lstat(nm, &s) == 0) {
368 i = S_IFLNK;
369 goto filetype;
370 }
371#endif
372 return 0;
373 }
374
375 if (stat(nm, &s) != 0)
376 return 0;
377
378 switch (mode) {
379 case FILRD:
380 return test_eaccess(nm, R_OK) == 0;
381 case FILWR:
382 return test_eaccess(nm, W_OK) == 0;
383 case FILEX:
384 return test_eaccess(nm, X_OK) == 0;
385 case FILEXIST:
386 return 1;
387 case FILREG:
388 i = S_IFREG;
389 goto filetype;
390 case FILDIR:
391 i = S_IFDIR;
392 goto filetype;
393 case FILCDEV:
394 i = S_IFCHR;
395 goto filetype;
396 case FILBDEV:
397 i = S_IFBLK;
398 goto filetype;
399 case FILFIFO:
400#ifdef S_IFIFO
401 i = S_IFIFO;
402 goto filetype;
403#else
404 return 0;
405#endif
406 case FILSOCK:
407#ifdef S_IFSOCK
408 i = S_IFSOCK;
409 goto filetype;
410#else
411 return 0;
412#endif
413 case FILSUID:
414 i = S_ISUID;
415 goto filebit;
416 case FILSGID:
417 i = S_ISGID;
418 goto filebit;
419 case FILSTCK:
420 i = S_ISVTX;
421 goto filebit;
422 case FILGZ:
423 return s.st_size > 0L;
424 case FILUID:
425 return s.st_uid == geteuid();
426 case FILGID:
427 return s.st_gid == getegid();
428 default:
429 return 1;
430 }
431
432filetype:
433 return ((s.st_mode & S_IFMT) == i);
434
435filebit:
436 return ((s.st_mode & i) != 0);
437}
438
439static enum token
440t_lex(s)
441 char *s;
442{
443 struct t_op const *op = ops;
444
445 if (s == 0) {
446 t_wp_op = (struct t_op *)0;
447 return EOI;
448 }
449 while (op->op_text) {
450 if (strcmp(s, op->op_text) == 0) {
451 t_wp_op = op;
452 return op->op_num;
453 }
454 op++;
455 }
456 t_wp_op = (struct t_op *)0;
457 return OPERAND;
458}
459
460/* atoi with error detection */
461static int
462getn(s)
463 char *s;
464{
465 char *p;
466 long r;
467
468 errno = 0;
469 r = strtol(s, &p, 10);
470
471 if (errno != 0)
472 fatalError("%s: out of range", s);
473
474 while (isspace(*p))
475 p++;
476
477 if (*p)
478 fatalError("%s: bad number", s);
479
480 return (int) r;
481}
482
483static int
484newerf (f1, f2)
485char *f1, *f2;
486{
487 struct stat b1, b2;
488
489 return (stat (f1, &b1) == 0 &&
490 stat (f2, &b2) == 0 &&
491 b1.st_mtime > b2.st_mtime);
492}
493
494static int
495olderf (f1, f2)
496char *f1, *f2;
497{
498 struct stat b1, b2;
499
500 return (stat (f1, &b1) == 0 &&
501 stat (f2, &b2) == 0 &&
502 b1.st_mtime < b2.st_mtime);
503}
504
505static int
506equalf (f1, f2)
507char *f1, *f2;
508{
509 struct stat b1, b2;
510
511 return (stat (f1, &b1) == 0 &&
512 stat (f2, &b2) == 0 &&
513 b1.st_dev == b2.st_dev &&
514 b1.st_ino == b2.st_ino);
515}
516
517/* Do the same thing access(2) does, but use the effective uid and gid,
518 and don't make the mistake of telling root that any file is
519 executable. */
520static int
521test_eaccess (path, mode)
522char *path;
523int mode;
524{
525 struct stat st;
526 int euid = geteuid();
527
528 if (stat (path, &st) < 0)
529 return (-1);
530
531 if (euid == 0) {
532 /* Root can read or write any file. */
533 if (mode != X_OK)
534 return (0);
535
536 /* Root can execute any file that has any one of the execute
537 bits set. */
538 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
539 return (0);
540 }
541
542 if (st.st_uid == euid) /* owner */
543 mode <<= 6;
544 else if (is_a_group_member (st.st_gid))
545 mode <<= 3;
546
547 if (st.st_mode & mode)
548 return (0);
549
550 return (-1);
551}
552
553static void
554initialize_group_array ()
555{
556 ngroups = getgroups(0, NULL);
557 if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
558 fatalError("Out of space");
559
560 getgroups(ngroups, group_array);
561}
562
563/* Return non-zero if GID is one that we have in our groups list. */
564static int
565is_a_group_member (gid)
566gid_t gid;
567{
568 register int i;
569
570 /* Short-circuit if possible, maybe saving a call to getgroups(). */
571 if (gid == getgid() || gid == getegid())
572 return (1);
573
574 if (ngroups == 0)
575 initialize_group_array ();
576
577 /* Search through the list looking for GID. */
578 for (i = 0; i < ngroups; i++)
579 if (gid == group_array[i])
580 return (1);
581
582 return (0);
583}
diff --git a/echo.c b/echo.c
new file mode 100644
index 000000000..91f17aa0f
--- /dev/null
+++ b/echo.c
@@ -0,0 +1,126 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * echo implementation for busybox
4 *
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Original copyright notice is retained at the end of this file.
23 */
24
25#include "internal.h"
26#include <stdio.h>
27
28extern int
29echo_main(int argc, char** argv)
30{
31 register char **ap;
32 register char *p;
33 register char c;
34 int nflag = 0;
35 int eflag = 0;
36
37 ap = argv;
38 if (argc)
39 ap++;
40 while ((p = *ap) != NULL && *p == '-') {
41 if (strcmp(p, "-n")==0) {
42 nflag = 1;
43 } else if (strcmp(p, "-e")==0) {
44 eflag = 1;
45 } else if (strcmp(p, "-E")==0) {
46 eflag = 0;
47 }
48 else break;
49 ap++;
50 }
51 while ((p = *ap++) != NULL) {
52 while ((c = *p++) != '\0') {
53 if (c == '\\' && eflag) {
54 switch (c = *p++) {
55 case 'a': c = '\007'; break;
56 case 'b': c = '\b'; break;
57 case 'c': exit( 0); /* exit */
58 case 'f': c = '\f'; break;
59 case 'n': c = '\n'; break;
60 case 'r': c = '\r'; break;
61 case 't': c = '\t'; break;
62 case 'v': c = '\v'; break;
63 case '\\': break; /* c = '\\' */
64 case '0': case '1': case '2': case '3':
65 case '4': case '5': case '6': case '7':
66 c -= '0';
67 if (*p >= '0' && *p <= '7')
68 c = c * 8 + (*p++ - '0');
69 if (*p >= '0' && *p <= '7')
70 c = c * 8 + (*p++ - '0');
71 break;
72 default:
73 p--;
74 break;
75 }
76 }
77 putchar(c);
78 }
79 if (*ap)
80 putchar(' ');
81 }
82 if (! nflag)
83 putchar('\n');
84 fflush(stdout);
85 exit( 0);
86}
87
88/*-
89 * Copyright (c) 1991, 1993
90 * The Regents of the University of California. All rights reserved.
91 *
92 * This code is derived from software contributed to Berkeley by
93 * Kenneth Almquist.
94 *
95 * Redistribution and use in source and binary forms, with or without
96 * modification, are permitted provided that the following conditions
97 * are met:
98 * 1. Redistributions of source code must retain the above copyright
99 * notice, this list of conditions and the following disclaimer.
100 * 2. Redistributions in binary form must reproduce the above copyright
101 * notice, this list of conditions and the following disclaimer in the
102 * documentation and/or other materials provided with the distribution.
103 * 3. All advertising materials mentioning features or use of this software
104 * must display the following acknowledgement:
105 * This product includes software developed by the University of
106 * California, Berkeley and its contributors.
107 * 4. Neither the name of the University nor the names of its contributors
108 * may be used to endorse or promote products derived from this software
109 * without specific prior written permission.
110 *
111 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
112 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
113 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
114 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
115 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
116 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
117 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
118 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
119 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
120 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
121 * SUCH DAMAGE.
122 *
123 * @(#)echo.c 8.1 (Berkeley) 5/31/93
124 */
125
126
diff --git a/shell/cmdedit.c b/shell/cmdedit.c
new file mode 100644
index 000000000..d1604f1d1
--- /dev/null
+++ b/shell/cmdedit.c
@@ -0,0 +1,405 @@
1/*
2 * Termios command line History and Editting for NetBSD sh (ash)
3 * Copyright (c) 1999
4 * Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
5 * Etc: Dave Cinege <dcinege@psychosis.com>
6 * Adjusted for busybox: Erik Andersen <andersee@debian.org>
7 *
8 * You may use this code as you wish, so long as the original author(s)
9 * are attributed in any redistributions of the source code.
10 * This code is 'as is' with no warranty.
11 * This code may safely be consumed by a BSD or GPL license.
12 *
13 * v 0.5 19990328 Initial release
14 *
15 * Future plans: Simple file and path name completion. (like BASH)
16 *
17 */
18
19/*
20 Usage and Known bugs:
21 Terminal key codes are not extensive, and more will probably
22 need to be added. This version was created on Debian GNU/Linux 2.x.
23 Delete, Backspace, Home, End, and the arrow keys were tested
24 to work in an Xterm and console. Ctrl-A also works as Home.
25 Ctrl-E also works as End. The binary size increase is <3K.
26
27 Editting will not display correctly for lines greater then the
28 terminal width. (more then one line.) However, history will.
29 */
30
31#include "internal.h"
32#ifdef BB_FEATURE_SH_COMMAND_EDITING
33
34#include <stdio.h>
35#include <errno.h>
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39#include <termio.h>
40#include <ctype.h>
41#include <signal.h>
42
43#include "cmdedit.h"
44
45
46#define MAX_HISTORY 15 /* Maximum length of the linked list for the command line history */
47
48#define ESC 27
49#define DEL 127
50
51static struct history *his_front = NULL; /* First element in command line list */
52static struct history *his_end = NULL; /* Last element in command line list */
53static struct termio old_term, new_term; /* Current termio and the previous termio before starting ash */
54
55static int history_counter = 0; /* Number of commands in history list */
56static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
57char *parsenextc; /* copy of parsefile->nextc */
58
59struct history {
60 char *s;
61 struct history *p;
62 struct history *n;
63};
64
65
66/* Version of write which resumes after a signal is caught. */
67int xwrite(int fd, char *buf, int nbytes)
68{
69 int ntry;
70 int i;
71 int n;
72
73 n = nbytes;
74 ntry = 0;
75 for (;;) {
76 i = write(fd, buf, n);
77 if (i > 0) {
78 if ((n -= i) <= 0)
79 return nbytes;
80 buf += i;
81 ntry = 0;
82 } else if (i == 0) {
83 if (++ntry > 10)
84 return nbytes - n;
85 } else if (errno != EINTR) {
86 return -1;
87 }
88 }
89}
90
91
92/* Version of ioctl that retries after a signal is caught. */
93int xioctl(int fd, unsigned long request, char *arg)
94{
95 int i;
96 while ((i = ioctl(fd, request, arg)) == -1 && errno == EINTR);
97 return i;
98}
99
100
101void cmdedit_reset_term(void)
102{
103 if (reset_term)
104 xioctl(fileno(stdin), TCSETA, (void *) &old_term);
105}
106
107void gotaSignal(int sig)
108{
109 cmdedit_reset_term();
110 fprintf(stdout, "\n");
111 exit(TRUE);
112}
113
114void input_home(int outputFd, int *cursor)
115{ /* Command line input routines */
116 while (*cursor > 0) {
117 xwrite(outputFd, "\b", 1);
118 --*cursor;
119 }
120}
121
122
123void input_delete(int outputFd, int cursor)
124{
125 int j = 0;
126
127 memmove(parsenextc + cursor, parsenextc + cursor + 1,
128 BUFSIZ - cursor - 1);
129 for (j = cursor; j < (BUFSIZ - 1); j++) {
130 if (!*(parsenextc + j))
131 break;
132 else
133 xwrite(outputFd, (parsenextc + j), 1);
134 }
135
136 xwrite(outputFd, " \b", 2);
137
138 while (j-- > cursor)
139 xwrite(outputFd, "\b", 1);
140}
141
142
143void input_end(int outputFd, int *cursor, int len)
144{
145 while (*cursor < len) {
146 xwrite(outputFd, "\033[C", 3);
147 ++*cursor;
148 }
149}
150
151
152void input_backspace(int outputFd, int *cursor, int *len)
153{
154 int j = 0;
155
156 if (*cursor > 0) {
157 xwrite(outputFd, "\b \b", 3);
158 --*cursor;
159 memmove(parsenextc + *cursor, parsenextc + *cursor + 1,
160 BUFSIZ - *cursor + 1);
161
162 for (j = *cursor; j < (BUFSIZ - 1); j++) {
163 if (!*(parsenextc + j))
164 break;
165 else
166 xwrite(outputFd, (parsenextc + j), 1);
167 }
168
169 xwrite(outputFd, " \b", 2);
170
171 while (j-- > *cursor)
172 xwrite(outputFd, "\b", 1);
173
174 --*len;
175 }
176}
177
178extern int cmdedit_read_input(int inputFd, int outputFd,
179 char command[BUFSIZ])
180{
181
182 int nr = 0;
183 int len = 0;
184 int j = 0;
185 int cursor = 0;
186 int break_out = 0;
187 int ret = 0;
188 char c = 0;
189 struct history *hp = his_end;
190
191 memset(command, 0, sizeof(command));
192 parsenextc = command;
193 if (!reset_term) {
194 xioctl(inputFd, TCGETA, (void *) &old_term);
195 memcpy(&new_term, &old_term, sizeof(struct termio));
196 new_term.c_cc[VMIN] = 1;
197 new_term.c_cc[VTIME] = 0;
198 new_term.c_lflag &= ~ICANON; /* unbuffered input */
199 new_term.c_lflag &= ~ECHO;
200 xioctl(inputFd, TCSETA, (void *) &new_term);
201 reset_term = 1;
202 } else {
203 xioctl(inputFd, TCSETA, (void *) &new_term);
204 }
205
206 memset(parsenextc, 0, BUFSIZ);
207
208 while (1) {
209
210 if ((ret = read(inputFd, &c, 1)) < 1)
211 return ret;
212
213 switch (c) {
214 case 1: /* Control-A Beginning of line */
215 input_home(outputFd, &cursor);
216 break;
217 case 5: /* Control-E EOL */
218 input_end(outputFd, &cursor, len);
219 break;
220 case 4: /* Control-D */
221 if (cursor != len) {
222 input_delete(outputFd, cursor);
223 len--;
224 }
225 break;
226 case '\b': /* Backspace */
227 case DEL:
228 input_backspace(outputFd, &cursor, &len);
229 break;
230 case '\n': /* Enter */
231 *(parsenextc + len++ + 1) = c;
232 xwrite(outputFd, &c, 1);
233 break_out = 1;
234 break;
235 case ESC: /* escape sequence follows */
236 if ((ret = read(inputFd, &c, 1)) < 1)
237 return ret;
238
239 if (c == '[') { /* 91 */
240 if ((ret = read(inputFd, &c, 1)) < 1)
241 return ret;
242
243 switch (c) {
244 case 'A':
245 if (hp && hp->p) { /* Up */
246 hp = hp->p;
247 goto hop;
248 }
249 break;
250 case 'B':
251 if (hp && hp->n && hp->n->s) { /* Down */
252 hp = hp->n;
253 goto hop;
254 }
255 break;
256
257 hop: /* hop */
258 len = strlen(parsenextc);
259
260 for (; cursor > 0; cursor--) /* return to begining of line */
261 xwrite(outputFd, "\b", 1);
262
263 for (j = 0; j < len; j++) /* erase old command */
264 xwrite(outputFd, " ", 1);
265
266 for (j = len; j > 0; j--) /* return to begining of line */
267 xwrite(outputFd, "\b", 1);
268
269 strcpy(parsenextc, hp->s); /* write new command */
270 len = strlen(hp->s);
271 xwrite(outputFd, parsenextc, len);
272 cursor = len;
273 break;
274 case 'C': /* Right */
275 if (cursor < len) {
276 xwrite(outputFd, "\033[C", 3);
277 cursor++;
278 }
279 break;
280 case 'D': /* Left */
281 if (cursor > 0) {
282 xwrite(outputFd, "\033[D", 3);
283 cursor--;
284 }
285 break;
286 case '3': /* Delete */
287 if (cursor != len) {
288 input_delete(outputFd, cursor);
289 len--;
290 }
291 break;
292 case '1': /* Home (Ctrl-A) */
293 input_home(outputFd, &cursor);
294 break;
295 case '4': /* End (Ctrl-E) */
296 input_end(outputFd, &cursor, len);
297 break;
298 }
299 if (c == '1' || c == '3' || c == '4')
300 if ((ret = read(inputFd, &c, 1)) < 1)
301 return ret; /* read 126 (~) */
302 }
303 if (c == 'O') { /* 79 */
304 if ((ret = read(inputFd, &c, 1)) < 1)
305 return ret;
306 switch (c) {
307 case 'H': /* Home (xterm) */
308 input_home(outputFd, &cursor);
309 break;
310 case 'F': /* End (xterm) */
311 input_end(outputFd, &cursor, len);
312 break;
313 }
314 }
315 c = 0;
316 break;
317
318 default: /* If it's regular input, do the normal thing */
319
320 if (!isprint(c)) /* Skip non-printable characters */
321 break;
322
323 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
324 break;
325
326 len++;
327
328 if (cursor == (len - 1)) { /* Append if at the end of the line */
329 *(parsenextc + cursor) = c;
330 } else { /* Insert otherwise */
331 memmove(parsenextc + cursor + 1, parsenextc + cursor,
332 len - cursor - 1);
333
334 *(parsenextc + cursor) = c;
335
336 for (j = cursor; j < len; j++)
337 xwrite(outputFd, parsenextc + j, 1);
338 for (; j > cursor; j--)
339 xwrite(outputFd, "\033[D", 3);
340 }
341
342 cursor++;
343 xwrite(outputFd, &c, 1);
344 break;
345 }
346
347 if (break_out) /* Enter is the command terminator, no more input. */
348 break;
349 }
350
351 nr = len + 1;
352 xioctl(inputFd, TCSETA, (void *) &old_term);
353 reset_term = 0;
354
355
356 if (*(parsenextc)) { /* Handle command history log */
357
358 struct history *h = his_end;
359
360 if (!h) { /* No previous history */
361 h = his_front = malloc(sizeof(struct history));
362 h->n = malloc(sizeof(struct history));
363 h->p = NULL;
364 h->s = strdup(parsenextc);
365
366 h->n->p = h;
367 h->n->n = NULL;
368 h->n->s = NULL;
369 his_end = h->n;
370 history_counter++;
371 } else { /* Add a new history command */
372
373 h->n = malloc(sizeof(struct history));
374
375 h->n->p = h;
376 h->n->n = NULL;
377 h->n->s = NULL;
378 h->s = strdup(parsenextc);
379 his_end = h->n;
380
381 if (history_counter >= MAX_HISTORY) { /* After max history, remove the last known command */
382
383 struct history *p = his_front->n;
384
385 p->p = NULL;
386 free(his_front->s);
387 free(his_front);
388 his_front = p;
389 } else {
390 history_counter++;
391 }
392 }
393 }
394
395 return nr;
396}
397
398extern void cmdedit_init(void)
399{
400 atexit(cmdedit_reset_term);
401 signal(SIGINT, gotaSignal);
402 signal(SIGQUIT, gotaSignal);
403 signal(SIGTERM, gotaSignal);
404}
405#endif /* BB_FEATURE_SH_COMMAND_EDITING */
diff --git a/shell/cmdedit.h b/shell/cmdedit.h
new file mode 100644
index 000000000..e776543d6
--- /dev/null
+++ b/shell/cmdedit.h
@@ -0,0 +1,17 @@
1/*
2 * Termios command line History and Editting for NetBSD sh (ash)
3 * Copyright (c) 1999
4 * Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
5 * Etc: Dave Cinege <dcinege@psychosis.com>
6 * Adjusted for busybox: Erik Andersen <andersee@debian.org>
7 *
8 * You may use this code as you wish, so long as the original author(s)
9 * are attributed in any redistributions of the source code.
10 * This code is 'as is' with no warranty.
11 * This code may safely be consumed by a BSD or GPL license.
12 *
13 */
14
15extern int cmdedit_read_input(int inputFd, int outputFd, char command[BUFSIZ]);
16extern void cmdedit_init(void);
17
diff --git a/test.c b/test.c
new file mode 100644
index 000000000..85d06a83a
--- /dev/null
+++ b/test.c
@@ -0,0 +1,583 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * echo implementation for busybox
4 *
5 * Copyright (c) by a whole pile of folks:
6 *
7 * test(1); version 7-like -- author Erik Baalbergen
8 * modified by Eric Gisin to be used as built-in.
9 * modified by Arnold Robbins to add SVR3 compatibility
10 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
11 * modified by J.T. Conklin for NetBSD.
12 * modified by Herbert Xu to be used as built-in in ash.
13 * modified by Erik Andersen <andersee@debian.org> to be used
14 * in busybox.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * Original copyright notice states:
31 * "This program is in the Public Domain."
32 */
33
34#include "internal.h"
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <unistd.h>
38#include <ctype.h>
39#include <errno.h>
40#include <stdlib.h>
41#include <string.h>
42
43/* test(1) accepts the following grammar:
44 oexpr ::= aexpr | aexpr "-o" oexpr ;
45 aexpr ::= nexpr | nexpr "-a" aexpr ;
46 nexpr ::= primary | "!" primary
47 primary ::= unary-operator operand
48 | operand binary-operator operand
49 | operand
50 | "(" oexpr ")"
51 ;
52 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
53 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
54
55 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
56 "-nt"|"-ot"|"-ef";
57 operand ::= <any legal UNIX file name>
58*/
59
60enum token {
61 EOI,
62 FILRD,
63 FILWR,
64 FILEX,
65 FILEXIST,
66 FILREG,
67 FILDIR,
68 FILCDEV,
69 FILBDEV,
70 FILFIFO,
71 FILSOCK,
72 FILSYM,
73 FILGZ,
74 FILTT,
75 FILSUID,
76 FILSGID,
77 FILSTCK,
78 FILNT,
79 FILOT,
80 FILEQ,
81 FILUID,
82 FILGID,
83 STREZ,
84 STRNZ,
85 STREQ,
86 STRNE,
87 STRLT,
88 STRGT,
89 INTEQ,
90 INTNE,
91 INTGE,
92 INTGT,
93 INTLE,
94 INTLT,
95 UNOT,
96 BAND,
97 BOR,
98 LPAREN,
99 RPAREN,
100 OPERAND
101};
102
103enum token_types {
104 UNOP,
105 BINOP,
106 BUNOP,
107 BBINOP,
108 PAREN
109};
110
111struct t_op {
112 const char *op_text;
113 short op_num, op_type;
114} const ops [] = {
115 {"-r", FILRD, UNOP},
116 {"-w", FILWR, UNOP},
117 {"-x", FILEX, UNOP},
118 {"-e", FILEXIST,UNOP},
119 {"-f", FILREG, UNOP},
120 {"-d", FILDIR, UNOP},
121 {"-c", FILCDEV,UNOP},
122 {"-b", FILBDEV,UNOP},
123 {"-p", FILFIFO,UNOP},
124 {"-u", FILSUID,UNOP},
125 {"-g", FILSGID,UNOP},
126 {"-k", FILSTCK,UNOP},
127 {"-s", FILGZ, UNOP},
128 {"-t", FILTT, UNOP},
129 {"-z", STREZ, UNOP},
130 {"-n", STRNZ, UNOP},
131 {"-h", FILSYM, UNOP}, /* for backwards compat */
132 {"-O", FILUID, UNOP},
133 {"-G", FILGID, UNOP},
134 {"-L", FILSYM, UNOP},
135 {"-S", FILSOCK,UNOP},
136 {"=", STREQ, BINOP},
137 {"!=", STRNE, BINOP},
138 {"<", STRLT, BINOP},
139 {">", STRGT, BINOP},
140 {"-eq", INTEQ, BINOP},
141 {"-ne", INTNE, BINOP},
142 {"-ge", INTGE, BINOP},
143 {"-gt", INTGT, BINOP},
144 {"-le", INTLE, BINOP},
145 {"-lt", INTLT, BINOP},
146 {"-nt", FILNT, BINOP},
147 {"-ot", FILOT, BINOP},
148 {"-ef", FILEQ, BINOP},
149 {"!", UNOT, BUNOP},
150 {"-a", BAND, BBINOP},
151 {"-o", BOR, BBINOP},
152 {"(", LPAREN, PAREN},
153 {")", RPAREN, PAREN},
154 {0, 0, 0}
155};
156
157char **t_wp;
158struct t_op const *t_wp_op;
159static gid_t *group_array = NULL;
160static int ngroups;
161
162static enum token t_lex();
163static int oexpr();
164static int aexpr();
165static int nexpr();
166static int binop();
167static int primary();
168static int filstat();
169static int getn();
170static int newerf();
171static int olderf();
172static int equalf();
173static void syntax();
174static int test_eaccess();
175static int is_a_group_member();
176static void initialize_group_array();
177
178extern int
179test_main(int argc, char** argv)
180{
181 int res;
182
183 if (strcmp(argv[0], "[") == 0) {
184 if (strcmp(argv[--argc], "]"))
185 fatalError("missing ]");
186 argv[argc] = NULL;
187 }
188
189 /* Implement special cases from POSIX.2, section 4.62.4 */
190 switch (argc) {
191 case 1:
192 exit( 1);
193 case 2:
194 exit (*argv[1] == '\0');
195 case 3:
196 if (argv[1][0] == '!' && argv[1][1] == '\0') {
197 exit (!(*argv[2] == '\0'));
198 }
199 break;
200 case 4:
201 if (argv[1][0] != '!' || argv[1][1] != '\0') {
202 if (t_lex(argv[2]),
203 t_wp_op && t_wp_op->op_type == BINOP) {
204 t_wp = &argv[1];
205 exit (binop() == 0);
206 }
207 }
208 break;
209 case 5:
210 if (argv[1][0] == '!' && argv[1][1] == '\0') {
211 if (t_lex(argv[3]),
212 t_wp_op && t_wp_op->op_type == BINOP) {
213 t_wp = &argv[2];
214 exit (!(binop() == 0));
215 }
216 }
217 break;
218 }
219
220 t_wp = &argv[1];
221 res = !oexpr(t_lex(*t_wp));
222
223 if (*t_wp != NULL && *++t_wp != NULL)
224 syntax(*t_wp, "unknown operand");
225
226 exit( res);
227}
228
229static void
230syntax(op, msg)
231 char *op;
232 char *msg;
233{
234 if (op && *op)
235 fatalError("%s: %s", op, msg);
236 else
237 fatalError("%s", msg);
238}
239
240static int
241oexpr(n)
242 enum token n;
243{
244 int res;
245
246 res = aexpr(n);
247 if (t_lex(*++t_wp) == BOR)
248 return oexpr(t_lex(*++t_wp)) || res;
249 t_wp--;
250 return res;
251}
252
253static int
254aexpr(n)
255 enum token n;
256{
257 int res;
258
259 res = nexpr(n);
260 if (t_lex(*++t_wp) == BAND)
261 return aexpr(t_lex(*++t_wp)) && res;
262 t_wp--;
263 return res;
264}
265
266static int
267nexpr(n)
268 enum token n; /* token */
269{
270 if (n == UNOT)
271 return !nexpr(t_lex(*++t_wp));
272 return primary(n);
273}
274
275static int
276primary(n)
277 enum token n;
278{
279 int res;
280
281 if (n == EOI)
282 syntax(NULL, "argument expected");
283 if (n == LPAREN) {
284 res = oexpr(t_lex(*++t_wp));
285 if (t_lex(*++t_wp) != RPAREN)
286 syntax(NULL, "closing paren expected");
287 return res;
288 }
289 if (t_wp_op && t_wp_op->op_type == UNOP) {
290 /* unary expression */
291 if (*++t_wp == NULL)
292 syntax(t_wp_op->op_text, "argument expected");
293 switch (n) {
294 case STREZ:
295 return strlen(*t_wp) == 0;
296 case STRNZ:
297 return strlen(*t_wp) != 0;
298 case FILTT:
299 return isatty(getn(*t_wp));
300 default:
301 return filstat(*t_wp, n);
302 }
303 }
304
305 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
306 return binop();
307 }
308
309 return strlen(*t_wp) > 0;
310}
311
312static int
313binop()
314{
315 const char *opnd1, *opnd2;
316 struct t_op const *op;
317
318 opnd1 = *t_wp;
319 (void) t_lex(*++t_wp);
320 op = t_wp_op;
321
322 if ((opnd2 = *++t_wp) == (char *)0)
323 syntax(op->op_text, "argument expected");
324
325 switch (op->op_num) {
326 case STREQ:
327 return strcmp(opnd1, opnd2) == 0;
328 case STRNE:
329 return strcmp(opnd1, opnd2) != 0;
330 case STRLT:
331 return strcmp(opnd1, opnd2) < 0;
332 case STRGT:
333 return strcmp(opnd1, opnd2) > 0;
334 case INTEQ:
335 return getn(opnd1) == getn(opnd2);
336 case INTNE:
337 return getn(opnd1) != getn(opnd2);
338 case INTGE:
339 return getn(opnd1) >= getn(opnd2);
340 case INTGT:
341 return getn(opnd1) > getn(opnd2);
342 case INTLE:
343 return getn(opnd1) <= getn(opnd2);
344 case INTLT:
345 return getn(opnd1) < getn(opnd2);
346 case FILNT:
347 return newerf (opnd1, opnd2);
348 case FILOT:
349 return olderf (opnd1, opnd2);
350 case FILEQ:
351 return equalf (opnd1, opnd2);
352 }
353 /* NOTREACHED */
354 return 1;
355}
356
357static int
358filstat(nm, mode)
359 char *nm;
360 enum token mode;
361{
362 struct stat s;
363 int i;
364
365 if (mode == FILSYM) {
366#ifdef S_IFLNK
367 if (lstat(nm, &s) == 0) {
368 i = S_IFLNK;
369 goto filetype;
370 }
371#endif
372 return 0;
373 }
374
375 if (stat(nm, &s) != 0)
376 return 0;
377
378 switch (mode) {
379 case FILRD:
380 return test_eaccess(nm, R_OK) == 0;
381 case FILWR:
382 return test_eaccess(nm, W_OK) == 0;
383 case FILEX:
384 return test_eaccess(nm, X_OK) == 0;
385 case FILEXIST:
386 return 1;
387 case FILREG:
388 i = S_IFREG;
389 goto filetype;
390 case FILDIR:
391 i = S_IFDIR;
392 goto filetype;
393 case FILCDEV:
394 i = S_IFCHR;
395 goto filetype;
396 case FILBDEV:
397 i = S_IFBLK;
398 goto filetype;
399 case FILFIFO:
400#ifdef S_IFIFO
401 i = S_IFIFO;
402 goto filetype;
403#else
404 return 0;
405#endif
406 case FILSOCK:
407#ifdef S_IFSOCK
408 i = S_IFSOCK;
409 goto filetype;
410#else
411 return 0;
412#endif
413 case FILSUID:
414 i = S_ISUID;
415 goto filebit;
416 case FILSGID:
417 i = S_ISGID;
418 goto filebit;
419 case FILSTCK:
420 i = S_ISVTX;
421 goto filebit;
422 case FILGZ:
423 return s.st_size > 0L;
424 case FILUID:
425 return s.st_uid == geteuid();
426 case FILGID:
427 return s.st_gid == getegid();
428 default:
429 return 1;
430 }
431
432filetype:
433 return ((s.st_mode & S_IFMT) == i);
434
435filebit:
436 return ((s.st_mode & i) != 0);
437}
438
439static enum token
440t_lex(s)
441 char *s;
442{
443 struct t_op const *op = ops;
444
445 if (s == 0) {
446 t_wp_op = (struct t_op *)0;
447 return EOI;
448 }
449 while (op->op_text) {
450 if (strcmp(s, op->op_text) == 0) {
451 t_wp_op = op;
452 return op->op_num;
453 }
454 op++;
455 }
456 t_wp_op = (struct t_op *)0;
457 return OPERAND;
458}
459
460/* atoi with error detection */
461static int
462getn(s)
463 char *s;
464{
465 char *p;
466 long r;
467
468 errno = 0;
469 r = strtol(s, &p, 10);
470
471 if (errno != 0)
472 fatalError("%s: out of range", s);
473
474 while (isspace(*p))
475 p++;
476
477 if (*p)
478 fatalError("%s: bad number", s);
479
480 return (int) r;
481}
482
483static int
484newerf (f1, f2)
485char *f1, *f2;
486{
487 struct stat b1, b2;
488
489 return (stat (f1, &b1) == 0 &&
490 stat (f2, &b2) == 0 &&
491 b1.st_mtime > b2.st_mtime);
492}
493
494static int
495olderf (f1, f2)
496char *f1, *f2;
497{
498 struct stat b1, b2;
499
500 return (stat (f1, &b1) == 0 &&
501 stat (f2, &b2) == 0 &&
502 b1.st_mtime < b2.st_mtime);
503}
504
505static int
506equalf (f1, f2)
507char *f1, *f2;
508{
509 struct stat b1, b2;
510
511 return (stat (f1, &b1) == 0 &&
512 stat (f2, &b2) == 0 &&
513 b1.st_dev == b2.st_dev &&
514 b1.st_ino == b2.st_ino);
515}
516
517/* Do the same thing access(2) does, but use the effective uid and gid,
518 and don't make the mistake of telling root that any file is
519 executable. */
520static int
521test_eaccess (path, mode)
522char *path;
523int mode;
524{
525 struct stat st;
526 int euid = geteuid();
527
528 if (stat (path, &st) < 0)
529 return (-1);
530
531 if (euid == 0) {
532 /* Root can read or write any file. */
533 if (mode != X_OK)
534 return (0);
535
536 /* Root can execute any file that has any one of the execute
537 bits set. */
538 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
539 return (0);
540 }
541
542 if (st.st_uid == euid) /* owner */
543 mode <<= 6;
544 else if (is_a_group_member (st.st_gid))
545 mode <<= 3;
546
547 if (st.st_mode & mode)
548 return (0);
549
550 return (-1);
551}
552
553static void
554initialize_group_array ()
555{
556 ngroups = getgroups(0, NULL);
557 if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
558 fatalError("Out of space");
559
560 getgroups(ngroups, group_array);
561}
562
563/* Return non-zero if GID is one that we have in our groups list. */
564static int
565is_a_group_member (gid)
566gid_t gid;
567{
568 register int i;
569
570 /* Short-circuit if possible, maybe saving a call to getgroups(). */
571 if (gid == getgid() || gid == getegid())
572 return (1);
573
574 if (ngroups == 0)
575 initialize_group_array ();
576
577 /* Search through the list looking for GID. */
578 for (i = 0; i < ngroups; i++)
579 if (gid == group_array[i])
580 return (1);
581
582 return (0);
583}