// 
// smeg - A satellite modelling and prediction tool
// Copyright (C) 1999  Tom Rothamel
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

#include <malloc.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "sts2.h"


extern "C" {
#include <readline/readline.h>	
#include <readline/history.h>	
}


struct proc {
	struct proc *next;
	char *name;
	char *body;
};

struct proc *procs = NULL;

void DefProc(char *name, char *body) {
	struct proc *np;

	np = (struct proc *) calloc(sizeof(struct proc), 1);

	np->next = procs;
	np->name = name;
	np->body = body;

	procs = np;
}

void CallProc(char *name) {
	struct proc *cur;

	cur = procs;

	while (cur) {
		if (!strcmp(name, cur->name)) {
			ParseString(cur->body);
			return;
		}
		cur = cur->next;
	}

	fprintf(stderr, "Error: the procedure '%s' isn't defined.\n", name);
}


void quit() {
	exit(0);
}

int main(int argc, char **argv) {
	char *read;
	char input[512];
	FILE *rcfile;
	
	CreateSun();

	snprintf(input, 512, "%s/.smeg1rc", getenv("HOME"));
	rcfile = fopen(input, "r");

	if (rcfile) {
		while(fgets(input, 512, rcfile)) {
			if (input[0] == '#') continue;
			ParseString(input);
		}

		fclose(rcfile);
	}
		
	
	while (read = readline("smeg> ")) {
		if (*read) add_history(read);
		ParseString(read);
	}
	quit();
}

