/*
 *
 * utils.c 
 * BIG BROTHER C utilities functions
 * Sean MacGuire - The MacLawran Group Inc.
 * Version 14
 * Mar 05th, 2000
 *
 * This program is Copyright (c) 1997-2000
 * The MacLawran Group Inc.
 * All Rights Reserved
 *
 */


#include "bb.h"		/* THE BIG BROTHER INCLUDE FILE */


int replacestr(dst,dstmaxlen,from,to) 
char *dst;
int dstmaxlen;
char *from;
char *to;
{
char *realdst;
char *frompos;
char *dsttmp = NULL;

	debug("INTO REPLACESTR\n");

	realdst = dst;

	/* Dont work on null strings */
	if (!dst || !from || !to || (dstmaxlen == 0)) {
		debug("dst: [%s] from: [%s] to: [%s] dstmaxlen: [%d]\n",dst,from,to,dstmaxlen);
		return(1);
	}

	while( (frompos=(char *)strstr(dst,from)) ) {
		debug("match on [%s]\n",from);
		if( !dsttmp ) {
			dsttmp = (char *) malloc(dstmaxlen);
			/* Couldn't malloc */
			if( !dsttmp )
				return(2);
			memset(dsttmp,'\0',dstmaxlen);
		}
		/* Some strncpy barf on len = 0 */
		if( frompos-dst ) {
			*frompos = '\0';
			strcat(dsttmp,dst);
		}
		if( strlen(dsttmp) + strlen(to) > dstmaxlen ) {
			free(dsttmp);
			return(3);
		}
		strcat(dsttmp,to);
		dst = frompos + strlen(from);
		debug("dsttmp at this point: [%s], dst: [%s]\n",dsttmp,dst);
	}
	if( dsttmp ) {
		if( strlen(dsttmp) + strlen(dst) > dstmaxlen ) {
			free(dsttmp);
			return(3);
		}
		strcat(dsttmp,dst);
		strcpy(realdst,dsttmp);
		free(dsttmp);
	}

	debug("String returned: [%s]\n",dst);

	return(0);
}

/*
 * Strip leading blanks
 */

void stripleadspaces(str)
char *str;
{
char *strptr;

	strptr = str;
	while(isspace(*strptr))
		strptr++;

	strcpy(str,strptr);

	return;
}

/* 
 * retrieve IP address of host from bb-hosts
 * 
 * If IP address cannot be found then return hostname
 * If IP address is 0.0.0.0 then return hostname unless
 *   force flag is on
 */

int getipaddr(bbhost,storestr, forceip)
char *bbhost;
char *storestr;
int forceip;
{
char *bbhosts = NULL, *fqdn = NULL;
FILE *bbhosts_fp;
char *hostptr, hostname[MAXLINE], *nextchar;
char ip_host[MAXLINE], *bbip;
int beginline, fullname, namelen;

	/* Check for stupid errors, return host name passwd as argument */

	strcpy(storestr,bbhost);

	if( bbhost == NULL ) {
		return(1);
	}
	if( strlen(bbhost) == 0 ) {
		return(1);
	}

        bbhosts = (char *) getenv("BBHOSTS");
        if( bbhosts != NULL ) {
		if( (bbhosts_fp=fopen(bbhosts,"r")) != NULL ) {
			beginline = 1;
			while( fgets(ip_host,sizeof(ip_host)-1,bbhosts_fp) != NULL ) {
				if( beginline ) {
					/* Have we read a full line ? */
					if( strlen(ip_host) == (sizeof(ip_host)-1) ) {
						beginline = 0;
					}
					stripleadspaces(ip_host);
					/* Is first column a digit ? */
					/* Expecting 255.255.255.255 hostname ... in bb-hosts */
					if( isdigit(ip_host[0]) ) {
						bbip = hostptr = ip_host;
						while( isdigit(*hostptr) || (*hostptr == '.') ) {
							hostptr++;
						}
						if( !isspace(*hostptr) ) {
							continue;
						}
						/* Put in EOS such that bbip only contains IP addr */
						*hostptr = '\0';
						hostptr++;
						strcpy(hostname,hostptr);
						stripleadspaces(hostname);
						fullname = 0;
        					fqdn = (char *) getenv("FQDN");
					        if( fqdn != NULL ) {
							if( ! strcmp(fqdn,"TRUE") ) {
								fullname = 1;
							}
						}
						namelen = strlen(bbhost);
						if( !strncmp(hostname,bbhost,namelen) ) {
							nextchar = hostname + namelen;
							if( fullname ) {
								if( ! (isspace(*nextchar) || (*nextchar == '\0')) ) {
									continue;
								}
							}
							else {
								if( ! (isspace(*nextchar) || (*nextchar == '\0') || (*nextchar == '.')) ) {
									continue;
								}
							}

							/* Return IP address if non-zero or forceip flag is on */
							if( strcmp(bbip,"0.0.0.0") || forceip ) {
								strcpy(storestr,bbip);
							}

							return(0);
						}
					}
				}
				else {
					/* Reset if we've read the last part of a line */
					if( strlen(ip_host) != (sizeof(ip_host)-1) ) {
						beginline = 1;
					}
				}
			}
			fclose(bbhosts_fp);
		}
	}

	/* made it here, not found, return original string */
	return(1);
}

int getcolor(color)
char *color;
{
	if(!strcmp(color,"red"))
		return(6);
	if(!strcmp(color,"purple"))
		return(5);
	if(!strcmp(color,"yellow"))
		return(4);
	if(!strcmp(color,"green"))
		return(3);
	if(!strcmp(color,"blue"))
		return(2);
	if(!strcmp(color,"clear"))
		return(1);
	return(0);
}
