
/*
 *  CONFIG.C
 *
 *  $Header: Beta:src/uucp/src/lib/RCS/config.c,v 1.1 90/02/02 12:08:37 dillon Exp Locker: dillon $
 *
 *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
 *
 *  Extract fields from UULIB:Config
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "config.h"

Prototype char *FindConfig(const char *);
Prototype char *GetConfig(const char *, char *);
Prototype char *GetConfigDir(char *);
Prototype char *GetConfigProgram(char *);
Prototype char *MakeConfigPath(const char *, const char *);
Prototype char *MakeConfigPathBuf(char *, const char *, const char *);
Prototype char *MallocEnviro(const char *);
Prototype FILE *openlib(const char *);
Prototype FILE *openlib_write(const char *);

#define CTLZ	('z'&0x1F)

static char *ConfBuf = NULL;

char *
FindConfig(field)
const char *field;
{
    char *str;
    short flen = strlen(field);

    if (ConfBuf == NULL) {
	FILE *fi;
	fi = fopen("S:UUConfig", "r");
	if (fi == NULL)
	    fi = fopen("UULIB:Config", "r");
	if (fi) {
	    long buflen;
	    fseek(fi, 0L, 2);
	    buflen = ftell(fi);
	    fseek(fi, 0L, 0);
	    if (buflen > 0 && (ConfBuf = malloc(buflen + 1))) {
		fread(ConfBuf, buflen, 1, fi);
		ConfBuf[buflen] = CTLZ;     /*	can't use \0 */
		for (str = ConfBuf; *str && *str != CTLZ; ++str) {
		    char *bup;
		    if (*str == '\n') {     /*  make separate strs */
			*str = 0;
					    /*	remove white space at end */
			for (bup = str - 1; bup >= ConfBuf && (*bup == ' ' || *bup == 9); --bup)
			    *bup = 0;
		    }
		}
	    } else {
		ConfBuf = NULL;
	    }
	} else {
	    fprintf(stderr, "Couldn't open S:UUConfig or UULIB:Config\n");
	}
    }
    if (ConfBuf == NULL)
	return(NULL);
    /*
     *	Search ConfBuf for Field<space/tab>
     */

    for (str = ConfBuf; *str != CTLZ; str += strlen(str) + 1) {
	if (*str == 0 || *str == '#')
	    continue;
	if (strncmp(str, field, flen) == 0 && (str[flen] == ' ' || str[flen] == '\t')) {
	    str += flen;
	    while (*str == ' ' || *str == 9)
		++str;
	    return(str);
	}
    }
    return(NULL);
}

char *
GetConfig(field, def)
const char *field;
char *def;
{
    char *result = FindConfig(field);

    if (result == NULL)
	result = def;
    return(result);
}

char *
GetConfigDir(field)
char *field;
{
    char *result = FindConfig(field);

    if (result == NULL)
	result = field + strlen(field) + 1;
    return(result);
}

char *
GetConfigProgram(field)
char *field;
{
    char *result = FindConfig(field);
    if (result == NULL)
	result = field;
    return(result);
}

char *
MakeConfigPath(field, trailer)
const char *field;
const char *trailer;
{
    static char Buf[512];
    return(MakeConfigPathBuf(Buf, field, trailer));
}

char *
MakeConfigPathBuf(buf, field, trailer)
char *buf;
const char *field;
const char *trailer;
{
    char *result = GetConfigDir(field);
    short len = strlen(result) - 1;

    if (len > 0 && result[len] == '/' || result[len] == ':')
	sprintf(buf, "%s%s", result, trailer);
    else
	sprintf(buf, "%s/%s", result, trailer);
    return(buf);
}


char *
MallocEnviro(envname)
const char *envname;
{
    int fd;
    int len;
    char *tmp = malloc(strlen(envname) + 32);

    sprintf(tmp, "ENV:%s", envname);
    fd = open(tmp, O_RDONLY);
    free(tmp);

    if (fd < 0)
	return(NULL);
    len = lseek(fd, 0L, 2);
    lseek(fd, 0L, 0);
    if (len >= 0) {
	tmp = malloc(len + 1);
	read(fd, tmp, len);
	tmp[len] = 0;
    } else {
	tmp = NULL;
    }
    close(fd);
    return(tmp);
}

FILE *
openlib(filename)
const char *filename;
{
    return (fopen(MakeConfigPath(UULIB, filename), "r"));
}

FILE *
openlib_write(filename)
const char *filename;
{
    return (fopen(MakeConfigPath(UULIB, filename), "w"));
}

