blob: 531539deeca1fcf8d837aa6134cc3bb25c491381 [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 Landley6882ee82008-02-12 18:41:34 -060018<p>The primary goal of toybox is _simple_ code. Small is second,
19speed and lots of features come in somewhere after that. Note that
20environmental dependencies are a type of complexity, so needing other packages
21to build or run is a downside. For example, don't use curses when you can
22output ansi escape sequences instead.</p>
23
Rob Landley4e68de12007-12-13 07:00:27 -060024<p><h1>Infrastructure:</h1></p>
25
Rob Landley7c04f012008-01-20 19:00:16 -060026<p>The toybox source code is in following directories:</p>
27<ul>
28<li>The <a href="#top">top level directory</a> contains the file main.c (were
29execution starts), the header file toys.h (included by every command), and
30other global infrastructure.</li>
31<li>The <a href="#lib">lib directory</a> contains common functions shared by
32multiple commands.</li>
33<li>The <a href="#toys">toys directory</a> contains the C files implementating
34each command.</li>
35<li>The <a href="#scripts">scripts directory</a> contains the build and
36test infrastructure.</li>
37<li>The <a href="#kconfig">kconfig directory</a> contains the configuration
38infrastructure implementing menuconfig (copied from the Linux kernel).</li>
39<li>The <a href="#generated">generated directory</a> contains intermediate
40files generated from other parts of the source code.</li>
41</ul>
Rob Landley4e68de12007-12-13 07:00:27 -060042
Rob Landley7c04f012008-01-20 19:00:16 -060043<p><h1>Adding a new command</h1></p>
44<p>To add a new command to toybox, add a C file implementing that command to
45the toys directory. No other files need to be modified; the build extracts
Rob Landley6882ee82008-02-12 18:41:34 -060046all the information it needs (such as command line arguments) from specially
Rob Landley7c04f012008-01-20 19:00:16 -060047formatted comments and macros in the C file. (See the description of the
48<a href="#generated">generated directory</a> for details.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -060049
Rob Landley7c04f012008-01-20 19:00:16 -060050<p>An easy way to start a new command is copy the file "hello.c" to
51the name of the new command, and modify this copy to implement the new command.
Rob Landley6882ee82008-02-12 18:41:34 -060052This file is an example command meant to be used as a "skeleton" for
Rob Landley7c04f012008-01-20 19:00:16 -060053new commands (more or less by turning every instance of "hello" into the
54name of your command, updating the command line arguments, globals, and
55help data, and then filling out its "main" function with code that does
Rob Landley6882ee82008-02-12 18:41:34 -060056something interesting). It provides examples of all the build infrastructure
57(including optional elements like command line argument parsing and global
58variables that a "hello world" program doesn't strictly need).</p>
Rob Landley7c04f012008-01-20 19:00:16 -060059
60<p>Here's a checklist of steps to turn hello.c into another command:</p>
61
62<ul>
63<li><p>First "cd toys" and "cp hello.c yourcommand.c". Note that the name
64of this file is significant, it's the name of the new command you're adding
65to toybox. Open your new file in your favorite editor.</p></li>
66
67<li><p>Change the one line comment at the top of the file (currently
68"hello.c - A hello world program") to describe your new file.</p></li>
69
70<li><p>Change the copyright notice to your name, email, and the current
71year.</p></li>
72
73<li><p>Give a URL to the relevant standards document, or say "Not in SUSv3" if
74there is no relevant standard. (Currently both lines are there, delete
75whichever is appropriate.) The existing link goes to the directory of SUSv3
76command line utility standards on the Open Group's website, where there's often
77a relevant commandname.html file. Feel free to link to other documentation or
78standards as appropriate.</p></li>
79
Rob Landley6882ee82008-02-12 18:41:34 -060080<li><p>Update the USE_YOURCOMMAND(NEWTOY(yourcommand,"blah",0)) line. The
81arguments to newtoy are: 1) the name used to run your command, 2)
82the command line arguments (NULL if none), and additional information such
83as where your command should be installed on a running system. See [TODO] for
84details.</p></li>
Rob Landley7c04f012008-01-20 19:00:16 -060085
86<li><p>Change the kconfig data (from "config YOURCOMMAND" to the end of the
87comment block) to supply your command's configuration and help
88information. The uppper case config symbols are used by menuconfig, and are
89also what the CFG_ and USE_() macros are generated from (see [TODO]). The
90help information here is used by menuconfig, and also by the "help" command to
91describe your new command. (See [TODO] for details.) By convention,
92unfinished commands default to "n" and finished commands default to "y".<p></li>
93
94<li><p>Update the DEFINE_GLOBALS() macro to contain your command's global
95variables, and also change the name "hello" in the #define TT line afterwards
96to the name of your command. If your command has no global variables, delete
97this macro (and the #define TT line afterwards). Note that if you specified
98two-character command line arguments in NEWTOY(), the first few global
99variables will be initialized by the automatic argument parsing logic, and
100the type and order of these variables must correspond to the arguments
101specified in NEWTOY(). See [TODO] for details.</p></li>
102
Rob Landley6882ee82008-02-12 18:41:34 -0600103<li><p>If you didn't delete the DEFINE_GLOBALS macro, change the "#define TT
104this.hello" line to use your command name in place of the "hello". This is a
105shortcut to access your global variables as if they were members of the global
106struct "TT". (Access these members with a period ".", not a right arrow
107"->".)</p></li>
Rob Landley7c04f012008-01-20 19:00:16 -0600108
109<li><p>Rename hello_main() to yourcommand_main(). This is the main() function
Rob Landley6882ee82008-02-12 18:41:34 -0600110where execution of your command starts. See [TODO] to figure out what
Rob Landley7c04f012008-01-20 19:00:16 -0600111happened to your command line arguments and how to access them.</p></li>
112</ul>
113
114<p><a name="top" /><h2>Top level directory.</h2></p>
115
116<p>This directory contains global infrastructure.
Rob Landley4e68de12007-12-13 07:00:27 -0600117
118<h3>main.c</h3>
119<p>Contains the main() function where execution starts, plus
120common infrastructure to initialize global variables and select which command
Rob Landley6882ee82008-02-12 18:41:34 -0600121to run. The "toybox" multiplexer command also lives here. (This is the
Rob Landley7c04f012008-01-20 19:00:16 -0600122only command defined outside of the toys directory.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600123
Rob Landley6882ee82008-02-12 18:41:34 -0600124<p>Execution starts in main() which trims any path off of the first command
125name and calls toybox_main(), which calls toy_exec(), which calls toy_find()
126and toy_init() before calling the appropriate command's function from toy_list.
127If the command is "toybox", execution recurses into toybox_main(), otherwise
128the call goes to the appropriate commandname_main() from a C file in the toys
129directory.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600130
Rob Landley6882ee82008-02-12 18:41:34 -0600131<p>The following global variables are defined in main.c:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600132<ul>
133<li><p>struct toy_list <b>toy_list[]</b> - array describing all the
134commands currently configured into toybox. The first entry (toy_list[0]) is
135for the "toybox" multiplexer command, which runs all the other built-in commands
136without symlinks by using its first argument as the name of the command to
137run and the rest as that command's argument list (ala "./toybox echo hello").
138The remaining entries are the commands in alphabetical order (for efficient
139binary search).</p>
140
141<p>This is a read-only array initialized at compile time by
Rob Landley6882ee82008-02-12 18:41:34 -0600142defining macros and #including generated/newtoys.h.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600143
144<p>Members of struct toy_list include:</p>
145<ul>
146<li><p>char *<b>name</b> - the name of this command.</p></li>
147<li><p>void (*<b>toy_main</b>)(void) - function pointer to run this
148command.</p></li>
149<li><p>char *<b>options</b> - command line option string (used by
150get_optflags() in lib/args.c to intialize toys.optflags, toys.optargs, and
151entries in the toy union). If this is NULL, no option parsing is done before
152calling toy_main().</p></li>
Rob Landley6882ee82008-02-12 18:41:34 -0600153<li><p>int <b>flags</b> - Behavior flags for this command. The following flags are currently understood:</p>
154
155<ul>
156<li><b>TOYFLAG_USR</b> - Install this command under /usr</li>
157<li><b>TOYFLAG_BIN</b> - Install this command under /bin</li>
158<li><b>TOYFLAG_SBIN</b> - Install this command under /sbin</li>
159<li><b>TOYFLAG_NOFORK</b> - This command can be used as a shell builtin.</li>
160<li><b>TOYFLAG_UMASK</b> - Call umask(0) before running this command.</li>
161</ul>
162<br>
163
164<p>These flags are combined with | (or). For example, to install a command
165in /usr/bin, or together TOYFLAG_USR|TOYFLAG_BIN.</p>
166</ul>
Rob Landley4e68de12007-12-13 07:00:27 -0600167</li>
168
169<li><p>struct toy_context <b>toys</b> - global structure containing information
170common to all commands, initializd by toy_init(). Members of this structure
171include:</p>
172<ul>
173<li><p>struct toy_list *<b>which</b> - a pointer to this command's toy_list
174structure. Mostly used to grab the name of the running command
175(toys->which.name).</p>
176</li>
177<li><p>int <b>exitval</b> - Exit value of this command. Defaults to zero. The
178error_exit() functions will return 1 if this is zero, otherwise they'll
179return this value.</p></li>
180<li><p>char **<b>argv</b> - "raw" command line options, I.E. the original
181unmodified string array passed in to main(). Note that modifying this changes
182"ps" output, and is not recommended.</p>
183<p>Most commands don't use this field, instead the use optargs, optflags,
184and the fields in the toy union initialized by get_optflags().</p>
185</li>
186<li><p>unsigned <b>optflags</b> - Command line option flags, set by
187get_optflags(). Indicates which of the command line options listed in
Rob Landley6882ee82008-02-12 18:41:34 -0600188toys->which.options occurred this time.</p>
189
190<p>The rightmost command line argument listed in toys->which.options sets bit
1911, the next one sets bit 2, and so on. This means the bits are set in the same
192order the binary digits would be listed if typed out as a string. For example,
193the option string "abcd" would parse the command line "-c" to set optflags to 2,
194"-a" would set optflags to 8, and "-bd" would set optflags to 6 (4|2).</p>
195
196<p>Only letters are relevant to optflags. In the string "a*b:c#d", d=1, c=2,
197b=4, a=8. The punctuation after a letter initializes global variables
198(see [TODO] DECLARE_GLOBALS() for details).</p>
199
200<p>For more information on option parsing, see [TODO] get_optflags().</p>
201
202</li>
Rob Landley4e68de12007-12-13 07:00:27 -0600203<li><p>char **<b>optargs</b> - Null terminated array of arguments left over
204after get_optflags() removed all the ones it understood. Note: optarg[0] is
205the first argument, not the command name. Use toys.which->name for the command
206name.</p></li>
Rob Landley6882ee82008-02-12 18:41:34 -0600207<li><p>int <b>optc</b> - Optarg count, equivalent to argc but for
208optargs[].<p></li>
Rob Landley4e68de12007-12-13 07:00:27 -0600209<li><p>int <b>exithelp</b> - Whether error_exit() should print a usage message
210via help_main() before exiting. (True during option parsing, defaults to
211false afterwards.)</p></li>
212</ul><br>
213
Rob Landley6882ee82008-02-12 18:41:34 -0600214<li><p>union toy_union <b>this</b> - Union of structures containing each
Rob Landley4e68de12007-12-13 07:00:27 -0600215command's global variables.</p>
216
Rob Landley6882ee82008-02-12 18:41:34 -0600217<p>Global variables are useful: they reduce the overhead of passing extra
218command line arguments between functions, they conveniently start prezeroed to
219save initialization costs, and the command line argument parsing infrastructure
220can also initialize global variables with its results.</p>
221
222<p>But since each toybox process can only run one command at a time, allocating
223space for global variables belonging to other commands you aren't currently
224running would be wasteful.</p>
225
226<p>Toybox handles this by encapsulating each command's global variables in
227a structure, and declaring a union of those structures. The DECLARE_GLOBALS()
228macro contains the global variables that should go in a command's global
229structure. Each variable can then be accessed as "this.commandname.varname".
230Generally, the macro TT is #defined to this.commandname so the variable
231can then be accessed as "TT.variable".</p>
232
233A command that needs global variables should declare a structure to
Rob Landley4e68de12007-12-13 07:00:27 -0600234contain them all, and add that structure to this union. A command should never
235declare global variables outside of this, because such global variables would
236allocate memory when running other commands that don't use those global
237variables.</p>
238
239<p>The first few fields of this structure can be intialized by get_optargs(),
240as specified by the options field off this command's toy_list entry. See
241the get_optargs() description in lib/args.c for details.</p>
242</li>
243
Rob Landley81b899d2007-12-18 02:02:47 -0600244<li><b>char toybuf[4096]</b> - a common scratch space buffer so
Rob Landley4e68de12007-12-13 07:00:27 -0600245commands don't need to allocate their own. Any command is free to use this,
246and it should never be directly referenced by functions in lib/ (although
247commands are free to pass toybuf in to a library function as an argument).</li>
248</ul>
249
Rob Landley6882ee82008-02-12 18:41:34 -0600250<p>The following functions are defined in main.c:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600251<ul>
252<li><p>struct toy_list *<b>toy_find</b>(char *name) - Return the toy_list
253structure for this command name, or NULL if not found.</p></li>
Rob Landley81b899d2007-12-18 02:02:47 -0600254<li><p>void <b>toy_init</b>(struct toy_list *which, char *argv[]) - fill out
255the global toys structure, calling get_optargs() if necessary.</p></li>
Rob Landley6882ee82008-02-12 18:41:34 -0600256<li><p>void <b>toy_exec</b>(char *argv[]) - Run a built-in command with
257arguments.</p>
258<p>Calls toy_find() on argv[0] (which must be just a command name
Rob Landley4e68de12007-12-13 07:00:27 -0600259without path). Returns if it can't find this command, otherwise calls
Rob Landley6882ee82008-02-12 18:41:34 -0600260toy_init(), toys->which.toy_main(), and exit() instead of returning.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600261
Rob Landley6882ee82008-02-12 18:41:34 -0600262<p>Use the library function xexec() to fall back to external executables
263in $PATH if toy_exec() can't find a built-in command. Note that toy_exec()
264does not strip paths before searching for a command, so "./command" will
265never match an internal command.</li>
266
267<li><p>void <b>toybox_main</b>(void) - the main function for the multiplexer
268command (I.E. "toybox"). Given a command name as its first argument, calls
269toy_exec() on its arguments. With no arguments, it lists available commands.
270If the first argument starts with "-" it lists each command with its default
271install path prepended.</p></li>
Rob Landley4e68de12007-12-13 07:00:27 -0600272
273</ul>
274
275<h3>Config.in</h3>
276
277<p>Top level configuration file in a stylized variant of
Rob Landley6882ee82008-02-12 18:41:34 -0600278<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 -0600279
280<p>These files are directly used by "make menuconfig" to select which commands
281to build into toybox (thus generating a .config file), and by
Rob Landley6882ee82008-02-12 18:41:34 -0600282scripts/config2help.py to create generated/help.h.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600283
284<h3>Temporary files:</h3>
285
Rob Landley6882ee82008-02-12 18:41:34 -0600286<p>There is one temporary file in the top level source directory:</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600287<ul>
288<li><p><b>.config</b> - Configuration file generated by kconfig, indicating
289which commands (and options to commands) are currently enabled. Used
Rob Landley6882ee82008-02-12 18:41:34 -0600290to make generated/config.h and determine which toys/*.c files to build.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600291
Rob Landley6882ee82008-02-12 18:41:34 -0600292<p>You can create a human readable "miniconfig" version of this file using
293<a href=http://landley.net/code/firmware/new_platform.html#miniconfig>these
294instructions</a>.</p>
295</li>
296</ul>
297
298<p>The "generated/" directory contains files generated from other source code
299in toybox. All of these files can be recreated by the build system, although
300some (such as generated/help.h) are shipped in release versions to reduce
301environmental dependencies (I.E. so you don't need python on your build
302system).</p>
303
304<ul>
305<li><p><b>generated/config.h</b> - list of CFG_SYMBOL and USE_SYMBOL() macros,
Rob Landley4e68de12007-12-13 07:00:27 -0600306generated from .config by a sed invocation in the top level Makefile.</p>
307
308<p>CFG_SYMBOL is a comple time constant set to 1 for enabled symbols and 0 for
Rob Landley6882ee82008-02-12 18:41:34 -0600309disabled symbols. This allows the use of normal if() statements to remove
310code at compile time via the optimizer's dead code elimination (which removes
311from the binary any code that cannot be reached). This saves space without
Rob Landley4e68de12007-12-13 07:00:27 -0600312cluttering the code with #ifdefs or leading to configuration dependent build
313breaks. (See the 1992 Usenix paper
314<a href=http://www.chris-lott.org/resources/cstyle/ifdefs.pdf>#ifdef
315Considered Harmful</a> for more information.)</p>
316
317<p>USE_SYMBOL(code) evaluates to the code in parentheses when the symbol
318is enabled, and nothing when the symbol is disabled. This can be used
319for things like varargs or variable declarations which can't always be
Rob Landley6882ee82008-02-12 18:41:34 -0600320eliminated by a simple test on CFG_SYMBOL. Note that
Rob Landley4e68de12007-12-13 07:00:27 -0600321(unlike CFG_SYMBOL) this is really just a variant of #ifdef, and can
322still result in configuration dependent build breaks. Use with caution.</p>
323</li>
324</ul>
325
Rob Landley81b899d2007-12-18 02:02:47 -0600326<p><h2>Directory toys/</h2></p>
Rob Landley4e68de12007-12-13 07:00:27 -0600327
328<h3>toys/Config.in</h3>
329
330<p>Included from the top level Config.in, contains one or more
331configuration entries for each command.</p>
332
Rob Landley81b899d2007-12-18 02:02:47 -0600333<p>Each command has a configuration entry matching the command name (although
334configuration symbols are uppercase and command names are lower case).
Rob Landley4e68de12007-12-13 07:00:27 -0600335Options to commands start with the command name followed by an underscore and
336the option name. Global options are attachd to the "toybox" command,
337and thus use the prefix "TOYBOX_". This organization is used by
Rob Landley81b899d2007-12-18 02:02:47 -0600338scripts/cfg2files to select which toys/*.c files to compile for a given
339.config.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600340
341<p>A commands with multiple names (or multiple similar commands implemented in
342the same .c file) should have config symbols prefixed with the name of their
343C file. I.E. config symbol prefixes are NEWTOY() names. If OLDTOY() names
344have config symbols they're options (symbols with an underscore and suffix)
345to the NEWTOY() name. (See toys/toylist.h)</p>
346
347<h3>toys/toylist.h</h3>
Rob Landley81b899d2007-12-18 02:02:47 -0600348<p>The first half of this file prototypes all the structures to hold
Rob Landleyda09b7f2007-12-20 06:29:59 -0600349global variables for each command, and puts them in toy_union. These
350prototypes are only included if the macro NEWTOY isn't defined (in which
351case NEWTOY is defined to a default value that produces function
352prototypes).</p>
Rob Landley81b899d2007-12-18 02:02:47 -0600353
Rob Landleyda09b7f2007-12-20 06:29:59 -0600354<p>The second half of this file lists all the commands in alphabetical
355order, along with their command line arguments and install location.
356Each command has an appropriate configuration guard so only the commands that
357are enabled wind up in the list.</p>
358
359<p>The first time this header is #included, it defines structures and
360produces function prototypes for the commands in the toys directory.</p>
361
362
363<p>The first time it's included, it defines structures and produces function
364prototypes.
365 This
Rob Landley81b899d2007-12-18 02:02:47 -0600366is used to initialize toy_list in main.c, and later in that file to initialize
367NEED_OPTIONS (to figure out whether the command like parsing logic is needed),
368and to put the help entries in the right order in toys/help.c.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600369
370<h3>toys/help.h</h3>
371
372<p>#defines two help text strings for each command: a single line
373command_help and an additinal command_help_long. This is used by help_main()
374in toys/help.c to display help for commands.</p>
375
376<p>Although this file is generated from Config.in help entries by
377scripts/config2help.py, it's shipped in release tarballs so you don't need
378python on the build system. (If you check code out of source control, or
379modify Config.in, then you'll need python installed to rebuild it.)</p>
380
381<p>This file contains help for all commands, regardless of current
382configuration, but only the currently enabled ones are entered into help_data[]
383in toys/help.c.</p>
384
Rob Landley81b899d2007-12-18 02:02:47 -0600385<h2>Directory lib/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600386
Rob Landley7c04f012008-01-20 19:00:16 -0600387<p>lib: llist, getmountlist(), error_msg/error_exit, xmalloc(),
388strlcpy(), xexec(), xopen()/xread(), xgetcwd(), xabspath(), find_in_path(),
389itoa().</p>
390
391
392
Rob Landley81b899d2007-12-18 02:02:47 -0600393<h2>Directory scripts/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600394
395<h3>scripts/cfg2files.sh</h3>
396
397<p>Run .config through this filter to get a list of enabled commands, which
398is turned into a list of files in toys via a sed invocation in the top level
399Makefile.
400</p>
401
Rob Landley81b899d2007-12-18 02:02:47 -0600402<h2>Directory kconfig/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600403
404<p>Menuconfig infrastructure copied from the Linux kernel. See the
405Linux kernel's Documentation/kbuild/kconfig-language.txt</p>
406
407<!--#include file="footer.html" -->