Mercurial > hg > toybox
annotate toys/other/hello.c @ 1150:ac4a0cde89c2 draft
Don't permute toys.optargs, cleanup code (xexec()) can free it.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 19 Dec 2013 21:38:12 -0600 |
parents | a25239480e7a |
children | 6ca31490f581 |
rev | line source |
---|---|
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
1 /* hello.c - A hello world program. (Template for new commands.) |
194
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
2 * |
403
f6ffc6685a9e
Fluff out documentation and skeleton code.
Rob Landley <rob@landley.net>
parents:
381
diff
changeset
|
3 * Copyright 2012 Rob Landley <rob@landley.net> |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
4 * |
729
aaa638e82e51
The open group changed their website stupidly, so opengroup.org/onlinepubs forwards to a dead server and you have to use pubs.opengroup.org/onlinepubs now. Change the hello template to note the new location. (Waiting to see if they fix it upstream before touching every command in toys/posix.)
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ |
617
a3e014fdeb35
Add LSB link to the template, for reference.
Rob Landley <rob@landley.net>
parents:
403
diff
changeset
|
6 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
7 |
944
b4faf2ae39e8
This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents:
729
diff
changeset
|
8 // Accept many different kinds of command line argument: |
b4faf2ae39e8
This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents:
729
diff
changeset
|
9 |
1036
a25239480e7a
Improve --longopt parsing: general bugfixes, better error reporting, new ; option for optional arguments only suppliable with =.
Rob Landley <rob@landley.net>
parents:
944
diff
changeset
|
10 USE_HELLO(NEWTOY(hello, "(walrus)(blubber):;(also):e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN)) |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
11 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
12 config HELLO |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
13 bool "hello" |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
14 default n |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
15 help |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
16 usage: hello [-a] [-b string] [-c number] [-d list] [-e count] [...] |
403
f6ffc6685a9e
Fluff out documentation and skeleton code.
Rob Landley <rob@landley.net>
parents:
381
diff
changeset
|
17 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
18 A hello world program. You don't need this. |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
19 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
20 Mostly used as an example/skeleton file for adding new commands, |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
21 occasionally nice to test kernel booting via "init=/bin/hello". |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
22 */ |
13
d87d8a056295
Add a hello world applet, partly as an example and partly for testing purposes.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
24 #define FOR_hello |
13
d87d8a056295
Add a hello world applet, partly as an example and partly for testing purposes.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 #include "toys.h" |
d87d8a056295
Add a hello world applet, partly as an example and partly for testing purposes.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 |
239
4f1ca01db000
Fluff out hello.c to supply more example code as a skeleton for new commands,
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
27 // Hello doesn't use these globals, they're here for example/skeleton purposes. |
4f1ca01db000
Fluff out hello.c to supply more example code as a skeleton for new commands,
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
28 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
29 GLOBALS( |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
30 char *b_string; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
31 long c_number; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
32 struct arg_list *d_list; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
33 long e_count; |
1036
a25239480e7a
Improve --longopt parsing: general bugfixes, better error reporting, new ; option for optional arguments only suppliable with =.
Rob Landley <rob@landley.net>
parents:
944
diff
changeset
|
34 char *also_string; |
944
b4faf2ae39e8
This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents:
729
diff
changeset
|
35 char *blubber_string; |
239
4f1ca01db000
Fluff out hello.c to supply more example code as a skeleton for new commands,
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
36 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
37 int more_globals; |
239
4f1ca01db000
Fluff out hello.c to supply more example code as a skeleton for new commands,
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
38 ) |
4f1ca01db000
Fluff out hello.c to supply more example code as a skeleton for new commands,
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
39 |
944
b4faf2ae39e8
This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents:
729
diff
changeset
|
40 // Parse many different kinds of command line argument: |
b4faf2ae39e8
This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents:
729
diff
changeset
|
41 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
13
diff
changeset
|
42 void hello_main(void) |
13
d87d8a056295
Add a hello world applet, partly as an example and partly for testing purposes.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 { |
1150
ac4a0cde89c2
Don't permute toys.optargs, cleanup code (xexec()) can free it.
Rob Landley <rob@landley.net>
parents:
1036
diff
changeset
|
44 char **optargs; |
ac4a0cde89c2
Don't permute toys.optargs, cleanup code (xexec()) can free it.
Rob Landley <rob@landley.net>
parents:
1036
diff
changeset
|
45 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
46 printf("Hello world\n"); |
403
f6ffc6685a9e
Fluff out documentation and skeleton code.
Rob Landley <rob@landley.net>
parents:
381
diff
changeset
|
47 |
944
b4faf2ae39e8
This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents:
729
diff
changeset
|
48 if (toys.optflags) printf("flags=%x\n", toys.optflags); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
49 if (toys.optflags & FLAG_a) printf("Saw a\n"); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
50 if (toys.optflags & FLAG_b) printf("b=%s\n", TT.b_string); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
51 if (toys.optflags & FLAG_c) printf("c=%ld\n", TT.c_number); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
52 while (TT.d_list) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
53 printf("d=%s\n", TT.d_list->arg); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
54 TT.d_list = TT.d_list->next; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
55 } |
944
b4faf2ae39e8
This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents:
729
diff
changeset
|
56 if (TT.e_count) printf("e was seen %ld times\n", TT.e_count); |
1150
ac4a0cde89c2
Don't permute toys.optargs, cleanup code (xexec()) can free it.
Rob Landley <rob@landley.net>
parents:
1036
diff
changeset
|
57 for (optargs = toys.optargs; *optargs; optargs++) |
ac4a0cde89c2
Don't permute toys.optargs, cleanup code (xexec()) can free it.
Rob Landley <rob@landley.net>
parents:
1036
diff
changeset
|
58 printf("optarg=%s\n", *optargs); |
944
b4faf2ae39e8
This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents:
729
diff
changeset
|
59 if (toys.optflags & FLAG_walrus) printf("Saw --walrus\n"); |
b4faf2ae39e8
This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents:
729
diff
changeset
|
60 if (TT.blubber_string) printf("--blubber=%s\n", TT.blubber_string); |
13
d87d8a056295
Add a hello world applet, partly as an example and partly for testing purposes.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 } |