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.

243 lines
7.0 KiB

  1. //+-------------------------------------------------------------------
  2. // Microsoft OLE
  3. // Copyright (C) Microsoft Corporation, 1994-1995.
  4. //
  5. // File: arghelp.cxx
  6. //
  7. // Contents: Helper functions for manipulating & parsing command
  8. // line arguments (both Windows and Command Line styles).
  9. //
  10. // Classes: None
  11. //
  12. // History: 22-Nov-94 DeanE Created
  13. //---------------------------------------------------------------------
  14. #include <dfheader.hxx>
  15. #pragma hdrstop
  16. #ifdef _MAC
  17. //
  18. // On the mac, default to win95, since
  19. // we're not going to do any tests that
  20. // we don't do on win95
  21. //
  22. DWORD g_dwOperatingSystem = OS_WIN95 ;
  23. #else //!_MAC
  24. DWORD g_dwOperatingSystem = OS_NT ;
  25. #endif //_MAC
  26. #define CCH_MAX_MODULE_NAME 250
  27. //+---------------------------------------------------------------------
  28. // Macro: FindNextToken - looks for next non-space char and
  29. // writes NILs over spaces it finds
  30. // FindNextSpace - looks for next space char
  31. // FindNextNil - looks for next NIl before tail, does a
  32. // continue instead of a break at tail to
  33. // allow loop to increment
  34. // FindNextNonNil - looks for next non-NIl before tail
  35. //
  36. // Synopsis: Helper macros for walking strings. Walks pointer to
  37. // desired point in string.
  38. //
  39. // History: 28-Mar-94 DarrylA Created.
  40. //----------------------------------------------------------------------
  41. #define FindNextToken(ptr) \
  42. while('\0' != *(ptr)&&' '==(*(ptr))) { *(ptr) = '\0'; ++(ptr); } \
  43. if('\0' == *(ptr)) break
  44. #define FindNextSpace(ptr) \
  45. while('\0' != *(ptr)&&' '!=(*(ptr))) ++(ptr); \
  46. if('\0' == *(ptr)) break
  47. #define FindNextNil(ptr, tail) \
  48. while('\0' != *(ptr)&&(ptr)<(tail)) ++(ptr); \
  49. if((tail) == (ptr)) continue
  50. #define FindNextNonNil(ptr, tail) \
  51. while('\0' == *(ptr)&&(ptr)<(tail)) ++(ptr); \
  52. if((tail) == (ptr)) break
  53. //+-------------------------------------------------------------------
  54. // Function: CmdlineToArgs
  55. //
  56. // Synopsis: Turns the Windows-style Command Line passed into argc/
  57. // argv-style arguments.
  58. //
  59. // Arguments: [paszCmdline] - Windows-style ANSI command line.
  60. // [pargc] - Pointer to resulting argc.
  61. // [pargv] - Pointer to resulting argv.
  62. //
  63. // Returns: S_OK if no problems, error code otherwise.
  64. //
  65. // History: 05-Apr-94 DarrylA Created.
  66. // 22-Nov-94 DeanE Stolen from Marshal tests
  67. //--------------------------------------------------------------------
  68. HRESULT CmdlineToArgs(
  69. LPSTR paszCmdline,
  70. PINT pargc,
  71. CHAR ***pargv)
  72. {
  73. // DH_ASSERT(!IsBadWritePtr(pargc, sizeof(PINT)));
  74. // DH_ASSERT(!IsBadWritePtr(pargv, sizeof(CHAR ***)));
  75. int cArgs = 1;
  76. int cchTemp = 0;
  77. ULONG cchCmdline = 0;
  78. CHAR **ppArgs = NULL;
  79. PCHAR ptail = NULL;
  80. LPSTR aszCmdline = NULL;
  81. PCHAR ptr = NULL;
  82. // Copy command line string into an ANSI buffer
  83. cchCmdline = (ULONG) strlen(paszCmdline);
  84. aszCmdline = new(NullOnFail) CHAR[cchCmdline+1];
  85. if (aszCmdline == NULL)
  86. {
  87. return(E_OUTOFMEMORY);
  88. }
  89. strcpy(aszCmdline, paszCmdline);
  90. cchTemp = (int) cchCmdline;
  91. ptr = aszCmdline;
  92. // The command line is now in the ansi buffer. Now we need to traverse
  93. // it and figure out the number of parameters. While we walk over
  94. // spaces, we will replace them with '\0' so that afterwards, we just
  95. // dup each string into the new array.
  96. //
  97. while('\0' != *ptr)
  98. {
  99. FindNextToken(ptr);
  100. ++cArgs;
  101. FindNextSpace(ptr);
  102. }
  103. ptail = ptr; // now points to NIL at end of string
  104. ptr = aszCmdline;
  105. // Now we need to allocate space for the arguments
  106. ppArgs = new(NullOnFail) LPSTR[cArgs];
  107. if (NULL == ppArgs)
  108. {
  109. delete aszCmdline;
  110. return(E_OUTOFMEMORY);
  111. }
  112. BOOL fNewFail = FALSE;
  113. int i = 0; // init to zero in case the strdup fails
  114. // Initialize ppArgs[0] with the module name
  115. ppArgs[0] = new(NullOnFail) CHAR[CCH_MAX_MODULE_NAME];
  116. if (NULL == ppArgs[0])
  117. {
  118. delete aszCmdline;
  119. delete ppArgs;
  120. return(E_OUTOFMEMORY);
  121. }
  122. char szTempModule[CCH_MAX_MODULE_NAME];
  123. short ret;
  124. cchTemp = (int) GetModuleFileNameA(NULL, szTempModule, CCH_MAX_MODULE_NAME);
  125. ret = GetFileTitleA(szTempModule, ppArgs[0], CCH_MAX_MODULE_NAME);
  126. if (ret != 0)
  127. {
  128. cchTemp = 0;
  129. }
  130. else
  131. {
  132. cchTemp = strlen(ppArgs[0]);
  133. }
  134. if ((cchTemp == 0) || (cchTemp == CCH_MAX_MODULE_NAME))
  135. {
  136. delete aszCmdline;
  137. delete ppArgs[0];
  138. delete ppArgs;
  139. return(E_FAIL);
  140. }
  141. // Now traverse the command line, plucking arguments and copying them
  142. // into the ppArgs array
  143. //
  144. for(i=1; i<cArgs; i++)
  145. {
  146. FindNextNonNil(ptr, ptail);
  147. ppArgs[i] = new(NullOnFail) CHAR[strlen(ptr)+1];
  148. if (NULL == ppArgs[i])
  149. {
  150. fNewFail = TRUE;
  151. break;
  152. }
  153. else
  154. {
  155. strcpy(ppArgs[i], ptr);
  156. }
  157. FindNextNil(ptr, ptail);
  158. }
  159. // Check for errors - clean up if we got one
  160. if (i != cArgs || TRUE == fNewFail)
  161. {
  162. for (int j=0; j<i; j++)
  163. {
  164. delete ppArgs[j];
  165. }
  166. delete aszCmdline;
  167. delete ppArgs;
  168. return(E_OUTOFMEMORY);
  169. }
  170. // Set up return parameters
  171. *pargc = cArgs;
  172. *pargv = ppArgs;
  173. // Clean up and exit
  174. delete aszCmdline;
  175. return(S_OK);
  176. }
  177. //+---------------------------------------------------------------------------
  178. //
  179. // Function: GetOSFromCmdline
  180. //
  181. // Synopsis: The operating system can be specified by putting /OS:<os>
  182. // on the command line. Check for it.
  183. //
  184. // Parameters: [pCmdLine] -- The command object for /OS
  185. //
  186. // Returns: OS_NT -- Running on NT
  187. // OS_WIN95 -- Running on Win95
  188. // OS_WIN95_DCOM -- Running on Win95 + DCOM
  189. //
  190. // History: 06-Jun-96 AlexE Created
  191. //
  192. //----------------------------------------------------------------------------
  193. DWORD GetOSFromCmdline(CBaseCmdlineObj *pCmdLine)
  194. {
  195. //
  196. // If there is an OS specifier on the command line, set
  197. // the global variable g_dwOperatingSystem to the right
  198. // thing -- it is set to NT by default during compile time.
  199. //
  200. if (pCmdLine->IsFound())
  201. {
  202. if (0 == _olestricmp(pCmdLine->GetValue(), OS_STRING_WIN95))
  203. {
  204. g_dwOperatingSystem = OS_WIN95 ;
  205. }
  206. else if (0 == _olestricmp(pCmdLine->GetValue(), OS_STRING_WIN95DCOM))
  207. {
  208. g_dwOperatingSystem = OS_WIN95_DCOM ;
  209. }
  210. }
  211. return g_dwOperatingSystem ;
  212. }