10f66f451Sopenharmony_ci/* skeleton.c - Example program to act as template for new commands. 20f66f451Sopenharmony_ci * (Although really, half the time copying hello.c is easier.) 30f66f451Sopenharmony_ci * 40f66f451Sopenharmony_ci * Copyright 2014 Rob Landley <rob@landley.net> 50f66f451Sopenharmony_ci * 60f66f451Sopenharmony_ci * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ 70f66f451Sopenharmony_ci * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html 80f66f451Sopenharmony_ci * See https://www.ietf.org/rfc/rfc3.txt 90f66f451Sopenharmony_ci * See http://man7.org/linux/man-pages/dir_section_1.html 100f66f451Sopenharmony_ci 110f66f451Sopenharmony_ci// Accept many different kinds of command line argument (see top of lib/args.c) 120f66f451Sopenharmony_ci// Demonstrate two commands in the same file (see www/documentation.html) 130f66f451Sopenharmony_ci 140f66f451Sopenharmony_ciUSE_SKELETON(NEWTOY(skeleton, "(walrus)(blubber):;(also):e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN)) 150f66f451Sopenharmony_ciUSE_SKELETON_ALIAS(NEWTOY(skeleton_alias, "b#dq", TOYFLAG_USR|TOYFLAG_BIN)) 160f66f451Sopenharmony_ci 170f66f451Sopenharmony_ciconfig SKELETON 180f66f451Sopenharmony_ci bool "skeleton" 190f66f451Sopenharmony_ci default n 200f66f451Sopenharmony_ci help 210f66f451Sopenharmony_ci usage: skeleton [-a] [-b STRING] [-c NUMBER] [-d LIST] [-e COUNT] [...] 220f66f451Sopenharmony_ci 230f66f451Sopenharmony_ci Template for new commands. You don't need this. 240f66f451Sopenharmony_ci 250f66f451Sopenharmony_ci When creating a new command, copy this file and delete the parts you 260f66f451Sopenharmony_ci don't need. Be sure to replace all instances of "skeleton" (upper and lower 270f66f451Sopenharmony_ci case) with your new command name. 280f66f451Sopenharmony_ci 290f66f451Sopenharmony_ci For simple commands, "hello.c" is probably a better starting point. 300f66f451Sopenharmony_ci 310f66f451Sopenharmony_ciconfig SKELETON_ALIAS 320f66f451Sopenharmony_ci bool "skeleton_alias" 330f66f451Sopenharmony_ci default n 340f66f451Sopenharmony_ci help 350f66f451Sopenharmony_ci usage: skeleton_alias [-dq] [-b NUMBER] 360f66f451Sopenharmony_ci 370f66f451Sopenharmony_ci Example of a second command with different arguments in the same source 380f66f451Sopenharmony_ci file as the first. This allows shared infrastructure not added to lib/. 390f66f451Sopenharmony_ci*/ 400f66f451Sopenharmony_ci 410f66f451Sopenharmony_ci#define FOR_skeleton 420f66f451Sopenharmony_ci#include "toys.h" 430f66f451Sopenharmony_ci 440f66f451Sopenharmony_ci// The union lets lib/args.c store arguments for either command. 450f66f451Sopenharmony_ci// It's customary to put a space between argument variables and other globals. 460f66f451Sopenharmony_ciGLOBALS( 470f66f451Sopenharmony_ci union { 480f66f451Sopenharmony_ci struct { 490f66f451Sopenharmony_ci char *b; 500f66f451Sopenharmony_ci long c; 510f66f451Sopenharmony_ci struct arg_list *d; 520f66f451Sopenharmony_ci long e; 530f66f451Sopenharmony_ci char *also, *blubber; 540f66f451Sopenharmony_ci } s; 550f66f451Sopenharmony_ci struct { 560f66f451Sopenharmony_ci long b; 570f66f451Sopenharmony_ci } a; 580f66f451Sopenharmony_ci }; 590f66f451Sopenharmony_ci 600f66f451Sopenharmony_ci int more_globals; 610f66f451Sopenharmony_ci) 620f66f451Sopenharmony_ci 630f66f451Sopenharmony_ci// Don't blindly build allyesconfig. The maximum _sane_ config is defconfig. 640f66f451Sopenharmony_ci#warning skeleton.c is just an example, not something to deploy. 650f66f451Sopenharmony_ci 660f66f451Sopenharmony_ci// Parse many different kinds of command line argument: 670f66f451Sopenharmony_civoid skeleton_main(void) 680f66f451Sopenharmony_ci{ 690f66f451Sopenharmony_ci char **optargs; 700f66f451Sopenharmony_ci 710f66f451Sopenharmony_ci printf("Ran %s\n", toys.which->name); 720f66f451Sopenharmony_ci 730f66f451Sopenharmony_ci // Command line options parsing is done for you by lib/args.c called 740f66f451Sopenharmony_ci // from main.c using the optstring in the NEWTOY macros. Display results. 750f66f451Sopenharmony_ci if (toys.optflags) printf("flags=%llx\n", toys.optflags); 760f66f451Sopenharmony_ci if (FLAG(a)) printf("Saw a\n"); 770f66f451Sopenharmony_ci if (FLAG(b)) printf("b=%s\n", TT.s.b); 780f66f451Sopenharmony_ci if (FLAG(c)) printf("c=%ld\n", TT.s.c); 790f66f451Sopenharmony_ci while (TT.s.d) { 800f66f451Sopenharmony_ci printf("d=%s\n", TT.s.d->arg); 810f66f451Sopenharmony_ci TT.s.d = TT.s.d->next; 820f66f451Sopenharmony_ci } 830f66f451Sopenharmony_ci if (TT.s.e) printf("e was seen %ld times\n", TT.s.e); 840f66f451Sopenharmony_ci for (optargs = toys.optargs; *optargs; optargs++) 850f66f451Sopenharmony_ci printf("optarg=%s\n", *optargs); 860f66f451Sopenharmony_ci if (FLAG(walrus)) printf("Saw --walrus\n"); 870f66f451Sopenharmony_ci if (TT.s.blubber) printf("--blubber=%s\n", TT.s.blubber); 880f66f451Sopenharmony_ci 890f66f451Sopenharmony_ci printf("Other globals should start zeroed: %d\n", TT.more_globals); 900f66f451Sopenharmony_ci} 910f66f451Sopenharmony_ci 920f66f451Sopenharmony_ci// Switch gears from skeleton to skeleton_alias (swap FLAG macros). 930f66f451Sopenharmony_ci#define CLEANUP_skeleton 940f66f451Sopenharmony_ci#define FOR_skeleton_alias 950f66f451Sopenharmony_ci#include "generated/flags.h" 960f66f451Sopenharmony_ci 970f66f451Sopenharmony_civoid skeleton_alias_main(void) 980f66f451Sopenharmony_ci{ 990f66f451Sopenharmony_ci printf("Ran %s\n", toys.which->name); 1000f66f451Sopenharmony_ci printf("flags=%llx\n", toys.optflags); 1010f66f451Sopenharmony_ci 1020f66f451Sopenharmony_ci // Note, this FLAG_b is a different bit position than the other FLAG_b, 1030f66f451Sopenharmony_ci // and fills out a different variable of a different type. 1040f66f451Sopenharmony_ci if (FLAG(b)) printf("b=%ld", TT.a.b); 1050f66f451Sopenharmony_ci} 106