blob: 06c777d9c2c5b7a32a8a471f48eab287efa62147 [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
Rob Landley7c04f012008-01-20 19:00:16 -060020<p>The toybox source code is in following directories:</p>
21<ul>
22<li>The <a href="#top">top level directory</a> contains the file main.c (were
23execution starts), the header file toys.h (included by every command), and
24other global infrastructure.</li>
25<li>The <a href="#lib">lib directory</a> contains common functions shared by
26multiple commands.</li>
27<li>The <a href="#toys">toys directory</a> contains the C files implementating
28each command.</li>
29<li>The <a href="#scripts">scripts directory</a> contains the build and
30test infrastructure.</li>
31<li>The <a href="#kconfig">kconfig directory</a> contains the configuration
32infrastructure implementing menuconfig (copied from the Linux kernel).</li>
33<li>The <a href="#generated">generated directory</a> contains intermediate
34files generated from other parts of the source code.</li>
35</ul>
Rob Landley4e68de12007-12-13 07:00:27 -060036
Rob Landley7c04f012008-01-20 19:00:16 -060037<p><h1>Adding a new command</h1></p>
38<p>To add a new command to toybox, add a C file implementing that command to
39the toys directory. No other files need to be modified; the build extracts
40other information it needs (such as command line arguments) from specially
41formatted comments and macros in the C file. (See the description of the
42<a href="#generated">generated directory</a> for details.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -060043
Rob Landley7c04f012008-01-20 19:00:16 -060044<p>An easy way to start a new command is copy the file "hello.c" to
45the name of the new command, and modify this copy to implement the new command.
46This file is a small, simple command meant to be used as a "skeleton" for
47new commands (more or less by turning every instance of "hello" into the
48name of your command, updating the command line arguments, globals, and
49help data, and then filling out its "main" function with code that does
50something interesting).</p>
51
52<p>Here's a checklist of steps to turn hello.c into another command:</p>
53
54<ul>
55<li><p>First "cd toys" and "cp hello.c yourcommand.c". Note that the name
56of this file is significant, it's the name of the new command you're adding
57to toybox. Open your new file in your favorite editor.</p></li>
58
59<li><p>Change the one line comment at the top of the file (currently
60"hello.c - A hello world program") to describe your new file.</p></li>
61
62<li><p>Change the copyright notice to your name, email, and the current
63year.</p></li>
64
65<li><p>Give a URL to the relevant standards document, or say "Not in SUSv3" if
66there is no relevant standard. (Currently both lines are there, delete
67whichever is appropriate.) The existing link goes to the directory of SUSv3
68command line utility standards on the Open Group's website, where there's often
69a relevant commandname.html file. Feel free to link to other documentation or
70standards as appropriate.</p></li>
71
72<li><p>Update the USE_YOURCOMMAND(NEWTOY(yourcommand,NULL,0)) line. This
73specifies the name used to run your command, the command line arguments (NULL
74if none), and where your command should be installed on a running system. See
75[TODO] for details.</p></li>
76
77<li><p>Change the kconfig data (from "config YOURCOMMAND" to the end of the
78comment block) to supply your command's configuration and help
79information. The uppper case config symbols are used by menuconfig, and are
80also what the CFG_ and USE_() macros are generated from (see [TODO]). The
81help information here is used by menuconfig, and also by the "help" command to
82describe your new command. (See [TODO] for details.) By convention,
83unfinished commands default to "n" and finished commands default to "y".<p></li>
84
85<li><p>Update the DEFINE_GLOBALS() macro to contain your command's global
86variables, and also change the name "hello" in the #define TT line afterwards
87to the name of your command. If your command has no global variables, delete
88this macro (and the #define TT line afterwards). Note that if you specified
89two-character command line arguments in NEWTOY(), the first few global
90variables will be initialized by the automatic argument parsing logic, and
91the type and order of these variables must correspond to the arguments
92specified in NEWTOY(). See [TODO] for details.</p></li>
93
94<li><p>Change the "#define TT this.hello" line to use your command name in
95place of the "hello". This is a shortcut to access your global variables
96as if they were members of the global struct "TT". (Access these members with
97a period ".", not a right arrow "->".)</p></li>
98
99<li><p>Rename hello_main() to yourcommand_main(). This is the main() function
100where execution off your command starts. See [TODO] to figure out what
101happened to your command line arguments and how to access them.</p></li>
102</ul>
103
104<p><a name="top" /><h2>Top level directory.</h2></p>
105
106<p>This directory contains global infrastructure.
Rob Landley4e68de12007-12-13 07:00:27 -0600107
108<h3>main.c</h3>
109<p>Contains the main() function where execution starts, plus
110common infrastructure to initialize global variables and select which command
Rob Landley7c04f012008-01-20 19:00:16 -0600111to run. The "toybox" multiplexer command is also defined here. (This is the
112only command defined outside of the toys directory.)</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600113
114<p>Execution starts in main() which removes the path from the first command
115name and calls toybox_main(), which calls toy_exec(), which calls toy_find(),
Rob Landley7c04f012008-01-20 19:00:16 -0600116toy_init() and the appropriate command's function from toy_list. If
117the command is "toybox", execution returns to toybox_main(), otherwise
118the call goes to the appropriate command_main() from the toys directory.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600119
120<p>The following global variables are defined here:</p>
121<ul>
122<li><p>struct toy_list <b>toy_list[]</b> - array describing all the
123commands currently configured into toybox. The first entry (toy_list[0]) is
124for the "toybox" multiplexer command, which runs all the other built-in commands
125without symlinks by using its first argument as the name of the command to
126run and the rest as that command's argument list (ala "./toybox echo hello").
127The remaining entries are the commands in alphabetical order (for efficient
128binary search).</p>
129
130<p>This is a read-only array initialized at compile time by
131defining macros and #including toys/toylist.h.</p>
132
133<p>Members of struct toy_list include:</p>
134<ul>
135<li><p>char *<b>name</b> - the name of this command.</p></li>
136<li><p>void (*<b>toy_main</b>)(void) - function pointer to run this
137command.</p></li>
138<li><p>char *<b>options</b> - command line option string (used by
139get_optflags() in lib/args.c to intialize toys.optflags, toys.optargs, and
140entries in the toy union). If this is NULL, no option parsing is done before
141calling toy_main().</p></li>
142<li><p>int <b>flags</b> - Behavior flags such as where to install this command
143(in usr/bin/sbin) and whether this is a shell builtin (NOFORK) or a standalone
144command.</p></li>
145</ul><br>
146</li>
147
148<li><p>struct toy_context <b>toys</b> - global structure containing information
149common to all commands, initializd by toy_init(). Members of this structure
150include:</p>
151<ul>
152<li><p>struct toy_list *<b>which</b> - a pointer to this command's toy_list
153structure. Mostly used to grab the name of the running command
154(toys->which.name).</p>
155</li>
156<li><p>int <b>exitval</b> - Exit value of this command. Defaults to zero. The
157error_exit() functions will return 1 if this is zero, otherwise they'll
158return this value.</p></li>
159<li><p>char **<b>argv</b> - "raw" command line options, I.E. the original
160unmodified string array passed in to main(). Note that modifying this changes
161"ps" output, and is not recommended.</p>
162<p>Most commands don't use this field, instead the use optargs, optflags,
163and the fields in the toy union initialized by get_optflags().</p>
164</li>
165<li><p>unsigned <b>optflags</b> - Command line option flags, set by
166get_optflags(). Indicates which of the command line options listed in
167toys->which.options were seen this time. See get_optflags() for
168details.</p></li>
169<li><p>char **<b>optargs</b> - Null terminated array of arguments left over
170after get_optflags() removed all the ones it understood. Note: optarg[0] is
171the first argument, not the command name. Use toys.which->name for the command
172name.</p></li>
173<li><p>int <b>exithelp</b> - Whether error_exit() should print a usage message
174via help_main() before exiting. (True during option parsing, defaults to
175false afterwards.)</p></li>
176</ul><br>
177
178<li><p>union toy_union <b>toy</b> - Union of structures containing each
179command's global variables.</p>
180
181<p>A command that needs global variables should declare a structure to
182contain them all, and add that structure to this union. A command should never
183declare global variables outside of this, because such global variables would
184allocate memory when running other commands that don't use those global
185variables.</p>
186
187<p>The first few fields of this structure can be intialized by get_optargs(),
188as specified by the options field off this command's toy_list entry. See
189the get_optargs() description in lib/args.c for details.</p>
190</li>
191
Rob Landley81b899d2007-12-18 02:02:47 -0600192<li><b>char toybuf[4096]</b> - a common scratch space buffer so
Rob Landley4e68de12007-12-13 07:00:27 -0600193commands don't need to allocate their own. Any command is free to use this,
194and it should never be directly referenced by functions in lib/ (although
195commands are free to pass toybuf in to a library function as an argument).</li>
196</ul>
197
198<p>The following functions are defined here:</p>
199<ul>
200<li><p>struct toy_list *<b>toy_find</b>(char *name) - Return the toy_list
201structure for this command name, or NULL if not found.</p></li>
Rob Landley81b899d2007-12-18 02:02:47 -0600202<li><p>void <b>toy_init</b>(struct toy_list *which, char *argv[]) - fill out
203the global toys structure, calling get_optargs() if necessary.</p></li>
Rob Landley4e68de12007-12-13 07:00:27 -0600204<li><p>void <b>toy_exec</b>(char *argv[]) - Run a built-in command with arguments.
205Calls toy_find() on the first argument (which must be just a command name
206without path). Returns if it can't find this command, otherwise calls
207toy_init(), toys->which.toy_main(), and exit() instead of returning.</p></li>
208
209<li><p>void <b>toybox_main</b>(void) - the main function for multiplexer
210command. Given a command name as its first argument, calls toy_exec() on its
211arguments. With no arguments, it lists available commands. If the first
212argument starts with "-" it lists each command with its default install
213path prepended.</p></li>
214
215</ul>
216
217<h3>Config.in</h3>
218
219<p>Top level configuration file in a stylized variant of
220<a href=http://kernel.org/doc/Documentation/kbuild/kconfig-language.txt>kconfig</a> format. Includes toys/Config.in.</p>
221
222<p>These files are directly used by "make menuconfig" to select which commands
223to build into toybox (thus generating a .config file), and by
224scripts/config2help.py to generate toys/help.h.</p>
225
226<h3>Temporary files:</h3>
227
228<ul>
229<li><p><b>.config</b> - Configuration file generated by kconfig, indicating
230which commands (and options to commands) are currently enabled. Used
231to generate gen_config.h and the toys/*.c dependency list.</p></li>
232
233<li><p><b>gen_config.h</b> - list of CFG_SYMBOL and USE_SYMBOL() macros,
234generated from .config by a sed invocation in the top level Makefile.</p>
235
236<p>CFG_SYMBOL is a comple time constant set to 1 for enabled symbols and 0 for
237disabled symbols. This can be used via normal if() statements to remove
238code at compile time via the optimizer's dead code elimination, which removes
239from the binary any code that cannot be reached. This saves space without
240cluttering the code with #ifdefs or leading to configuration dependent build
241breaks. (See the 1992 Usenix paper
242<a href=http://www.chris-lott.org/resources/cstyle/ifdefs.pdf>#ifdef
243Considered Harmful</a> for more information.)</p>
244
245<p>USE_SYMBOL(code) evaluates to the code in parentheses when the symbol
246is enabled, and nothing when the symbol is disabled. This can be used
247for things like varargs or variable declarations which can't always be
248eliminated by a compile time removalbe test on CFG_SYMBOL. Note that
249(unlike CFG_SYMBOL) this is really just a variant of #ifdef, and can
250still result in configuration dependent build breaks. Use with caution.</p>
251</li>
252</ul>
253
Rob Landley81b899d2007-12-18 02:02:47 -0600254<p><h2>Directory toys/</h2></p>
Rob Landley4e68de12007-12-13 07:00:27 -0600255
256<h3>toys/Config.in</h3>
257
258<p>Included from the top level Config.in, contains one or more
259configuration entries for each command.</p>
260
Rob Landley81b899d2007-12-18 02:02:47 -0600261<p>Each command has a configuration entry matching the command name (although
262configuration symbols are uppercase and command names are lower case).
Rob Landley4e68de12007-12-13 07:00:27 -0600263Options to commands start with the command name followed by an underscore and
264the option name. Global options are attachd to the "toybox" command,
265and thus use the prefix "TOYBOX_". This organization is used by
Rob Landley81b899d2007-12-18 02:02:47 -0600266scripts/cfg2files to select which toys/*.c files to compile for a given
267.config.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600268
269<p>A commands with multiple names (or multiple similar commands implemented in
270the same .c file) should have config symbols prefixed with the name of their
271C file. I.E. config symbol prefixes are NEWTOY() names. If OLDTOY() names
272have config symbols they're options (symbols with an underscore and suffix)
273to the NEWTOY() name. (See toys/toylist.h)</p>
274
275<h3>toys/toylist.h</h3>
Rob Landley81b899d2007-12-18 02:02:47 -0600276<p>The first half of this file prototypes all the structures to hold
Rob Landleyda09b7f2007-12-20 06:29:59 -0600277global variables for each command, and puts them in toy_union. These
278prototypes are only included if the macro NEWTOY isn't defined (in which
279case NEWTOY is defined to a default value that produces function
280prototypes).</p>
Rob Landley81b899d2007-12-18 02:02:47 -0600281
Rob Landleyda09b7f2007-12-20 06:29:59 -0600282<p>The second half of this file lists all the commands in alphabetical
283order, along with their command line arguments and install location.
284Each command has an appropriate configuration guard so only the commands that
285are enabled wind up in the list.</p>
286
287<p>The first time this header is #included, it defines structures and
288produces function prototypes for the commands in the toys directory.</p>
289
290
291<p>The first time it's included, it defines structures and produces function
292prototypes.
293 This
Rob Landley81b899d2007-12-18 02:02:47 -0600294is used to initialize toy_list in main.c, and later in that file to initialize
295NEED_OPTIONS (to figure out whether the command like parsing logic is needed),
296and to put the help entries in the right order in toys/help.c.</p>
Rob Landley4e68de12007-12-13 07:00:27 -0600297
298<h3>toys/help.h</h3>
299
300<p>#defines two help text strings for each command: a single line
301command_help and an additinal command_help_long. This is used by help_main()
302in toys/help.c to display help for commands.</p>
303
304<p>Although this file is generated from Config.in help entries by
305scripts/config2help.py, it's shipped in release tarballs so you don't need
306python on the build system. (If you check code out of source control, or
307modify Config.in, then you'll need python installed to rebuild it.)</p>
308
309<p>This file contains help for all commands, regardless of current
310configuration, but only the currently enabled ones are entered into help_data[]
311in toys/help.c.</p>
312
Rob Landley81b899d2007-12-18 02:02:47 -0600313<h2>Directory lib/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600314
Rob Landley7c04f012008-01-20 19:00:16 -0600315<p>lib: llist, getmountlist(), error_msg/error_exit, xmalloc(),
316strlcpy(), xexec(), xopen()/xread(), xgetcwd(), xabspath(), find_in_path(),
317itoa().</p>
318
319
320
Rob Landley81b899d2007-12-18 02:02:47 -0600321<h2>Directory scripts/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600322
323<h3>scripts/cfg2files.sh</h3>
324
325<p>Run .config through this filter to get a list of enabled commands, which
326is turned into a list of files in toys via a sed invocation in the top level
327Makefile.
328</p>
329
Rob Landley81b899d2007-12-18 02:02:47 -0600330<h2>Directory kconfig/</h2>
Rob Landley4e68de12007-12-13 07:00:27 -0600331
332<p>Menuconfig infrastructure copied from the Linux kernel. See the
333Linux kernel's Documentation/kbuild/kconfig-language.txt</p>
334
335<!--#include file="footer.html" -->