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
10 // Accept many different kinds of command line argument (see top of lib/args.c)
11 // Demonstrate two commands in the same file (see www/documentation.html)
12
13 USE_SKELETON(NEWTOY(skeleton, "(walrus)(blubber):;(also):e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN))
14 USE_SKELETON_ALIAS(NEWTOY(skeleton_alias, "b#dq", TOYFLAG_USR|TOYFLAG_BIN))
15
16 config SKELETON
17 bool "skeleton"
18 default n
19 help
20 usage: skeleton [-a] [-b STRING] [-c NUMBER] [-d LIST] [-e COUNT] [...]
21
22 Template for new commands. You don't need this.
23
24 When creating a new command, copy this file and delete the parts you
25 don't need. Be sure to replace all instances of "skeleton" (upper and lower
26 case) with your new command name.
27
28 For simple commands, "hello.c" is probably a better starting point.
29
30 config SKELETON_ALIAS
31 bool "skeleton_alias"
32 default n
33 help
34 usage: skeleton_alias [-dq] [-b NUMBER]
35
36 Example of a second command with different arguments in the same source
37 file as the first. This allows shared infrastructure not added to lib/.
38 */
39
40 #define FOR_skeleton
41 #include "toys.h"
42
43 // The union lets lib/args.c store arguments for either command.
44 // It's customary to put a space between argument variables and other globals.
GLOBALS(union{ struct { char *b_string; long c_number; struct arg_list *d_list; long e_count; char *also_string; char *blubber_string; } s; struct { long b_number; } a; };int more_globals;)45 GLOBALS(
46 union {
47 struct {
48 char *b_string;
49 long c_number;
50 struct arg_list *d_list;
51 long e_count;
52 char *also_string;
53 char *blubber_string;
54 } s;
55 struct {
56 long b_number;
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 (toys.optflags & FLAG_a) printf("Saw a\n");
77 if (toys.optflags & FLAG_b) printf("b=%s\n", TT.s.b_string);
78 if (toys.optflags & FLAG_c) printf("c=%ld\n", TT.s.c_number);
79 while (TT.s.d_list) {
80 printf("d=%s\n", TT.s.d_list->arg);
81 TT.s.d_list = TT.s.d_list->next;
82 }
83 if (TT.s.e_count) printf("e was seen %ld times\n", TT.s.e_count);
84 for (optargs = toys.optargs; *optargs; optargs++)
85 printf("optarg=%s\n", *optargs);
86 if (toys.optflags & FLAG_walrus) printf("Saw --walrus\n");
87 if (TT.s.blubber_string) printf("--blubber=%s\n", TT.s.blubber_string);
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 (toys.optflags & FLAG_b) printf("b=%ld", TT.a.b_number);
105 }
106