PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/usb-modeswitch-1.2.3/jim/jim-subcmd.h

#
C Header | 92 lines | 28 code | 13 blank | 51 comment | 0 complexity | 4940949c569036b31254eedbd36133b5 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0
  1. /* Provides a common approach to implementing Tcl commands
  2. * which implement subcommands
  3. */
  4. #ifndef JIM_SUBCMD_H
  5. #define JIM_SUBCMD_H
  6. #include <jim.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #define JIM_MODFLAG_HIDDEN 0x0001 /* Don't show the subcommand in usage or commands */
  11. #define JIM_MODFLAG_FULLARGV 0x0002 /* Subcmd proc gets called with full argv */
  12. /* Custom flags start at 0x0100 */
  13. /**
  14. * Returns JIM_OK if OK, JIM_ERR (etc.) on error, break, continue, etc.
  15. * Returns -1 if invalid args.
  16. */
  17. typedef int tclmod_cmd_function(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  18. typedef struct {
  19. const char *cmd; /* Name of the (sub)command */
  20. const char *args; /* Textual description of allowed args */
  21. tclmod_cmd_function *function; /* Function implementing the subcommand */
  22. short minargs; /* Minimum required arguments */
  23. short maxargs; /* Maximum allowed arguments or -1 if no limit */
  24. unsigned flags; /* JIM_MODFLAG_... plus custom flags */
  25. const char *description; /* Description of the subcommand */
  26. } jim_subcmd_type;
  27. /**
  28. * Looks up the appropriate subcommand in the given command table and return
  29. * the command function which implements the subcommand.
  30. * NULL will be returned and an appropriate error will be set if the subcommand or
  31. * arguments are invalid.
  32. *
  33. * Typical usage is:
  34. * {
  35. * const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, command_table, argc, argv);
  36. *
  37. * return Jim_CallSubCmd(interp, ct, argc, argv);
  38. * }
  39. *
  40. */
  41. const jim_subcmd_type *
  42. Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv);
  43. /**
  44. * Parses the args against the given command table and executes the subcommand if found
  45. * or sets an appropriate error if the subcommand or arguments is invalid.
  46. *
  47. * Can be used directly with Jim_CreateCommand() where the ClientData is the command table.
  48. *
  49. * e.g. Jim_CreateCommand(interp, "mycmd", Jim_SubCmdProc, command_table, NULL);
  50. */
  51. int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  52. /**
  53. * Invokes the given subcmd with the given args as returned
  54. * by Jim_ParseSubCmd()
  55. *
  56. * If ct is NULL, returns JIM_ERR, leaving any message.
  57. * Otherwise invokes ct->function
  58. *
  59. * If ct->function returns -1, sets an error message and returns JIM_ERR.
  60. * Otherwise returns the result of ct->function.
  61. */
  62. int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type *ct, int argc, Jim_Obj *const *argv);
  63. /**
  64. * Standard processing for a command.
  65. *
  66. * This does the '-help' and '-usage' check and the number of args checks.
  67. * for a top level command against a single 'jim_subcmd_type' structure.
  68. *
  69. * Additionally, if command_table->function is set, it should point to a sub command table
  70. * and '-subhelp ?subcmd?', '-subusage' and '-subcommands' are then also recognised.
  71. *
  72. * Returns 0 if user requested usage, -1 on arg error, 1 if OK to process.
  73. */
  74. int
  75. Jim_CheckCmdUsage(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv);
  76. #ifdef __cplusplus
  77. }
  78. #endif
  79. #endif