[Toybox] HOWTO: File with multiple commands.

Rob Landley rob at landley.net
Mon Feb 24 21:41:23 PST 2014


Attached is an extended version of hello.c showing how to implement
multiple commands with different arguments in the same file. (This
allows them to share infrastructure that doesn't live in lib/lib.c.)

To switch from one command to another, you do this:

#define CLEANUP_oldcommand
#define FOR_newcommand
#include "generated/flags.h"

Most of the infrastructure was already in place, the recent fixes were
things like not undefining TT and redefining it (which didn't work).
Instead, you need to put a union at the start of GLOBALS with the
variables the command line option parsing logic populates, and then the
other global variables after that. That way the layout's correct for the
commands, and both their FLAG_ and TT. values are usable from both
contexts. (You have to use the union name to access the variables, but
oh well.)

Plus a lot of header generation fixes...

I needed this for gzip/zlib/zip to share deflate code without putting it
in /lib. (Tar can fork and pipe data through gzip, as is the unix way.)
I've also moved bunzip2 out of lib and into the command using it: bzcat
and bunzip2 aren't enough reason to slow down every single command
build. Especially with scripts/single.sh builds, significant extra lib
code is noticeable.

I've decided _not_ to complicate hello.c with the extra plumbing, it's
complicated enough as it is, but here's how it would look if I did.

I should add this to the documentation.

Rob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hello.c
Type: text/x-csrc
Size: 2439 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20140224/07925375/attachment.c>


More information about the Toybox mailing list