16881f68fSopenharmony_ci/* 26881f68fSopenharmony_ci FUSE: Filesystem in Userspace 36881f68fSopenharmony_ci Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu> 46881f68fSopenharmony_ci 56881f68fSopenharmony_ci This program can be distributed under the terms of the GNU LGPLv2. 66881f68fSopenharmony_ci See the file COPYING.LIB. 76881f68fSopenharmony_ci*/ 86881f68fSopenharmony_ci 96881f68fSopenharmony_ci#ifndef FUSE_OPT_H_ 106881f68fSopenharmony_ci#define FUSE_OPT_H_ 116881f68fSopenharmony_ci 126881f68fSopenharmony_ci/** @file 136881f68fSopenharmony_ci * 146881f68fSopenharmony_ci * This file defines the option parsing interface of FUSE 156881f68fSopenharmony_ci */ 166881f68fSopenharmony_ci 176881f68fSopenharmony_ci#ifdef __cplusplus 186881f68fSopenharmony_ciextern "C" { 196881f68fSopenharmony_ci#endif 206881f68fSopenharmony_ci 216881f68fSopenharmony_ci/** 226881f68fSopenharmony_ci * Option description 236881f68fSopenharmony_ci * 246881f68fSopenharmony_ci * This structure describes a single option, and action associated 256881f68fSopenharmony_ci * with it, in case it matches. 266881f68fSopenharmony_ci * 276881f68fSopenharmony_ci * More than one such match may occur, in which case the action for 286881f68fSopenharmony_ci * each match is executed. 296881f68fSopenharmony_ci * 306881f68fSopenharmony_ci * There are three possible actions in case of a match: 316881f68fSopenharmony_ci * 326881f68fSopenharmony_ci * i) An integer (int or unsigned) variable determined by 'offset' is 336881f68fSopenharmony_ci * set to 'value' 346881f68fSopenharmony_ci * 356881f68fSopenharmony_ci * ii) The processing function is called, with 'value' as the key 366881f68fSopenharmony_ci * 376881f68fSopenharmony_ci * iii) An integer (any) or string (char *) variable determined by 386881f68fSopenharmony_ci * 'offset' is set to the value of an option parameter 396881f68fSopenharmony_ci * 406881f68fSopenharmony_ci * 'offset' should normally be either set to 416881f68fSopenharmony_ci * 426881f68fSopenharmony_ci * - 'offsetof(struct foo, member)' actions i) and iii) 436881f68fSopenharmony_ci * 446881f68fSopenharmony_ci * - -1 action ii) 456881f68fSopenharmony_ci * 466881f68fSopenharmony_ci * The 'offsetof()' macro is defined in the <stddef.h> header. 476881f68fSopenharmony_ci * 486881f68fSopenharmony_ci * The template determines which options match, and also have an 496881f68fSopenharmony_ci * effect on the action. Normally the action is either i) or ii), but 506881f68fSopenharmony_ci * if a format is present in the template, then action iii) is 516881f68fSopenharmony_ci * performed. 526881f68fSopenharmony_ci * 536881f68fSopenharmony_ci * The types of templates are: 546881f68fSopenharmony_ci * 556881f68fSopenharmony_ci * 1) "-x", "-foo", "--foo", "--foo-bar", etc. These match only 566881f68fSopenharmony_ci * themselves. Invalid values are "--" and anything beginning 576881f68fSopenharmony_ci * with "-o" 586881f68fSopenharmony_ci * 596881f68fSopenharmony_ci * 2) "foo", "foo-bar", etc. These match "-ofoo", "-ofoo-bar" or 606881f68fSopenharmony_ci * the relevant option in a comma separated option list 616881f68fSopenharmony_ci * 626881f68fSopenharmony_ci * 3) "bar=", "--foo=", etc. These are variations of 1) and 2) 636881f68fSopenharmony_ci * which have a parameter 646881f68fSopenharmony_ci * 656881f68fSopenharmony_ci * 4) "bar=%s", "--foo=%lu", etc. Same matching as above but perform 666881f68fSopenharmony_ci * action iii). 676881f68fSopenharmony_ci * 686881f68fSopenharmony_ci * 5) "-x ", etc. Matches either "-xparam" or "-x param" as 696881f68fSopenharmony_ci * two separate arguments 706881f68fSopenharmony_ci * 716881f68fSopenharmony_ci * 6) "-x %s", etc. Combination of 4) and 5) 726881f68fSopenharmony_ci * 736881f68fSopenharmony_ci * If the format is "%s", memory is allocated for the string unlike with 746881f68fSopenharmony_ci * scanf(). The previous value (if non-NULL) stored at the this location is 756881f68fSopenharmony_ci * freed. 766881f68fSopenharmony_ci */ 776881f68fSopenharmony_cistruct fuse_opt { 786881f68fSopenharmony_ci /** Matching template and optional parameter formatting */ 796881f68fSopenharmony_ci const char *templ; 806881f68fSopenharmony_ci 816881f68fSopenharmony_ci /** 826881f68fSopenharmony_ci * Offset of variable within 'data' parameter of fuse_opt_parse() 836881f68fSopenharmony_ci * or -1 846881f68fSopenharmony_ci */ 856881f68fSopenharmony_ci unsigned long offset; 866881f68fSopenharmony_ci 876881f68fSopenharmony_ci /** 886881f68fSopenharmony_ci * Value to set the variable to, or to be passed as 'key' to the 896881f68fSopenharmony_ci * processing function. Ignored if template has a format 906881f68fSopenharmony_ci */ 916881f68fSopenharmony_ci int value; 926881f68fSopenharmony_ci}; 936881f68fSopenharmony_ci 946881f68fSopenharmony_ci/** 956881f68fSopenharmony_ci * Key option. In case of a match, the processing function will be 966881f68fSopenharmony_ci * called with the specified key. 976881f68fSopenharmony_ci */ 986881f68fSopenharmony_ci#define FUSE_OPT_KEY(templ, key) { templ, -1U, key } 996881f68fSopenharmony_ci 1006881f68fSopenharmony_ci/** 1016881f68fSopenharmony_ci * Last option. An array of 'struct fuse_opt' must end with a NULL 1026881f68fSopenharmony_ci * template value 1036881f68fSopenharmony_ci */ 1046881f68fSopenharmony_ci#define FUSE_OPT_END { NULL, 0, 0 } 1056881f68fSopenharmony_ci 1066881f68fSopenharmony_ci/** 1076881f68fSopenharmony_ci * Argument list 1086881f68fSopenharmony_ci */ 1096881f68fSopenharmony_cistruct fuse_args { 1106881f68fSopenharmony_ci /** Argument count */ 1116881f68fSopenharmony_ci int argc; 1126881f68fSopenharmony_ci 1136881f68fSopenharmony_ci /** Argument vector. NULL terminated */ 1146881f68fSopenharmony_ci char **argv; 1156881f68fSopenharmony_ci 1166881f68fSopenharmony_ci /** Is 'argv' allocated? */ 1176881f68fSopenharmony_ci int allocated; 1186881f68fSopenharmony_ci}; 1196881f68fSopenharmony_ci 1206881f68fSopenharmony_ci/** 1216881f68fSopenharmony_ci * Initializer for 'struct fuse_args' 1226881f68fSopenharmony_ci */ 1236881f68fSopenharmony_ci#define FUSE_ARGS_INIT(argc, argv) { argc, argv, 0 } 1246881f68fSopenharmony_ci 1256881f68fSopenharmony_ci/** 1266881f68fSopenharmony_ci * Key value passed to the processing function if an option did not 1276881f68fSopenharmony_ci * match any template 1286881f68fSopenharmony_ci */ 1296881f68fSopenharmony_ci#define FUSE_OPT_KEY_OPT -1 1306881f68fSopenharmony_ci 1316881f68fSopenharmony_ci/** 1326881f68fSopenharmony_ci * Key value passed to the processing function for all non-options 1336881f68fSopenharmony_ci * 1346881f68fSopenharmony_ci * Non-options are the arguments beginning with a character other than 1356881f68fSopenharmony_ci * '-' or all arguments after the special '--' option 1366881f68fSopenharmony_ci */ 1376881f68fSopenharmony_ci#define FUSE_OPT_KEY_NONOPT -2 1386881f68fSopenharmony_ci 1396881f68fSopenharmony_ci/** 1406881f68fSopenharmony_ci * Special key value for options to keep 1416881f68fSopenharmony_ci * 1426881f68fSopenharmony_ci * Argument is not passed to processing function, but behave as if the 1436881f68fSopenharmony_ci * processing function returned 1 1446881f68fSopenharmony_ci */ 1456881f68fSopenharmony_ci#define FUSE_OPT_KEY_KEEP -3 1466881f68fSopenharmony_ci 1476881f68fSopenharmony_ci/** 1486881f68fSopenharmony_ci * Special key value for options to discard 1496881f68fSopenharmony_ci * 1506881f68fSopenharmony_ci * Argument is not passed to processing function, but behave as if the 1516881f68fSopenharmony_ci * processing function returned zero 1526881f68fSopenharmony_ci */ 1536881f68fSopenharmony_ci#define FUSE_OPT_KEY_DISCARD -4 1546881f68fSopenharmony_ci 1556881f68fSopenharmony_ci/** 1566881f68fSopenharmony_ci * Processing function 1576881f68fSopenharmony_ci * 1586881f68fSopenharmony_ci * This function is called if 1596881f68fSopenharmony_ci * - option did not match any 'struct fuse_opt' 1606881f68fSopenharmony_ci * - argument is a non-option 1616881f68fSopenharmony_ci * - option did match and offset was set to -1 1626881f68fSopenharmony_ci * 1636881f68fSopenharmony_ci * The 'arg' parameter will always contain the whole argument or 1646881f68fSopenharmony_ci * option including the parameter if exists. A two-argument option 1656881f68fSopenharmony_ci * ("-x foo") is always converted to single argument option of the 1666881f68fSopenharmony_ci * form "-xfoo" before this function is called. 1676881f68fSopenharmony_ci * 1686881f68fSopenharmony_ci * Options of the form '-ofoo' are passed to this function without the 1696881f68fSopenharmony_ci * '-o' prefix. 1706881f68fSopenharmony_ci * 1716881f68fSopenharmony_ci * The return value of this function determines whether this argument 1726881f68fSopenharmony_ci * is to be inserted into the output argument vector, or discarded. 1736881f68fSopenharmony_ci * 1746881f68fSopenharmony_ci * @param data is the user data passed to the fuse_opt_parse() function 1756881f68fSopenharmony_ci * @param arg is the whole argument or option 1766881f68fSopenharmony_ci * @param key determines why the processing function was called 1776881f68fSopenharmony_ci * @param outargs the current output argument list 1786881f68fSopenharmony_ci * @return -1 on error, 0 if arg is to be discarded, 1 if arg should be kept 1796881f68fSopenharmony_ci */ 1806881f68fSopenharmony_citypedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key, 1816881f68fSopenharmony_ci struct fuse_args *outargs); 1826881f68fSopenharmony_ci 1836881f68fSopenharmony_ci/** 1846881f68fSopenharmony_ci * Option parsing function 1856881f68fSopenharmony_ci * 1866881f68fSopenharmony_ci * If 'args' was returned from a previous call to fuse_opt_parse() or 1876881f68fSopenharmony_ci * it was constructed from 1886881f68fSopenharmony_ci * 1896881f68fSopenharmony_ci * A NULL 'args' is equivalent to an empty argument vector 1906881f68fSopenharmony_ci * 1916881f68fSopenharmony_ci * A NULL 'opts' is equivalent to an 'opts' array containing a single 1926881f68fSopenharmony_ci * end marker 1936881f68fSopenharmony_ci * 1946881f68fSopenharmony_ci * A NULL 'proc' is equivalent to a processing function always 1956881f68fSopenharmony_ci * returning '1' 1966881f68fSopenharmony_ci * 1976881f68fSopenharmony_ci * @param args is the input and output argument list 1986881f68fSopenharmony_ci * @param data is the user data 1996881f68fSopenharmony_ci * @param opts is the option description array 2006881f68fSopenharmony_ci * @param proc is the processing function 2016881f68fSopenharmony_ci * @return -1 on error, 0 on success 2026881f68fSopenharmony_ci */ 2036881f68fSopenharmony_ciint fuse_opt_parse(struct fuse_args *args, void *data, 2046881f68fSopenharmony_ci const struct fuse_opt opts[], fuse_opt_proc_t proc); 2056881f68fSopenharmony_ci 2066881f68fSopenharmony_ci/** 2076881f68fSopenharmony_ci * Add an option to a comma separated option list 2086881f68fSopenharmony_ci * 2096881f68fSopenharmony_ci * @param opts is a pointer to an option list, may point to a NULL value 2106881f68fSopenharmony_ci * @param opt is the option to add 2116881f68fSopenharmony_ci * @return -1 on allocation error, 0 on success 2126881f68fSopenharmony_ci */ 2136881f68fSopenharmony_ciint fuse_opt_add_opt(char **opts, const char *opt); 2146881f68fSopenharmony_ci 2156881f68fSopenharmony_ci/** 2166881f68fSopenharmony_ci * Add an option, escaping commas, to a comma separated option list 2176881f68fSopenharmony_ci * 2186881f68fSopenharmony_ci * @param opts is a pointer to an option list, may point to a NULL value 2196881f68fSopenharmony_ci * @param opt is the option to add 2206881f68fSopenharmony_ci * @return -1 on allocation error, 0 on success 2216881f68fSopenharmony_ci */ 2226881f68fSopenharmony_ciint fuse_opt_add_opt_escaped(char **opts, const char *opt); 2236881f68fSopenharmony_ci 2246881f68fSopenharmony_ci/** 2256881f68fSopenharmony_ci * Add an argument to a NULL terminated argument vector 2266881f68fSopenharmony_ci * 2276881f68fSopenharmony_ci * @param args is the structure containing the current argument list 2286881f68fSopenharmony_ci * @param arg is the new argument to add 2296881f68fSopenharmony_ci * @return -1 on allocation error, 0 on success 2306881f68fSopenharmony_ci */ 2316881f68fSopenharmony_ciint fuse_opt_add_arg(struct fuse_args *args, const char *arg); 2326881f68fSopenharmony_ci 2336881f68fSopenharmony_ci/** 2346881f68fSopenharmony_ci * Add an argument at the specified position in a NULL terminated 2356881f68fSopenharmony_ci * argument vector 2366881f68fSopenharmony_ci * 2376881f68fSopenharmony_ci * Adds the argument to the N-th position. This is useful for adding 2386881f68fSopenharmony_ci * options at the beginning of the array which must not come after the 2396881f68fSopenharmony_ci * special '--' option. 2406881f68fSopenharmony_ci * 2416881f68fSopenharmony_ci * @param args is the structure containing the current argument list 2426881f68fSopenharmony_ci * @param pos is the position at which to add the argument 2436881f68fSopenharmony_ci * @param arg is the new argument to add 2446881f68fSopenharmony_ci * @return -1 on allocation error, 0 on success 2456881f68fSopenharmony_ci */ 2466881f68fSopenharmony_ciint fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg); 2476881f68fSopenharmony_ci 2486881f68fSopenharmony_ci/** 2496881f68fSopenharmony_ci * Free the contents of argument list 2506881f68fSopenharmony_ci * 2516881f68fSopenharmony_ci * The structure itself is not freed 2526881f68fSopenharmony_ci * 2536881f68fSopenharmony_ci * @param args is the structure containing the argument list 2546881f68fSopenharmony_ci */ 2556881f68fSopenharmony_civoid fuse_opt_free_args(struct fuse_args *args); 2566881f68fSopenharmony_ci 2576881f68fSopenharmony_ci 2586881f68fSopenharmony_ci/** 2596881f68fSopenharmony_ci * Check if an option matches 2606881f68fSopenharmony_ci * 2616881f68fSopenharmony_ci * @param opts is the option description array 2626881f68fSopenharmony_ci * @param opt is the option to match 2636881f68fSopenharmony_ci * @return 1 if a match is found, 0 if not 2646881f68fSopenharmony_ci */ 2656881f68fSopenharmony_ciint fuse_opt_match(const struct fuse_opt opts[], const char *opt); 2666881f68fSopenharmony_ci 2676881f68fSopenharmony_ci#ifdef __cplusplus 2686881f68fSopenharmony_ci} 2696881f68fSopenharmony_ci#endif 2706881f68fSopenharmony_ci 2716881f68fSopenharmony_ci#endif /* FUSE_OPT_H_ */ 272