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.

234 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. wargs.c
  5. Abstract:
  6. Routines to process unicode command line arguments
  7. into argc and argv.
  8. Author:
  9. Ted Miller (tedm) 16-June-1993
  10. Revision History:
  11. --*/
  12. #include <windows.h>
  13. #include <wargs.h>
  14. #include <wchar.h>
  15. LPWSTR
  16. _NextToken(
  17. IN OUT LPWSTR *CurrentPosition
  18. )
  19. /*++
  20. Routine Description:
  21. Get the next token from the command line.
  22. Arguments:
  23. argcW - receives the number of arguments.
  24. argvW - receives pointer to array of wide char strings.
  25. Return Value:
  26. TRUE if the command line was parsed and stored successfully.
  27. FALSE if not.
  28. --*/
  29. {
  30. BOOL InQuote;
  31. LPWSTR Start;
  32. UINT Length;
  33. LPWSTR p;
  34. LPWSTR Token;
  35. //
  36. // Skip leading whitespace.
  37. //
  38. Start = *CurrentPosition;
  39. while(*Start && iswspace(*Start)) {
  40. Start++;
  41. }
  42. //
  43. // If first char is a quote, skip it.
  44. //
  45. if(*Start == '\"') {
  46. InQuote = TRUE;
  47. Start++;
  48. } else {
  49. InQuote = FALSE;
  50. }
  51. //
  52. // Scan until we find the end of the token.
  53. //
  54. p = Start;
  55. while(*p) {
  56. if(iswspace(*p) && !InQuote) {
  57. break;
  58. }
  59. if((*p == '\"') && InQuote) {
  60. p++;
  61. break;
  62. }
  63. p++;
  64. }
  65. //
  66. // p is the first character that is not part of the token.
  67. //
  68. Length = (UINT)(p-Start);
  69. if(InQuote) {
  70. Length--; // compensate for terminating quote.
  71. }
  72. //
  73. // Skip past trailing whitespace.
  74. //
  75. while(*p && iswspace(*p)) {
  76. p++;
  77. }
  78. //
  79. // Copy the token.
  80. //
  81. if(Token = LocalAlloc(LPTR,(Length+1)*sizeof(WCHAR))) {
  82. CopyMemory(Token,Start,Length*sizeof(WCHAR));
  83. }
  84. *CurrentPosition = p;
  85. return(Token);
  86. }
  87. BOOL
  88. InitializeUnicodeArguments(
  89. OUT int *argcW,
  90. OUT PWCHAR **argvW
  91. )
  92. /*++
  93. Routine Description:
  94. Fetch the unicode command line and process it into argc/argv-like
  95. global variables.
  96. Arguments:
  97. argcW - receives the number of arguments.
  98. argvW - receives pointer to array of wide char strings.
  99. Return Value:
  100. TRUE if the command line was parsed and stored successfully.
  101. FALSE if not.
  102. --*/
  103. {
  104. LPWSTR CommandLine;
  105. LPWSTR CurrentPosition;
  106. int ArgCount;
  107. LPWSTR Arg;
  108. PWCHAR *Args=NULL;
  109. PWCHAR *TmpArgs;
  110. CommandLine = GetCommandLineW();
  111. CurrentPosition=CommandLine;
  112. ArgCount = 0;
  113. while(*CurrentPosition) {
  114. Arg = _NextToken(&CurrentPosition);
  115. if(Arg) {
  116. if(Args) {
  117. TmpArgs = LocalReAlloc((HLOCAL)Args,(ArgCount+1)*sizeof(PWCHAR),LMEM_MOVEABLE);
  118. if ( TmpArgs )
  119. Args = TmpArgs;
  120. else {
  121. LocalFree(Args);
  122. Args = NULL;
  123. }
  124. } else {
  125. Args = LocalAlloc(LPTR,sizeof(PWCHAR));
  126. }
  127. if(Args == NULL) {
  128. return(FALSE);
  129. }
  130. Args[ArgCount++] = Arg;
  131. } else {
  132. return(FALSE);
  133. }
  134. }
  135. *argcW = ArgCount;
  136. *argvW = Args;
  137. return(TRUE);
  138. }
  139. VOID
  140. FreeUnicodeArguments(
  141. IN int argcW,
  142. IN PWCHAR *argvW
  143. )
  144. /*++
  145. Routine Description:
  146. Free any resources used by the global unicode argc/argv.
  147. Arguments:
  148. None.
  149. Return Value:
  150. TRUE if the command line was parsed and stored successfully.
  151. The global variables argcW and argvW will be filled in.
  152. --*/
  153. {
  154. int i;
  155. for(i=0; i<argcW; i++) {
  156. if(argvW[i]) {
  157. LocalFree((HLOCAL)argvW[i]);
  158. }
  159. }
  160. LocalFree((HLOCAL)argvW);
  161. }