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.

197 lines
5.2 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. int _cdecl main(int argc, char *argv[])
  22. {
  23. int retCode;
  24. PrintBanner();
  25. retCode = gCommandLine.ParseCmdLine(argc, argv);
  26. if (retCode != ERROR_SUCCESS)
  27. return retCode;
  28. _W(WARN_CHECKPOINT, IDS_CHKP_WRN01);
  29. retCode = gParser.ParseInputFile();
  30. if (retCode != ERROR_SUCCESS)
  31. return retCode;
  32. _W(WARN_CHECKPOINT, IDS_CHKP_WRN02, gCommandLine.m_szFileName);
  33. retCode = gRegistry.Connect();
  34. if (retCode != ERROR_SUCCESS)
  35. return retCode;
  36. _W(WARN_CHECKPOINT, IDS_CHKP_WRN03, gCommandLine.m_szSystem == NULL ? "localhost" : gCommandLine.m_szSystem);
  37. retCode = gParser.ProcessCommands();
  38. if (retCode != ERROR_SUCCESS)
  39. return retCode;
  40. _W(WARN_CHECKPOINT, IDS_CHKP_WRN04);
  41. if (gCommandLine.m_nFlags & CMDLINE_FLG_NORESTART)
  42. {
  43. if (gRegistry.m_dwFlags & REG_FLG_NEEDRESTART)
  44. _W(WARN_ALERT, IDS_ALRT_WRN05);
  45. }
  46. else
  47. {
  48. if (gRegistry.m_dwFlags & REG_FLG_NEEDRESTART)
  49. {
  50. if (!gSNMPController.IsSNMPRunning())
  51. _W(WARN_ATTENTION, IDS_ATTN_WRN06);
  52. else
  53. {
  54. _W(WARN_CHECKPOINT, IDS_CHKP_WRN07);
  55. retCode = gSNMPController.StopSNMP();
  56. if (retCode != ERROR_SUCCESS)
  57. return retCode;
  58. _W(WARN_CHECKPOINT, IDS_CHKP_WRN08);
  59. retCode = gSNMPController.StartSNMP();
  60. if (retCode != ERROR_SUCCESS)
  61. return retCode;
  62. _W(WARN_CHECKPOINT, IDS_CHKP_WRN09);
  63. }
  64. }
  65. else
  66. _W(WARN_ATTENTION, IDS_ATTN_WRN10);
  67. }
  68. return retCode;
  69. }
  70. CCommandLine::CCommandLine()
  71. {
  72. m_szFileName = NULL;
  73. m_szSystem = NULL;
  74. m_nVerboseLevel = WARN_CHECKPOINT;
  75. m_nFlags = 0;
  76. }
  77. CCommandLine::~CCommandLine()
  78. {
  79. if (m_szFileName)
  80. delete m_szFileName;
  81. if (m_szSystem)
  82. delete m_szSystem;
  83. }
  84. DWORD CCommandLine::ParseCmdLine(int argc, char *argv[])
  85. {
  86. enum
  87. {
  88. STATE_ANY,
  89. STATE_ARG_VERBOSE,
  90. STATE_ARG_SYSTEM
  91. } state = STATE_ANY;
  92. for (int i=1; i<argc; i++)
  93. {
  94. switch(state)
  95. {
  96. case STATE_ANY:
  97. if (strchr(CMDLINE_DELIM,argv[i][0]) != NULL)
  98. {
  99. if (strchr(CMDLINE_OPTION_HELP, argv[i][1]) != NULL &&
  100. argv[i][2] == '\0')
  101. {
  102. PrintUsage(argv[0]);
  103. return ERROR_NO_DATA;
  104. }
  105. if (strchr(CMDLINE_OPTION_VERBOSE, argv[i][1]) != NULL)
  106. {
  107. if (argv[i][2] != '\0')
  108. {
  109. m_nVerboseLevel = atoi(argv[i]+2);
  110. _W(WARN_ATTENTION,IDS_ATTN_WRN11, m_nVerboseLevel);
  111. }
  112. else
  113. state = STATE_ARG_VERBOSE;
  114. break;
  115. }
  116. if (strchr(CMDLINE_OPTION_SYSTEM, argv[i][1]) != NULL &&
  117. argv[i][2] == '\0')
  118. {
  119. state = STATE_ARG_SYSTEM;
  120. break;
  121. }
  122. if (strchr(CMDLINE_OPTION_NORESTART, argv[i][1]) != NULL &&
  123. argv[i][2] == '\0')
  124. {
  125. m_nFlags |= CMDLINE_FLG_NORESTART;
  126. break;
  127. }
  128. else
  129. _W(WARN_ALERT,IDS_ALRT_WRN12, argv[i]);
  130. }
  131. else
  132. {
  133. if (m_szFileName != NULL)
  134. {
  135. _W(WARN_ALERT,
  136. IDS_ALRT_WRN13,
  137. argv[i]);
  138. delete m_szFileName;
  139. }
  140. m_szFileName = new char[strlen(argv[i])+1];
  141. if (m_szFileName == NULL)
  142. return _E(ERROR_OUTOFMEMORY, IDS_ERR01);
  143. strcpy(m_szFileName, argv[i]);
  144. }
  145. break;
  146. case STATE_ARG_VERBOSE:
  147. m_nVerboseLevel = atoi(argv[i]);
  148. _W(WARN_ATTENTION,IDS_ATTN_WRN14, m_nVerboseLevel);
  149. state = STATE_ANY;
  150. break;
  151. case STATE_ARG_SYSTEM:
  152. if (m_szSystem != NULL)
  153. {
  154. _W(WARN_ALERT,
  155. IDS_ALRT_WRN15,
  156. argv[i]);
  157. delete m_szSystem;
  158. }
  159. m_szSystem = new char[strlen(argv[i])+1];
  160. if (m_szSystem == NULL)
  161. return _E(ERROR_OUTOFMEMORY, IDS_ERR01);
  162. strcpy(m_szSystem, argv[i]);
  163. state = STATE_ANY;
  164. break;
  165. }
  166. }
  167. if (m_szFileName == NULL)
  168. {
  169. PrintUsage(argv[0]);
  170. return ERROR_NO_DATA;
  171. }
  172. return ERROR_SUCCESS;
  173. }
  174. DWORD CCommandLine::GetVerboseLevel()
  175. {
  176. return m_nVerboseLevel;
  177. }