fuse_opt.h 6.5 KB

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