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.

246 lines
6.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: cmdline.cpp
  7. //
  8. // Contents: commandline helper routines.
  9. //
  10. // Classes:
  11. //
  12. // Notes:
  13. //
  14. // History: 17-Nov-97 rogerg Created.
  15. //
  16. //--------------------------------------------------------------------------
  17. #include "precomp.h"
  18. //+---------------------------------------------------------------------------
  19. //
  20. // Member: CCmdLine::CCmdLine, public
  21. //
  22. // Synopsis: Constructor.
  23. //
  24. // Arguments:
  25. //
  26. // Returns:
  27. //
  28. // Modifies:
  29. //
  30. // History: 17-Nov-97 rogerg Created.
  31. //
  32. //----------------------------------------------------------------------------
  33. CCmdLine::CCmdLine()
  34. {
  35. m_cmdLineFlags = 0;
  36. m_pszJobFile = NULL;
  37. }
  38. //+---------------------------------------------------------------------------
  39. //
  40. // Member: CCmdLine::~CCmdLine, public
  41. //
  42. // Synopsis: destructor.
  43. //
  44. // Arguments:
  45. //
  46. // Returns:
  47. //
  48. // Modifies:
  49. //
  50. // History: 07-Jul-98 rogerg Created.
  51. //
  52. //----------------------------------------------------------------------------
  53. CCmdLine::~CCmdLine()
  54. {
  55. if (m_pszJobFile)
  56. {
  57. FREE(m_pszJobFile);
  58. }
  59. }
  60. //+---------------------------------------------------------------------------
  61. //
  62. // Member: CCmdLine::ParseCommandLine, public
  63. //
  64. // Synopsis: Parses the command line passed to the Application
  65. // and Sets the member variables accordingly.
  66. //
  67. // Arguments:
  68. //
  69. // Returns:
  70. //
  71. // Modifies:
  72. //
  73. // History: 17-Nov-97 rogerg Created.
  74. //
  75. //----------------------------------------------------------------------------
  76. void CCmdLine::ParseCommandLine()
  77. {
  78. char *lpszRemaining;
  79. m_cmdLineFlags = 0;
  80. // start at 1 -- the first is the exe
  81. for (int i=1; i< __argc; i++)
  82. {
  83. if (MatchOption(__argv[i], "Embedding"))
  84. {
  85. m_cmdLineFlags |= CMDLINE_COMMAND_EMBEDDING;
  86. }
  87. else if (MatchOption(__argv[i],"Register"))
  88. {
  89. m_cmdLineFlags |= CMDLINE_COMMAND_REGISTER;
  90. }
  91. else if (MatchOption(__argv[i], "logon") )
  92. {
  93. m_cmdLineFlags |= CMDLINE_COMMAND_LOGON;
  94. }
  95. else if (MatchOption(__argv[i],"logoff") )
  96. {
  97. m_cmdLineFlags |= CMDLINE_COMMAND_LOGOFF;
  98. }
  99. else if (MatchOption(__argv[i],"DllRegisterServer") )
  100. {
  101. m_cmdLineFlags |= CMDLINE_COMMAND_REGISTER;
  102. }
  103. else if (MatchOption(__argv[i],"Idle") )
  104. {
  105. // pretend idle is the idle schedule firing for this
  106. // user so same code path is exceriside if command
  107. // line invoked or TS launched us.
  108. m_cmdLineFlags |= CMDLINE_COMMAND_SCHEDULE;
  109. // m_pszJobFile will be scheduleguid_UserName
  110. TCHAR szDomainUserName[MAX_DOMANDANDMACHINENAMESIZE];
  111. GetDefaultDomainAndUserName(szDomainUserName,TEXT("_"), MAX_DOMANDANDMACHINENAMESIZE);
  112. DWORD dwJobFileLen = ( lstrlen(WSZGUID_IDLESCHEDULE) /* guid of schedule */
  113. + 1 /* space for separator */
  114. + lstrlen(szDomainUserName) /* space for domainUserNmae */
  115. + 1 /* space for NULL */
  116. );
  117. m_pszJobFile = (WCHAR *) ALLOC(dwJobFileLen * sizeof(WCHAR));
  118. if (m_pszJobFile)
  119. {
  120. StringCchCopy(m_pszJobFile, dwJobFileLen, WSZGUID_IDLESCHEDULE);
  121. StringCchCat(m_pszJobFile, dwJobFileLen, TEXT("_"));
  122. StringCchCat(m_pszJobFile, dwJobFileLen, szDomainUserName);
  123. }
  124. }
  125. else if (MatchOption(__argv[i], "Schedule=", FALSE, &lpszRemaining))
  126. {
  127. m_cmdLineFlags |= CMDLINE_COMMAND_SCHEDULE;
  128. // command lines are always in ANSI so convert jobname to WCHAR
  129. ULONG ulJobFileSize = lstrlenA(lpszRemaining) + 1;
  130. m_pszJobFile = (WCHAR *) ALLOC(ulJobFileSize*sizeof(WCHAR));
  131. // UP to Schedule method on invoke to handle if jobfile is null.
  132. if (m_pszJobFile)
  133. {
  134. MultiByteToWideChar(CP_ACP, 0, lpszRemaining, -1, m_pszJobFile, ulJobFileSize);
  135. }
  136. }
  137. else
  138. {
  139. AssertSz( i== 0, "Unknown Command Line"); // unknown command line
  140. }
  141. }
  142. }
  143. //+---------------------------------------------------------------------------
  144. //
  145. // Member: CCmdLine::MatchOption, private
  146. //
  147. // Synopsis: given a command line and an option determines if the command
  148. // line matches the option up until the end of the option text
  149. // if there is additional text after the option text a pointer
  150. // to it will be returned in lpRemaining, else lpRemaining wil
  151. // be set to NULL.
  152. //
  153. // !!!CmdLine options always come in in ANSI
  154. //
  155. // Arguments: [lpsz] - command line value to match
  156. // [lpszOption] - Option string to match command line too.
  157. // [fExactMatch] - when true, the command line must contain the same
  158. // number of characters as the Option (i.e.) There shouldn't
  159. // be any remaining characters
  160. // [lpszRemaining] - Points to any remaining part of the command after the match
  161. //
  162. // Returns:
  163. //
  164. // Modifies:
  165. //
  166. // History: 17-Nov-97 rogerg Created.
  167. //
  168. //----------------------------------------------------------------------------
  169. BOOL CCmdLine::MatchOption(LPSTR lpsz, LPSTR lpszOption,BOOL fExactMatch,LPSTR *lpszRemaining)
  170. {
  171. if (lpszRemaining)
  172. *lpszRemaining = '\0';
  173. if ( lpsz && (lpsz[0] == TEXT('-') || lpsz[0] == TEXT('/')) )
  174. {
  175. int nRet = 0;
  176. lpsz++;
  177. while (! (nRet = toupper(*lpsz) - toupper(*lpszOption)) && *lpsz)
  178. {
  179. lpsz++;
  180. lpszOption++;
  181. }
  182. if (*lpszOption || (*lpsz && fExactMatch) )
  183. return FALSE;
  184. if (lpszRemaining)
  185. *lpszRemaining = lpsz;
  186. return(TRUE);
  187. }
  188. return FALSE;
  189. }
  190. //+---------------------------------------------------------------------------
  191. //
  192. // Member: CCmdLine::MatchOption, private
  193. //
  194. // Synopsis: given a command line and an option determines if the command
  195. // line matches exactly matches the option
  196. //
  197. // Arguments: [lpsz] - command line value to match
  198. // [lpszOption] - Option string to match command line too.
  199. //
  200. // Returns:
  201. //
  202. // Modifies:
  203. //
  204. // History: 17-Nov-97 rogerg Created.
  205. //
  206. //----------------------------------------------------------------------------
  207. BOOL CCmdLine::MatchOption(LPSTR lpsz, LPSTR lpszOption)
  208. {
  209. return MatchOption(lpsz, lpszOption, TRUE /* fExactmatch */, NULL);
  210. }