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.

209 lines
5.8 KiB

  1. #include <stdafx.h>
  2. #include "EventCmd.h"
  3. #include "Errors.h"
  4. #include "Parser.h"
  5. #include "Registry.h"
  6. #include "SNMPCtrl.h"
  7. CString gStrMessage;
  8. CCommandLine gCommandLine;
  9. void PrintUsage(char *szCmdName)
  10. {
  11. gStrMessage.LoadString(100);
  12. gStrMessage.AnsiToOem();
  13. printf("%s", gStrMessage);
  14. }
  15. void PrintBanner()
  16. {
  17. gStrMessage.LoadString(101);
  18. gStrMessage.AnsiToOem();
  19. printf("%s", gStrMessage);
  20. }
  21. void InitThreadOutputCodepage()
  22. {
  23. setlocale (LC_COLLATE, ".OCP"); // sets the sort order
  24. setlocale (LC_MONETARY, ".OCP"); // sets the currency formatting rules
  25. setlocale (LC_NUMERIC, ".OCP"); // sets the formatting of numerals
  26. setlocale (LC_TIME, ".OCP"); // defines the date/time formatting
  27. SetThreadUILanguage(0);
  28. }
  29. int _cdecl main(int argc, char *argv[])
  30. {
  31. int retCode;
  32. InitThreadOutputCodepage();
  33. PrintBanner();
  34. retCode = gCommandLine.ParseCmdLine(argc, argv);
  35. if (retCode != ERROR_SUCCESS)
  36. return retCode;
  37. _W(WARN_CHECKPOINT, IDS_CHKP_WRN01);
  38. retCode = gParser.ParseInputFile();
  39. if (retCode != ERROR_SUCCESS)
  40. return retCode;
  41. _W(WARN_CHECKPOINT, IDS_CHKP_WRN02, gCommandLine.m_szFileName);
  42. retCode = gRegistry.Connect();
  43. if (retCode != ERROR_SUCCESS)
  44. return retCode;
  45. _W(WARN_CHECKPOINT, IDS_CHKP_WRN03, gCommandLine.m_szSystem == NULL ? "localhost" : gCommandLine.m_szSystem);
  46. retCode = gParser.ProcessCommands();
  47. if (retCode != ERROR_SUCCESS)
  48. return retCode;
  49. _W(WARN_CHECKPOINT, IDS_CHKP_WRN04);
  50. if (gCommandLine.m_nFlags & CMDLINE_FLG_NORESTART)
  51. {
  52. if (gRegistry.m_dwFlags & REG_FLG_NEEDRESTART)
  53. _W(WARN_ALERT, IDS_ALRT_WRN05);
  54. }
  55. else
  56. {
  57. if (gRegistry.m_dwFlags & REG_FLG_NEEDRESTART)
  58. {
  59. if (!gSNMPController.IsSNMPRunning())
  60. _W(WARN_ATTENTION, IDS_ATTN_WRN06);
  61. else
  62. {
  63. _W(WARN_CHECKPOINT, IDS_CHKP_WRN07);
  64. retCode = gSNMPController.StopSNMP();
  65. if (retCode != ERROR_SUCCESS)
  66. return retCode;
  67. _W(WARN_CHECKPOINT, IDS_CHKP_WRN08);
  68. retCode = gSNMPController.StartSNMP();
  69. if (retCode != ERROR_SUCCESS)
  70. return retCode;
  71. _W(WARN_CHECKPOINT, IDS_CHKP_WRN09);
  72. }
  73. }
  74. else
  75. _W(WARN_ATTENTION, IDS_ATTN_WRN10);
  76. }
  77. return retCode;
  78. }
  79. CCommandLine::CCommandLine()
  80. {
  81. m_szFileName = NULL;
  82. m_szSystem = NULL;
  83. m_nVerboseLevel = WARN_CHECKPOINT;
  84. m_nFlags = 0;
  85. }
  86. CCommandLine::~CCommandLine()
  87. {
  88. if (m_szFileName)
  89. delete m_szFileName;
  90. if (m_szSystem)
  91. delete m_szSystem;
  92. }
  93. DWORD CCommandLine::ParseCmdLine(int argc, char *argv[])
  94. {
  95. enum
  96. {
  97. STATE_ANY,
  98. STATE_ARG_VERBOSE,
  99. STATE_ARG_SYSTEM
  100. } state = STATE_ANY;
  101. for (int i=1; i<argc; i++)
  102. {
  103. switch(state)
  104. {
  105. case STATE_ANY:
  106. if (strchr(CMDLINE_DELIM,argv[i][0]) != NULL)
  107. {
  108. if (strchr(CMDLINE_OPTION_HELP, argv[i][1]) != NULL &&
  109. argv[i][2] == '\0')
  110. {
  111. PrintUsage(argv[0]);
  112. return ERROR_NO_DATA;
  113. }
  114. if (strchr(CMDLINE_OPTION_VERBOSE, argv[i][1]) != NULL)
  115. {
  116. if (argv[i][2] != '\0')
  117. {
  118. m_nVerboseLevel = atoi(argv[i]+2);
  119. _W(WARN_ATTENTION,IDS_ATTN_WRN11, m_nVerboseLevel);
  120. }
  121. else
  122. state = STATE_ARG_VERBOSE;
  123. break;
  124. }
  125. if (strchr(CMDLINE_OPTION_SYSTEM, argv[i][1]) != NULL &&
  126. argv[i][2] == '\0')
  127. {
  128. state = STATE_ARG_SYSTEM;
  129. break;
  130. }
  131. if (strchr(CMDLINE_OPTION_NORESTART, argv[i][1]) != NULL &&
  132. argv[i][2] == '\0')
  133. {
  134. m_nFlags |= CMDLINE_FLG_NORESTART;
  135. break;
  136. }
  137. else
  138. _W(WARN_ALERT,IDS_ALRT_WRN12, argv[i]);
  139. }
  140. else
  141. {
  142. if (m_szFileName != NULL)
  143. {
  144. _W(WARN_ALERT,
  145. IDS_ALRT_WRN13,
  146. argv[i]);
  147. delete m_szFileName;
  148. }
  149. m_szFileName = new char[strlen(argv[i])+1];
  150. if (m_szFileName == NULL)
  151. return _E(ERROR_OUTOFMEMORY, IDS_ERR01);
  152. strcpy(m_szFileName, argv[i]);
  153. }
  154. break;
  155. case STATE_ARG_VERBOSE:
  156. m_nVerboseLevel = atoi(argv[i]);
  157. _W(WARN_ATTENTION,IDS_ATTN_WRN14, m_nVerboseLevel);
  158. state = STATE_ANY;
  159. break;
  160. case STATE_ARG_SYSTEM:
  161. if (m_szSystem != NULL)
  162. {
  163. _W(WARN_ALERT,
  164. IDS_ALRT_WRN15,
  165. argv[i]);
  166. delete m_szSystem;
  167. }
  168. m_szSystem = new char[strlen(argv[i])+1];
  169. if (m_szSystem == NULL)
  170. return _E(ERROR_OUTOFMEMORY, IDS_ERR01);
  171. strcpy(m_szSystem, argv[i]);
  172. state = STATE_ANY;
  173. break;
  174. }
  175. }
  176. if (m_szFileName == NULL)
  177. {
  178. PrintUsage(argv[0]);
  179. return ERROR_NO_DATA;
  180. }
  181. return ERROR_SUCCESS;
  182. }
  183. DWORD CCommandLine::GetVerboseLevel()
  184. {
  185. return m_nVerboseLevel;
  186. }