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