/* $XConsortium: LibTest.c,v 1.7 94/04/17 20:53:59 rws Exp $ */
/*

Copyright (c) 1989, 1990  X Consortium

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the X Consortium.


Copyright 1989 1990, Tektronix, Inc.
All Rights Reserved

Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of Tektronix not be
used in advertising or publicity pertaining to distribution
of the software without specific prior written permission.
Tektronix makes no representation about the
suitability of this software for any purpose. It is provided
"as is" without any express or implied warranty.

TEKTRONIX DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL 
TEKTRONIX BE LIABLE FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA  OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

/*
 *
 *	NAME
 *		cmstest - Interactive Library Testing Interface.
 *
 *	SYNOPSIS
 *		cmstest
 *
 *	DESCRIPTION
 *		This program is a front-end for testing the library.
 *		It takes commands from stdin.
 *
 *	FILES
 *		Command Source File:
 *			C source file containing a) definition for the
 *			Command Table (table of command string / function
 *			pointer tuples) and b) function source.
 *		Command Header File:
 *			Header file as a result from running 'autohdr'
 *			on the Command Source File.
 *		Command Object File:
 *			The result of compiling the Command Source File
 *		Library:
 *			The library under test.
 *		Makefile:
 *			All the above files are defined in the Makefile
 *			and generated and appropriately linked to this
 *			program.
 */

/*
 *      EXTERNAL INCLUDES
 *              Include files that must be exported to any package or
 *              program using this package.
 */
#include "LibTest.h"
#define NEED_EVENTS
#include <X11/Xlibint.h>      

/*
 *	EXTERNALS
 *		Declarations that are needed by calling modules.
 *		When using 'autohdr', these declaration will be placed
 *		in the resulting header file.
 */
int CommandArgc;	/* GLOBAL */
char **CommandArgv;	/* GLOBAL */
int EchoInput = 0;	/* GLOBAL */

/*
 *      INTERNAL INCLUDES
 *              Include files that need NOT be exported to any package or
 *              program using this package.
 */
#include <stdio.h>
#include "CmdTbl.h"

/*
 *	INTERNALS
 *		Declarations that are local to this module.
 *		(ignored by 'autohdr').
 */
#ifndef LIBTEST_PROMPT
#define LIBTEST_PROMPT	"What! >"
#endif

#ifndef LIBTEST_COMMENT_CHAR
#define LIBTEST_COMMENT_CHAR	'#'
#endif

#define LIBTEST_EOLN_CHAR	'\n'

PFStatus LtStrToFuncPtr();

int errno;

extern Display *pDpy ;


/************************************************************************
 *									*
 *			   	M A I N					*
 *									*
 ************************************************************************/

/*
 *	NAME
 *		main
 *
 *	SYNOPSIS
 */
main(argc, argv)
    int argc;
    char **argv;
/*
 *	DESCRIPTION
 *		<complete external description of the function>
 *
 *	RETURNS
 *		<value returned by the function -- not required for void functions>
 *
 */
{
    char buf[BUFSIZ];
    PFStatus pfunc;
    char command[STRSIZ];

    /*
     * Set global CommandArgc and CommandArgv
     */
    CommandArgc = argc;
    CommandArgv = argv;

    /*
     * Process args as necessary here.
     */

    for (;argc--;argv++) {
	if (strcmp(*argv, "-echo") == 0) {
	    EchoInput = 1;
	}
    }

    for(;;) {
	printf("%s", LIBTEST_PROMPT);
	if (fgets(buf, BUFSIZ, stdin) == NULL) {
	    return;
	}
	if (EchoInput) {
	    printf("%s", buf);
	    fflush(stdout);
	}
	if ((buf[0] == LIBTEST_COMMENT_CHAR) ||
		(buf[0] == LIBTEST_EOLN_CHAR)) {
	    continue;
	}
	if (sscanf(buf, "%s", command) != 0) {
	  if ((pfunc = LtStrToFuncPtr(LIBTEST_CMDTBL, command)) 
		== (PFStatus) -1) {
		printf("Error: Invalid command\n\n");
		fflush(stdout);
		continue;
	    }
	    (*pfunc)(&buf[strlen(command)]);
	    fflush(stdout);
	}
    }
}


/************************************************************************
 *									*
 *			   PRIVATE ROUTINES				*
 *									*
 ************************************************************************/

/*
 *	NAME
 *		_XPrintDefaultError
 *
 *	SYNOPSIS
 */
static int
_XPrintDefaultError (dpy, event, fp)
    Display *dpy;
    XErrorEvent *event;
    FILE *fp;
/*
 *	DESCRIPTION
 *		Prints error message to stderr
 *		This code lifted directly from Xlib Source
 *		XlibInt.c,v 11.124 90/06/15 13:09:20 rws
 *
 *	RETURNS
 *		int
 *
 */
{
    char buffer[STRSIZ];
    char mesg[STRSIZ];
    char number[32];
    char *mtype = "XlibMessage";
    register _XExtension *ext = (_XExtension *)NULL;
    XGetErrorText(dpy, event->error_code, buffer, STRSIZ);
    XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, STRSIZ);
    (void) fprintf(fp, "%s:  %s\n  ", mesg, buffer);
    XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d", 
	mesg, STRSIZ);
    (void) fprintf(fp, mesg, event->request_code);
    if (event->request_code < 128) {
	sprintf(number, "%d", event->request_code);
	XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, STRSIZ);
    } else {
	for (ext = dpy->ext_procs;
	     ext && (ext->codes.major_opcode != event->request_code);
	     ext = ext->next) /* SUPPRESS 530 */
	  ;
	if (ext)
	    strcpy(buffer, ext->name);
	else
	    buffer[0] = '\0';
    }
    (void) fprintf(fp, " (%s)\n  ", buffer);
    if (event->request_code >= 128) {
	XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
			      mesg, STRSIZ);
	(void) fprintf(fp, mesg, event->minor_code);
	if (ext) {
	    sprintf(mesg, "%s.%d", ext->name, event->minor_code);
	    XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, STRSIZ);
	    (void) fprintf(fp, " (%s)", buffer);
	}
	fputs("\n  ", fp);
    }
    if (event->error_code >= 128) {
	/* kludge, try to find the extension that caused it */
	buffer[0] = '\0';
	for (ext = dpy->ext_procs; ext; ext = ext->next) {
	    if (ext->error_string) 
		(*ext->error_string)(dpy, event->error_code, &ext->codes,
				     buffer, STRSIZ);
	    if (buffer[0])
		break;
	}    
	if (buffer[0])
	    sprintf(buffer, "%s.%d", ext->name,
		    event->error_code - ext->codes.first_error);
	else
	    strcpy(buffer, "Value");
	XGetErrorDatabaseText(dpy, mtype, buffer, "Value 0x%x", mesg, STRSIZ);
	if (*mesg) {
	    (void) fprintf(fp, mesg, event->resourceid);
	    fputs("\n  ", fp);
	}
    } else if ((event->error_code == BadWindow) ||
	       (event->error_code == BadPixmap) ||
	       (event->error_code == BadCursor) ||
	       (event->error_code == BadFont) ||
	       (event->error_code == BadDrawable) ||
	       (event->error_code == BadColor) ||
	       (event->error_code == BadGC) ||
	       (event->error_code == BadIDChoice) ||
	       (event->error_code == BadValue) ||
	       (event->error_code == BadAtom)) {
	if (event->error_code == BadValue)
	    XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
				  mesg, STRSIZ);
	else if (event->error_code == BadAtom)
	    XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
				  mesg, STRSIZ);
	else
	    XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
				  mesg, STRSIZ);
	(void) fprintf(fp, mesg, event->resourceid);
	fputs("\n  ", fp);
    }
    XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d", 
	mesg, STRSIZ);
    (void) fprintf(fp, mesg, event->serial);
    fputs("\n  ", fp);
    XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
	mesg, STRSIZ);
    (void) fprintf(fp, mesg, dpy->request);
    fputs("\n", fp);
    if (event->error_code == BadImplementation) return 0;
    return 1;
}



/************************************************************************
 *									*
 *			   PUBLIC ROUTINES				*
 *									*
 ************************************************************************/

/*
 *	NAME
 *		LtErrorHandler - Special LibTest error handler
 *
 *	SYNOPSIS
 */
int
LtErrorHandler (pDpy, pError)
    Display	*pDpy ;
    XErrorEvent	*pError ;
/*
 *	DESCRIPTION
 *		Traps XErrorEvents
 *		Prints a message to stderr,
 *		Sets a global variable,
 *		Synchronizes output
 *
 *	RETURNS
 *		int
 *
 */
{
    _XPrintDefaultError (pDpy, pError, stderr);

    return (1) ;
}


/*
 *	NAME
 *		LtStrToFuncPtr - convert a string to a function pointer
 *
 *	SYNOPSIS
 */
PFStatus
LtStrToFuncPtr(pde,pstring)
    FuncTableEntry	*pde;	/* IN: table of string/functionptr  pairs
			     *     last entry must contain pair "", 0 */
    char	*pstring;	/* IN: string to be looked up in that table  */
/*
 *	DESCRIPTION
 *		Converts a string to an integer define.
 *
 *		Looks up the string in the table and returns the integer
 *		associated with the string.
 *
 *		Later may need similar function for unsigned long define.
 *
 *	RETURNS
 *		The int equivalent of the defined string.
 *		-1 if the string is not found in table
 *
 */
{
    if (!pde) return((PFStatus) -1); /* defend against NULL pointer */
    while( strcmp(pde->pstring,"") != 0 ){
	if( strcmp(pde->pstring, pstring) == 0){
	    return(pde->pfunc);
	}
	pde++;
    }
    return((PFStatus) -1);
}


/*
 *	NAME
 *		LtDefineToStr - convert a define to a string
 *
 *	SYNOPSIS
 */
char *
LtDefineToStr(pde,define)
LtDefineEntry	pde[];	/* IN: table of X string-define pairs
			 *     last entry must contain pair "", 0
			 */
int		define;	/* IN: define to be looked up in that table	*/
/*
 *	DESCRIPTION
 *		Converts an integer define to a string pointer.
 *		Later may need similar function for unsigned long define.
 *
 *		Check XuDefineString.h for predefined tables.
 *
 *	RETURNS
 *		Pointer to a valid string.
 *		If no string is found, returns a pointer to a 0 length string.
 *
 */
{
    while( strcmp(pde->pstring,"") != 0 ){
	if( pde->define == define) {
	    return(pde->pstring);
	}
	pde++;
    }
    return("");
}

/*
 *	NAME
 *		LtStrToDefine - convert a string to a define
 *
 *	SYNOPSIS
 */
int
LtStrToDefine(pde,pstring)
    LtDefineEntry	pde[];	/* IN: table of X string-define pairs
				 *     last entry must contain pair "", 0
				 */
    char	*pstring;	/* IN: string to be looked up in that table */
/*
 *	DESCRIPTION
 *		Converts a string to an integer define.
 *
 *		Looks up the string in the table and returns the integer
 *		associated with the string.
 *
 *		Later may need similar function for unsigned long define.
 *
 *		Check XuDefineString.h for predefined tables.
 *
 *
 *	RETURNS
 *		The int equivalent of the defined string.
 *		-1 if the string is not found in table
 *
 */
{
    while( strcmp(pde->pstring,"") != 0 ){
	if( strcmp(pde->pstring,pstring) == 0){
	    return(pde->define);
	}
	pde++;
    }
    return(-1);
}