Documentation fixes
[fuse.git] / include / fuse_opt.h
blob20653b189eaba565a327083c56d2cb2fec7eb0c2
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB.
7 */
9 #ifndef _FUSE_OPT_H_
10 #define _FUSE_OPT_H_
12 /** @file
14 * This file defines the option parsing interface of FUSE
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
21 /**
22 * Option description
24 * This structure describes a single option, and action associated
25 * with it, in case it matches.
27 * More than one such match may occur, in which case the action for
28 * each match is executed.
30 * There are three possible actions in case of a match:
32 * i) An integer (int or unsigned) variable determined by 'offset' is
33 * set to 'value'
35 * ii) The processing function is called, with 'value' as the key
37 * iii) An integer (any) or string (char *) variable determined by
38 * 'offset' is set to the value of an option parameter
40 * 'offset' should normally be either set to
42 * - 'offsetof(struct foo, member)' actions i) and iii)
44 * - -1 action ii)
46 * The 'offsetof()' macro is defined in the <stddef.h> header.
48 * The template determines which options match, and also have an
49 * effect on the action. Normally the action is either i) or ii), but
50 * if a format is present in the template, then action iii) is
51 * performed.
53 * The types of templates are:
55 * 1) "-x", "-foo", "--foo", "--foo-bar", etc. These match only
56 * themselves. Invalid values are "--" and anything beginning
57 * with "-o"
59 * 2) "foo", "foo-bar", etc. These match "-ofoo", "-ofoo-bar" or
60 * the relevant option in a comma separated option list
62 * 3) "bar=", "--foo=", etc. These are variations of 1) and 2)
63 * which have a parameter
65 * 4) "bar=%s", "--foo=%lu", etc. Same matching as above but perform
66 * action iii).
68 * 5) "-x ", etc. Matches either "-xparam" or "-x param" as
69 * two separate arguments
71 * 6) "-x %s", etc. Combination of 4) and 5)
73 * If the format is "%s", memory is allocated for the string unlike with
74 * scanf(). The previous value (if non-NULL) stored at the this location is
75 * freed.
77 struct fuse_opt {
78 /** Matching template and optional parameter formatting */
79 const char *templ;
81 /**
82 * Offset of variable within 'data' parameter of fuse_opt_parse()
83 * or -1
85 unsigned long offset;
87 /**
88 * Value to set the variable to, or to be passed as 'key' to the
89 * processing function. Ignored if template has a format
91 int value;
94 /**
95 * Key option. In case of a match, the processing function will be
96 * called with the specified key.
98 #define FUSE_OPT_KEY(templ, key) { templ, -1U, key }
101 * Last option. An array of 'struct fuse_opt' must end with a NULL
102 * template value
104 #define FUSE_OPT_END { NULL, 0, 0 }
107 * Argument list
109 struct fuse_args {
110 /** Argument count */
111 int argc;
113 /** Argument vector. NULL terminated */
114 char **argv;
116 /** Is 'argv' allocated? */
117 int allocated;
121 * Initializer for 'struct fuse_args'
123 #define FUSE_ARGS_INIT(argc, argv) { argc, argv, 0 }
126 * Key value passed to the processing function if an option did not
127 * match any template
129 #define FUSE_OPT_KEY_OPT -1
132 * Key value passed to the processing function for all non-options
134 * Non-options are the arguments beginning with a character other than
135 * '-' or all arguments after the special '--' option
137 #define FUSE_OPT_KEY_NONOPT -2
140 * Special key value for options to keep
142 * Argument is not passed to processing function, but behave as if the
143 * processing function returned 1
145 #define FUSE_OPT_KEY_KEEP -3
148 * Special key value for options to discard
150 * Argument is not passed to processing function, but behave as if the
151 * processing function returned zero
153 #define FUSE_OPT_KEY_DISCARD -4
156 * Processing function
158 * This function is called if
159 * - option did not match any 'struct fuse_opt'
160 * - argument is a non-option
161 * - option did match and offset was set to -1
163 * The 'arg' parameter will always contain the whole argument or
164 * option including the parameter if exists. A two-argument option
165 * ("-x foo") is always converted to single argument option of the
166 * form "-xfoo" before this function is called.
168 * Options of the form '-ofoo' are passed to this function without the
169 * '-o' prefix.
171 * The return value of this function determines whether this argument
172 * is to be inserted into the output argument vector, or discarded.
174 * @param data is the user data passed to the fuse_opt_parse() function
175 * @param arg is the whole argument or option
176 * @param key determines why the processing function was called
177 * @param outargs the current output argument list
178 * @return -1 on error, 0 if arg is to be discarded, 1 if arg should be kept
180 typedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key,
181 struct fuse_args *outargs);
184 * Option parsing function
186 * If 'args' was returned from a previous call to fuse_opt_parse() or
187 * it was constructed from
189 * A NULL 'args' is equivalent to an empty argument vector
191 * A NULL 'opts' is equivalent to an 'opts' array containing a single
192 * end marker
194 * A NULL 'proc' is equivalent to a processing function always
195 * returning '1'
197 * @param args is the input and output argument list
198 * @param data is the user data
199 * @param opts is the option description array
200 * @param proc is the processing function
201 * @return -1 on error, 0 on success
203 int fuse_opt_parse(struct fuse_args *args, void *data,
204 const struct fuse_opt opts[], fuse_opt_proc_t proc);
207 * Add an option to a comma separated option list
209 * @param opts is a pointer to an option list, may point to a NULL value
210 * @param opt is the option to add
211 * @return -1 on allocation error, 0 on success
213 int fuse_opt_add_opt(char **opts, const char *opt);
216 * Add an option, escaping commas, to a comma separated option list
218 * @param opts is a pointer to an option list, may point to a NULL value
219 * @param opt is the option to add
220 * @return -1 on allocation error, 0 on success
222 int fuse_opt_add_opt_escaped(char **opts, const char *opt);
225 * Add an argument to a NULL terminated argument vector
227 * @param args is the structure containing the current argument list
228 * @param arg is the new argument to add
229 * @return -1 on allocation error, 0 on success
231 int fuse_opt_add_arg(struct fuse_args *args, const char *arg);
234 * Add an argument at the specified position in a NULL terminated
235 * argument vector
237 * Adds the argument to the N-th position. This is useful for adding
238 * options at the beginning of the array which must not come after the
239 * special '--' option.
241 * @param args is the structure containing the current argument list
242 * @param pos is the position at which to add the argument
243 * @param arg is the new argument to add
244 * @return -1 on allocation error, 0 on success
246 int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg);
249 * Free the contents of argument list
251 * The structure itself is not freed
253 * @param args is the structure containing the argument list
255 void fuse_opt_free_args(struct fuse_args *args);
259 * Check if an option matches
261 * @param opts is the option description array
262 * @param opt is the option to match
263 * @return 1 if a match is found, 0 if not
265 int fuse_opt_match(const struct fuse_opt opts[], const char *opt);
267 #ifdef __cplusplus
269 #endif
271 #endif /* _FUSE_OPT_H_ */