blob: f92fcc778c84b291676d9b49e28cf9c5fa49f749 [file] [log] [blame]
Rob Landley4e68de12007-12-13 07:00:27 -06001<!--#include file="header.html" -->
2
Rob Landley5a0660f2007-12-27 21:36:44 -06003<p><h1>Code style</h1></p>
4
5<p>Toybox source is formatted to be read with 4-space tab stops. Each file
6starts with a special comment telling vi to set the tab stop to 4. Note that
7one of the bugs in Ubuntu 7.10 broke vi's ability to parse these comments; you
8must either rebuild vim from source, or go ":ts=4" yourself each time you load
9the file.</p>
10
11<p>Gotos are allowed for error handling, and for breaking out of
12nested loops. In general, a goto should only jump forward (not back), and
13should either jump to the end of an outer loop, or to error handling code
14at the end of the function. Goto labels are never indented: they override the
15block structure of the file. Putting them at the left edge makes them easy
16to spot as overrides to the normal flow of control, which they are.</p>
17
Rob Landley4e68de12007-12-13 07:00:27 -060018<p><h1>Infrastructure:</h1></p>
19
20<p>The toybox source code is in three directories. The top level directory
21contains the file main.c and the header file toys.h. The "lib" directory
22contains generic functions shared by multiple commands. The "toys" directory
23contains the implementations of individual commands.</p>
24
25<p><h2>Top level directory.</h2></p>
26
27<p>lib: llist, getmountlist(), error_msg/error_exit, xmalloc(),
28strlcpy(), xexec(), xopen()/xread(), xgetcwd(), xabspath(), find_in_path(),
29itoa().</p>
30
31<h3>main.c</h3>
32<p>Contains the main() function where execution starts, plus
33common infrastructure to initialize global variables and select which command
34to run.</p>
35
36<p>Execution starts in main() which removes the path from the first command
37name and calls toybox_main(), which calls toy_exec(), which calls toy_find(),
38toy_init() and the appropriate command's function from toy_list.</p>
39
40<p>The following global variables are defined here:</p>
41<ul>
42<li><p>struct toy_list <b>toy_list[]</b> - array describing all the
43commands currently configured into toybox. The first entry (toy_list[0]) is
44for the "toybox" multiplexer command, which runs all the other built-in commands
45without symlinks by using its first argument as the name of the command to
46run and the rest as that command's argument list (ala "./toybox echo hello").
47The remaining entries are the commands in alphabetical order (for efficient
48binary search).</p>
49
50<p>This is a read-only array initialized at compile time by
51defining macros and #including toys/toylist.h.</p>
52
53<p>Members of struct toy_list include:</p>
54<ul>
55<li><p>char *<b>name</b> - the name of this command.</p></li>
56<li><p>void (*<b>toy_main</b>)(void) - function pointer to run this
57command.</p></li>
58<li><p>char *<b>options</b> - command line option string (used by
59get_optflags() in lib/args.c to intialize toys.optflags, toys.optargs, and
60entries in the toy union). If this is NULL, no option parsing is done before
61calling toy_main().</p></li>
62<li><p>int <b>flags</b> - Behavior flags such as where to install this command
63(in usr/bin/sbin) and whether this is a shell builtin (NOFORK) or a standalone
64command.</p></li>
65</ul><br>
66</li>
67
68<li><p>struct toy_context <b>toys</b> - global structure containing information
69common to all commands, initializd by toy_init(). Members of this structure
70include:</p>
71<ul>
72<li><p>struct toy_list *<b>which</b> - a pointer to this command's toy_list
73structure. Mostly used to grab the name of the running command
74(toys->which.name).</p>
75</li>
76<li><p>int <b>exitval</b> - Exit value of this command. Defaults to zero. The
77error_exit() functions will return 1 if this is zero, otherwise they'll
78return this value.</p></li>
79<li><p>char **<b>argv</b> - "raw" command line options, I.E. the original
80unmodified string array passed in to main(). Note that modifying this changes
81"ps" output, and is not recommended.</p>
82<p>Most commands don't use this field, instead the use optargs, optflags,
83and the fields in the toy union initialized by get_optflags().</p>
84</li>
85<li><p>unsigned <b>optflags</b> - Command line option flags, set by
86get_optflags(). Indicates which of the command line options listed in
87toys->which.options were seen this time. See get_optflags() for
88details.</p></li>
89<li><p>char **<b>optargs</b> - Null terminated array of arguments left over
90after get_optflags() removed all the ones it understood. Note: optarg[0] is
91the first argument, not the command name. Use toys.which->name for the command
92name.</p></li>
93<li><p>int <b>exithelp</b> - Whether error_exit() should print a usage message
94via help_main() before exiting. (True during option parsing, defaults to
95false afterwards.)</p></li>
96</ul><br>
97
98<li><p>union toy_union <b>toy</b> - Union of structures containing each
99command's global variables.</p>
100
101<p>A command that needs global variables should declare a structure to
102contain them all, and add that structure to this union. A command should never
103declare global variables outside of this, because such global variables would
104allocate memory when running other commands that don't use those global
105variables.</p>
106
107<p>The first few fields of this structure can be intialized by get_optargs(),
108as specified by the options field off this command's toy_list entry. See
109the get_optargs() description in lib/args.c for details.</p>
110</li>
111
Rob Landley81b899d2007-12-18 02:02:47 -0600112<li><b>char toybuf[4096]</b> - a common scratch space buffer so
Rob Landley4e68de12007-12-13 07:00:27 -0600113commands don't need to allocate their own. Any command is free to use this,
114and it should never be directly referenced by functions in lib/ (although
115commands are free to pass toybuf in to a library function as an argument).</li>
116</ul>
117
118<p>The following functions are defined here:</p>
119<ul>
120<li><p>struct toy_list *<b>toy_find</b>(char *name) - Return the toy_list
121structure for this command name, or NULL if not found.</p></li>
Rob Landley81b899d2007-12-18 02:02:47 -0600122<li><p>void <b>toy_init</b>(struct toy_list *which, char *argv[]) - fill out
123the global toys structure, calling get_optargs() if necessary.</p></li>
Rob Landley4e68de12007-12-13 07:00:27 -0600124<li><p>void <b>toy_exec</b>(char *argv[]) - Run a built-in command with arguments.
125Calls toy_find() on the first argument (which must be just a command name
126without path). Returns if it can't find this command, otherwise calls
127toy_init(), toys->which.toy_main(), and exit() instead of returning.</p></li>
128
129<li><p>void <b>toybox_main</b>(void) - the main function for multiplexer
130command. Given a command name as its first argument, calls toy_exec() on its
131arguments. With no arguments, it lists available commands. If the first
132argument starts with "-" it lists each command with its default install
133path prepended.</p></li>
134
135</ul>
136
137<h3>Config.in</h3>
138
139<p>Top level configuration file in a stylized variant of
140<a href=http://kernel.org/doc/Documentation/kbuild/kconfig-language.txt>kconfig</a> format. Includes toys/Config.in.</p>
141
142<p>These files are directly used by "make menuconfig" to select which commands
143to build into toybox (thus generating a .config file), and by
144scripts/config2help.py to generate toys/help.h.</p>
145
146<h3>Temporary files:</h3>
147
148<ul>
149<li><p><b>.config</b> - Configuration file generated by kconfig, indicating
150which commands (and options to commands) are currently enabled. Used
151to generate gen_config.h and the toys/*.c dependency list.</p></li>
152
153<li><p><b>gen_config.h</b> - list of CFG_SYMBOL and USE_SYMBOL() macros,
154generated from .config by a sed invocation in the top level Makefile.</p>
155
156<p>CFG_SYMBOL is a comple time constant set to 1 for enabled symbols and 0 for
157disabled symbols. This can be used via normal if() statements to remove
158code at compile time via the optimizer's dead code elimination, which removes
159from the binary any code that cannot be reached. This saves space without
160cluttering the code with #ifdefs or leading to configuration dependent build
161breaks. (See the 1992 Usenix paper
162<a href=http://www.chris-lott.org/resources/cstyle/ifdefs.pdf>#ifdef
163Considered Harmful</a> for more information.)</p>
164
165<p>USE_SYMBOL(code) evaluates to the code in parentheses when the symbol
166is enabled, and nothing when the symbol is disabled. This can be used
167for things like varargs or variable declarations which can't always be
168eliminated by a compile time removalbe test on CFG_SYMBOL. Note that
169(unlike CFG_SYMBOL) this is really just a variant of #ifdef, and can
170still result in configuration dependent build breaks. Use with caution.</p>
171</li>
172</ul>
173
Rob Landley81b899d2007-12-18 02:02:47 -0600174<p><h2>Directory toys/</h2></p>
Rob Landley4e68de12007-12-13 07:00:27 -0600175
176<h3>toys/Config.in</h3>
177
178<p>Included from the top level Config.in, contains one or more
179configuration entries for each command.</p>
180
Rob Landley81b899d2007-12-18 02:02:47 -0600181<p>Each command has a configuration entry matching the command name (although
182configuration symbols are uppercase and command names are lower case).
Rob Landley4e68de12007-12-13 07:00:27 -0600183Options to commands start with the command name followed by an underscore and
184the option name. Global options are attachd to the "toybox" command,
185and thus use the prefix "TOYBOX_". This organization is used by
Rob Landley81b899d2007-12-18 02:02:47 -0600186scripts/cfg2files to select which toys/*.c files to compile for a given
187.config.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600188
189<p>A commands with multiple names (or multiple similar commands implemented in
190the same .c file) should have config symbols prefixed with the name of their
191C file. I.E. config symbol prefixes are NEWTOY() names. If OLDTOY() names
192have config symbols they're options (symbols with an underscore and suffix)
193to the NEWTOY() name. (See toys/toylist.h)</p>
194
195<h3>toys/toylist.h</h3>
Rob Landley81b899d2007-12-18 02:02:47 -0600196<p>The first half of this file prototypes all the structures to hold
Rob Landleyda09b7f2007-12-20 06:29:59 -0600197global variables for each command, and puts them in toy_union. These
198prototypes are only included if the macro NEWTOY isn't defined (in which
199case NEWTOY is defined to a default value that produces function
200prototypes).</p>
Rob Landley81b899d2007-12-18 02:02:47 -0600201
Rob Landleyda09b7f2007-12-20 06:29:59 -0600202<p>The second half of this file lists all the commands in alphabetical
203order, along with their command line arguments and install location.
204Each command has an appropriate configuration guard so only the commands that
205are enabled wind up in the list.</p>
206
207<p>The first time this header is #included, it defines structures and
208produces function prototypes for the commands in the toys directory.</p>
209
210
211<p>The first time it's included, it defines structures and produces function
212prototypes.
213 This
Rob Landley81b899d2007-12-18 02:02:47 -0600214is used to initialize toy_list in main.c, and later in that file to initialize
215NEED_OPTIONS (to figure out whether the command like parsing logic is needed),
216and to put the help entries in the right order in toys/help.c.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600217
218<h3>toys/help.h</h3>
219
220<p>#defines two help text strings for each command: a single line
221command_help and an additinal command_help_long. This is used by help_main()
222in toys/help.c to display help for commands.</p>
223
224<p>Although this file is generated from Config.in help entries by
225scripts/config2help.py, it's shipped in release tarballs so you don't need
226python on the build system. (If you check code out of source control, or
227modify Config.in, then you'll need python installed to rebuild it.)</p>
228
229<p>This file contains help for all commands, regardless of current
230configuration, but only the currently enabled ones are entered into help_data[]
231in toys/help.c.</p>
232
Rob Landley81b899d2007-12-18 02:02:47 -0600233<h2>Directory lib/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600234
Rob Landley81b899d2007-12-18 02:02:47 -0600235<h2>Directory scripts/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600236
237<h3>scripts/cfg2files.sh</h3>
238
239<p>Run .config through this filter to get a list of enabled commands, which
240is turned into a list of files in toys via a sed invocation in the top level
241Makefile.
242</p>
243
Rob Landley81b899d2007-12-18 02:02:47 -0600244<h2>Directory kconfig/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600245
246<p>Menuconfig infrastructure copied from the Linux kernel. See the
247Linux kernel's Documentation/kbuild/kconfig-language.txt</p>
248
249<!--#include file="footer.html" -->