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.

332 lines
9.5 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. /*****************************************************************************
  3. *
  4. * PARSE_A.C
  5. *
  6. * ANSI stubs / replacements for the UNICODE command line parsing
  7. * routines (parse.c)
  8. *
  9. * External Entry Points: (defined in utilsub.h)
  10. *
  11. * ParseCommandLineA()
  12. * IsTokenPresentA()
  13. * SetTokenPresentA()
  14. * SetTokenNotPresentA()
  15. *
  16. *
  17. ****************************************************************************/
  18. /* Get the standard C includes */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <windows.h>
  23. #include <winstaw.h>
  24. #include <utilsub.h>
  25. /*=============================================================================
  26. == Local Functions Defined
  27. ============================================================================*/
  28. /*=============================================================================
  29. == External Functions Used
  30. ============================================================================*/
  31. /*=============================================================================
  32. == Local Variables Used
  33. ============================================================================*/
  34. /*=============================================================================
  35. == Global Variables Used
  36. ============================================================================*/
  37. /*****************************************************************************
  38. *
  39. * ParseCommandLineA (ANSI stub for ParseCommandLineW)
  40. *
  41. * Thunks over argv_a (ANSI) to argv_w (UNICODE) and TOKMAPA to TOKMAPW,
  42. * calls ParseCommandLineW(), then thunks back TOKMAPW to TOKMAPA and
  43. * returns
  44. *
  45. * ENTRY:
  46. * (refer to ParseCommandLineW)
  47. * EXIT:
  48. * (refer to ParseCommandLineW), plus
  49. * PARSE_FLAG_NOT_ENOUGH_MEMORY
  50. *
  51. ****************************************************************************/
  52. #define tmFormIsString(x) ((x == TMFORM_S_STRING) || (x == TMFORM_DATE) || (x == TMFORM_PHONE) || (x == TMFORM_STRING) || (x == TMFORM_X_STRING))
  53. USHORT WINAPI
  54. ParseCommandLineA( INT argc,
  55. CHAR **argv_a,
  56. PTOKMAPA ptm_a,
  57. USHORT flag )
  58. {
  59. int i;
  60. size_t len1, len2;
  61. USHORT rc = PARSE_FLAG_NOT_ENOUGH_MEMORY; // default to memory error
  62. WCHAR **argv_w = NULL;
  63. PTOKMAPA ptmtmp_a;
  64. PTOKMAPW ptmtmp_w, ptm_w = NULL;
  65. /*
  66. * If no parameters, we skip a lot of work.
  67. */
  68. if ( argc == 0 ) {
  69. rc = PARSE_FLAG_NO_PARMS;
  70. return(rc);
  71. }
  72. /*
  73. * Alloc and form WCHAR argvw array.
  74. */
  75. if ( !(argv_w = (WCHAR **)malloc( (len1 = argc * sizeof(WCHAR *)) )) )
  76. goto done; // memory error
  77. memset(argv_w, 0, len1); // zero all to init pointers to NULL
  78. for ( i = 0; i < argc; i++ ) {
  79. if ( argv_w[i] = malloc((len1 = ((len2 = strlen(argv_a[i])+1) * 2))) ) {
  80. memset(argv_w[i], 0, len1);
  81. mbstowcs(argv_w[i], argv_a[i], len2);
  82. } else {
  83. goto done; // memory error
  84. }
  85. }
  86. /*
  87. * Alloc and form TOKMAPW array.
  88. */
  89. for ( ptmtmp_a=ptm_a, i=0;
  90. ptmtmp_a->tmToken != NULL;
  91. ptmtmp_a++, i++ );
  92. if ( !(ptm_w = (PTOKMAPW)malloc( (len1 = ++i * sizeof(TOKMAPW)) )) )
  93. goto done; // memory error
  94. memset(ptm_w, 0, len1); // zero all to init pointers to NULL
  95. for ( ptmtmp_w=ptm_w, ptmtmp_a=ptm_a;
  96. ptmtmp_a->tmToken != NULL;
  97. ptmtmp_w++, ptmtmp_a++ ) {
  98. /*
  99. * Allocate and convert token.
  100. */
  101. if ( ptmtmp_w->tmToken =
  102. malloc((len1 = ((len2 = strlen(ptmtmp_a->tmToken)+1) * 2))) ) {
  103. memset(ptmtmp_w->tmToken, 0, len1);
  104. mbstowcs(ptmtmp_w->tmToken, ptmtmp_a->tmToken, len2);
  105. } else {
  106. goto done; // memory error
  107. }
  108. /*
  109. * Copy flag, form, and length (no conversion needed).
  110. */
  111. ptmtmp_w->tmFlag = ptmtmp_a->tmFlag;
  112. ptmtmp_w->tmForm = ptmtmp_a->tmForm;
  113. ptmtmp_w->tmDLen = ptmtmp_a->tmDLen;
  114. /*
  115. * Allocate or copy address if a data length was specified.
  116. */
  117. if ( ptmtmp_w->tmDLen ) {
  118. /*
  119. * Allocate new WCHAR address if we're a string type.
  120. * Otherwise, point to original address (no conversion needed).
  121. */
  122. if ( tmFormIsString(ptmtmp_w->tmForm) ) {
  123. if ( ptmtmp_w->tmAddr =
  124. malloc(len1 = ptmtmp_w->tmDLen*sizeof(WCHAR)) )
  125. memset(ptmtmp_w->tmAddr, 0, len1);
  126. else
  127. goto done; // memory error
  128. } else {
  129. ptmtmp_w->tmAddr = ptmtmp_a->tmAddr;
  130. }
  131. /*
  132. * For proper default behavior, zero ANSI address contents if
  133. * the "don't clear memory" flag is not set.
  134. */
  135. if ( !(flag & PCL_FLAG_NO_CLEAR_MEMORY) )
  136. memset(ptmtmp_a->tmAddr, 0, ptmtmp_a->tmDLen);
  137. }
  138. }
  139. /*
  140. * Call ParseCommandLineW
  141. */
  142. rc = ParseCommandLineW(argc, argv_w, ptm_w, flag);
  143. /*
  144. * Copy flags for each TOPMAPW element. Also, convert to ANSI strings
  145. * that were present on the command line into caller's TOKMAPA array, if
  146. * data length was specified.
  147. */
  148. for ( ptmtmp_w=ptm_w, ptmtmp_a=ptm_a;
  149. ptmtmp_w->tmToken != NULL;
  150. ptmtmp_w++, ptmtmp_a++ ) {
  151. ptmtmp_a->tmFlag = ptmtmp_w->tmFlag;
  152. if ( ptmtmp_w->tmDLen &&
  153. (ptmtmp_w->tmFlag & TMFLAG_PRESENT) &&
  154. tmFormIsString(ptmtmp_w->tmForm) )
  155. wcstombs(ptmtmp_a->tmAddr, ptmtmp_w->tmAddr, ptmtmp_w->tmDLen);
  156. }
  157. done:
  158. /*
  159. * Free the argvw array.
  160. */
  161. if ( argv_w ) {
  162. for ( i = 0; i < argc; i++ ) {
  163. if ( argv_w[i] )
  164. free(argv_w[i]);
  165. }
  166. free(argv_w);
  167. }
  168. /*
  169. * Free the TOKMAPW tokens, string addresses, and TOKMAK array itself.
  170. */
  171. if ( ptm_w ) {
  172. for ( ptmtmp_w=ptm_w; ptmtmp_w->tmToken != NULL; ptmtmp_w++ ) {
  173. /*
  174. * Free token.
  175. */
  176. free(ptmtmp_w->tmToken);
  177. /*
  178. * Free address if a data length was specified and we're a
  179. * string type.
  180. */
  181. if ( ptmtmp_w->tmDLen && tmFormIsString(ptmtmp_w->tmForm) )
  182. free(ptmtmp_w->tmAddr);
  183. }
  184. free(ptm_w);
  185. }
  186. /*
  187. * Return ParseCommandLineW status.
  188. */
  189. return(rc);
  190. } // end ParseCommandLineA
  191. /*****************************************************************************
  192. *
  193. * IsTokenPresentA (ANSI version)
  194. *
  195. * Determines if a specified command line token (in given TOKMAPA array)
  196. * was present on the command line.
  197. *
  198. * ENTRY:
  199. * ptm (input)
  200. * Points to 0-terminated TOKMAPA array to scan.
  201. * pToken (input)
  202. * The token to scan for.
  203. *
  204. * EXIT:
  205. * TRUE if the specified token was present on the command line;
  206. * FALSE otherwise.
  207. *
  208. ****************************************************************************/
  209. BOOLEAN WINAPI
  210. IsTokenPresentA( PTOKMAPA ptm,
  211. PCHAR pToken )
  212. {
  213. int i;
  214. for ( i = 0; ptm[i].tmToken; i++ ) {
  215. if ( !strcmp( ptm[i].tmToken, pToken ) )
  216. return( (ptm[i].tmFlag & TMFLAG_PRESENT) ? TRUE : FALSE );
  217. }
  218. return(FALSE);
  219. } // end IsTokenPresentA
  220. /*****************************************************************************
  221. *
  222. * SetTokenPresentA (ANSI version)
  223. *
  224. * Forces a specified command line token (in given TOKMAPA array)
  225. * to be flagged as 'present' on the command line.
  226. *
  227. * ENTRY:
  228. * ptm (input)
  229. * Points to 0-terminated TOKMAPA array to scan.
  230. * pToken (input)
  231. * The token to scan for and set flags.
  232. *
  233. * EXIT:
  234. * TRUE if the specified token was found in the TOKMAPA array
  235. * (TMFLAG_PRESENT flag is set). FALSE otherwise.
  236. *
  237. ****************************************************************************/
  238. BOOLEAN WINAPI
  239. SetTokenPresentA( PTOKMAPA ptm,
  240. PCHAR pToken )
  241. {
  242. int i;
  243. for ( i = 0; ptm[i].tmToken; i++ ) {
  244. if ( !strcmp( ptm[i].tmToken, pToken ) ) {
  245. ptm[i].tmFlag |= TMFLAG_PRESENT;
  246. return(TRUE);
  247. }
  248. }
  249. return(FALSE);
  250. } // end SetTokenPresentA
  251. /*****************************************************************************
  252. *
  253. * SetTokenNotPresentA (ANSI Versio)
  254. *
  255. * Forces a specified command line token (in given TOKMAPA array)
  256. * to be flagged as 'not present' on the command line.
  257. *
  258. * ENTRY:
  259. * ptm (input)
  260. * Points to 0-terminated TOKMAPA array to scan.
  261. * pToken (input)
  262. * The token to scan for and set flags.
  263. *
  264. * EXIT:
  265. * TRUE if the specified token was found in the TOKMAPA array
  266. * (TMFLAG_PRESENT flag is reset). FALSE otherwise.
  267. *
  268. ****************************************************************************/
  269. BOOLEAN WINAPI
  270. SetTokenNotPresentA( PTOKMAPA ptm,
  271. PCHAR pToken )
  272. {
  273. int i;
  274. for ( i = 0; ptm[i].tmToken; i++ ) {
  275. if ( !strcmp( ptm[i].tmToken, pToken ) ) {
  276. ptm[i].tmFlag &= ~TMFLAG_PRESENT;
  277. return(TRUE);
  278. }
  279. }
  280. return(FALSE);
  281. } // end SetTokenNotPresentA