blob: ad2a7a8b9960ddb9f17637615a923324ee60a823 [file] [log] [blame]
Rob Landley4e68de12007-12-13 07:00:27 -06001<!--#include file="header.html" -->
2
Rob Landleye7c9a6d2012-02-28 06:34:09 -06003<h1>Simplicity first: spending your complexity budget wisely.</h1>
4
5<p>The primary goal of toybox is _simple_ code. Keeping the code small is
6second, with speed and lots of features coming in somewhere after that.</p>
7
8<p>These goals are usually complementary: simplifying code generally reduces
9its size (both in terms of binary size and runtime memory usage), and avoiding
10unnecessary work makes code run faster. Smaller code also tends to run faster
11on modern hardware due to CPU cacheing: fitting your code into L1 cache is
12great, and staying in L2 cache is still pretty good.</p>
13
14<p>A simple implementation usually takes up fewer lines of source code,
15meaning more code can fit on the screen at once, meaning the programmer can
16see more of it on the screen and thus keep more if in their head at once.
17This helps code auditing and thus reduces bugs.</p>
18
19<p>Ken Thompson's maximum "when in doubt, use brute force" is an admonishment
20to start with the simplest possible approach and only optimize as needed.
21Although implementing a given set of features is the eventual purpose of
22toybox, we choose to weight simplicity more heavily than anything else.
23Complexity is what we spend to get features (and occasionally smaller size
24or faster running time than the simplest possible implementation). Sometimes
25a feature, speedup, or code shrink isn't worth the complexity cost. We want to
26get "the best bang for the byte" we can, but sometimes being more explicit
27is preferable to being clever enough to outsmart yourself. (Even the best
28programmers are only human.)</p>
29
30<p>Environmental dependencies are a type of complexity, so needing other
31packages to build or run is a big downside. For example, we don't use curses
32when we can simply output ansi escape sequences and trust all terminal
33programs written in the past 30 years to be able to support them. (A common
34use case is to download a statically linked toybox binary to an arbitrary
35Linux system, and use it in an otherwise unknown environment. It _must_ be
36completely self-contained to support this.)</p>
37
Rob Landley5a0660f2007-12-27 21:36:44 -060038<p><h1>Code style</h1></p>
39
40<p>Toybox source is formatted to be read with 4-space tab stops. Each file
41starts with a special comment telling vi to set the tab stop to 4. Note that
42one of the bugs in Ubuntu 7.10 broke vi's ability to parse these comments; you
43must either rebuild vim from source, or go ":ts=4" yourself each time you load
44the file.</p>
45
46<p>Gotos are allowed for error handling, and for breaking out of
47nested loops. In general, a goto should only jump forward (not back), and
48should either jump to the end of an outer loop, or to error handling code
49at the end of the function. Goto labels are never indented: they override the
50block structure of the file. Putting them at the left edge makes them easy
51to spot as overrides to the normal flow of control, which they are.</p>
52
Rob Landleye7c9a6d2012-02-28 06:34:09 -060053<p><h1>Building Toybox:</h1></p>
54
55<p>Toybox is configured using the Kconfig language pioneered by the Linux
56kernel, and adopted by many other projects (uClibc, OpenEmbedded, etc).
57This generates a ".config" file containing the selected options, which
58controls which features to enable when building toybox.</p>
59
60<p>Each configuration option has a default value. The defaults indicate the
61"maximum sane configuration", I.E. if the feature defaults to "n" then it
62either isn't complete or is a special-purpose option (such as debugging
63code) that isn't intended for general purpose use.</p>
64
65<p>The standard build invocation is:</p>
66
67<ul>
68<li>make defconfig #(or menuconfig)</li>
69<li>make</li>
70<li>make install</li>
71</ul>
72
73<p>Type "make help" to see all available build options.</p>
74
75<p>The file "configure" contains a number of environment variable definitions
76which influence the build, such as specifying which compiler to use or where
77to install the resulting binaries. This file is included by the build, but
78accepts existing definitions of the environment variables, so it may be sourced
79or modified by the developer before building and the definitions exported
80to the environment will take precedence.</p>
81
82<p>(To clarify: "configure" describes the build and installation environment,
83".config" lists the features selected by defconfig/menuconfig.)</p>
Rob Landley6882ee82008-02-12 18:41:34 -060084
Rob Landley4e68de12007-12-13 07:00:27 -060085<p><h1>Infrastructure:</h1></p>
86
Rob Landley7c04f012008-01-20 19:00:16 -060087<p>The toybox source code is in following directories:</p>
88<ul>
89<li>The <a href="#top">top level directory</a> contains the file main.c (were
90execution starts), the header file toys.h (included by every command), and
91other global infrastructure.</li>
92<li>The <a href="#lib">lib directory</a> contains common functions shared by
93multiple commands.</li>
94<li>The <a href="#toys">toys directory</a> contains the C files implementating
95each command.</li>
96<li>The <a href="#scripts">scripts directory</a> contains the build and
97test infrastructure.</li>
98<li>The <a href="#kconfig">kconfig directory</a> contains the configuration
99infrastructure implementing menuconfig (copied from the Linux kernel).</li>
100<li>The <a href="#generated">generated directory</a> contains intermediate
101files generated from other parts of the source code.</li>
102</ul>
Rob Landley4e68de12007-12-13 07:00:27 -0600103
Rob Landleybbe500e2012-02-26 21:53:15 -0600104<a name="adding" />
Rob Landley7c04f012008-01-20 19:00:16 -0600105<p><h1>Adding a new command</h1></p>
106<p>To add a new command to toybox, add a C file implementing that command to
107the toys directory. No other files need to be modified; the build extracts
Rob Landley6882ee82008-02-12 18:41:34 -0600108all the information it needs (such as command line arguments) from specially
Rob Landley7c04f012008-01-20 19:00:16 -0600109formatted comments and macros in the C file. (See the description of the
Rob Landleye7c9a6d2012-02-28 06:34:09 -0600110<a href="#generated">"generated" directory</a> for details.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600111
Rob Landley7c04f012008-01-20 19:00:16 -0600112<p>An easy way to start a new command is copy the file "hello.c" to
113the name of the new command, and modify this copy to implement the new command.
Rob Landley6882ee82008-02-12 18:41:34 -0600114This file is an example command meant to be used as a "skeleton" for
Rob Landley7c04f012008-01-20 19:00:16 -0600115new commands (more or less by turning every instance of "hello" into the
116name of your command, updating the command line arguments, globals, and
117help data, and then filling out its "main" function with code that does
Rob Landley6882ee82008-02-12 18:41:34 -0600118something interesting). It provides examples of all the build infrastructure
119(including optional elements like command line argument parsing and global
120variables that a "hello world" program doesn't strictly need).</p>
Rob Landley7c04f012008-01-20 19:00:16 -0600121
122<p>Here's a checklist of steps to turn hello.c into another command:</p>
123
124<ul>
125<li><p>First "cd toys" and "cp hello.c yourcommand.c". Note that the name
126of this file is significant, it's the name of the new command you're adding
127to toybox. Open your new file in your favorite editor.</p></li>
128
129<li><p>Change the one line comment at the top of the file (currently
130"hello.c - A hello world program") to describe your new file.</p></li>
131
132<li><p>Change the copyright notice to your name, email, and the current
133year.</p></li>
134
Rob Landley66a69d92012-01-16 01:44:17 -0600135<li><p>Give a URL to the relevant standards document, or say "Not in SUSv4" if
Rob Landley7c04f012008-01-20 19:00:16 -0600136there is no relevant standard. (Currently both lines are there, delete
Rob Landley66a69d92012-01-16 01:44:17 -0600137whichever is inappropriate.) The existing link goes to the directory of SUSv4
Rob Landley7c04f012008-01-20 19:00:16 -0600138command line utility standards on the Open Group's website, where there's often
139a relevant commandname.html file. Feel free to link to other documentation or
140standards as appropriate.</p></li>
141
Rob Landley66a69d92012-01-16 01:44:17 -0600142<li><p>Update the USE_YOURCOMMAND(NEWTOY(yourcommand,"blah",0)) line.
143The NEWTOY macro fills out this command's <a href="#toy_list">toy_list</a>
144structure. The arguments to the NEWTOY macro are:</p>
145
146<ol>
147<li><p>the name used to run your command</p></li>
148<li><p>the command line argument <a href="#lib_args">option parsing string</a> (NULL if none)</p></li>
149<li><p>a bitfield of TOYFLAG values
150(defined in toys.h) providing additional information such as where your
151command should be installed on a running system, whether to blank umask
152before running, whether or not the command must run as root (and thus should
153retain root access if installed SUID), and so on.</p></li>
154</ol>
155</li>
Rob Landley7c04f012008-01-20 19:00:16 -0600156
157<li><p>Change the kconfig data (from "config YOURCOMMAND" to the end of the
158comment block) to supply your command's configuration and help
159information. The uppper case config symbols are used by menuconfig, and are
160also what the CFG_ and USE_() macros are generated from (see [TODO]). The
161help information here is used by menuconfig, and also by the "help" command to
162describe your new command. (See [TODO] for details.) By convention,
Rob Landley66a69d92012-01-16 01:44:17 -0600163unfinished commands default to "n" and finished commands default to "y",
164so "make defconfig" selects all finished commands. (Note, "finished" means
165"ready to be used", not that it'll never change again.)<p>
166
167<p>Each help block should start with a "usage: yourcommand" line explaining
168any command line arguments added by this config option. The "help" command
169outputs this text, and scripts/config2help.c in the build infrastructure
170collates these usage lines for commands with multiple configuration
171options when producing generated/help.h.</p>
172</li>
Rob Landley7c04f012008-01-20 19:00:16 -0600173
174<li><p>Update the DEFINE_GLOBALS() macro to contain your command's global
175variables, and also change the name "hello" in the #define TT line afterwards
176to the name of your command. If your command has no global variables, delete
177this macro (and the #define TT line afterwards). Note that if you specified
178two-character command line arguments in NEWTOY(), the first few global
179variables will be initialized by the automatic argument parsing logic, and
180the type and order of these variables must correspond to the arguments
181specified in NEWTOY(). See [TODO] for details.</p></li>
182
Rob Landley6882ee82008-02-12 18:41:34 -0600183<li><p>If you didn't delete the DEFINE_GLOBALS macro, change the "#define TT
184this.hello" line to use your command name in place of the "hello". This is a
185shortcut to access your global variables as if they were members of the global
186struct "TT". (Access these members with a period ".", not a right arrow
187"->".)</p></li>
Rob Landley7c04f012008-01-20 19:00:16 -0600188
189<li><p>Rename hello_main() to yourcommand_main(). This is the main() function
Rob Landley6882ee82008-02-12 18:41:34 -0600190where execution of your command starts. See [TODO] to figure out what
Rob Landley7c04f012008-01-20 19:00:16 -0600191happened to your command line arguments and how to access them.</p></li>
192</ul>
193
194<p><a name="top" /><h2>Top level directory.</h2></p>
195
Rob Landley66a69d92012-01-16 01:44:17 -0600196<p>This directory contains global infrastructure.</p>
197
198<h3>toys.h</h3>
199<p>Each command #includes "toys.h" as part of its standard prolog.</p>
200
201<p>This file sucks in most of the commonly used standard #includes, so
202individual files can just #include "toys.h" and not have to worry about
203stdargs.h and so on. Individual commands still need to #include
204special-purpose headers that may not be present on all systems (and thus would
205prevent toybox from building that command on such a system with that command
206enabled). Examples include regex support, any "linux/" or "asm/" headers, mtab
207support (mntent.h and sys/mount.h), and so on.</p>
208
209<p>The toys.h header also defines structures for most of the global variables
210provided to each command by toybox_main(). These are described in
211detail in the description for main.c, where they are initialized.</p>
212
213<p>The global variables are grouped into structures (and a union) for space
214savings, to more easily track the amount of memory consumed by them,
215so that they may be automatically cleared/initialized as needed, and so
216that access to global variables is more easily distinguished from access to
217local variables.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600218
219<h3>main.c</h3>
220<p>Contains the main() function where execution starts, plus
221common infrastructure to initialize global variables and select which command
Rob Landley6882ee82008-02-12 18:41:34 -0600222to run. The "toybox" multiplexer command also lives here. (This is the
Rob Landley7c04f012008-01-20 19:00:16 -0600223only command defined outside of the toys directory.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600224
Rob Landley6882ee82008-02-12 18:41:34 -0600225<p>Execution starts in main() which trims any path off of the first command
226name and calls toybox_main(), which calls toy_exec(), which calls toy_find()
Rob Landley66a69d92012-01-16 01:44:17 -0600227and toy_init() before calling the appropriate command's function from
228toy_list[] (via toys.which->toy_main()).
Rob Landley6882ee82008-02-12 18:41:34 -0600229If the command is "toybox", execution recurses into toybox_main(), otherwise
230the call goes to the appropriate commandname_main() from a C file in the toys
231directory.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600232
Rob Landley6882ee82008-02-12 18:41:34 -0600233<p>The following global variables are defined in main.c:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600234<ul>
Rob Landley66a69d92012-01-16 01:44:17 -0600235<a name="toy_list" />
236<li><p><b>struct toy_list toy_list[]</b> - array describing all the
Rob Landley4e68de12007-12-13 07:00:27 -0600237commands currently configured into toybox. The first entry (toy_list[0]) is
238for the "toybox" multiplexer command, which runs all the other built-in commands
239without symlinks by using its first argument as the name of the command to
240run and the rest as that command's argument list (ala "./toybox echo hello").
241The remaining entries are the commands in alphabetical order (for efficient
242binary search).</p>
243
244<p>This is a read-only array initialized at compile time by
Rob Landley6882ee82008-02-12 18:41:34 -0600245defining macros and #including generated/newtoys.h.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600246
Rob Landley66a69d92012-01-16 01:44:17 -0600247<p>Members of struct toy_list (defined in "toys.h") include:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600248<ul>
249<li><p>char *<b>name</b> - the name of this command.</p></li>
250<li><p>void (*<b>toy_main</b>)(void) - function pointer to run this
251command.</p></li>
252<li><p>char *<b>options</b> - command line option string (used by
253get_optflags() in lib/args.c to intialize toys.optflags, toys.optargs, and
Rob Landley66a69d92012-01-16 01:44:17 -0600254entries in the toy's DEFINE_GLOBALS struct). When this is NULL, no option
255parsing is done before calling toy_main().</p></li>
Rob Landley6882ee82008-02-12 18:41:34 -0600256<li><p>int <b>flags</b> - Behavior flags for this command. The following flags are currently understood:</p>
257
258<ul>
259<li><b>TOYFLAG_USR</b> - Install this command under /usr</li>
260<li><b>TOYFLAG_BIN</b> - Install this command under /bin</li>
261<li><b>TOYFLAG_SBIN</b> - Install this command under /sbin</li>
262<li><b>TOYFLAG_NOFORK</b> - This command can be used as a shell builtin.</li>
263<li><b>TOYFLAG_UMASK</b> - Call umask(0) before running this command.</li>
Rob Landley66a69d92012-01-16 01:44:17 -0600264<li><b>TOYFLAG_STAYROOT</b> - Don't drop permissions for this command if toybox is installed SUID root.</li>
265<li><b>TOYFLAG_NEEDROOT</b> - This command cannot function unless run with root access.</li>
Rob Landley6882ee82008-02-12 18:41:34 -0600266</ul>
267<br>
268
269<p>These flags are combined with | (or). For example, to install a command
270in /usr/bin, or together TOYFLAG_USR|TOYFLAG_BIN.</p>
271</ul>
Rob Landley4e68de12007-12-13 07:00:27 -0600272</li>
273
Rob Landley66a69d92012-01-16 01:44:17 -0600274<li><p><b>struct toy_context toys</b> - global structure containing information
275common to all commands, initializd by toy_init() and defined in "toys.h".
276Members of this structure include:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600277<ul>
278<li><p>struct toy_list *<b>which</b> - a pointer to this command's toy_list
279structure. Mostly used to grab the name of the running command
280(toys->which.name).</p>
281</li>
282<li><p>int <b>exitval</b> - Exit value of this command. Defaults to zero. The
283error_exit() functions will return 1 if this is zero, otherwise they'll
284return this value.</p></li>
285<li><p>char **<b>argv</b> - "raw" command line options, I.E. the original
286unmodified string array passed in to main(). Note that modifying this changes
Rob Landley66a69d92012-01-16 01:44:17 -0600287"ps" output, and is not recommended. This array is null terminated; a NULL
288entry indicates the end of the array.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600289<p>Most commands don't use this field, instead the use optargs, optflags,
Rob Landley66a69d92012-01-16 01:44:17 -0600290and the fields in the DEFINE_GLOBALS struct initialized by get_optflags().</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600291</li>
292<li><p>unsigned <b>optflags</b> - Command line option flags, set by
Rob Landley66a69d92012-01-16 01:44:17 -0600293<a href="#lib_args">get_optflags()</a>. Indicates which of the command line options listed in
Rob Landley6882ee82008-02-12 18:41:34 -0600294toys->which.options occurred this time.</p>
295
296<p>The rightmost command line argument listed in toys->which.options sets bit
2971, the next one sets bit 2, and so on. This means the bits are set in the same
298order the binary digits would be listed if typed out as a string. For example,
299the option string "abcd" would parse the command line "-c" to set optflags to 2,
300"-a" would set optflags to 8, and "-bd" would set optflags to 6 (4|2).</p>
301
302<p>Only letters are relevant to optflags. In the string "a*b:c#d", d=1, c=2,
303b=4, a=8. The punctuation after a letter initializes global variables
304(see [TODO] DECLARE_GLOBALS() for details).</p>
305
Rob Landley66a69d92012-01-16 01:44:17 -0600306<p>For more information on option parsing, see <a href="#lib_args">get_optflags()</a>.</p>
Rob Landley6882ee82008-02-12 18:41:34 -0600307
308</li>
Rob Landley4e68de12007-12-13 07:00:27 -0600309<li><p>char **<b>optargs</b> - Null terminated array of arguments left over
310after get_optflags() removed all the ones it understood. Note: optarg[0] is
311the first argument, not the command name. Use toys.which->name for the command
312name.</p></li>
Rob Landley6882ee82008-02-12 18:41:34 -0600313<li><p>int <b>optc</b> - Optarg count, equivalent to argc but for
314optargs[].<p></li>
Rob Landley4e68de12007-12-13 07:00:27 -0600315<li><p>int <b>exithelp</b> - Whether error_exit() should print a usage message
316via help_main() before exiting. (True during option parsing, defaults to
317false afterwards.)</p></li>
Rob Landley66a69d92012-01-16 01:44:17 -0600318</ul>
Rob Landley4e68de12007-12-13 07:00:27 -0600319
Rob Landley66a69d92012-01-16 01:44:17 -0600320<li><p><b>union toy_union this</b> - Union of structures containing each
Rob Landley4e68de12007-12-13 07:00:27 -0600321command's global variables.</p>
322
Rob Landley6882ee82008-02-12 18:41:34 -0600323<p>Global variables are useful: they reduce the overhead of passing extra
324command line arguments between functions, they conveniently start prezeroed to
325save initialization costs, and the command line argument parsing infrastructure
326can also initialize global variables with its results.</p>
327
328<p>But since each toybox process can only run one command at a time, allocating
329space for global variables belonging to other commands you aren't currently
330running would be wasteful.</p>
331
332<p>Toybox handles this by encapsulating each command's global variables in
Rob Landley66a69d92012-01-16 01:44:17 -0600333a structure, and declaring a union of those structures with a single global
334instance (called "this"). The DEFINE_GLOBALS() macro contains the global
335variables that should go in the current command's global structure. Each
336variable can then be accessed as "this.commandname.varname".
Rob Landley6882ee82008-02-12 18:41:34 -0600337Generally, the macro TT is #defined to this.commandname so the variable
Rob Landley66a69d92012-01-16 01:44:17 -0600338can then be accessed as "TT.variable". See toys/hello.c for an example.</p>
Rob Landley6882ee82008-02-12 18:41:34 -0600339
Rob Landley66a69d92012-01-16 01:44:17 -0600340<p>A command that needs global variables should declare a structure to
Rob Landley4e68de12007-12-13 07:00:27 -0600341contain them all, and add that structure to this union. A command should never
342declare global variables outside of this, because such global variables would
343allocate memory when running other commands that don't use those global
344variables.</p>
345
Rob Landley66a69d92012-01-16 01:44:17 -0600346<p>The first few fields of this structure can be intialized by <a href="#lib_args">get_optargs()</a>,
Rob Landley4e68de12007-12-13 07:00:27 -0600347as specified by the options field off this command's toy_list entry. See
348the get_optargs() description in lib/args.c for details.</p>
349</li>
350
Rob Landley81b899d2007-12-18 02:02:47 -0600351<li><b>char toybuf[4096]</b> - a common scratch space buffer so
Rob Landley4e68de12007-12-13 07:00:27 -0600352commands don't need to allocate their own. Any command is free to use this,
353and it should never be directly referenced by functions in lib/ (although
354commands are free to pass toybuf in to a library function as an argument).</li>
355</ul>
356
Rob Landley6882ee82008-02-12 18:41:34 -0600357<p>The following functions are defined in main.c:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600358<ul>
359<li><p>struct toy_list *<b>toy_find</b>(char *name) - Return the toy_list
360structure for this command name, or NULL if not found.</p></li>
Rob Landley81b899d2007-12-18 02:02:47 -0600361<li><p>void <b>toy_init</b>(struct toy_list *which, char *argv[]) - fill out
362the global toys structure, calling get_optargs() if necessary.</p></li>
Rob Landley6882ee82008-02-12 18:41:34 -0600363<li><p>void <b>toy_exec</b>(char *argv[]) - Run a built-in command with
364arguments.</p>
365<p>Calls toy_find() on argv[0] (which must be just a command name
Rob Landley4e68de12007-12-13 07:00:27 -0600366without path). Returns if it can't find this command, otherwise calls
Rob Landley6882ee82008-02-12 18:41:34 -0600367toy_init(), toys->which.toy_main(), and exit() instead of returning.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600368
Rob Landley6882ee82008-02-12 18:41:34 -0600369<p>Use the library function xexec() to fall back to external executables
370in $PATH if toy_exec() can't find a built-in command. Note that toy_exec()
371does not strip paths before searching for a command, so "./command" will
372never match an internal command.</li>
373
374<li><p>void <b>toybox_main</b>(void) - the main function for the multiplexer
375command (I.E. "toybox"). Given a command name as its first argument, calls
376toy_exec() on its arguments. With no arguments, it lists available commands.
377If the first argument starts with "-" it lists each command with its default
378install path prepended.</p></li>
Rob Landley4e68de12007-12-13 07:00:27 -0600379
380</ul>
381
382<h3>Config.in</h3>
383
384<p>Top level configuration file in a stylized variant of
Rob Landley6882ee82008-02-12 18:41:34 -0600385<a href=http://kernel.org/doc/Documentation/kbuild/kconfig-language.txt>kconfig</a> format. Includes generated/Config.in.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600386
387<p>These files are directly used by "make menuconfig" to select which commands
388to build into toybox (thus generating a .config file), and by
Rob Landley6882ee82008-02-12 18:41:34 -0600389scripts/config2help.py to create generated/help.h.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600390
391<h3>Temporary files:</h3>
392
Rob Landley6882ee82008-02-12 18:41:34 -0600393<p>There is one temporary file in the top level source directory:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600394<ul>
395<li><p><b>.config</b> - Configuration file generated by kconfig, indicating
396which commands (and options to commands) are currently enabled. Used
Rob Landley6882ee82008-02-12 18:41:34 -0600397to make generated/config.h and determine which toys/*.c files to build.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600398
Rob Landley6882ee82008-02-12 18:41:34 -0600399<p>You can create a human readable "miniconfig" version of this file using
Rob Landley66a69d92012-01-16 01:44:17 -0600400<a href=http://landley.net/aboriginal/new_platform.html#miniconfig>these
Rob Landley6882ee82008-02-12 18:41:34 -0600401instructions</a>.</p>
402</li>
403</ul>
404
Rob Landleye7c9a6d2012-02-28 06:34:09 -0600405<a name="generated" />
Rob Landley6882ee82008-02-12 18:41:34 -0600406<p>The "generated/" directory contains files generated from other source code
407in toybox. All of these files can be recreated by the build system, although
408some (such as generated/help.h) are shipped in release versions to reduce
409environmental dependencies (I.E. so you don't need python on your build
410system).</p>
411
412<ul>
413<li><p><b>generated/config.h</b> - list of CFG_SYMBOL and USE_SYMBOL() macros,
Rob Landley4e68de12007-12-13 07:00:27 -0600414generated from .config by a sed invocation in the top level Makefile.</p>
415
416<p>CFG_SYMBOL is a comple time constant set to 1 for enabled symbols and 0 for
Rob Landley6882ee82008-02-12 18:41:34 -0600417disabled symbols. This allows the use of normal if() statements to remove
418code at compile time via the optimizer's dead code elimination (which removes
419from the binary any code that cannot be reached). This saves space without
Rob Landley4e68de12007-12-13 07:00:27 -0600420cluttering the code with #ifdefs or leading to configuration dependent build
421breaks. (See the 1992 Usenix paper
Rob Landleyb6063de2012-01-29 13:54:13 -0600422<a href=http://doc.cat-v.org/henry_spencer/ifdef_considered_harmful.pdf>#ifdef
Rob Landley4e68de12007-12-13 07:00:27 -0600423Considered Harmful</a> for more information.)</p>
424
425<p>USE_SYMBOL(code) evaluates to the code in parentheses when the symbol
426is enabled, and nothing when the symbol is disabled. This can be used
427for things like varargs or variable declarations which can't always be
Rob Landley6882ee82008-02-12 18:41:34 -0600428eliminated by a simple test on CFG_SYMBOL. Note that
Rob Landley4e68de12007-12-13 07:00:27 -0600429(unlike CFG_SYMBOL) this is really just a variant of #ifdef, and can
430still result in configuration dependent build breaks. Use with caution.</p>
431</li>
432</ul>
433
Rob Landley81b899d2007-12-18 02:02:47 -0600434<p><h2>Directory toys/</h2></p>
Rob Landley4e68de12007-12-13 07:00:27 -0600435
436<h3>toys/Config.in</h3>
437
438<p>Included from the top level Config.in, contains one or more
439configuration entries for each command.</p>
440
Rob Landley81b899d2007-12-18 02:02:47 -0600441<p>Each command has a configuration entry matching the command name (although
442configuration symbols are uppercase and command names are lower case).
Rob Landley4e68de12007-12-13 07:00:27 -0600443Options to commands start with the command name followed by an underscore and
Rob Landley66a69d92012-01-16 01:44:17 -0600444the option name. Global options are attached to the "toybox" command,
Rob Landley4e68de12007-12-13 07:00:27 -0600445and thus use the prefix "TOYBOX_". This organization is used by
Rob Landley81b899d2007-12-18 02:02:47 -0600446scripts/cfg2files to select which toys/*.c files to compile for a given
447.config.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600448
Rob Landley66a69d92012-01-16 01:44:17 -0600449<p>A command with multiple names (or multiple similar commands implemented in
Rob Landley4e68de12007-12-13 07:00:27 -0600450the same .c file) should have config symbols prefixed with the name of their
451C file. I.E. config symbol prefixes are NEWTOY() names. If OLDTOY() names
452have config symbols they're options (symbols with an underscore and suffix)
453to the NEWTOY() name. (See toys/toylist.h)</p>
454
455<h3>toys/toylist.h</h3>
Rob Landley81b899d2007-12-18 02:02:47 -0600456<p>The first half of this file prototypes all the structures to hold
Rob Landleyda09b7f2007-12-20 06:29:59 -0600457global variables for each command, and puts them in toy_union. These
458prototypes are only included if the macro NEWTOY isn't defined (in which
459case NEWTOY is defined to a default value that produces function
460prototypes).</p>
Rob Landley81b899d2007-12-18 02:02:47 -0600461
Rob Landleyda09b7f2007-12-20 06:29:59 -0600462<p>The second half of this file lists all the commands in alphabetical
463order, along with their command line arguments and install location.
464Each command has an appropriate configuration guard so only the commands that
465are enabled wind up in the list.</p>
466
467<p>The first time this header is #included, it defines structures and
468produces function prototypes for the commands in the toys directory.</p>
469
470
471<p>The first time it's included, it defines structures and produces function
472prototypes.
473 This
Rob Landley81b899d2007-12-18 02:02:47 -0600474is used to initialize toy_list in main.c, and later in that file to initialize
475NEED_OPTIONS (to figure out whether the command like parsing logic is needed),
476and to put the help entries in the right order in toys/help.c.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600477
478<h3>toys/help.h</h3>
479
480<p>#defines two help text strings for each command: a single line
481command_help and an additinal command_help_long. This is used by help_main()
482in toys/help.c to display help for commands.</p>
483
484<p>Although this file is generated from Config.in help entries by
485scripts/config2help.py, it's shipped in release tarballs so you don't need
486python on the build system. (If you check code out of source control, or
487modify Config.in, then you'll need python installed to rebuild it.)</p>
488
489<p>This file contains help for all commands, regardless of current
490configuration, but only the currently enabled ones are entered into help_data[]
491in toys/help.c.</p>
492
Rob Landley81b899d2007-12-18 02:02:47 -0600493<h2>Directory lib/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600494
Rob Landley7c04f012008-01-20 19:00:16 -0600495<p>lib: llist, getmountlist(), error_msg/error_exit, xmalloc(),
496strlcpy(), xexec(), xopen()/xread(), xgetcwd(), xabspath(), find_in_path(),
497itoa().</p>
498
Rob Landley66a69d92012-01-16 01:44:17 -0600499<a name="lib_args"><h3>lib/args.c</h3>
Rob Landley7c04f012008-01-20 19:00:16 -0600500
Rob Landley66a69d92012-01-16 01:44:17 -0600501<p>Toybox's main.c automatically parses command line options before calling the
502command's main function. Option parsing starts in get_optflags(), which stores
503results in the global structures "toys" (optflags and optargs) and "this".</p>
504
505<p>The option parsing infrastructure stores a bitfield in toys.optflags to
506indicate which options the current command line contained. Arguments
507attached to those options are saved into the command's global structure
508("this"). Any remaining command line arguments are collected together into
509the null-terminated array toys.optargs, with the length in toys.optc. (Note
510that toys.optargs does not contain the current command name at position zero,
511use "toys.which->name" for that.) The raw command line arguments get_optflags()
512parsed are retained unmodified in toys.argv[].</p>
513
514<p>Toybox's option parsing logic is controlled by an "optflags" string, using
515a format reminiscent of getopt's optargs but has several important differences.
516Toybox does not use the getopt()
517function out of the C library, get_optflags() is an independent implementation
518which doesn't permute the original arguments (and thus doesn't change how the
519command is displayed in ps and top), and has many features not present in
520libc optargs() (such as the ability to describe long options in the same string
521as normal options).</p>
522
523<p>Each command's NEWTOY() macro has an optflags string as its middle argument,
524which sets toy_list.options for that command to tell get_optflags() what
525command line arguments to look for, and what to do with them.
526If a command has no option
527definition string (I.E. the argument is NULL), option parsing is skipped
528for that command, which must look at the raw data in toys.argv to parse its
529own arguments. (If no currently enabled command uses option parsing,
530get_optflags() is optimized out of the resulting binary by the compiler's
531--gc-sections option.)</p>
532
533<p>You don't have to free the option strings, which point into the environment
534space (I.E. the string data is not copied). A TOYFLAG_NOFORK command
535that uses the linked list type "*" should free the list objects but not
536the data they point to, via "llist_free(TT.mylist, NULL);". (If it's not
537NOFORK, exit() will free all the malloced data anyway unless you want
538to implement a CONFIG_TOYBOX_FREE cleanup for it.)</p>
539
540<h4>Optflags format string</h4>
541
542<p>Note: the optflags option description string format is much more
543concisely described by a large comment at the top of lib/args.c.</p>
544
545<p>The general theory is that letters set optflags, and punctuation describes
546other actions the option parsing logic should take.</p>
547
548<p>For example, suppose the command line <b>command -b fruit -d walrus -a 42</b>
549is parsed using the optflags string "<b>a#b:c:d</b>". (I.E.
550toys.which->options="a#b:c:d" and argv = ["command", "-b", "fruit", "-d",
551"walrus", "-a", "42"]). When get_optflags() returns, the following data is
552available to command_main():
553
554<ul>
555<li><p>In <b>struct toys</b>:
556<ul>
557<li>toys.optflags = 13; // -a = 8 | -b = 4 | -d = 1</li>
558<li>toys.optargs[0] = "walrus"; // leftover argument</li>
559<li>toys.optargs[1] = NULL; // end of list</li>
560<li>toys.optc=1; // there was 1 leftover argument</li>
561<li>toys.argv[] = {"-b", "fruit", "-d", "walrus", "-a", "42"}; // The original command line arguments
562</ul>
563<p></li>
564
565<li><p>In <b>union this</b> (treated as <b>long this[]</b>):
566<ul>
567<li>this[0] = NULL; // -c didn't get an argument this time, so get_optflags() didn't change it and toys_init() zeroed "this" during setup.)</li>
568<li>this[1] = (long)"fruit"; // argument to -b</li>
569<li>this[2] = 42; // argument to -a</li>
570</ul>
571</p></li>
572</ul>
573
574<p>If the command's globals are:</p>
575
576<blockquote><pre>
577DECLARE_GLOBALS(
578 char *c;
579 char *b;
580 long a;
581)
582#define TT this.command
583</pre></blockquote>
584<p>That would mean TT.c == NULL, TT.b == "fruit", and TT.a == 42. (Remember,
585each entry that receives an argument must be a long or pointer, to line up
586with the array position. Right to left in the optflags string corresponds to
587top to bottom in DECLARE_GLOBALS().</p>
588
589<p><b>long toys.optflags</b></p>
590
591<p>Each option in the optflags string corresponds to a bit position in
592toys.optflags, with the same value as a corresponding binary digit. The
593rightmost argument is (1<<0), the next to last is (1<<1) and so on. If
Rob Landleyb4a0efa2012-02-06 21:15:19 -0600594the option isn't encountered while parsing argv[], its bit remains 0.</p>
595
596<p>For example,
Rob Landley66a69d92012-01-16 01:44:17 -0600597the optflags string "abcd" would parse the command line argument "-c" to set
598optflags to 2, "-a" would set optflags to 8, "-bd" would set optflags to
5996 (I.E. 4|2), and "-a -c" would set optflags to 10 (2|8).</p>
600
601<p>Only letters are relevant to optflags, punctuation is skipped: in the
602string "a*b:c#d", d=1, c=2, b=4, a=8. The punctuation after a letter
603usually indicate that the option takes an argument.</p>
604
Rob Landleyb4a0efa2012-02-06 21:15:19 -0600605<p>Since toys.optflags is an unsigned int, it only stores 32 bits. (Which is
606the amount a long would have on 32-bit platforms anyway; 64 bit code on
60732 bit platforms is too expensive to require in common code used by almost
608all commands.) Bit positions beyond the 1<<31 aren't recorded, but
609parsing higher options can still set global variables.</p>
610
Rob Landley66a69d92012-01-16 01:44:17 -0600611<p><b>Automatically setting global variables from arguments (union this)</b></p>
612
613<p>The following punctuation characters may be appended to an optflags
614argument letter, indicating the option takes an additional argument:</p>
615
616<ul>
617<li><b>:</b> - plus a string argument, keep most recent if more than one.</li>
618<li><b>*</b> - plus a string argument, appended to a linked list.</li>
Rob Landley66a69d92012-01-16 01:44:17 -0600619<li><b>@</b> - plus an occurrence counter (stored in a long)</li>
Rob Landleyb6063de2012-01-29 13:54:13 -0600620<li><b>#</b> - plus a signed long argument.
621<li><b>.</b> - plus a floating point argument (if CFG_TOYBOX_FLOAT).</li>
622<ul>The following can be appended to a float or double:
623<li><b>&lt;123</b> - error if argument is less than this</li>
624<li><b>&gt;123</b> - error if argument is greater than this</li>
625<li><b>=123</b> - default value if argument not supplied</li>
626</ul>
627<ul><li>Option parsing only understands <>= after . when CFG_TOYBOX_FLOAT
628is enabled. (Otherwise the code to determine where floating point constants
629end drops out. When disabled, it can reserve a global data slot for the
630argument so offsets won't change, but will never fill it out.). You can handle
631this by using the USE_BLAH() macros with C string concatenation, ala:
632"abc." USE_TOYBOX_FLOAT("<1.23>4.56=7.89") "def"</li></ul>
Rob Landley66a69d92012-01-16 01:44:17 -0600633</ul>
634
635<p>Arguments may occur with or without a space (I.E. "-a 42" or "-a42").
636The command line argument "-abc" may be interepreted many different ways:
637the optflags string "cba" sets toys.optflags = 7, "c:ba" sets toys.optflags=4
638and saves "ba" as the argument to -c, and "cb:a" sets optflags to 6 and saves
639"c" as the argument to -b.</p>
640
641<p>Options which have an argument fill in the corresponding slot in the global
642union "this" (see generated/globals.h), treating it as an array of longs
643with the rightmost saved in this[0]. Again using "a*b:c#d", "-c 42" would set
644this[0]=42; and "-b 42" would set this[1]="42"; each slot is left NULL if
645the corresponding argument is not encountered.</p>
646
647<p>This behavior is useful because the LP64 standard ensures long and pointer
Rob Landleyb4a0efa2012-02-06 21:15:19 -0600648are the same size. C99 guarantees structure members will occur in memory
649in the same order they're declared, and that padding won't be inserted between
Rob Landley66a69d92012-01-16 01:44:17 -0600650consecutive variables of register size. Thus the first few entries can
651be longs or pointers corresponding to the saved arguments.</p>
652
653<p><b>char *toys.optargs[]</b></p>
654
655<p>Command line arguments in argv[] which are not consumed by option parsing
656(I.E. not recognized either as -flags or arguments to -flags) will be copied
657to toys.optargs[], with the length of that array in toys.optc.
658(When toys.optc is 0, no unrecognized command line arguments remain.)
659The order of entries is preserved, and as with argv[] this new array is also
660terminated by a NULL entry.</p>
661
662<p>Option parsing can require a minimum or maximum number of optargs left
663over, by adding "<1" (read "at least one") or ">9" ("at most nine") to the
664start of the optflags string.</p>
665
666<p>The special argument "--" terminates option parsing, storing all remaining
667arguments in optargs. The "--" itself is consumed.</p>
668
669<p><b>Other optflags control characters</b></p>
670
671<p>The following characters may occur at the start of each command's
672optflags string, before any options that would set a bit in toys.optflags:</p>
673
674<ul>
675<li><b>^</b> - stop at first nonoption argument (for nice, xargs...)</li>
676<li><b>?</b> - allow unknown arguments (pass non-option arguments starting
677with - through to optargs instead of erroring out).</li>
678<li><b>&amp;</b> - the first argument has imaginary dash (ala tar/ps. If given twice, all arguments have imaginary dash.)</li>
679<li><b>&lt;</b> - must be followed by a decimal digit indicating at least this many leftover arguments are needed in optargs (default 0)</li>
680<li><b>&gt;</b> - must be followed by a decimal digit indicating at most this many leftover arguments allowed (default MAX_INT)</li>
681</ul>
682
683<p>The following characters may be appended to an option character, but do
684not by themselves indicate an extra argument should be saved in this[].
685(Technically any character not recognized as a control character sets an
686optflag, but letters are never control characters.)</p>
687
688<ul>
689<li><b>^</b> - stop parsing options after encountering this option, everything else goes into optargs.</li>
690<li><b>|</b> - this option is required. If more than one marked, only one is required.</li>
691<li><b>+X</b> enabling this option also enables option X (switch bit on).</li>
692<li><b>~X</b> enabling this option disables option X (switch bit off).</li>
693<li><b>!X</b> this option cannot be used in combination with X (die with error).</li>
694<li><b>[yz]</b> this option requires at least one of y or z to also be enabled.</li>
695</ul>
696
Rob Landleyb6063de2012-01-29 13:54:13 -0600697<p>The following may be appended to a float or double:</p>
698
699<ul>
700<li><b>&lt;123</b> - error if argument is less than this</li>
701<li><b>&gt;123</b> - error if argument is greater than this</li>
702<li><b>=123</b> - default value if argument not supplied</li>
703</ul>
704
705<p>Option parsing only understands <>= after . when CFG_TOYBOX_FLOAT
706is enabled. (Otherwise the code to determine where floating point constants
707end drops out. When disabled, it can reserve a global data slot for the
708argument so offsets won't change, but will never fill it out.). You can handle
709this by using the USE_BLAH() macros with C string concatenation, ala:</p>
710
711<blockquote>"abc." USE_TOYBOX_FLOAT("<1.23>4.56=7.89") "def"</blockquote>
712
Rob Landley66a69d92012-01-16 01:44:17 -0600713<p><b>--longopts</b></p>
714
715<p>The optflags string can contain long options, which are enclosed in
716parentheses. They may be appended to an existing option character, in
717which case the --longopt is a synonym for that option, ala "a:(--fred)"
718which understands "-a blah" or "--fred blah" as synonyms.</p>
719
720<p>Longopts may also appear before any other options in the optflags string,
721in which case they have no corresponding short argument, but instead set
722their own bit based on position. So for "(walrus)#(blah)xy:z" "command
723--walrus 42" would set toys.optflags = 16 (-z = 1, -y = 2, -x = 4, --blah = 8)
724and would assign this[1] = 42;</p>
725
726<p>A short option may have multiple longopt synonyms, "a(one)(two)", but
727each "bare longopt" (ala "(one)(two)abc" before any option characters)
728always sets its own bit (although you can group them with +X).</p>
Rob Landley7c04f012008-01-20 19:00:16 -0600729
Rob Landley81b899d2007-12-18 02:02:47 -0600730<h2>Directory scripts/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600731
732<h3>scripts/cfg2files.sh</h3>
733
734<p>Run .config through this filter to get a list of enabled commands, which
735is turned into a list of files in toys via a sed invocation in the top level
736Makefile.
737</p>
738
Rob Landley81b899d2007-12-18 02:02:47 -0600739<h2>Directory kconfig/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600740
741<p>Menuconfig infrastructure copied from the Linux kernel. See the
742Linux kernel's Documentation/kbuild/kconfig-language.txt</p>
743
Rob Landley66a69d92012-01-16 01:44:17 -0600744<a name="generated">
745<h2>Directory generated/</h2>
746
747<p>All the files in this directory except the README are generated by the
748build. (See scripts/make.sh)</p>
749
750<ul>
751<li><p><b>config.h</b> - CFG_COMMAND and USE_COMMAND() macros set by menuconfig via .config.</p></li>
752
753<li><p><b>Config.in</b> - Kconfig entries for each command. Included by top level Config.in. The help text in here is used to generated help.h</p></li>
754
755<li><p><b>help.h</b> - Help text strings for use by "help" command. Building
756this file requires python on the host system, so the prebuilt file is shipped
757in the build tarball to avoid requiring python to build toybox.</p></li>
758
759<li><p><b>newtoys.h</b> - List of NEWTOY() or OLDTOY() macros for all available
760commands. Associates command_main() functions with command names, provides
761option string for command line parsing (<a href="#lib_args">see lib/args.c</a>),
762specifies where to install each command and whether toysh should fork before
763calling it.</p></li>
764</ul>
765
766<p>Everything in this directory is a derivative file produced from something
767else. The entire directory is deleted by "make distclean".</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600768<!--#include file="footer.html" -->