/********************************************************
*
* command.c	Command interpretor for R4
*
* Author:	Gary Hoggard	26 Apr 1998
*
* Change History:
*	01 Jan 1999	added help text
*	29 Mar 1999	added -q opt & "z" command
********************************************************/

#include "r4.h"

char *pname;			/* program name */
int verbose = YES;		/* a flag */

usage()
	{
	fprintf(stderr, "Usage: %s [-options]\n", pname);
	fprintf(stderr, "where options are:\n");
	fprintf(stderr, "\t-q		quiet mode (no output)\n");
	exit(1);
	}

main(int argc, char **argv)
	{
	char pipe[128];		/* pathname of named pipe */
	FILE *PIPE;		/* file pointer for named pipe */
	char buffer[BUFSIZ];
	extern char *optarg;
	extern int optind;
	int c;

	pname = argv[0];

	while ((c = getopt(argc, argv, "q")) != EOF)
		{
		switch(c)
			{
			case 'q':
				verbose = NO;
				break;
			case '?' :
				usage();
				break;
			}
		}

	argc -= optind;

	if (getenv("DEBUG"))
		{
		debug=YES;
		dlog = fopen(DEBUGFILE, "a");
		fprintf(dlog, "command: started\n");
		fflush(dlog);
		}

	/* open the named pipe for writing */
	if (getenv("R4_LIB"))
		strcpy(pipe, getenv("R4_LIB"));
	else
		strcpy(pipe, LIB);
	strcat(pipe, "/command_pipe");
	if ((PIPE = fopen(pipe, "a")) == NULL)
		{
		perror("cannot open named pipe");
		exit(1);
		}

	if (verbose)
		puts("Hi.  I'm R4 the robot.  Type 'help' or give me a command.");
	while (YES)
		{
		if (verbose)
			printf("robot> ");
		fgets(buffer, BUFSIZ, stdin);
		if (!strncmp(buffer, "exit", 4))
			{
			fclose(PIPE);
			exit(0);
			}
		else if (!strncmp(buffer, "help", 4) || *buffer == '?')
			give_help();
		else if (!strncmp(buffer, "stop", 4))
			{
			fputs("m0\n", PIPE);
			fflush(PIPE);
			}
		else if (*buffer == 'z')
			sleep(atoi(&buffer[1]));
		else
			{
			if (debug)
				{
				fprintf(dlog, "command: sending '%s'\n", buffer);
				fflush(dlog);
				}
			fputs(buffer, PIPE);
			fflush(PIPE);
			}
		}
	}

give_help()
	{
	puts("\nType 'exit' to terminate program, or a command of the form:");
	puts("\tCxxxxxx");
	puts("where \"C\" is a 1 byte command and \"xxxxxx\" is a variable");
	puts("parameter string defined by the type of command.  The allowed");
	puts("commands are:");
	puts("\ta\tmove the arm.");
	puts("\t\tclose		claw closed");
	puts("\t\topen		claw open");
	puts("\t\tp x y z		move claw to relative position (x,y,z)\n");
	puts("\tf\tdisplay facial expression.  One of:");
	puts("\t\trefresh		frown");
	puts("\t\tnormal		surprise");
	puts("\t\tblink		worry");
	puts("\t\tsmile		m{a|e|i|o|u|m|d|w}\n");
	puts("\tt\tprint text on the screen.  The arg string has the form:");
	puts("\t\tn \"1st line of text\" \"2nd line of text\" \"etc...\"");
	puts("\t\twhere 'n' is the number of seconds to pause before redrawing");
	puts("\t\tthe face.  Note that the lines MUST be quoted.\n");
	puts("\tm\tmove the robot.  The arg string is a single char:");
	puts("\t\t	1 f 2");
	puts("\t\t	l 0 r");
	puts("\t\t	3 b 4\n");
	puts("\tp\tplay a sound.  Followed by <w>ave or <a>udio and the");
	puts("\t\tfilename to play, eg, pwwhistle\n");
	puts("\tv\tvoice.  The arg string is a word to say.\n");
	puts("\tz\tsleep (\"catch some ZZZzzz's\").  The arg is no. of secs.\n");
	}

/*______________________end_of_command.c_____________________*/

