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