Leaked source code of windows server 2003
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.

387 lines
10 KiB

  1. /*++
  2. UTEST.C
  3. Test program for the options subsystem. Must compile under
  4. NT and UNIX
  5. Created, 5/24/1997 by DavidCHR
  6. --*/
  7. #include "private.h"
  8. optEnumStruct enums[] = {
  9. { "one", (PVOID) 1, "should end up 1" },
  10. { "two", (PVOID) 2, "should end up 2" },
  11. { "beef", (PVOID) 0xdeadbeef, "should be 0xdeadbeef" },
  12. { "beefs", (PVOID) 0xbadf00d, "should be 0xbadf00d" },
  13. { "secret", (PVOID) 60, }, // shouldn't show in help
  14. TERMINATE_ARRAY
  15. };
  16. ULONG enumTestVariable=0;
  17. PCHAR SubString = NULL;
  18. optionStruct substructOptions[] = {
  19. { "h", NULL, OPT_HELP },
  20. { "substr", &SubString, OPT_STRING, "Substring option" },
  21. TERMINATE_ARRAY
  22. };
  23. PCHAR FuncString1 = NULL;
  24. PCHAR FuncString2 = NULL;
  25. PCHAR StringOption = NULL;
  26. int IntegerOption = 0L;
  27. BOOL BooleanOption = FALSE;
  28. float FloatOption = 0.0;
  29. PCHAR UndocumentedString = NULL;
  30. PWCHAR WideCharOption = NULL;
  31. UNICODE_STRING UnicodeStringOption = { 0 };
  32. int MyOptFunc( IN int argc,
  33. IN PCHAR *argv );
  34. BOOL MyOptFunc2( IN BOOL fHelp,
  35. IN POPT_FUNC_PARAMETER_DATA pData );
  36. optionStruct sampleOptions[ ] = {
  37. { "help", NULL, OPT_HELP },
  38. { "?", NULL, OPT_HELP },
  39. { NULL, NULL, OPT_DUMMY, "-----------------------------" },
  40. { NULL, NULL, OPT_CONTINUE, "These are the useful options:" },
  41. { NULL, NULL, OPT_DUMMY, "-----------------------------" },
  42. { "enum", &enumTestVariable, OPT_ENUMERATED,
  43. "Test enumerated type", enums },
  44. { "mask", &enumTestVariable, OPT_ENUMERATED | OPT_ENUM_IS_MASK,
  45. "Test enumerated type with OPT_ENUM_IS_MASK", enums },
  46. { "substruct", substructOptions, OPT_SUBOPTION | OPT_RECURSE,
  47. "substruct:help for help" },
  48. { "recurse", sampleOptions, OPT_SUBOPTION, "recurse:help for help" },
  49. { "string", &StringOption, OPT_STRING | OPT_ENVIRONMENT, "String Option",
  50. "StringOption" },
  51. { NULL, NULL, OPT_CONTINUE, "Use this option to request a string" },
  52. { "int", &IntegerOption, OPT_INT | OPT_ENVIRONMENT, "integer option",
  53. "IntegerOption" },
  54. { NULL, NULL, OPT_CONTINUE, "Use this option to request an integer" },
  55. { "func", MyOptFunc, OPT_FUNC, "function option" },
  56. { NULL, NULL, OPT_CONTINUE, "Use this option to request two strings" },
  57. { "func2", &IntegerOption, OPT_FUNC2, "FUNC2 option",
  58. MyOptFunc2 },
  59. { "bool", &BooleanOption, OPT_BOOL, "boolean option" },
  60. { NULL, NULL, OPT_CONTINUE, "Use this option to request a boolean" },
  61. { "float", &FloatOption, OPT_FLOAT, "floating point option" },
  62. { NULL, NULL, OPT_CONTINUE, "Use this option to request a float" },
  63. { "wstring",&WideCharOption, OPT_WSTRING | OPT_ENVIRONMENT,
  64. "Wide Char String option", "WideCharOption" },
  65. { "ustring",&UnicodeStringOption, OPT_USTRING | OPT_ENVIRONMENT,
  66. "Unicode String Option", "UnicodeStringOption" },
  67. { "hidden", &UndocumentedString, OPT_STRING | OPT_HIDDEN,
  68. "you should never see this line. This option is OPT_HIDDEN" },
  69. { "stop", NULL, OPT_STOP_PARSING,
  70. "halts parsing of the command line." },
  71. { NULL, NULL, OPT_DUMMY | OPT_NOHEADER, "" },
  72. { NULL, NULL, OPT_DUMMY | OPT_NOHEADER,
  73. "Example: utest /string \"foo bar baz\" /int 0x15 +bool /float 3.14" },
  74. { NULL, NULL, OPT_PAUSE },
  75. TERMINATE_ARRAY
  76. };
  77. int
  78. MyOptFunc( int argc,
  79. PCHAR *argv ) {
  80. if ( ( argv == NULL ) || (argc < 3 ) ) {
  81. /* this means the user requested help */
  82. fprintf( stderr, "func [string1] [string2]\n" );
  83. return 0;
  84. }
  85. printf( "MyOptFunc was called. argc=%d.\n", argc );
  86. FuncString1 = argv[1];
  87. FuncString2 = argv[2];
  88. return 3; /* number of arguments eaten--
  89. -func argv[1] argv[2] == 3 options */
  90. }
  91. VOID
  92. DumpArgs( int argc,
  93. PCHAR argv[] ) {
  94. int i;
  95. for ( i = 0 ; i < argc ; i++ ) {
  96. fprintf( stderr, "arg %d = %s\n", i, argv[i] );
  97. }
  98. }
  99. BOOL
  100. MyOptFunc2( IN BOOL fHelp,
  101. IN POPT_FUNC_PARAMETER_DATA pData ) {
  102. #if OPT_FUNC_PARAMETER_VERSION != 1
  103. #error "OptFuncParameterVersion has changed. Update this function."
  104. #endif
  105. optionStruct OptFunc2Options[] = {
  106. { "help", NULL, OPT_HELP },
  107. { "FuncString1", &FuncString1, OPT_INT },
  108. { "FuncString2", &FuncString2, OPT_INT },
  109. { "recurse", sampleOptions, OPT_SUBOPTION,
  110. "points back to the toplevel structure. Very amusing. :-)" },
  111. { "STOP", NULL, OPT_STOP_PARSING,
  112. "halts parsing within the Func2." },
  113. TERMINATE_ARRAY
  114. };
  115. if ( fHelp ) {
  116. CHAR buffer[ 255 ];
  117. PrintUsageEntry( stderr,
  118. "[-/+]", // switch characters
  119. pData->argv[0], // command GUARANTEED TO EXIST
  120. "->", // separator
  121. "Exercises the OPT_FUNC2 interface. Options follow",
  122. FALSE ); // FALSE--> do not repeat switch.
  123. sprintf( buffer, "-> options %s takes ", pData->argv[0] );
  124. PrintUsageEntry( stderr,
  125. NULL,
  126. NULL,
  127. "-=", // the NULLs will fill with this string.
  128. buffer,
  129. TRUE );
  130. sprintf( buffer, "(%s) ", pData->argv[0] );
  131. PrintUsage( stderr,
  132. 0L, // flags
  133. OptFunc2Options,
  134. buffer );
  135. PrintUsageEntry( stderr,
  136. NULL,
  137. NULL,
  138. "-*",
  139. "-> (end of OPT_FUNC2 options)",
  140. TRUE );
  141. return TRUE;
  142. } else {
  143. if ( pData->optionVersion != OPT_FUNC_PARAMETER_VERSION ) {
  144. fprintf( stderr,
  145. "WARNING: option library out of sync with header\n" );
  146. }
  147. fprintf( stderr,
  148. "MyOptFunc2 called. Data follows:\n"
  149. "pData->optionVersion = %d\n"
  150. "pData->dataField = 0x%p\n"
  151. "pData->argc = %d\n"
  152. "pData->argv = 0x%p\n"
  153. "pData->optionFlags = 0x%x\n"
  154. "pData->pSaveQueue = 0x%p\n",
  155. pData->optionVersion,
  156. pData->dataFieldPointer,
  157. pData->argc,
  158. pData->argv,
  159. pData->optionFlags,
  160. pData->pSaveQueue );
  161. fprintf( stderr, "%s arguments are:\n", pData->argv[0] );
  162. DumpArgs( pData->argc, pData->argv );
  163. if ( ParseOptionsEx( pData->argc-1,
  164. pData->argv+1,
  165. OptFunc2Options,
  166. pData->optionFlags,
  167. &pData->pSaveQueue,
  168. &pData->argsused,
  169. &pData->argv ) ) {
  170. fprintf( stderr,
  171. "pData->argsused IN = %d\n", pData->argsused );
  172. pData->argsused = pData->argc - pData->argsused;
  173. fprintf( stderr,
  174. "pData->argsused OUT = %d\n", pData->argsused );
  175. } else return FALSE;
  176. }
  177. fprintf( stderr, "\n Leaving OPT_FUNC2. \n\n" );
  178. return TRUE;
  179. }
  180. int
  181. main( int argc,
  182. char *argv[] ) {
  183. BOOL foo;
  184. PVOID pCleanup = NULL;
  185. optEnumStruct parserOptions[] = {
  186. { "skip", (PVOID) OPT_FLAG_SKIP_UNKNOWNS,
  187. "OPT_FLAG_SKIP_UNKNOWNS" },
  188. { "reassemble", (PVOID) OPT_FLAG_REASSEMBLE,
  189. "OPT_FLAG_REASSEMBLE" },
  190. { "terminate", (PVOID) OPT_FLAG_TERMINATE,
  191. "OPT_FLAG_TERMINATE" },
  192. TERMINATE_ARRAY
  193. };
  194. ULONG ParserFlag = OPT_FLAG_REASSEMBLE;
  195. #ifndef DEBUG_OPTIONS
  196. BOOL DebugFlag = 0; // will be ignored... just here for convenience
  197. #endif
  198. optionStruct singleOption[] = {
  199. { "utestHelp", NULL, OPT_HELP },
  200. { "parserFlag", &ParserFlag, OPT_ENUMERATED,
  201. "flags to pass to ParseOptionsEx", parserOptions },
  202. { "headerlength", &OptMaxHeaderLength, OPT_INT,
  203. "OptMaxHeaderLength value (formatting)" },
  204. { "commandLength", &OptMaxCommandLength, OPT_INT,
  205. "OptMaxCommandLength value (formatting)" },
  206. { "separatorLength", &OptMaxSeparatorLength, OPT_INT,
  207. "OptMaxSeparatorLength value (formatting)" },
  208. { "descriptionLength", &OptMaxDescriptionLength, OPT_INT,
  209. "OptMaxDescriptionLength value (formatting)" },
  210. { "debug", &DebugFlag, OPT_INT, "Debugger status" },
  211. TERMINATE_ARRAY
  212. };
  213. DebugFlag |= OPTION_DEBUGGING_LEVEL;
  214. foo = ParseOptionsEx( argc-1, argv+1, singleOption,
  215. 0, &pCleanup, &argc, &argv );
  216. ParserFlag |= OPT_FLAG_MEMORYLIST_OK;
  217. fprintf( stderr,
  218. "first ParseOptionsEx returned 0x%x \n"
  219. " saveQueue 0x%p \n"
  220. " passing in flags 0x%x \n",
  221. foo, pCleanup, ParserFlag );
  222. if ( argc ) {
  223. ULONG i;
  224. fprintf( stderr, "%d new options: \n", argc );
  225. DumpArgs( argc, argv );
  226. fprintf( stderr, "\n" );
  227. }
  228. foo = ParseOptionsEx( argc, argv, sampleOptions, ParserFlag,
  229. &pCleanup, &argc, &argv );
  230. fprintf( stderr,
  231. "ParseOptionsEx returns %d\n"
  232. " new argv = 0x%p\n"
  233. " new argc = %d \n",
  234. foo, argv, argc );
  235. DumpArgs( argc, argv );
  236. printf( "Formatting values were:\n"
  237. "\tOptMaxHeaderLength = %d\n"
  238. "\tOptMaxCommandLength = %d\n"
  239. "\tOptMaxSeparatorLength = %d\n"
  240. "\tOptMaxDescriptionLength = %d\n",
  241. OptMaxHeaderLength,
  242. OptMaxCommandLength,
  243. OptMaxSeparatorLength,
  244. OptMaxDescriptionLength );
  245. printf( "\nOptions used:\n"
  246. "\tstring = \"%hs\"\n"
  247. "\tint = %d \n"
  248. "\tBool = 0x%x \n"
  249. /* "Float = %f \n" */
  250. "\twstring = L\"%ws\"\n"
  251. "\tustring = (unicode string) %wZ \n"
  252. "\thidden = \"%hs\"\n"
  253. "\tsubstruct:substr = \"%hs\"\n"
  254. "\tfuncString1 = \"%hs\"\n"
  255. "\tfuncString2 = \"%hs\"\n"
  256. "\tenum = %d (0x%x)\n",
  257. StringOption,
  258. IntegerOption,
  259. BooleanOption,
  260. /* FloatOption, */ /* floating point not loaded?
  261. What does that mean?! */
  262. WideCharOption,
  263. &UnicodeStringOption,
  264. UndocumentedString,
  265. SubString,
  266. FuncString1,
  267. FuncString2,
  268. enumTestVariable, enumTestVariable );
  269. CleanupOptionDataEx( pCleanup );
  270. return foo;
  271. }