File: opt.h

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (300 lines) | stat: -rw-r--r-- 12,478 bytes parent folder | download | duplicates (6)
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
/* LIBDGL -- a Directed Graph Library implementation
 * Copyright (C) 2002 Roberto Micarelli
 *
 * 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 of the License, 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* best view tabstop=4
 */

/*@*********************************************************************
 * @pack:       GNO_PACK
 * @descr:      Command line options utility.
 *
 * @notes:      Support to easily parse command line options. Some concepts
 *                      were taken from the posix getopt() family functions, while
 *                      our specific experience in C programming suggested us a
 *                      proprietary implementation to make source code more readable
 *                      at life easier (I hope).
 *                      Option format:
 *
 *                      syntax                          name
 *                      ----------------------------------------------
 *                      --option=value          long-parametric
 *                      --option                        long-boolean
 *                      -option value           short-parametric
 *                      -option                         short-boolean
 *
 *              C sample:
 *              ----------------------------------------------
 *              #include <stdio.h>
 *              #include <opt.h>
 *
 *              int main( int argc , char ** argv )
 *              {
 *                      Boolean         fHelp;
 *                      Boolean         fTcp;
 *                      Boolean         fUdp;
 *                      char    *       pszProtocol;
 *                      char    *       pszInterface;
 *                      int                     i;
 *
 *                      GNO_BEGIN
 *                      GNO_SWITCH( "h", "help", False , & fHelp , "Print this help." )
 *                      GNO_SWITCH( "t", "tcp", False , & fTcp , NULL )
 *                      GNO_SWITCH( "u", "udp", False , & fUdp , NULL )
 *                      GNO_OPTION( "p", "protocol", "tcp" , & pszProtocol , NULL )
 *                      GNO_OPTION( "i", "interface", "eth0" , & pszInterface , NULL )
 *                      GNO_END
 *
 *
 *                      if ( GNO_PARSE( argc , argv ) < 0 )
 *                      {
 *                              return 1;
 *                      }
 *
 *                      if ( fHelp == True )
 *                      {
 *                              GNO_HELP( "t_opt usage" );
 *                              return 0;
 *                      }
 *
 *                      printf ( "t/tcp  = %s\n", (fTcp  == True) ? "True" : "False" );
 *                      printf ( "u/udp  = %s\n", (fUdp  == True) ? "True" : "False" );
 *                      printf ( "p/protocol  = <%s>\n", pszProtocol );
 *                      printf ( "i/interface = <%s>\n", pszInterface );
 *
 *                      GNO_FREE();
 *
 *                      printf( "orphan options:\n" );
 *
 *                      for ( i = 0 ; i < argc ; i ++ )
 *                      {
 *                              if ( argv[ i ] )
 *                              {
 *                                      printf( "arg %d: %s\n", i, argv[i ] );
 *                              }
 *                              else
 *                              {
 *                                      printf( "arg %d: --\n", i );
 *                              }
 *                      }
 *                      return 0;
 *              }
 *                                              
 **********************************************************************/

#ifndef _GNOPT_H_
#define _GNOPT_H_

#ifdef __cplusplus
extern "C"
{
#endif

/***********************************************************************
 *				DEFINES
 **********************************************************************/
    /*@*--------------------------------------------------------------------
     * @defs:       GNO_FLG
     * @descr:      flags used to set the nFlg field in the GnoOption_s
     *                      SWITCH    = boolean option required
     *
     * @see:        GnoOption_s
     *
     *--------------------------------------------------------------------*/

#define GNO_FLG_SWITCH		0x01

    /*@*--------------------------------------------------------------------
     * @defs:       True/False
     * @descr:      one more useful (?) true/false definition
     *
     *--------------------------------------------------------------------*/
#define True  1
#define False 0

/***********************************************************************
 *				STRUCTS/UNIONS/TYPES/FUNCDEFS
 **********************************************************************/
    /*@*--------------------------------------------------------------------
     * @type:       Boolean
     * @descr:      wasn't it better to use 'int'?
     *--------------------------------------------------------------------*/
    typedef int Boolean;

    /*@*--------------------------------------------------------------------
     * @type:       GnoOption_s
     * @descr:      This structure describes an option. We intend to use an array
     *          of GnoOption_s, terminated by a 'NULL' entry (one containing
     *                      all binary-zero fields), and to pass it to GnoParse() together
     *                      with the typical argc,argv couple of main() args. The array's
     *                      entries are looked-up and filled with coherent argc,argv
     *                      values. Some fields are statically filled by user while other
     *                      are returned by the GnoParse() function.
     *                      Fields set by the user:
     *
     *                      nFlg      = So far the only supported flag is GNO_FLG_SWITCH
     *                                  to address a boolean option type.
     *                      fDef      = The default value for a boolean option.
     *                      pszDef    = The default value for a parametric option.
     *                      pszShort  = The short name of the option.
     *                      pszLong   = The long name of the option.
     *                      pfValue   = Pointer to a boolean option return value.
     *                      ppszValue = Pointer to a parametric option return value.
     *                      pszDescr  = A brief option description
     *
     *                      Fields set by GnoParse():
     *
     *                      iArg       = argv option index
     *                      *ppszValue = pointer to parametric option value 
     *                      *pfValue   = True/False
     *
     *                      User supplied fields are mandatory only within specific
     *                      conditions:
     *
     *                      - at least one of pszShort/pszLong must be specified
     *                      - fDef and pfValue apply to a boolean option only
     *                      - pszDef and ppszValue apply to a parametric option only
     *                      - pszDescr is optional
     *
     * @see:        GnoParse(), GNO_FLG
     *
     *--------------------------------------------------------------------*/

    typedef struct GnoOption
    {
	int iArg;		/* returned argv option index */
	int nFlg;		/* flags describing the option */
	Boolean fDef;		/* default returned value for a boolean option */
	char *pszDef;		/* default returned value for a parametric option */
	char *pszShort;		/* short-option recogniser */
	char *pszLong;		/* long-option recogniser */
	Boolean *pfValue;	/* address to return a boolean option */
	char **ppszValue;	/* address to return a parametric option */
	char *pszDescr;		/* a brief option description */

    } GnoOption_s;


/***********************************************************************
 *				MACROS
 **********************************************************************/
    /*@*--------------------------------------------------------------------
     *
     * @macro:      GNO_BEGIN
     * @descr:      Begin an option array declaration
     * 
     * @notes:      The best use is to start option declaration immediately after
     *                      the automatic-variable declaration in the main() function.
     *
     *--------------------------------------------------------------------*/
#define GNO_BEGIN GnoOption_s _aopt[] = {

    /*@*--------------------------------------------------------------------
     *
     * @macro:      GNO_OPTION
     * @descr:      Declare a parametric option
     *
     * 
     * @args:       I:      chsopt  =       short option character
     *                      I:      pszlopt ->      long option psz
     *                      I:      pszdef  ->      default-returned psz
     *                      I:      ppszv   ->      user-addressed return variable pointer
     *
     *--------------------------------------------------------------------*/
#define GNO_OPTION(pszsopt,pszlopt,pszdef,ppszv,pszdescr) \
			{ 0, 0, 0, pszdef, pszsopt, pszlopt, NULL, ppszv, pszdescr },

    /*@*--------------------------------------------------------------------
     *
     * @macro:      GNO_SWITCH
     * @descr:      Declare a boolean option
     * 
     * @args:       I:      chsopt  =       short option character
     *                      I:      pszlopt ->      long option psz
     *                      I:      fdef    =       default-returned Boolean
     *                      I:      pfv             ->      user-addressed return variable pointer
     *
     *--------------------------------------------------------------------*/
#define GNO_SWITCH(pszsopt,pszlopt,fdef,pfv,pszdescr) \
			{	\
				0, \
				GNO_FLG_SWITCH, \
				fdef, NULL, \
				pszsopt, pszlopt, \
				pfv, NULL, \
				pszdescr \
			},

    /*@*--------------------------------------------------------------------
     *
     * @macro:      GNO_PARSE
     * @descr:      Parse a GnoOption_s array declaration
     * 
     * @args:       I:      argc    =       count of argv entries
     *                      I:      argv    ->      array of sz pointers
     *
     *--------------------------------------------------------------------*/
#define GNO_PARSE(argc,argv) GnoParse( (argc), (argv), _aopt )

    /*@*--------------------------------------------------------------------
     *
     * @macro:      GNO_END
     * @descr:      Terminate an option array declaration
     * 
     *--------------------------------------------------------------------*/
#define GNO_END	{ 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL } };

    /*@*--------------------------------------------------------------------
     *
     * @macro:      GNO_HELP
     *
     * @descr:      Print a brief option's help on the standard error
     *
     * @args:       I:      pszhead ->      help header string
     * 
     *--------------------------------------------------------------------*/
#define GNO_HELP(pszhead) GnoHelp( pszhead , _aopt )

    /*@*--------------------------------------------------------------------
     *
     * @macro:      GNO_FREE
     *
     * @descr:      Free resources created by a previously parsed array
     *
     * 
     *--------------------------------------------------------------------*/
#define GNO_FREE() GnoFree( _aopt )


/***********************************************************************
 *				FUNCTION PROTOTYPES
 **********************************************************************/

    extern int GnoParse(int argc, char **argv, GnoOption_s * pOpt);

    extern void GnoFree(GnoOption_s * pOpt);

    extern void GnoHelp(char *pszHead, GnoOption_s * pOpt);


#ifdef __cplusplus
}
#endif

#endif				/* top of file */

/***************************** END OF FILE ****************************/