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 (at) 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. 45 GLOBALS( 46 union { 47 struct { 48 char *b; 49 long c; 50 struct arg_list *d; 51 long e; 52 char *also, *blubber; 53 } s; 54 struct { 55 long b; 56 } a; 57 }; 58 59 int more_globals; 60 ) 61 62 // Don't blindly build allyesconfig. The maximum _sane_ config is defconfig. 63 #warning skeleton.c is just an example, not something to deploy. 64 65 // Parse many different kinds of command line argument: 66 void skeleton_main(void) 67 { 68 char **optargs; 69 70 printf("Ran %s\n", toys.which->name); 71 72 // Command line options parsing is done for you by lib/args.c called 73 // from main.c using the optstring in the NEWTOY macros. Display results. 74 if (toys.optflags) printf("flags=%llx\n", toys.optflags); 75 if (toys.optflags & FLAG_a) printf("Saw a\n"); 76 if (toys.optflags & FLAG_b) printf("b=%s\n", TT.s.b); 77 if (toys.optflags & FLAG_c) printf("c=%ld\n", TT.s.c); 78 while (TT.s.d) { 79 printf("d=%s\n", TT.s.d->arg); 80 TT.s.d = TT.s.d->next; 81 } 82 if (TT.s.e) printf("e was seen %ld times\n", TT.s.e); 83 for (optargs = toys.optargs; *optargs; optargs++) 84 printf("optarg=%s\n", *optargs); 85 if (toys.optflags & FLAG_walrus) printf("Saw --walrus\n"); 86 if (TT.s.blubber) printf("--blubber=%s\n", TT.s.blubber); 87 88 printf("Other globals should start zeroed: %d\n", TT.more_globals); 89 } 90 91 // Switch gears from skeleton to skeleton_alias (swap FLAG macros). 92 #define CLEANUP_skeleton 93 #define FOR_skeleton_alias 94 #include "generated/flags.h" 95 96 void skeleton_alias_main(void) 97 { 98 printf("Ran %s\n", toys.which->name); 99 printf("flags=%llx\n", toys.optflags); 100 101 // Note, this FLAG_b is a different bit position than the other FLAG_b, 102 // and fills out a different variable of a different type. 103 if (toys.optflags & FLAG_b) printf("b=%ld", TT.a.b); 104 } 105