1 /*
2     FUSE: Filesystem in Userspace
3     Copyright (C) 2001-2006  Miklos Szeredi <miklos@szeredi.hu>
4 
5     This program can be distributed under the terms of the GNU GPL.
6     See the file COPYING.
7 */
8 
9 #ifndef _FUSE_OPT_H_
10 #define _FUSE_OPT_H_
11 
12 /* This file defines the option parsing interface of FUSE */
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * Option description
20  *
21  * This structure describes a single option, and and action associated
22  * with it, in case it matches.
23  *
24  * More than one such match may occur, in which case the action for
25  * each match is executed.
26  *
27  * There are three possible actions in case of a match:
28  *
29  * i) An integer (int or unsigned) variable determined by 'offset' is
30  *    set to 'value'
31  *
32  * ii) The processing function is called, with 'value' as the key
33  *
34  * iii) An integer (any) or string (char *) variable determined by
35  *    'offset' is set to the value of an option parameter
36  *
37  * 'offset' should normally be either set to
38  *
39  *  - 'offsetof(struct foo, member)'  actions i) and iii)
40  *
41  *  - -1                              action ii)
42  *
43  * The 'offsetof()' macro is defined in the <stddef.h> header.
44  *
45  * The template determines which options match, and also have an
46  * effect on the action.  Normally the action is either i) or ii), but
47  * if a format is present in the template, then action iii) is
48  * performed.
49  *
50  * The types of templates are:
51  *
52  * 1) "-x", "-foo", "--foo", "--foo-bar", etc.  These match only
53  *   themselves.  Invalid values are "--" and anything beginning
54  *   with "-o"
55  *
56  * 2) "foo", "foo-bar", etc.  These match "-ofoo", "-ofoo-bar" or
57  *    the relevant option in a comma separated option list
58  *
59  * 3) "bar=", "--foo=", etc.  These are variations of 1) and 2)
60  *    which have a parameter
61  *
62  * 4) "bar=%s", "--foo=%lu", etc.  Same matching as above but perform
63  *    action iii).
64  *
65  * 5) "-x ", etc.  Matches either "-xparam" or "-x param" as
66  *    two separate arguments
67  *
68  * 6) "-x %s", etc.  Combination of 4) and 5)
69  *
70  * If the format is "%s", memory is allocated for the string unlike
71  * with scanf().
72  */
73 struct fuse_opt {
74     /** Matching template and optional parameter formatting */
75     const char *templ;
76 
77     /**
78      * Offset of variable within 'data' parameter of fuse_opt_parse()
79      * or -1
80      */
81     unsigned long offset;
82 
83     /**
84      * Value to set the variable to, or to be passed as 'key' to the
85      * processing function.  Ignored if template has a format
86      */
87     int value;
88 };
89 
90 /**
91  * Key option.  In case of a match, the processing function will be
92  * called with the specified key.
93  */
94 #define FUSE_OPT_KEY(templ, key) { templ, -1U, key }
95 
96 /**
97  * Last option.  An array of 'struct fuse_opt' must end with a NULL
98  * template value
99  */
100 #define FUSE_OPT_END { .templ = NULL }
101 
102 /**
103  * Argument list
104  */
105 struct fuse_args {
106     /** Argument count */
107     int argc;
108 
109     /** Argument vector.  NULL terminated */
110     char **argv;
111 
112     /** Is 'argv' allocated? */
113     int allocated;
114 };
115 
116 /**
117  * Initializer for 'struct fuse_args'
118  */
119 #define FUSE_ARGS_INIT(argc, argv) { argc, argv, 0 }
120 
121 /**
122  * Key value passed to the processing function if an option did not
123  * match any template
124  */
125 #define FUSE_OPT_KEY_OPT     -1
126 
127 /**
128  * Key value passed to the processing function for all non-options
129  *
130  * Non-options are the arguments beginning with a charater other than
131  * '-' or all arguments after the special '--' option
132  */
133 #define FUSE_OPT_KEY_NONOPT  -2
134 
135 /**
136  * Special key value for options to keep
137  *
138  * Argument is not passed to processing function, but behave as if the
139  * processing function returned 1
140  */
141 #define FUSE_OPT_KEY_KEEP -3
142 
143 /**
144  * Special key value for options to discard
145  *
146  * Argument is not passed to processing function, but behave as if the
147  * processing function returned zero
148  */
149 #define FUSE_OPT_KEY_DISCARD -4
150 
151 /**
152  * Processing function
153  *
154  * This function is called if
155  *    - option did not match any 'struct fuse_opt'
156  *    - argument is a non-option
157  *    - option did match and offset was set to -1
158  *
159  * The 'arg' parameter will always contain the whole argument or
160  * option including the parameter if exists.  A two-argument option
161  * ("-x foo") is always converted to single arguemnt option of the
162  * form "-xfoo" before this function is called.
163  *
164  * Options of the form '-ofoo' are passed to this function without the
165  * '-o' prefix.
166  *
167  * The return value of this function determines whether this argument
168  * is to be inserted into the output argument vector, or discarded.
169  *
170  * @param data is the user data passed to the fuse_opt_parse() function
171  * @param arg is the whole argument or option
172  * @param key determines why the processing function was called
173  * @param outargs the current output argument list
174  * @return -1 on error, 0 if arg is to be discarded, 1 if arg should be kept
175  */
176 typedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key,
177                                struct fuse_args *outargs);
178 
179 /**
180  * Option parsing function
181  *
182  * If 'args' was returned from a previous call to fuse_opt_parse() or
183  * it was constructed from
184  *
185  * A NULL 'args' is equivalent to an empty argument vector
186  *
187  * A NULL 'opts' is equivalent to an 'opts' array containing a single
188  * end marker
189  *
190  * A NULL 'proc' is equivalent to a processing function always
191  * returning '1'
192  *
193  * @param args is the input and output argument list
194  * @param data is the user data
195  * @param opts is the option description array
196  * @param proc is the processing function
197  * @return -1 on error, 0 on success
198  */
199 int fuse_opt_parse(struct fuse_args *args, void *data,
200                    const struct fuse_opt opts[], fuse_opt_proc_t proc);
201 
202 /**
203  * Add an option to a comma separated option list
204  *
205  * @param opts is a pointer to an option list, may point to a NULL value
206  * @param opt is the option to add
207  * @return -1 on allocation error, 0 on success
208  */
209 int fuse_opt_add_opt(char **opts, const char *opt);
210 
211 /**
212  * Add an argument to a NULL terminated argument vector
213  *
214  * @param args is the structure containing the current argument list
215  * @param arg is the new argument to add
216  * @return -1 on allocation error, 0 on success
217  */
218 int fuse_opt_add_arg(struct fuse_args *args, const char *arg);
219 
220 /**
221  * Add an argument at the specified position in a NULL terminated
222  * argument vector
223  *
224  * Adds the argument to the N-th position.  This is useful for adding
225  * options at the beggining of the array which must not come after the
226  * special '--' option.
227  *
228  * @param args is the structure containing the current argument list
229  * @param pos is the position at which to add the argument
230  * @param arg is the new argument to add
231  * @return -1 on allocation error, 0 on success
232  */
233 int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg);
234 
235 /**
236  * Free the contents of argument list
237  *
238  * The structure itself is not freed
239  *
240  * @param args is the structure containing the argument list
241  */
242 void fuse_opt_free_args(struct fuse_args *args);
243 
244 
245 /**
246  * Check if an option matches
247  *
248  * @param opts is the option description array
249  * @param opt is the option to match
250  * @return 1 if a match is found, 0 if not
251  */
252 int fuse_opt_match(const struct fuse_opt opts[], const char *opt);
253 
254 #ifdef __cplusplus
255 }
256 #endif
257 
258 #endif /* _FUSE_OPT_H_ */
259