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.

272 lines
7.8 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. {
  73. /** Matching template and optional parameter formatting */
  74. const char *templ;
  75. /**
  76. * Offset of variable within 'data' parameter of fuse_opt_parse()
  77. * or -1
  78. */
  79. unsigned long offset;
  80. /**
  81. * Value to set the variable to, or to be passed as 'key' to the
  82. * processing function. Ignored if template has a format
  83. */
  84. int value;
  85. };
  86. /**
  87. * Key option. In case of a match, the processing function will be
  88. * called with the specified key.
  89. */
  90. #define FUSE_OPT_KEY(templ, key) { templ, -1U, key }
  91. /**
  92. * Last option. An array of 'struct fuse_opt' must end with a NULL
  93. * template value
  94. */
  95. #define FUSE_OPT_END { NULL, 0, 0 }
  96. /**
  97. * Argument list
  98. */
  99. struct fuse_args
  100. {
  101. /** Argument count */
  102. int argc;
  103. /** Argument vector. NULL terminated */
  104. char **argv;
  105. /** Is 'argv' allocated? */
  106. int allocated;
  107. };
  108. /**
  109. * Initializer for 'struct fuse_args'
  110. */
  111. #define FUSE_ARGS_INIT(argc, argv) { argc, argv, 0 }
  112. /**
  113. * Key value passed to the processing function if an option did not
  114. * match any template
  115. */
  116. #define FUSE_OPT_KEY_OPT -1
  117. /**
  118. * Key value passed to the processing function for all non-options
  119. *
  120. * Non-options are the arguments beginning with a character other than
  121. * '-' or all arguments after the special '--' option
  122. */
  123. #define FUSE_OPT_KEY_NONOPT -2
  124. /**
  125. * Special key value for options to keep
  126. *
  127. * Argument is not passed to processing function, but behave as if the
  128. * processing function returned 1
  129. */
  130. #define FUSE_OPT_KEY_KEEP -3
  131. /**
  132. * Special key value for options to discard
  133. *
  134. * Argument is not passed to processing function, but behave as if the
  135. * processing function returned zero
  136. */
  137. #define FUSE_OPT_KEY_DISCARD -4
  138. /**
  139. * Processing function
  140. *
  141. * This function is called if
  142. * - option did not match any 'struct fuse_opt'
  143. * - argument is a non-option
  144. * - option did match and offset was set to -1
  145. *
  146. * The 'arg' parameter will always contain the whole argument or
  147. * option including the parameter if exists. A two-argument option
  148. * ("-x foo") is always converted to single argument option of the
  149. * form "-xfoo" before this function is called.
  150. *
  151. * Options of the form '-ofoo' are passed to this function without the
  152. * '-o' prefix.
  153. *
  154. * The return value of this function determines whether this argument
  155. * is to be inserted into the output argument vector, or discarded.
  156. *
  157. * @param data is the user data passed to the fuse_opt_parse() function
  158. * @param arg is the whole argument or option
  159. * @param key determines why the processing function was called
  160. * @param outargs the current output argument list
  161. * @return -1 on error, 0 if arg is to be discarded, 1 if arg should be kept
  162. */
  163. typedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key,
  164. struct fuse_args *outargs);
  165. /**
  166. * Option parsing function
  167. *
  168. * If 'args' was returned from a previous call to fuse_opt_parse() or
  169. * it was constructed from
  170. *
  171. * A NULL 'args' is equivalent to an empty argument vector
  172. *
  173. * A NULL 'opts' is equivalent to an 'opts' array containing a single
  174. * end marker
  175. *
  176. * A NULL 'proc' is equivalent to a processing function always
  177. * returning '1'
  178. *
  179. * @param args is the input and output argument list
  180. * @param data is the user data
  181. * @param opts is the option description array
  182. * @param proc is the processing function
  183. * @return -1 on error, 0 on success
  184. */
  185. int fuse_opt_parse(struct fuse_args *args, void *data,
  186. const struct fuse_opt opts[], fuse_opt_proc_t proc);
  187. /**
  188. * Add an option to a comma separated option list
  189. *
  190. * @param opts is a pointer to an option list, may point to a NULL value
  191. * @param opt is the option to add
  192. * @return -1 on allocation error, 0 on success
  193. */
  194. int fuse_opt_add_opt(char **opts, const char *opt);
  195. /**
  196. * Add an option, escaping commas, to a comma separated option list
  197. *
  198. * @param opts is a pointer to an option list, may point to a NULL value
  199. * @param opt is the option to add
  200. * @return -1 on allocation error, 0 on success
  201. */
  202. int fuse_opt_add_opt_escaped(char **opts, const char *opt);
  203. /**
  204. * Add an argument to a NULL terminated argument vector
  205. *
  206. * @param args is the structure containing the current argument list
  207. * @param arg is the new argument to add
  208. * @return -1 on allocation error, 0 on success
  209. */
  210. int fuse_opt_add_arg(struct fuse_args *args, const char *arg);
  211. /**
  212. * Add an argument at the specified position in a NULL terminated
  213. * argument vector
  214. *
  215. * Adds the argument to the N-th position. This is useful for adding
  216. * options at the beginning of the array which must not come after the
  217. * special '--' option.
  218. *
  219. * @param args is the structure containing the current argument list
  220. * @param pos is the position at which to add the argument
  221. * @param arg is the new argument to add
  222. * @return -1 on allocation error, 0 on success
  223. */
  224. int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg);
  225. /**
  226. * Free the contents of argument list
  227. *
  228. * The structure itself is not freed
  229. *
  230. * @param args is the structure containing the argument list
  231. */
  232. void fuse_opt_free_args(struct fuse_args *args);
  233. /**
  234. * Check if an option matches
  235. *
  236. * @param opts is the option description array
  237. * @param opt is the option to match
  238. * @return 1 if a match is found, 0 if not
  239. */
  240. int fuse_opt_match(const struct fuse_opt opts[], const char *opt);
  241. #ifdef __cplusplus
  242. }
  243. #endif
  244. #endif /* _FUSE_OPT_H_ */