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
|
#ifndef INCLUDED_ARGS_H_
#define INCLUDED_ARGS_H_
#include <getopt.h>
#include "../root/root.h"
/*
Provide args_construct with the optionstring for getopt() and a pointer to
long-options structs, terminating in a final element of 0's.
*/
typedef struct
{
char const *d_name;
int d_type;
unsigned char d_value;
}
LongOption;
extern int args_data;
char const *args_arg(size_t idx); /* 0: 1st arg not counting */
/* argv[0] */
void args_construct(int argc, char **argv, char *options,
LongOption const *long_options);
// P.M.: args_destruct
/* get the next occurrence */
/* of a series of optargs */
/* to get the next one, */
/* give optarg 0 */
/* return value 0: no arg */
/* PFAILED: no (more) */
char const *args_multiarg(int optchar); /* optchars specified */
char const *args_optarg(int optchar); /* 0: no such option */
/* or value of option */
int args_optint(int optchar); /* int value of option */
/* optchar or FAILED and */
/* error message */
/*
Internal Arg use only. Not used outside of this directory functions, needed here
to allow proper compilation of the static inline functions below
*/
#include "../string/string.h"
typedef struct
{
String d_option; /* all option characters encountered */
char **d_optarg; /* all option arguments, or 0-pointers */
/* the location of the option values */
/* matches the index of the corresponding */
/* character in d_option */
char **d_argv;
int d_argc;
char *d_home;
char *d_programName;
char *d_initial_dir;
bool d_ok;
int d_optind;
}
Args;
extern Args args;
/* public interface continues from here */
static inline char const *args_home()
{
return args.d_home ? args.d_home : NULL;
}
static inline char const *args_initial_dir()
{
return args.d_initial_dir;
}
static inline size_t args_nArgs()
{
return args.d_argc - args.d_optind;
}
static inline bool args_ok()
{
return args.d_ok;
}
static inline bool args_option(int optchar)
{
return strchr(string_str(&args.d_option), optchar) != NULL;
}
static inline bool args_options(char const *optchars)
{
return string_find_first_of(&args.d_option, optchars) != UFAILED;
}
static inline char const *args_programName()
{
return args.d_programName;
}
#endif
|