Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 1 | <!--#include file="header.html" --> |
| 2 | |
Rob Landley | ed6ed62 | 2012-03-06 20:49:03 -0600 | [diff] [blame] | 3 | <p><h1>Code style</h1></p> |
Rob Landley | e7c9a6d | 2012-02-28 06:34:09 -0600 | [diff] [blame] | 4 | |
| 5 | <p>The primary goal of toybox is _simple_ code. Keeping the code small is |
Rob Landley | ed6ed62 | 2012-03-06 20:49:03 -0600 | [diff] [blame] | 6 | second, with speed and lots of features coming in somewhere after that. |
| 7 | (For more on that, see the <a href=design.html>design</a> page.)</p> |
Rob Landley | e7c9a6d | 2012-02-28 06:34:09 -0600 | [diff] [blame] | 8 | |
| 9 | <p>A simple implementation usually takes up fewer lines of source code, |
| 10 | meaning more code can fit on the screen at once, meaning the programmer can |
| 11 | see more of it on the screen and thus keep more if in their head at once. |
Rob Landley | ed6ed62 | 2012-03-06 20:49:03 -0600 | [diff] [blame] | 12 | This helps code auditing and thus reduces bugs. That said, sometimes being |
| 13 | more explicit is preferable to being clever enough to outsmart yourself: |
| 14 | don't be so terse your code is unreadable.</p> |
Rob Landley | 5a0660f | 2007-12-27 21:36:44 -0600 | [diff] [blame] | 15 | |
| 16 | <p>Toybox source is formatted to be read with 4-space tab stops. Each file |
| 17 | starts with a special comment telling vi to set the tab stop to 4. Note that |
| 18 | one of the bugs in Ubuntu 7.10 broke vi's ability to parse these comments; you |
| 19 | must either rebuild vim from source, or go ":ts=4" yourself each time you load |
| 20 | the file.</p> |
| 21 | |
| 22 | <p>Gotos are allowed for error handling, and for breaking out of |
| 23 | nested loops. In general, a goto should only jump forward (not back), and |
| 24 | should either jump to the end of an outer loop, or to error handling code |
| 25 | at the end of the function. Goto labels are never indented: they override the |
| 26 | block structure of the file. Putting them at the left edge makes them easy |
| 27 | to spot as overrides to the normal flow of control, which they are.</p> |
| 28 | |
Rob Landley | e7c9a6d | 2012-02-28 06:34:09 -0600 | [diff] [blame] | 29 | <p><h1>Building Toybox:</h1></p> |
| 30 | |
| 31 | <p>Toybox is configured using the Kconfig language pioneered by the Linux |
| 32 | kernel, and adopted by many other projects (uClibc, OpenEmbedded, etc). |
| 33 | This generates a ".config" file containing the selected options, which |
| 34 | controls which features to enable when building toybox.</p> |
| 35 | |
| 36 | <p>Each configuration option has a default value. The defaults indicate the |
| 37 | "maximum sane configuration", I.E. if the feature defaults to "n" then it |
| 38 | either isn't complete or is a special-purpose option (such as debugging |
| 39 | code) that isn't intended for general purpose use.</p> |
| 40 | |
| 41 | <p>The standard build invocation is:</p> |
| 42 | |
| 43 | <ul> |
| 44 | <li>make defconfig #(or menuconfig)</li> |
| 45 | <li>make</li> |
| 46 | <li>make install</li> |
| 47 | </ul> |
| 48 | |
| 49 | <p>Type "make help" to see all available build options.</p> |
| 50 | |
| 51 | <p>The file "configure" contains a number of environment variable definitions |
| 52 | which influence the build, such as specifying which compiler to use or where |
| 53 | to install the resulting binaries. This file is included by the build, but |
| 54 | accepts existing definitions of the environment variables, so it may be sourced |
| 55 | or modified by the developer before building and the definitions exported |
| 56 | to the environment will take precedence.</p> |
| 57 | |
| 58 | <p>(To clarify: "configure" describes the build and installation environment, |
| 59 | ".config" lists the features selected by defconfig/menuconfig.)</p> |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 60 | |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 61 | <p><h1>Infrastructure:</h1></p> |
| 62 | |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 63 | <p>The toybox source code is in following directories:</p> |
| 64 | <ul> |
| 65 | <li>The <a href="#top">top level directory</a> contains the file main.c (were |
| 66 | execution starts), the header file toys.h (included by every command), and |
| 67 | other global infrastructure.</li> |
| 68 | <li>The <a href="#lib">lib directory</a> contains common functions shared by |
| 69 | multiple commands.</li> |
| 70 | <li>The <a href="#toys">toys directory</a> contains the C files implementating |
| 71 | each command.</li> |
| 72 | <li>The <a href="#scripts">scripts directory</a> contains the build and |
| 73 | test infrastructure.</li> |
| 74 | <li>The <a href="#kconfig">kconfig directory</a> contains the configuration |
| 75 | infrastructure implementing menuconfig (copied from the Linux kernel).</li> |
| 76 | <li>The <a href="#generated">generated directory</a> contains intermediate |
| 77 | files generated from other parts of the source code.</li> |
| 78 | </ul> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 79 | |
Rob Landley | bbe500e | 2012-02-26 21:53:15 -0600 | [diff] [blame] | 80 | <a name="adding" /> |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 81 | <p><h1>Adding a new command</h1></p> |
| 82 | <p>To add a new command to toybox, add a C file implementing that command to |
| 83 | the toys directory. No other files need to be modified; the build extracts |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 84 | all the information it needs (such as command line arguments) from specially |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 85 | formatted comments and macros in the C file. (See the description of the |
Rob Landley | e7c9a6d | 2012-02-28 06:34:09 -0600 | [diff] [blame] | 86 | <a href="#generated">"generated" directory</a> for details.)</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 87 | |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 88 | <p>An easy way to start a new command is copy the file "hello.c" to |
| 89 | the name of the new command, and modify this copy to implement the new command. |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 90 | This file is an example command meant to be used as a "skeleton" for |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 91 | new commands (more or less by turning every instance of "hello" into the |
| 92 | name of your command, updating the command line arguments, globals, and |
| 93 | help data, and then filling out its "main" function with code that does |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 94 | something interesting). It provides examples of all the build infrastructure |
| 95 | (including optional elements like command line argument parsing and global |
| 96 | variables that a "hello world" program doesn't strictly need).</p> |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 97 | |
| 98 | <p>Here's a checklist of steps to turn hello.c into another command:</p> |
| 99 | |
| 100 | <ul> |
| 101 | <li><p>First "cd toys" and "cp hello.c yourcommand.c". Note that the name |
| 102 | of this file is significant, it's the name of the new command you're adding |
| 103 | to toybox. Open your new file in your favorite editor.</p></li> |
| 104 | |
| 105 | <li><p>Change the one line comment at the top of the file (currently |
| 106 | "hello.c - A hello world program") to describe your new file.</p></li> |
| 107 | |
| 108 | <li><p>Change the copyright notice to your name, email, and the current |
| 109 | year.</p></li> |
| 110 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 111 | <li><p>Give a URL to the relevant standards document, or say "Not in SUSv4" if |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 112 | there is no relevant standard. (Currently both lines are there, delete |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 113 | whichever is inappropriate.) The existing link goes to the directory of SUSv4 |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 114 | command line utility standards on the Open Group's website, where there's often |
| 115 | a relevant commandname.html file. Feel free to link to other documentation or |
| 116 | standards as appropriate.</p></li> |
| 117 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 118 | <li><p>Update the USE_YOURCOMMAND(NEWTOY(yourcommand,"blah",0)) line. |
| 119 | The NEWTOY macro fills out this command's <a href="#toy_list">toy_list</a> |
| 120 | structure. The arguments to the NEWTOY macro are:</p> |
| 121 | |
| 122 | <ol> |
| 123 | <li><p>the name used to run your command</p></li> |
| 124 | <li><p>the command line argument <a href="#lib_args">option parsing string</a> (NULL if none)</p></li> |
| 125 | <li><p>a bitfield of TOYFLAG values |
| 126 | (defined in toys.h) providing additional information such as where your |
| 127 | command should be installed on a running system, whether to blank umask |
| 128 | before running, whether or not the command must run as root (and thus should |
| 129 | retain root access if installed SUID), and so on.</p></li> |
| 130 | </ol> |
| 131 | </li> |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 132 | |
| 133 | <li><p>Change the kconfig data (from "config YOURCOMMAND" to the end of the |
| 134 | comment block) to supply your command's configuration and help |
| 135 | information. The uppper case config symbols are used by menuconfig, and are |
| 136 | also what the CFG_ and USE_() macros are generated from (see [TODO]). The |
| 137 | help information here is used by menuconfig, and also by the "help" command to |
| 138 | describe your new command. (See [TODO] for details.) By convention, |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 139 | unfinished commands default to "n" and finished commands default to "y", |
| 140 | so "make defconfig" selects all finished commands. (Note, "finished" means |
| 141 | "ready to be used", not that it'll never change again.)<p> |
| 142 | |
| 143 | <p>Each help block should start with a "usage: yourcommand" line explaining |
| 144 | any command line arguments added by this config option. The "help" command |
| 145 | outputs this text, and scripts/config2help.c in the build infrastructure |
| 146 | collates these usage lines for commands with multiple configuration |
| 147 | options when producing generated/help.h.</p> |
| 148 | </li> |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 149 | |
| 150 | <li><p>Update the DEFINE_GLOBALS() macro to contain your command's global |
| 151 | variables, and also change the name "hello" in the #define TT line afterwards |
| 152 | to the name of your command. If your command has no global variables, delete |
| 153 | this macro (and the #define TT line afterwards). Note that if you specified |
| 154 | two-character command line arguments in NEWTOY(), the first few global |
| 155 | variables will be initialized by the automatic argument parsing logic, and |
| 156 | the type and order of these variables must correspond to the arguments |
| 157 | specified in NEWTOY(). See [TODO] for details.</p></li> |
| 158 | |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 159 | <li><p>If you didn't delete the DEFINE_GLOBALS macro, change the "#define TT |
| 160 | this.hello" line to use your command name in place of the "hello". This is a |
| 161 | shortcut to access your global variables as if they were members of the global |
| 162 | struct "TT". (Access these members with a period ".", not a right arrow |
| 163 | "->".)</p></li> |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 164 | |
| 165 | <li><p>Rename hello_main() to yourcommand_main(). This is the main() function |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 166 | where execution of your command starts. See [TODO] to figure out what |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 167 | happened to your command line arguments and how to access them.</p></li> |
| 168 | </ul> |
| 169 | |
| 170 | <p><a name="top" /><h2>Top level directory.</h2></p> |
| 171 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 172 | <p>This directory contains global infrastructure.</p> |
| 173 | |
| 174 | <h3>toys.h</h3> |
| 175 | <p>Each command #includes "toys.h" as part of its standard prolog.</p> |
| 176 | |
| 177 | <p>This file sucks in most of the commonly used standard #includes, so |
| 178 | individual files can just #include "toys.h" and not have to worry about |
| 179 | stdargs.h and so on. Individual commands still need to #include |
| 180 | special-purpose headers that may not be present on all systems (and thus would |
| 181 | prevent toybox from building that command on such a system with that command |
| 182 | enabled). Examples include regex support, any "linux/" or "asm/" headers, mtab |
| 183 | support (mntent.h and sys/mount.h), and so on.</p> |
| 184 | |
| 185 | <p>The toys.h header also defines structures for most of the global variables |
| 186 | provided to each command by toybox_main(). These are described in |
| 187 | detail in the description for main.c, where they are initialized.</p> |
| 188 | |
| 189 | <p>The global variables are grouped into structures (and a union) for space |
| 190 | savings, to more easily track the amount of memory consumed by them, |
| 191 | so that they may be automatically cleared/initialized as needed, and so |
| 192 | that access to global variables is more easily distinguished from access to |
| 193 | local variables.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 194 | |
| 195 | <h3>main.c</h3> |
| 196 | <p>Contains the main() function where execution starts, plus |
| 197 | common infrastructure to initialize global variables and select which command |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 198 | to run. The "toybox" multiplexer command also lives here. (This is the |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 199 | only command defined outside of the toys directory.)</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 200 | |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 201 | <p>Execution starts in main() which trims any path off of the first command |
| 202 | name and calls toybox_main(), which calls toy_exec(), which calls toy_find() |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 203 | and toy_init() before calling the appropriate command's function from |
| 204 | toy_list[] (via toys.which->toy_main()). |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 205 | If the command is "toybox", execution recurses into toybox_main(), otherwise |
| 206 | the call goes to the appropriate commandname_main() from a C file in the toys |
| 207 | directory.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 208 | |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 209 | <p>The following global variables are defined in main.c:</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 210 | <ul> |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 211 | <a name="toy_list" /> |
| 212 | <li><p><b>struct toy_list toy_list[]</b> - array describing all the |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 213 | commands currently configured into toybox. The first entry (toy_list[0]) is |
| 214 | for the "toybox" multiplexer command, which runs all the other built-in commands |
| 215 | without symlinks by using its first argument as the name of the command to |
| 216 | run and the rest as that command's argument list (ala "./toybox echo hello"). |
| 217 | The remaining entries are the commands in alphabetical order (for efficient |
| 218 | binary search).</p> |
| 219 | |
| 220 | <p>This is a read-only array initialized at compile time by |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 221 | defining macros and #including generated/newtoys.h.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 222 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 223 | <p>Members of struct toy_list (defined in "toys.h") include:</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 224 | <ul> |
| 225 | <li><p>char *<b>name</b> - the name of this command.</p></li> |
| 226 | <li><p>void (*<b>toy_main</b>)(void) - function pointer to run this |
| 227 | command.</p></li> |
| 228 | <li><p>char *<b>options</b> - command line option string (used by |
| 229 | get_optflags() in lib/args.c to intialize toys.optflags, toys.optargs, and |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 230 | entries in the toy's DEFINE_GLOBALS struct). When this is NULL, no option |
| 231 | parsing is done before calling toy_main().</p></li> |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 232 | <li><p>int <b>flags</b> - Behavior flags for this command. The following flags are currently understood:</p> |
| 233 | |
| 234 | <ul> |
| 235 | <li><b>TOYFLAG_USR</b> - Install this command under /usr</li> |
| 236 | <li><b>TOYFLAG_BIN</b> - Install this command under /bin</li> |
| 237 | <li><b>TOYFLAG_SBIN</b> - Install this command under /sbin</li> |
| 238 | <li><b>TOYFLAG_NOFORK</b> - This command can be used as a shell builtin.</li> |
| 239 | <li><b>TOYFLAG_UMASK</b> - Call umask(0) before running this command.</li> |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 240 | <li><b>TOYFLAG_STAYROOT</b> - Don't drop permissions for this command if toybox is installed SUID root.</li> |
| 241 | <li><b>TOYFLAG_NEEDROOT</b> - This command cannot function unless run with root access.</li> |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 242 | </ul> |
| 243 | <br> |
| 244 | |
| 245 | <p>These flags are combined with | (or). For example, to install a command |
| 246 | in /usr/bin, or together TOYFLAG_USR|TOYFLAG_BIN.</p> |
| 247 | </ul> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 248 | </li> |
| 249 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 250 | <li><p><b>struct toy_context toys</b> - global structure containing information |
| 251 | common to all commands, initializd by toy_init() and defined in "toys.h". |
| 252 | Members of this structure include:</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 253 | <ul> |
| 254 | <li><p>struct toy_list *<b>which</b> - a pointer to this command's toy_list |
| 255 | structure. Mostly used to grab the name of the running command |
| 256 | (toys->which.name).</p> |
| 257 | </li> |
| 258 | <li><p>int <b>exitval</b> - Exit value of this command. Defaults to zero. The |
| 259 | error_exit() functions will return 1 if this is zero, otherwise they'll |
| 260 | return this value.</p></li> |
| 261 | <li><p>char **<b>argv</b> - "raw" command line options, I.E. the original |
| 262 | unmodified string array passed in to main(). Note that modifying this changes |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 263 | "ps" output, and is not recommended. This array is null terminated; a NULL |
| 264 | entry indicates the end of the array.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 265 | <p>Most commands don't use this field, instead the use optargs, optflags, |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 266 | and the fields in the DEFINE_GLOBALS struct initialized by get_optflags().</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 267 | </li> |
| 268 | <li><p>unsigned <b>optflags</b> - Command line option flags, set by |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 269 | <a href="#lib_args">get_optflags()</a>. Indicates which of the command line options listed in |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 270 | toys->which.options occurred this time.</p> |
| 271 | |
| 272 | <p>The rightmost command line argument listed in toys->which.options sets bit |
| 273 | 1, the next one sets bit 2, and so on. This means the bits are set in the same |
| 274 | order the binary digits would be listed if typed out as a string. For example, |
| 275 | the option string "abcd" would parse the command line "-c" to set optflags to 2, |
| 276 | "-a" would set optflags to 8, and "-bd" would set optflags to 6 (4|2).</p> |
| 277 | |
| 278 | <p>Only letters are relevant to optflags. In the string "a*b:c#d", d=1, c=2, |
| 279 | b=4, a=8. The punctuation after a letter initializes global variables |
| 280 | (see [TODO] DECLARE_GLOBALS() for details).</p> |
| 281 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 282 | <p>For more information on option parsing, see <a href="#lib_args">get_optflags()</a>.</p> |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 283 | |
| 284 | </li> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 285 | <li><p>char **<b>optargs</b> - Null terminated array of arguments left over |
| 286 | after get_optflags() removed all the ones it understood. Note: optarg[0] is |
| 287 | the first argument, not the command name. Use toys.which->name for the command |
| 288 | name.</p></li> |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 289 | <li><p>int <b>optc</b> - Optarg count, equivalent to argc but for |
| 290 | optargs[].<p></li> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 291 | <li><p>int <b>exithelp</b> - Whether error_exit() should print a usage message |
| 292 | via help_main() before exiting. (True during option parsing, defaults to |
| 293 | false afterwards.)</p></li> |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 294 | </ul> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 295 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 296 | <li><p><b>union toy_union this</b> - Union of structures containing each |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 297 | command's global variables.</p> |
| 298 | |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 299 | <p>Global variables are useful: they reduce the overhead of passing extra |
| 300 | command line arguments between functions, they conveniently start prezeroed to |
| 301 | save initialization costs, and the command line argument parsing infrastructure |
| 302 | can also initialize global variables with its results.</p> |
| 303 | |
| 304 | <p>But since each toybox process can only run one command at a time, allocating |
| 305 | space for global variables belonging to other commands you aren't currently |
| 306 | running would be wasteful.</p> |
| 307 | |
| 308 | <p>Toybox handles this by encapsulating each command's global variables in |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 309 | a structure, and declaring a union of those structures with a single global |
| 310 | instance (called "this"). The DEFINE_GLOBALS() macro contains the global |
| 311 | variables that should go in the current command's global structure. Each |
| 312 | variable can then be accessed as "this.commandname.varname". |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 313 | Generally, the macro TT is #defined to this.commandname so the variable |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 314 | can then be accessed as "TT.variable". See toys/hello.c for an example.</p> |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 315 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 316 | <p>A command that needs global variables should declare a structure to |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 317 | contain them all, and add that structure to this union. A command should never |
| 318 | declare global variables outside of this, because such global variables would |
| 319 | allocate memory when running other commands that don't use those global |
| 320 | variables.</p> |
| 321 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 322 | <p>The first few fields of this structure can be intialized by <a href="#lib_args">get_optargs()</a>, |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 323 | as specified by the options field off this command's toy_list entry. See |
| 324 | the get_optargs() description in lib/args.c for details.</p> |
| 325 | </li> |
| 326 | |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 327 | <li><b>char toybuf[4096]</b> - a common scratch space buffer so |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 328 | commands don't need to allocate their own. Any command is free to use this, |
| 329 | and it should never be directly referenced by functions in lib/ (although |
| 330 | commands are free to pass toybuf in to a library function as an argument).</li> |
| 331 | </ul> |
| 332 | |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 333 | <p>The following functions are defined in main.c:</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 334 | <ul> |
| 335 | <li><p>struct toy_list *<b>toy_find</b>(char *name) - Return the toy_list |
| 336 | structure for this command name, or NULL if not found.</p></li> |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 337 | <li><p>void <b>toy_init</b>(struct toy_list *which, char *argv[]) - fill out |
| 338 | the global toys structure, calling get_optargs() if necessary.</p></li> |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 339 | <li><p>void <b>toy_exec</b>(char *argv[]) - Run a built-in command with |
| 340 | arguments.</p> |
| 341 | <p>Calls toy_find() on argv[0] (which must be just a command name |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 342 | without path). Returns if it can't find this command, otherwise calls |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 343 | toy_init(), toys->which.toy_main(), and exit() instead of returning.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 344 | |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 345 | <p>Use the library function xexec() to fall back to external executables |
| 346 | in $PATH if toy_exec() can't find a built-in command. Note that toy_exec() |
| 347 | does not strip paths before searching for a command, so "./command" will |
| 348 | never match an internal command.</li> |
| 349 | |
| 350 | <li><p>void <b>toybox_main</b>(void) - the main function for the multiplexer |
| 351 | command (I.E. "toybox"). Given a command name as its first argument, calls |
| 352 | toy_exec() on its arguments. With no arguments, it lists available commands. |
| 353 | If the first argument starts with "-" it lists each command with its default |
| 354 | install path prepended.</p></li> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 355 | |
| 356 | </ul> |
| 357 | |
| 358 | <h3>Config.in</h3> |
| 359 | |
| 360 | <p>Top level configuration file in a stylized variant of |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 361 | <a href=http://kernel.org/doc/Documentation/kbuild/kconfig-language.txt>kconfig</a> format. Includes generated/Config.in.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 362 | |
| 363 | <p>These files are directly used by "make menuconfig" to select which commands |
| 364 | to build into toybox (thus generating a .config file), and by |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 365 | scripts/config2help.py to create generated/help.h.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 366 | |
| 367 | <h3>Temporary files:</h3> |
| 368 | |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 369 | <p>There is one temporary file in the top level source directory:</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 370 | <ul> |
| 371 | <li><p><b>.config</b> - Configuration file generated by kconfig, indicating |
| 372 | which commands (and options to commands) are currently enabled. Used |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 373 | to make generated/config.h and determine which toys/*.c files to build.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 374 | |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 375 | <p>You can create a human readable "miniconfig" version of this file using |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 376 | <a href=http://landley.net/aboriginal/new_platform.html#miniconfig>these |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 377 | instructions</a>.</p> |
| 378 | </li> |
| 379 | </ul> |
| 380 | |
Rob Landley | e7c9a6d | 2012-02-28 06:34:09 -0600 | [diff] [blame] | 381 | <a name="generated" /> |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 382 | <p>The "generated/" directory contains files generated from other source code |
| 383 | in toybox. All of these files can be recreated by the build system, although |
| 384 | some (such as generated/help.h) are shipped in release versions to reduce |
| 385 | environmental dependencies (I.E. so you don't need python on your build |
| 386 | system).</p> |
| 387 | |
| 388 | <ul> |
| 389 | <li><p><b>generated/config.h</b> - list of CFG_SYMBOL and USE_SYMBOL() macros, |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 390 | generated from .config by a sed invocation in the top level Makefile.</p> |
| 391 | |
| 392 | <p>CFG_SYMBOL is a comple time constant set to 1 for enabled symbols and 0 for |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 393 | disabled symbols. This allows the use of normal if() statements to remove |
| 394 | code at compile time via the optimizer's dead code elimination (which removes |
| 395 | from the binary any code that cannot be reached). This saves space without |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 396 | cluttering the code with #ifdefs or leading to configuration dependent build |
| 397 | breaks. (See the 1992 Usenix paper |
Rob Landley | b6063de | 2012-01-29 13:54:13 -0600 | [diff] [blame] | 398 | <a href=http://doc.cat-v.org/henry_spencer/ifdef_considered_harmful.pdf>#ifdef |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 399 | Considered Harmful</a> for more information.)</p> |
| 400 | |
| 401 | <p>USE_SYMBOL(code) evaluates to the code in parentheses when the symbol |
| 402 | is enabled, and nothing when the symbol is disabled. This can be used |
| 403 | for things like varargs or variable declarations which can't always be |
Rob Landley | 6882ee8 | 2008-02-12 18:41:34 -0600 | [diff] [blame] | 404 | eliminated by a simple test on CFG_SYMBOL. Note that |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 405 | (unlike CFG_SYMBOL) this is really just a variant of #ifdef, and can |
| 406 | still result in configuration dependent build breaks. Use with caution.</p> |
| 407 | </li> |
| 408 | </ul> |
| 409 | |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 410 | <p><h2>Directory toys/</h2></p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 411 | |
| 412 | <h3>toys/Config.in</h3> |
| 413 | |
| 414 | <p>Included from the top level Config.in, contains one or more |
| 415 | configuration entries for each command.</p> |
| 416 | |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 417 | <p>Each command has a configuration entry matching the command name (although |
| 418 | configuration symbols are uppercase and command names are lower case). |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 419 | Options to commands start with the command name followed by an underscore and |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 420 | the option name. Global options are attached to the "toybox" command, |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 421 | and thus use the prefix "TOYBOX_". This organization is used by |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 422 | scripts/cfg2files to select which toys/*.c files to compile for a given |
| 423 | .config.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 424 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 425 | <p>A command with multiple names (or multiple similar commands implemented in |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 426 | the same .c file) should have config symbols prefixed with the name of their |
| 427 | C file. I.E. config symbol prefixes are NEWTOY() names. If OLDTOY() names |
| 428 | have config symbols they're options (symbols with an underscore and suffix) |
| 429 | to the NEWTOY() name. (See toys/toylist.h)</p> |
| 430 | |
| 431 | <h3>toys/toylist.h</h3> |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 432 | <p>The first half of this file prototypes all the structures to hold |
Rob Landley | da09b7f | 2007-12-20 06:29:59 -0600 | [diff] [blame] | 433 | global variables for each command, and puts them in toy_union. These |
| 434 | prototypes are only included if the macro NEWTOY isn't defined (in which |
| 435 | case NEWTOY is defined to a default value that produces function |
| 436 | prototypes).</p> |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 437 | |
Rob Landley | da09b7f | 2007-12-20 06:29:59 -0600 | [diff] [blame] | 438 | <p>The second half of this file lists all the commands in alphabetical |
| 439 | order, along with their command line arguments and install location. |
| 440 | Each command has an appropriate configuration guard so only the commands that |
| 441 | are enabled wind up in the list.</p> |
| 442 | |
| 443 | <p>The first time this header is #included, it defines structures and |
| 444 | produces function prototypes for the commands in the toys directory.</p> |
| 445 | |
| 446 | |
| 447 | <p>The first time it's included, it defines structures and produces function |
| 448 | prototypes. |
| 449 | This |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 450 | is used to initialize toy_list in main.c, and later in that file to initialize |
| 451 | NEED_OPTIONS (to figure out whether the command like parsing logic is needed), |
| 452 | and to put the help entries in the right order in toys/help.c.</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 453 | |
| 454 | <h3>toys/help.h</h3> |
| 455 | |
| 456 | <p>#defines two help text strings for each command: a single line |
| 457 | command_help and an additinal command_help_long. This is used by help_main() |
| 458 | in toys/help.c to display help for commands.</p> |
| 459 | |
| 460 | <p>Although this file is generated from Config.in help entries by |
| 461 | scripts/config2help.py, it's shipped in release tarballs so you don't need |
| 462 | python on the build system. (If you check code out of source control, or |
| 463 | modify Config.in, then you'll need python installed to rebuild it.)</p> |
| 464 | |
| 465 | <p>This file contains help for all commands, regardless of current |
| 466 | configuration, but only the currently enabled ones are entered into help_data[] |
| 467 | in toys/help.c.</p> |
| 468 | |
Rob Landley | 137bf34 | 2012-03-09 08:33:57 -0600 | [diff] [blame] | 469 | <a name="lib"> |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 470 | <h2>Directory lib/</h2> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 471 | |
Rob Landley | 137bf34 | 2012-03-09 08:33:57 -0600 | [diff] [blame] | 472 | <p>TODO: document lots more here.</p> |
| 473 | |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 474 | <p>lib: llist, getmountlist(), error_msg/error_exit, xmalloc(), |
| 475 | strlcpy(), xexec(), xopen()/xread(), xgetcwd(), xabspath(), find_in_path(), |
| 476 | itoa().</p> |
| 477 | |
Rob Landley | 137bf34 | 2012-03-09 08:33:57 -0600 | [diff] [blame] | 478 | <h3>lib/portability.h</h3> |
| 479 | |
| 480 | <p>This file is automatically included from the top of toys.h, and smooths |
| 481 | over differences between platforms (hardware targets, compilers, C libraries, |
| 482 | operating systems, etc).</p> |
| 483 | |
| 484 | <p>This file provides SWAP macros (SWAP_BE16(x) and SWAP_LE32(x) and so on).</p> |
| 485 | |
| 486 | <p>A macro like SWAP_LE32(x) means "The value in x is stored as a little |
| 487 | endian 32 bit value, so perform the translation to/from whatever the native |
| 488 | 32-bit format is". You do the swap once on the way in, and once on the way |
| 489 | out. If your target is already little endian, the macro is a NOP.</p> |
| 490 | |
| 491 | <p>The SWAP macros come in BE and LE each with 16, 32, and 64 bit versions. |
| 492 | In each case, the name of the macro refers to the _external_ representation, |
| 493 | and converts to/from whatever your native representation happens to be (which |
| 494 | can vary depending on what you're currently compiling for).</p> |
| 495 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 496 | <a name="lib_args"><h3>lib/args.c</h3> |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 497 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 498 | <p>Toybox's main.c automatically parses command line options before calling the |
| 499 | command's main function. Option parsing starts in get_optflags(), which stores |
| 500 | results in the global structures "toys" (optflags and optargs) and "this".</p> |
| 501 | |
| 502 | <p>The option parsing infrastructure stores a bitfield in toys.optflags to |
| 503 | indicate which options the current command line contained. Arguments |
| 504 | attached to those options are saved into the command's global structure |
| 505 | ("this"). Any remaining command line arguments are collected together into |
| 506 | the null-terminated array toys.optargs, with the length in toys.optc. (Note |
| 507 | that toys.optargs does not contain the current command name at position zero, |
| 508 | use "toys.which->name" for that.) The raw command line arguments get_optflags() |
| 509 | parsed are retained unmodified in toys.argv[].</p> |
| 510 | |
| 511 | <p>Toybox's option parsing logic is controlled by an "optflags" string, using |
| 512 | a format reminiscent of getopt's optargs but has several important differences. |
| 513 | Toybox does not use the getopt() |
| 514 | function out of the C library, get_optflags() is an independent implementation |
| 515 | which doesn't permute the original arguments (and thus doesn't change how the |
| 516 | command is displayed in ps and top), and has many features not present in |
| 517 | libc optargs() (such as the ability to describe long options in the same string |
| 518 | as normal options).</p> |
| 519 | |
| 520 | <p>Each command's NEWTOY() macro has an optflags string as its middle argument, |
| 521 | which sets toy_list.options for that command to tell get_optflags() what |
| 522 | command line arguments to look for, and what to do with them. |
| 523 | If a command has no option |
| 524 | definition string (I.E. the argument is NULL), option parsing is skipped |
| 525 | for that command, which must look at the raw data in toys.argv to parse its |
| 526 | own arguments. (If no currently enabled command uses option parsing, |
| 527 | get_optflags() is optimized out of the resulting binary by the compiler's |
| 528 | --gc-sections option.)</p> |
| 529 | |
| 530 | <p>You don't have to free the option strings, which point into the environment |
| 531 | space (I.E. the string data is not copied). A TOYFLAG_NOFORK command |
| 532 | that uses the linked list type "*" should free the list objects but not |
| 533 | the data they point to, via "llist_free(TT.mylist, NULL);". (If it's not |
| 534 | NOFORK, exit() will free all the malloced data anyway unless you want |
| 535 | to implement a CONFIG_TOYBOX_FREE cleanup for it.)</p> |
| 536 | |
| 537 | <h4>Optflags format string</h4> |
| 538 | |
| 539 | <p>Note: the optflags option description string format is much more |
| 540 | concisely described by a large comment at the top of lib/args.c.</p> |
| 541 | |
| 542 | <p>The general theory is that letters set optflags, and punctuation describes |
| 543 | other actions the option parsing logic should take.</p> |
| 544 | |
| 545 | <p>For example, suppose the command line <b>command -b fruit -d walrus -a 42</b> |
| 546 | is parsed using the optflags string "<b>a#b:c:d</b>". (I.E. |
| 547 | toys.which->options="a#b:c:d" and argv = ["command", "-b", "fruit", "-d", |
| 548 | "walrus", "-a", "42"]). When get_optflags() returns, the following data is |
| 549 | available to command_main(): |
| 550 | |
| 551 | <ul> |
| 552 | <li><p>In <b>struct toys</b>: |
| 553 | <ul> |
| 554 | <li>toys.optflags = 13; // -a = 8 | -b = 4 | -d = 1</li> |
| 555 | <li>toys.optargs[0] = "walrus"; // leftover argument</li> |
| 556 | <li>toys.optargs[1] = NULL; // end of list</li> |
| 557 | <li>toys.optc=1; // there was 1 leftover argument</li> |
| 558 | <li>toys.argv[] = {"-b", "fruit", "-d", "walrus", "-a", "42"}; // The original command line arguments |
| 559 | </ul> |
| 560 | <p></li> |
| 561 | |
| 562 | <li><p>In <b>union this</b> (treated as <b>long this[]</b>): |
| 563 | <ul> |
| 564 | <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> |
| 565 | <li>this[1] = (long)"fruit"; // argument to -b</li> |
| 566 | <li>this[2] = 42; // argument to -a</li> |
| 567 | </ul> |
| 568 | </p></li> |
| 569 | </ul> |
| 570 | |
| 571 | <p>If the command's globals are:</p> |
| 572 | |
| 573 | <blockquote><pre> |
| 574 | DECLARE_GLOBALS( |
| 575 | char *c; |
| 576 | char *b; |
| 577 | long a; |
| 578 | ) |
| 579 | #define TT this.command |
| 580 | </pre></blockquote> |
| 581 | <p>That would mean TT.c == NULL, TT.b == "fruit", and TT.a == 42. (Remember, |
| 582 | each entry that receives an argument must be a long or pointer, to line up |
| 583 | with the array position. Right to left in the optflags string corresponds to |
| 584 | top to bottom in DECLARE_GLOBALS().</p> |
| 585 | |
| 586 | <p><b>long toys.optflags</b></p> |
| 587 | |
| 588 | <p>Each option in the optflags string corresponds to a bit position in |
| 589 | toys.optflags, with the same value as a corresponding binary digit. The |
| 590 | rightmost argument is (1<<0), the next to last is (1<<1) and so on. If |
Rob Landley | b4a0efa | 2012-02-06 21:15:19 -0600 | [diff] [blame] | 591 | the option isn't encountered while parsing argv[], its bit remains 0.</p> |
| 592 | |
| 593 | <p>For example, |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 594 | the optflags string "abcd" would parse the command line argument "-c" to set |
| 595 | optflags to 2, "-a" would set optflags to 8, "-bd" would set optflags to |
| 596 | 6 (I.E. 4|2), and "-a -c" would set optflags to 10 (2|8).</p> |
| 597 | |
| 598 | <p>Only letters are relevant to optflags, punctuation is skipped: in the |
| 599 | string "a*b:c#d", d=1, c=2, b=4, a=8. The punctuation after a letter |
| 600 | usually indicate that the option takes an argument.</p> |
| 601 | |
Rob Landley | b4a0efa | 2012-02-06 21:15:19 -0600 | [diff] [blame] | 602 | <p>Since toys.optflags is an unsigned int, it only stores 32 bits. (Which is |
| 603 | the amount a long would have on 32-bit platforms anyway; 64 bit code on |
| 604 | 32 bit platforms is too expensive to require in common code used by almost |
| 605 | all commands.) Bit positions beyond the 1<<31 aren't recorded, but |
| 606 | parsing higher options can still set global variables.</p> |
| 607 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 608 | <p><b>Automatically setting global variables from arguments (union this)</b></p> |
| 609 | |
| 610 | <p>The following punctuation characters may be appended to an optflags |
| 611 | argument letter, indicating the option takes an additional argument:</p> |
| 612 | |
| 613 | <ul> |
| 614 | <li><b>:</b> - plus a string argument, keep most recent if more than one.</li> |
| 615 | <li><b>*</b> - plus a string argument, appended to a linked list.</li> |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 616 | <li><b>@</b> - plus an occurrence counter (stored in a long)</li> |
Rob Landley | b6063de | 2012-01-29 13:54:13 -0600 | [diff] [blame] | 617 | <li><b>#</b> - plus a signed long argument. |
| 618 | <li><b>.</b> - plus a floating point argument (if CFG_TOYBOX_FLOAT).</li> |
| 619 | <ul>The following can be appended to a float or double: |
| 620 | <li><b><123</b> - error if argument is less than this</li> |
| 621 | <li><b>>123</b> - error if argument is greater than this</li> |
| 622 | <li><b>=123</b> - default value if argument not supplied</li> |
| 623 | </ul> |
| 624 | <ul><li>Option parsing only understands <>= after . when CFG_TOYBOX_FLOAT |
| 625 | is enabled. (Otherwise the code to determine where floating point constants |
| 626 | end drops out. When disabled, it can reserve a global data slot for the |
| 627 | argument so offsets won't change, but will never fill it out.). You can handle |
| 628 | this by using the USE_BLAH() macros with C string concatenation, ala: |
| 629 | "abc." USE_TOYBOX_FLOAT("<1.23>4.56=7.89") "def"</li></ul> |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 630 | </ul> |
| 631 | |
| 632 | <p>Arguments may occur with or without a space (I.E. "-a 42" or "-a42"). |
| 633 | The command line argument "-abc" may be interepreted many different ways: |
| 634 | the optflags string "cba" sets toys.optflags = 7, "c:ba" sets toys.optflags=4 |
| 635 | and saves "ba" as the argument to -c, and "cb:a" sets optflags to 6 and saves |
| 636 | "c" as the argument to -b.</p> |
| 637 | |
| 638 | <p>Options which have an argument fill in the corresponding slot in the global |
| 639 | union "this" (see generated/globals.h), treating it as an array of longs |
| 640 | with the rightmost saved in this[0]. Again using "a*b:c#d", "-c 42" would set |
| 641 | this[0]=42; and "-b 42" would set this[1]="42"; each slot is left NULL if |
| 642 | the corresponding argument is not encountered.</p> |
| 643 | |
| 644 | <p>This behavior is useful because the LP64 standard ensures long and pointer |
Rob Landley | b4a0efa | 2012-02-06 21:15:19 -0600 | [diff] [blame] | 645 | are the same size. C99 guarantees structure members will occur in memory |
| 646 | in the same order they're declared, and that padding won't be inserted between |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 647 | consecutive variables of register size. Thus the first few entries can |
| 648 | be longs or pointers corresponding to the saved arguments.</p> |
| 649 | |
| 650 | <p><b>char *toys.optargs[]</b></p> |
| 651 | |
| 652 | <p>Command line arguments in argv[] which are not consumed by option parsing |
| 653 | (I.E. not recognized either as -flags or arguments to -flags) will be copied |
| 654 | to toys.optargs[], with the length of that array in toys.optc. |
| 655 | (When toys.optc is 0, no unrecognized command line arguments remain.) |
| 656 | The order of entries is preserved, and as with argv[] this new array is also |
| 657 | terminated by a NULL entry.</p> |
| 658 | |
| 659 | <p>Option parsing can require a minimum or maximum number of optargs left |
| 660 | over, by adding "<1" (read "at least one") or ">9" ("at most nine") to the |
| 661 | start of the optflags string.</p> |
| 662 | |
| 663 | <p>The special argument "--" terminates option parsing, storing all remaining |
| 664 | arguments in optargs. The "--" itself is consumed.</p> |
| 665 | |
| 666 | <p><b>Other optflags control characters</b></p> |
| 667 | |
| 668 | <p>The following characters may occur at the start of each command's |
| 669 | optflags string, before any options that would set a bit in toys.optflags:</p> |
| 670 | |
| 671 | <ul> |
| 672 | <li><b>^</b> - stop at first nonoption argument (for nice, xargs...)</li> |
| 673 | <li><b>?</b> - allow unknown arguments (pass non-option arguments starting |
| 674 | with - through to optargs instead of erroring out).</li> |
| 675 | <li><b>&</b> - the first argument has imaginary dash (ala tar/ps. If given twice, all arguments have imaginary dash.)</li> |
| 676 | <li><b><</b> - must be followed by a decimal digit indicating at least this many leftover arguments are needed in optargs (default 0)</li> |
| 677 | <li><b>></b> - must be followed by a decimal digit indicating at most this many leftover arguments allowed (default MAX_INT)</li> |
| 678 | </ul> |
| 679 | |
| 680 | <p>The following characters may be appended to an option character, but do |
| 681 | not by themselves indicate an extra argument should be saved in this[]. |
| 682 | (Technically any character not recognized as a control character sets an |
| 683 | optflag, but letters are never control characters.)</p> |
| 684 | |
| 685 | <ul> |
| 686 | <li><b>^</b> - stop parsing options after encountering this option, everything else goes into optargs.</li> |
| 687 | <li><b>|</b> - this option is required. If more than one marked, only one is required.</li> |
| 688 | <li><b>+X</b> enabling this option also enables option X (switch bit on).</li> |
| 689 | <li><b>~X</b> enabling this option disables option X (switch bit off).</li> |
| 690 | <li><b>!X</b> this option cannot be used in combination with X (die with error).</li> |
| 691 | <li><b>[yz]</b> this option requires at least one of y or z to also be enabled.</li> |
| 692 | </ul> |
| 693 | |
Rob Landley | b6063de | 2012-01-29 13:54:13 -0600 | [diff] [blame] | 694 | <p>The following may be appended to a float or double:</p> |
| 695 | |
| 696 | <ul> |
| 697 | <li><b><123</b> - error if argument is less than this</li> |
| 698 | <li><b>>123</b> - error if argument is greater than this</li> |
| 699 | <li><b>=123</b> - default value if argument not supplied</li> |
| 700 | </ul> |
| 701 | |
| 702 | <p>Option parsing only understands <>= after . when CFG_TOYBOX_FLOAT |
| 703 | is enabled. (Otherwise the code to determine where floating point constants |
| 704 | end drops out. When disabled, it can reserve a global data slot for the |
| 705 | argument so offsets won't change, but will never fill it out.). You can handle |
| 706 | this by using the USE_BLAH() macros with C string concatenation, ala:</p> |
| 707 | |
| 708 | <blockquote>"abc." USE_TOYBOX_FLOAT("<1.23>4.56=7.89") "def"</blockquote> |
| 709 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 710 | <p><b>--longopts</b></p> |
| 711 | |
| 712 | <p>The optflags string can contain long options, which are enclosed in |
| 713 | parentheses. They may be appended to an existing option character, in |
| 714 | which case the --longopt is a synonym for that option, ala "a:(--fred)" |
| 715 | which understands "-a blah" or "--fred blah" as synonyms.</p> |
| 716 | |
| 717 | <p>Longopts may also appear before any other options in the optflags string, |
| 718 | in which case they have no corresponding short argument, but instead set |
| 719 | their own bit based on position. So for "(walrus)#(blah)xy:z" "command |
| 720 | --walrus 42" would set toys.optflags = 16 (-z = 1, -y = 2, -x = 4, --blah = 8) |
| 721 | and would assign this[1] = 42;</p> |
| 722 | |
| 723 | <p>A short option may have multiple longopt synonyms, "a(one)(two)", but |
| 724 | each "bare longopt" (ala "(one)(two)abc" before any option characters) |
| 725 | always sets its own bit (although you can group them with +X).</p> |
Rob Landley | 7c04f01 | 2008-01-20 19:00:16 -0600 | [diff] [blame] | 726 | |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 727 | <h2>Directory scripts/</h2> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 728 | |
| 729 | <h3>scripts/cfg2files.sh</h3> |
| 730 | |
| 731 | <p>Run .config through this filter to get a list of enabled commands, which |
| 732 | is turned into a list of files in toys via a sed invocation in the top level |
| 733 | Makefile. |
| 734 | </p> |
| 735 | |
Rob Landley | 81b899d | 2007-12-18 02:02:47 -0600 | [diff] [blame] | 736 | <h2>Directory kconfig/</h2> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 737 | |
| 738 | <p>Menuconfig infrastructure copied from the Linux kernel. See the |
| 739 | Linux kernel's Documentation/kbuild/kconfig-language.txt</p> |
| 740 | |
Rob Landley | 66a69d9 | 2012-01-16 01:44:17 -0600 | [diff] [blame] | 741 | <a name="generated"> |
| 742 | <h2>Directory generated/</h2> |
| 743 | |
| 744 | <p>All the files in this directory except the README are generated by the |
| 745 | build. (See scripts/make.sh)</p> |
| 746 | |
| 747 | <ul> |
| 748 | <li><p><b>config.h</b> - CFG_COMMAND and USE_COMMAND() macros set by menuconfig via .config.</p></li> |
| 749 | |
| 750 | <li><p><b>Config.in</b> - Kconfig entries for each command. Included by top level Config.in. The help text in here is used to generated help.h</p></li> |
| 751 | |
| 752 | <li><p><b>help.h</b> - Help text strings for use by "help" command. Building |
| 753 | this file requires python on the host system, so the prebuilt file is shipped |
| 754 | in the build tarball to avoid requiring python to build toybox.</p></li> |
| 755 | |
| 756 | <li><p><b>newtoys.h</b> - List of NEWTOY() or OLDTOY() macros for all available |
| 757 | commands. Associates command_main() functions with command names, provides |
| 758 | option string for command line parsing (<a href="#lib_args">see lib/args.c</a>), |
| 759 | specifies where to install each command and whether toysh should fork before |
| 760 | calling it.</p></li> |
| 761 | </ul> |
| 762 | |
| 763 | <p>Everything in this directory is a derivative file produced from something |
| 764 | else. The entire directory is deleted by "make distclean".</p> |
Rob Landley | 4e68de1 | 2007-12-13 07:00:27 -0600 | [diff] [blame] | 765 | <!--#include file="footer.html" --> |