/* TERMLIB: Terminal independant database.
 *
 * Module: tgetent
 *
 * Purpose: Get termcap entry for <term> into buffer at <tbuf>.
 *
 * Calling conventions: char tbuf[1024+], term=canonical name for
 *			terminal.
 *
 * Returned values: 1 = success, -1 = can't open file,
 *		    0 = can't find terminal.
 *
 * Notes
 *		Should probably supply static buffer.
 *
 *		Uses environment variables "TERM" and
 *	"TERMCAP". If TERM = term (that is, if the argument
 *	matches the environment) then it looks at TERMCAP.
 *		If TERMCAP begins with a slash, then it assumes
 *	this is the file to search rather than /etc/termcap.
 *		If TERMCAP does not begin with a slash, and it
 *	matches TERM, then this is used as the entry.
 *
 *		This could be simplified considerably for non-UNIX
 *	systems.
 */
#include <stdio.h>
#include <ctype.h>
#include "termlib.h"

/* tgetent.c (libtermlib.a)
 *
 */
#if AMIGA
#define TERMCAP "s:termcap"
#else
#define TERMCAP "/etc/termcap"
#endif

tgetent(tbuf, term)
char	*tbuf,               /* Buffer to hold termcap entry, 1024 bytes max */
	*term;                                           /* Name of terminal */
{
	char    tcbuf[32],                          /* Temp buffer to handle */
		*tc,                                     /* :tc=: entry for  */
		*tcptr = tcbuf;                          /* extended entries */
	char    *tcap = TERMCAP,              /* Default termcap file */
		*getenv();
	char    *tmp;
	FILE    *termcap;

	if((tmp=getenv("TERMCAP")) != NULL) {
		if(*tmp == '/')           /* TERMCAP = name of termcap file */
			tcap = tmp ;
		else {                    /* TERMCAP = termcap entry itself */
			int tlen = strlen(term);
			while(*tmp && *tmp != ':') {/* Check if TERM matches */
				while(*tmp == '|')
					tmp++;
				if(_match(tmp, term)==tlen) {
					strcpy(tbuf, tmp);
					tent=tbuf;
					return 1;
				} 
				else
					tmp = _find(tmp, ":|");
			}
		}
	}
	if(!(termcap=fopen(tcap, "r"))) {
		strcpy(tbuf, tcap);
		return -1;
	}

	if(getent(tbuf, term, termcap)) {
		if(tc=tgetstr("tc", &tcptr)) {              /* extended entry */
			rewind(termcap);
			if(getent(tbuf+strlen(tbuf), tc, termcap)) { 
				fclose(termcap);               /* Completed */
				return 1; 
			}
			else { 
				fclose(termcap);              /* Incomplete */
				return 0; 
			}
		} else { 
			fclose(termcap);              /* non-extended entry */
			return 1; 
		}
	} else { 
		fclose(termcap);                                /* No entry */
		return 0; 
	}
}

getent(tbuf, term, termcap)
char *tbuf, *term;
FILE *termcap;
{
	char    *tptr;
	int    tlen = strlen(term);

	while(nextent(tbuf, termcap)) {           /* For each possible entry */
		tptr = tbuf;
		while(*tptr && *tptr != ':') {    /* : terminates name field */
			while(*tptr == '|')             /* | seperates names */
				tptr++;
			if(_match(tptr, term)==tlen) {             /* FOUND! */
				fclose(termcap);
				tent=tbuf;
				return 1;
			} 
			else                           /* Look for next name */
				tptr = _find(tptr, ":|");
		}
	}

	return 0;
}

nextent(tbuf, termcap)                     /* Read 1 entry from TERMCAP file */
char    *tbuf;
FILE    *termcap;
{
	char *lbuf =                                     /* lbuf=line buffer */
	     tbuf;                        /* read lines straight into buffer */

	while(lbuf < tbuf+1024 &&                        /* There's room and */
	      fgets(lbuf, tbuf+1024-lbuf, termcap)) {        /* another line */
		int llen = strlen(lbuf);

		if(*lbuf=='#')                               /* eat comments */
			continue;
		if(lbuf[-1]==':' &&                        /* and whitespace */
		   lbuf[0]=='\t' &&
		   lbuf[1]==':') {
			strcpy(lbuf, lbuf+2);
			llen -= 2;
		}
		if(lbuf[llen-2]=='\\')                  /* and continuations */
			lbuf += llen-2;
		else {
			lbuf[llen-1]=0;           /* no continuation, return */
			return 1;
		}
	}

	return 0;                                    /* ran into end of file */
}
