-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts_parse.h
155 lines (121 loc) · 3.97 KB
/
opts_parse.h
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
/*****************************************************************************
* opts_parse -- command line option parsing and help output
*
* NOTE: Edit this file with tabstop=4 !
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* See file COPYING for information on distribution conditions.
*
*****************************************************************************/
#ifndef __OPTS_PARSE_H__
#define __OPTS_PARSE_H__
#include <stdarg.h>
/*
* opts_cols defines the column sizes for output
* column 1 == initial indentation (default 2)
* column 2 == option/argument area (default 28)
* column 3 == separator (default 2)
* column 4 == description (default 47)
*/
extern int opts_cols[];
/* parsed_opts flags */
#define OPT_NOT_MODAL 0x0000
#define OPT_MODAL 0x0001
#define OPT_DEFAULT_ARGS 0x0002 // Collect all unconsumed arguments
#define OPT_MOD_MASK 0x000F
/* arg_info -- max count */
#define ARGS_INFINITE 0xFFFFFFFF
/* Access the modality portion of the parse_opts flags */
#define OPT_MOD(f) ((f) & OPT_MOD_MASK)
/* Is the option null? */
#define NULL_OPT(o) (! (o).status )
/* Several checks about the number of arguments for a given option */
#define HAS_ARGS(p) ((p)->args.maxcnt != 0)
#define HAS_MULTIPLE_ARGS(p) ((p)->args.maxcnt > 1)
#define HAS_OPTIONAL_ARGS(p) (((p)->args.mincnt == 0) && \
((p)->args.maxcnt > 1))
#define HAS_MANDATORY_ARGS(p) ((p)->args.mincnt > 0)
/*
* Structure initialization macros
*/
#define NULL_ARG_INFO { NULL, 0, 0, 0 }
#define NULL_OPTS { 0, NULL, NULL, NULL, NULL_ARG_INFO, NULL,NULL}
#define EMPTY_OPT_INFO { 0, NULL, NULL, NULL, 0 }
#define DEFAULT_ARGS OPT_DEFAULT_ARGS, \
NULL, NULL, NULL, NULL_ARG_INFO, NULL
typedef struct arg_info arg_info;
typedef struct opt_info opt_info;
typedef struct parsed_opts parsed_opts;
/*
* Argument information
*/
struct arg_info
{
// Output information
char* argname; // Name of argument (for output)
unsigned long argblk; // # arguments per "block"
unsigned long mincnt; // Minimum # argument blocks
unsigned long maxcnt; // Maximum # argument blocks (-1 = no limit)
};
/*
* Option parse information
*/
struct opt_info
{
int optidex; // Index of 'opt' in argv[]
char** opt; // pointer to this option in argv[]
char* optmod; // option modified (everything after ':')
char** optarg; // pointer to first argv[] option argument
unsigned long optarg_cnt; // count of arguments for this option
};
/*
* Description of options to parse
*/
struct parsed_opts
{
// Parse information
unsigned short flags; // flags - modality (is modal or not),
// - default argument catcher
char* shortopts; // Short options: e.g. "ai"
char* longopts; // Long options : e.g. "add:insert"
char* description; // Option description (for output)
arg_info args; // Argument parsing information
// Sub options
parsed_opts* subopts; // sub-options
// Parse status
opt_info* status;
};
/******************************************************************************
* Prototypes
*/
extern
char* opt_str (parsed_opts* pOpt);
extern
void opts_vprintf (int hFile,
parsed_opts opts[],
char* fmt,
va_list args);
extern
char* opts_parse (parsed_opts opts[],
int argc,
char** argv);
extern
void opts_printf (int hFile,
parsed_opts opts[],
char* fmt,
...);
extern
void opts_fprintf (FILE* pFile,
parsed_opts opts[],
char* fmt,
...);
#endif // __OPTS_PARSE_H__