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