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.

459 lines
8.4 KiB

  1. //
  2. // MODULE: APGTSHDT.CPP
  3. //
  4. // PURPOSE: Methods for the various commands (classes). Commands are
  5. // Resposable for executing themseleves.
  6. //
  7. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  8. //
  9. // COMPANY: Saltmine Creative, Inc. (206)-633-4743 [email protected]
  10. //
  11. // AUTHOR: Roman Mach
  12. //
  13. // ORIGINAL DATE: 8-2-96
  14. //
  15. // NOTES:
  16. // 1. Based on Print Troubleshooter DLL
  17. //
  18. // Version Date By Comments
  19. //--------------------------------------------------------------------
  20. // V0.1 - RM Original
  21. // V0.15 8/15/96 VM New htx format
  22. // V0.2 6/4/97 RWM Local Version for Memphis
  23. // V0.3 04/09/98 JM/OK+ Local Version for NT5
  24. //
  25. #include "stdafx.h"
  26. //#include <windows.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <search.h>
  32. #include <dos.h>
  33. #include <ctype.h>
  34. #include <limits.h>
  35. #include <time.h>
  36. #include "apgts.h"
  37. #include "bnts.h"
  38. #include "BackupInfo.h"
  39. #include "cachegen.h"
  40. #include "apgtsinf.h"
  41. #include "apgtscmd.h"
  42. #include "apgtshtx.h"
  43. #include "ErrorEnums.h"
  44. #include "chmread.h"
  45. //
  46. //
  47. HTXCommand::HTXCommand(UINT type, const TCHAR *idstr)
  48. {
  49. m_beforehtmlstr = NULL;
  50. m_afterhtmlstr = NULL;
  51. m_beforesize = 0;
  52. m_aftersize = 0;
  53. m_start = 0;
  54. m_end = 0;
  55. m_type = type;
  56. m_idstr = idstr;
  57. m_error = FALSE;
  58. m_next = NULL;
  59. }
  60. //
  61. //
  62. HTXCommand::~HTXCommand()
  63. {
  64. if (m_beforehtmlstr != NULL)
  65. free(m_beforehtmlstr);
  66. if (m_afterhtmlstr != NULL)
  67. free(m_afterhtmlstr);
  68. }
  69. //
  70. //
  71. void HTXCommand::SetStart(UINT pos)
  72. {
  73. m_start = pos;
  74. }
  75. //
  76. //
  77. void HTXCommand::SetEnd(UINT pos)
  78. {
  79. m_end = pos;
  80. }
  81. //
  82. //
  83. UINT HTXCommand::GetStart()
  84. {
  85. return (m_start);
  86. }
  87. //
  88. //
  89. UINT HTXCommand::GetEnd()
  90. {
  91. return (m_end);
  92. }
  93. //
  94. //
  95. UINT HTXCommand::GetType()
  96. {
  97. return (m_type);
  98. }
  99. //
  100. //
  101. UINT HTXCommand::GetStatus()
  102. {
  103. return (m_error);
  104. }
  105. //
  106. //
  107. const TCHAR *HTXCommand::GetIDStr()
  108. {
  109. return (m_idstr);
  110. }
  111. //
  112. //
  113. UINT HTXCommand::GetBeforeLen()
  114. {
  115. return (m_beforelen);
  116. }
  117. //
  118. //
  119. UINT HTXCommand::GetAfterLen()
  120. {
  121. return (m_afterlen);
  122. }
  123. //
  124. //
  125. TCHAR *HTXCommand::GetBeforeStr()
  126. {
  127. return (m_beforehtmlstr);
  128. }
  129. //
  130. //
  131. TCHAR *HTXCommand::GetAfterStr()
  132. {
  133. return (m_afterhtmlstr);
  134. }
  135. //
  136. //
  137. UINT HTXCommand::ReadBeforeStr(UINT start, UINT end, LPCTSTR startstr)
  138. {
  139. m_beforesize = (UINT) (end - start);
  140. ASSERT (m_beforesize >= 0);
  141. m_beforehtmlstr = (TCHAR *)malloc((m_beforesize + 1) * sizeof (TCHAR));
  142. if (m_beforehtmlstr == NULL)
  143. return (m_error = TRUE);
  144. // copy data
  145. memcpy(m_beforehtmlstr, &startstr[start], m_beforesize * sizeof (TCHAR));
  146. m_beforehtmlstr[m_beforesize] = _T('\0');
  147. m_beforelen = _tcslen(m_beforehtmlstr);
  148. return (FALSE);
  149. }
  150. //
  151. //
  152. UINT HTXCommand::ReadAfterStr(UINT start, UINT end, LPCTSTR startstr)
  153. {
  154. m_aftersize = (UINT) (end - start);
  155. ASSERT (m_aftersize >= 0);
  156. m_afterhtmlstr = (TCHAR *)malloc((m_aftersize + 1) * sizeof (TCHAR));
  157. if (m_afterhtmlstr == NULL)
  158. return (m_error = TRUE);
  159. // copy data
  160. memcpy(m_afterhtmlstr, &startstr[start], m_aftersize * sizeof (TCHAR));
  161. m_afterhtmlstr[m_aftersize] = _T('\0');
  162. m_afterlen = _tcslen(m_afterhtmlstr);
  163. return (FALSE);
  164. }
  165. //
  166. //
  167. HTXCommand *HTXCommand::Execute(CString *cstr, CInfer *infer)
  168. {
  169. *cstr += GetAfterStr();
  170. return( this);
  171. }
  172. HTXCommand *HTXCommand::GetNext()
  173. {
  174. return(m_next);
  175. }
  176. void HTXCommand::SetNext(HTXCommand *next)
  177. {
  178. m_next = next;
  179. }
  180. HTXIfCommand::HTXIfCommand(UINT type, TCHAR *idstr, UINT variable): HTXCommand(type, idstr)
  181. {
  182. m_else = NULL;
  183. m_endif = NULL;
  184. m_var_index = variable;
  185. }
  186. HTXIfCommand::~HTXIfCommand()
  187. {
  188. }
  189. //
  190. // PURPOSE: Will execute the 'if' command. When done it will return a pointer
  191. // to the 'endif' command.
  192. //
  193. HTXCommand *HTXIfCommand::Execute(CString *cstr, CInfer *infer)
  194. {
  195. HTXCommand *cur_com;
  196. if ( infer->EvalData(m_var_index ) != NULL) {
  197. *cstr += GetAfterStr();
  198. //execute if commands
  199. cur_com = this->GetNext();
  200. while (cur_com->GetType() != HTX_TYPEELSE &&
  201. cur_com->GetType() != HTX_TYPEENDIF) {
  202. cur_com = cur_com->Execute(cstr, infer);
  203. cur_com = cur_com->GetNext();
  204. }
  205. } else {
  206. if ((cur_com = this->GetElse()) != NULL) {
  207. while (cur_com->GetType() != HTX_TYPEENDIF) {
  208. cur_com = cur_com->Execute(cstr, infer);
  209. cur_com = cur_com->GetNext();
  210. }
  211. }
  212. }
  213. cur_com = this->GetEndIf();
  214. cur_com->Execute(cstr, infer);
  215. return(cur_com);
  216. }
  217. HTXCommand *HTXCommand::GetElse()
  218. {
  219. return(NULL);
  220. }
  221. HTXCommand *HTXCommand::GetEndIf()
  222. {
  223. return(NULL);
  224. }
  225. HTXCommand *HTXCommand::GetEndFor()
  226. {
  227. return(NULL);
  228. }
  229. void HTXCommand::SetElse(HTXCommand *elseif)
  230. {
  231. }
  232. void HTXCommand::SetEndIf(HTXCommand *endifif)
  233. {
  234. }
  235. void HTXCommand::SetEndFor(HTXCommand *endfor)
  236. {
  237. }
  238. void HTXCommand::GetResource(CString &strResPath, const CString& strCHM)
  239. {
  240. }
  241. //
  242. //
  243. HTXCommand *HTXIfCommand::GetEndIf()
  244. {
  245. return(m_endif);
  246. }
  247. //
  248. //
  249. void HTXIfCommand::SetEndIf(HTXCommand *endif)
  250. {
  251. m_endif = endif;
  252. }
  253. //
  254. //
  255. HTXCommand *HTXIfCommand::GetElse()
  256. {
  257. return(m_else);
  258. }
  259. //
  260. //
  261. void HTXIfCommand::SetElse(HTXCommand *elseif)
  262. {
  263. m_else = elseif;
  264. }
  265. HTXForCommand::HTXForCommand(UINT type, TCHAR *idstr, UINT variable): HTXCommand(type, idstr)
  266. {
  267. m_endfor = NULL;
  268. m_var_index = variable;
  269. }
  270. HTXForCommand::~HTXForCommand()
  271. {
  272. }
  273. //
  274. // PURPOSE: Executes the 'forany' command. when done it
  275. // will retrurn a pointer to the 'endfor' command.
  276. //
  277. HTXCommand *HTXForCommand::Execute(CString *cstr, CInfer *infer)
  278. {
  279. HTXCommand *cur_com;
  280. if (!(infer->InitVar(m_var_index)) ){
  281. this->GetEndFor()->Execute(cstr,infer);
  282. return(this->GetEndFor());
  283. }
  284. cur_com = this;
  285. do {
  286. *cstr += cur_com->GetAfterStr();
  287. cur_com = cur_com->GetNext();
  288. while (cur_com->GetType() != HTX_TYPEENDFOR) {
  289. cur_com = cur_com->Execute(cstr, infer);
  290. cur_com = cur_com->GetNext();
  291. }
  292. cur_com = this;
  293. } while (infer->NextVar(m_var_index));
  294. cur_com = this->GetEndFor();
  295. cur_com->Execute(cstr,infer);
  296. return(cur_com);
  297. }
  298. //
  299. //
  300. HTXCommand *HTXForCommand::GetEndFor()
  301. {
  302. return(m_endfor);
  303. }
  304. //
  305. //
  306. void HTXForCommand::SetEndFor(HTXCommand *endfor)
  307. {
  308. m_endfor = endfor;
  309. }
  310. HTXDisplayCommand::HTXDisplayCommand(UINT type, TCHAR *idstr, UINT variable): HTXCommand(type, idstr)
  311. {
  312. m_var_index = variable;
  313. }
  314. HTXDisplayCommand::~HTXDisplayCommand()
  315. {
  316. }
  317. HTXCommand *HTXDisplayCommand::Execute(CString *cstr, CInfer *infer)
  318. {
  319. TCHAR *pstr;
  320. if ((pstr=infer->EvalData(m_var_index))!= NULL)
  321. *cstr += pstr;
  322. *cstr += GetAfterStr();
  323. return(this);
  324. }
  325. HTXResourceCommand::HTXResourceCommand(UINT type, TCHAR *idstr)
  326. : HTXCommand(type, idstr)
  327. {
  328. m_strResource = _T("");
  329. m_strFileName = _T("");
  330. return;
  331. }
  332. HTXResourceCommand::~HTXResourceCommand()
  333. {
  334. return;
  335. }
  336. void HTXResourceCommand::GetResName(LPCTSTR var_name)
  337. {
  338. m_strFileName = &var_name[1];
  339. // Remove the > from the end.
  340. TCHAR EndChar[2];
  341. EndChar[0] = m_strFileName.GetAt(m_strFileName.GetLength() - 1);
  342. EndChar[1] = NULL;
  343. if (0 == _tcsncmp(_T(">") , EndChar, 1))
  344. {
  345. m_strFileName.GetBufferSetLength(m_strFileName.GetLength() - 1);
  346. m_strFileName.ReleaseBuffer();
  347. }
  348. return;
  349. }
  350. // strCHM contains CHM file name. Must be empty if resource file is not inside CHM.
  351. void HTXResourceCommand::GetResource(CString& strResPath, const CString& strCHM)
  352. {
  353. CString strFile;
  354. if (strCHM.GetLength())
  355. {
  356. char* tmp_buf =NULL;
  357. ULONG Bytes =0;
  358. strFile = strResPath + strCHM;
  359. // m_filename is CHM file path and name
  360. // and strFile - file name within CHM
  361. if (S_OK != ::ReadChmFile(strFile, m_strFileName, (void**)&tmp_buf, &Bytes))
  362. {
  363. // ERROR READING CHM
  364. return;
  365. }
  366. tmp_buf[Bytes] = NULL;
  367. m_strResource = tmp_buf;
  368. }
  369. else
  370. {
  371. FILE *pFile;
  372. CHAR szBuf[4097];
  373. size_t Bytes =0;
  374. strFile = strResPath + m_strFileName;
  375. if (NULL == (pFile = _tfopen((LPCTSTR) strFile, _T("rb"))))
  376. ReportError(TSERR_RES_MISSING);
  377. do
  378. {
  379. if((Bytes = fread(szBuf, 1, 4096, pFile)) > 0)
  380. {
  381. szBuf[Bytes] = NULL;
  382. m_strResource += szBuf;
  383. }
  384. } while (Bytes == 4096);
  385. if (!feof(pFile))
  386. {
  387. fclose(pFile);
  388. ReportError(TSERR_RES_MISSING);
  389. }
  390. fclose(pFile);
  391. }
  392. return;
  393. }
  394. HTXCommand *HTXResourceCommand::Execute(CString *cstr, CInfer *)
  395. {
  396. // Read the resource file into cstr.
  397. *cstr += m_strResource;
  398. *cstr += GetAfterStr();
  399. return(this);
  400. }