/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred."
 *
 */
/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

/*
 * Copyright  (c) 1985 AT&T
 *	All Rights Reserved
 */
#ident	"$Header: expand.c 1.2 89/10/10 $@(#)vm:sys/expand.c	1.1"

#include	<stdio.h>
#include	<string.h>
#include	<ctype.h>
#include	"wish.h"
#include	"moremacros.h"

char *
expand(name)
char	*name;
{
	char	buf[BUFSIZ];
	char	*pexpand();

	(void) pexpand(buf, name, '\0');
	return strsave(buf);
}

static char *
pexpand(buf, name, eos)
char	*buf;
char	*name;
char	eos;
{
	register char	delim;
	register char	*src;
	register char	*dst;
	register char	*file;
	char	fbuf[PATHSIZ];
	char	*anyenv();
	char	*getepenv();

	dst = buf;
	src = name;
	while (*src && *src != eos) {
		if (*src == '\\') {
			++src;
			*dst++ = *src++;
		}
		else if (*src == '$') {
			register char	*start;

			if ((delim = (*++src == '{') ? '}' : '\0'))
				start = ++src;
			else
				start = src;
			file = NULL;
			if (*src == '(') {
				src = pexpand(fbuf, ++src, ')');
				if (*src) {
					start = ++src;
					file = fbuf;
				}
			}
			if (isalpha(*src)) {
				register char	*p;
				register char	savechar;

				while (isalpha(*src) || isdigit(*src) || *src == '_')
					src++;
				savechar = *src;
				*src = '\0';
				if ((p = (file ? anyenv(file, start) : getepenv(start))) == NULL) {
					if (delim) {
						if ((*src = savechar) == ':' && *++src == '-')
							while (*++src && *src != delim)
								*dst++ = *src;
					}
					else
						*src = savechar;
				}
				else {
					*src = savechar;
					strcpy(dst, p);
					dst += strlen(p);
					free(p);
				}
				if (delim)
					while (*src && *src++ != delim)
						;
			}
		}
		else
			*dst++ = *src++;
	}
	*dst = '\0';
	return src;
}
