# HG changeset patch # User Rob Landley # Date 1398291789 18000 # Node ID dd2fd057111fec0e6c2a9910c9ec3412fd74af0c # Parent a302299dbb98ad9673e17c7caae9e6312552badd Add example directory, move hello.c into it, add skeleton.c to demonstrate more complciated stuff (multiple commands per file, etc), and have genconfig.sh sort backwards so posix is first and example last in menuconfig. diff -r a302299dbb98 -r dd2fd057111f scripts/genconfig.sh --- a/scripts/genconfig.sh Wed Apr 23 08:38:29 2014 -0500 +++ b/scripts/genconfig.sh Wed Apr 23 17:23:09 2014 -0500 @@ -43,9 +43,8 @@ genconfig() { - # I could query the directory here, but I want to control the order - # and capitalization in the menu - for j in toys/*/README + # Reverse sort puts posix first, examples last. + for j in $(ls toys/*/README | sort -r) do DIR="$(dirname "$j")" diff -r a302299dbb98 -r dd2fd057111f toys/example/README --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/toys/example/README Wed Apr 23 17:23:09 2014 -0500 @@ -0,0 +1,4 @@ +Example commands + +You probably don't want to deploy this, but it shows how to use the +toybox infrastructure and provides templates for new commands. diff -r a302299dbb98 -r dd2fd057111f toys/example/hello.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/toys/example/hello.c Wed Apr 23 17:23:09 2014 -0500 @@ -0,0 +1,35 @@ +/* hello.c - A hello world program. (Simple template for new commands.) + * + * Copyright 2012 Rob Landley + * + * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ + * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html + +USE_HELLO(NEWTOY(hello, 0, TOYFLAG_USR|TOYFLAG_BIN)) + +config HELLO + bool "hello" + default n + help + usage: hello [-s] + + A hello world program. You don't need this. + + Mostly used as a simple template for adding new commands. + Occasionally nice to smoketest kernel booting via "init=/usr/bin/hello". +*/ + +#define FOR_hello +#include "toys.h" + +GLOBALS( + int unused; +) + +void hello_main(void) +{ + xprintf("Hello world\n"); + + // Avoid kernel panic if run as init. + if (getpid() == 1) wait(&TT.unused); +} diff -r a302299dbb98 -r dd2fd057111f toys/example/skeleton.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/toys/example/skeleton.c Wed Apr 23 17:23:09 2014 -0500 @@ -0,0 +1,104 @@ +/* skeleton.c - Example program to act as template for new commands. + * + * Copyright 2014 Rob Landley + * + * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ + * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html + +// Accept many different kinds of command line argument (see top of lib/args.c) +// Demonstrate two commands in the same file (see www/documentation.html) + +USE_SKELETON(NEWTOY(skeleton, "(walrus)(blubber):;(also):e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN)) +USE_SKELETON_ALIAS(NEWTOY(skeleton_alias, "b#dq", TOYFLAG_USR|TOYFLAG_BIN)) + +config SKELETON + bool "skeleton" + default n + help + usage: skeleton [-a] [-b STRING] [-c NUMBER] [-d LIST] [-e COUNT] [...] + + Template for new commands. You don't need this. + + When creating a new command, copy this file and delete the parts you + don't need. Be sure to replace all instances of "skeleton" (upper and lower + case) with your new command name. + + For simple commands, "hello.c" is probably a better starting point. + +config SKELETON_ALIAS + bool "skeleton_alias" + default n + depends on SKELETON + help + usage: skeleton_alias [-dq] [-b NUMBER] + + Example of a second command with different arguments in the same source + file as the first. This allows shared infrastructure not added to lib/. +*/ + +#define FOR_skeleton +#include "toys.h" + +// The union lets lib/args.c store arguments for either command. +// It's customary to put a space between argument variables and other globals. +GLOBALS( + union { + struct { + char *b_string; + long c_number; + struct arg_list *d_list; + long e_count; + char *also_string; + char *blubber_string; + } s; + struct { + long b_number; + } a; + }; + + int more_globals; +) + +// Don't blindly build allyesconfig. The maximum _sane_ config is defconfig. +#warning skeleton.c is just an example, not something to deploy. + +// Parse many different kinds of command line argument: +void skeleton_main(void) +{ + char **optargs; + + printf("Ran %s\n", toys.which->name); + + // Command line options parsing is done for you by lib/args.c called + // from main.c using the optstring in the NEWTOY macros. Display results. + if (toys.optflags) printf("flags=%x\n", toys.optflags); + if (toys.optflags & FLAG_a) printf("Saw a\n"); + if (toys.optflags & FLAG_b) printf("b=%s\n", TT.s.b_string); + if (toys.optflags & FLAG_c) printf("c=%ld\n", TT.s.c_number); + while (TT.s.d_list) { + printf("d=%s\n", TT.s.d_list->arg); + TT.s.d_list = TT.s.d_list->next; + } + if (TT.s.e_count) printf("e was seen %ld times\n", TT.s.e_count); + for (optargs = toys.optargs; *optargs; optargs++) + printf("optarg=%s\n", *optargs); + if (toys.optflags & FLAG_walrus) printf("Saw --walrus\n"); + if (TT.s.blubber_string) printf("--blubber=%s\n", TT.s.blubber_string); + + printf("Other globals should start zeroed: %d\n", TT.more_globals); +} + +// Switch gears from skeleton to skeleton_alias (swap FLAG macros). +#define CLEANUP_skeleton +#define FOR_skeleton_alias +#include "generated/flags.h" + +void skeleton_alias_main(void) +{ + printf("Ran %s\n", toys.which->name); + printf("flags=%x\n", toys.optflags); + + // Note, this FLAG_b is a different bit position than the other FLAG_b, + // and fills out a different variable of a different type. + if (toys.optflags & FLAG_b) printf("b=%ld", TT.a.b_number); +} diff -r a302299dbb98 -r dd2fd057111f toys/other/hello.c --- a/toys/other/hello.c Wed Apr 23 08:38:29 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -/* hello.c - A hello world program. (Template for new commands.) - * - * Copyright 2012 Rob Landley - * - * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ - * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html - -// Accept many different kinds of command line argument: - -USE_HELLO(NEWTOY(hello, "(walrus)(blubber):;(also):e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN)) - -config HELLO - bool "hello" - default n - help - usage: hello [-a] [-b string] [-c number] [-d list] [-e count] [...] - - A hello world program. You don't need this. - - Mostly used as an example/skeleton file for adding new commands, - occasionally nice to test kernel booting via "init=/bin/hello". -*/ - -#define FOR_hello -#include "toys.h" - -// Hello doesn't use these globals, they're here for example/skeleton purposes. - -GLOBALS( - char *b_string; - long c_number; - struct arg_list *d_list; - long e_count; - char *also_string; - char *blubber_string; - - int more_globals; -) - -// Parse many different kinds of command line argument: - -void hello_main(void) -{ - char **optargs; - - printf("Hello world\n"); - - if (toys.optflags) printf("flags=%x\n", toys.optflags); - if (toys.optflags & FLAG_a) printf("Saw a\n"); - if (toys.optflags & FLAG_b) printf("b=%s\n", TT.b_string); - if (toys.optflags & FLAG_c) printf("c=%ld\n", TT.c_number); - while (TT.d_list) { - printf("d=%s\n", TT.d_list->arg); - TT.d_list = TT.d_list->next; - } - if (TT.e_count) printf("e was seen %ld times\n", TT.e_count); - for (optargs = toys.optargs; *optargs; optargs++) - printf("optarg=%s\n", *optargs); - if (toys.optflags & FLAG_walrus) printf("Saw --walrus\n"); - if (TT.blubber_string) printf("--blubber=%s\n", TT.blubber_string); -}