1 /*
2  * scamper_options.h: code to handle parsing of options
3  *
4  * $Id: scamper_options.h,v 1.8 2019/07/12 23:37:57 mjl Exp $
5  *
6  * Copyright (C) 2006-2008 The University of Waikato
7  * Author: Matthew Luckie
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 
24 #ifndef __SCAMPER_OPTIONS_H
25 #define __SCAMPER_OPTIONS_H
26 
27 #define SCAMPER_OPTION_TYPE_NULL 0x00 /* no parameter to option */
28 #define SCAMPER_OPTION_TYPE_STR  0x01 /* string parameter */
29 #define SCAMPER_OPTION_TYPE_NUM  0x02 /* integer (number) parameter */
30 
31 /*
32  * scamper_option_in
33  *
34  * define the format of an option.  this structure defines the short and long
35  * strings used for the option, the parameter type, and an associated integer
36  * id for the type, so that the caller does not have to examine the option
37  * string after it has been parsed.
38  */
39 typedef struct scamper_option_in
40 {
41   /*
42    * the character (for one-letter options) or the string (for long options)
43    * defining the option.  if c is '\0', then the option does not have a short
44    * form.  if str is NULL, then the option is
45    */
46   char c;
47   char *str;
48 
49   /*
50    * an integer mapping for the option.  this integer mapping is used when
51    * returning the parsed options to the caller.
52    */
53   int id;
54 
55   /*
56    * the type of the paramater for the option, if there is one.  type codes
57    * are defined above.
58    */
59   int type;
60 
61 } scamper_option_in_t;
62 
63 #define SCAMPER_OPTION_COUNT(opts) (sizeof(opts)/sizeof(scamper_option_in_t))
64 
65 /*
66  * scamper_option_out
67  *
68  * a simple struct to associate an option with its supplied value.  the
69  * id comes from the scamper_option_in structure.
70  *
71  * the next parameter is used to assemble a linked list of option structures.
72  */
73 typedef struct scamper_option_out
74 {
75   int                        id;
76   int                        type;
77   char                      *str;
78   struct scamper_option_out *next;
79 } scamper_option_out_t;
80 
81 /*
82  * scamper_options_parse
83  *
84  * given an input string, parse the string for options based on the options
85  * supplied in the opts_in parameter.
86  *
87  * the parsed options are put into opts_out.  the caller must use
88  * scamper_options_free() on the opts_out parameter when the structure is no
89  * longer required.
90  *
91  * this function will modify the opt_str parameter passed in rather than
92  * duplicate portions of the input string.
93  */
94 int scamper_options_parse(char *opt_str,
95 			  const scamper_option_in_t *opts_in, const int cnt_in,
96 			  scamper_option_out_t **opts_out, char **stop);
97 
98 int scamper_options_validate(const scamper_option_in_t *opts, const int cnt,
99 			     int argc, char *argv[], int *stop,
100 			     int validate(int optid, char *param,
101 					  long long *out));
102 
103 /*
104  * scamper_options_count
105  *
106  * return a count of the number of opt_out structures in the list
107  */
108 int scamper_options_count(scamper_option_out_t *opts);
109 
110 int scamper_options_c2id(const scamper_option_in_t *opts,const int cnt,char c);
111 
112 /*
113  * scamper_options_free
114  *
115  * free the list of scamper_option_out structures passed as the only parameter.
116  */
117 void scamper_options_free(scamper_option_out_t *opts);
118 
119 #endif /* __SCAMPER_OPTIONS_H */
120