📄 eventcmt.cpp
字号:
//****************************************************************************
//
// Copyright (c) 1996, Microsoft Corporation
//
// File: EVENTCMT.CPP
//
// Implementation of the EventConfigModifier class.
//
// Author: Nadir Ahmed (nadira@microsoft.com)
//
// History:
//
// nadira 03/20/96 Created.
//
//****************************************************************************
// Public Includes
// ===============
// Private includes
// ================
#include "stdafx.h"
#include "resource.h"
#include <eventcmd.h>
#include <eventcmt.h>
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
// Defines
// =======
#define EVENTCMT_LINE_LENGTH 500
// Moved to eventcmt.h.
// ====================
// #define EVENTCMT_SYSTEM_MODE 0
// #define EVENTCMT_CUSTOM_MODE 1
// #define EVENTCMT_REQUST_MODE 2
// Globals
// =======
//============================================================================
// EventConfigModifier::EventConfigModifier
//
// This is the EventConfigModifier class's only constructor. This class checks
// the command arguments, connects to and opens the target machines registry.
// Handles writing to the eventcmt.log. Reads in the configuration file, builds
// lists of configuration items to be added/deleted and processes these lists.
// Starts and stops the SNMP service if necessary and generates a status mif.
//
//
// Parameters:
//
// LPCSTR CmdLine This is a space separated list of arguments.
//
// LPCSTR machine The machine name whose configuration is to be
// modified.
//
// Returns:
//
// none
//
//============================================================================
EventConfigModifier::EventConfigModifier(LPCSTR CmdLine, LPCSTR machine)
{
nl.LoadString(IDS_NL);
tab.LoadString(IDS_TAB);
#ifdef EVENTCMT_OLD_LOG
NL.LoadString(IDS_LFCR);
#else //EVENTCMT_OLD_LOG
NL = nl;
#endif //EVENTCMT_OLD_LOG
if(CmdLine)
CommandLine = CmdLine;
if (machine)
Machine = machine;
// This class variable's condition indicates whether an ERROR status mif has been written
// ======================================================================================
PrevStatus = TRUE;
// This class variable's condition indicates whether the SNMP config has been modified
// ===================================================================================
SNMPModified = FALSE;
// This class variable's condition indicates whether help needs to be printed
// ==========================================================================
Printhelp = FALSE;
}
//============================================================================
// EventConfigModifier::~EventConfigModifier
//
// This is the EventConfigModifier class's only destructor.
//
//
// Parameters:
//
// none
//
// Returns:
//
// none
//
//============================================================================
EventConfigModifier::~EventConfigModifier()
{
}
//============================================================================
// EventConfigModifier::SetError
//
// This private method sets the error code that the Main() method returns.
//
//
// Parameters:
//
// DWORD val The error code to be set. If this has already been set
// return.
//
// Returns:
//
// none
//
//============================================================================
void EventConfigModifier::SetError(DWORD val)
{
// Check the current error state. Only add the code if it is new.
// ==============================================================
if(EvCmtReturnCode & val)
return;
else
EvCmtReturnCode += val;
}
//============================================================================
// EventConfigModifier::ProcessCommandLine
//
// This private method processes the command line that was passed to the class
// constructor as the first parameter. The command line may have several args
// which define how this class will modify the configuration. The first argumemt
// is compulsory and is always the name of the configuration file. If this is
// missing this function will return false. Any following arguments are command
// switches. Case is ignored for the switches and the those currently supported
// are...
// /NOMIF - no mif file will be generated
// /NOLOG - the log file will remain unchanged
// /DEFAULT - only run if the current config has default settings
// /SETCUSTOM - set this config to have custom settings
// /NOSTOPSTART - do not stop and start the snmp service
//
//
// Parameters:
//
// none
//
// Returns:
//
// BOOL True if there were no errors, False otherwise
//
//============================================================================
BOOL EventConfigModifier::ProcessCommandLine()
{
// return FALSE if there is no command line
// ========================================
if (!CommandLine.GetLength())
{
SetError(EVCMT_BAD_ARGS);
return FALSE;
}
LONG Result;
if (Machine.GetLength())
{
Result = RegConnectRegistry(Machine.GetBuffer(1), HKEY_LOCAL_MACHINE, &hkey_machine);
Machine.ReleaseBuffer();
}
else
Result = RegConnectRegistry(NULL, HKEY_LOCAL_MACHINE, &hkey_machine);
if (Result != ERROR_SUCCESS)
{
StatusMif(IDS_NO_REG_CNT, FALSE);
SetError(EVCMT_REG_CNNCT_FAILED);
return FALSE;
}
BOOL validArgs = TRUE;
LogWanted = TRUE;
StatusMifWritten = FALSE;
MandatoryMode = TRUE;
SetCustom = FALSE;
SNMPStopStart = TRUE;
CString tempbuff(CommandLine);
CString * next = CStringStrtok(&tempbuff);
CString help;
help.LoadString(IDS_ARG_HELP);
CString helph;
helph.LoadString(IDS_ARG_HELPH);
CString isHelp(*next);
isHelp.MakeUpper();
if ((isHelp == helph) || (isHelp == help))
{
Printhelp = TRUE;
}
else
{
// return FALSE if there is no file specified
// ==========================================
if (next)
{
CommandFile = *next; //set the config file.
delete next;
}
else
{
RegCloseKey(hkey_machine);
return FALSE;
}
next = CStringStrtok(&tempbuff);
UniqueList commandargs;
// get all the other arguments into a list for processing
// ======================================================
while (next)
{
//check miff
ListItem * tempL = new ListItem(next);
if(commandargs.Add(tempL))
delete tempL;
next = CStringStrtok(&tempbuff);
}
// load all the valid command args
// ===============================
CString NoMif;
CString NoLog;
CString Mode;
CString Custom;
CString noSNMPstopstart;
NoMif.LoadString(IDS_ARG_NOMIF);
Mode.LoadString(IDS_ARG_MODE);
NoLog.LoadString(IDS_ARG_NOLOG);
Custom.LoadString(IDS_ARG_CUSTOM);
noSNMPstopstart.LoadString(IDS_ARG_SNMP);
// process all the command args
// ============================
while (!commandargs.IsEmpty())
{
ListItem * item = commandargs.RemoveHead();
CString * tmp = item->GetString();
tmp->MakeUpper();
if (*tmp == NoMif)
{
StatusMifWritten = TRUE; //no status mif so indicate that one has been written
PrevStatus = FALSE; //an error mif so another will not be written
}
else if (*tmp == NoLog)
LogWanted = FALSE; //no information to be added to the log file
else if (*tmp == Mode)
MandatoryMode = FALSE; //only run if we have a DEFAULT config.
else if (*tmp == Custom)
SetCustom = TRUE; //set our config to be CUSTOM
else if (*tmp == noSNMPstopstart)
SNMPStopStart = FALSE; //do not stop and start the SNMP service
else if ((*tmp == help) || (*tmp == helph))
Printhelp = TRUE; //we're gonna print help!
else
{
validArgs = FALSE; //invalid arg specified
}
delete item;
}
if (Printhelp)
{
validArgs = TRUE;
StatusMifWritten = TRUE; //no staus mif if we print help
}
else if (!validArgs)
{
StatusMifWritten = FALSE; //want a status mif generated if there was an error above!
PrevStatus = TRUE; //no error mif has been written yet.
RegCloseKey(hkey_machine);
LogWanted = TRUE;
SetError(EVCMT_BAD_ARGS);
}
#ifndef EVENTCMT_OLD_LOG
/*
if (LogWanted)
{
CString logname;
logname.LoadString(IDS_LOG_FILE);
if (ERROR_SUCCESS != SMSCliLogInitialize(logname))
LogWanted = FALSE;
}
*/
#endif EVENTCMT_OLD_LOG
}
return validArgs;
}
//============================================================================
// EventConfigModifier::ReadLineIn
//
// This private method is used to read from a certain posn to the end of a
// line from a file.
//
//
// Parameters:
//
// HANDLE * h_file a pointer to the handle of the file to read from
//
// CString * Line a pointer to the CString to accept the text read
//
// DWORD * startpoint a ponter to the starting point for the read.
// when the procedure exits this will be the point
// in the file where reading stopped i.e. start of a
// new line.
//
// Returns:
//
// BOOL True if there were no errors, False otherwise
//
//============================================================================
BOOL EventConfigModifier::ReadLineIn(HANDLE * h_file, CString * Line, DWORD * startpoint)
{
Line->Empty(); //make sure the CString we're writing to is empty
TCHAR buff[EVENTCMT_LINE_LENGTH +1]; //a buffer to read into
DWORD bytesRead; //the number of bytes read from the file
OVERLAPPED overlapped;
overlapped.OffsetHigh = 0;
overlapped.hEvent = NULL;
// This next loop will run until we read a newline or an EOF
// =========================================================
while(TRUE)
{
memset(buff, 0, sizeof(TCHAR)*(EVENTCMT_LINE_LENGTH +1));
overlapped.Offset = *startpoint; //set the start point for the next read
int x = 0;
bytesRead = 0;
// Do the actual read from the file
// ================================
if (!ReadFile(*h_file, &buff, EVENTCMT_LINE_LENGTH, &bytesRead, &overlapped))
{
DWORD e = GetLastError();
return FALSE;
}
if (!bytesRead)
return FALSE;
CString tmp(buff);
CString linefeed;
linefeed.LoadString(IDS_LINEFEED);
// This loop will see if we have read a newline if yes, build the CString
// ======================================================================
while(x < tmp.GetLength())
{
if(tmp.GetAt(x) == nl.GetAt(0)) //we've found a newline
{
*startpoint = *startpoint + x + 1; //adjust startpoint for the next read
for(int i = 0; i < x - 1; i++)
*Line += tmp.GetAt(i);
if(tmp.GetAt(x-1) != linefeed.GetAt(0)) //don't want char before \n if it is \r
*Line += tmp.GetAt(x-1);
return TRUE;
}
x++;
}
// We haven't got a newline so set things up for the next read from file
// =====================================================================
*Line += tmp; //add the text to our CString
*startpoint = *startpoint + bytesRead; //adjust the startpoint for the next read.
if (bytesRead < EVENTCMT_LINE_LENGTH) //we must have hit the end of file so return
return TRUE;
}
}
//============================================================================
// EventConfigModifier::ReadConfigIn
//
// This private method is used to read the configuration file and build the
// the two (traps dests and translator config) lists of configuration updates
// required.
//
//
// Parameters:
//
// none
//
// Returns:
//
// none
//
//============================================================================
void EventConfigModifier::ReadConfigIn()
{
CString buff;
CString CBuff;
// Open the configuration file for read
// ====================================
HANDLE hfile = CreateFile(CommandFile,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
StatusMif(IDS_IFILE, FALSE);
SetError(EVCMT_BAD_INPUT_FILE);
return;
}
else // we have an open file
{
CString * next;
CString msg1str;
msg1str += NL;
msg1str += NL;
CString addmsg;
addmsg.LoadString(IDS_MSG24);
msg1str += addmsg;
msg1str += NL;
msg1str += NL;
WriteToLog(&msg1str);
int badlines = 0;
int goodlines = 0;
DWORD Offset = 0;
// this loop reads and processes the file line by line
// ===================================================
while (ReadLineIn(&hfile, &buff, &Offset))
{
CString comment;
comment.LoadString(IDS_COMMENT);
CBuff = buff;
CString wholebuff(buff);
if (!CBuff.GetLength())
continue; //blank line
// Is it a comment, if it is just skip it.
if (CBuff.GetAt(0) == comment.GetAt(0))
{
CString msg;
msg.LoadString(IDS_MSG17);
wholebuff += msg;
wholebuff += NL;
WriteToLog(&wholebuff);
continue;
}
next = CStringStrtok (&CBuff); //get the first token from the line
CString prag;
prag.LoadString(IDS_PRAGMA);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -