From 7c04f01bc73082a170c9a1988bf62c2428acc4f9 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 20 Jan 2008 19:00:16 -0600 Subject: Fluff out hello.c to supply more example code as a skeleton for new commands, and update a chunk of code.html (much more to do there). --- scripts/genconfig.sh | 3 ++ toys/hello.c | 15 +++++++- www/code.html | 106 ++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 113 insertions(+), 11 deletions(-) diff --git a/scripts/genconfig.sh b/scripts/genconfig.sh index 59c63c5b..126e52ef 100755 --- a/scripts/genconfig.sh +++ b/scripts/genconfig.sh @@ -1,5 +1,8 @@ #!/bin/bash +# This has to be a separate file from scripts/make.sh so it can be called +# before menuconfig. (It's called again from scripts/make.sh just to be sure.) + mkdir -p generated function genconfig() diff --git a/toys/hello.c b/toys/hello.c index a8caeefe..e5d78f6a 100644 --- a/toys/hello.c +++ b/toys/hello.c @@ -7,7 +7,7 @@ * Not in SUSv3. * See http://www.opengroup.org/onlinepubs/009695399/utilities/ -USE_HELLO(NEWTOY(hello, NULL, TOYFLAG_USR|TOYFLAG_BIN)) +USE_HELLO(NEWTOY(hello, "e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN)) config HELLO bool "hello" @@ -21,6 +21,19 @@ config HELLO #include "toys.h" +// Hello doesn't use these globals, they're here for example/skeleton purposes. + +DEFINE_GLOBALS( + char *b_string; + long c_number; + struct arg_list *d_list; + long e_count; + + int more_globals; +) + +#define TT this.hello + void hello_main(void) { printf("Hello world\n"); diff --git a/www/code.html b/www/code.html index f92fcc77..06c777d9 100644 --- a/www/code.html +++ b/www/code.html @@ -17,25 +17,105 @@ to spot as overrides to the normal flow of control, which they are.

Infrastructure:

-

The toybox source code is in three directories. The top level directory -contains the file main.c and the header file toys.h. The "lib" directory -contains generic functions shared by multiple commands. The "toys" directory -contains the implementations of individual commands.

+

The toybox source code is in following directories:

+ -

Top level directory.

+

Adding a new command

+

To add a new command to toybox, add a C file implementing that command to +the toys directory. No other files need to be modified; the build extracts +other information it needs (such as command line arguments) from specially +formatted comments and macros in the C file. (See the description of the +generated directory for details.)

-

lib: llist, getmountlist(), error_msg/error_exit, xmalloc(), -strlcpy(), xexec(), xopen()/xread(), xgetcwd(), xabspath(), find_in_path(), -itoa().

+

An easy way to start a new command is copy the file "hello.c" to +the name of the new command, and modify this copy to implement the new command. +This file is a small, simple command meant to be used as a "skeleton" for +new commands (more or less by turning every instance of "hello" into the +name of your command, updating the command line arguments, globals, and +help data, and then filling out its "main" function with code that does +something interesting).

+ +

Here's a checklist of steps to turn hello.c into another command:

+ + + +

Top level directory.

+ +

This directory contains global infrastructure.

main.c

Contains the main() function where execution starts, plus common infrastructure to initialize global variables and select which command -to run.

+to run. The "toybox" multiplexer command is also defined here. (This is the +only command defined outside of the toys directory.)

Execution starts in main() which removes the path from the first command name and calls toybox_main(), which calls toy_exec(), which calls toy_find(), -toy_init() and the appropriate command's function from toy_list.

+toy_init() and the appropriate command's function from toy_list. If +the command is "toybox", execution returns to toybox_main(), otherwise +the call goes to the appropriate command_main() from the toys directory.

The following global variables are defined here: