[net.sources.games] ish :-)

dad@aluxp.UUCP (DAPKUS) (07/02/85)

I wasted time writing this once.  Considering the garbage
posted to other net.*'s, you certainly wouldn't mind this
version of my shell, ish.  It's not too complete as is, and
I'm not too experienced in C, so maybe someone would like to
expand it.

----- Cut Here (C program follows) -----
#include <stdio.h>
#include <signal.h>
#include <setjmp.h>

#define PROMPT	"$ "

jmp_buf	address;
char buffer[256];
char word[256];
int position;

int probno;

char *problem[] = {
	"no way josea\n",
	"cannot fork (); try again later\n",
	"no\n",
	"\n\nMessage from root on tty05 at 21:17 ...\007\007\n\n\n",
	"why should I?\n",
	"memory fault - core dumped\n",
	"don't boss ME around\n",
	"login: ",
	"I don't want to\n",
	": cannot execute\n",
	"lemme alone\n",
	"From root 21:17:  SYSTEM WILL BE GOING DOWN IN 1 MINUTE\n",
	"fat chance buster\n",
	"permission denied\n",
	0
};

int ls ();
int rm ();
int cd ();
int cat ();
int ed ();
int vi ();
int iff ();
int then ();
int elif ();
int fi ();
int echo ();
int whyle ();
int doo ();
int done ();
int cace ();
int in ();
int esac ();
int ps ();
int who ();

struct entry {
	char *word;
	int (*func) ();
} oper[] = {
	{	"ls",	ls	},
	{	"rm",	rm	},
	{	"cd",	cd	},
	{	"cat",	cat	},
	{	"ed",	ed	},
	{	"vi",	vi	},
	{	"if",	iff	},
	{	"then",	then	},
	{	"elif",	elif	},
	{	"fi",	fi	},
	{	"echo",	echo	},
	{	"while",whyle	},
	{	"do",	doo	},
	{	"done",	done	},
	{	"case",	cace	},
	{	"in",	in	},
	{	"esac",	esac	},
	{	"ps",	ps	},
	{	"who",	who	},
	0, 0
};

int intr ();

main ()
{
	setjmp (address);

main:
	signal (SIGINT, intr);
	printf (PROMPT);
	if (!gets (buffer))
		quit ();
	position = 0;
	if (!*buffer)
		goto main;
	parse ();
	goto main;
}

quit ()
{
	printf ("\nBye!\n");
	exit (0);
}

intr ()
{
	putchar ('\n');
	longjmp (address);
}

readword ()
{
	char c;
	int pos;

	pos = 0;

	if (!buffer[position]) {
		word[0] = '\0';
		return;
	}

	while (buffer[position] == ' ' || buffer[position] == '\t')
		position++;
	while ((c = buffer[position]) && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'))
		word[pos++] = buffer[position++];
	word[pos] = '\0';
}

parse ()
{
	readword ();
	if (!*word)
		fatal ("%c unexpected\n", buffer[position]);
	action (word);
	fatal ("ish: %s not found\n", word);
}

fatal (string, arg)
char *string;
int arg;
{
	printf (string, arg);
	longjmp (address);
}

action (string)
char *string;
{
	int i;
	difficult ();
	for (i = 0; oper[i].word; i++)
		if (strcmp (word, oper[i].word) == 0)
			(*(oper[i].func)) ();
}

difficult ()		/* routine to be a general pain */
{
	int prob;
	prob = rand () % 4;
	if (prob)
		return;
	if (!problem[++probno])
		probno = 0;
	fatal (problem[probno]);
}
ls ()
{
	readword ();
	if (*word)
		fatal ("ls: can't seem to find %s\n", word);
	puts ("READ_ME");
	puts ("answer");
	puts ("copyright");
	puts ("peace");
	puts ("puzzle");
	puts ("rogue.c");
	longjmp (address);
}

rm ()
{
	fatal ("rm: usage rm *.*\n");
}

cd ()
{
	printf ("I can't, I'm stuck\n");
	longjmp (address);
}

cat ()
{
	readword ();
	if (*word)
		printfile ();
	if (buffer[position] == '>')
		fatal ("cat: cannot open output file\n");
	fatal ("cat: usage cat -abcdefghijklmnopqrstuvwxyz0123456789 [ file ] ...\n");
}

printfile ()
{
	if (!strcmp (word, "READ_ME")) {
		printf ("UNIX 5.0                   Self Assertion              UNIX 5.0\n\n");
		printf ("SYNTAX:\n\n\tassert yourself\n\n");
		printf ("DESCRIPTION:\n\n\t   Are you one of those people who always\n");
		printf ("does what he is told without question?  Would you jump in a lake\n");
		printf ("if told to?  Would you give someone $10 if told to?  Would you read\n");
		printf ("a file because it was named READ_ME, READ.ME, README, or the like?\n");
		printf ("If so, send $20 to the following address:\n");
		printf ("\tSelf Assertion Foundation\n");
		printf ("\tPennsylvania Avenue\n");
		printf ("\tWashington, D.C. 00018\n");
		printf ("\n\n\n\n\n\n\n\n\n");
		printf ("Page 1                   UNIX MANUAL                    Page 1\n");
	} else
	if (!strcmp (word, "copyright")) {
		printf ("This file may not be used for purpose of profit or sale; it may\n");
		printf ("not be changed, distributed, killed, read, kept, or executed.\n");
	} else
	if (!strcmp (word, "rogue.c")) {
		printf ("char *sccs = \"@1#0 rogue.c\";\n");
		printf ("\n#include <sys/rogue.h>\n");
		printf ("\nmain (argc, argv)\n");
		printf ("bool argc;\nchar ****argv[][];\n");
		printf ("{\n\tfor (;;)\n\t\tmakedungeon ()\n");
		printf ("\t\tenterdungeon ()\n");
		printf ("\t}\n");
		printf ("}\n");
	} else
	if (!strcmp (word, "peace")) {
		printf ("peace: does not exist\n");
	} else
	if (!strcmp (word, "puzzle")) {
		printf ("...and the answer to the Mystery of Life is:\n");
		sleep (3);
		printf ("\007\007disk read error -- core dumped\n");
	} else
		system ("/usr/games/fortune");
	longjmp (address);
}

ed ()
{
	sleep (2);
	readword ();
	printf ("?%s\n", word);
	for (;;) {
		if (!gets (buffer))
			break;
		if (*buffer == 'q')
			break;
		printf ("?\n");
	}
	longjmp (address);
}

vi ()
{
	strcpy (word, getenv ("TERM"));
	fatal ("vi: terminal %s not found\n", word);
}

iff ()
{
	fatal ("I don't know if I should do it or not\n");
}

then ()
{
	fatal ("then??? you MUST be kidding\n");
}

elif ()
{
	fatal ("you must be too lazy to use 'else if'\n");
}

fi ()
{
	fatal ("fi to you too\n");
}

echo ()
{
	fatal ("HELLO!!  HELLO!  Hello!  Hello  hello\n");
}

whyle ()
{
	fatal ("while: boolean expression too complex\n");
}

doo ()
{
	fatal ("no! do it yourself.\n");
}

done ()
{
	fatal ("it's about time\n");
}

cace ()
{
	printf ("case? what case? wait! please, hold on! I don't understand!\n");
	fatal ("slow down, pleeeze!! pretty please?? grovel grovel...\n");
}

in ()
{
	printf ("come on, stop using commands like in, else, then, which are only\n");
	fatal ("parts of other commands, willya??\n");
}

esac ()
{
	printf ("Wait, you meant 'case' didn't you?  You must have your monitor\n");
	fatal ("upside-down.\n");
}

ps ()
{
	fatal ("the processor is not being put to any reasonable use.\n");
}

who ()
{
	printf ("root     tty00   Jun 23 17:21\n");
	printf ("root     tty01   Jun 23 17:21\n");
	printf ("uucp     tty03   Jun 23 17:21\n");
	printf ("jerk     tty05   Jun 23 17:22\n");
	printf ("reagan   ttyaa   Jun 23 17:25\n");
	printf ("mondale  ttyab   Jun 23 17:27\n");
	printf ("carter   ttybf   Jun 23 17:25\n");
	printf ("nixon    ttyzz   Jan 1  00:01\n");
	printf ("ford     ttyyz9  Jun 33 25:-10\n");
	fatal ("jfc      tty1e6  4 p.m. yesterday\n");
}

/* --- Cut here if you feel like it ---		*/
/*			Don Dapkus		*/