cmdla.c 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
/*
 * cmdla.c: Some small function to make command line argument parsing
 *    more convinient. Actually this is only intended for single
 *    character arguments.
 *    COMMENT: This is only appropriate for default application that do not 
 *             want to do special things like assining same parameters to 
 *             different files. For an example of such an application see sox.
 *             But this behavier can be, at least to some level achieved by 
 *             using an appropriate callback, that saves all paramters in a
 *             special manner, associated to the given filename that in turn 
 *             needs to be an option too.
 *
 * Copyright (C) 2006 Georg Steffers. All rights reserved.
 *
 * This software is licensed under the terms of the GNU Genral Public
 * License (GPL). Please see gpl.txt for details or refer to 
 * http://www.gnu.org/licenses/gpl.html
 *
 * Author: Georg Steffers <georg@steffers.org>
 *
 * 01/14/2006: Georg Steffers - First implemented
 * 01/15/2006: Georg Steffers - V0.1 ready. Modified this and that.
 *                              now long arguments are supported as
 *                              well as arguments with am optional
 *                              parameter.
 * 01/23/2006: Georg Steffers - add support for gettext.
 * 01/24/2006: Georg Steffers - changes so that cmdla uses the textdomain
 *                              of san (the lib cmdla belongs to) for the
 *                              few strings that introduces itself, namely
 *                              "help", "gives this help", "switches",
 *                              "arguments" and the formatstring
 *                              "usage: %s [%s] [%s] %s". All other
 *                              strings given to process_cmd_line from
 *                              within a program are translated with
 *                              the textdomain of that program, e.g. 
 *                              the function should always be called with
 *                              the untranslated strings. This is done to make
 *                              it possible to call it with constant strings
 *                              declared before any call of bindtextdomain
 *                              or textdomain.
 */

#define MAX_ARGS           200


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include <scot/cmdla.h>
#include <scot/memory.h>

/*
 * search for an appropriate cb to @name in @pcbs and @scbs and call
 * it. If none is found call the help cb.
 */
static 
int
call_cb (
      const struct cmdlap_cbt *pcbs, 
      const struct cmdlas_cbt *scbs, 
      const char              *optarg,
      const char              *name,
      const char              val)
{
   int   cbs_idx;

   bindtextdomain (PACKAGE, LOCALEDIR);

   cbs_idx = 0;
   while (pcbs[cbs_idx].cb != NULL)
   {
      if ((name != NULL && pcbs[cbs_idx].sarg == name) ||
            (val != 0 && pcbs[cbs_idx].carg == val))
      {
         switch (pcbs[cbs_idx].cmdla_argt) {
            case CMDLA_REQ_ARG:
               if (optarg == NULL)
                  return call_cb (pcbs, scbs, optarg, D_("help"), '?');

               /* we got an arg, so fallthrough CMDLA_OPT_ARG to get
                * it processed */
            case CMDLA_OPT_ARG:
               return 
                  pcbs[cbs_idx].cb (
                        optarg, 
                        pcbs[cbs_idx].cmdla_type,
                        pcbs[cbs_idx].var);
         }
      }

      cbs_idx++;
   }

   cbs_idx = 0;
   while (scbs[cbs_idx].cb != NULL)
   {
      if ((name != NULL && scbs[cbs_idx].sarg == name) ||
            (val != 0 && scbs[cbs_idx].carg == val))
         return scbs[cbs_idx].cb (scbs[cbs_idx].var);

      cbs_idx++;
   }

   return call_cb (pcbs, scbs, optarg, D_("help"), '?');
}

/*
 * If no usage_cb is given to cmdla, then create a basic default usage.
 */
int
default_usage_cb (void *info)
{
   struct du_infot         *du_infop;
   const struct cmdlas_cbt *sw;
   const struct cmdlap_cbt *ar;

   bindtextdomain (PACKAGE, LOCALEDIR);

   du_infop = (struct du_infot *) info;
   sw = du_infop->switches;
   ar = du_infop->arguments;

   if (du_infop->about != NULL)
      fprintf (stderr, du_infop->about);

   if (du_infop->copyright != NULL)
   {
      fprintf (stderr, "\n\n");
      fprintf (stderr, du_infop->copyright);
   }

   fprintf (stderr, "\n\n");
   fprintf (
         stderr, 
         D_("Usage: %s [%s] [%s] %s"),
         du_infop->program_name, 
         D_("switches"), D_("arguments"),
         du_infop->usage_aa != NULL ? du_infop->usage_aa : "");
   fprintf (stderr, "\n\n");

   fprintf (stderr, "\t%s: \n", D_("switches"));

   while (sw != NULL && sw->cb != NULL)
   {
      fprintf (
            stderr, 
            "\t  %c%c%s%-7s : %-45s\n", 
            sw->carg != 0 ? '-' : ' ',
            sw->carg != 0 ? sw->carg : ' ',
            (sw->sarg != NULL && 
             sw->carg != 0) ? " / --" : 
            (sw->sarg != NULL &&
             sw->carg == 0) ? "   --" : "     ",
            sw->sarg != NULL ? sw->sarg : "",
            sw->info != NULL ? sw->info : "");

      sw++;
   }

   fprintf (stderr, "\t%s: \n", D_("arguments"));

   while (ar != NULL && ar->cb != NULL)
   {
      fprintf (
            stderr,
            "\t  %c%c%-8s%s%s%-10s : %-20s\n",
            ar->carg != 0 ? '-' : ' ',
            ar->carg != 0 ? ar->carg : ' ',
            (ar->cmdla_argt != CMDLA_OPT_ARG &&
             ar->carg != 0) ? "[ ]<val>" : 
            (ar->carg == 0) ? "" : "[val]",
            (ar->sarg != NULL && 
             ar->carg != 0) ? " | --" :
            (ar->sarg != NULL &&
             ar->carg == 0) ? "   --" : "     ",
            ar->sarg != NULL ?  ar->sarg : "",
            (ar->cmdla_argt != CMDLA_OPT_ARG &&
             ar->sarg != NULL) ? "[=| ]<val>" : 
            (ar->sarg == NULL) ? "" : "[=val]",
            ar->info != NULL ? ar->info : "");

      ar++;
   }

   exit(-1);
}

int 
switch_cb (void *arg)
{
   int * sw = (int *) arg;

   *sw = (*sw == 0)?1:0;
}

int 
inc_cb (void *arg)
{
   int * sw = (int *) arg;

   *sw = *sw + 1;
}


/*
 * The default callback for process_cmd_line to get a parameter saved to
 * a variable of the correct type.
 * Converts @param to the Type specified in the @type and saves it into
 * the pointer to a variable delivered in @arg
 */
int 
get_cmdlap_cb (
      const char  *param, 
      const int   cmdla_type, 
      void        *arg)
{
   if (param != NULL)
      switch (cmdla_type)
      {
         case CMDLA_TYPE_STRING:
            * (const char **) arg = param;
            break;
         case CMDLA_TYPE_INT:
            sscanf (param, "%d", (int *) arg);
            break;
         case CMDLA_TYPE_FLOAT:
            sscanf (param, "%f", (float *) arg);
      }

   return 0;
}

/*
 * Processes command line patameters
 */
int 
process_cmd_line (
      int                     argc, 
      char                    *argv[], 
      const char              *about,
      const char              *copyright,
      const char              *usage_aa,  /* usage additional args */
      const struct cmdlap_cbt *pcbs,
      const struct cmdlas_cbt *scbs)
{
   char              optstring[MAX_ARGS];
   int               option_index = 0;
   struct option     long_opts[MAX_ARGS];

   int               cbs_idx, optstring_idx, long_opts_idx;
   int               has_u = 0;
   int               ch;

   struct cmdlas_cbt switches[MAX_ARGS];
   struct cmdlap_cbt arguments[MAX_ARGS];

   struct du_infot   du_info = { 
      switches, arguments, _(about), _(copyright), _(usage_aa), argv[0] };

   bindtextdomain (PACKAGE, LOCALEDIR);

   /*
    * Every command line argument, either with or without an additional
    * parameter is associated with a callback function, which in turn
    * does the neccessaty initialization. Callbacks for switches get an
    * void* as parameter and callbacks for parameter arguments get the
    * parameter as a const char*, the datatype the parameter has and
    * a pointer to the variable that should hold the parameter throughout
    * the program.
    */

   /* 
    * create optstring and long_opts for getopt_long().
    * As this should be called with the untranslated messages
    * we need to call _() to put the translated ones into optstring
    * and long_opts. Also we need to update the values in switches and 
    * arguments must be converted to the translated strings.
    */

   /* Zero out optsting and long_opts */
   SCOT_MEM_FILL (optstring, 0, MAX_ARGS);
   SCOT_MEM_FILL (long_opts, 0, MAX_ARGS * sizeof (struct option));

   /* zero switches and copy scbs to switches */
   SCOT_MEM_FILL (switches, 0, MAX_ARGS * sizeof (struct cmdlas_cbt));
   cbs_idx = 0;
   while (scbs != NULL && scbs[cbs_idx].cb != NULL)
   {
      SCOT_MEM_COPY (
            &(switches[cbs_idx]), 
            &(scbs[cbs_idx]), 
            sizeof (struct cmdlas_cbt));
      switches[cbs_idx].sarg  = _(scbs[cbs_idx].sarg);
      switches[cbs_idx].info  = _(scbs[cbs_idx].info);

      cbs_idx++;
   }

   /* zero arguments and copy scbs to switches */
   SCOT_MEM_FILL (arguments, 0, MAX_ARGS * sizeof (struct cmdlap_cbt));
   cbs_idx = 0;
   while (pcbs != NULL && pcbs[cbs_idx].cb != NULL)
   {
      SCOT_MEM_COPY (
            &(arguments[cbs_idx]), 
            &(pcbs[cbs_idx]), 
            sizeof (struct cmdlap_cbt));
      arguments[cbs_idx].sarg  = _(pcbs[cbs_idx].sarg);
      arguments[cbs_idx].info  = _(pcbs[cbs_idx].info);

      cbs_idx++;
   }

   /* walk through arguments */
   cbs_idx = optstring_idx = long_opts_idx = 0;
   while (arguments != NULL && arguments[cbs_idx].cb != NULL) {
      if (arguments[cbs_idx].carg != 0)
      {
         optstring[optstring_idx++] = arguments[cbs_idx].carg;

         if (arguments[cbs_idx].carg == '?')
         {
            has_u = 1;
            if (switches[cbs_idx].var == NULL)
            {
               switches[cbs_idx].var = (void *) &du_info;
            }
         }

         switch (arguments[cbs_idx].cmdla_argt)
         {
            case CMDLA_OPT_ARG:  optstring[optstring_idx++] = ':';
            case CMDLA_REQ_ARG:  optstring[optstring_idx++] = ':';
         }
      }

      if (pcbs[cbs_idx].sarg != NULL)
      {
         long_opts[long_opts_idx].name    = arguments[cbs_idx].sarg;
         long_opts[long_opts_idx].has_arg = arguments[cbs_idx].cmdla_argt;
         long_opts[long_opts_idx].val     = arguments[cbs_idx].carg;
         long_opts_idx++;
      }

      cbs_idx++;
   }

   /* and now through switches */
   cbs_idx = 0;
   while (switches != NULL && switches[cbs_idx].cb != NULL) {
      if (switches[cbs_idx].carg != 0)
         optstring[optstring_idx++] = switches[cbs_idx].carg;

      if (switches[cbs_idx].carg == '?')
         has_u = 1;

      if (switches[cbs_idx].sarg != NULL)
      {
         long_opts[long_opts_idx].name    = switches[cbs_idx].sarg;
         long_opts[long_opts_idx].val     = switches[cbs_idx].carg;
         long_opts_idx++;
      }

      cbs_idx++;
   }

   /* if no usage callback is give use our default usage */
   if (has_u == 0)
   {
      switches[cbs_idx].carg   = '?';
      switches[cbs_idx].sarg   = D_("help");
      switches[cbs_idx].cb     = default_usage_cb;
      switches[cbs_idx].var    = (void *) &du_info;
      switches[cbs_idx].info   = D_("gives this help");
      cbs_idx++;
      SCOT_MEM_FILL (&(switches[cbs_idx]), 0, sizeof (struct cmdlas_cbt));

      optstring[optstring_idx++]   = '?';
      long_opts[long_opts_idx].name    = D_("help");
      long_opts[long_opts_idx].val     = '?';
      long_opts_idx++;
   }

   /* 
    * Now parse the command line arguments.
    * Check if the argument appears in switch_cids, or in param_cids, or
    * of the don't if there is a '?'-switch defined and call the
    * appropriate cb if possible.
    */
   ch = getopt_long (argc, argv, optstring, long_opts, &option_index);
   while (ch != EOF)
   {
      const char *name;

      if (ch == 0)
         name = long_opts[option_index].name;
      else
         name = NULL;

      call_cb (arguments, switches, optarg, name, ch);

      ch = getopt_long (argc, argv, optstring, long_opts, &option_index);
   }

   return optind;
}