/* ********************************************************************** *\
 *         Copyright IBM Corporation 1988,1991 - All Rights Reserved      *
\* ********************************************************************** */

/*
	$Disclaimer: 
 * Permission to use, copy, modify, and distribute this software and its 
 * documentation for any purpose is hereby granted without fee, 
 * provided that the above copyright notice appear in all copies and that 
 * both that copyright notice, this permission notice, and the following 
 * disclaimer appear in supporting documentation, and that the names of 
 * IBM, Carnegie Mellon University, and other copyright holders, not be 
 * used in advertising or publicity pertaining to distribution of the software 
 * without specific, written prior permission.
 * 
 * IBM, CARNEGIE MELLON UNIVERSITY, AND THE OTHER COPYRIGHT HOLDERS 
 * DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT 
 * SHALL IBM, CARNEGIE MELLON UNIVERSITY, OR ANY OTHER COPYRIGHT HOLDER 
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 *  $
*/

/* nessrun.c
	run a ness program

	first argument is file name containing the ness program
		it must have a function main, which will be called
		to initiate execution
	remaining args are concatenated and passed as a single parameter to main()

*/

/*
 * $Log: nessrun.ci,v $
 * Revision 1.22  1994/03/01  23:05:54  rr2b
 * Commented out text after #endif's
 * BUG
 *
 * Revision 1.21  1993/08/25  20:40:22  susan
 * backed out Telmat changes
 *
 * Revision 1.20  1993/08/20  20:23:09  susan
 * Changes submitted by Telmat Informatique
 *
 * Revision 1.19  1993/05/04  01:23:55  susan
 * RCS Tree Split
 *
 * Revision 1.17.1.3  1993/02/09  01:47:54  wjh
 * fixed syntax error
 *
 * Revision 1.17.1.2  1993/02/08  17:34:02  wjh
 * switch from lex/lexdef to parse/lexan/tlex
 *
 * Revision 1.17.1.1  1993/02/02  03:03:22  rr2b
 * new R6tape branch
 *
 * Revision 1.17  1992/12/15  01:10:18  rr2b
 * fix disclaimerization
 * .
 *
 * Revision 1.16  1992/12/08  04:13:36  wjh
 * remove useless reference to xgetchar.h
 * .
 *
 * Revision 1.15  1992/11/26  02:42:25  wjh
 * converted CorrectGetChar to GetUnsignedChar
 * moved ExtendShortSign to interp.h
 * remove xgetchar.h; use simpletext_GetUnsignedChar
 * nessrun timing messages go to stderr
 * replaced curNess with curComp
 * replaced genPush/genPop with struct compilation
 * created compile.c to handle compilation
 * moved scope routines to compile.c
 * converted from lex to tlex
 * convert to use lexan_ParseNumber
 * truncated logs to 1992 only
 * use bison and gentlex instead of yacc and lexdef/lex
 *

... log truncated  Dec 92   -wjh

 * 
 * Revision 1.0  88/07/11  09:57:23  wjh
 * Created from nesstest.c
 */


#include <stdio.h>
#include <system.h>
#include <event.ih>
#include <mark.ih>
#include <nessmark.ih>
#include <ness.ih>
#include <filetype.ih>
#include <text.ih>
#include <textv.ih>
#include <error.h>

/* include .ih files to be loaded with the binary of the file but none of whose
		methods are called in this file itself */
#define class_StaticEntriesOnly
#	include <observe.ih>
#	include <proctbl.ih>
#	include <dataobj.ih>
#	include <im.ih>

#if defined(GOODNESSRUN) || defined(STATICNESS)
#	include <parse.ih>
#	include <lexan.ih>
#	include <tlex.ih>
#	include <sym.ih>
#	include <toksym.ih>
#	include <nesssym.ih>
#endif

#ifdef GOODNESSRUN
#	include <cursor.ih>
#	include <stylesht.ih>
#	include <nstdmark.ih>
#	include <envrment.ih>
#	include <bind.ih>
#	include <keyrec.ih>
#	include <updlist.ih>
#	include <atom.ih>
#	include <winsys.ih>
#	include <menulist.ih>
#	include <keymap.ih>
#	include <environ.ih>
#	include <namespc.ih>
#	include <view.ih>

#	include <graphic.ih>
#	include <fontdesc.ih>
#	include <keystate.ih>
#	include <style.ih>
#	include <lpair.ih>
#	include <scroll.ih>
#	include <matte.ih>
#	include <atomlist.ih>

/* there is no library for the following */
/*
#	include <cel.ih>
#	include <arbiterv.ih>
#	include <celv.ih>
#	include <valuev.ih>
*/

#ifdef WM_ENV
#	include <wws.ih>
#	include <mrl.ih>
#	include <wcursor.ih>
#	include <wgraphic.ih>
#	include <wfontd.ih>
#	include <wim.ih>
#endif /* WM_ENV */

/* if one were really trying to get arbiterview, too, one would need also all of the following
*	at present, goodnessrun gets a non-fatal error when it calls
*		arbiterview_GetNamedView()
*		if the version is out-of-date
*	#include <dict.ih>
*	#include <rm.ih>
*	#include <arbiter.ih>
*	#include <lsetv.ih>
*	#include <message.ih>
*	#include <complete.ih>
*	#include <arbcon.ih>
*	#include <buffer.ih>
*/
#endif /* GOODNESSRUN */

#undef class_StaticEntriesOnly

struct ness *program;


	void
dumpall()
{
	struct ness *n;
	unsigned char *name;
	for (n = ness_GetList(); n != NULL; n = ness_GetNext(n)) {
		name = ness_GetName(n);
		printf("\nObject code for %s\n", (name != NULL) ? name : (unsigned char *)"unknown");
		ness_dumpattrs(n, stdout);
	}
}


main(argc, argv)
	register int  argc;
	register unsigned char  **argv;
{
	struct errornode *result;
	struct nessmark *arg, *args, *blank;
	long t0, t1;
	unsigned char *fnm;
	boolean debug = FALSE;
	struct text *text;
	struct textview *textview;

	class_Init(AndrewDir("/dlib/atk"));

	observable_StaticEntry;
	proctable_StaticEntry;
	dataobject_StaticEntry;
	im_StaticEntry;

#if defined(GOODNESSRUN) || defined(STATICNESS)
	ness_StaticEntry;
	nessmark_StaticEntry;
	parse_StaticEntry;
	lexan_StaticEntry;
	tlex_StaticEntry;
	sym_StaticEntry;
	toksym_StaticEntry;
	nesssym_StaticEntry;
#endif

#ifdef GOODNESSRUN
	/* statically load whatever we need, so we can run the binary even while
	the system is under development */

	text_StaticEntry;
	filetype_StaticEntry;
	simpletext_StaticEntry;
	cursor_StaticEntry;
	event_StaticEntry;
	stylesheet_StaticEntry;
	nestedmark_StaticEntry;
	environment_StaticEntry;
	textview_StaticEntry;
	bind_StaticEntry;
	keyrec_StaticEntry;
	updatelist_StaticEntry;
	atom_StaticEntry;
	windowsystem_StaticEntry;
	menulist_StaticEntry;
	keymap_StaticEntry;
	environ_StaticEntry;
	namespace_StaticEntry;
	mark_StaticEntry;
	view_StaticEntry;

	graphic_StaticEntry;
	fontdesc_StaticEntry;
	keystate_StaticEntry;
	style_StaticEntry;
	lpair_StaticEntry;
	scroll_StaticEntry;
	matte_StaticEntry;
	atomlist_StaticEntry;
/*
	cel_StaticEntry;
	arbiterview_StaticEntry;
	celview_StaticEntry;
	valueview_StaticEntry;
*/


#ifdef WM_ENV
	wmws_StaticEntry;
	mrl_StaticEntry;
	wmcursor_StaticEntry;
	wmgraphic_StaticEntry;
	wmfontdesc_StaticEntry;
	wmim_StaticEntry;
#endif /* WM_ENV */
#endif /* GOODNESSRUN */

	fnm = NULL;
	argv++, argc--;		/* skip "nessrun" */
	while (argc-- > 0) {
		unsigned char *arg = *argv++;
		if (*arg == '-')
			switch (*(arg+1)) {
			case 'd': /* Debugging. */
				debug = TRUE;
				break;
			default: 
				printf("\"%s\" is invalid. The only valid switches are r, o, y, g, b, A, d\n",
						arg);
				break;
			}
		else { /* It's a file, right? */
			fnm = arg;
			break;	/* remaining args go the the ness program */
		}
	}

	text = text_New();
	textview = textview_New();
	textview_SetDataObject(textview, text);

	program = ness_New();
	ness_SetDefaultText(program, textview);
	ness_SetName(program, fnm);
	ness_SetAccessLevel(program, ness_codeUV);

	if (fnm == NULL) {
		printf("missing argument: <filename>.n\n");
		exit(1);
	}
	else if (ness_ReadNamedFile(program, fnm) != dataobject_NOREADERROR)
		exit(1);

			t0 = event_TUtoMSEC(event_Now());
	result = ness_Compile(program);
			t1 = event_TUtoMSEC(event_Now());

	if (debug)
		dumpall();

	if (ness_PrintAllErrors("Compile") != 0)
		exit(1);
	else printf ("Compile okay.  Elapsed time is %d msec.\n", t1-t0);


	blank = nessmark_New();
	nessmark_MakeConst(blank, " ");
	arg = nessmark_New();
	args = nessmark_New();
	nessmark_SetText(args, simpletext_New());
	while (argc-- > 0) {
		nessmark_MakeConst(arg, *argv);
		nessmark_Next(args);
		nessmark_Replace(args, arg);
		nessmark_Next(args);
		nessmark_Replace(args, blank);
		argv++;
	}
	nessmark_Base(args);
	ness_SupplyMarkerArg(program, args);

			t0 = event_TUtoMSEC(event_Now());
	result = ness_Execute(program, "main");
			t1 = event_TUtoMSEC(event_Now());

	if (result != NULL) 
		ness_PrintAllErrors("Execution");
	else
		printf ("Execution okay.  Elapsed time is %d msec.\n", t1-t0);
	exit((result == NULL) ? 0 : 1);
}

