aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-09-05 03:22:19 +0000
committerlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-09-05 03:22:19 +0000
commit6c01118cc07948fd47fb09ae8117ac1df9cb7e6a (patch)
treee11c90e12d56264ded29c590b55c36a8d1850e8b /shell
parent1b0bf8531aaef416e6ef5f38d5160ff43e3ff8c9 (diff)
downloadbusybox-w32-6c01118cc07948fd47fb09ae8117ac1df9cb7e6a.tar.gz
busybox-w32-6c01118cc07948fd47fb09ae8117ac1df9cb7e6a.tar.bz2
busybox-w32-6c01118cc07948fd47fb09ae8117ac1df9cb7e6a.zip
Might as well commit this to have the history. It's not linked in to the
applet list yet (and won't be until it can replace lash, I'm not having five shells in menuconfig at once), but you can build it with scripts/individual and mostly this is checked in so I can bloatcheck future versions against it easily.... This is about as small as a shell can get and still be a shell. git-svn-id: svn://busybox.net/trunk/busybox@16051 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'shell')
-rw-r--r--shell/bbsh.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/shell/bbsh.c b/shell/bbsh.c
new file mode 100644
index 000000000..f2d76cc01
--- /dev/null
+++ b/shell/bbsh.c
@@ -0,0 +1,73 @@
1/* vi: set ts=4 :
2 *
3 * bbsh - busybox shell
4 *
5 * Copyright 2006 Rob Landley <rob@landley.net>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10// Handle embedded NUL bytes in the command line.
11
12#include <busybox.h>
13
14static int handle(char *command)
15{
16 int argc=0;
17 char *argv[10], *start = command;
18
19 // Parse command into argv[]
20 for (;;) {
21 char *end;
22
23 // Skip leading whitespace and detect EOL.
24 while(isspace(*start)) start++;
25 if(!*start || *start=='#') break;
26
27 // Grab next word. (Add dequote and envvar logic here)
28 end=start;
29 while(*end && !isspace(*end)) end++;
30 argv[argc++]=xstrndup(start,end-start);
31 start=end;
32 }
33 argv[argc]=0;
34
35 if (!argc) return 0;
36 if (argc==2 && !strcmp(argv[0],"cd")) chdir(argv[1]);
37 else if(!strcmp(argv[0],"exit")) exit(argc>1 ? atoi(argv[1]) : 0);
38 else {
39 int status;
40 pid_t pid=fork();
41 if(!pid) {
42 run_applet_by_name(argv[0],argc,argv);
43 execvp(argv[0],argv);
44 printf("No %s",argv[0]);
45 exit(1);
46 } else waitpid(pid, &status, 0);
47 }
48 while(argc) free(argv[--argc]);
49
50 return 0;
51}
52
53int bbsh_main(int argc, char *argv[])
54{
55 char *command=NULL;
56 FILE *f;
57
58 bb_getopt_ulflags(argc, argv, "c:", &command);
59
60 f = argv[optind] ? xfopen(argv[optind],"r") : NULL;
61 if (command) handle(command);
62 else {
63 unsigned cmdlen=0;
64 for (;;) {
65 if(!f) putchar('$');
66 if(1 > getline(&command,&cmdlen,f ? : stdin)) break;
67 handle(command);
68 }
69 if (ENABLE_FEATURE_CLEAN_UP) free(command);
70 }
71
72 return 1;
73}