home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zoo21-2.zip / source / prterror.c < prev    next >
C/C++ Source or Header  |  1992-09-20  |  9KB  |  341 lines

  1. #ifndef LINT
  2. /* @(#) prterror.c 2.8 88/01/31 18:48:17 */
  3. static char sccsid[]="@(#) prterror.c 2.8 88/01/31 18:48:17";
  4. #endif /* LINT */
  5.  
  6. /*
  7. The contents of this file are hereby released to the public domain.
  8.  
  9.                                  -- Rahul Dhesi 1986/11/14
  10.  
  11. */
  12.  
  13. #include "options.h"
  14. #ifndef    OK_STDIO
  15. #include <stdio.h>
  16. #define    OK_STDIO
  17. #endif
  18. #include "various.h"
  19. #include "zoo.h"
  20. #include "zooio.h"
  21. #include "zoofns.h"
  22.  
  23. #ifdef NEEDCTYP
  24. # include <ctype.h>    /* for isdigit() */
  25. #endif
  26.  
  27. #ifdef STDARG
  28. # include <stdarg.h>
  29. #else
  30. # ifdef VARARGS
  31. #  include <varargs.h>
  32. # else
  33. #  include "MUST DEFINE STDARG OR VARARGS"
  34. # endif
  35. #endif
  36.  
  37. #ifdef NEED_VPRINTF
  38. static int zvfprintf();
  39. #endif
  40.  
  41. /* General error handler.  Input format:
  42.  
  43.    parameter 1:  'w', 'e', or 'f'.
  44.  
  45.       'm':  message
  46.       'M':  message without preceding identification
  47.       'w':  WARNING
  48.       'e':  ERROR
  49.       'f':  FATAL
  50.       'F':  FATAL but program doesn't exist immediately
  51.  
  52.    All text printed is preceded by "Zoo:  " or "Ooz:  "  depending
  53.    upon conditional compilation, except in the case of 'M' messages
  54.    which are printed without any text being added.
  55.  
  56.    For messages, the text supplied is printed if and only if the global
  57.    variable "quiet" is zero.  Control then returns to the caller.
  58.  
  59.    For warnings, errors, and fatal errors, the variable "quiet" is used
  60.     as follows.  Warning messages are suppressed if quiet > 1;  error
  61.     messages are suppressed if quiet > 2.  Fatal error messages are
  62.     never suppressed--doing so would be a bit risky.
  63.  
  64.    For warnings and errors, the error message is preceded by the "WARNING:"
  65.    or "ERROR".  The error message is printed and control returns to the
  66.    caller.
  67.  
  68.    For fatal errors, the error message is preceded by "FATAL:" and an
  69.    error message is printed.  If the option was 'f', the program exits with
  70.    a status of 1.  If the option was 'F', control returns to the caller and
  71.    it is assumed that the caller will do any cleaning up necessary and then
  72.    exit with an error status.
  73.  
  74.    parameter 2:  The format control string for printf.
  75.     remining parameters:  passed on to vprintf().
  76.  
  77.     All messages, whether informative or error, are sent to standard
  78.     output via printf.  It might be a good idea to eventually send 'e' and
  79.     'f' class messages to the standard error stream.  Best would be
  80.     some way of telling if standard output and standard error are not
  81.     the same device, so that we could always send error messages to
  82.     standard error, and also duplicate them to standard output if
  83.     different from standard error.  This is one thing that VMS seems
  84.     to be capable of doing.  There seems to be no way of doing this
  85.     in the general case.
  86. */
  87.  
  88. extern int quiet;
  89.  
  90. /* These declarations must be equivalent to those in errors.i */
  91. char no_match[] = "No files matched.\n";
  92. char failed_consistency[] = "Archive header failed consistency check.\n";
  93. char invalid_header[] = "Invalid or corrupted archive.\n";
  94. char internal_error[]="Internal error.\n";
  95. char disk_full[]      = "I/O error or disk full.\n";
  96. char bad_directory[]  = "Directory entry in archive is invalid.\n";
  97. char no_memory[] = "Ran out of memory.\n";
  98. char too_many_files[] = "Some filenames ignored -- can only handle %d.\n";
  99. char packfirst[] = "Old format archive -- please pack first with P command.\n";
  100. char garbled[] = "Command is garbled.\n";
  101. char start_ofs[] = "Starting at %ld (offset %ld)\n";
  102.  
  103. #ifndef OOZ
  104. char wrong_version[]=
  105.    "Zoo %d.%d or later is needed to fully manipulate this archive.\n";
  106. char cant_process[] =
  107.    "The rest of the archive (%lu bytes) cannot be processed.\n";
  108. char option_ignored[] = "Ignoring option %c.\n";
  109. char inv_option[] = "Option %c is invalid.\n";
  110. char bad_crc[] = "\007Bad CRC, %s probably corrupted\n";
  111. #endif
  112.  
  113. #ifdef OOZ
  114. char could_not_open[] = "Could not open ";
  115. #else
  116. char could_not_open[] = "Could not open %s.\n";
  117. #endif
  118.  
  119. #ifdef STDARG
  120. void prterror(int level, char *format, ...)
  121. #else
  122. /*VARARGS*/
  123. void prterror(va_alist)
  124. va_dcl
  125. #endif
  126. {
  127.     va_list args;
  128.    char string[120];       /* local format string */
  129. #ifdef VARARGS
  130.     int level;
  131.     char *format;
  132. #endif
  133.  
  134. #ifdef STDARG
  135.     va_start(args, format);
  136. #else
  137.     va_start(args);
  138.     level = va_arg(args, int);
  139.     format = va_arg(args, char *);
  140. #endif
  141.  
  142.    *string = '\0';         /* get a null string to begin with */
  143.  
  144. #ifdef OOZ
  145.    strcpy (string, "Ooz:  ");
  146. #else
  147.    strcpy (string, "Zoo:  ");
  148. #endif
  149.  
  150.    switch (level) {
  151.       case 'M': *string = '\0';                    /* fall through to 'm' */
  152.       case 'm': if (quiet) return; break;
  153.       case 'w':
  154.             if (quiet > 1) return;
  155.             strcat (string, "WARNING:  "); break;
  156.       case 'e':
  157.             if (quiet > 2) return;
  158.             strcat (string, "ERROR:  ");   break;
  159.       case 'F':
  160.       case 'f': strcat (string, "FATAL:  ");   break;
  161.       default: prterror ('f', internal_error);  /* slick recursive call */
  162.    }
  163.  
  164.    strcat (string, format);      /* just append supplied format string */
  165.  
  166.     /* and print the whole thing */
  167. #ifdef NEED_VPRINTF
  168.     (void) zvfprintf(stdout, string, args);
  169. #else
  170.    (void) vprintf(string, args);
  171. #endif
  172.     fflush (stdout);
  173.  
  174.    if (level == 'f')       /* and abort on fatal error 'f' but not 'F' */
  175.       zooexit (1);
  176. }
  177.  
  178.  
  179. #ifdef NEED_VPRINTF
  180. /* Some systems don't have vprintf;  if so, we roll our own.  The following
  181. has been adapted from a Usenet posting by Jef Poskanzer <jef@well.sf.ca.us>.
  182.  
  183. This is a portable mini-vfprintf that depends only on fprintf.
  184.  
  185. We don't call this routine vfprintf to avoid unexpected conflicts with any
  186. library routine of the same name, notwithstanding the fact that we will
  187. usually use it only when there is no conflict.  Also, even though we only
  188. need vprintf, the routine used here implements vfprintf.  This will allow
  189. future uses as needed when output is to be sent to a stream other than
  190. stdout.  */
  191.  
  192. /* Whether to support double.  Better not to, because it may cause
  193. math stuff to be linked in */
  194.  
  195. #undef NEED_DOUBLE
  196.  
  197. static int zvfprintf(stream, format, args)
  198. FILE *stream;
  199. char *format;
  200. va_list args;
  201. {
  202.     char *ep;
  203.     char fchar;
  204.     char tformat[512];
  205.     int do_long;        /* whether to print as long (l format suffix) */
  206.     int do_star;        /* * used in format => get width from argument */
  207.     int star_size;        /* size arg corresponding to "*" format */
  208.     int i;
  209.     long l;
  210.     unsigned u;
  211.     unsigned long ul;
  212.     char *s;
  213. #ifdef NEED_DOUBLE
  214.     double d;
  215. #endif
  216.  
  217.     while (*format != '\0') {
  218.         if (*format != '%') { /* Not special, just write out the char. */
  219.             putc(*format, stream);
  220.             ++format;
  221.        } else {
  222.             do_star = 0;
  223.             do_long = 0;
  224.             ep = format + 1;
  225.  
  226.             /* Skip over all the field width and precision junk. */
  227.             if (*ep == '-')
  228.                 ++ep;
  229.             if (*ep == '0')
  230.                 ++ep;
  231.             while (isdigit(*ep))
  232.                 ++ep;
  233.           if (*ep == '.') {
  234.                 ++ep;
  235.                 while (isdigit(*ep))
  236.                     ++ep;
  237.             }
  238.             if (*ep == '#')
  239.                 ++ep;
  240.             if (*ep == '*') {
  241.                 do_star = 1;
  242.                 star_size = va_arg(args, int);    /* get * argument */
  243.                 ++ep;
  244.             }
  245.             if (*ep == 'l') {
  246.                 do_long = 1;
  247.                 ++ep;
  248.             }
  249.  
  250.           /* Here's the field type.  Extract it, and copy this format
  251.           ** specifier to a temp string so we can add an end-of-string.
  252.           */
  253.           fchar = *ep;
  254.           (void) strncpy(tformat, format, ep - format + 1);
  255.           tformat[ep - format + 1] = '\0';
  256.  
  257.           /* Now do a one-argument printf with the format string we have
  258.             isolated.  If the * format was used, we will also supply the
  259.             additional parameter star_size, which we have already obtained
  260.             from the variable argument list.  */
  261.  
  262.           switch (fchar) {
  263.              case 'd':
  264.                 if (do_long) {
  265.                     l = va_arg(args, long);
  266.                     if (do_star)
  267.                         (void) fprintf(stream, tformat, star_size, l);
  268.                     else
  269.                         (void) fprintf(stream, tformat, l);
  270.                 } else {
  271.                     i = va_arg(args, int);
  272.                     if (do_star)
  273.                         (void) fprintf(stream, tformat, star_size, i);
  274.                     else
  275.                         (void) fprintf(stream, tformat, i);
  276.                 }
  277.                 break;
  278.  
  279.            case 'o':
  280.            case 'x':
  281.            case 'u':
  282.               if (do_long) {
  283.                     ul = va_arg(args, unsigned long);
  284.                     if (do_star)
  285.                         (void) fprintf(stream, tformat, star_size, ul);
  286.                     else
  287.                         (void) fprintf(stream, tformat, ul);
  288.               } else {
  289.                   u = va_arg(args, unsigned);
  290.                     if (do_star)
  291.                         (void) fprintf(stream, tformat, star_size, u);
  292.                     else
  293.                         (void) fprintf(stream, tformat, u);
  294.               }
  295.                break;
  296.  
  297.              case 'c':
  298.                 i = (char) va_arg(args, int);
  299.                 if (do_star)
  300.                     (void) fprintf(stream, tformat, star_size, i);
  301.                 else
  302.                     (void) fprintf(stream, tformat, i);
  303.                 break;
  304.  
  305.              case 's':
  306.                 s = va_arg(args, char *);
  307.                 if (do_star)
  308.                     (void) fprintf(stream, tformat, star_size, s);
  309.                 else
  310.                     (void) fprintf(stream, tformat, s);
  311.                 break;
  312.  
  313. #ifdef NEED_DOUBLE
  314.            case 'e':
  315.            case 'f':
  316.            case 'g':
  317.                 d = va_arg(args, double);
  318.                 if (do_star)
  319.                     (void) fprintf(stream, tformat, star_size, d);
  320.                 else
  321.                     (void) fprintf(stream, tformat, d);
  322.                 break;
  323. #endif
  324.  
  325.            case '%':
  326.                 putc('%', stream);
  327.                 break;
  328.  
  329.              default:
  330.                 return -1;
  331.             }
  332.  
  333.             /* Resume formatting on the next character. */
  334.             format = ep + 1;
  335.         }
  336.     }
  337.     va_end(args);
  338.     return 0;
  339. }
  340. #endif /*NEED_VPRINTF*/
  341.