/* The following software is (C) 1984 Peter da Silva,
   the Mad Australian, in the public domain. It may
   be re-distributed for any purpose with the inclusion
   of this notice. */
/* TERMLIB: Terminal independant database.
 *
 * Module: tgetstr
 *
 * Purpose: get terminal capability string from database.
 *
 * Calling conventions: id is the two character capability id.
 *			(*buf) points into a hold buffer for the
 *			id. the capability is copied into the buffer
 *			and (*buf) is advanced to point to the next
 *			free byte in the buffer.
 *
 * Returned values: 0 = no such entry, otherwise returns original
 *		    (*buf) (now a pointer to the string).
 *
 * Notes
 *		It also decodes certain escape sequences in the buffer.
 *	they should be obvious from the code:
 *		\E = escape.
 *		\n, \r, \t, \f, \b match the 'c' escapes.
 *		^x matches control-x (^@...^_).
 *		\nnn matches nnn octal.
 *		\x, where x is anything else, matches x. I differ
 *	from the standard library here, in that I allow ^: to match
 *	:.
 *
 */
#include <stdio.h>
#include <ctype.h>
#include "termlib.h"

/* tgetstr.c (libtermlib.a)
 *
BUILD
OBJS=tinit.o tutil.o tvars.o \
     tgoto.o tputs.o tgetent.o tgetflag.o tgetnum.o tgetstr.o
CFLAGS=-O

libtermlib.a: $(OBJS) termlib.h
	ar cr libtermlib.a $(OBJS)
	ranlib libtermlib.a

$(OBJS):: termlib.h
END
*/

char *
tgetstr(id, buf)
char    *id, **buf;
{
	int    len = strlen(id);
	char *tmp=tent;
	char *hold;

	do {
		tmp = _find(tmp, ":");                     /* For each field */
		while(*tmp==':')                        /* skip empty fields */
			tmp++;
		if(!*tmp)
			break;

		if(_match(id, tmp)==len) {
			tmp += len;                   /* find '=' '@' or '#' */
			if(*tmp=='@')                  /* :xx@: entry for tc */
				return 0;                   /* deleted entry */
			hold= *buf;
			while(*++tmp && *tmp != ':') {/* not at end of field */
				switch(*tmp) {
				case '\\':            /* Expand escapes here */
					switch(*++tmp) {
					case 0:        /* ignore backslashes */
						tmp--;    /* at end of entry */
						break;   /* shouldn't happen */
					case 'e':
					case 'E':                     /* ESC */
						*(*buf)++ = '\033'; 
						break;
					case 'n':                      /* \n */
						*(*buf)++ = '\n'; 
						break;
					case 'r':                      /* \r */
						*(*buf)++ = '\r'; 
						break;
					case 't':                      /* \t */
						*(*buf)++ = '\t'; 
						break;
					case 'b':                      /* \b */
						*(*buf)++ = '\b'; 
						break;
					case 'f':                      /* \f */
						*(*buf)++ = '\f'; 
						break;
					case '0':                    /* \nnn */
					case '1': 
					case '2': 
					case '3': 
					case '4':
					case '5': 
					case '6': 
					case '7': 
					case '8': 
					case '9':
						**buf = 0;
						while(isdigit(*tmp))
							**buf = **buf * 8 + *tmp++ - '0';
						(*buf)++;
						tmp--;
						break;
					default:      /* \x, for all other x */
						*(*buf)++= *tmp;
					}
					break;
				case '^':              /* control characters */
					*(*buf)++ = *++tmp - '@'; 
					break;
				default: 
					*(*buf)++ = *tmp;
				}
			}
			*(*buf)++ = 0;
			return hold;
		}
	} while(*tmp);

	return 0;
}
