
/**************************************************************************
 *                                                                        *
 * Copyright (c) 1996 Michael Richmond and Richard Treffers               *
 *                                                                        *
 *                    This software may be copied and distributed for     *
 *                    educational, research and not for profit services   *
 *                    provided that this copyright and statement are      *
 *                    included in all such copies.                        *
 *                                                                        *
 **************************************************************************/


/*	
	saves variables in environment table

		LET [-e] string1=string2
	or
	 	LET string
	or
		LET
	
	If no arguments then all strings are listed.
	If just a name is given, the value of the given string is printed.
	Otherwise, the value of the symbol 'string1' is set equal to 'string2'.

	If -e flag, then 'string1' is set equal to the EVALUATION of 'string2'.

	Exit with code zero, unless the user asks for a specific symbol which
	doesn't exist, or if there is some sort of catastrophic error. 
	     
	3/2/94 - if symbol doesn't exist don't print it.
	1/23/96 - added "help" option.  MWR
*/

#include "pcvista.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define PASS_ALL	0
#define LIST_ALL	1
#define EVALUATE 	2
#define PRINT       3

#ifdef PROTO
int main(int,char **);
static void evaluate_symbol(char *string);
#else
int main();
static void evaluate_symbol();
#endif

char buf[BIG_BUF];

int
main(argc, argv)
int argc;
char *argv[];
{
	int i, mode = PASS_ALL;
	FILE *fin;
	char *table,  *s;

	/* check for the special case of first argument "help" */
	if ((argc == 2) && (strcmp(argv[1], "help") == 0)) {
		printf("usage: let [-e symbol=expr] [symbol] [help]\n");
		exit(0);
	}

	if (argc == 1)
		mode = LIST_ALL;
	else if ((argv[1][0] == '-') && (argv[1][1] == 'e')) {
		mode = EVALUATE;
	}
	else {
		if (strchr(argv[1], '=') == NULL)
			mode = PRINT;
	}

	switch (mode) {
		case LIST_ALL:
			if ((table = getenv("SYM_TABLE")) == NULL)
				error(-1, "SYM_TAB not set in environment");
			if ((fin = fopen(table, "r")) != NULL)
				while (fgets(buf, NBUF, fin) != NULL)
					printf(buf);
			break;
		
		case PASS_ALL:
			for (i = 1; i < argc; i++) {	/* squash all the symbols into one */
				if (i > 1)					/* big string */
					strcat(buf, " ");
					strcat(buf, argv[i]);
				}				
			putsym(buf);
			break;

		case EVALUATE:
			evaluate_symbol(argv[2]);
			break;

		case PRINT:
			s = getsym(argv[1]);
			if (s == NULL){
				exit(1);
			}
			printf("%s\n", s);
			break;
	}

	exit(0);
}
				
/*  
	replaces the right hand side of the string with	the 
	arithmetic symbol evaluation (float)

	If there is nothing to the right of the equals sign, then
	this routine sends the whole string to 'putsym', which will
	delete the symbol from the symbol table.
*/

static void 
evaluate_symbol(string)
char *string;
{
	char *equals;
	double val;

	equals = strchr(string, '=');
	if (equals == NULL) {
		error(-1, "can't find equals sign in expression");
	}
	else if (*(equals + 1) == '\0')		/* delete symbol */
		putsym(string);
	else {
		*equals = '\0';
		val = evaluate(equals + 1);
		sprintf(buf, "%s=%.6f", string, val);
		putsym(buf);
	}
}

