home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xcmstest / LibTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-31  |  10.9 KB  |  441 lines

  1. /* $XConsortium: LibTest.c,v 1.3 91/05/14 15:02:45 dave Exp $ */
  2.  
  3. /*
  4.  *    Copyright 1989 1990, Tektronix, Inc.
  5.  *    Copyright 1989 1990 by The Massachusetts Institute of Technology
  6.  *    
  7.  *                        All Rights Reserved
  8.  *    
  9.  *    Permission to use, copy, modify, and distribute this
  10.  *    software and its documentation for any purpose and without
  11.  *    fee is hereby granted, provided that the above copyright
  12.  *    notice appear in all copies and that both that copyright
  13.  *    notice and this permission notice appear in supporting
  14.  *    documentation, and that the names of MIT and Tektronix not be
  15.  *    used in advertising or publicity pertaining to distribution
  16.  *    of the software without specific prior written permission.
  17.  *    M.I.T. and Tektronix make no representation about the
  18.  *    suitability of this software for any purpose. It is provided
  19.  *    "as is" without any express or implied warranty.
  20.  *    
  21.  *    MIT AND TEKTRONIX DISCLAIM ALL WARRANTIES WITH REGARD TO  THIS
  22.  *    SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  23.  *    AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MIT OR
  24.  *    TEKTRONIX BE LIABLE FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25.  *    DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  26.  *    DATA  OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  27.  *    OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  28.  *    THE USE OR PERFORMANCE OF THIS SOFTWARE.
  29.  *
  30.  *    NAME
  31.  *        cmstest - Interactive Library Testing Interface.
  32.  *
  33.  *    SYNOPSIS
  34.  *        cmstest
  35.  *
  36.  *    DESCRIPTION
  37.  *        This program is a front-end for testing the library.
  38.  *        It takes commands from stdin.
  39.  *
  40.  *    FILES
  41.  *        Command Source File:
  42.  *            C source file containing a) definition for the
  43.  *            Command Table (table of command string / function
  44.  *            pointer tuples) and b) function source.
  45.  *        Command Header File:
  46.  *            Header file as a result from running 'autohdr'
  47.  *            on the Command Source File.
  48.  *        Command Object File:
  49.  *            The result of compiling the Command Source File
  50.  *        Library:
  51.  *            The library under test.
  52.  *        Makefile:
  53.  *            All the above files are defined in the Makefile
  54.  *            and generated and appropriately linked to this
  55.  *            program.
  56.  */
  57.  
  58. /*
  59.  *      EXTERNAL INCLUDES
  60.  *              Include files that must be exported to any package or
  61.  *              program using this package.
  62.  */
  63. #include "LibTest.h"
  64. #include <X11/Xlib.h>
  65. #include <X11/Xlibint.h>      
  66.  
  67. /*
  68.  *    EXTERNALS
  69.  *        Declarations that are needed by calling modules.
  70.  *        When using 'autohdr', these declaration will be placed
  71.  *        in the resulting header file.
  72.  */
  73. int CommandArgc;    /* GLOBAL */
  74. char **CommandArgv;    /* GLOBAL */
  75. int EchoInput = 0;    /* GLOBAL */
  76.  
  77. /*
  78.  *      INTERNAL INCLUDES
  79.  *              Include files that need NOT be exported to any package or
  80.  *              program using this package.
  81.  */
  82. #include <stdio.h>
  83. #include "CmdTbl.h"
  84.  
  85. /*
  86.  *    INTERNALS
  87.  *        Declarations that are local to this module.
  88.  *        (ignored by 'autohdr').
  89.  */
  90. #ifndef LIBTEST_PROMPT
  91. #define LIBTEST_PROMPT    "What! >"
  92. #endif
  93.  
  94. #ifndef LIBTEST_COMMENT_CHAR
  95. #define LIBTEST_COMMENT_CHAR    '#'
  96. #endif
  97.  
  98. #define LIBTEST_EOLN_CHAR    '\n'
  99.  
  100. PFStatus LtStrToFuncPtr();
  101.  
  102. int errno;
  103.  
  104. extern Display *pDpy ;
  105.  
  106.  
  107. /************************************************************************
  108.  *                                    *
  109.  *                   M A I N                    *
  110.  *                                    *
  111.  ************************************************************************/
  112.  
  113. /*
  114.  *    NAME
  115.  *        main
  116.  *
  117.  *    SYNOPSIS
  118.  */
  119. main(argc, argv)
  120.     int argc;
  121.     char **argv;
  122. /*
  123.  *    DESCRIPTION
  124.  *        <complete external description of the function>
  125.  *
  126.  *    RETURNS
  127.  *        <value returned by the function -- not required for void functions>
  128.  *
  129.  */
  130. {
  131.     char buf[BUFSIZ];
  132.     PFStatus pfunc;
  133.     char command[BUFSIZ];
  134.  
  135.     /*
  136.      * Set global CommandArgc and CommandArgv
  137.      */
  138.     CommandArgc = argc;
  139.     CommandArgv = argv;
  140.  
  141.     /*
  142.      * Process args as necessary here.
  143.      */
  144.  
  145.     for (;argc--;argv++) {
  146.     if (strcmp(*argv, "-echo") == 0) {
  147.         EchoInput = 1;
  148.     }
  149.     }
  150.  
  151.     for(;;) {
  152.     printf("%s", LIBTEST_PROMPT);
  153.     if (fgets(buf, BUFSIZ, stdin) == NULL) {
  154.         return;
  155.     }
  156.     if (EchoInput) {
  157.         printf("%s", buf);
  158.         fflush(stdout);
  159.     }
  160.     if ((buf[0] == LIBTEST_COMMENT_CHAR) ||
  161.         (buf[0] == LIBTEST_EOLN_CHAR)) {
  162.         continue;
  163.     }
  164.     if (sscanf(buf, "%s", command) != 0) {
  165.       if ((pfunc = LtStrToFuncPtr(LIBTEST_CMDTBL, command)) 
  166.         == (PFStatus) -1) {
  167.         printf("Error: Invalid command\n\n");
  168.         fflush(stdout);
  169.         continue;
  170.         }
  171.         (*pfunc)(&buf[strlen(command)]);
  172.         fflush(stdout);
  173.     }
  174.     }
  175. }
  176.  
  177.  
  178. /************************************************************************
  179.  *                                    *
  180.  *               PRIVATE ROUTINES                *
  181.  *                                    *
  182.  ************************************************************************/
  183.  
  184. /*
  185.  *    NAME
  186.  *        _XPrintDefaultError
  187.  *
  188.  *    SYNOPSIS
  189.  */
  190. static int
  191. _XPrintDefaultError (dpy, event, fp)
  192.     Display *dpy;
  193.     XErrorEvent *event;
  194.     FILE *fp;
  195. /*
  196.  *    DESCRIPTION
  197.  *        Prints error message to stderr
  198.  *        This code lifted directly from Xlib Source
  199.  *        XlibInt.c,v 11.124 90/06/15 13:09:20 rws
  200.  *
  201.  *    RETURNS
  202.  *        int
  203.  *
  204.  */
  205. {
  206.     char buffer[BUFSIZ];
  207.     char mesg[BUFSIZ];
  208.     char number[32];
  209.     char *mtype = "XlibMessage";
  210.     register _XExtension *ext = (_XExtension *)NULL;
  211.     XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
  212.     XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
  213.     (void) fprintf(fp, "%s:  %s\n  ", mesg, buffer);
  214.     XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d", 
  215.     mesg, BUFSIZ);
  216.     (void) fprintf(fp, mesg, event->request_code);
  217.     if (event->request_code < 128) {
  218.     sprintf(number, "%d", event->request_code);
  219.     XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
  220.     } else {
  221.     for (ext = dpy->ext_procs;
  222.          ext && (ext->codes.major_opcode != event->request_code);
  223.          ext = ext->next) /* SUPPRESS 530 */
  224.       ;
  225.     if (ext)
  226.         strcpy(buffer, ext->name);
  227.     else
  228.         buffer[0] = '\0';
  229.     }
  230.     (void) fprintf(fp, " (%s)\n  ", buffer);
  231.     if (event->request_code >= 128) {
  232.     XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
  233.                   mesg, BUFSIZ);
  234.     (void) fprintf(fp, mesg, event->minor_code);
  235.     if (ext) {
  236.         sprintf(mesg, "%s.%d", ext->name, event->minor_code);
  237.         XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
  238.         (void) fprintf(fp, " (%s)", buffer);
  239.     }
  240.     fputs("\n  ", fp);
  241.     }
  242.     if (event->error_code >= 128) {
  243.     /* kludge, try to find the extension that caused it */
  244.     buffer[0] = '\0';
  245.     for (ext = dpy->ext_procs; ext; ext = ext->next) {
  246.         if (ext->error_string) 
  247.         (*ext->error_string)(dpy, event->error_code, &ext->codes,
  248.                      buffer, BUFSIZ);
  249.         if (buffer[0])
  250.         break;
  251.     }    
  252.     if (buffer[0])
  253.         sprintf(buffer, "%s.%d", ext->name,
  254.             event->error_code - ext->codes.first_error);
  255.     else
  256.         strcpy(buffer, "Value");
  257.     XGetErrorDatabaseText(dpy, mtype, buffer, "Value 0x%x", mesg, BUFSIZ);
  258.     if (*mesg) {
  259.         (void) fprintf(fp, mesg, event->resourceid);
  260.         fputs("\n  ", fp);
  261.     }
  262.     } else if ((event->error_code == BadWindow) ||
  263.            (event->error_code == BadPixmap) ||
  264.            (event->error_code == BadCursor) ||
  265.            (event->error_code == BadFont) ||
  266.            (event->error_code == BadDrawable) ||
  267.            (event->error_code == BadColor) ||
  268.            (event->error_code == BadGC) ||
  269.            (event->error_code == BadIDChoice) ||
  270.            (event->error_code == BadValue) ||
  271.            (event->error_code == BadAtom)) {
  272.     if (event->error_code == BadValue)
  273.         XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
  274.                   mesg, BUFSIZ);
  275.     else if (event->error_code == BadAtom)
  276.         XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
  277.                   mesg, BUFSIZ);
  278.     else
  279.         XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
  280.                   mesg, BUFSIZ);
  281.     (void) fprintf(fp, mesg, event->resourceid);
  282.     fputs("\n  ", fp);
  283.     }
  284.     XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d", 
  285.     mesg, BUFSIZ);
  286.     (void) fprintf(fp, mesg, event->serial);
  287.     fputs("\n  ", fp);
  288.     XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
  289.     mesg, BUFSIZ);
  290.     (void) fprintf(fp, mesg, dpy->request);
  291.     fputs("\n", fp);
  292.     if (event->error_code == BadImplementation) return 0;
  293.     return 1;
  294. }
  295.  
  296.  
  297.  
  298. /************************************************************************
  299.  *                                    *
  300.  *               PUBLIC ROUTINES                *
  301.  *                                    *
  302.  ************************************************************************/
  303.  
  304. /*
  305.  *    NAME
  306.  *        LtErrorHandler - Special LibTest error handler
  307.  *
  308.  *    SYNOPSIS
  309.  */
  310. int
  311. LtErrorHandler (pDpy, pError)
  312.     Display    *pDpy ;
  313.     XErrorEvent    *pError ;
  314. /*
  315.  *    DESCRIPTION
  316.  *        Traps XErrorEvents
  317.  *        Prints a message to stderr,
  318.  *        Sets a global variable,
  319.  *        Synchronizes output
  320.  *
  321.  *    RETURNS
  322.  *        int
  323.  *
  324.  */
  325. {
  326.     _XPrintDefaultError (pDpy, pError, stderr);
  327.  
  328.     return (1) ;
  329. }
  330.  
  331.  
  332. /*
  333.  *    NAME
  334.  *        LtStrToFuncPtr - convert a string to a function pointer
  335.  *
  336.  *    SYNOPSIS
  337.  */
  338. PFStatus
  339. LtStrToFuncPtr(pde,pstring)
  340.     FuncTableEntry    *pde;    /* IN: table of string/functionptr  pairs
  341.                  *     last entry must contain pair "", 0 */
  342.     char    *pstring;    /* IN: string to be looked up in that table  */
  343. /*
  344.  *    DESCRIPTION
  345.  *        Converts a string to an integer define.
  346.  *
  347.  *        Looks up the string in the table and returns the integer
  348.  *        associated with the string.
  349.  *
  350.  *        Later may need similar function for unsigned long define.
  351.  *
  352.  *    RETURNS
  353.  *        The int equivalent of the defined string.
  354.  *        -1 if the string is not found in table
  355.  *
  356.  */
  357. {
  358.     if (!pde) return((PFStatus) -1); /* defend against NULL pointer */
  359.     while( strcmp(pde->pstring,"") != 0 ){
  360.     if( strcmp(pde->pstring, pstring) == 0){
  361.         return(pde->pfunc);
  362.     }
  363.     pde++;
  364.     }
  365.     return((PFStatus) -1);
  366. }
  367.  
  368.  
  369. /*
  370.  *    NAME
  371.  *        LtDefineToStr - convert a define to a string
  372.  *
  373.  *    SYNOPSIS
  374.  */
  375. char *
  376. LtDefineToStr(pde,define)
  377. LtDefineEntry    pde[];    /* IN: table of X string-define pairs
  378.              *     last entry must contain pair "", 0
  379.              */
  380. int        define;    /* IN: define to be looked up in that table    */
  381. /*
  382.  *    DESCRIPTION
  383.  *        Converts an integer define to a string pointer.
  384.  *        Later may need similar function for unsigned long define.
  385.  *
  386.  *        Check XuDefineString.h for predefined tables.
  387.  *
  388.  *    RETURNS
  389.  *        Pointer to a valid string.
  390.  *        If no string is found, returns a pointer to a 0 length string.
  391.  *
  392.  */
  393. {
  394.     while( strcmp(pde->pstring,"") != 0 ){
  395.     if( pde->define == define) {
  396.         return(pde->pstring);
  397.     }
  398.     pde++;
  399.     }
  400.     return("");
  401. }
  402.  
  403. /*
  404.  *    NAME
  405.  *        LtStrToDefine - convert a string to a define
  406.  *
  407.  *    SYNOPSIS
  408.  */
  409. int
  410. LtStrToDefine(pde,pstring)
  411.     LtDefineEntry    pde[];    /* IN: table of X string-define pairs
  412.                  *     last entry must contain pair "", 0
  413.                  */
  414.     char    *pstring;    /* IN: string to be looked up in that table */
  415. /*
  416.  *    DESCRIPTION
  417.  *        Converts a string to an integer define.
  418.  *
  419.  *        Looks up the string in the table and returns the integer
  420.  *        associated with the string.
  421.  *
  422.  *        Later may need similar function for unsigned long define.
  423.  *
  424.  *        Check XuDefineString.h for predefined tables.
  425.  *
  426.  *
  427.  *    RETURNS
  428.  *        The int equivalent of the defined string.
  429.  *        -1 if the string is not found in table
  430.  *
  431.  */
  432. {
  433.     while( strcmp(pde->pstring,"") != 0 ){
  434.     if( strcmp(pde->pstring,pstring) == 0){
  435.         return(pde->define);
  436.     }
  437.     pde++;
  438.     }
  439.     return(-1);
  440. }
  441.