source: FTPfs/curlftpfs-0.9.1/compat/fuse_opt.h @ 10

Last change on this file since 10 was 10, checked in by zsjheng, 16 years ago
File size: 6.2 KB
Line 
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
15extern "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 */
73struct fuse_opt {
74    /** Matching template and optional parameter formatting */
75    const char *template_opt;
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 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(template_opt, key) { template_opt, -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 { .template_opt = NULL }
101
102/**
103 * Argument list
104 */
105struct 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 templated
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 * Processing function
137 *
138 * This function is called if
139 *    - option did not match any 'struct fuse_opt'
140 *    - argument is a non-option
141 *    - option did match and offset was set to -1
142 *
143 * The 'arg' parameter will always contain the whole argument or
144 * option including the parameter if exists.  A two-argument option
145 * ("-x foo") is always converted to single arguemnt option of the
146 * form "-xfoo" before this function is called.
147 *
148 * Options of the form '-ofoo' are passed to this function without the
149 * '-o' prefix.
150 *
151 * The return value of this function determines whether this argument
152 * is to be inserted into the output argument vector, or discarded.
153 *
154 * @param data is the user data passed to the fuse_opt_parse() function
155 * @param arg is the whole argument or option
156 * @param key determines why the processing function was called
157 * @param outargs the current output argument list
158 * @return -1 on error, 0 if arg is to be discarded, 1 if arg should be kept
159 */
160typedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key,
161                               struct fuse_args *outargs);
162
163/**
164 * Option parsing function
165 *
166 * If 'args' was returned from a previous call to fuse_opt_parse() or
167 * it was constructed from
168 *
169 * A NULL 'args' is equivalent to an empty argument vector
170 *
171 * A NULL 'opts' is equivalent to an 'opts' array containing a single
172 * end marker
173 *
174 * A NULL 'proc' is equivalent to a processing function always
175 * returning '1'
176 *
177 * @param args is the input and output argument list
178 * @param data is the user data
179 * @param opts is the option description array
180 * @param proc is the processing function
181 * @return -1 on error, 0 on success
182 */
183int fuse_opt_parse(struct fuse_args *args, void *data,
184                   const struct fuse_opt opts[], fuse_opt_proc_t proc);
185
186/**
187 * Add an option to a comma separated option list
188 *
189 * @param opts is a pointer to an option list, may point to a NULL value
190 * @param opt is the option to add
191 * @return -1 on allocation error, 0 on success
192 */
193int fuse_opt_add_opt(char **opts, const char *opt);
194
195/**
196 * Add an argument to a NULL terminated argument vector
197 *
198 * @param args is the structure containing the current argument list
199 * @param arg is the new argument to add
200 * @return -1 on allocation error, 0 on success
201 */
202int fuse_opt_add_arg(struct fuse_args *args, const char *arg);
203
204/**
205 * Free the contents of argument list
206 *
207 * The structure itself is not freed
208 *
209 * @param args is the structure containing the argument list
210 */
211void fuse_opt_free_args(struct fuse_args *args);
212
213
214/**
215 * Check if an option matches
216 *
217 * @param opts is the option description array
218 * @param opt is the option to match
219 * @return 1 if a match is found, 0 if not
220 */
221int fuse_opt_match(const struct fuse_opt opts[], const char *opt);
222
223#ifdef __cplusplus
224}
225#endif
226
227#endif /* _FUSE_OPT_H_ */
Note: See TracBrowser for help on using the repository browser.