You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
7.3 KiB

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