dclib-ui.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /***************************************************************************
  2. * *
  3. * _____ ____ *
  4. * | __ \ / __ \ _ _ _____ *
  5. * | | \ \ / / \_\ | | | | _ \ *
  6. * | | \ \| | | | | | |_| | *
  7. * | | | || | | | | | ___/ *
  8. * | | / /| | __ | | | | _ \ *
  9. * | |__/ / \ \__/ / | |___| | |_| | *
  10. * |_____/ \____/ |_____|_|_____/ *
  11. * *
  12. * Wiimms source code library *
  13. * *
  14. ***************************************************************************
  15. * *
  16. * Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
  17. * *
  18. ***************************************************************************
  19. * *
  20. * This library is free software; you can redistribute it and/or modify *
  21. * it under the terms of the GNU General Public License as published by *
  22. * the Free Software Foundation; either version 2 of the License, or *
  23. * (at your option) any later version. *
  24. * *
  25. * This library is distributed in the hope that it will be useful, *
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  28. * GNU General Public License for more details. *
  29. * *
  30. * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
  31. * *
  32. ***************************************************************************/
  33. #ifndef DCLIB_UI_H
  34. #define DCLIB_UI_H
  35. #include "dclib-basics.h"
  36. #include <getopt.h>
  37. //
  38. ///////////////////////////////////////////////////////////////////////////////
  39. /////////////// definitions ///////////////
  40. ///////////////////////////////////////////////////////////////////////////////
  41. // [[OptionIndex_t]] // max size of OptionIndex[]
  42. #ifndef UIOPT_INDEX_SIZE
  43. #define UIOPT_INDEX_SIZE 0x100
  44. #endif
  45. #undef UIOPT_INDEX_FW
  46. #if UIOPT_INDEX_SIZE <= 0x100
  47. #define UIOPT_INDEX_FW 2
  48. typedef u8 OptionIndex_t;
  49. #else
  50. #define UIOPT_INDEX_FW 3
  51. typedef u16 OptionIndex_t;
  52. #endif
  53. ///////////////////////////////////////////////////////////////////////////////
  54. enum // some const
  55. {
  56. UIOPT_USED_MASK = 0x7f, // mask to calculate usage count
  57. UIOPT_LONG_BASE = 0x80, // first index for "only long options"
  58. };
  59. //
  60. ///////////////////////////////////////////////////////////////////////////////
  61. /////////////// InfoOption_t ///////////////
  62. ///////////////////////////////////////////////////////////////////////////////
  63. // [[InfoOption_t]]
  64. typedef struct InfoOption_t
  65. {
  66. int id; // id of the option
  67. bool optional_parm; // true: option has optional parameters
  68. bool hidden; // true: option is hidden
  69. bool deprecated; // true: option is deprecated
  70. bool ignore; // true: ignore on 'HELP OPTIONS'
  71. bool separator; // true: print a separator above
  72. char short_name; // short option name
  73. ccp long_name; // the main long option name
  74. ccp param; // parameter name
  75. ccp help; // help info
  76. } InfoOption_t;
  77. //
  78. ///////////////////////////////////////////////////////////////////////////////
  79. /////////////// InfoCommand_t ///////////////
  80. ///////////////////////////////////////////////////////////////////////////////
  81. // [[InfoCommand_t]]
  82. typedef struct InfoCommand_t
  83. {
  84. int id; // id of the command
  85. bool hidden; // true: command is hidden
  86. bool deprecated; // true: command is deprecated
  87. bool separator; // true: print a separator above
  88. ccp name1; // main name
  89. ccp name2; // NULL or alternative name
  90. ccp syntax; // NULL or syntax info
  91. ccp help; // help text
  92. ccp xhelp; // NULL or a extended help text
  93. int n_opt; // number of options == elements of 'opt'
  94. const InfoOption_t ** opt; // field with option info
  95. u8 * opt_allowed; // field with OPT__N_SPECIFIC elements
  96. // 0: option permitted, 1: option allowed
  97. } InfoCommand_t;
  98. //
  99. ///////////////////////////////////////////////////////////////////////////////
  100. /////////////// InfoUI_t ///////////////
  101. ///////////////////////////////////////////////////////////////////////////////
  102. // [[InfoUI_t]]
  103. typedef struct InfoUI_t
  104. {
  105. ccp tool_name; // name of the tool
  106. //----- commands -----
  107. int n_cmd; // == CMD__N
  108. const KeywordTab_t * cmd_tab; // NULL or pointer to command table
  109. const InfoCommand_t * cmd_info; // pointer to 'CommandInfo[]'
  110. //----- options -----
  111. int n_opt_specific; // == OPT__N_SPECIFIC
  112. int n_opt_total; // == OPT__N_TOTAL
  113. const InfoOption_t * opt_info; // pointer to 'OptionInfo[]'
  114. u8 * opt_used; // pointer to 'OptionUsed[]'
  115. const OptionIndex_t * opt_index; // pointer to 'OptionIndex[]'
  116. ccp opt_short; // pointer to 'OptionShort[]'
  117. const struct option * opt_long; // pointer to 'OptionLong[]'
  118. } InfoUI_t;
  119. //
  120. ///////////////////////////////////////////////////////////////////////////////
  121. /////////////// Interface ///////////////
  122. ///////////////////////////////////////////////////////////////////////////////
  123. ///////////////////////////////////////////////////////////////////////////////
  124. enumError RegisterOptionByIndex
  125. (
  126. const InfoUI_t * iu, // valid pointer
  127. int opt_index, // index of option (OPT_*)
  128. int level, // the level of registration
  129. bool is_env // true: register environment pre setting
  130. );
  131. enumError RegisterOptionByName
  132. (
  133. const InfoUI_t * iu, // valid pointer
  134. int opt_name, // short name of GO_* value of option
  135. int level, // the level of registration
  136. bool is_env // true: register environment pre setting
  137. );
  138. enumError VerifySpecificOptions ( const InfoUI_t * iu, const KeywordTab_t * cmd );
  139. int GetOptionCount ( const InfoUI_t * iu, int option );
  140. void DumpUsedOptions ( const InfoUI_t * iu, FILE * f, int indent );
  141. void WarnDepractedOptions ( const InfoUI_t * iu );
  142. typedef enumError (*check_opt_func) ( int argc, char ** argv, bool mode );
  143. enumError CheckEnvOptions ( ccp varname, check_opt_func );
  144. ///////////////////////////////////////////////////////////////////////////////
  145. void DumpText
  146. (
  147. FILE * f, // output file, if NULL: 'pbuf' must be set
  148. char * pbuf, // output buffer, ignored if 'f' not NULL
  149. char * pbuf_end, // end of output buffer
  150. ccp text, // source text
  151. int text_len, // >=0: length of text, <0: use strlen(text)
  152. bool is_makedoc, // true: write makedoc format
  153. ccp end // write this text at the end of all
  154. );
  155. ///////////////////////////////////////////////////////////////////////////////
  156. void PrintHelp
  157. (
  158. const InfoUI_t * iu, // valid pointer
  159. FILE * f, // valid output stream
  160. int indent, // indent of output
  161. ccp help_cmd, // NULL or name of help command
  162. ccp info, // NULL or additional text
  163. ccp base_uri, // NULL or base URI for a external help link
  164. ccp first_param // NULL or first param of argv[]
  165. );
  166. void PrintHelpCmd
  167. (
  168. const InfoUI_t * iu, // valid pointer
  169. FILE * f, // valid output stream
  170. int indent, // indent of output
  171. int cmd, // index of command
  172. ccp help_cmd, // NULL or name of help command
  173. ccp info, // NULL or poiner to additional text
  174. ccp base_uri // NULL or base URI for a external help link
  175. );
  176. //
  177. ///////////////////////////////////////////////////////////////////////////////
  178. /////////////// END ///////////////
  179. ///////////////////////////////////////////////////////////////////////////////
  180. #endif // DCLIB_UI_H