From c56215062c961402515daeef8330ed75cd94af29 Mon Sep 17 00:00:00 2001 From: landley Date: Thu, 28 Sep 2006 17:18:51 -0400 Subject: Next snapshot. Tries to grab something out of lib in order to build, I have an empty "blah.c" in there to make it happy but I'm not checking that in. --- Makefile | 5 ++++ main.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ toys.h | 48 +++++++++++++++++++++++++++++++ toys/df.c | 7 +++++ toys/main.c | 95 ------------------------------------------------------------- 5 files changed, 148 insertions(+), 95 deletions(-) create mode 100644 Makefile create mode 100644 main.c create mode 100644 toys.h create mode 100644 toys/df.c delete mode 100644 toys/main.c diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..984cdeb0 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: + gcc -Os -s $(CFLAGS) -I . main.c toys/*.c lib/*.c -o toybox + +clean: + rm toybox diff --git a/main.c b/main.c new file mode 100644 index 00000000..396a354c --- /dev/null +++ b/main.c @@ -0,0 +1,88 @@ +/* vi: set ts=4 :*/ +/* Toybox infrastructure. + * + * Copyright 2006 Rob Landley + * + * Licensed under GPL version 2, see file LICENSE in this tarball for details. + */ + +#include "toys.h" + +// The monster fun applet list. + +struct toy_list toy_list[] = { + {"toybox", toybox_main}, + {"df", df_main}, + {"toysh", toysh_main} +}; + +// global context for this applet. + +struct toy_context toys; + + + +/* +name +main() +struct +usage (short long example info) +path (/usr/sbin) +*/ + +int toybox_main(void) +{ + printf("toybox\n"); + return 0; +} + +int toysh_main(void) +{ + printf("toysh\n"); +} + +struct toy_list *find_toy_by_name(char *name) +{ + int top, bottom, middle; + + // If the name starts with "toybox", accept that as a match. Otherwise + // skip the first entry, which is out of order. + + if (!strncmp(name,"toybox",6)) return toy_list; + bottom=1; + + // Binary search to find this applet. + + top=(sizeof(toy_list)/sizeof(struct toy_list))-1; + for(;;) { + int result; + + middle=(top+bottom)/2; + if(middletop) return NULL; + result = strcmp(name,toy_list[middle].name); + if(!result) return toy_list+middle; + if(result<0) top=--middle; + else bottom=++middle; + } +} + +int main(int argc, char *argv[]) +{ + char *name; + + // Record command line arguments. + toys.argc = argc; + toys.argv = argv; + + // Figure out which applet got called. + name = rindex(argv[0],'/'); + if (!name) name = argv[0]; + else name++; + toys.which = find_toy_by_name(name); + + if (!toys.which) { + dprintf(2,"No behavior for %s\n",name); + return 1; + } + return toys.which->toy_main(); +} diff --git a/toys.h b/toys.h new file mode 100644 index 00000000..ec1ad762 --- /dev/null +++ b/toys.h @@ -0,0 +1,48 @@ +/* vi: set ts=4 :*/ +/* Toybox infrastructure. + * + * Copyright 2006 Rob Landley + * + * Licensed under GPL version 2, see file LICENSE in this tarball for details. + */ + +#include +#include + +/* +name +main() +struct +usage (short long example info) +path (/usr/sbin) +*/ + +int toybox_main(void); +int toysh_main(void); +int df_main(void); + +extern struct toy_list { + char *name; + int (*toy_main)(void); +} toy_list[]; +struct toy_list *find_toy_by_name(char *name); + +// Global context for this applet. + +extern struct toy_context { + struct toy_list *which; + int argc; + char **argv; + char buf[4096]; +} toys; + +struct toybox_data {;}; +struct toysh_data {;}; +struct df_data {;}; + +union toy_union { + struct toybox_data toybox; + struct toysh_data toysh; + struct df_data df; +} toy; + diff --git a/toys/df.c b/toys/df.c new file mode 100644 index 00000000..05c4b6c4 --- /dev/null +++ b/toys/df.c @@ -0,0 +1,7 @@ +/* vi: set ts=4 : */ +#include "toys.h" + +int df_main(void) +{ + printf("toys.which->name=%s\n",toys.which->name); +} diff --git a/toys/main.c b/toys/main.c deleted file mode 100644 index bd15d602..00000000 --- a/toys/main.c +++ /dev/null @@ -1,95 +0,0 @@ -/* vi: set ts=4 :*/ -/* Toybox infrastructure. - * - * Copyright 2006 Rob Landley - * - * Licensed under GPL version 2, see file LICENSE in this tarball for details. - */ - -#include -#include - -/* -name -main() -struct -usage (short long example info) -path (/usr/sbin) -*/ - -int toybox_main(void) -{ - printf("toybox\n"); - return 0; -} - -int toysh_main(void) -{ - printf("toysh\n"); -} - -// The monster fun applet list. - -struct toy_list { - char *name; - int (*toy_main)(void); -} toy_list[] = { - {"toybox", toybox_main}, - {"toysh", toysh_main} -}; - -// Global context for this applet. - -struct toy_context { - struct toy_list *which; - int argc; - char **argv; - char buf[4096]; -// toy_union toydata; -} toys; - -struct toy_list *find_toy_by_name(char *name) -{ - int top, bottom, middle; - - // If the name starts with "toybox", accept that as a match. Otherwise - // skip the first entry, which is out of order. - - if (!strncmp(name,"toybox",6)) return toy_list; - bottom=1; - - // Binary search to find this applet. - - top=(sizeof(toy_list)/sizeof(struct toy_list))-1; - for(;;) { - int result; - - middle=(top+bottom)/2; - if(middletop) return NULL; - result = strcmp(name,toy_list[middle].name); - if(!result) return toy_list+middle; - if(result<0) top=--middle; - else bottom=++middle; - } -} - -int main(int argc, char *argv[]) -{ - char *name; - - // Record command line arguments. - toys.argc = argc; - toys.argv = argv; - - // Figure out which applet got called. - name = rindex(argv[0],'/'); - if (!name) name = argv[0]; - else name++; - toys.which = find_toy_by_name(name); - - if (!toys.which) { - dprintf(2,"No behavior for %s\n",name); - return 1; - } - return toys.which->toy_main(); -} -- cgit v1.2.3