h14867
s 00021/00009/00141
d D 1.11 80/10/29 11:23:01 lcd 11 10
c Fixed ! command
e
s 00002/00000/00148
d D 1.10 80/09/09 16:06:27 lcd 10 9
c dded PREV and NEXT
e
s 00022/00040/00126
d D 1.9 80/09/09 15:05:53 lcd 9 8
c Added next command
e
s 00002/00002/00164
d D 1.8 80/09/09 02:21:07 lcd 8 7
c Changed "struct" command to "table"
e
s 00000/00060/00166
d D 1.7 80/09/08 23:00:11 lcd 7 6
c Moved struct code to struct.c
e
s 00014/00009/00212
d D 1.6 80/09/07 15:20:31 lcd 6 5
c Added GST structure
e
s 00058/00000/00163
d D 1.5 80/09/07 13:20:04 lcd 5 4
c Added structure print out command
e
s 00041/00001/00122
d D 1.4 80/09/07 10:04:19 lcd 4 3
c Added shell escape for general commands
e
s 00035/00001/00088
d D 1.3 80/09/05 16:03:07 lcd 3 2
c Added ? and ! commands
e
s 00006/00000/00083
d D 1.2 80/05/15 14:35:41 lcd 2 1
c Added instructions for building
e
s 00083/00000/00000
d D 1.1 80/05/15 14:27:08 lcd 1 0
e
u
sunix
lcd
U
t
T
I 1
#

/*
 *	main.c: main loop of ksosdb
I 2
 *
 *
 *	Build by get(I)'ing all the source files into
 *		a directory. Then execute:
 *
D 3
 *			cc *.c -lS -lpw
E 3
I 3
 *			cc *.c -lS -lpw -lPW
E 3
E 2
 */

#include "globals.h"
#include <stdio.h>

char	*_S_main	"%W%";

PROCESS main( argc, argv )

VAR	char	**argv;
	integer	argc;

BEGIN	initialize( argc, argv );

	WHILE command != QUIT
	DO	printf("> ");
		command = getcmd();

		CASE command OF
D 9
		case NOCMD	: BEGIN
					printf("?\n");
E 9
I 9
		case NOCMD	: BEGIN printf("?\n");
E 9
				  ENDCASE;
D 9
		case HELP	: BEGIN
					help();
E 9
I 9
		case HELP	: BEGIN help();
E 9
				  ENDCASE;
D 9
		case QUIT	: BEGIN
					cleanup();
E 9
I 9
		case QUIT	: BEGIN cleanup();
E 9
				  ENDCASE;
D 9
		case DOT	: BEGIN
					display( addr-dsplysz, addr );
E 9
I 9
		case DOT	: BEGIN display( addr-dsplysz, addr );
E 9
				  ENDCASE;
D 9
		case NL		: BEGIN
					addr = addr + step;
E 9
I 9
		case NL		: BEGIN addr = addr + step;
E 9
					display( addr, addr );
				  ENDCASE;
D 9
		case DISPLAY	: BEGIN
					addr = addr+step;
E 9
I 9
		case DISPLAY	: BEGIN addr = addr+step;
E 9
					display( addr, addr+dsplysz );
					addr = addr+dsplysz;
				  ENDCASE;
D 9
		case BACK	: BEGIN
					addr = addr - step;
E 9
I 9
		case BACK	: BEGIN addr = addr - step;
E 9
				  ENDCASE;
D 9
		case EQUAL	: BEGIN
					prtdot();
E 9
I 9
		case EQUAL	: BEGIN prtdot();
E 9
				  ENDCASE;
D 9
		case LOOK	: BEGIN
					look();
E 9
I 9
		case LOOK	: BEGIN look();
E 9
				  ENDCASE;
D 9
		case TEXT	: BEGIN
					text();
E 9
I 9
		case TEXT	: BEGIN text();
E 9
				  ENDCASE;
D 9
		case DATA	: BEGIN
					data();
E 9
I 9
		case DATA	: BEGIN data();
E 9
				  ENDCASE;
D 9
		case LABEL	: BEGIN
					label();
E 9
I 9
		case LABEL	: BEGIN label();
E 9
				  ENDCASE;
D 9
		case NAME	: BEGIN	
					fndlab();
E 9
I 9
		case NAME	: BEGIN	fndlab();
E 9
				  ENDCASE;
D 9
		case STACK	: BEGIN
					stack();
E 9
I 9
		case STACK	: BEGIN stack();
E 9
				  ENDCASE;
D 9
		case REG	: BEGIN
					reg();
E 9
I 9
		case REG	: BEGIN reg();
E 9
				  ENDCASE;
D 9
		case PPT	: BEGIN
					ppt();
E 9
I 9
		case PPT	: BEGIN ppt();
E 9
				  ENDCASE;
D 9
		case SIGNAL	: BEGIN
					sgnal();
E 9
I 9
		case SIGNAL	: BEGIN sgnal();
E 9
				  ENDCASE;
I 3
D 9
		case SHELL	: BEGIN
					shell();
E 9
I 9
		case SHELL	: BEGIN shell();
E 9
				  ENDCASE;
I 10
		case PREV	: BEGIN	prev();
				  ENDCASE;
E 10
I 5
D 8
		case STRUCT	: BEGIN
					Struct();
E 8
I 8
D 9
		case TABLE	: BEGIN
					table();
E 9
I 9
		case NEXT	: BEGIN next();
E 9
E 8
				  ENDCASE;
E 5
E 3
D 9
		case MODE	: BEGIN
					setmode();
E 9
I 9
		case TABLE	: BEGIN table();
				  ENDCASE;
		case MODE	: BEGIN setmode();
E 9
				  END;
		END
I 3
	END
END


I 4
D 11
VAR	pathstr[128];
	shellnam[16];

E 11
#define	MAXARGS	20

I 11
char	pathstr[128];
char	shellnam[16];

E 11
E 4
/*
 *	shell: fork off inferior shell(I)
 */

PROCEDURE shell()

BEGIN	VAR	integer	childid;
		integer	status;
		integer pid;
		integer	oldsig;
I 4
		integer	index;
		integer i;
D 11
		char	argv[MAXARGS][MAXLINELENGTH];
E 11
I 11
		char	args[MAXARGS][MAXLINELENGTH];
		char	*argv[MAXARGS];
E 11
		char	name[MAXLINELENGTH];
D 6
		integer	command;
E 6
I 6
		boolean command;
E 6
E 4

I 4

	pathstr[0] = eos;
	shellnam[0] = eos;

I 11
	pexinit();

E 11
	index = 0;

D 11
	LOOP	getword( argv[index] );
E 11
I 11
	LOOP	getword( args[index] );
E 11

D 11
		WHEN ( argv[index][0] == eos ) DO EXIT;
E 11
I 11
		WHEN ( args[index][0] == eos ) DO EXIT;
E 11

		WHEN index == MAXARGS
		DO	printf("Too many arguments\n");
		EXIT;

		inc( index );
	END;

I 11
	i = 0;
	LOOP	WHEN i == index 
		DO	argv[i] = 0;
		EXIT;
	
		argv[i] = &args[i];

		inc(i);
	END;

E 11
	IF index == 0
D 6
	THEN	command = 0;
E 6
I 6
	THEN	command = false;
E 6
D 11
	ELSE	move( argv[0], name, size(argv[0]) );
		argv[index][0] = eos;
E 11
I 11
	ELSE	move( args[0], name, size(args[0]) );
E 11
D 6
		command = 1;
E 6
I 6
		command = true;
E 6
	END;

E 4
	oldsig = signal( 2, 1 );

	IF childid = fork()
	THEN	/* parent process */

		LOOP	pid = wait( &status );

			WHEN ( pid == -1 ) OR ( pid == childid ) DO EXIT;
		END;

		signal( 2, oldsig );

	ELSE	/* child process */

		signal( 2, oldsig );
D 4
		execl( "/bin/sh", "-", 0 );
E 4
I 4

		IF command
		THEN	pexec( name, argv );
D 11
		ELSE	execl("/bin/sh", "-", 0 );
E 11
I 11
		ELSE	execl( shellnam, shellnam, 0 );
E 11
		END;

		exit(0);
I 5
	END
END
D 7


#define	NOrec	0
#define	UPTrec	1
I 6
#define GSTrec	2
E 6

VAR	struct strtable 
	{	COMMAND	strcod;
		char	*strnam;
	} strtab[] {	UPTrec, "upt",
I 6
			GSTrec, "gst",
E 6
			NOrec, "",
		   };



Strinit()

{	UPTinit();
I 6
	GSTinit();
E 6
}


/*
 *	Struct: print out various structures
 */

Struct()

{	char	word[MAXLINELENGTH];
	integer	index;

	getword( word );

	IF word[0] == eos
D 6
	THEN	printf("HELP struct:\n\n");
E 6
I 6
	THEN	printf("struct HELP:\n\n");
E 6
		printf("struct upt,<upt_index>\n");
I 6
		printf("struct gst,<gst_index>\n");
E 6

	ELSE	index = 0;

		LOOP	WHEN strtab[ index ].strcod == NOrec DO EXIT;

			WHEN imatch( word, strtab[ index ].strnam ) DO EXIT;

I 6
			inc( index );
E 6
		END;


		CASE strtab[ index ].strcod OF

D 6
		case NOrec	: BEGIN
					printf("?\n");
E 6
I 6
		case NOrec	: BEGIN printf("?\n");
E 6
				  ENDCASE;
D 6
		case UPTrec	: BEGIN
					UPTdisp();
				  END;
E 6
I 6
		case UPTrec	: BEGIN UPTdisp();
				  ENDCASE;
		case GSTrec	: BEGIN GSTdisp();
				  ENDCASE;
E 6
		END;
E 5
E 4
E 3
	END
END
E 7
E 1
