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.

351 lines
7.3 KiB

  1. // Sink.cpp : Implementation of CRoutingSinkApp and DLL registration.
  2. #include "stdinc.h"
  3. ////////////////////////////////////////////////////////////////////////////
  4. // Method: CCmdInfo()
  5. // Member of:
  6. // Arguments:
  7. // Returns:
  8. // Description:
  9. ////////////////////////////////////////////////////////////////////////////
  10. CCmdInfo::CCmdInfo(LPSTR szCmd)
  11. {
  12. nArgNo = 0;
  13. pArgs = NULL;
  14. ZeroMemory(szDefTag, sizeof(szDefTag));
  15. ZeroMemory(szCmdKey, sizeof(szCmdKey));
  16. ParseLine(szCmd, this);
  17. }
  18. ////////////////////////////////////////////////////////////////////////////
  19. // Method: ~CCmdInfo()
  20. // Member of:
  21. // Arguments:
  22. // Returns:
  23. // Description:
  24. ////////////////////////////////////////////////////////////////////////////
  25. CCmdInfo::~CCmdInfo()
  26. {
  27. while(NULL != pArgs)
  28. {
  29. CArgList *tmp = pArgs->pNext;
  30. delete pArgs;
  31. pArgs = tmp;
  32. }
  33. }
  34. void CCmdInfo::SetDefTag(LPSTR szTag)
  35. {
  36. if(szTag && szTag[0])
  37. lstrcpy(szDefTag, szTag);
  38. for(CArgList *p = pArgs; NULL != p; p = p->pNext)
  39. {
  40. // set the tag to the default value if not already set
  41. if(p->szTag[0] == 0 && szDefTag[0] != 0)
  42. lstrcpy(p->szTag, szDefTag);
  43. }
  44. }
  45. ////////////////////////////////////////////////////////////////////////////
  46. // Method: GetValue()
  47. // Member of:
  48. // Arguments:
  49. // Returns:
  50. // Description:
  51. ////////////////////////////////////////////////////////////////////////////
  52. HRESULT CCmdInfo::GetValue(LPSTR szTag, LPSTR szVal)
  53. {
  54. HRESULT hr = E_FAIL;
  55. if(NULL != szTag)
  56. {
  57. // set the search parameters
  58. pSearchPos = pArgs;
  59. lstrcpy(szSearchTag, szTag);
  60. }
  61. for(CArgList *p = pSearchPos; NULL != p; p = p->pNext)
  62. {
  63. if(!lstrcmpi(p->szTag, szSearchTag))
  64. {
  65. if(NULL != szVal)
  66. lstrcpy(szVal, p->szVal);
  67. hr = S_OK;
  68. pSearchPos = p->pNext;
  69. break;
  70. }
  71. }
  72. return hr;
  73. }
  74. ////////////////////////////////////////////////////////////////////////////
  75. // Method: GetValue()
  76. // Member of:
  77. // Arguments:
  78. // Returns:
  79. // Description:
  80. ////////////////////////////////////////////////////////////////////////////
  81. HRESULT CCmdInfo::AllocValue(LPSTR szTag, LPSTR* pszVal)
  82. {
  83. HRESULT hr = E_FAIL;
  84. if(NULL != szTag)
  85. {
  86. // set the search parameters
  87. pSearchPos = pArgs;
  88. lstrcpy(szSearchTag, szTag);
  89. }
  90. for(CArgList *p = pSearchPos; NULL != p; p = p->pNext)
  91. {
  92. if(!lstrcmpi(p->szTag, szSearchTag))
  93. {
  94. if(NULL != (*pszVal))
  95. delete [] (*pszVal);
  96. (*pszVal) = new char[lstrlen(p->szVal) + 1];
  97. if(NULL == (*pszVal))
  98. {
  99. hr = E_OUTOFMEMORY;
  100. }
  101. else
  102. {
  103. lstrcpy((*pszVal), p->szVal);
  104. hr = S_OK;
  105. pSearchPos = p->pNext;
  106. }
  107. break;
  108. }
  109. }
  110. return hr;
  111. }
  112. ////////////////////////////////////////////////////////////////////////////
  113. // Method: ParseLine()
  114. // Member of:
  115. // Arguments:
  116. // Returns:
  117. // Description:
  118. ////////////////////////////////////////////////////////////////////////////
  119. HRESULT CCmdInfo::ParseLine(LPSTR szCmd, CCmdInfo *pCmd)
  120. {
  121. HRESULT hr = E_FAIL;
  122. // quick hack for keeping info on quoted strings
  123. unsigned nQStart[64];
  124. unsigned nQEnd[64];
  125. unsigned nQIdx = 0;
  126. BOOL fInQ = FALSE;
  127. unsigned nFirstEqualPos = 0;
  128. char *s, *token;
  129. ZeroMemory(&nQStart, sizeof(nQStart));
  130. ZeroMemory(&nQEnd, sizeof(nQEnd));
  131. // find out where the arguments begin
  132. for(s = szCmd; !isspace(*s); s++);
  133. int nPref = (int) (s - szCmd);
  134. // scan the buffer for quoted strings
  135. for(s = szCmd; *s; s++)
  136. {
  137. if(*s == '"')
  138. {
  139. if(fInQ)
  140. {
  141. nQEnd[nQIdx++] = (unsigned) (s - szCmd - nPref);
  142. fInQ = FALSE;
  143. }
  144. else
  145. {
  146. nQStart[nQIdx] = (unsigned) (s - szCmd - nPref);
  147. fInQ = TRUE;
  148. }
  149. }
  150. }
  151. // get the position of the first equal sign
  152. s = strchr(szCmd, '=');
  153. nFirstEqualPos = (unsigned) (s - szCmd - nPref);
  154. // get the command code
  155. token = strtok(szCmd, " ");
  156. if(NULL == token)
  157. {
  158. pCmd->szCmdKey[0] = 0;
  159. goto Exit;
  160. }
  161. else
  162. lstrcpy(pCmd->szCmdKey, token);
  163. // we have a partial command. return S_OK
  164. hr = S_OK;
  165. // build the argument list
  166. do
  167. {
  168. char *en, *mid;
  169. char buf[1024];
  170. ZeroMemory(buf, sizeof(buf));
  171. char *token = NULL;
  172. BOOL fInQ;
  173. do
  174. {
  175. fInQ = FALSE;
  176. token = strtok(NULL, ",");
  177. if(NULL == token)
  178. break;
  179. lstrcat(buf, token);
  180. // if ',' is in a quoted string concatenate to buf and continue
  181. for(unsigned i = 0; i < nQIdx; i++)
  182. {
  183. unsigned nAux = (unsigned) (token - szCmd + lstrlen(token) - nPref);
  184. if(nAux > nQStart[i] && nAux < nQEnd[i])
  185. {
  186. lstrcat(buf, ",");
  187. fInQ = TRUE;
  188. break;
  189. }
  190. }
  191. }
  192. while(fInQ);
  193. if(buf[0] == '\0')
  194. break;
  195. else
  196. token = (LPSTR)buf;
  197. // strip spaces
  198. for(; isspace(*token); token++);
  199. // check if there's anything left
  200. if(!(*token))
  201. continue;
  202. for(en = token; *en; en++);
  203. for(--en; isspace(*en); en--);
  204. // check if there's anything left
  205. if(token > en)
  206. continue;
  207. // allocate a pair object
  208. CCmdInfo::CArgList *tmp = new CCmdInfo::CArgList;
  209. if(NULL == tmp)
  210. {
  211. hr = E_OUTOFMEMORY;
  212. goto Exit;
  213. }
  214. // insert into list
  215. tmp->pNext = pCmd->pArgs;
  216. pCmd->pArgs = tmp;
  217. // set the no. of pairs
  218. pCmd->nArgNo++;
  219. // set the values
  220. // if first '=' is in quoted string, treat whole expression as
  221. // untagged value.
  222. fInQ = FALSE;
  223. for(unsigned i = 0; i < nQIdx; i++)
  224. {
  225. if(nFirstEqualPos > nQStart[i] && nFirstEqualPos < nQEnd[i])
  226. {
  227. fInQ = TRUE;
  228. break;
  229. }
  230. }
  231. mid = fInQ ? NULL : strchr(token, '=');
  232. if(NULL == mid)
  233. {
  234. // this is not a pair. Treating as un-named value.
  235. // remove the quotes around the value
  236. if(token[0] == '"' && token[en - token] == '"')
  237. {
  238. token++;
  239. en--;
  240. }
  241. tmp->SetVal(token, (unsigned) (en - token + 1));
  242. }
  243. else
  244. {
  245. // set the tag
  246. for(char *t = mid - 1; isspace(*t); t--);
  247. // check if we have a tag (might be something like "..., = value"
  248. if(t - token + 1 > 0)
  249. CopyMemory(tmp->szTag, token, t - token + 1);
  250. // set the value
  251. for(t = mid + 1; isspace(*t) && t < en; t++);
  252. // remove the quotes around the value
  253. if(t[0] == '"' && t[en - t] == '"')
  254. {
  255. t++;
  256. en--;
  257. }
  258. tmp->SetVal(t, (unsigned) (en - t + 1));
  259. }
  260. }while(TRUE);
  261. Exit:
  262. return hr;
  263. }
  264. ////////////////////////////////////////////////////////////////////////////
  265. // Method: StringToHRES()
  266. // Member of:
  267. // Arguments:
  268. // Returns:
  269. // Description:
  270. ////////////////////////////////////////////////////////////////////////////
  271. HRESULT CCmdInfo::StringToHRES(LPSTR szVal, HRESULT *phrRes)
  272. {
  273. HRESULT hr = S_OK;
  274. if(isdigit(*szVal))
  275. {
  276. DWORD hr;
  277. int n;
  278. if(*szVal == '0' && tolower(*(szVal+1)) == 'x' && isxdigit(*(szVal+2)))
  279. // read as hex number
  280. n = sscanf(szVal+2, "%lx", &hr);
  281. else
  282. // read as dec number
  283. n = sscanf(szVal, "%lu", &hr);
  284. if(n == 1)
  285. (*phrRes) = (HRESULT)hr;
  286. }
  287. else if(isalpha(*szVal))
  288. {
  289. // see if this a HRESULT code
  290. if(!lstrcmp(szVal, "S_OK")) (*phrRes) = S_OK;
  291. else if(!lstrcmp(szVal, "S_FALSE")) (*phrRes) = S_FALSE;
  292. else if(!lstrcmp(szVal, "E_FAIL")) (*phrRes) = E_FAIL;
  293. else if(!lstrcmp(szVal, "E_OUTOFMEMORY")) (*phrRes) = E_OUTOFMEMORY;
  294. else
  295. hr = S_FALSE;
  296. }
  297. else
  298. hr = S_FALSE;
  299. return hr;
  300. }