cli.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Standard Command Line Interface
  20. */
  21. #ifndef _ASTERISK_CLI_H
  22. #define _ASTERISK_CLI_H
  23. #if defined(__cplusplus) || defined(c_plusplus)
  24. extern "C" {
  25. #endif
  26. #include "asterisk/linkedlists.h"
  27. #include "asterisk/strings.h"
  28. void ast_cli(int fd, const char *fmt, ...)
  29. __attribute__((format(printf, 2, 3)));
  30. /* dont check permissions while passing this option as a 'uid'
  31. * to the cli_has_permissions() function. */
  32. #define CLI_NO_PERMS -1
  33. #define RESULT_SUCCESS 0
  34. #define RESULT_SHOWUSAGE 1
  35. #define RESULT_FAILURE 2
  36. #define CLI_SUCCESS (char *)RESULT_SUCCESS
  37. #define CLI_SHOWUSAGE (char *)RESULT_SHOWUSAGE
  38. #define CLI_FAILURE (char *)RESULT_FAILURE
  39. #define AST_MAX_CMD_LEN 16
  40. #define AST_MAX_ARGS 64
  41. #define AST_CLI_COMPLETE_EOF "_EOF_"
  42. /*!
  43. * In many cases we need to print singular or plural
  44. * words depending on a count. This macro helps us e.g.
  45. * printf("we have %d object%s", n, ESS(n));
  46. */
  47. #define ESS(x) ((x) == 1 ? "" : "s")
  48. /*!
  49. * \brief Return Yes or No depending on the argument.
  50. *
  51. * Note that this should probably still be used for CLI commands instead of
  52. * AST_YESNO(), in the off chance we someday want to translate the CLI.
  53. *
  54. * \param x Boolean value
  55. * \return "Yes" if x is true (non-zero)
  56. * \return "No" if x is false (zero)
  57. */
  58. #define AST_CLI_YESNO(x) AST_YESNO(x)
  59. /*! \brief return On or Off depending on the argument.
  60. * This is used in many places in CLI command, having a function to generate
  61. * this helps maintaining a consistent output (and possibly emitting the
  62. * output in other languages, at some point).
  63. */
  64. #define AST_CLI_ONOFF(x) (x) ? "On" : "Off"
  65. /*! \page CLI_command_API CLI command API
  66. CLI commands are described by a struct ast_cli_entry that contains
  67. all the components for their implementation.
  68. In the "old-style" format, the record must contain:
  69. - a NULL-terminated array of words constituting the command, e.g.
  70. { "set", "debug", "on", NULL },
  71. - a summary string (short) and a usage string (longer);
  72. - a handler which implements the command itself, invoked with
  73. a file descriptor and argc/argv as typed by the user
  74. - a 'generator' function which, given a partial string, can
  75. generate legal completions for it.
  76. An example is
  77. int old_setdebug(int fd, int argc, char *argv[]);
  78. char *dbg_complete(const char *line, const char *word, int pos, int n);
  79. { { "set", "debug", "on", NULL }, do_setdebug, "Enable debugging",
  80. set_debug_usage, dbg_complete },
  81. In the "new-style" format, all the above functionalities are implemented
  82. by a single function, and the arguments tell which output is required.
  83. The prototype is the following:
  84. char *new_setdebug(const struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  85. ...
  86. // this is how we create the entry to register
  87. AST_CLI_DEFINE(new_setdebug, "short description")
  88. ...
  89. To help the transition, we make the pointer to the struct ast_cli_entry
  90. available to old-style handlers via argv[-1].
  91. An example of new-style handler is the following
  92. \code
  93. static char *test_new_cli(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  94. {
  95. static const char * const choices[] = { "one", "two", "three", NULL };
  96. switch (cmd) {
  97. case CLI_INIT:
  98. e->command = "do this well";
  99. e->usage =
  100. "Usage: do this well <arg>\n"
  101. " typically multiline with body indented\n";
  102. return NULL;
  103. case CLI_GENERATE:
  104. if (a->pos > e->args)
  105. return NULL;
  106. return ast_cli_complete(a->word, choices, a->n);
  107. default:
  108. // we are guaranteed to be called with argc >= e->args;
  109. if (a->argc > e->args + 1) // we accept one extra argument
  110. return CLI_SHOWUSAGE;
  111. ast_cli(a->fd, "done this well for %s\n", e->args[argc-1]);
  112. return CLI_SUCCESS;
  113. }
  114. }
  115. \endcode
  116. */
  117. /*! \brief calling arguments for new-style handlers.
  118. * \arg \ref CLI_command_API
  119. */
  120. enum ast_cli_command {
  121. CLI_INIT = -2, /* return the usage string */
  122. CLI_GENERATE = -3, /* behave as 'generator', remap argv to struct ast_cli_args */
  123. CLI_HANDLER = -4, /* run the normal handler */
  124. };
  125. /* argument for new-style CLI handler */
  126. struct ast_cli_args {
  127. const int fd;
  128. const int argc;
  129. const char * const *argv;
  130. const char *line; /* the current input line */
  131. const char *word; /* the word we want to complete */
  132. const int pos; /* position of the word to complete */
  133. const int n; /* the iteration count (n-th entry we generate) */
  134. };
  135. /*! \brief descriptor for a cli entry.
  136. * \arg \ref CLI_command_API
  137. */
  138. struct ast_cli_entry {
  139. const char * const cmda[AST_MAX_CMD_LEN]; /*!< words making up the command.
  140. * set the first entry to NULL for a new-style entry.
  141. */
  142. const char * const summary; /*!< Summary of the command (< 60 characters) */
  143. const char * usage; /*!< Detailed usage information */
  144. int inuse; /*!< For keeping track of usage */
  145. struct module *module; /*!< module this belongs to */
  146. char *_full_cmd; /*!< built at load time from cmda[] */
  147. int cmdlen; /*!< len up to the first invalid char [<{% */
  148. /*! \brief This gets set in ast_cli_register()
  149. */
  150. int args; /*!< number of non-null entries in cmda */
  151. char *command; /*!< command, non-null for new-style entries */
  152. char *(*handler)(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  153. /*! For linking */
  154. AST_LIST_ENTRY(ast_cli_entry) list;
  155. };
  156. #if defined(__cplusplus) || defined(c_plusplus)
  157. #define AST_CLI_DEFINE(fn, txt) { { "" }, txt, NULL, 0, NULL, NULL, 0, 0, NULL, fn }
  158. #else
  159. /* XXX the parser in gcc 2.95 gets confused if you don't put a space
  160. * between the last arg before VA_ARGS and the comma */
  161. #define AST_CLI_DEFINE(fn, txt , ... ) { .handler = fn, .summary = txt, ## __VA_ARGS__ }
  162. #endif
  163. /*!
  164. * Helper function to generate cli entries from a NULL-terminated array.
  165. * Returns the n-th matching entry from the array, or NULL if not found.
  166. * Can be used to implement generate() for static entries as below
  167. * (in this example we complete the word in position 2):
  168. \code
  169. char *my_generate(const char *line, const char *word, int pos, int n)
  170. {
  171. static const char * const choices[] = { "one", "two", "three", NULL };
  172. if (pos == 2)
  173. return ast_cli_complete(word, choices, n);
  174. else
  175. return NULL;
  176. }
  177. \endcode
  178. */
  179. char *ast_cli_complete(const char *word, const char * const choices[], int pos);
  180. /*!
  181. * \brief Interprets a command
  182. * Interpret a command s, sending output to fd if uid:gid has permissions
  183. * to run this command. uid = CLI_NO_PERMS to avoid checking user permissions
  184. * gid = CLI_NO_PERMS to avoid checking group permissions.
  185. * \param uid User ID that is trying to run the command.
  186. * \param gid Group ID that is trying to run the command.
  187. * \param fd pipe
  188. * \param s incoming string
  189. * \retval 0 on success
  190. * \retval -1 on failure
  191. */
  192. int ast_cli_command_full(int uid, int gid, int fd, const char *s);
  193. #define ast_cli_command(fd,s) ast_cli_command_full(CLI_NO_PERMS, CLI_NO_PERMS, fd, s)
  194. /*!
  195. * \brief Executes multiple CLI commands
  196. * Interpret strings separated by NULL and execute each one, sending output to fd
  197. * if uid has permissions, uid = CLI_NO_PERMS to avoid checking users permissions.
  198. * gid = CLI_NO_PERMS to avoid checking group permissions.
  199. * \param uid User ID that is trying to run the command.
  200. * \param gid Group ID that is trying to run the command.
  201. * \param fd pipe
  202. * \param size is the total size of the string
  203. * \param s incoming string
  204. * \retval number of commands executed
  205. */
  206. int ast_cli_command_multiple_full(int uid, int gid, int fd, size_t size, const char *s);
  207. #define ast_cli_command_multiple(fd,size,s) ast_cli_command_multiple_full(CLI_NO_PERMS, CLI_NO_PERMS, fd, size, s)
  208. /*! \brief Registers a command or an array of commands
  209. * \param e which cli entry to register.
  210. * Register your own command
  211. * \retval 0 on success
  212. * \retval -1 on failure
  213. */
  214. int ast_cli_register(struct ast_cli_entry *e);
  215. /*!
  216. * \brief Register multiple commands
  217. * \param e pointer to first cli entry to register
  218. * \param len number of entries to register
  219. */
  220. int ast_cli_register_multiple(struct ast_cli_entry *e, int len);
  221. /*!
  222. * \brief Unregisters a command or an array of commands
  223. * \param e which cli entry to unregister
  224. * Unregister your own command. You must pass a completed ast_cli_entry structure
  225. * \return 0
  226. */
  227. int ast_cli_unregister(struct ast_cli_entry *e);
  228. /*!
  229. * \brief Unregister multiple commands
  230. * \param e pointer to first cli entry to unregister
  231. * \param len number of entries to unregister
  232. */
  233. int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len);
  234. /*!
  235. * \brief Readline madness
  236. * Useful for readline, that's about it
  237. * \retval 0 on success
  238. * \retval -1 on failure
  239. *
  240. * Only call this function to proxy the CLI generator to
  241. * another.
  242. */
  243. char *ast_cli_generator(const char *, const char *, int);
  244. int ast_cli_generatornummatches(const char *, const char *);
  245. /*!
  246. * \brief Generates a NULL-terminated array of strings that
  247. * 1) begin with the string in the second parameter, and
  248. * 2) are valid in a command after the string in the first parameter.
  249. *
  250. * The first entry (offset 0) of the result is the longest common substring
  251. * in the results, useful to extend the string that has been completed.
  252. * Subsequent entries are all possible values, followed by a NULL.
  253. * All strings and the array itself are malloc'ed and must be freed
  254. * by the caller.
  255. *
  256. * \warning This function cannot be called recursively so it will always
  257. * fail if called from a CLI_GENERATE callback.
  258. */
  259. char **ast_cli_completion_matches(const char *, const char *);
  260. /*!
  261. * \brief Generates a vector of strings for CLI completion.
  262. *
  263. * \param text Complete input being matched.
  264. * \param word Current word being matched
  265. *
  266. * The results contain strings that both:
  267. * 1) Begin with the string in \a word.
  268. * 2) Are valid in a command after the string in \a text.
  269. *
  270. * The first entry (offset 0) of the result is the longest common substring
  271. * in the results, useful to extend the string that has been completed.
  272. * Subsequent entries are all possible values.
  273. *
  274. * \note All strings and the vector itself are malloc'ed and must be freed
  275. * by the caller.
  276. *
  277. * \note The vector is sorted and does not contain any duplicates.
  278. *
  279. * \warning This function cannot be called recursively so it will always
  280. * fail if called from a CLI_GENERATE callback.
  281. */
  282. struct ast_vector_string *ast_cli_completion_vector(const char *text, const char *word);
  283. /*!
  284. * \brief Add a result to a request for completion options.
  285. *
  286. * \param value A completion option text.
  287. *
  288. * \retval 0 Success
  289. * \retval -1 Failure
  290. *
  291. * This is an alternative to returning individual values from CLI_GENERATE. Instead
  292. * of repeatedly being asked for the next match and having to start over, you can
  293. * call this function repeatedly from your own stateful loop. When all matches have
  294. * been added you can return NULL from the CLI_GENERATE function.
  295. *
  296. * \note This function always eventually results in calling ast_free on \a value.
  297. */
  298. int ast_cli_completion_add(char *value);
  299. /*!
  300. * \brief Command completion for the list of active channels.
  301. *
  302. * This can be called from a CLI command completion function that wants to
  303. * complete from the list of active channels. 'rpos' is the required
  304. * position in the command. This function will return NULL immediately if
  305. * 'rpos' is not the same as the current position, 'pos'.
  306. */
  307. char *ast_complete_channels(const char *line, const char *word, int pos, int state, int rpos);
  308. /*!
  309. * \brief Allow a CLI command to be executed while Asterisk is shutting down.
  310. *
  311. * CLI commands by defeault are disabled when Asterisk is shutting down. This is
  312. * to ensure the safety of the shutdown since CLI commands may attempt to access
  313. * resources that have been freed as a result of the shutdown.
  314. *
  315. * If a CLI command should be allowed at shutdown, then the best way to enable this
  316. * is to call ast_cli_allow_at_shutdown during the CLI_INIT state of the CLI handler.
  317. */
  318. int ast_cli_allow_at_shutdown(struct ast_cli_entry *e);
  319. #if defined(__cplusplus) || defined(c_plusplus)
  320. }
  321. #endif
  322. #endif /* _ASTERISK_CLI_H */