DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T e

⟦abd723f81⟧ TextFile

    Length: 2769 (0xad1)
    Types: TextFile
    Names: »expand.c«

Derivation

└─⟦2d1937cfd⟧ Bits:30007241 EUUGD22: P.P 5.0
    └─⟦dc59850a2⟧ »EurOpenD22/pp5.0/pp-5.tar.Z« 
        └─⟦e5a54fb17⟧ 
            └─⟦this⟧ »pp-5.0/Lib/util/expand.c« 

TextFile

/* expand.c */

# ifndef lint
static char Rcsid[] = "@(#)$Header: /cs/research/pp/hubris/pp-beta/Lib/util/RCS/expand.c,v 5.0 90/09/20 16:17:15 pp Exp Locker: pp $";
# endif

/*
 * $Header: /cs/research/pp/hubris/pp-beta/Lib/util/RCS/expand.c,v 5.0 90/09/20 16:17:15 pp Exp Locker: pp $
 *
 * $Log:	expand.c,v $
 * Revision 5.0  90/09/20  16:17:15  pp
 * rcsforce : 5.0 public release
 * 
 */



#include "util.h"
#include "expand.h"

/*
 *			E X P A N D . C
 *
 *	Expand expands a text string expanding macros provided
 *  in a vector of key and value pairs.
 *
 *  e.g.	expand(buf, "$A+$B=$(RESULT)", array)
 *  with	Expand array[] = {"A", "1",
 *				 "B", "2",
 *				 "RESULT", "3",
 *				 0, 0};
 *  gives	"1+2=3" in buf.
 *
 *  wja@uk.ac.nott.maths		Wed Feb 22 15:00:26 GMT 1984
 *  DPK@BRL, 13 Apr 84	Made more portable and changed array usage.
 *  jpo@cs.nott.ac.uk 18 July 1989	Changed to structure.
 *  pac@cs.nott.ac.uk 			Mon Mar 12 19:54:39 1990
 *					added expand_dyn
 */


static  char *exname(), *exlookup();

#define	INC	BUFSIZ

char *expand_dyn(fmt, macros)
char	*fmt;
Expand	macros[];
{
	register char *bp = NULLCP, *cp;
	int	ix = 0, len = 0;
	char	name[LINESIZE];

	bp = (char *) malloc(INC * sizeof(*bp));
	len += INC;

	while (fmt && *fmt) {
		if (ix >= len) {
			/* resize */
			len += INC;
			bp = (char *) realloc(bp, (unsigned)(len*sizeof(char)));
		}
		if (*fmt != '$') 
			bp[ix++] =  *fmt++;
		else if (*++fmt == '$')
			bp[ix++] = *fmt++;
		else {
			fmt = exname(name, fmt);
			cp = exlookup (name, macros);
			while (cp && *cp) {
				if (ix >= len) {
					/* resize */
					len += INC;
					bp = (char *) realloc(bp, 
							      (unsigned)(len*sizeof(char)));
				}
				bp[ix++] = *cp++;
			}
		}
	}
	if (ix >= len) {
		/* resize */
		len += INC;
		bp = (char *) realloc(bp, (unsigned)(len*sizeof(char)));
	}
	bp[ix] = '\0';
	len = strlen(bp);
	bp = (char *) realloc(bp, (unsigned) (len*sizeof(char)));
	return bp;
}

char *expand(buf, fmt, macros)
char *buf, *fmt;
Expand macros[];
{
	register char *bp = buf, *cp;
	char name[LINESIZE];

	while(fmt && *fmt) {
		if(*fmt != '$')
			*bp++ = *fmt++;
		else if(*++fmt == '$')
			*bp++ = *fmt++;
		else {
			fmt = exname(name, fmt);
			cp = exlookup(name, macros);
			while(cp && *cp)
				*bp++ = *cp++;
		}
	}
	*bp = '\0';
	return buf;
}

static char *
exname(buf, str)
register char *buf, *str;
{
	if(*str == '(') {
		str++;
		while(*str != ')' && *str != '\0')
			*buf++ = *str++;
		if(*str != '\0')
			str++;
	}
	else
		*buf++ = *str++;
	*buf = '\0';
	return str;
}

static char *
exlookup(name, list)
register char *name;
Expand list[];
{
	register Expand *ep;

	for (ep = list; ep -> macro; ep++) {
		if(lexequ(name, ep -> macro) == 0)
			return (ep -> expansion);
	}
	return ("");
}