/***********************************************************************
* face		draw a face from data commands from r4d
*		or display a text message for some seconds.
*
* AUTHOR:	Gary Hoggard	7 May, 1998
*
* Last mod:	10 Aug 1999
***********************************************************************/

#include "r4.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <curses.h>

typedef char EYE[8][22];
typedef char NOSE[2][15];
typedef char MOUTH[5][29];

char s = ' ';		/* space */
char f = 0xdb;		/* full block */
char t = 0xdf;		/* top half block */
char b = 0xdc;		/* bottom half block */
char l = 0xdd;		/* left half block */
char r = 0xde;		/* right half block */
char g1 = 0xb0;		/* light grey block */
char g2 = 0xb1;		/* medium grey block */
char g3 = 0xb2;		/* dark grey block */

main()
	{
	char pipe[128];		/* pathname of named pipe */
	FILE *PIPE;		/* file pointer for named pipe */
	char buffer[BUFSIZ];
	EYE *left_eye, *right_eye;
	NOSE *nose;
	MOUTH *mouth;
	
	EYE left_eye_open =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, b, b, b, t, t, t, t, t, t, t, b, b, s, s, s, (char)NULL,
		s, b, b, t, t, t, s, s, s, s, b, b, b, b, b, b, s, s, t, t, b, (char)NULL,
		s, s, s, s, s, s, s, b, t, t, g1,g1,g1,g1,g1,g1,t, b, s, s, s, (char)NULL,
		s, s, s, b, b, t, t, s, s, g1,g1,b, f, f, b, g1,g1,s, t, l, s, (char)NULL,
		b, b, t, s, s, t, b, b, s, g1,g1,t, f, f, t, g1,g1,s, s, f, s, (char)NULL,
		s, s, s, s, s, s, s, s, t, t, b, g1,g1,g1,g1,g1,b, b, t, t, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, t, t, t, t, t, s, s, s, s, s, (char)NULL
		};

	EYE right_eye_open =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, b, b, t, t, t, t, t, t, t, b, b, b, s, s, s, s, s, s, (char)NULL,
		b, t, t, s, s, b, b, b, b, b, b, s, s, s, s, t, t, t, b, b, s, (char)NULL,
		s, s, s, b, t, g1,g1,g1,g1,g1,g1,t, t, b, s, s, s, s, s, s, s, (char)NULL,
		s, r, t, s, g1,g1,b, f, f, b, g1,g1,s, s, t, t, b, b, s, s, s, (char)NULL,
		s, f, s, s, g1,g1,t, f, f, t, g1,g1,s, b, b, t, s, s, t, b, b, (char)NULL,
		s, t, t, b, b, g1,g1,g1,g1,g1,b, t, t, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, t, t, t, t, t, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};
	
	EYE left_eye_worry =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, b, b, b, s, s, (char)NULL,
		s, s, s, s, s, s, b, b, b, b, b, t, t, t, t, t, s, s, f, s, s, (char)NULL,
		b, t, t, t, t, t, s, s, s, s, s, s, b, b, b, b, s, s, s, f, s, (char)NULL,
		s, s, s, s, s, s, s, s, b, b, t, t, g1,g1,g1,g1,t, b, s, s, t, (char)NULL,
		s, s, s, s, b, b, t, t, s, g1,g1,b, f, f, b, g1,g1,s, t, l, s, (char)NULL,
		b, b, t, t, s, t, b, b, s, g1,g1,t, f, f, t, g1,g1,s, s, f, s, (char)NULL,
		s, s, s, s, s, s, s, s, t, t, b, g1,g1,g1,g1,g1,b, b, t, t, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, t, t, t, t, t, s, s, s, s, s, (char)NULL
		};

	EYE right_eye_worry =
		{
		s, s, b, b, b, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, f, s, s, t, t, t, t, t, b, b, b, b, b, s, s, s, s, s, s, (char)NULL,
		s, f, s, s, s, b, b, b, b, s, s, s, s, s, s, t, t, t, t, t, b, (char)NULL,
		t, s, s, b, t, g1,g1,g1,g1,t, t, b, b, s, s, s, s, s, s, s, s, (char)NULL,
		s, r, t, s, g1,g1,b, f, f, b, g1,g1,s, t, t, b, b, s, s, s, s, (char)NULL,
		s, f, s, s, g1,g1,t, f, f, t, g1,g1,s, b, b, t, s, t, t, b, b, (char)NULL,
		s, t, t, b, b, g1,g1,g1,g1,g1,b, t, t, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, t, t, t, t, t, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};
	
	EYE left_eye_frown =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, b, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, t, b, s, (char)NULL,
		s, b, b, b, t, t, t, t, t, t, t, t, t, t, t, t, b, b, b, s, t, (char)NULL,
		s, s, s, s, s, s, s, b, b, b, b, b, b, b, b, b, b, b, s, t, t, (char)NULL,
		s, s, s, b, b, t, t, s, s, g1,g1,b, f, f, b, g1,g1,s, t, l, s, (char)NULL,
		b, b, t, s, s, t, b, b, s, g1,g1,t, f, f, t, g1,g1,s, s, f, s, (char)NULL,
		s, s, s, s, s, s, s, s, t, t, b, g1,g1,g1,g1,g1,b, b, t, t, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, t, t, t, t, t, s, s, s, s, s, (char)NULL
		};

	EYE right_eye_frown =
		{
		s, s, s, b, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, b, t, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		t, s, b, b, b, t, t, t, t, t, t, t, t, t, t, t, t, b, b, b, s, (char)NULL,
		t, t, s, b, b, b, b, b, b, b, b, b, b, b, s, s, s, s, s, s, s, (char)NULL,
		s, r, t, s, g1,g1,b, f, f, b, g1,g1,s, s, t, t, b, b, s, s, s, (char)NULL,
		s, f, s, s, g1,g1,t, f, f, t, g1,g1,s, b, b, t, s, s, t, b, b, (char)NULL,
		s, t, t, b, b, g1,g1,g1,g1,g1,b, t, t, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, t, t, t, t, t, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};
	
	EYE left_eye_smile =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, b, b, b, t, t, t, t, t, t, t, b, b, s, s, s, (char)NULL,
		s, b, b, t, t, t, s, s, s, s, b, b, b, b, b, b, s, s, t, t, b, (char)NULL,
		s, s, s, s, s, s, s, b, t, t, g1,g1,g1,g1,g1,g1,t, b, s, s, s, (char)NULL,
		t, t, b, b, b, f, t, s, s, g1,g1,b, f, f, b, g1,g1,s, t, l, s, (char)NULL,
		s, s, s, s, s, t, t, t, b, b, b, f, f, f, f, b, b, b, b, t, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};

	EYE right_eye_smile =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, b, b, t, t, t, t, t, t, t, b, b, b, s, s, s, s, s, s, (char)NULL,
		b, t, t, s, s, b, b, b, b, b, b, s, s, s, s, t, t, t, b, b, s, (char)NULL,
		s, s, s, b, t, g1,g1,g1,g1,g1,g1,t, t, b, s, s, s, s, s, s, s, (char)NULL,
		s, r, t, s, g1,g1,b, f, f, b, g1,g1,s, s, t, f, b, b, b, t, t, (char)NULL,
		s, t, b, b, b, b, f, f, f, f, b, b, b, t, t, t, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};
	
	EYE left_eye_raised =
		{
		s, s, s, s, s, s, b, b, t, t, t, t, t, t, b, b, s, s, s, s, s, (char)NULL,
		s, s, s, b, t, t, s, s, s, s, s, s, s, s, s, s, t, t, b, s, s, (char)NULL,
		s, b, t, s, s, s, s, s, s, s, b, b, b, b, b, b, s, s, s, t, b, (char)NULL,
		s, s, s, s, s, s, s, b, t, t, g1,g1,g1,g1,g1,g1,t, b, s, s, s, (char)NULL,
		s, s, s, b, b, t, t, s, s, g1,g1,b, f, f, b, g1,g1,s, t, l, s, (char)NULL,
		b, b, t, s, s, t, b, b, s, g1,g1,t, f, f, t, g1,g1,s, s, f, s, (char)NULL,
		s, s, s, s, s, s, s, s, t, t, b, g1,g1,g1,g1,g1,b, b, t, t, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, t, t, t, t, t, s, s, s, s, s, (char)NULL
		};

	EYE right_eye_raised =
		{
		s, s, s, s, s, b, b, t, t, t, t, t, t, b, b, s, s, s, s, s, s, (char)NULL,
		s, s, b, t, t, s, s, s, s, s, s, s, s, s, s, t, t, b, s, s, s, (char)NULL,
		b, t, s, s, s, b, b, b, b, b, b, s, s, s, s, s, s, s, t, b, s, (char)NULL,
		s, s, s, b, t, g1,g1,g1,g1,g1,g1,t, t, b, s, s, s, s, s, s, s, (char)NULL,
		s, r, t, s, g1,g1,b, f, f, b, g1,g1,s, s, t, t, b, b, s, s, s, (char)NULL,
		s, f, s, s, g1,g1,t, f, f, t, g1,g1,s, b, b, t, s, s, t, b, b, (char)NULL,
		s, t, t, b, b, g1,g1,g1,g1,g1,b, t, t, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, t, t, t, t, t, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};

	EYE left_eye_close =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, b, b, b, t, t, t, t, t, t, t, b, b, s, s, s, (char)NULL,
		s, b, b, t, t, t, s, s, s, s, s, s, s, s, s, s, s, s, t, t, b, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, s, s, (char)NULL,
		b, b, t, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, t, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};

	EYE right_eye_close =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, b, b, t, t, t, t, t, t, t, b, b, b, s, s, s, s, s, s, (char)NULL,
		b, t, t, s, s, s, s, s, s, s, s, s, s, s, s, t, t, t, b, b, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, s, s, s, (char)NULL,
		s, t, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, t, b, b, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};

	NOSE nose_normal =
		{
		s, s, b, t, s, s, s, s, s, s, t, b, s, s, (char)NULL,
		b, t, s, s, s, s, s, s, s, s, s, s, t, b, (char)NULL
		};

	MOUTH mouth_normal =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, b, t, s, s, s, s, s, s, b, b, t, t, t, t, t, t, b, b, s, s, s, s, s, s, t, b, s, (char)NULL,
		t, s, t, t, t, t, f, t, t, s, s, s, s, s, s, s, s, s, s, t, t, f, t, t, t, t, s, t, (char)NULL,
		s, s, s, s, s, s, s, t, t, b, b, s, s, s, s, s, s, b, b, t, t, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, t, t, t, t, t, t, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};

	MOUTH mouth_open =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, b, t, t, t, t, t, t, b, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, t, t, b, b, t, s, s, s, s, s, s, s, s, t, b, b, t, t, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, t, b, s, s, s, s, s, s, s, s, b, t, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, t, b, b, b, b, b, b, t, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};

	MOUTH mouth_smile =
		{
		s, b, t, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, t, b, s, (char)NULL,
		t, s, t, b, b, s, s, s, s, b, b, b, b, b, b, b, b, b, b, s, s, s, s, b, b, t, s, t, (char)NULL,
		s, s, s, s, s, t, t, f, t, s, s, s, s, s, s, s, s, s, s, t, f, t, t, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, t, t, t, b, b, b, b, b, b, t, t, t, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};

	MOUTH mouth_angry =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, b, b, b, b, b, b, b, b, t, t, t, t, t, t, t, t, b, b, b, b, b, b, b, b, s, s, (char)NULL,
		b, t, s, s, s, s, s, s, t, t, t, t, f, f, f, f, t, t, t, t, s, s, s, s, s, s, t, b, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};

	MOUTH mouth_worry =
		{
		s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, b, b, t, t, t, t, b, b, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, b, t, t, t, t, t, t, f, s, s, s, s, s, s, s, s, f, t, t, t, t, t, t, b, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, t, b, s, s, s, s, b, t, s, s, s, s, s, s, s, s, s, s, (char)NULL,
		s, s, s, s, s, s, s, s, s, s, s, s, t, t, t, t, s, s, s, s, s, s, s, s, s, s, s, s, (char)NULL
		};

	if (getenv("DEBUG"))
		{
		debug=YES;
		dlog = fopen(DEBUGFILE, "a");
		}

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

	/* open /dev/tty1 (console) as fd=1 (rather than STDOUT) */
	close(1);
	open("/dev/tty1", O_WRONLY|O_NDELAY);

	/* put normal face on screen */
	setup_crt();
	left_eye = &left_eye_open;
	right_eye = &right_eye_open;
	mouth = &mouth_normal;
	nose = &nose_normal;
	draw_face(*left_eye, *right_eye, *nose, *mouth);

	/* process facial requests */
	while (YES)
		{
		if ((fgets(buffer, BUFSIZ, PIPE)) == 0)
			{
			fclose(PIPE);
			break;
			}
		if (debug)
			{
			fprintf(dlog, "face: read '%s' from face_pipe\n", buffer);
			fflush(dlog);
			}
		if (*buffer == 't')
			{
			display_text(&buffer[1]);
			clear();
			clearok(stdscr, YES);
			wrefresh(stdscr);
			draw_face(*left_eye, *right_eye, *nose, *mouth);
			continue;
			}
		if (*buffer != 'f')	/* only 'f'ace and 't'ext commands are valid */
			continue;
		if (buffer[1] == 'm')
			{
			/* mouth a phonem */
			switch (buffer[2])
				{
				case 'a': break;
				case 'e': break;
				case 'i': break;
				case 'o': break;
				case 'u': break;
				case 'm': break;
				case 'd': break;
				case 'w': break;
				}
			}
		else if (!strncmp(&buffer[1], "refresh", 7))
			{
			clearok(curscr, YES);
			}
		else if (!strncmp(&buffer[1], "normal", 6))
			{
			left_eye = &left_eye_open;
			right_eye = &right_eye_open;
			nose = &nose_normal;
			mouth = &mouth_normal;
			}
		else if (!strncmp(&buffer[1], "blink", 5))
			{
			draw_face(left_eye_close, right_eye_close, *nose, *mouth);
			pause_msec(100);
			}
		else if (!strncmp(&buffer[1], "smile", 5))
			{
			left_eye = &left_eye_smile;
			right_eye = &right_eye_smile;
			mouth = &mouth_smile;
			}
		else if (!strncmp(&buffer[1], "surprise", 8))
			{
			left_eye = &left_eye_raised;
			right_eye = &right_eye_raised;
			mouth = &mouth_open;
			}
		else if (!strncmp(&buffer[1], "frown", 5))
			{
			left_eye = &left_eye_frown;
			right_eye = &right_eye_frown;
			mouth = &mouth_angry;
			}
		else if (!strncmp(&buffer[1], "worry", 5))
			{
			left_eye = &left_eye_worry;
			right_eye = &right_eye_worry;
			mouth = &mouth_worry;
			}
		draw_face(*left_eye, *right_eye, *nose, *mouth);
		}
	fixup_crt();
	}

draw_face(EYE left_eye, EYE right_eye, NOSE nose, MOUTH mouth)
	{
	attrset(COLOR_PAIR(4));
	mvaddstr(5, 9, left_eye[0]);
	mvaddstr(6, 9, left_eye[1]);
	mvaddstr(7, 9, left_eye[2]);
	mvaddstr(8, 9, left_eye[3]);
	mvaddstr(9, 9, left_eye[4]);
	mvaddstr(10, 9, left_eye[5]);
	mvaddstr(11, 9, left_eye[6]);
	mvaddstr(12, 9, left_eye[7]);
	mvaddstr(5, 46, right_eye[0]);
	mvaddstr(6, 46, right_eye[1]);
	mvaddstr(7, 46, right_eye[2]);
	mvaddstr(8, 46, right_eye[3]);
	mvaddstr(9, 46, right_eye[4]);
	mvaddstr(10, 46, right_eye[5]);
	mvaddstr(11, 46, right_eye[6]);
	mvaddstr(12, 46, right_eye[7]);

	attrset(COLOR_PAIR(1));
	mvaddstr(13, 31, nose[0]);
	mvaddstr(14, 31, nose[1]);

	attrset(COLOR_PAIR(2));
	mvaddstr(17, 24, mouth[0]);
	mvaddstr(18, 24, mouth[1]);
	mvaddstr(19, 24, mouth[2]);
	mvaddstr(20, 24, mouth[3]);
	mvaddstr(21, 24, mouth[4]);

	attrset(COLOR_PAIR(1));
	move(0, 0);
	refresh();
	}

setup_crt()	/* initialise screen for curses */
	{
	initscr();		/* initialise curses routines */
	start_color();		/* initialise use of colours */

	/* set up colour pairs: */
	init_pair(1, COLOR_YELLOW, COLOR_BLACK);
	init_pair(2, COLOR_MAGENTA, COLOR_BLACK);
	init_pair(3, COLOR_RED, COLOR_BLACK);
	init_pair(4, COLOR_CYAN, COLOR_BLACK);
	attrset(COLOR_PAIR(1));

	nonl();			/* unset newline mapping */
	noecho();		/* unset echo mode */
	clear();		/* clear memory image of screen (stdscr) */
	refresh();		/* update terminal screen with stdscr */
	inittext();		/* set up text display window */
	return;
	}

fixup_crt()	/* set terminal back to normal operation */
	{
	nl();			/* set newline mapping*/
	nocbreak();		/* unset cbreak mode (allow buffered input) */
	echo();			/* set echo mode */
	endwin();		/* de-initialise curses */
	return;
	}

pause_msec(int duration)	/* pause for "duration" milliseconds */
	{
	struct timeval timer;

	timer.tv_sec = duration / 1000;
	timer.tv_usec = (duration % 1000) * 1000;
	select(0, NULL, NULL, NULL, &timer);
	}

/*----------------------------------------------------------------------
display_text receives the command buffer (less the initial 't') as an
argument.  This is in the form:
		n "1st line" "2nd line" "etc"<null>
We need to strip the numerical 1st part and then convert the rest to:
		1st line<nl>2nd line<nl>etc<null>
then call disptext() after setting up the display for ordinary text
rather than curses(3) handling.
----------------------------------------------------------------------*/

display_text(char *cmdstring)
	{
	char text[BUFSIZ], *p = text;
	int wait_time;

	if (debug)
		{
		fprintf(dlog, "display_text() got: '%s'\n", cmdstring);
		fflush(dlog);
		}

	/* save wait_time and position string pointer to start of text */
	wait_time = atoi(cmdstring);
	while (*cmdstring && *cmdstring != '"')
		++cmdstring;
	++cmdstring;		/* position at the first char AFTER the '"' */
		
	/* convert text to form needed for disptext() */
	while (*cmdstring && *cmdstring != '\n')
		{
		if (*cmdstring == '"')
			{
			*p++ = '\n';
			while (*cmdstring == ' ' || *cmdstring == '"')
				++cmdstring;
			}
		*p = *cmdstring == '\n' ? '\0' : *cmdstring;
		if (*p)
			{
			p++;
			cmdstring++;
			}
		}

	/* clear trailing NL char */
	if (text[strlen(text)-1] == '\n')
		text[strlen(text)-1] = '\0';

	/* now  clear the screen and display the text: */
	if (debug)
		{
		fprintf(dlog, "display_text() calling disptext() with: '%s'\n", text);
		fflush(dlog);
		}
	disptext(text);
	sleep(wait_time);
	return;
	}

/*_______________________end_of_face.c_________________________*/

