home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / misc.c < prev    next >
C/C++ Source or Header  |  1993-10-21  |  6KB  |  238 lines

  1. /* misc.c:
  2.  *
  3.  * miscellaneous funcs
  4.  *
  5.  * jim frost 10.05.89
  6.  *
  7.  * Copyright 1989, 1990, 1991 Jim Frost.
  8.  * See included file "copyright.h" for complete copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "xloadimage.h"
  13. #ifdef VMS
  14. #include "patchlevel."
  15. #else
  16. #include "patchlevel"
  17. #endif
  18. #include <signal.h>
  19.  
  20. extern int      _Xdebug;
  21. extern char    *ProgramName;
  22. extern Display *Disp;
  23. extern int      Scrn;
  24. extern char    *BuildDate;
  25. extern char    *BuildUser;
  26. extern char    *BuildSystem;
  27.  
  28. static char *signalName(sig)
  29.      int sig;
  30. { static char buf[32];
  31.  
  32.   switch (sig) {
  33.   case SIGSEGV:
  34.     return("SEGV");
  35.   case SIGBUS:
  36.     return("BUS");
  37.   case SIGFPE:
  38.     return("FPE");
  39.   case SIGILL:
  40.     return("ILL");
  41.   default:
  42.     sprintf(buf, "Signal %d", sig);
  43.     return(buf);
  44.   }
  45. }
  46.  
  47. void memoryExhausted()
  48. {
  49.   fprintf(stderr,
  50.       "\n\nMemory has been exhausted; operation cannot continue (sorry).\n");
  51.   if (_Xdebug)
  52.     abort();
  53.   else
  54.     exit(1);
  55. }
  56.  
  57. void internalError(sig)
  58.      int sig;
  59. { static int handling_error= 0;
  60.   int a, b;
  61.   Screen *screen;
  62.  
  63.   switch(handling_error++) {
  64.   case 0:
  65.     printf("\n\n\
  66. An internal error (%s) has occurred.  If you would like to file a bug\n\
  67. report, please send email to %s\n\
  68. with a description of how you triggered the bug, the output of xloadimage\n\
  69. before the failure, and the following information:\n\n", signalName(sig),
  70.        AUTHOR_EMAIL);
  71.     printf("Xloadimage Version %s.%s\n\n", VERSION, PATCHLEVEL);
  72.     if (BuildUser)
  73.       printf("Built by:     %s\n", BuildUser);
  74.     if (BuildDate)
  75.       printf("Built on:     %s\n", BuildDate);
  76.     printf("Build system: %s\n", BuildSystem);
  77.     
  78.     if (Disp) {
  79.       screen= ScreenOfDisplay(Disp, Scrn);
  80.       printf("Server: %s Version %d\n", ServerVendor(Disp), VendorRelease(Disp));
  81.       printf("Depths and visuals supported:\n");
  82.       for (a= 0; a < screen->ndepths; a++) {
  83.     printf("%2d:", screen->depths[a].depth);
  84.     for (b= 0; b < screen->depths[a].nvisuals; b++)
  85.       printf(" %s", nameOfVisualClass(screen->depths[a].visuals[b].class));
  86.     printf("\n");
  87.       }
  88.     }
  89.     else
  90.       printf("[No information on server; error occurred before connection]\n");
  91.     break;
  92.   case 1:
  93.     fprintf(stderr, "\n\n\
  94. An internal error has occurred within the internal error handler.  No more\n\
  95. information about the error is available, sorry.\n");
  96.     exit(1);
  97.     break;
  98.   }
  99.   if (_Xdebug) /* dump core if -debug is on */
  100.     abort();
  101.   exit(1);
  102. }
  103.  
  104. void version()
  105. {
  106.   printf("Xloadimage version %s.%s by Jim Frost.\n",
  107.      VERSION, PATCHLEVEL);
  108.   printf("Built on %s\n", BuildSystem);
  109.   printf("Please send email to %s for\npraise or bug reports.\n",
  110.      AUTHOR_EMAIL);
  111. }
  112.  
  113. void usageHelp()
  114. {
  115.   printf("\nUsage: %s [global options] {[image options] image_name ...}\n\n",
  116.      tail(ProgramName));
  117.   printf("\
  118. Type `%s -help [option ...]' for information on a particular option, or\n\
  119. `%s -help' to enter the interactive help facility.\n",
  120.      tail(ProgramName), tail(ProgramName));
  121.   exit(1);
  122. }
  123.  
  124. void usage()
  125. {
  126.   version();
  127.   usageHelp();
  128.   /* NOTREACHED */
  129. }
  130.  
  131. char *tail(path)
  132.      char *path;
  133. { int   s;
  134.   char *t;
  135.  
  136.   t= path;
  137.   for (s= 0; *(path + s) != '\0'; s++)
  138.     if (*(path + s) == '/')
  139.       t= path + s + 1;
  140.   return(t);
  141. }
  142.  
  143. /* simple error handler.  this provides us with some kind of error recovery.
  144.  */
  145.  
  146. int errorHandler(disp, error)
  147.      Display *disp;
  148.      XErrorEvent *error;
  149. { char errortext[BUFSIZ];
  150.  
  151.   XGetErrorText(disp, error->error_code, errortext, BUFSIZ);
  152.   fprintf(stderr, "xloadimage: X Error: %s on 0x%x\n",
  153.       errortext, error->resourceid);
  154.   if (_Xdebug) /* if -debug mode is enabled, dump a core when we hit this */
  155.     abort();
  156.   else
  157.     return(0);
  158. }
  159.  
  160. /*
  161.   findstr - public-domain implementation of standard C 'strstr' library
  162.             function (renamed and slightly modified to avoid naming
  163.             conflicts - jimf)
  164.  
  165.   last edit:    02-Sep-1990    D A Gwyn
  166.  
  167.   This is an original implementation based on an idea by D M Sunday,
  168.   essentially the "quick search" algorithm described in CACM V33 N8.
  169.   Unlike Sunday's implementation, this one does not wander past the
  170.   ends of the strings (which can cause malfunctions under certain
  171.   circumstances), nor does it require the length of the searched
  172.   text to be determined in advance.  There are numerous other subtle
  173.   improvements too.  The code is intended to be fully portable, but in
  174.   environments that do not conform to the C standard, you should check
  175.   the sections below marked "configure as required".  There are also
  176.   a few compilation options, as follows:
  177. */
  178.  
  179. #define BYTE_MAX 255
  180.  
  181. #define EOS '\0'        /* C string terminator */
  182.  
  183. char *                    /* returns -> leftmost occurrence,
  184.                        or null pointer if not present */
  185. findstr( s1, s2 )
  186.      char    *s1;        /* -> string to be searched */
  187.      char    *s2;        /* -> search-pattern string */
  188. {
  189.   register byte    *t;        /* -> text character being tested */
  190.   register byte    *p;        /* -> pattern char being tested */
  191.   register byte    *tx;        /* -> possible start of match */
  192.   register unsigned int    m;      /* length of pattern */
  193.   register byte    *top;        /* -> high water mark in text */
  194.   unsigned int  shift[BYTE_MAX + 1];    /* pattern shift table */
  195.  
  196.   if ( s1 == NULL || s2 == NULL )
  197.     return NULL;        /* certainly, no match is found! */
  198.  
  199.   /* Precompute shift intervals based on the pattern;
  200.      the length of the pattern is determined as a side effect: */
  201.  
  202.   bzero(&shift[1], (BYTE_MAX * sizeof(unsigned int)));
  203.  
  204.   /* Note: shift[0] is undefined at this point (fixed later). */
  205.  
  206.   for ( m = 1, p = (byte *)s2; *p != EOS; ++m, ++p )
  207.     shift[(byte)*p] = m;
  208.  
  209.   {
  210.     register byte c;
  211.  
  212.     c = BYTE_MAX;
  213.     do
  214.       shift[c] = m - shift[c];
  215.     while ( --c > 0 );
  216.     /* Note: shift[0] is still undefined at this point. */
  217.   }
  218.  
  219.   shift[0] = --m;         /* shift[EOS]; important details! */
  220.  
  221.   /* Try to find the pattern in the text string: */
  222.  
  223.   for ( top = tx = (byte *)s1; ; tx += shift[*(top = t)] ) {
  224.     for ( t = tx, p = (byte *)s2; ; ++t, ++p ) {
  225.       if ( *p == EOS )       /* entire pattern matched */
  226.     return (char *)tx;
  227.       if ( *p != *t )
  228.     break;
  229.     }
  230.     if ( t < top ) /* idea due to ado@elsie.nci.nih.gov */
  231.       t = top;       /* already scanned this far for EOS */
  232.     do    {
  233.       if ( *t == EOS )
  234.     return NULL;    /* no match */
  235.     } while ( ++t - tx != m );    /* < */
  236.   }
  237. }
  238.