1 /* skeleton.c - Example program to act as template for new commands.
2 * (Although really, half the time copying hello.c is easier.)
3 *
4 * Copyright 2014 Rob Landley <rob@landley.net>
5 *
6 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
7 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html
8 * See https://www.ietf.org/rfc/rfc3.txt
9 * See http://man7.org/linux/man-pages/dir_section_1.html
10
11 // Accept many different kinds of command line argument (see top of lib/args.c)
12 // Demonstrate two commands in the same file (see www/documentation.html)
13
14 USE_SKELETON(NEWTOY(skeleton, "(walrus)(blubber):;(also):e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN))
15 USE_SKELETON_ALIAS(NEWTOY(skeleton_alias, "b#dq", TOYFLAG_USR|TOYFLAG_BIN))
16
17 config SKELETON
18 bool "skeleton"
19 default n
20 help
21 usage: skeleton [-a] [-b STRING] [-c NUMBER] [-d LIST] [-e COUNT] [...]
22
23 Template for new commands. You don't need this.
24
25 When creating a new command, copy this file and delete the parts you
26 don't need. Be sure to replace all instances of "skeleton" (upper and lower
27 case) with your new command name.
28
29 For simple commands, "hello.c" is probably a better starting point.
30
31 config SKELETON_ALIAS
32 bool "skeleton_alias"
33 default n
34 help
35 usage: skeleton_alias [-dq] [-b NUMBER]
36
37 Example of a second command with different arguments in the same source
38 file as the first. This allows shared infrastructure not added to lib/.
39 */
40
41 #define FOR_skeleton
42 #include "toys.h"
43
44 // The union lets lib/args.c store arguments for either command.
45 // It's customary to put a space between argument variables and other globals.
GLOBALS(union{ struct { char *b; long c; struct arg_list *d; long e; char *also, *blubber; } s; struct { long b; } a; };int more_globals;)46 GLOBALS(
47 union {
48 struct {
49 char *b;
50 long c;
51 struct arg_list *d;
52 long e;
53 char *also, *blubber;
54 } s;
55 struct {
56 long b;
57 } a;
58 };
59
60 int more_globals;
61 )
62
63 // Don't blindly build allyesconfig. The maximum _sane_ config is defconfig.
64 #warning skeleton.c is just an example, not something to deploy.
65
66 // Parse many different kinds of command line argument:
67 void skeleton_main(void)
68 {
69 char **optargs;
70
71 printf("Ran %s\n", toys.which->name);
72
73 // Command line options parsing is done for you by lib/args.c called
74 // from main.c using the optstring in the NEWTOY macros. Display results.
75 if (toys.optflags) printf("flags=%llx\n", toys.optflags);
76 if (FLAG(a)) printf("Saw a\n");
77 if (FLAG(b)) printf("b=%s\n", TT.s.b);
78 if (FLAG(c)) printf("c=%ld\n", TT.s.c);
79 while (TT.s.d) {
80 printf("d=%s\n", TT.s.d->arg);
81 TT.s.d = TT.s.d->next;
82 }
83 if (TT.s.e) printf("e was seen %ld times\n", TT.s.e);
84 for (optargs = toys.optargs; *optargs; optargs++)
85 printf("optarg=%s\n", *optargs);
86 if (FLAG(walrus)) printf("Saw --walrus\n");
87 if (TT.s.blubber) printf("--blubber=%s\n", TT.s.blubber);
88
89 printf("Other globals should start zeroed: %d\n", TT.more_globals);
90 }
91
92 // Switch gears from skeleton to skeleton_alias (swap FLAG macros).
93 #define CLEANUP_skeleton
94 #define FOR_skeleton_alias
95 #include "generated/flags.h"
96
skeleton_alias_main(void)97 void skeleton_alias_main(void)
98 {
99 printf("Ran %s\n", toys.which->name);
100 printf("flags=%llx\n", toys.optflags);
101
102 // Note, this FLAG_b is a different bit position than the other FLAG_b,
103 // and fills out a different variable of a different type.
104 if (FLAG(b)) printf("b=%ld", TT.a.b);
105 }
106