/*
	mak_cfg.c -- 	get configuration information
						for the makemak program
*/

/*	Last revised : Sun February 21, 1988 at 1:14:57 pm*/
/* Loran W. Richardson */

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	"makemak.h"

char defaults[LIBS + 1][SLEN];
char mak_defs[LIBS + 1][FNAMELEN];

int mak_cfg(char *pname)
{
	char line[SLEN];
	char temp[SLEN];
	char path[_MAX_PATH];
	char *s;
	FILE *fp;
	FLAGS i;

/* set up header strings */
	strcpy(mak_defs[TARGET], "target");
	strcpy(mak_defs[CC], "cc");
	strcpy(mak_defs[C_OPTS], "c_opts");
	strcpy(mak_defs[CDEFS] ,"cdefs");
	strcpy(mak_defs[MC], "mc");
	strcpy(mak_defs[M_OPTS], "m_opts");
	strcpy(mak_defs[MDEFS], "mdefs");
	strcpy(mak_defs[LINK], "link");
	strcpy(mak_defs[L_OPTS], "l_opts");
	strcpy(mak_defs[LMAP], "lmap");
	strcpy(mak_defs[LIBS], "libs");

/* set up the defaults */

	strcpy(defaults[CC],	"cl");
	strcpy(defaults[C_OPTS], "/Zi /Od /c");
	strcpy(defaults[CDEFS],	"");
	strcpy(defaults[MC],	"masm");
	strcpy(defaults[M_OPTS], "/Zi");
	strcpy(defaults[MDEFS],	"");
	strcpy(defaults[LINK], "link");
	strcpy(defaults[L_OPTS], "/CO");
	strcpy(defaults[LMAP], "NUL");
	strcpy(defaults[LIBS], "");




/* get the environment settings */
	if ((s = getenv("CL")) != NULL)
		strcpy(defaults[C_OPTS], s);

	if ((s = getenv("MASM")) != NULL)
		strcpy(defaults[M_OPTS], s);

	if ((s = getenv("LINK")) != NULL)
		strcpy(defaults[L_OPTS], s);

/* get the configuration file values */
	_searchenv("makemak.cfg", "CONFIG", path);	/* find the configuration file */
	if (path[0] != '\0')
		if ((fp = fopen(path, "r")) != NULL)
			while ((s = fgets(line, SLEN, fp)) != NULL) /* read in a line */
				{
				strcpy(temp, strtok(line, " \t\n="));	/* get the first word */
				for (i = CC; i <= LIBS; ++i)	/* loop through the options */
					if (strcmpi(temp, mak_defs[i]) == 0) /* word matches option ? */
						{
						strcpy(defaults[i], strtok(NULL, "\n")); /* copy the rest of the line */
						if (defaults[i][0] == '=')					  /* into the defaults */
							strcpy(defaults[i], (defaults[i]) + 1);
						break;
						}
				}
	return(0);
}
