cmdla.h 3.84 KB
/*
 * cmdla.h: Prototypes, defines, etc. for cmdla
 *
 * 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.
 */

#ifndef CMDLA_H
#define CMDLA_H

/* Datatypes of given commandline values */
#define CMDLA_TYPE_STRING  0x00
#define CMDLA_TYPE_INT     0x01
#define CMDLA_TYPE_FLOAT   0x02

/* Indicates wether an arg requires an parameter or not */
#define CMDLA_REQ_ARG      0x01
#define CMDLA_OPT_ARG      0x02

#define CMDLAP_CBT_END     { 0, NULL, 0, NULL, 0, NULL, NULL }
#define CMDLAS_CBT_END     { 0, NULL, NULL, NULL, NULL }

#define _GNU_SOURCE        /* for getopt */

#include <unistd.h>
#include <wchar.h>
#include <getopt.h>

#include <scot_common.h>

/*
 * This struct holds all neccesary infomation for the default
 * usage callback. It is filled within process_cmd_line and used
 * with any usage function that did not specify explicitly different
 * behaviour.
 */
struct du_infot
{
   const struct cmdlas_cbt *switches;
   const struct cmdlap_cbt *arguments;
   const char              *about;
   const char              *copyright;
   const char              *usage_aa;
   const char              *program_name;
};


typedef int (*cmdlap_cb)  (const char *, const int, void *);
typedef int (*cmdlas_cb)  (void *);

/* 
 * CoMmanDLine Argument Parameter CallBack Type.
 * This holds the definition of an argument that takes a parameter.
 */
struct cmdlap_cbt {
   char        carg;             /* The single letter argument e.g. -a */
   char        *sarg;            /* The string argument e.g. --arg */
   int         cmdla_argt;       /* Datatype the arg take e.g. CMDLA_TYPE_INT */
   cmdlap_cb   cb;               /* pointer to the callback funtion */
   int         cmdla_type;       /* requires parameter? e.g. CMDLA_REQ_ARG */
   void        *var;             /* this will be given to the callback */
   const char  *info;            /* a short description for the help */
};

/* 
 * CoMmanDLine Argument Switch CallBack Type.
 * This holds the definition of an argument that is a switch.
 */
struct cmdlas_cbt {
   char        carg;             /* The single letter argument e.g. -a */
   char        *sarg;            /* The string argument e.g. --arg */
   cmdlas_cb   cb;               /* pointer to the callback funtion */
   void        *var;             /* this will be given to the callback */
   const char  *info;            /* a short description for the help */
};


/*
 * The defaul usage method....this was previously static within
 * cmdla but i realised that it is useful to make it callable
 * within the calling code to do special usage in special cases
 * and call this if no special case occured.
 */
int   default_usage_cb  (void *);

/* 
 * a default callback for struct cmdlap_cbt. It simply parses its first
 * argument into its last according to the type specified by its sec. arg.
 */
int   get_cmdlap_cb     (const char *, const int, void *);

/*
 * this does all the work. 
 * Arguments are: (argc, argv, a program description, a copyright string, 
 * additional text for the usage information, 
 * a list of arguments with parameter, a list of switches).
 */
int   process_cmd_line  (
      int, char *[], const char*, const char*, const char*,
      const struct cmdlap_cbt *, const struct cmdlas_cbt *);

int   switch_cb         (void *);
int   inc_cb            (void *);

#endif /* CMDLA_H */