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.

325 lines
9.0 KiB

  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "cmdline.h"
  6. kCommandLine::kCommandLine()
  7. {
  8. pArgListBegin = (ARGUMENTS *)malloc(sizeof(ARGUMENTS));
  9. if(pArgListBegin) {
  10. pArgListBegin->pNext = NULL;
  11. pArgListBegin->szArg = NULL;
  12. pArgListBegin->sArgumentNumber = 0;
  13. pArgListBegin->pPrevious = NULL;
  14. pArgListCurrent = pArgListBegin;
  15. sNumberOfDrives = 0;
  16. szCommandLine = (char *)malloc((lstrlen(GetCommandLine()) + sizeof(TCHAR)));
  17. if(szCommandLine) {
  18. lstrcpy(szCommandLine, (char *)GetCommandLine());
  19. if (sNumberOfArgs = GetNumberOfArguments()) {
  20. WORD wArgNums = 0;
  21. wArgNums = FillArgumentList();
  22. }
  23. }
  24. }
  25. }
  26. kCommandLine::~kCommandLine()
  27. {
  28. if(szCommandLine)
  29. free (szCommandLine);
  30. Rewind();
  31. if(pArgListCurrent->szArg)
  32. free (pArgListCurrent->szArg);
  33. while (GetNext())
  34. {
  35. if(pArgListCurrent->pPrevious)
  36. free (pArgListCurrent->pPrevious);
  37. if(pArgListCurrent->szArg)
  38. free (pArgListCurrent->szArg);
  39. }
  40. if(pArgListCurrent)
  41. free (pArgListCurrent);
  42. }
  43. void kCommandLine::DebugOutf(char *szFormat, ...)
  44. {
  45. char szBuffer[MAX_PATH * 4];
  46. va_list va;
  47. va_start (va,szFormat);
  48. vsprintf (szBuffer, szFormat, va);
  49. va_end (va);
  50. #ifdef DEBUG
  51. OutputDebugString (szBuffer);
  52. #endif
  53. }
  54. WORD kCommandLine::GetNumberOfArguments()
  55. {
  56. WORD wSpaceCounter = 0;
  57. for (WORD wCounter = 4; szCommandLine[wCounter] !=NULL; wCounter++)
  58. if ( ((szCommandLine[wCounter]=='/') || (szCommandLine[wCounter]=='-')) && szCommandLine[wCounter+1]!=NULL)
  59. wSpaceCounter++;
  60. return wSpaceCounter;
  61. }
  62. WORD kCommandLine::FillArgumentList()
  63. {
  64. WORD wCounter;
  65. WORD TKCounter;
  66. WORD wArgNumCounter = 0;
  67. char szHolder[MAX_PATH * 2];
  68. szHolder[0] = NULL;
  69. wCounter = 4;
  70. for (Rewind(); szCommandLine[wCounter] != NULL; wCounter++)
  71. {
  72. if(szCommandLine[wCounter] == '/')
  73. {
  74. {
  75. WORD TempwCounter;
  76. WORD TempHolderCounter;
  77. TCHAR TempHolder[MAX_PATH];
  78. TempHolderCounter = 0;
  79. TempwCounter=wCounter;
  80. while ( (szCommandLine[TempwCounter] != ' ') && (szCommandLine[TempwCounter] != NULL) )
  81. {
  82. TempHolder[TempHolderCounter] = szCommandLine[TempwCounter];
  83. TempHolderCounter++;
  84. TempwCounter++;
  85. }
  86. TempHolder[TempHolderCounter] = '\0';
  87. if (' ' == TempHolder[strlen(TempHolder) - 1])
  88. {
  89. TempHolder[strlen(TempHolder)-1] = '\0';
  90. }
  91. Add(TempHolder);
  92. wCounter = TempwCounter;
  93. }
  94. while ( (szCommandLine[wCounter] != NULL) && (szCommandLine[wCounter] != ' ') )
  95. {
  96. wCounter++;
  97. }
  98. wCounter++;
  99. wArgNumCounter++;
  100. WORD wTK=0;
  101. for (TKCounter = 0; TKCounter < 256; TKCounter++)
  102. {
  103. szHolder[TKCounter] = '\0';
  104. }
  105. while ( (szCommandLine[wCounter] != '/') && (szCommandLine[wCounter] != NULL) )
  106. {
  107. szHolder[wTK] = szCommandLine[wCounter];
  108. wCounter++;
  109. wTK++;
  110. }
  111. if (szHolder[wTK] == ' ')
  112. {
  113. szHolder[wTK-1] = NULL;
  114. }
  115. if (' ' == szHolder[strlen(szHolder) - 1])
  116. {
  117. szHolder[strlen(szHolder)-1] = '\0';
  118. }
  119. Add(szHolder);
  120. wCounter--;
  121. }
  122. }//end of for loop
  123. #ifdef _DEBUG
  124. DebugOutf("FillArgumentList=%d\r\n",wArgNumCounter);
  125. #endif
  126. return wArgNumCounter;
  127. }
  128. void kCommandLine::Rewind()
  129. {
  130. pArgListCurrent=pArgListBegin;
  131. }
  132. void kCommandLine::Add(char *szArgpass)
  133. {
  134. //MessageBox(GetFocus(), szArgpass, "Add", MB_OK);
  135. FindLast();
  136. if (pArgListCurrent != pArgListBegin)
  137. {
  138. pArgListCurrent->pNext = (ARGUMENTS *)malloc(sizeof ARGUMENTS);
  139. if(!pArgListCurrent->pNext)
  140. return;
  141. pArgListCurrent->pNext->pPrevious = pArgListCurrent;
  142. pArgListCurrent = pArgListCurrent->pNext;
  143. pArgListCurrent->szArg = (char *)malloc(strlen(szArgpass) + 1);
  144. if(!pArgListCurrent->szArg)
  145. return;
  146. strcpy(pArgListCurrent->szArg, szArgpass);
  147. pArgListCurrent->pNext = NULL;
  148. pArgListCurrent->sArgumentNumber = pArgListCurrent->pPrevious->sArgumentNumber + 1;
  149. }
  150. else if (pArgListCurrent==pArgListBegin && !pArgListCurrent->szArg)
  151. {
  152. pArgListCurrent->szArg = (char *)malloc(strlen(szArgpass)+1);
  153. if(!pArgListCurrent->szArg)
  154. return;
  155. strcpy(pArgListCurrent->szArg, szArgpass);
  156. pArgListCurrent->pNext = NULL;
  157. pArgListCurrent->sArgumentNumber = 1;
  158. pArgListCurrent->pPrevious = NULL;
  159. }
  160. else if (pArgListCurrent == pArgListBegin && pArgListCurrent->szArg)
  161. {
  162. pArgListCurrent->pNext = (ARGUMENTS *)malloc(sizeof ARGUMENTS);
  163. if(!pArgListCurrent->pNext)
  164. return;
  165. pArgListCurrent->pNext->pPrevious = pArgListCurrent;
  166. pArgListCurrent = pArgListCurrent->pNext;
  167. pArgListCurrent->szArg = (char *)malloc(strlen(szArgpass)+1);
  168. if(!pArgListCurrent->szArg)
  169. return;
  170. strcpy(pArgListCurrent->szArg, szArgpass);
  171. pArgListCurrent->pNext = NULL;
  172. pArgListCurrent->sArgumentNumber = 2;
  173. }
  174. #ifdef _DEBUG
  175. DebugOutf("Arg[%d]=|%s|",pArgListCurrent->sArgumentNumber, pArgListCurrent->szArg);
  176. DebugOutf(" ArgPass=|%s|\r\n",szArgpass);
  177. #endif
  178. }
  179. void kCommandLine::FindLast()
  180. {
  181. for(Rewind(); pArgListCurrent->pNext; pArgListCurrent=pArgListCurrent->pNext);
  182. }
  183. WORD kCommandLine::GetArgumentNumber(TCHAR *Argument, BOOL CaseInsensitive)
  184. {
  185. TCHAR Temp[MAX_PATH * 4];
  186. if (!Argument)
  187. return 0;
  188. Rewind();
  189. if (CaseInsensitive)
  190. {
  191. _strlwr (Argument);
  192. }
  193. while (pArgListCurrent)
  194. {
  195. if (pArgListCurrent->szArg)
  196. {
  197. lstrcpy(Temp, pArgListCurrent->szArg);
  198. _strlwr(Temp);
  199. if (!lstrcmp(Temp, Argument))
  200. {
  201. return pArgListCurrent->sArgumentNumber;
  202. }
  203. }
  204. pArgListCurrent=pArgListCurrent->pNext;
  205. }
  206. return 0;
  207. }
  208. char *kCommandLine::GetNextArgument()
  209. {
  210. if (pArgListCurrent->pNext)
  211. {
  212. pArgListCurrent = pArgListCurrent->pNext;
  213. return pArgListCurrent->szArg;
  214. }
  215. else
  216. return NULL;
  217. }
  218. char *kCommandLine::GetSwitchValue(char *szArgpass, BOOL bCaseInsensitive)
  219. {
  220. Rewind();
  221. if (bCaseInsensitive)
  222. _strlwr(szArgpass);
  223. if (pArgListCurrent->szArg)
  224. {
  225. if (!strcmp(szArgpass, pArgListCurrent->szArg) && pArgListCurrent->pNext)
  226. {
  227. return pArgListCurrent->pNext->szArg;
  228. }
  229. while (GetNext())
  230. {
  231. if (!strcmp(szArgpass, pArgListCurrent->szArg) && pArgListCurrent->pNext)
  232. return pArgListCurrent->pNext->szArg;
  233. }
  234. }
  235. //fallthrough
  236. return NULL;
  237. }
  238. char *kCommandLine::GetArgumentByNumber(WORD wNumber)
  239. {
  240. Rewind();
  241. if (pArgListCurrent->szArg)
  242. {
  243. if (pArgListCurrent->sArgumentNumber == wNumber)
  244. {
  245. return pArgListCurrent->szArg;
  246. }
  247. while (GetNext())
  248. {
  249. if (pArgListCurrent->sArgumentNumber == wNumber)
  250. {
  251. return pArgListCurrent->szArg;
  252. }
  253. }
  254. }
  255. //fallthrough
  256. return NULL;
  257. }
  258. BOOL kCommandLine::IsSpecified(char *szArgpass, BOOL bCaseInsensitive)
  259. {
  260. char szTemp[MAX_PATH * 4];
  261. Rewind();
  262. if (bCaseInsensitive)
  263. _strlwr(szArgpass);
  264. if (pArgListCurrent->szArg)
  265. {
  266. strcpy(szTemp, pArgListCurrent->szArg);
  267. if (bCaseInsensitive)
  268. _strlwr(szTemp);
  269. //MessageBox(GetFocus(), szArgpass, szTemp, MB_OK);
  270. if (!strcmp(szArgpass, szTemp))
  271. return TRUE;
  272. while (GetNext())
  273. {
  274. strcpy(szTemp, pArgListCurrent->szArg);
  275. if (bCaseInsensitive)
  276. _strlwr(szTemp);
  277. //MessageBox(GetFocus(), szArgpass, szTemp, MB_OK);
  278. if (!strcmp(szArgpass, szTemp))
  279. return TRUE;
  280. }
  281. }
  282. //fallthrough
  283. return FALSE;
  284. }
  285. ARGUMENTS *kCommandLine::GetNext()
  286. {
  287. if (pArgListCurrent->pNext)
  288. pArgListCurrent = pArgListCurrent->pNext;
  289. else
  290. return NULL;
  291. return pArgListCurrent;
  292. }
  293. ARGUMENTS *kCommandLine::GetPrevious()
  294. {
  295. if (pArgListCurrent->pPrevious)
  296. pArgListCurrent = pArgListCurrent->pPrevious;
  297. else
  298. return NULL;
  299. return pArgListCurrent;
  300. }