tttm

git clone https://orangeshoelaces.net/git/tttm.git

/errors.c

   1 #include <errno.h>
   2 #include <stdarg.h>
   3 #include <stdio.h>
   4 #include <stdlib.h>
   5 #include <string.h>
   6 #include <unistd.h>
   7 
   8 #include "errors.h"
   9 
  10 const char *errmsgs[] = {
  11         [TE_PARSE]      = "input not recognized",
  12         [TE_EOF]        = "unexpected eof",
  13         [TE_TIMEOUT]    = "source read timed out",
  14         [TE_PTOFLOW]    = "cannot fit parse tree within supplied space",
  15         [TE_XLSTR]      = "stalled upon a string longer than 2^32 - 1",
  16         [TE_PROTO]      = "IMAP protocol violated",
  17         [TE_NO]         = "got NO result",
  18         [TE_BAD]        = "got BAD result",
  19         [TE_BYE]        = "server terminated the session",
  20         [TE_NIL]        = "got NIL message",
  21         [TE_CMDFMT]     = "command draws unsupported conversion specifier",
  22         [TE_XLCMD]      = "command is too long",
  23         [TE_NOMEM]      = "not enough memory",
  24         [TE_IN]         = "input error",
  25         [TE_OUT]        = "output error",
  26 };
  27 
  28 static void vwarn(char *, va_list);
  29 static void vwarnx(char *, va_list);
  30 
  31 void warn(char *fmt, ...)
  32 {
  33         va_list ap;
  34 
  35         va_start(ap, fmt);
  36         vwarn(fmt, ap);
  37         va_end(ap);
  38 }
  39 
  40 void warnx(char *fmt, ...)
  41 {
  42         va_list ap;
  43 
  44         va_start(ap, fmt);
  45         vwarnx(fmt, ap);
  46         va_end(ap);
  47 }
  48 
  49 void err(int status, char *fmt, ...)
  50 {
  51         va_list ap;
  52 
  53         va_start(ap, fmt);
  54         vwarn(fmt, ap);
  55         va_end(ap);
  56         exit(status);
  57 }
  58 
  59 void errx(int status, char *fmt, ...)
  60 {
  61         va_list ap;
  62 
  63         va_start(ap, fmt);
  64         vwarnx(fmt, ap);
  65         va_end(ap);
  66         exit(status);
  67 }
  68 
  69 void vwarn(char *fmt, va_list ap)
  70 {
  71         dprintf(STDERR_FILENO, fmt ? "%s: " : "%s", prog);
  72         if (fmt) vdprintf(STDERR_FILENO, fmt, ap);
  73         dprintf(STDERR_FILENO, ": %s\n", strerror(errno));
  74 }
  75 
  76 void vwarnx(char *fmt, va_list ap)
  77 {
  78         dprintf(STDERR_FILENO, fmt ? "%s: " : "%s", prog);
  79         if (fmt) vdprintf(STDERR_FILENO, fmt, ap);
  80         dprintf(STDERR_FILENO, "\n");
  81 }