home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / cpp114.zoo / src / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-20  |  4.6 KB  |  216 lines

  1.  
  2. /*---------------------------------------------------------------------*\
  3. |                                    |
  4. | CPP -- a stand-alone C preprocessor                    |
  5. | Copyright (c) 1993-95 Hacker Ltd.        Author: Scott Bigham    |
  6. |                                    |
  7. | Permission is granted to anyone to use this software for any purpose    |
  8. | on any computer system, and to redistribute it freely, with the    |
  9. | following restrictions:                        |
  10. | - No charge may be made other than reasonable charges for repro-    |
  11. |     duction.                                |
  12. | - Modified versions must be clearly marked as such.            |
  13. | - The author is not responsible for any harmful consequences of    |
  14. |     using this software, even if they result from defects therein.    |
  15. |                                    |
  16. | utils.c -- assorted utility routines                    |
  17. \*---------------------------------------------------------------------*/
  18.  
  19. #include <stdio.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #if defined(__STDC__) || defined(__SOZOBONC__) || defined(__SOZOBONX__)
  23. #include <stdarg.h>
  24. #else
  25. #include <varargs.h>
  26. #endif
  27. #include "global.h"
  28. #include "ztype.h"
  29.  
  30. #define EM_ERROR  0
  31. #define EM_WARN   1
  32. #define EM_FATAL  2
  33. #define EM_BUGCHK 3
  34.  
  35. #ifdef __STDC__
  36. #define __EM_fmt__ (const char *fmt, ...)
  37. #else
  38. # if defined(__SOZOBONC__) || defined(__SOZOBONX__)
  39. # define __EM_fmt__ (fmt) char *fmt;
  40. # else
  41. # define __EM_fmt__ (va_alist) va_dcl
  42. # endif
  43. #endif
  44.  
  45. extern int nerrs;
  46.  
  47. static char *em_text[] =
  48. {"", " warning:", " FATAL:", " compiler bug:"};
  49.  
  50. /* em_header() -- add file and line number information to error message */
  51. static void em_header(n)
  52.   int n;
  53. {
  54.   if (cur_file)
  55.     fprintf(stderr, "In file \"%s\" (%lu):%s ", cur_file, this_line, em_text[n]);
  56.   else
  57.     fprintf(stderr, "%s:%s ", argv0, em_text[n]);
  58. }
  59.  
  60. /* fatal() -- print an error message and punt */
  61. void fatal __EM_fmt__
  62. {
  63.   va_list ap;
  64.  
  65.   em_header(EM_FATAL);
  66.   va_start(ap, fmt);
  67.   vfprintf(stderr, fmt, ap);
  68.   va_end(ap);
  69.   fputc('\n', stderr);
  70.   exit(1);
  71. }
  72.  
  73. /*
  74.    bugchk() -- similar to fatal(), but reserved for flagging compiler bugs
  75. */
  76. void bugchk __EM_fmt__
  77. {
  78.   va_list ap;
  79.  
  80.   em_header(EM_BUGCHK);
  81.   va_start(ap, fmt);
  82.   vfprintf(stderr, fmt, ap);
  83.   va_end(ap);
  84.   fputc('\n', stderr);
  85.   exit(1);
  86. }
  87.  
  88. /* error() -- print an error message and bump the error counter */
  89. void error __EM_fmt__
  90. {
  91.   va_list ap;
  92.  
  93.   em_header(EM_ERROR);
  94.   va_start(ap, fmt);
  95.   vfprintf(stderr, fmt, ap);
  96.   va_end(ap);
  97.   fputc('\n', stderr);
  98.   nerrs++;
  99. }
  100.  
  101. /* warning() -- print an error/informational message */
  102. void warning __EM_fmt__
  103. {
  104.   va_list ap;
  105.  
  106.   em_header(EM_WARN);
  107.   va_start(ap, fmt);
  108.   vfprintf(stderr, fmt, ap);
  109.   va_end(ap);
  110.   fputc('\n', stderr);
  111. }
  112.  
  113. /* message() -- print an informational message */
  114. void message __EM_fmt__
  115. {
  116.   va_list ap;
  117.  
  118.   em_header(EM_ERROR);
  119.   va_start(ap, fmt);
  120.   vfprintf(stderr, fmt, ap);
  121.   va_end(ap);
  122.   fputc('\n', stderr);
  123. }
  124.  
  125. /* mallok() -- a paranoia wrapper around malloc() */
  126. void *mallok(n)
  127.   size_t n;
  128. {
  129.   register void *t = malloc(n);
  130.  
  131.   if (!t)
  132.     fatal("cannot get %lu bytes", (unsigned long)n);
  133.   return t;
  134. }
  135.  
  136. /* reallok() -- a paranoia wrapper around realloc() */
  137. void *reallok(t, n)
  138.   void *t;
  139.   size_t n;
  140. {
  141.   register void *u = realloc(t, n);
  142.  
  143.   if (!u)
  144.     fatal("cannot get %lu bytes", (unsigned long)n);
  145.   return u;
  146. }
  147.  
  148. /*
  149.    copy_filename() -- return malloc()'ed memory containing the first |len|
  150.    characters of |fnam|, or all of |fnam| if |len==0|
  151. */
  152. char *copy_filename(fnam, len)
  153.   char *fnam;
  154.   int len;
  155. {
  156.   char *newfnam;
  157.   register char *s = fnam, *t;
  158.   register int i;
  159.  
  160.   if (len == 0)
  161.     len = strlen(fnam);
  162.   t = newfnam = mallok(len + 1);
  163.   for (i = 0; i < len; s++, t++, i++)
  164.     *t = ((*s == '/' || *s == '\\') ? PATH_SEP : *s);
  165.   *t = '\0';
  166.   return newfnam;
  167. }
  168.  
  169. #ifndef __GNUC__
  170. /* strdup() -- return malloc()'ed space containing a copy of |s| */
  171. char *strdup(s)
  172.   register const char *s;
  173. {
  174.   return strcpy((char *)mallok(strlen(s) + 1), s);
  175. }
  176.  
  177. #endif
  178.  
  179. /*
  180.    xfopen() -- like fopen(), except that setvbuf() is called to increase the
  181.    buffer size
  182. */
  183. FILE *xfopen(fnam, mode)
  184.   const char *fnam, *mode;
  185. {
  186.   FILE *f = fopen(fnam, mode);
  187.  
  188.   if (!f)
  189.     fatal("cannot open %s file %s", (*mode == 'r' ? "input" : "output"),
  190.       fnam);
  191.   if (setvbuf(f, NULL, _IOFBF, NEWBUFSIZ) != 0)
  192.     fatal("cannot get file buffer for %s", fnam);
  193.   return f;
  194. }
  195.  
  196. /*
  197.    grow() -- expand buffer |*buf|, originally of length |*buflen|, to hold an
  198.    additional |add_len| characters.  |in_ptr| is a pointer into the buffer;
  199.    return a pointer into the new buffer with the same offset.
  200. */
  201. char *grow(buf, buflen, in_ptr, add_len)
  202.   char **buf;
  203.   size_t *buflen;
  204.   char *in_ptr;
  205.   int add_len;
  206. {
  207.   ptrdiff_t dp = in_ptr - *buf;
  208.  
  209.   if (dp + add_len <= *buflen)
  210.     return in_ptr;
  211.   while (*buflen < dp + add_len)
  212.     *buflen *= 2;
  213.   *buf = reallok(*buf, *buflen);
  214.   return *buf + dp;
  215. }
  216.