ide_main.c 2.56 KB
/*
 * Interactive Diagnostic Environment Command Line Interpreter
 *
 */

#include <stdio.h>
#include "signal.h"
#include "ide.h"
#include <getopt.h>

int goteof = 0;

extern char	yyinbuff[];
extern int	yyinlen;
extern int	yyinbp;
extern char	ide_startup[];
extern int	inloop;   /* semantic feedback: the parser is inside a loop */
extern int	inblock;  /* ditto for block {} */
extern int	inswitch; /* ditto for switch */
extern int	inif;     /* ditto for if */
extern int 	ide_dosource;

char	*ide_pstartup = ide_startup;
char	*ide_sname;

int	ide_cancelstartup;

jmp_buf		intr_jump;

void onsigint();

static void (* ide_clean)(void) = 0;

void ide_set_clean(void (* user_clean)(void))
{
	ide_clean = user_clean;
}

main(argc,argv)
int argc;
char *argv[];
{
	int val;

	ide_init();

	init_context(argc,argv);

	if (argc == 1) {
		sigset(SIGINT,onsigint);
		if (val = setjmp(intr_jump)) {
			/*
			 * Reset the global optind, in case the test 
			 * used getopt() to parse its option arguments.
			 *
			 * We would ordinarily do this when the test exited
			 * cleanly, but we need to do it here in case the user
			 * aborted the test with an interrupt (e.g. ctrl-c).
			 */
			optind = 1;

			if ((val == 1) && (ide_clean)) ide_clean();
			printf("\n<Stopped>\n");
			ide_cancelstartup = 1;
			yyinlen = 0;
			handle_interrupt();
			sigrelse(SIGINT);
		}
	}

	while ( ! goteof )
		yyparse();
	
	putchar('\n');

	_doexit(0);
}
/*
 * Get input from somewhere
 *
 * ide supports three different types of input:
 *  1. From a startup script linked in at compile time
 *  2. From the keyboard
 *  3. From a remote device via the "source" command
 *
 */
int
ide_refill()
{
	if ( !ide_cancelstartup && *ide_pstartup ) {
		ide_sname = "startup script";
		if ( (yyinlen = strlen(ide_pstartup)) >= MAXLINELEN )
			strncpy(yyinbuff,ide_pstartup, yyinlen = MAXLINELEN-1 );
		else
			strcpy( yyinbuff, ide_pstartup );
		ide_pstartup += yyinlen;
	} else {
		if ( !(ide_dosource && (yyinlen = getsfromsource(yyinbuff))) ) {
			ide_sname = 0;
			if ( inloop || inswitch || inblock || inif ) {
				sym_t *psym = findsym("PS2");
				if ( psym )
					ide_puts(psym->sym_s);
				else
					ide_puts("> ");
			} else	{
				sym_t *psym = findsym("PS1");
				if ( psym )
					ide_puts(psym->sym_s);
				else
					ide_puts("! ");
			}

#ifdef __sgi__
			if ( ! gets(yyinbuff) )
#else
			if ( ! fgets(yyinbuff, MAXLINELEN, stdin) )
#endif
				return EOF;
			yyinlen = strlen(yyinbuff);
			yyinbuff[yyinlen++] = '\n';
		}
	}

	yyinbp = 0;
	yyinbuff[yyinlen] = '\0';

	return yyinbuff[yyinbp++];
}

void
onsigint()
{
	longjmp(intr_jump, 1);
}