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 https://man7.org/linux/man-pages/man1/intro.1.html 10 * No standard. 11 12// Accept many different kinds of command line argument (see top of lib/args.c) 13// Demonstrate two commands in the same file (see www/documentation.html) 14 15USE_SKELETON(NEWTOY(skeleton, "(walrus)(blubber):;(also):h(hlong):; g(glong): f(longf):;e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN)) 16USE_SKELETON_ALIAS(NEWTOY(skeleton_alias, "b#dq", TOYFLAG_USR|TOYFLAG_BIN)) 17 18config SKELETON 19 bool "skeleton" 20 default n 21 help 22 usage: skeleton [-a] [-b STRING] [-c NUMBER] [-d LIST] [-e COUNT] [...] 23 24 Template for new commands. You don't need this. 25 26 When creating a new command, copy this file and delete the parts you 27 don't need. Be sure to replace all instances of "skeleton" (upper and lower 28 case) with your new command name. 29 30 For simple commands, "hello.c" is probably a better starting point. 31 32config SKELETON_ALIAS 33 bool "skeleton_alias" 34 default n 35 help 36 usage: skeleton_alias [-dq] [-b NUMBER] 37 38 Example of a second command with different arguments in the same source 39 file as the first. This allows shared infrastructure outside of lib/. 40*/ 41 42#define FOR_skeleton 43#include "toys.h" 44 45// The union lets lib/args.c store arguments for either command. 46// It's customary to put a space between argument variables and other globals. 47GLOBALS( 48 union { 49 struct { 50 char *b; 51 long c; 52 struct arg_list *d; 53 long e; 54 char *f, *g, *h, *also, *blubber; 55 } s; 56 struct { 57 long b; 58 } a; 59 }; 60 61 int more_globals; 62) 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=%llx\n", toys.optflags); 74 if (FLAG(a)) printf("Saw a\n"); 75 if (FLAG(b)) printf("b=%s\n", TT.s.b); 76 if (FLAG(c)) printf("c=%ld\n", TT.s.c); 77 while (TT.s.d) { 78 printf("d=%s\n", TT.s.d->arg); 79 TT.s.d = TT.s.d->next; 80 } 81 if (TT.s.e) printf("e was seen %ld times\n", TT.s.e); 82 if (TT.s.f) printf("f=%s\n", TT.s.f); 83 if (TT.s.g) printf("g=%s\n", TT.s.g); 84 if (TT.s.h) printf("h=%s\n", TT.s.h); 85 for (optargs = toys.optargs; *optargs; optargs++) 86 printf("optarg=%s\n", *optargs); 87 if (FLAG(walrus)) printf("Saw --walrus\n"); 88 if (TT.s.blubber) printf("--blubber=%s\n", TT.s.blubber); 89 90 printf("Other globals should start zeroed: %d\n", TT.more_globals); 91} 92 93// Switch gears from skeleton to skeleton_alias (swap FLAG macros). 94#define FOR_skeleton_alias 95#include "generated/flags.h" 96 97void 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