KSH (AIX)-HistoryConv.c

/*
 * File Name: HistoryConv.c
 * File Creation Date: 19980104
 * Copyright 1998 by OZUS LLC       Auth0r: Lance Lenz
 * Copyright 2009 by TriParadigm LLC
 * Copyright 2009 by MtXia Inc.     Author: Dana French
 * Copyright 2009 by Edgetec Inc.   Author: Joseph Stephens
 * 
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby Restricted,
 * the above copyright notice must appear in all copies and that both the
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the names of 
 * OZUS LLC, TriParadigm LLC, MtXia Inc., Edgetec Inc.
 * not be used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  
 *  makes no representations about the OZUS LLC, TriParadigm LLC, 
 *  MtXia Inc., Edgetec Inc. suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 * 
 * Users Restricted Rights - Use, duplication or disclosure restricted
 * Restricted License: 2000      Company Name Here
 * 
 * Update Log:
 * 20091231: Lance Lenz:
 *
 * Compile cc user.c -lpq
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define NUL '\x00'	// Start of Line
#define SOH '\x01'	// Start of Record
#define CTRL_B '\x02'	// CTRL_B  
#define DE '\xde'	// Start of Time
#define DF '\xdf'	// End of Time
#define LF '\x0a'	// Line Feed  
#define CR '\x0d'	// Line Feed  
#define MAXDATA 20480


// Global Counts
int usCnt = 0, secCnt = 0;
int ServerNumber = 0;

// Time Conversion
convertTime(char *timeEpoch, char *rtnBuf)
{
	struct tm *t;
	time_t seconds;
	sscanf (timeEpoch, "%ld", &seconds);
	t = localtime(&seconds);
	sprintf(rtnBuf,"%4d-%02d-%02d %02d:%02d:%02d"
		, t->tm_year+1900
		, t->tm_mon+1
		, t->tm_mday
		, t->tm_hour
		, t->tm_min
		, t->tm_sec
	);
}

int main(int argc, char * *argv)
{
	FILE *in_file, *out_file;  /* input data file            */
	//struct tm *t;
	//time_t seconds;
	int i = 0;
	int vl_1 = 0;             /* Used in for loop read in from OCC         */
	int vl_2 = 0;             /* Used in for loop read in from OCC         */
	int startLine = 0;        /* Starting point of good data               */
	int startTime = 0;        /* Starting point of ephoc                   */
	int endLine = 0;          /* Ending point of good data                 */
	int c;                    /* Used as a char to read in from OCC        */
	char buf1[MAXDATA];
	char hold_buf[MAXDATA];
	char commandText[MAXDATA];
	char commandTextBuf[MAXDATA];
	char epoch[20];
	char convertedEpoch[20];


	if (argc != 2)
	{
		fprintf(stderr,"Usage: HistoryConv FileName\n");
		fprintf(stderr,"Usage: HistoryConv /OZUS/History/odcapmdm00-bmcaddm-sh_history.20120415.015741.bmcaddm.bmcaddm.146.61.182.59.pts-3\n");
		exit(1);
	}

	/* Open the password file and read into x array the user names */
	//strcpy(buf1,"/home/infosec/OZUS_Infosec/TestText");
	//strcat(buf1,"/odcapmdm00-bmcaddm-sh_history.20120415.015741.bmcaddm.bmcaddm.146.61.182.59.pts-3");
	strcat(buf1,argv[1]);
	if (!(in_file = fopen(buf1, "r"))) {
		printf("error opening input file; %s\n", buf1);
		exit(1);
	}
        printf("\n<TABLE Border=\"2\" Width=\"100%%\" Cellspacing=\"2\" Cellpadding=\"2\"><TR><TD Bgcolor=\"#B0C4DE\" width=\"10%%\">****** History File ******</TD> <TD Bgcolor=\"#B0C4DE\" width=\"95%%\"> %s</TD></TR>\n",buf1);
	/*
 	* Read in char-x-char to look for CTRL and NewLine to determin where the line breaks are
 	* A line ends just after the EPOCH with a 0x0A or something like #df#\n
 	*/
        vl_1 = 0;
        vl_2 = 0;
	memset(&commandText,'\0', sizeof(commandText));
	memset(&hold_buf,'\0', sizeof(hold_buf));
	startLine = 0;
	startTime = 0;
	endLine = 0;

	while (( c = getc(in_file)) != EOF)  /* read in record */
	{
		// if for some reason we miss a counter reset everyting to keep the buffer from over flowing
		if((vl_1 >= MAXDATA) || (vl_2 >= MAXDATA))
		{
			printf("Error MAXDATA: read exceeded %d chars for vl_1 %d or vl_2 %d.\n File Name: %s\n",MAXDATA, vl_1, vl_2, buf1);
			fclose(in_file); // Close the input file
			exit(2);
		}

		// Load the buffer on char at a time
		hold_buf[vl_1] = c; 
		//printf("VL= %d %s\n",vl_1, hold_buf);


		// Setting the buffer to NULL and the vl_1 counter to -1 for the else statment that does a vl_1++
		// this is a port way to make it Zero after the while loop
		if(hold_buf[vl_1] == SOH)
		{
			vl_1 = -1;
			vl_2 = 0;
			memset(&commandText,'\0', sizeof(commandText));
			memset(&hold_buf,'\0', sizeof(hold_buf));
			startLine = 0;
			startTime = 0;
			endLine = 0;
		}
		// Conver to lower ascii value on the chart
		if(hold_buf[vl_1] == CTRL_B)
			hold_buf[vl_1] = ' '; 
		if(hold_buf[vl_1] == DE)
			hold_buf[vl_1] = '^'; 
		// Conver to lower ascii value on the chart
		if(hold_buf[vl_1] == DF)
			hold_buf[vl_1] = '_'; 
		// Conver to a different char to test with
		if(hold_buf[vl_1] == LF)
		{
			hold_buf[vl_1] = ';'; 
		}

		// Setting the buffer to NULL and the vl_1 counter to -1 for the else statment that does a vl_1++
		// this is a port way to make it Zero after the while loop
		if(hold_buf[vl_1] == NUL)
		{
			vl_1 = -1;
			vl_2 = 0;
			memset(&commandText,'\0', sizeof(commandText));
			memset(&hold_buf,'\0', sizeof(hold_buf));
			startLine = 0;
			startTime = 0;
			endLine = 0;
		}

		// Test for the start location of epoch
		if((strncmp(&hold_buf[vl_1 -2],"#^#", 3)) == 0)
				startTime= vl_1 -2;

		// Test for the end location of epoch and end of the record we want to capture
		if((strncmp(&hold_buf[vl_1 -3],"#_#;", 4)) == 0)
		{
			endLine = (vl_1 - startTime);
			strncpy(epoch,&hold_buf[startTime+3],endLine-6);
			convertTime(epoch,convertedEpoch);

			//strncpy(commandText,&hold_buf[0],startTime - 4);
			strncpy(commandText,&hold_buf[0],startTime);

			// This is a good place to put a function call to parce the commandText for chars that cannot go into the database
			// or wrap escape chars around before the insert into the DB
			//
			vl_2 = 0;
			memset(&commandTextBuf,'\0', sizeof(commandTextBuf));
			 for(i = 0; i<= sizeof(commandText);i++)
			 {
				commandTextBuf[vl_2] =  commandText[i]; 

				if(commandTextBuf[vl_2] == '\'')
				{
					commandTextBuf[vl_2] = '\\'; 
					vl_2++;
					commandTextBuf[vl_2] = '\''; 
					vl_2++;
				}
				else if(commandTextBuf[vl_2] == '\"')
				{
					commandTextBuf[vl_2] = '\\'; 
					vl_2++;
					commandTextBuf[vl_2] = '\"'; 
					vl_2++;
				}
				else
					vl_2++;
			}
			//printf("\n********StartLine=%d StartTime=%d EndTime=%d Epoc %s Time=%s********\n",startLine,startTime,endLine,epoch,convertedEpoch);
			//printf("\n------------First--Totalcount=%d -----Text=%s ---------------\n",vl_1,hold_buf);
                        printf("<TR><TD>%s</TD><TD>%s</TD></TR>\n",convertedEpoch,commandTextBuf);

			// Reset all the buffers back to zero and keep reading in the next X chars.
			vl_1 = 0;
			vl_2 = 0;
			memset(&commandText,'\0', sizeof(commandText));
			memset(&hold_buf,'\0', sizeof(hold_buf));
			startLine = 0;
			startTime = 0;
			endLine = 0;
		}
		else
			vl_1++;
	}
	printf("</TABLE>\n");

	fclose(in_file); // Close the input file
	exit(0);