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