/*
 * $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: getval.c 1.2 89/10/10 $@(#)vm.oh:src/oh/getval.c	1.10"

#include	<stdio.h>
#include	<string.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<ctype.h>
#include	"wish.h"
#include	"token.h"
#include	"winp.h"
#include	"form.h"
#include	"fm_mn_par.h"
#include	"objform.h"
#include	"var_arrays.h"
#include	"terror.h"
#include	"eval.h"

char *pline();

void part_expand();
extern int EV_retcode;

#define SKIPLINES TRUE
#define NONE 0
#define BQMODE 1
#define DQMODE 2

#define getac(fp, Q) ((fp) ? Getc(fp) : *(Q)++)
#define UNGetac(C, fp, Q) ((fp) ? unGetc((C), (fp)) : *(Q)--)
void evalattr();

/*
** Evaluate one of the single occurrence attributes.
*/

/* Les: replace with MACRO

char *
sing_eval(fm_mn, fldtype)
struct fm_mn *fm_mn;
int fldtype;
{
	char *fld_eval();

	return(fld_eval(&fm_mn->single, fldtype, fm_mn->seqno));
}
*/

/*
** Evaluate one of the multi-occurrence attributes.
*/

/* Les: replace with 1 line function

char *
multi_eval(fm_mn, fldno, fldtype)
struct fm_mn *fm_mn;
int fldno, fldtype;
{
	char *fld_eval();

	return(fld_eval(fm_mn->multi + fldno, fldtype, fm_mn->seqno));
}
*/

/*
** Evaluate one attribute based on a seqno.
*/
/* only called within this file */
char *
fld_eval(fld, fldtype, seqno)
struct fld *fld;
int fldtype;
int seqno;
{
	char *eval_string();
	struct attribute *attr;

	attr = fld->attrs[fldtype];

/*
** Decides whether to re_evaluate the attribute and then calls pline()
** to do it.
*/

	if (!(((attr->flags & EVAL_ONCE) && attr->seqno) ||
	   ((attr->flags & EVAL_SOMETIMES) && (attr->seqno >= seqno)))) {
		if ((attr->flags & FREEIT) && attr->cur) {
			if (((attr->flags & RETS) == RET_LIST) ||
			    ((attr->flags & RETS) == RET_ARGS))
				listfree(attr->cur);
			else
				free(attr->cur);
			attr->cur = NULL;
		}
		attr->cur = eval_string(attr->def, &attr->flags);
		attr->seqno = seqno;
	}

	return(fld->attrs[fldtype]->cur);
}

/*
** Forces reevaluation of current value for an attribute.
*/
void
de_const(fm_mn, fldno, fldtype)
struct fm_mn *fm_mn;
int fldno, fldtype;
{
	fm_mn->multi[fldno].attrs[fldtype]->seqno = 0;
}
	
void
set_def(attr, str)
struct attribute *attr;
char *str;
{
	attr->def = str;
}

/*
** Set current value of an attribute (only used in "value" field of
** form).
*/
void
set_cur(fm_mn, fldno, fldtype, str)
register struct fm_mn *fm_mn;
register int fldno, fldtype;
char *str;
{
	struct attribute *attr;

	attr = fm_mn->multi[fldno].attrs[fldtype];
	if ((attr->flags & FREEIT) && attr->cur) {
		if (((attr->flags & RETS) == RET_LIST) ||
		    ((attr->flags & RETS) == RET_ARGS))
			listfree(attr->cur);
		else
			free(attr->cur);
	}
	attr->cur = str;
	attr->seqno = 1;
	attr->flags |= EVAL_ONCE;
}

/*
** Set current value of an attribute (only used in "text" objects) 
*/
void
set_sing_cur(fm_mn, desctype, str)
register struct fm_mn *fm_mn;
register int desctype;
char *str;
{
	struct attribute *attr;

	attr = fm_mn->single.attrs[desctype];
	if ((attr->flags & FREEIT) && attr->cur) {
		if (((attr->flags & RETS) == RET_LIST) ||
		    ((attr->flags & RETS) == RET_ARGS))
			listfree(attr->cur);
		else
			free(attr->cur);
	}
	attr->cur = str;
	attr->seqno = 1;
	attr->flags |= EVAL_ONCE;
}

/*
** Get default value of an attribute
*/
char *
get_def(fm_mn, fldno, fldtype) 
register struct fm_mn *fm_mn;
register int fldno, fldtype;
{
	return(fm_mn->multi[fldno].attrs[fldtype]->def);
}

/*
** Free a list of strings generated by parselist.
*/
listfree(list)
char **list;
{
	int i;
	int	lcv;

	if (!list)
		return;
	lcv = array_len(list);
	for (i = 0; i < lcv; i++)
		free(list[i]);
	array_destroy(list);
}
