From 66a69d9f5751fb38ebd2ade662b90b68a1cc7c2d Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 16 Jan 2012 01:44:17 -0600 Subject: Fluff out documentation and skeleton code. --- www/code.html | 331 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 298 insertions(+), 33 deletions(-) (limited to 'www/code.html') diff --git a/www/code.html b/www/code.html index 531539de..541b1019 100644 --- a/www/code.html +++ b/www/code.html @@ -18,7 +18,7 @@ to spot as overrides to the normal flow of control, which they are.

The primary goal of toybox is _simple_ code. Small is second, speed and lots of features come in somewhere after that. Note that environmental dependencies are a type of complexity, so needing other packages -to build or run is a downside. For example, don't use curses when you can +to build or run is a big downside. For example, don't use curses when you can output ansi escape sequences instead.

Infrastructure:

@@ -70,18 +70,27 @@ to toybox. Open your new file in your favorite editor.

  • Change the copyright notice to your name, email, and the current year.

  • -
  • Give a URL to the relevant standards document, or say "Not in SUSv3" if +

  • Give a URL to the relevant standards document, or say "Not in SUSv4" if there is no relevant standard. (Currently both lines are there, delete -whichever is appropriate.) The existing link goes to the directory of SUSv3 +whichever is inappropriate.) The existing link goes to the directory of SUSv4 command line utility standards on the Open Group's website, where there's often a relevant commandname.html file. Feel free to link to other documentation or standards as appropriate.

  • -
  • Update the USE_YOURCOMMAND(NEWTOY(yourcommand,"blah",0)) line. The -arguments to newtoy are: 1) the name used to run your command, 2) -the command line arguments (NULL if none), and additional information such -as where your command should be installed on a running system. See [TODO] for -details.

  • +
  • Update the USE_YOURCOMMAND(NEWTOY(yourcommand,"blah",0)) line. +The NEWTOY macro fills out this command's toy_list +structure. The arguments to the NEWTOY macro are:

    + +
      +
    1. the name used to run your command

    2. +
    3. the command line argument option parsing string (NULL if none)

    4. +
    5. a bitfield of TOYFLAG values +(defined in toys.h) providing additional information such as where your +command should be installed on a running system, whether to blank umask +before running, whether or not the command must run as root (and thus should +retain root access if installed SUID), and so on.

    6. +
    +
  • Change the kconfig data (from "config YOURCOMMAND" to the end of the comment block) to supply your command's configuration and help @@ -89,7 +98,16 @@ information. The uppper case config symbols are used by menuconfig, and are also what the CFG_ and USE_() macros are generated from (see [TODO]). The help information here is used by menuconfig, and also by the "help" command to describe your new command. (See [TODO] for details.) By convention, -unfinished commands default to "n" and finished commands default to "y".

  • +unfinished commands default to "n" and finished commands default to "y", +so "make defconfig" selects all finished commands. (Note, "finished" means +"ready to be used", not that it'll never change again.)

    + +

    Each help block should start with a "usage: yourcommand" line explaining +any command line arguments added by this config option. The "help" command +outputs this text, and scripts/config2help.c in the build infrastructure +collates these usage lines for commands with multiple configuration +options when producing generated/help.h.

    +
  • Update the DEFINE_GLOBALS() macro to contain your command's global variables, and also change the name "hello" in the #define TT line afterwards @@ -113,7 +131,28 @@ happened to your command line arguments and how to access them.

  • Top level directory.

    -

    This directory contains global infrastructure. +

    This directory contains global infrastructure.

    + +

    toys.h

    +

    Each command #includes "toys.h" as part of its standard prolog.

    + +

    This file sucks in most of the commonly used standard #includes, so +individual files can just #include "toys.h" and not have to worry about +stdargs.h and so on. Individual commands still need to #include +special-purpose headers that may not be present on all systems (and thus would +prevent toybox from building that command on such a system with that command +enabled). Examples include regex support, any "linux/" or "asm/" headers, mtab +support (mntent.h and sys/mount.h), and so on.

    + +

    The toys.h header also defines structures for most of the global variables +provided to each command by toybox_main(). These are described in +detail in the description for main.c, where they are initialized.

    + +

    The global variables are grouped into structures (and a union) for space +savings, to more easily track the amount of memory consumed by them, +so that they may be automatically cleared/initialized as needed, and so +that access to global variables is more easily distinguished from access to +local variables.

    main.c

    Contains the main() function where execution starts, plus @@ -123,14 +162,16 @@ only command defined outside of the toys directory.)

    Execution starts in main() which trims any path off of the first command name and calls toybox_main(), which calls toy_exec(), which calls toy_find() -and toy_init() before calling the appropriate command's function from toy_list. +and toy_init() before calling the appropriate command's function from +toy_list[] (via toys.which->toy_main()). If the command is "toybox", execution recurses into toybox_main(), otherwise the call goes to the appropriate commandname_main() from a C file in the toys directory.

    The following global variables are defined in main.c:

    -
  • union toy_union this - Union of structures containing each +

  • union toy_union this - Union of structures containing each command's global variables.

    Global variables are useful: they reduce the overhead of passing extra @@ -224,19 +268,20 @@ space for global variables belonging to other commands you aren't currently running would be wasteful.

    Toybox handles this by encapsulating each command's global variables in -a structure, and declaring a union of those structures. The DECLARE_GLOBALS() -macro contains the global variables that should go in a command's global -structure. Each variable can then be accessed as "this.commandname.varname". +a structure, and declaring a union of those structures with a single global +instance (called "this"). The DEFINE_GLOBALS() macro contains the global +variables that should go in the current command's global structure. Each +variable can then be accessed as "this.commandname.varname". Generally, the macro TT is #defined to this.commandname so the variable -can then be accessed as "TT.variable".

    +can then be accessed as "TT.variable". See toys/hello.c for an example.

    -A command that needs global variables should declare a structure to +

    A command that needs global variables should declare a structure to contain them all, and add that structure to this union. A command should never declare global variables outside of this, because such global variables would allocate memory when running other commands that don't use those global variables.

    -

    The first few fields of this structure can be intialized by get_optargs(), +

    The first few fields of this structure can be intialized by get_optargs(), as specified by the options field off this command's toy_list entry. See the get_optargs() description in lib/args.c for details.

  • @@ -290,7 +335,7 @@ which commands (and options to commands) are currently enabled. Used to make generated/config.h and determine which toys/*.c files to build.

    You can create a human readable "miniconfig" version of this file using -these +these instructions.

    @@ -333,12 +378,12 @@ configuration entries for each command.

    Each command has a configuration entry matching the command name (although configuration symbols are uppercase and command names are lower case). Options to commands start with the command name followed by an underscore and -the option name. Global options are attachd to the "toybox" command, +the option name. Global options are attached to the "toybox" command, and thus use the prefix "TOYBOX_". This organization is used by scripts/cfg2files to select which toys/*.c files to compile for a given .config.

    -

    A commands with multiple names (or multiple similar commands implemented in +

    A command with multiple names (or multiple similar commands implemented in the same .c file) should have config symbols prefixed with the name of their C file. I.E. config symbol prefixes are NEWTOY() names. If OLDTOY() names have config symbols they're options (symbols with an underscore and suffix) @@ -388,7 +433,203 @@ in toys/help.c.

    strlcpy(), xexec(), xopen()/xread(), xgetcwd(), xabspath(), find_in_path(), itoa().

    +

    lib/args.c

    + +

    Toybox's main.c automatically parses command line options before calling the +command's main function. Option parsing starts in get_optflags(), which stores +results in the global structures "toys" (optflags and optargs) and "this".

    + +

    The option parsing infrastructure stores a bitfield in toys.optflags to +indicate which options the current command line contained. Arguments +attached to those options are saved into the command's global structure +("this"). Any remaining command line arguments are collected together into +the null-terminated array toys.optargs, with the length in toys.optc. (Note +that toys.optargs does not contain the current command name at position zero, +use "toys.which->name" for that.) The raw command line arguments get_optflags() +parsed are retained unmodified in toys.argv[].

    + +

    Toybox's option parsing logic is controlled by an "optflags" string, using +a format reminiscent of getopt's optargs but has several important differences. +Toybox does not use the getopt() +function out of the C library, get_optflags() is an independent implementation +which doesn't permute the original arguments (and thus doesn't change how the +command is displayed in ps and top), and has many features not present in +libc optargs() (such as the ability to describe long options in the same string +as normal options).

    + +

    Each command's NEWTOY() macro has an optflags string as its middle argument, +which sets toy_list.options for that command to tell get_optflags() what +command line arguments to look for, and what to do with them. +If a command has no option +definition string (I.E. the argument is NULL), option parsing is skipped +for that command, which must look at the raw data in toys.argv to parse its +own arguments. (If no currently enabled command uses option parsing, +get_optflags() is optimized out of the resulting binary by the compiler's +--gc-sections option.)

    + +

    You don't have to free the option strings, which point into the environment +space (I.E. the string data is not copied). A TOYFLAG_NOFORK command +that uses the linked list type "*" should free the list objects but not +the data they point to, via "llist_free(TT.mylist, NULL);". (If it's not +NOFORK, exit() will free all the malloced data anyway unless you want +to implement a CONFIG_TOYBOX_FREE cleanup for it.)

    + +

    Optflags format string

    + +

    Note: the optflags option description string format is much more +concisely described by a large comment at the top of lib/args.c.

    + +

    The general theory is that letters set optflags, and punctuation describes +other actions the option parsing logic should take.

    + +

    For example, suppose the command line command -b fruit -d walrus -a 42 +is parsed using the optflags string "a#b:c:d". (I.E. +toys.which->options="a#b:c:d" and argv = ["command", "-b", "fruit", "-d", +"walrus", "-a", "42"]). When get_optflags() returns, the following data is +available to command_main(): + +

    + +

    If the command's globals are:

    + +
    +DECLARE_GLOBALS(
    +	char *c;
    +	char *b;
    +	long a;
    +)
    +#define TT this.command
    +
    +

    That would mean TT.c == NULL, TT.b == "fruit", and TT.a == 42. (Remember, +each entry that receives an argument must be a long or pointer, to line up +with the array position. Right to left in the optflags string corresponds to +top to bottom in DECLARE_GLOBALS().

    + +

    long toys.optflags

    + +

    Each option in the optflags string corresponds to a bit position in +toys.optflags, with the same value as a corresponding binary digit. The +rightmost argument is (1<<0), the next to last is (1<<1) and so on. If +the option isn't encountered while parsing argv[], its bit remains 0. +(Since toys.optflags is a long, it's only guaranteed to store 32 bits.) +For example, +the optflags string "abcd" would parse the command line argument "-c" to set +optflags to 2, "-a" would set optflags to 8, "-bd" would set optflags to +6 (I.E. 4|2), and "-a -c" would set optflags to 10 (2|8).

    + +

    Only letters are relevant to optflags, punctuation is skipped: in the +string "a*b:c#d", d=1, c=2, b=4, a=8. The punctuation after a letter +usually indicate that the option takes an argument.

    + +

    Automatically setting global variables from arguments (union this)

    + +

    The following punctuation characters may be appended to an optflags +argument letter, indicating the option takes an additional argument:

    + + + +

    Arguments may occur with or without a space (I.E. "-a 42" or "-a42"). +The command line argument "-abc" may be interepreted many different ways: +the optflags string "cba" sets toys.optflags = 7, "c:ba" sets toys.optflags=4 +and saves "ba" as the argument to -c, and "cb:a" sets optflags to 6 and saves +"c" as the argument to -b.

    + +

    Options which have an argument fill in the corresponding slot in the global +union "this" (see generated/globals.h), treating it as an array of longs +with the rightmost saved in this[0]. Again using "a*b:c#d", "-c 42" would set +this[0]=42; and "-b 42" would set this[1]="42"; each slot is left NULL if +the corresponding argument is not encountered.

    + +

    This behavior is useful because the LP64 standard ensures long and pointer +are the same size, and C99 guarantees structure members will occur in memory +in the +same order they're declared, and that padding won't be inserted between +consecutive variables of register size. Thus the first few entries can +be longs or pointers corresponding to the saved arguments.

    + +

    char *toys.optargs[]

    +

    Command line arguments in argv[] which are not consumed by option parsing +(I.E. not recognized either as -flags or arguments to -flags) will be copied +to toys.optargs[], with the length of that array in toys.optc. +(When toys.optc is 0, no unrecognized command line arguments remain.) +The order of entries is preserved, and as with argv[] this new array is also +terminated by a NULL entry.

    + +

    Option parsing can require a minimum or maximum number of optargs left +over, by adding "<1" (read "at least one") or ">9" ("at most nine") to the +start of the optflags string.

    + +

    The special argument "--" terminates option parsing, storing all remaining +arguments in optargs. The "--" itself is consumed.

    + +

    Other optflags control characters

    + +

    The following characters may occur at the start of each command's +optflags string, before any options that would set a bit in toys.optflags:

    + + + +

    The following characters may be appended to an option character, but do +not by themselves indicate an extra argument should be saved in this[]. +(Technically any character not recognized as a control character sets an +optflag, but letters are never control characters.)

    + + + +

    --longopts

    + +

    The optflags string can contain long options, which are enclosed in +parentheses. They may be appended to an existing option character, in +which case the --longopt is a synonym for that option, ala "a:(--fred)" +which understands "-a blah" or "--fred blah" as synonyms.

    + +

    Longopts may also appear before any other options in the optflags string, +in which case they have no corresponding short argument, but instead set +their own bit based on position. So for "(walrus)#(blah)xy:z" "command +--walrus 42" would set toys.optflags = 16 (-z = 1, -y = 2, -x = 4, --blah = 8) +and would assign this[1] = 42;

    + +

    A short option may have multiple longopt synonyms, "a(one)(two)", but +each "bare longopt" (ala "(one)(two)abc" before any option characters) +always sets its own bit (although you can group them with +X).

    Directory scripts/

    @@ -404,4 +645,28 @@ Makefile.

    Menuconfig infrastructure copied from the Linux kernel. See the Linux kernel's Documentation/kbuild/kconfig-language.txt

    +
    +

    Directory generated/

    + +

    All the files in this directory except the README are generated by the +build. (See scripts/make.sh)

    + +
    + +

    Everything in this directory is a derivative file produced from something +else. The entire directory is deleted by "make distclean".

    -- cgit v1.2.3