home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2096 / debug.c next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.2 KB  |  138 lines

  1. #include "defs.h"
  2.  
  3. #ifndef    lint
  4. static char SCCSid[] =
  5.     "@(#)debug.c: 2.4 Copyright 90/10/22 14:53:06 Chris Lewis";
  6. #endif
  7.  
  8. #ifdef    DEBUG
  9.  
  10. int debug = 0;
  11.  
  12. #define    D_CAT    1
  13. #define    D_SPEC    2
  14. #define    D_CHAR    4
  15. #define    D_FONT    8
  16. #define    D_BEND    0x10
  17. #define    D_PK    0x20
  18. #define    D_VERB    0x40
  19. #define    D_FLSH    0x80
  20.  
  21. struct dbm {
  22.     char req;
  23.     int bit;
  24. } dbm[] = {
  25.     {'c', D_CAT},
  26.     {'s', D_SPEC},
  27.     {'C', D_CHAR},
  28.     {'f', D_FONT},
  29.     {'F', D_FLSH},
  30.     {'b', D_BEND},
  31.     {'p', D_PK},
  32.     {'v', D_VERB},
  33.     {'A', ~0},
  34.     {0, 0}
  35. };
  36.  
  37. setdebug(str, df)
  38. char *str, *df; {
  39.     register struct dbm *d;
  40.     for(;*str; str++) {
  41.     for(d = dbm; d->req; d++)
  42.         if (d->req == *str) {
  43.         debug |= d->bit;
  44.         break;
  45.         }
  46.     if (!d->req) {
  47.         fprintf(stderr, "%s: don't understand %c debug flag\n",
  48.         progname, *str);
  49.         exit(1);
  50.     }
  51.     }
  52.  
  53.     if (debug) {
  54.     if (!(diagFile = fopen(df, "w"))) {
  55.         fprintf(stderr, "%s: Cannot open diagnostics file (%s)\n",
  56.         progname, df);
  57.         exit(1);
  58.     }
  59.     fprintf(diagFile, "Debug flags: %x\n", debug);
  60.     }
  61. }
  62.  
  63. #ifdef VFPRINTF
  64. #include <varargs.h>
  65. /* VARARGS */
  66. dprintf(level, va_alist)
  67. int level;
  68. va_dcl
  69. {
  70.     va_list args;
  71.     char *fmt;
  72.  
  73.     if (!(debug&level))
  74.         return;
  75.  
  76.     va_start(args);
  77.     fmt = va_arg(args, char *);
  78.     VFPRINTF(diagFile, fmt, args);
  79.     va_end(args);
  80.     if (debug&D_FLSH)
  81.         fflush(diagFile);
  82. }
  83. #else
  84. /* VARARGS1 ARGSUSED */
  85. dprintf(level, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
  86. int level;
  87. char    *fmt;
  88. int    a1, a2, a3, a4, a5, a6, a7, a8, a9, a10; {
  89.     char buf[BUFSIZ];
  90.  
  91.     if (!(debug&level))
  92.         return;
  93.  
  94.     sprintf(buf, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
  95.     fprintf(diagFile, buf);
  96.     if (debug&D_FLSH)
  97.         fflush(diagFile);
  98. }
  99. #endif
  100. #endif
  101.  
  102. char *
  103. mustmalloc(n, msg)
  104. int n;
  105. char *msg; {
  106.     extern char *malloc();
  107.     register char *p = malloc((unsigned) n);
  108.     if (!p) {
  109.     fprintf(stderr, "%s: Out of space! (requesting %d bytes, key: %s)\n",
  110.         progname, n, msg);
  111.     exit(1);
  112.     }
  113.     clrarray(p, n);
  114.     return(p);
  115. }
  116.  
  117. #ifdef    BCOPY
  118. #ifndef BCOPYLIB
  119. /*    "slowish" routines when you don't have memcpy and friends
  120.  */
  121. bcopy(from, to, len)
  122. register char *from, *to;
  123. register int len;
  124. {
  125.   while(len--)
  126.     *to++ = *from++;
  127. }
  128.  
  129. bzero(array, len)
  130. register char *array;
  131. register int len;
  132. {
  133.   while(len--)
  134.     *array++ = '\0';
  135. }
  136. #endif BCOPYLIB
  137. #endif
  138.