home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume24 / psroff3.0 / part18 / debug.c next >
Encoding:
C/C++ Source or Header  |  1991-10-09  |  2.3 KB  |  138 lines

  1. /*
  2.     Copyright 1985, 1986, 1987, 1988, 1989, 1990, 1991 Chris Lewis
  3.         All Rights Reserved
  4.  
  5.     See the LICENSE file for a full description of restrictions under which
  6.     this software is provided.
  7.  
  8.     Function: debugging facilities
  9.  */
  10. #include "defs.h"
  11.  
  12. #ifndef    lint
  13. static char SCCSid[] =
  14.     "@(#)debug.c: 2.6 Copyright 91/02/20 09:00:22 Chris Lewis";
  15. #endif
  16.  
  17. #ifdef    DEBUG
  18.  
  19. int debug = 0;
  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.