blob: 7603c841304e4db343503abb899d55d6309d3a53 [file] [log] [blame]
Rob Landley349ff522014-01-04 13:09:42 -06001<html><head><title>toybox source code walkthrough</title></head>
Rob Landley4e68de12007-12-13 07:00:27 -06002<!--#include file="header.html" -->
3
Rob Landley27048342013-08-18 14:24:59 -05004<p><h1><a name="style" /><a href="#style">Code style</a></h1></p>
Rob Landleye7c9a6d2012-02-28 06:34:09 -06005
6<p>The primary goal of toybox is _simple_ code. Keeping the code small is
Rob Landleyed6ed622012-03-06 20:49:03 -06007second, with speed and lots of features coming in somewhere after that.
8(For more on that, see the <a href=design.html>design</a> page.)</p>
Rob Landleye7c9a6d2012-02-28 06:34:09 -06009
10<p>A simple implementation usually takes up fewer lines of source code,
11meaning more code can fit on the screen at once, meaning the programmer can
12see more of it on the screen and thus keep more if in their head at once.
Rob Landleyed6ed622012-03-06 20:49:03 -060013This helps code auditing and thus reduces bugs. That said, sometimes being
14more explicit is preferable to being clever enough to outsmart yourself:
15don't be so terse your code is unreadable.</p>
Rob Landley5a0660f2007-12-27 21:36:44 -060016
Rob Landleyca733922014-05-19 18:24:35 -050017<p>Toybox has an actual coding style guide over on
18<a href=design.html#codestyle>the design page</a>, but in general we just
19want the code to be consistent.</p>
Rob Landley5a0660f2007-12-27 21:36:44 -060020
Rob Landley27048342013-08-18 14:24:59 -050021<p><h1><a name="building" /><a href="#building">Building Toybox</a></h1></p>
Rob Landleye7c9a6d2012-02-28 06:34:09 -060022
23<p>Toybox is configured using the Kconfig language pioneered by the Linux
24kernel, and adopted by many other projects (uClibc, OpenEmbedded, etc).
25This generates a ".config" file containing the selected options, which
Rob Landley7aa651a2012-11-13 17:14:08 -060026controls which features are included when compiling toybox.</p>
Rob Landleye7c9a6d2012-02-28 06:34:09 -060027
28<p>Each configuration option has a default value. The defaults indicate the
29"maximum sane configuration", I.E. if the feature defaults to "n" then it
30either isn't complete or is a special-purpose option (such as debugging
31code) that isn't intended for general purpose use.</p>
32
33<p>The standard build invocation is:</p>
34
35<ul>
36<li>make defconfig #(or menuconfig)</li>
37<li>make</li>
38<li>make install</li>
39</ul>
40
41<p>Type "make help" to see all available build options.</p>
42
43<p>The file "configure" contains a number of environment variable definitions
44which influence the build, such as specifying which compiler to use or where
45to install the resulting binaries. This file is included by the build, but
46accepts existing definitions of the environment variables, so it may be sourced
47or modified by the developer before building and the definitions exported
48to the environment will take precedence.</p>
49
50<p>(To clarify: "configure" describes the build and installation environment,
51".config" lists the features selected by defconfig/menuconfig.)</p>
Rob Landley6882ee82008-02-12 18:41:34 -060052
Rob Landley27048342013-08-18 14:24:59 -050053<p><h1><a name="running"><a href="#running">Running a command</a></h1></p>
54
55<h2>main</h2>
56
57<p>The toybox main() function is at the end of main.c at the top level. It has
58two possible codepaths, only one of which is configured into any given build
59of toybox.</p>
60
61<p>If CONFIG_SINGLE is selected, toybox is configured to contain only a single
62command, so most of the normal setup can be skipped. In this case the
63multiplexer isn't used, instead main() calls toy_singleinit() (also in main.c)
64to set up global state and parse command line arguments, calls the command's
65main function out of toy_list (in the CONFIG_SINGLE case the array has a single entry, no need to search), and if the function returns instead of exiting
66it flushes stdout (detecting error) and returns toys.exitval.</p>
67
68<p>When CONFIG_SINGLE is not selected, main() uses basename() to find the
69name it was run as, shifts its argument list one to the right so it lines up
70with where the multiplexer function expects it, and calls toybox_main(). This
71leverages the multiplexer command's infrastructure to find and run the
72appropriate command. (A command name starting with "toybox" will
73recursively call toybox_main(); you can go "./toybox toybox toybox toybox ls"
74if you want to...)</p>
75
76<h2>toybox_main</h2>
77
78<p>The toybox_main() function is also in main,c. It handles a possible
79--help option ("toybox --help ls"), prints the list of available commands if no
80arguments were provided to the multiplexer (or with full path names if any
81other option is provided before a command name, ala "toybox --list").
82Otherwise it calls toy_exec() on its argument list.</p>
83
84<p>Note that the multiplexer is the first entry in toy_list (the rest of the
85list is sorted alphabetically to allow binary search), so toybox_main can
86cheat and just grab the first entry to quickly set up its context without
87searching. Since all command names go through the multiplexer at least once
88in the non-TOYBOX_SINGLE case, this avoids a redundant search of
89the list.</p>
90
91<p>The toy_exec() function is also in main.c. It performs toy_find() to
92perform a binary search on the toy_list array to look up the command's
93entry by name and saves it in the global variable which, calls toy_init()
94to parse command line arguments and set up global state (using which->options),
95and calls the appropriate command's main() function (which->toy_main). On
96return it flushes all pending ansi FILE * I/O, detects if stdout had an
97error, and then calls xexit() (which uses toys.exitval).</p>
98
99<p><h1><a name="infrastructure" /><a href="#infrastructure">Infrastructure</a></h1></p>
Rob Landley4e68de12007-12-13 07:00:27 -0600100
Rob Landley7c04f012008-01-20 19:00:16 -0600101<p>The toybox source code is in following directories:</p>
102<ul>
103<li>The <a href="#top">top level directory</a> contains the file main.c (were
104execution starts), the header file toys.h (included by every command), and
105other global infrastructure.</li>
106<li>The <a href="#lib">lib directory</a> contains common functions shared by
Rob Landleyc8d0da52012-07-15 17:47:08 -0500107multiple commands:</li>
108<ul>
109<li><a href="#lib_lib">lib/lib.c</a></li>
Rob Landley52370152014-09-20 14:11:59 -0500110<li><a href="#lib_xwrap">lib/xwrap.c</a></li>
Rob Landleyc8d0da52012-07-15 17:47:08 -0500111<li><a href="#lib_llist">lib/llist.c</a></li>
112<li><a href="#lib_args">lib/args.c</a></li>
113<li><a href="#lib_dirtree">lib/dirtree.c</a></li>
114</ul>
Rob Landley7c04f012008-01-20 19:00:16 -0600115<li>The <a href="#toys">toys directory</a> contains the C files implementating
Rob Landley002a11e2014-05-22 08:16:55 -0500116each command. Currently it contains five subdirectories categorizing the
117commands: posix, lsb, other, example, and pending.</li>
Rob Landley7c04f012008-01-20 19:00:16 -0600118<li>The <a href="#scripts">scripts directory</a> contains the build and
119test infrastructure.</li>
120<li>The <a href="#kconfig">kconfig directory</a> contains the configuration
121infrastructure implementing menuconfig (copied from the Linux kernel).</li>
122<li>The <a href="#generated">generated directory</a> contains intermediate
123files generated from other parts of the source code.</li>
124</ul>
Rob Landley4e68de12007-12-13 07:00:27 -0600125
Rob Landleybbe500e2012-02-26 21:53:15 -0600126<a name="adding" />
Rob Landley7eaf4f52014-04-09 08:30:09 -0500127<p><h1><a href="#adding">Adding a new command</a></h1></p>
Rob Landley52370152014-09-20 14:11:59 -0500128<p>To add a new command to toybox, add a C file implementing that command to
129one of the subdirectories under the toys directory. No other files need to
130be modified; the build extracts all the information it needs (such as command
131line arguments) from specially formatted comments and macros in the C file.
132(See the description of the <a href="#generated">"generated" directory</a>
133for details.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600134
Rob Landley002a11e2014-05-22 08:16:55 -0500135<p>Currently there are five subdirectories under "toys", one for commands
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500136defined by the POSIX standard, one for commands defined by the Linux Standard
Rob Landley002a11e2014-05-22 08:16:55 -0500137Base, an "other" directory for commands not covered by an obvious standard,
138a directory of example commands (templates to use when starting new commands),
139and a "pending" directory of commands that need further review/cleanup
140before moving to one of the other directories (run these at your own risk,
141cleanup patches welcome).
142These directories are just for developer convenience sorting the commands,
143the directories are otherwise functionally identical. To add a new category,
144create the appropriate directory with a README file in it whose first line
145is the description menuconfig should use for the directory.)</p>
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500146
Rob Landley002a11e2014-05-22 08:16:55 -0500147<p>An easy way to start a new command is copy the file "toys/example/hello.c"
148to the name of the new command, and modify this copy to implement the new
149command (more or less by turning every instance of "hello" into the
Rob Landley7c04f012008-01-20 19:00:16 -0600150name of your command, updating the command line arguments, globals, and
Rob Landley002a11e2014-05-22 08:16:55 -0500151help data, and then filling out its "main" function with code that does
152something interesting).</p>
153
154<p>You could also start with "toys/example/skeleton.c", which provides a lot
155more example code (showing several variants of command line option
156parsing, how to implement multiple commands in the same file, and so on).
157But usually it's just more stuff to delete.</p>
Rob Landley7c04f012008-01-20 19:00:16 -0600158
159<p>Here's a checklist of steps to turn hello.c into another command:</p>
160
161<ul>
Rob Landley002a11e2014-05-22 08:16:55 -0500162<li><p>First "cp toys/example/hello.c toys/other/yourcommand.c" and open
163the new file in your preferred text editor.</p>
164<ul><li><p>Note that the
165name of the new file is significant: it's the name of the new command you're
166adding to toybox. The build includes all *.c files under toys/*/ whose
167names are a case insensitive match for an enabled config symbol. So
168toys/posix/cat.c only gets included if you have "CAT=y" in ".config".</p></li>
169</ul></p></li>
Rob Landley7c04f012008-01-20 19:00:16 -0600170
171<li><p>Change the one line comment at the top of the file (currently
172"hello.c - A hello world program") to describe your new file.</p></li>
173
174<li><p>Change the copyright notice to your name, email, and the current
175year.</p></li>
176
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500177<li><p>Give a URL to the relevant standards document, where applicable.
178(Sample links to SUSv4 and LSB are provided, feel free to link to other
179documentation or standards as appropriate.)</p></li>
Rob Landley7c04f012008-01-20 19:00:16 -0600180
Rob Landley66a69d92012-01-16 01:44:17 -0600181<li><p>Update the USE_YOURCOMMAND(NEWTOY(yourcommand,"blah",0)) line.
182The NEWTOY macro fills out this command's <a href="#toy_list">toy_list</a>
183structure. The arguments to the NEWTOY macro are:</p>
184
185<ol>
186<li><p>the name used to run your command</p></li>
Rob Landley002a11e2014-05-22 08:16:55 -0500187<li><p>the command line argument <a href="#lib_args">option parsing string</a> (0 if none)</p></li>
Rob Landley66a69d92012-01-16 01:44:17 -0600188<li><p>a bitfield of TOYFLAG values
189(defined in toys.h) providing additional information such as where your
190command should be installed on a running system, whether to blank umask
191before running, whether or not the command must run as root (and thus should
192retain root access if installed SUID), and so on.</p></li>
193</ol>
194</li>
Rob Landley7c04f012008-01-20 19:00:16 -0600195
196<li><p>Change the kconfig data (from "config YOURCOMMAND" to the end of the
197comment block) to supply your command's configuration and help
198information. The uppper case config symbols are used by menuconfig, and are
199also what the CFG_ and USE_() macros are generated from (see [TODO]). The
200help information here is used by menuconfig, and also by the "help" command to
201describe your new command. (See [TODO] for details.) By convention,
Rob Landley66a69d92012-01-16 01:44:17 -0600202unfinished commands default to "n" and finished commands default to "y",
203so "make defconfig" selects all finished commands. (Note, "finished" means
204"ready to be used", not that it'll never change again.)<p>
205
206<p>Each help block should start with a "usage: yourcommand" line explaining
207any command line arguments added by this config option. The "help" command
208outputs this text, and scripts/config2help.c in the build infrastructure
209collates these usage lines for commands with multiple configuration
210options when producing generated/help.h.</p>
211</li>
Rob Landley7c04f012008-01-20 19:00:16 -0600212
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500213<li><p>Change the "#define FOR_hello" line to "#define FOR_yourcommand" right
214before the "#include <toys.h>". (This selects the appropriate FLAG_ macros and
215does a "#define TT this.yourcommand" so you can access the global variables
216out of the space-saving union of structures. If you aren't using any command
217flag bits and aren't defining a GLOBAL block, you can delete this line.)</p></li>
Rob Landley7c04f012008-01-20 19:00:16 -0600218
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500219<li><p>Update the GLOBALS() macro to contain your command's global
220variables. If your command has no global variables, delete this macro.</p>
221
222<p>Variables in the GLOBALS() block are are stored in a space saving
223<a href="#toy_union">union of structures</a> format, which may be accessed
224using the TT macro as if TT were a global structure (so TT.membername).
225If you specified two-character command line arguments in
226NEWTOY(), the first few global variables will be initialized by the automatic
227argument parsing logic, and the type and order of these variables must
228correspond to the arguments specified in NEWTOY().
229(See <a href="#lib_args">lib/args.c</a> for details.)</p></li>
Rob Landley7c04f012008-01-20 19:00:16 -0600230
231<li><p>Rename hello_main() to yourcommand_main(). This is the main() function
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500232where execution of your command starts. Your command line options are
233already sorted into this.optflags, this.optargs, this.optc, and the GLOBALS()
234as appropriate by the time this function is called. (See
Rob Landley002a11e2014-05-22 08:16:55 -0500235<a href="#lib_args">get_optflags()</a> for details.)</p></li>
Rob Landley7c04f012008-01-20 19:00:16 -0600236</ul>
237
Rob Landley7eaf4f52014-04-09 08:30:09 -0500238<a name="headers" /><h2><a href="#headers">Headers.</a></h2>
Rob Landley85a32412013-12-27 06:53:15 -0600239
240<p>Commands generally don't have their own headers. If it's common code
241it can live in lib/, if it isn't put it in the command's .c file. (The line
242between implementing multiple commands in a C file via OLDTOY() to share
243infrastructure and moving that shared infrastructure to lib/ is a judgement
244call. Try to figure out which is simplest.)</p>
245
246<p>The top level toys.h should #include all the standard (posix) headers
247that any command uses. (Partly this is friendly to ccache and partly this
248makes the command implementations shorter.) Individual commands should only
249need to include nonstandard headers that might prevent that command from
250building in some context we'd care about (and thus requiring that command to
251be disabled to avoid a build break).</p>
252
253<p>Target-specific stuff (differences between compiler versions, libc versions,
254or operating systems) should be confined to lib/portability.h and
255lib/portability.c. (There's even some minimal compile-time environment probing
256that writes data to generated/portability.h, see scripts/genconfig.sh.)</p>
257
258<p>Only include linux/*.h headers from individual commands (not from other
259headers), and only if you really need to. Data that varies per architecture
260is a good reason to include a header. If you just need a couple constants
261that haven't changed since the 1990's, it's ok to #define them yourself or
262just use the constant inline with a comment explaining what it is. (A
263#define that's only used once isn't really helping.)</p>
264
Rob Landley7eaf4f52014-04-09 08:30:09 -0500265<p><a name="top" /><h1><a href="#top">Top level directory.</a></h1></p>
Rob Landley7c04f012008-01-20 19:00:16 -0600266
Rob Landley66a69d92012-01-16 01:44:17 -0600267<p>This directory contains global infrastructure.</p>
268
269<h3>toys.h</h3>
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500270<p>Each command #includes "toys.h" as part of its standard prolog. It
271may "#define FOR_commandname" before doing so to get some extra entries
272specific to this command.</p>
Rob Landley66a69d92012-01-16 01:44:17 -0600273
274<p>This file sucks in most of the commonly used standard #includes, so
275individual files can just #include "toys.h" and not have to worry about
276stdargs.h and so on. Individual commands still need to #include
277special-purpose headers that may not be present on all systems (and thus would
278prevent toybox from building that command on such a system with that command
279enabled). Examples include regex support, any "linux/" or "asm/" headers, mtab
280support (mntent.h and sys/mount.h), and so on.</p>
281
282<p>The toys.h header also defines structures for most of the global variables
283provided to each command by toybox_main(). These are described in
284detail in the description for main.c, where they are initialized.</p>
285
286<p>The global variables are grouped into structures (and a union) for space
287savings, to more easily track the amount of memory consumed by them,
288so that they may be automatically cleared/initialized as needed, and so
289that access to global variables is more easily distinguished from access to
290local variables.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600291
292<h3>main.c</h3>
293<p>Contains the main() function where execution starts, plus
294common infrastructure to initialize global variables and select which command
Rob Landley6882ee82008-02-12 18:41:34 -0600295to run. The "toybox" multiplexer command also lives here. (This is the
Rob Landley7c04f012008-01-20 19:00:16 -0600296only command defined outside of the toys directory.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600297
Rob Landley6882ee82008-02-12 18:41:34 -0600298<p>Execution starts in main() which trims any path off of the first command
299name and calls toybox_main(), which calls toy_exec(), which calls toy_find()
Rob Landley66a69d92012-01-16 01:44:17 -0600300and toy_init() before calling the appropriate command's function from
301toy_list[] (via toys.which->toy_main()).
Rob Landley6882ee82008-02-12 18:41:34 -0600302If the command is "toybox", execution recurses into toybox_main(), otherwise
303the call goes to the appropriate commandname_main() from a C file in the toys
304directory.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600305
Rob Landley6882ee82008-02-12 18:41:34 -0600306<p>The following global variables are defined in main.c:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600307<ul>
Rob Landley66a69d92012-01-16 01:44:17 -0600308<a name="toy_list" />
309<li><p><b>struct toy_list toy_list[]</b> - array describing all the
Rob Landley4e68de12007-12-13 07:00:27 -0600310commands currently configured into toybox. The first entry (toy_list[0]) is
311for the "toybox" multiplexer command, which runs all the other built-in commands
312without symlinks by using its first argument as the name of the command to
313run and the rest as that command's argument list (ala "./toybox echo hello").
314The remaining entries are the commands in alphabetical order (for efficient
315binary search).</p>
316
317<p>This is a read-only array initialized at compile time by
Rob Landley6882ee82008-02-12 18:41:34 -0600318defining macros and #including generated/newtoys.h.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600319
Rob Landley66a69d92012-01-16 01:44:17 -0600320<p>Members of struct toy_list (defined in "toys.h") include:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600321<ul>
322<li><p>char *<b>name</b> - the name of this command.</p></li>
323<li><p>void (*<b>toy_main</b>)(void) - function pointer to run this
324command.</p></li>
325<li><p>char *<b>options</b> - command line option string (used by
326get_optflags() in lib/args.c to intialize toys.optflags, toys.optargs, and
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500327entries in the toy's GLOBALS struct). When this is NULL, no option
Rob Landley66a69d92012-01-16 01:44:17 -0600328parsing is done before calling toy_main().</p></li>
Rob Landley6882ee82008-02-12 18:41:34 -0600329<li><p>int <b>flags</b> - Behavior flags for this command. The following flags are currently understood:</p>
330
331<ul>
332<li><b>TOYFLAG_USR</b> - Install this command under /usr</li>
333<li><b>TOYFLAG_BIN</b> - Install this command under /bin</li>
334<li><b>TOYFLAG_SBIN</b> - Install this command under /sbin</li>
335<li><b>TOYFLAG_NOFORK</b> - This command can be used as a shell builtin.</li>
336<li><b>TOYFLAG_UMASK</b> - Call umask(0) before running this command.</li>
Rob Landley66a69d92012-01-16 01:44:17 -0600337<li><b>TOYFLAG_STAYROOT</b> - Don't drop permissions for this command if toybox is installed SUID root.</li>
338<li><b>TOYFLAG_NEEDROOT</b> - This command cannot function unless run with root access.</li>
Rob Landley6882ee82008-02-12 18:41:34 -0600339</ul>
340<br>
341
342<p>These flags are combined with | (or). For example, to install a command
343in /usr/bin, or together TOYFLAG_USR|TOYFLAG_BIN.</p>
344</ul>
Rob Landley4e68de12007-12-13 07:00:27 -0600345</li>
346
Rob Landley66a69d92012-01-16 01:44:17 -0600347<li><p><b>struct toy_context toys</b> - global structure containing information
348common to all commands, initializd by toy_init() and defined in "toys.h".
349Members of this structure include:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600350<ul>
351<li><p>struct toy_list *<b>which</b> - a pointer to this command's toy_list
352structure. Mostly used to grab the name of the running command
353(toys->which.name).</p>
354</li>
355<li><p>int <b>exitval</b> - Exit value of this command. Defaults to zero. The
356error_exit() functions will return 1 if this is zero, otherwise they'll
357return this value.</p></li>
358<li><p>char **<b>argv</b> - "raw" command line options, I.E. the original
359unmodified string array passed in to main(). Note that modifying this changes
Rob Landley66a69d92012-01-16 01:44:17 -0600360"ps" output, and is not recommended. This array is null terminated; a NULL
361entry indicates the end of the array.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600362<p>Most commands don't use this field, instead the use optargs, optflags,
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500363and the fields in the GLOBALS struct initialized by get_optflags().</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600364</li>
365<li><p>unsigned <b>optflags</b> - Command line option flags, set by
Rob Landley66a69d92012-01-16 01:44:17 -0600366<a href="#lib_args">get_optflags()</a>. Indicates which of the command line options listed in
Rob Landley6882ee82008-02-12 18:41:34 -0600367toys->which.options occurred this time.</p>
368
369<p>The rightmost command line argument listed in toys->which.options sets bit
3701, the next one sets bit 2, and so on. This means the bits are set in the same
371order the binary digits would be listed if typed out as a string. For example,
372the option string "abcd" would parse the command line "-c" to set optflags to 2,
373"-a" would set optflags to 8, and "-bd" would set optflags to 6 (4|2).</p>
374
375<p>Only letters are relevant to optflags. In the string "a*b:c#d", d=1, c=2,
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500376b=4, a=8. Punctuation after a letter initializes global variables at the
377start of the GLOBALS() block (see <a href="#toy_union">union toy_union this</a>
378for details).</p>
379
380<p>The build infrastructure creates FLAG_ macros for each option letter,
381corresponding to the bit position, so you can check (toys.optflags & FLAG_x)
382to see if a flag was specified. (The correct set of FLAG_ macros is selected
383by defining FOR_mycommand before #including toys.h. The macros live in
384toys/globals.h which is generated by scripts/make.sh.)</p>
Rob Landley6882ee82008-02-12 18:41:34 -0600385
Rob Landley66a69d92012-01-16 01:44:17 -0600386<p>For more information on option parsing, see <a href="#lib_args">get_optflags()</a>.</p>
Rob Landley6882ee82008-02-12 18:41:34 -0600387
388</li>
Rob Landley4e68de12007-12-13 07:00:27 -0600389<li><p>char **<b>optargs</b> - Null terminated array of arguments left over
390after get_optflags() removed all the ones it understood. Note: optarg[0] is
391the first argument, not the command name. Use toys.which->name for the command
392name.</p></li>
Rob Landley6882ee82008-02-12 18:41:34 -0600393<li><p>int <b>optc</b> - Optarg count, equivalent to argc but for
394optargs[].<p></li>
Rob Landley4e68de12007-12-13 07:00:27 -0600395<li><p>int <b>exithelp</b> - Whether error_exit() should print a usage message
396via help_main() before exiting. (True during option parsing, defaults to
397false afterwards.)</p></li>
Rob Landley66a69d92012-01-16 01:44:17 -0600398</ul>
Rob Landley4e68de12007-12-13 07:00:27 -0600399
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500400<a name="toy_union" />
Rob Landley66a69d92012-01-16 01:44:17 -0600401<li><p><b>union toy_union this</b> - Union of structures containing each
Rob Landley4e68de12007-12-13 07:00:27 -0600402command's global variables.</p>
403
Rob Landley6882ee82008-02-12 18:41:34 -0600404<p>Global variables are useful: they reduce the overhead of passing extra
405command line arguments between functions, they conveniently start prezeroed to
406save initialization costs, and the command line argument parsing infrastructure
407can also initialize global variables with its results.</p>
408
409<p>But since each toybox process can only run one command at a time, allocating
410space for global variables belonging to other commands you aren't currently
411running would be wasteful.</p>
412
413<p>Toybox handles this by encapsulating each command's global variables in
Rob Landley66a69d92012-01-16 01:44:17 -0600414a structure, and declaring a union of those structures with a single global
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500415instance (called "this"). The GLOBALS() macro contains the global
Rob Landley66a69d92012-01-16 01:44:17 -0600416variables that should go in the current command's global structure. Each
417variable can then be accessed as "this.commandname.varname".
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500418If you #defined FOR_commandname before including toys.h, the macro TT is
419#defined to this.commandname so the variable can then be accessed as
420"TT.variable". See toys/hello.c for an example.</p>
Rob Landley6882ee82008-02-12 18:41:34 -0600421
Rob Landley66a69d92012-01-16 01:44:17 -0600422<p>A command that needs global variables should declare a structure to
Rob Landley4e68de12007-12-13 07:00:27 -0600423contain them all, and add that structure to this union. A command should never
424declare global variables outside of this, because such global variables would
425allocate memory when running other commands that don't use those global
426variables.</p>
427
Rob Landley66a69d92012-01-16 01:44:17 -0600428<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 -0600429as specified by the options field off this command's toy_list entry. See
430the get_optargs() description in lib/args.c for details.</p>
431</li>
432
Rob Landley81b899d2007-12-18 02:02:47 -0600433<li><b>char toybuf[4096]</b> - a common scratch space buffer so
Rob Landley4e68de12007-12-13 07:00:27 -0600434commands don't need to allocate their own. Any command is free to use this,
435and it should never be directly referenced by functions in lib/ (although
436commands are free to pass toybuf in to a library function as an argument).</li>
437</ul>
438
Rob Landley6882ee82008-02-12 18:41:34 -0600439<p>The following functions are defined in main.c:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600440<ul>
441<li><p>struct toy_list *<b>toy_find</b>(char *name) - Return the toy_list
442structure for this command name, or NULL if not found.</p></li>
Rob Landley81b899d2007-12-18 02:02:47 -0600443<li><p>void <b>toy_init</b>(struct toy_list *which, char *argv[]) - fill out
444the global toys structure, calling get_optargs() if necessary.</p></li>
Rob Landley6882ee82008-02-12 18:41:34 -0600445<li><p>void <b>toy_exec</b>(char *argv[]) - Run a built-in command with
446arguments.</p>
447<p>Calls toy_find() on argv[0] (which must be just a command name
Rob Landley4e68de12007-12-13 07:00:27 -0600448without path). Returns if it can't find this command, otherwise calls
Rob Landley6882ee82008-02-12 18:41:34 -0600449toy_init(), toys->which.toy_main(), and exit() instead of returning.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600450
Rob Landley6882ee82008-02-12 18:41:34 -0600451<p>Use the library function xexec() to fall back to external executables
452in $PATH if toy_exec() can't find a built-in command. Note that toy_exec()
453does not strip paths before searching for a command, so "./command" will
454never match an internal command.</li>
455
456<li><p>void <b>toybox_main</b>(void) - the main function for the multiplexer
457command (I.E. "toybox"). Given a command name as its first argument, calls
458toy_exec() on its arguments. With no arguments, it lists available commands.
459If the first argument starts with "-" it lists each command with its default
460install path prepended.</p></li>
Rob Landley4e68de12007-12-13 07:00:27 -0600461
462</ul>
463
464<h3>Config.in</h3>
465
466<p>Top level configuration file in a stylized variant of
Rob Landley6882ee82008-02-12 18:41:34 -0600467<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 -0600468
469<p>These files are directly used by "make menuconfig" to select which commands
470to build into toybox (thus generating a .config file), and by
Rob Landley6882ee82008-02-12 18:41:34 -0600471scripts/config2help.py to create generated/help.h.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600472
Rob Landley7eaf4f52014-04-09 08:30:09 -0500473<a name="generated" />
474<h1><a href="#generated">Temporary files:</a></h1>
Rob Landley4e68de12007-12-13 07:00:27 -0600475
Rob Landley6882ee82008-02-12 18:41:34 -0600476<p>There is one temporary file in the top level source directory:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600477<ul>
478<li><p><b>.config</b> - Configuration file generated by kconfig, indicating
479which commands (and options to commands) are currently enabled. Used
Rob Landley7eaf4f52014-04-09 08:30:09 -0500480to make generated/config.h and determine which toys/*/*.c files to build.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600481
Rob Landley6882ee82008-02-12 18:41:34 -0600482<p>You can create a human readable "miniconfig" version of this file using
Rob Landley66a69d92012-01-16 01:44:17 -0600483<a href=http://landley.net/aboriginal/new_platform.html#miniconfig>these
Rob Landley6882ee82008-02-12 18:41:34 -0600484instructions</a>.</p>
485</li>
486</ul>
487
Rob Landley7eaf4f52014-04-09 08:30:09 -0500488<p><h2>Directory generated/</h2></p>
489
490<p>The remaining temporary files live in the "generated/" directory,
491which is for files generated at build time from other source files.</p>
Rob Landley6882ee82008-02-12 18:41:34 -0600492
493<ul>
Rob Landley52370152014-09-20 14:11:59 -0500494<li><p><b>generated/Config.in</b> - Kconfig entries for each command, included
495from the top level Config.in. The help text here is used to generate
496help.h.</p>
Rob Landley7eaf4f52014-04-09 08:30:09 -0500497
498<p>Each command has a configuration entry with an upper case version of
499the command name. Options to commands start with the command
500name followed by an underscore and the option name. Global options are attached
501to the "toybox" command, and thus use the prefix "TOYBOX_". This organization
502is used by scripts/cfg2files to select which toys/*/*.c files to compile for a
503given .config.</p>
Rob Landley7eaf4f52014-04-09 08:30:09 -0500504</li>
505
Rob Landley6882ee82008-02-12 18:41:34 -0600506<li><p><b>generated/config.h</b> - list of CFG_SYMBOL and USE_SYMBOL() macros,
Rob Landley52370152014-09-20 14:11:59 -0500507generated from .config by a sed invocation in scripts/make.sh.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600508
509<p>CFG_SYMBOL is a comple time constant set to 1 for enabled symbols and 0 for
Rob Landley7eaf4f52014-04-09 08:30:09 -0500510disabled symbols. This allows the use of normal if() statements to remove
Rob Landley6882ee82008-02-12 18:41:34 -0600511code at compile time via the optimizer's dead code elimination (which removes
Rob Landley7eaf4f52014-04-09 08:30:09 -0500512from the binary any code that cannot be reached). This saves space without
Rob Landley4e68de12007-12-13 07:00:27 -0600513cluttering the code with #ifdefs or leading to configuration dependent build
Rob Landley7eaf4f52014-04-09 08:30:09 -0500514breaks. (See the 1992 Usenix paper
Rob Landleyb6063de2012-01-29 13:54:13 -0600515<a href=http://doc.cat-v.org/henry_spencer/ifdef_considered_harmful.pdf>#ifdef
Rob Landley4e68de12007-12-13 07:00:27 -0600516Considered Harmful</a> for more information.)</p>
517
Rob Landley52370152014-09-20 14:11:59 -0500518<p>When you can't entirely avoid an #ifdef, the USE_SYMBOL(code) macro
519provides a less intrusive alternative, evaluating to the code in parentheses
520when the symbol is enabled, and nothing when the symbol is disabled. This
521is most commonly used around NEWTOY() declarations (so only the enabled
522commands show up in toy_list), and in option strings. This can also be used
523for things like varargs or structure members which can't always be
524eliminated by a simple test on CFG_SYMBOL. Remember, unlike CFG_SYMBOL
525this is really just a variant of #ifdef, and can still result in configuration
526dependent build breaks. Use with caution.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600527</li>
Rob Landley4e68de12007-12-13 07:00:27 -0600528
Rob Landley7eaf4f52014-04-09 08:30:09 -0500529<li><p><b>generated/flags.h</b> - FLAG_? macros indicating which command
530line options were seen. The option parsing in lib/args.c sets bits in
531toys.optflags, which can be tested by anding with the appropriate FLAG_
532macro. (Bare longopts, which have no corresponding short option, will
533have the longopt name after FLAG_. All others use the single letter short
534option.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600535
Rob Landley7eaf4f52014-04-09 08:30:09 -0500536<p>To get the appropriate macros for your command, #define FOR_commandname
537before #including toys.h. To switch macro sets (because you have an OLDTOY()
538with different options in the same .c file), #define CLEANUP_oldcommand
539and also #define FOR_newcommand, then #include "generated/flags.h" to switch.
540</p>
541</li>
Rob Landley4e68de12007-12-13 07:00:27 -0600542
Rob Landley7eaf4f52014-04-09 08:30:09 -0500543<li><p><b>generated/globals.h</b> -
544Declares structures to hold the contents of each command's GLOBALS(),
545and combines them into "global_union this". (Yes, the name was
546chosen to piss off C++ developers who think that C
547is merely a subset of C++, not a language in its own right.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600548
Rob Landley7eaf4f52014-04-09 08:30:09 -0500549<p>The union reuses the same memory for each command's global struct:
550since only one command's globals are in use at any given time, collapsing
551them together saves space. The headers #define TT to the appropriate
552"this.commandname", so you can refer to the current command's global
553variables out of "this" as TT.variablename.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600554
Rob Landley7eaf4f52014-04-09 08:30:09 -0500555<p>The globals start zeroed, and the first few are filled out by the
556lib/args.c argument parsing code called from main.c.</p>
557</li>
Rob Landley4e68de12007-12-13 07:00:27 -0600558
Rob Landley52370152014-09-20 14:11:59 -0500559<li><p><b>toys/help.h</b> - Help strings for use by the "help" command and
560--help options. This file #defines a help_symbolname string for each
561symbolname, but only the symbolnames matching command names get used
562by show_help() in lib/help.c to display help for commands.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600563
Rob Landley7eaf4f52014-04-09 08:30:09 -0500564<p>This file is created by scripts/make.sh, which compiles scripts/config2help.c
565into the binary generated/config2help, and then runs it against the top
566level .config and Config.in files to extract the help text from each config
567entry and collate together dependent options.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600568
Rob Landley7eaf4f52014-04-09 08:30:09 -0500569<p>This file contains help text for all commands, regardless of current
570configuration, but only the ones currently enabled in the .config file
571wind up in the help_data[] array, and only the enabled dependent options
572have their help text added to the command they depend on.</p>
573</li>
574
575<li><p><b>generated/newtoys.h</b> -
Rob Landley52370152014-09-20 14:11:59 -0500576All the NEWTOY() and OLDTOY() macros from toys/*/*.c. The "toybox" multiplexer
577is the first entry, the rest are in alphabetical order. Each line should be
578inside an appropriate USE_ macro, so code that #includes this file only sees
579the currently enabled commands.</p>
Rob Landley7eaf4f52014-04-09 08:30:09 -0500580
581<p>By #definining NEWTOY() to various things before #including this file,
582it may be used to create function prototypes (in toys.h), initialize the
Rob Landley52370152014-09-20 14:11:59 -0500583help_data array (in lib/help.c), initialize the toy_list array (in main.c,
584the alphabetical order lets toy_find() do a binary search, the exception to
585the alphabetical order lets it use the multiplexer without searching), and so
586on. (It's even used to initialize the NEED_OPTIONS macro, which produces a 1
587or 0 for each command using command line option parsing, which is ORed together
588to allow compile-time dead code elimination to remove the whole of
Rob Landley7eaf4f52014-04-09 08:30:09 -0500589lib/args.c if nothing currently enabled is using it.)<p>
590
591<p>Each NEWTOY and OLDTOY macro contains the command name, command line
592option string (telling lib/args.c how to parse command line options for
593this command), recommended install location, and miscelaneous data such
594as whether this command should retain root permissions if installed suid.</p>
595</li>
596
597<li><p><b>toys/oldtoys.h</b> - Macros with the command line option parsing
598string for each NEWTOY. This allows an OLDTOY that's just an alias for an
599existing command to refer to the existing option string instead of
600having to repeat it.</p>
601</li>
602</ul>
Rob Landley4e68de12007-12-13 07:00:27 -0600603
Rob Landley137bf342012-03-09 08:33:57 -0600604<a name="lib">
Rob Landley81b899d2007-12-18 02:02:47 -0600605<h2>Directory lib/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600606
Rob Landley137bf342012-03-09 08:33:57 -0600607<p>TODO: document lots more here.</p>
608
Rob Landleyc8d0da52012-07-15 17:47:08 -0500609<p>lib: getmountlist(), error_msg/error_exit, xmalloc(),
Rob Landley7c04f012008-01-20 19:00:16 -0600610strlcpy(), xexec(), xopen()/xread(), xgetcwd(), xabspath(), find_in_path(),
611itoa().</p>
612
Rob Landley52370152014-09-20 14:11:59 -0500613
614
615<a name="lib_xwrap"><h3>lib/xwrap.c</h3>
616
617<p>Functions prefixed with the letter x call perror_exit() when they hit
618errors, to eliminate common error checking. This prints an error message
619and the strerror() string for the errno encountered.</p>
620
621<p>You can intercept this exit by assigning a setjmp/longjmp buffer to
622toys.rebound (set it back to zero to restore the default behavior).
623If you do this, cleaning up resource leaks is your problem.</p>
624
625<ul>
626<li><b>void xstrncpy(char *dest, char *src, size_t size)</b></li>
627<li><b>void xexit(void)</b></li>
628<li><b>void *xmalloc(size_t size)</b></li>
629<li><b>void *xzalloc(size_t size)</b></li>
630<li><b>void *xrealloc(void *ptr, size_t size)</b></li>
631<li><b>char *xstrndup(char *s, size_t n)</b></li>
632<li><b>char *xstrdup(char *s)</b></li>
633<li><b>char *xmprintf(char *format, ...)</b></li>
634<li><b>void xprintf(char *format, ...)</b></li>
635<li><b>void xputs(char *s)</b></li>
636<li><b>void xputc(char c)</b></li>
637<li><b>void xflush(void)</b></li>
638<li><b>pid_t xfork(void)</b></li>
639<li><b>void xexec_optargs(int skip)</b></li>
640<li><b>void xexec(char **argv)</b></li>
641<li><b>pid_t xpopen(char **argv, int *pipes)</b></li>
642<li><b>int xpclose(pid_t pid, int *pipes)</b></li>
643<li><b>void xaccess(char *path, int flags)</b></li>
644<li><b>void xunlink(char *path)</b></li>
645<li><p><b>int xcreate(char *path, int flags, int mode)<br />
646int xopen(char *path, int flags)</b></p>
647
648<p>The xopen() and xcreate() functions open an existing file (exiting if
649it's not there) and create a new file (exiting if it can't).</p>
650
651<p>They default to O_CLOEXEC so the filehandles aren't passed on to child
652processes. Feed in O_CLOEXEC to disable this.</p>
653</li>
654<li><p><b>void xclose(int fd)</b></p>
655
656<p>Because NFS is broken, and won't necessarily perform the requested
657operation (and report the error) until you close the file. Of course, this
658being NFS, it's not guaranteed to report the error there either, but it
659_can_.</p>
660
661<p>Nothing else ever reports an error on close, everywhere else it's just a
662VFS operation freeing some resources. NFS is _special_, in a way that
663other network filesystems like smbfs and v9fs aren't..</p>
664</li>
665<li><b>int xdup(int fd)</b></li>
666<li><p><b>size_t xread(int fd, void *buf, size_t len)</b></p>
667
668<p>Can return 0, but not -1.</p>
669</li>
670<li><p><b>void xreadall(int fd, void *buf, size_t len)</b></p>
671
672<p>Reads the entire len-sized buffer, retrying to complete short
673reads. Exits if it can't get enough data.</p></li>
674
675<li><p><b>void xwrite(int fd, void *buf, size_t len)</b></p>
676
677<p>Retries short writes, exits if can't write the entire buffer.</p></li>
678
679<li><b>off_t xlseek(int fd, off_t offset, int whence)</b></li>
680<li><b>char *xgetcwd(void)</b></li>
681<li><b>void xstat(char *path, struct stat *st)</b></li>
682<li><p><b>char *xabspath(char *path, int exact) </b></p>
683
684<p>After several years of
685<a href=http://landley.net/notes-2007.html#18-06-2007>wrestling</a>
686<a href=http://landley.net/notes-2008.html#19-01-2008>with</a> realpath(),
687I broke down and <a href=http://landley.net/notes-2012.html#20-11-2012>wrote
688my own</a> implementation that doesn't use the one in libc. As I explained:
689
690<blockquote><p>If the path ends with a broken link,
691readlink -f should show where the link points to, not where the broken link
692lives. (The point of readlink -f is "if I write here, where would it attempt
693to create a file".) The problem is, realpath() returns NULL for a path ending
694with a broken link, and I can't beat different behavior out of code locked
695away in libc.</p></blockquote>
696
697<p>
698</li>
699<li><b>void xchdir(char *path)</b></li>
700<li><b>void xchroot(char *path)</b></li>
701
702<li><p><b>struct passwd *xgetpwuid(uid_t uid)<br />
703struct group *xgetgrgid(gid_t gid)<br />
704struct passwd *xgetpwnam(char *name)</b></p>
705
706<p></p>
707</li>
708
709
710
711<li><b>void xsetuser(struct passwd *pwd)</b></li>
712<li><b>char *xreadlink(char *name)</b></li>
713<li><b>char *xreadfile(char *name, char *buf, off_t len)</b></li>
714<li><b>int xioctl(int fd, int request, void *data)</b></li>
715<li><b>void xpidfile(char *name)</b></li>
716<li><b>void xsendfile(int in, int out)</b></li>
717<li><b>long xparsetime(char *arg, long units, long *fraction)</b></li>
718<li><b>void xregcomp(regex_t *preg, char *regex, int cflags)</b></li>
719</ul>
720
721<a name="lib_lib"><h3>lib/lib.c</h3>
722<p>Eight gazillion common functions:</p>
723
724<ul>
725<li><b>void verror_msg(char *msg, int err, va_list va)</b></li>
726<li><b>void error_msg(char *msg, ...)</b></li>
727<li><b>void perror_msg(char *msg, ...)</b></li>
728<li><b>void error_exit(char *msg, ...)</b></li>
729<li><b>void perror_exit(char *msg, ...)</b></li>
730<li><b>ssize_t readall(int fd, void *buf, size_t len)</b></li>
731<li><b>ssize_t writeall(int fd, void *buf, size_t len)</b></li>
732<li><b>off_t lskip(int fd, off_t offset)</b></li>
733<li><b>int mkpathat(int atfd, char *dir, mode_t lastmode, int flags)</b></li>
734<li><b>struct string_list **splitpath(char *path, struct string_list **list)</b></li>
735<li><b>struct string_list *find_in_path(char *path, char *filename)</b></li>
736<li><b>long atolx(char *numstr)</b></li>
737<li><b>long atolx_range(char *numstr, long low, long high)</b></li>
738<li><b>int numlen(long l)</b></li>
739<li><b>int stridx(char *haystack, char needle)</b></li>
740<li><b>int strstart(char **a, char *b)</b></li>
741<li><b>off_t fdlength(int fd)</b></li>
742<li><b>char *readfile(char *name, char *ibuf, off_t len)</b></li>
743<li><b>void msleep(long miliseconds)</b></li>
744<li><b>int64_t peek_le(void *ptr, unsigned size)</b></li>
745<li><b>int64_t peek_be(void *ptr, unsigned size)</b></li>
746<li><b>int64_t peek(void *ptr, unsigned size)</b></li>
747<li><b>void poke(void *ptr, uint64_t val, int size)</b></li>
748<li><b>void loopfiles_rw(char **argv, int flags, int permissions, int failok,</b></li>
749<li><b>void loopfiles(char **argv, void (*function)(int fd, char *name))</b></li>
750<li><b>char *get_rawline(int fd, long *plen, char end)</b></li>
751<li><b>char *get_line(int fd)</b></li>
752<li><b>int wfchmodat(int fd, char *name, mode_t mode)</b></li>
753<li><b>static void tempfile_handler(int i)</b></li>
754<li><b>int copy_tempfile(int fdin, char *name, char **tempname)</b></li>
755<li><b>void delete_tempfile(int fdin, int fdout, char **tempname)</b></li>
756<li><b>void replace_tempfile(int fdin, int fdout, char **tempname)</b></li>
757<li><b>void crc_init(unsigned int *crc_table, int little_endian)</b></li>
758<li><b>int terminal_size(unsigned *xx, unsigned *yy)</b></li>
759<li><b>int yesno(char *prompt, int def)</b></li>
760<li><b>void generic_signal(int sig)</b></li>
761<li><b>void sigatexit(void *handler)</b></li>
762<li><b>int sig_to_num(char *pidstr)</b></li>
763<li><b>char *num_to_sig(int sig)</b></li>
764<li><b>mode_t string_to_mode(char *modestr, mode_t mode)</b></li>
765<li><b>void mode_to_string(mode_t mode, char *buf)</b></li>
766<li><b>void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))</b></li>
767<li><b>int human_readable(char *buf, unsigned long long num)</b></li>
768</ul>
769
Rob Landley137bf342012-03-09 08:33:57 -0600770<h3>lib/portability.h</h3>
771
772<p>This file is automatically included from the top of toys.h, and smooths
773over differences between platforms (hardware targets, compilers, C libraries,
774operating systems, etc).</p>
775
776<p>This file provides SWAP macros (SWAP_BE16(x) and SWAP_LE32(x) and so on).</p>
777
778<p>A macro like SWAP_LE32(x) means "The value in x is stored as a little
779endian 32 bit value, so perform the translation to/from whatever the native
78032-bit format is". You do the swap once on the way in, and once on the way
781out. If your target is already little endian, the macro is a NOP.</p>
782
783<p>The SWAP macros come in BE and LE each with 16, 32, and 64 bit versions.
784In each case, the name of the macro refers to the _external_ representation,
785and converts to/from whatever your native representation happens to be (which
786can vary depending on what you're currently compiling for).</p>
787
Rob Landleyc8d0da52012-07-15 17:47:08 -0500788<a name="lib_llist"><h3>lib/llist.c</h3>
789
790<p>Some generic single and doubly linked list functions, which take
791advantage of a couple properties of C:</p>
792
793<ul>
794<li><p>Structure elements are laid out in memory in the order listed, and
795the first element has no padding. This means you can always treat (typecast)
796a pointer to a structure as a pointer to the first element of the structure,
797even if you don't know anything about the data following it.</p></li>
798
799<li><p>An array of length zero at the end of a structure adds no space
800to the sizeof() the structure, but if you calculate how much extra space
801you want when you malloc() the structure it will be available at the end.
802Since C has no bounds checking, this means each struct can have one variable
803length array.</p></li>
804</ul>
805
806<p>Toybox's list structures always have their <b>next</b> pointer as
807the first entry of each struct, and singly linked lists end with a NULL pointer.
808This allows generic code to traverse such lists without knowing anything
809else about the specific structs composing them: if your pointer isn't NULL
810typecast it to void ** and dereference once to get the next entry.</p>
811
812<p><b>lib/lib.h</b> defines three structure types:</p>
813<ul>
814<li><p><b>struct string_list</b> - stores a single string (<b>char str[0]</b>),
815memory for which is allocated as part of the node. (I.E. llist_traverse(list,
816free); can clean up after this type of list.)</p></li>
817
818<li><p><b>struct arg_list</b> - stores a pointer to a single string
819(<b>char *arg</b>) which is stored in a separate chunk of memory.</p></li>
820
821<li><p><b>struct double_list</b> - has a second pointer (<b>struct double_list
822*prev</b> along with a <b>char *data</b> for payload.</p></li>
823</ul>
824
825<b>List Functions</b>
826
827<ul>
828<li><p>void *<b>llist_pop</b>(void **list) - advances through a list ala
829<b>node = llist_pop(&list);</b> This doesn't modify the list contents,
830but does advance the pointer you feed it (which is why you pass the _address_
831of that pointer, not the pointer itself).</p></li>
832
833<li><p>void <b>llist_traverse</b>(void *list, void (*using)(void *data)) -
834iterate through a list calling a function on each node.</p></li>
835
836<li><p>struct double_list *<b>dlist_add</b>(struct double_list **llist, char *data)
837- append an entry to a circular linked list.
838This function allocates a new struct double_list wrapper and returns the
839pointer to the new entry (which you can usually ignore since it's llist->prev,
840but if llist was NULL you need it). The argument is the ->data field for the
841new node.</p></li>
842<ul><li><p>void <b>dlist_add_nomalloc</b>(struct double_list **llist,
843struct double_list *new) - append existing struct double_list to
844list, does not allocate anything.</p></li></ul>
845</ul>
846
Rob Landley7eaf4f52014-04-09 08:30:09 -0500847<b>List code trivia questions:</b>
Rob Landleyc8d0da52012-07-15 17:47:08 -0500848
849<ul>
850<li><p><b>Why do arg_list and double_list contain a char * payload instead of
851a void *?</b> - Because you always have to typecast a void * to use it, and
Rob Landley7eaf4f52014-04-09 08:30:09 -0500852typecasting a char * does no harm. Since strings are the most common
853payload, and doing math on the pointer ala
854"(type *)(ptr+sizeof(thing)+sizeof(otherthing))" requires ptr to be char *
855anyway (at least according to the C standard), defaulting to char * saves
856a typecast.</p>
Rob Landleyc8d0da52012-07-15 17:47:08 -0500857</li>
858
859<li><p><b>Why do the names ->str, ->arg, and ->data differ?</b> - To force
860you to keep track of which one you're using, calling free(node->str) would
861be bad, and _failing_ to free(node->arg) leaks memory.</p></li>
862
863<li><p><b>Why does llist_pop() take a void * instead of void **?</b> -
864because the stupid compiler complains about "type punned pointers" when
Rob Landley7eaf4f52014-04-09 08:30:09 -0500865you typecast and dereference on the same line,
Rob Landleyc8d0da52012-07-15 17:47:08 -0500866due to insane FSF developers hardwiring limitations of their optimizer
867into gcc's warning system. Since C automatically typecasts any other
Rob Landley7eaf4f52014-04-09 08:30:09 -0500868pointer type to and from void *, the current code works fine. It's sad that it
Rob Landleyc8d0da52012-07-15 17:47:08 -0500869won't warn you if you forget the &, but the code crashes pretty quickly in
870that case.</p></li>
871
872<li><p><b>How do I assemble a singly-linked-list in order?</b> - use
873a double_list, dlist_add() your entries, and then break the circle with
874<b>list->prev->next = NULL;</b> when done.</li>
875</ul>
876
Rob Landley66a69d92012-01-16 01:44:17 -0600877<a name="lib_args"><h3>lib/args.c</h3>
Rob Landley7c04f012008-01-20 19:00:16 -0600878
Rob Landley66a69d92012-01-16 01:44:17 -0600879<p>Toybox's main.c automatically parses command line options before calling the
880command's main function. Option parsing starts in get_optflags(), which stores
881results in the global structures "toys" (optflags and optargs) and "this".</p>
882
883<p>The option parsing infrastructure stores a bitfield in toys.optflags to
884indicate which options the current command line contained. Arguments
885attached to those options are saved into the command's global structure
886("this"). Any remaining command line arguments are collected together into
887the null-terminated array toys.optargs, with the length in toys.optc. (Note
888that toys.optargs does not contain the current command name at position zero,
889use "toys.which->name" for that.) The raw command line arguments get_optflags()
890parsed are retained unmodified in toys.argv[].</p>
891
892<p>Toybox's option parsing logic is controlled by an "optflags" string, using
893a format reminiscent of getopt's optargs but has several important differences.
894Toybox does not use the getopt()
895function out of the C library, get_optflags() is an independent implementation
896which doesn't permute the original arguments (and thus doesn't change how the
897command is displayed in ps and top), and has many features not present in
898libc optargs() (such as the ability to describe long options in the same string
899as normal options).</p>
900
901<p>Each command's NEWTOY() macro has an optflags string as its middle argument,
902which sets toy_list.options for that command to tell get_optflags() what
903command line arguments to look for, and what to do with them.
904If a command has no option
905definition string (I.E. the argument is NULL), option parsing is skipped
906for that command, which must look at the raw data in toys.argv to parse its
907own arguments. (If no currently enabled command uses option parsing,
908get_optflags() is optimized out of the resulting binary by the compiler's
909--gc-sections option.)</p>
910
911<p>You don't have to free the option strings, which point into the environment
912space (I.E. the string data is not copied). A TOYFLAG_NOFORK command
913that uses the linked list type "*" should free the list objects but not
914the data they point to, via "llist_free(TT.mylist, NULL);". (If it's not
915NOFORK, exit() will free all the malloced data anyway unless you want
916to implement a CONFIG_TOYBOX_FREE cleanup for it.)</p>
917
918<h4>Optflags format string</h4>
919
920<p>Note: the optflags option description string format is much more
921concisely described by a large comment at the top of lib/args.c.</p>
922
923<p>The general theory is that letters set optflags, and punctuation describes
924other actions the option parsing logic should take.</p>
925
926<p>For example, suppose the command line <b>command -b fruit -d walrus -a 42</b>
927is parsed using the optflags string "<b>a#b:c:d</b>". (I.E.
928toys.which->options="a#b:c:d" and argv = ["command", "-b", "fruit", "-d",
929"walrus", "-a", "42"]). When get_optflags() returns, the following data is
930available to command_main():
931
932<ul>
933<li><p>In <b>struct toys</b>:
934<ul>
935<li>toys.optflags = 13; // -a = 8 | -b = 4 | -d = 1</li>
936<li>toys.optargs[0] = "walrus"; // leftover argument</li>
937<li>toys.optargs[1] = NULL; // end of list</li>
Rob Landleyb911d4d2013-09-21 14:27:26 -0500938<li>toys.optc = 1; // there was 1 leftover argument</li>
Rob Landley66a69d92012-01-16 01:44:17 -0600939<li>toys.argv[] = {"-b", "fruit", "-d", "walrus", "-a", "42"}; // The original command line arguments
940</ul>
941<p></li>
942
943<li><p>In <b>union this</b> (treated as <b>long this[]</b>):
944<ul>
945<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>
946<li>this[1] = (long)"fruit"; // argument to -b</li>
947<li>this[2] = 42; // argument to -a</li>
948</ul>
949</p></li>
950</ul>
951
952<p>If the command's globals are:</p>
953
954<blockquote><pre>
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500955GLOBALS(
Rob Landley66a69d92012-01-16 01:44:17 -0600956 char *c;
957 char *b;
958 long a;
959)
Rob Landley66a69d92012-01-16 01:44:17 -0600960</pre></blockquote>
961<p>That would mean TT.c == NULL, TT.b == "fruit", and TT.a == 42. (Remember,
962each entry that receives an argument must be a long or pointer, to line up
963with the array position. Right to left in the optflags string corresponds to
Rob Landleyc0e56ed2012-10-08 00:02:30 -0500964top to bottom in GLOBALS().</p>
Rob Landley66a69d92012-01-16 01:44:17 -0600965
Rob Landleyb911d4d2013-09-21 14:27:26 -0500966<p>Put globals not filled out by the option parsing logic at the end of the
967GLOBALS block. Common practice is to list the options one per line (to
968make the ordering explicit, first to last in globals corresponds to right
969to left in the option string), then leave a blank line before any non-option
970globals.</p>
971
Rob Landley66a69d92012-01-16 01:44:17 -0600972<p><b>long toys.optflags</b></p>
973
974<p>Each option in the optflags string corresponds to a bit position in
975toys.optflags, with the same value as a corresponding binary digit. The
976rightmost argument is (1<<0), the next to last is (1<<1) and so on. If
Rob Landleyb4a0efa2012-02-06 21:15:19 -0600977the option isn't encountered while parsing argv[], its bit remains 0.</p>
978
979<p>For example,
Rob Landley66a69d92012-01-16 01:44:17 -0600980the optflags string "abcd" would parse the command line argument "-c" to set
981optflags to 2, "-a" would set optflags to 8, "-bd" would set optflags to
9826 (I.E. 4|2), and "-a -c" would set optflags to 10 (2|8).</p>
983
984<p>Only letters are relevant to optflags, punctuation is skipped: in the
985string "a*b:c#d", d=1, c=2, b=4, a=8. The punctuation after a letter
986usually indicate that the option takes an argument.</p>
987
Rob Landleyb4a0efa2012-02-06 21:15:19 -0600988<p>Since toys.optflags is an unsigned int, it only stores 32 bits. (Which is
989the amount a long would have on 32-bit platforms anyway; 64 bit code on
99032 bit platforms is too expensive to require in common code used by almost
991all commands.) Bit positions beyond the 1<<31 aren't recorded, but
992parsing higher options can still set global variables.</p>
993
Rob Landley66a69d92012-01-16 01:44:17 -0600994<p><b>Automatically setting global variables from arguments (union this)</b></p>
995
996<p>The following punctuation characters may be appended to an optflags
997argument letter, indicating the option takes an additional argument:</p>
998
999<ul>
1000<li><b>:</b> - plus a string argument, keep most recent if more than one.</li>
1001<li><b>*</b> - plus a string argument, appended to a linked list.</li>
Rob Landley66a69d92012-01-16 01:44:17 -06001002<li><b>@</b> - plus an occurrence counter (stored in a long)</li>
Rob Landleyb6063de2012-01-29 13:54:13 -06001003<li><b>#</b> - plus a signed long argument.
Rob Landleyb911d4d2013-09-21 14:27:26 -05001004<li><b>-</b> - plus a signed long argument defaulting to negative (start argument with + to force a positive value).</li>
Rob Landleyb6063de2012-01-29 13:54:13 -06001005<li><b>.</b> - plus a floating point argument (if CFG_TOYBOX_FLOAT).</li>
1006<ul>The following can be appended to a float or double:
1007<li><b>&lt;123</b> - error if argument is less than this</li>
1008<li><b>&gt;123</b> - error if argument is greater than this</li>
1009<li><b>=123</b> - default value if argument not supplied</li>
1010</ul>
Rob Landley66a69d92012-01-16 01:44:17 -06001011</ul>
1012
Rob Landleyb911d4d2013-09-21 14:27:26 -05001013<p>A note about "." and CFG_TOYBOX_FLOAT: option parsing only understands <>=
1014after . when CFG_TOYBOX_FLOAT
1015is enabled. (Otherwise the code to determine where floating point constants
1016end drops out; it requires floating point). When disabled, it can reserve a
1017global data slot for the argument (so offsets won't change in your
1018GLOBALS[] block), but will never fill it out. You can handle
1019this by using the USE_BLAH() macros with C string concatenation, ala:
1020"abc." USE_TOYBOX_FLOAT("<1.23>4.56=7.89") "def"</p>
1021
1022<p><b>GLOBALS</b></p>
Rob Landley66a69d92012-01-16 01:44:17 -06001023
1024<p>Options which have an argument fill in the corresponding slot in the global
1025union "this" (see generated/globals.h), treating it as an array of longs
Rob Landleyb911d4d2013-09-21 14:27:26 -05001026with the rightmost saved in this[0]. As described above, using "a*b:c#d",
1027"-c 42" would set this[0] = 42; and "-b 42" would set this[1] = "42"; each
1028slot is left NULL if the corresponding argument is not encountered.</p>
Rob Landley66a69d92012-01-16 01:44:17 -06001029
1030<p>This behavior is useful because the LP64 standard ensures long and pointer
Rob Landleyb4a0efa2012-02-06 21:15:19 -06001031are the same size. C99 guarantees structure members will occur in memory
1032in the same order they're declared, and that padding won't be inserted between
Rob Landley66a69d92012-01-16 01:44:17 -06001033consecutive variables of register size. Thus the first few entries can
1034be longs or pointers corresponding to the saved arguments.</p>
1035
Rob Landleyb911d4d2013-09-21 14:27:26 -05001036<p>See toys/other/hello.c for a longer example of parsing options into the
1037GLOBALS block.</p>
1038
Rob Landley66a69d92012-01-16 01:44:17 -06001039<p><b>char *toys.optargs[]</b></p>
1040
1041<p>Command line arguments in argv[] which are not consumed by option parsing
1042(I.E. not recognized either as -flags or arguments to -flags) will be copied
1043to toys.optargs[], with the length of that array in toys.optc.
1044(When toys.optc is 0, no unrecognized command line arguments remain.)
1045The order of entries is preserved, and as with argv[] this new array is also
1046terminated by a NULL entry.</p>
1047
1048<p>Option parsing can require a minimum or maximum number of optargs left
1049over, by adding "<1" (read "at least one") or ">9" ("at most nine") to the
1050start of the optflags string.</p>
1051
1052<p>The special argument "--" terminates option parsing, storing all remaining
1053arguments in optargs. The "--" itself is consumed.</p>
1054
1055<p><b>Other optflags control characters</b></p>
1056
1057<p>The following characters may occur at the start of each command's
1058optflags string, before any options that would set a bit in toys.optflags:</p>
1059
1060<ul>
1061<li><b>^</b> - stop at first nonoption argument (for nice, xargs...)</li>
1062<li><b>?</b> - allow unknown arguments (pass non-option arguments starting
1063with - through to optargs instead of erroring out).</li>
1064<li><b>&amp;</b> - the first argument has imaginary dash (ala tar/ps. If given twice, all arguments have imaginary dash.)</li>
1065<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>
1066<li><b>&gt;</b> - must be followed by a decimal digit indicating at most this many leftover arguments allowed (default MAX_INT)</li>
1067</ul>
1068
1069<p>The following characters may be appended to an option character, but do
1070not by themselves indicate an extra argument should be saved in this[].
1071(Technically any character not recognized as a control character sets an
1072optflag, but letters are never control characters.)</p>
1073
1074<ul>
1075<li><b>^</b> - stop parsing options after encountering this option, everything else goes into optargs.</li>
1076<li><b>|</b> - this option is required. If more than one marked, only one is required.</li>
Rob Landley66a69d92012-01-16 01:44:17 -06001077</ul>
1078
Rob Landleyb6063de2012-01-29 13:54:13 -06001079<p>The following may be appended to a float or double:</p>
1080
1081<ul>
1082<li><b>&lt;123</b> - error if argument is less than this</li>
1083<li><b>&gt;123</b> - error if argument is greater than this</li>
1084<li><b>=123</b> - default value if argument not supplied</li>
1085</ul>
1086
1087<p>Option parsing only understands <>= after . when CFG_TOYBOX_FLOAT
1088is enabled. (Otherwise the code to determine where floating point constants
1089end drops out. When disabled, it can reserve a global data slot for the
1090argument so offsets won't change, but will never fill it out.). You can handle
1091this by using the USE_BLAH() macros with C string concatenation, ala:</p>
1092
1093<blockquote>"abc." USE_TOYBOX_FLOAT("<1.23>4.56=7.89") "def"</blockquote>
1094
Rob Landley66a69d92012-01-16 01:44:17 -06001095<p><b>--longopts</b></p>
1096
1097<p>The optflags string can contain long options, which are enclosed in
1098parentheses. They may be appended to an existing option character, in
1099which case the --longopt is a synonym for that option, ala "a:(--fred)"
1100which understands "-a blah" or "--fred blah" as synonyms.</p>
1101
1102<p>Longopts may also appear before any other options in the optflags string,
1103in which case they have no corresponding short argument, but instead set
1104their own bit based on position. So for "(walrus)#(blah)xy:z" "command
1105--walrus 42" would set toys.optflags = 16 (-z = 1, -y = 2, -x = 4, --blah = 8)
1106and would assign this[1] = 42;</p>
1107
1108<p>A short option may have multiple longopt synonyms, "a(one)(two)", but
1109each "bare longopt" (ala "(one)(two)abc" before any option characters)
1110always sets its own bit (although you can group them with +X).</p>
Rob Landley7c04f012008-01-20 19:00:16 -06001111
Rob Landleyb911d4d2013-09-21 14:27:26 -05001112<p><b>[groups]</b></p>
1113
1114<p>At the end of the option string, square bracket groups can define
1115relationships between existing options. (This only applies to short
1116options, bare --longopts can't participate.)</p>
1117
1118<p>The first character of the group defines the type, the remaining
1119characters are options it applies to:</p>
1120
1121<ul>
1122<li><b>-</b> - Exclusive, switch off all others in this group.</li>
1123<li><b>+</b> - Inclusive, switch on all others in this group.</li>
1124<li><b>!</b> - Error, fail if more than one defined.</li>
1125</ul>
1126
1127<p>So "abc[-abc]" means -ab = -b, -ba = -a, -abc = -c. "abc[+abc]"
1128means -ab=-abc, -c=-abc, and "abc[!abc] means -ab calls error_exit("no -b
1129with -a"). Note that [-] groups clear the GLOBALS option slot of
1130options they're switching back off, but [+] won't set options it didn't see
1131(just the optflags).</p>
1132
1133<p><b>whitespace</b></p>
1134
1135<p>Arguments may occur with or without a space (I.E. "-a 42" or "-a42").
1136The command line argument "-abc" may be interepreted many different ways:
1137the optflags string "cba" sets toys.optflags = 7, "c:ba" sets toys.optflags=4
1138and saves "ba" as the argument to -c, and "cb:a" sets optflags to 6 and saves
1139"c" as the argument to -b.</p>
1140
1141<p>Note that &amp; changes whitespace handling, so that the command line
1142"tar cvfCj outfile.tar.bz2 topdir filename" is parsed the same as
1143"tar filename -c -v -j -f outfile.tar.bz2 -C topdir". Note that "tar -cvfCj
1144one two three" would equal "tar -c -v -f Cj one two three". (This matches
1145historical usage.)</p>
1146
1147<p>Appending a space to the option in the option string ("a: b") makes it
1148require a space, I.E. "-ab" is interpreted as "-a" "-b". That way "kill -stop"
1149differs from "kill -s top".</p>
1150
1151<p>Appending ; to a longopt in the option string makes its argument optional,
1152and only settable with =, so in ls "(color):;" can accept "ls --color" and
1153"ls --color=auto" without complaining that the first has no argument.</p>
1154
Rob Landleyc8d0da52012-07-15 17:47:08 -05001155<a name="lib_dirtree"><h3>lib/dirtree.c</h3>
1156
1157<p>The directory tree traversal code should be sufficiently generic
1158that commands never need to use readdir(), scandir(), or the fts.h family
1159of functions.</p>
1160
1161<p>These functions do not call chdir() or rely on PATH_MAX. Instead they
1162use openat() and friends, using one filehandle per directory level to
1163recurseinto subdirectories. (I.E. they can descend 1000 directories deep
1164if setrlimit(RLIMIT_NOFILE) allows enough open filehandles, and the default
1165in /proc/self/limits is generally 1024.)</p>
1166
1167<p>The basic dirtree functions are:</p>
1168
1169<ul>
1170<li><p><b>dirtree_read(char *path, int (*callback)(struct dirtree node))</b> -
1171recursively read directories, either applying callback() or returning
1172a tree of struct dirtree if callback is NULL.</p></li>
1173
1174<li><p><b>dirtree_path(struct dirtree *node, int *plen)</b> - malloc() a
1175string containing the path from the root of this tree to this node. If
1176plen isn't NULL then *plen is how many extra bytes to malloc at the end
1177of string.</p></li>
1178
1179<li><p><b>dirtree_parentfd(struct dirtree *node)</b> - return fd of
1180containing directory, for use with openat() and such.</p></li>
1181</ul>
1182
1183<p>The <b>dirtree_read()</b> function takes two arguments, a starting path for
1184the root of the tree, and a callback function. The callback takes a
1185<b>struct dirtree *</b> (from lib/lib.h) as its argument. If the callback is
1186NULL, the traversal uses a default callback (dirtree_notdotdot()) which
1187recursively assembles a tree of struct dirtree nodes for all files under
1188this directory and subdirectories (filtering out "." and ".." entries),
1189after which dirtree_read() returns the pointer to the root node of this
1190snapshot tree.</p>
1191
1192<p>Otherwise the callback() is called on each entry in the directory,
1193with struct dirtree * as its argument. This includes the initial
1194node created by dirtree_read() at the top of the tree.</p>
1195
1196<p><b>struct dirtree</b></p>
1197
1198<p>Each struct dirtree node contains <b>char name[]</b> and <b>struct stat
1199st</b> entries describing a file, plus a <b>char *symlink</b>
1200which is NULL for non-symlinks.</p>
1201
1202<p>During a callback function, the <b>int data</b> field of directory nodes
1203contains a dirfd (for use with the openat() family of functions). This is
1204generally used by calling dirtree_parentfd() on the callback's node argument.
1205For symlinks, data contains the length of the symlink string. On the second
1206callback from DIRTREE_COMEAGAIN (depth-first traversal) data = -1 for
1207all nodes (that's how you can tell it's the second callback).</p>
1208
1209<p>Users of this code may put anything they like into the <b>long extra</b>
1210field. For example, "cp" and "mv" use this to store a dirfd for the destination
1211directory (and use DIRTREE_COMEAGAIN to get the second callback so they can
1212close(node->extra) to avoid running out of filehandles).
1213This field is not directly used by the dirtree code, and
1214thanks to LP64 it's large enough to store a typecast pointer to an
1215arbitrary struct.</p>
1216
1217<p>The return value of the callback combines flags (with boolean or) to tell
1218the traversal infrastructure how to behave:</p>
1219
1220<ul>
1221<li><p><b>DIRTREE_SAVE</b> - Save this node, assembling a tree. (Without
1222this the struct dirtree is freed after the callback returns. Filtering out
1223siblings is fine, but discarding a parent while keeping its child leaks
1224memory.)</p></li>
1225<li><p><b>DIRTREE_ABORT</b> - Do not examine any more entries in this
1226directory. (Does not propagate up tree: to abort entire traversal,
1227return DIRTREE_ABORT from parent callbacks too.)</p></li>
1228<li><p><b>DIRTREE_RECURSE</b> - Examine directory contents. Ignored for
1229non-directory entries. The remaining flags only take effect when
1230recursing into the children of a directory.</p></li>
1231<li><p><b>DIRTREE_COMEAGAIN</b> - Call the callback a second time after
1232examining all directory contents, allowing depth-first traversal.
1233On the second call, dirtree->data = -1.</p></li>
1234<li><p><b>DIRTREE_SYMFOLLOW</b> - follow symlinks when populating children's
1235<b>struct stat st</b> (by feeding a nonzero value to the symfollow argument of
1236dirtree_add_node()), which means DIRTREE_RECURSE treats symlinks to
1237directories as directories. (Avoiding infinite recursion is the callback's
1238problem: the non-NULL dirtree->symlink can still distinguish between
1239them.)</p></li>
1240</ul>
1241
1242<p>Each struct dirtree contains three pointers (next, parent, and child)
1243to other struct dirtree.</p>
1244
1245<p>The <b>parent</b> pointer indicates the directory
1246containing this entry; even when not assembling a persistent tree of
1247nodes the parent entries remain live up to the root of the tree while
1248child nodes are active. At the top of the tree the parent pointer is
1249NULL, meaning the node's name[] is either an absolute path or relative
1250to cwd. The function dirtree_parentfd() gets the directory file descriptor
1251for use with openat() and friends, returning AT_FDCWD at the top of tree.</p>
1252
1253<p>The <b>child</b> pointer points to the first node of the list of contents of
1254this directory. If the directory contains no files, or the entry isn't
1255a directory, child is NULL.</p>
1256
1257<p>The <b>next</b> pointer indicates sibling nodes in the same directory as this
1258node, and since it's the first entry in the struct the llist.c traversal
1259mechanisms work to iterate over sibling nodes. Each dirtree node is a
1260single malloc() (even char *symlink points to memory at the end of the node),
1261so llist_free() works but its callback must descend into child nodes (freeing
1262a tree, not just a linked list), plus whatever the user stored in extra.</p>
1263
1264<p>The <b>dirtree_read</b>() function is a simple wrapper, calling <b>dirtree_add_node</b>()
1265to create a root node relative to the current directory, then calling
1266<b>handle_callback</b>() on that node (which recurses as instructed by the callback
1267return flags). Some commands (such as chgrp) bypass this wrapper, for example
1268to control whether or not to follow symlinks to the root node; symlinks
1269listed on the command line are often treated differently than symlinks
1270encountered during recursive directory traversal).
1271
1272<p>The ls command not only bypasses the wrapper, but never returns
1273<b>DIRTREE_RECURSE</b> from the callback, instead calling <b>dirtree_recurse</b>() manually
1274from elsewhere in the program. This gives ls -lR manual control
1275of traversal order, which is neither depth first nor breadth first but
1276instead a sort of FIFO order requried by the ls standard.</p>
1277
Rob Landley7eaf4f52014-04-09 08:30:09 -05001278<a name="toys">
1279<h1><a href="#toys">Directory toys/</a></h1>
Rob Landleyc0e56ed2012-10-08 00:02:30 -05001280
1281<p>This directory contains command implementations. Each command is a single
1282self-contained file. Adding a new command involves adding a single
1283file, and removing a command involves removing that file. Commands use
1284shared infrastructure from the lib/ and generated/ directories.</p>
1285
Rob Landley52370152014-09-20 14:11:59 -05001286<p>Currently there are five subdirectories under "toys/" containing "posix"
1287commands described in POSIX-2008, "lsb" commands described in the Linux
1288Standard Base 4.1, "other" commands not described by either standard,
1289"pending" commands awaiting cleanup (which default to "n" in menuconfig
1290because they don't necessarily work right yet), and "example" code showing
1291how toybox infrastructure works and providing template/skeleton files to
1292start new commands.</p>
Rob Landleyc0e56ed2012-10-08 00:02:30 -05001293
Rob Landley52370152014-09-20 14:11:59 -05001294<p>The only difference directory location makes is which menu the command
1295shows up in during "make menuconfig", the directories are otherwise identical.
1296Note that the commands exist within a single namespace at runtime, so you can't
1297have the same command in multiple subdirectories. (The build tries to fail
1298informatively when you do that.)</p>
1299
1300<p>There is one more sub-menus in "make menuconfig" containing global
1301configuration options for toybox. This menu is defined in the top level
1302Config.in.</p>
Rob Landleyc0e56ed2012-10-08 00:02:30 -05001303
1304<p>See <a href="#adding">adding a new command</a> for details on the
1305layout of a command file.</p>
1306
Rob Landley81b899d2007-12-18 02:02:47 -06001307<h2>Directory scripts/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -06001308
Rob Landley1f4f41a2012-10-08 21:31:07 -05001309<p>Build infrastructure. The makefile calls scripts/make.sh for "make"
1310and scripts/install.sh for "make install".</p>
1311
1312<p>There's also a test suite, "make test" calls make/test.sh, which runs all
1313the tests in make/test/*. You can run individual tests via
1314"scripts/test.sh command", or "TEST_HOST=1 scripts/test.sh command" to run
1315that test against the host implementation instead of the toybox one.</p>
1316
Rob Landley4e68de12007-12-13 07:00:27 -06001317<h3>scripts/cfg2files.sh</h3>
1318
1319<p>Run .config through this filter to get a list of enabled commands, which
1320is turned into a list of files in toys via a sed invocation in the top level
1321Makefile.
1322</p>
1323
Rob Landley81b899d2007-12-18 02:02:47 -06001324<h2>Directory kconfig/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -06001325
1326<p>Menuconfig infrastructure copied from the Linux kernel. See the
1327Linux kernel's Documentation/kbuild/kconfig-language.txt</p>
1328
Rob Landley52370152014-09-20 14:11:59 -05001329<!-- todo
Rob Landley66a69d92012-01-16 01:44:17 -06001330
Rob Landley52370152014-09-20 14:11:59 -05001331Better OLDTOY and multiple command explanation. From Config.in:
Rob Landley66a69d92012-01-16 01:44:17 -06001332
Rob Landley52370152014-09-20 14:11:59 -05001333<p>A command with multiple names (or multiple similar commands implemented in
1334the same .c file) should have config symbols prefixed with the name of their
1335C file. I.E. config symbol prefixes are NEWTOY() names. If OLDTOY() names
1336have config symbols they must be options (symbols with an underscore and
1337suffix) to the NEWTOY() name. (See generated/toylist.h)</p>
1338-->
Rob Landleyca733922014-05-19 18:24:35 -05001339
Rob Landley4e68de12007-12-13 07:00:27 -06001340<!--#include file="footer.html" -->