Source code of Windows XP (NT5)
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.

293 lines
7.2 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. m_cmdLineFlags = 0;
  79. char *lpszRemaining;
  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. m_pszJobFile = (WCHAR *) ALLOC(
  113. ( lstrlen(WSZGUID_IDLESCHEDULE) /* guid of schedule */
  114. + 1 /* space for separator */
  115. + lstrlen(szDomainUserName) /* space for domainUserNmae */
  116. + 1 /* space for NULL */
  117. ) * sizeof(WCHAR) );
  118. lstrcpy(m_pszJobFile,WSZGUID_IDLESCHEDULE);
  119. lstrcat(m_pszJobFile,TEXT("_"));
  120. lstrcat(m_pszJobFile,szDomainUserName);
  121. }
  122. else if (MatchOption(__argv[i],"Schedule=",FALSE,&lpszRemaining))
  123. {
  124. ULONG ul_JobFileSize = 0;
  125. m_cmdLineFlags |= CMDLINE_COMMAND_SCHEDULE;
  126. // command lines are always in ANSI so convert jobname to WCHAR
  127. ul_JobFileSize = lstrlenA(lpszRemaining) + 1;
  128. m_pszJobFile = (WCHAR *) ALLOC(ul_JobFileSize*sizeof(WCHAR));
  129. // UP to Schedule method on invoke to handle if jobfile is null.
  130. if (m_pszJobFile)
  131. {
  132. MultiByteToWideChar(CP_ACP ,0,
  133. lpszRemaining,-1,m_pszJobFile,ul_JobFileSize);
  134. }
  135. }
  136. else
  137. {
  138. AssertSz( i== 0,"Unknown Command Line"); // unkown command line
  139. }
  140. }
  141. }
  142. //+---------------------------------------------------------------------------
  143. //
  144. // Member: CCmdLine::MatchOption, private
  145. //
  146. // Synopsis: given a command line and an option determines if the command
  147. // line matches the option up until the end of the option text
  148. // if there is additional text after the option text a pointer
  149. // to it will be returned in lpRemaining, else lpRemaining wil
  150. // be set to NULL.
  151. //
  152. // !!!CmdLine options always come in in ANSI
  153. //
  154. // Arguments: [lpsz] - command line value to match
  155. // [lpszOption] - Option string to match command line too.
  156. // [fExactMatch] - when true, the command line must contain the same
  157. // number of characters as the Option (i.e.) There shouldn't
  158. // be any remaining characters
  159. // [lpszRemaining] - Points to any remaining part of the command after the match
  160. //
  161. // Returns:
  162. //
  163. // Modifies:
  164. //
  165. // History: 17-Nov-97 rogerg Created.
  166. //
  167. //----------------------------------------------------------------------------
  168. BOOL CCmdLine::MatchOption(LPSTR lpsz, LPSTR lpszOption,BOOL fExactMatch,LPSTR *lpszRemaining)
  169. {
  170. if (lpszRemaining)
  171. *lpszRemaining = '\0';
  172. if (lpsz[0] == TEXT('-') || lpsz[0] == TEXT('/'))
  173. {
  174. int nRet = 0;
  175. lpsz++;
  176. while (! (nRet = toupper(*lpsz)
  177. - toupper(*lpszOption)) &&
  178. *lpsz)
  179. {
  180. lpsz++;
  181. lpszOption++;
  182. }
  183. if (*lpszOption || (*lpsz && fExactMatch) )
  184. return FALSE;
  185. if (lpszRemaining)
  186. *lpszRemaining = lpsz;
  187. return(TRUE);
  188. }
  189. return FALSE;
  190. }
  191. //+---------------------------------------------------------------------------
  192. //
  193. // Member: CCmdLine::MatchOption, private
  194. //
  195. // Synopsis: given a command line and an option determines if the command
  196. // line matches exactly matches the option
  197. //
  198. // Arguments: [lpsz] - command line value to match
  199. // [lpszOption] - Option string to match command line too.
  200. //
  201. // Returns:
  202. //
  203. // Modifies:
  204. //
  205. // History: 17-Nov-97 rogerg Created.
  206. //
  207. //----------------------------------------------------------------------------
  208. BOOL CCmdLine::MatchOption(LPSTR lpsz, LPSTR lpszOption)
  209. {
  210. return MatchOption(lpsz,lpszOption,TRUE /* fExactmatch */ ,NULL);
  211. }
  212. //+---------------------------------------------------------------------------
  213. //
  214. // Member: CCmdLine::strnicmp, private
  215. //
  216. // Synopsis: internal implimentation of strnicmp so don't need
  217. // the runtime.
  218. //
  219. // Arguments: [lpsz] - command line value to match
  220. // [lpszOption] - Option string to match command line too.
  221. // [count] - number of characters to compare.
  222. //
  223. // Returns: standard strnicmp values.
  224. //
  225. // Modifies:
  226. //
  227. // History: 17-Nov-97 rogerg Created.
  228. //
  229. //----------------------------------------------------------------------------
  230. int CCmdLine::strnicmp(LPWSTR lpsz, LPWSTR lpszOption,size_t count)
  231. {
  232. int nRet = 0;
  233. while (count-- &&
  234. !(nRet = toupper(*lpsz)
  235. - toupper(*lpszOption)) &&
  236. *lpsz)
  237. {
  238. lpsz++;
  239. lpszOption++;
  240. }
  241. return nRet;
  242. }