werror.c 1.57 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3 4 5 6 7 8
/* werror.c
 *
 */

#include "werror.h"

#include <stdio.h>
#include <stdarg.h>
Niels Möller's avatar
Niels Möller committed
9
#include <ctype.h>
Niels Möller's avatar
Niels Möller committed
10

11 12
int debug_flag = 0;
int quiet_flag = 0;
13
int verbose_flag = 0;
14

Niels Möller's avatar
Niels Möller committed
15 16 17 18
void werror(char *format, ...) 
{
  va_list args;

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  if (!quiet_flag)
    {
      va_start(args, format);
      vfprintf(stderr, format, args);
      va_end(args);
    }
}

void debug(char *format, ...) 
{
  va_list args;

  if (debug_flag)
    {
      va_start(args, format);
      vfprintf(stderr, format, args);
      va_end(args);
    }
}

39 40 41 42 43 44 45 46 47 48 49 50
void verbose(char *format, ...) 
{
  va_list args;

  if (verbose_flag)
    {
      va_start(args, format);
      vfprintf(stderr, format, args);
      va_end(args);
    }
}

51 52 53 54 55
/* Escape non-printable characters. */
void werror_washed(UINT32 length, UINT8 *msg)
{
  int i;

Niels Möller's avatar
Niels Möller committed
56
  for(i = 0; i<length; i++)
57 58 59 60 61 62 63 64 65 66 67 68
    {
      switch(msg[i])
	{
	case '\\':
	  fputs("\\\\", stderr);
	  break;
	case '\r':
	  /* Ignore */
	  break;
	default:
	  if (!isprint(msg[i]))
	    {
69
	      fprintf(stderr, "\\x%02x", msg[i]);
70 71 72 73
	      break;
	    }
	  /* Fall through */
	case '\n':
Niels Möller's avatar
Niels Möller committed
74
	  putc(msg[i], stderr);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	  break;
	}
    }
}

/* For outputting data recieved from the other end */
void werror_safe(UINT32 length, UINT8 *msg)
{
  if (!quiet_flag)
    werror_washed(length, msg);
}

void debug_safe(UINT32 length, UINT8 *msg)
{
  if (debug_flag)
    werror_washed(length, msg);
Niels Möller's avatar
Niels Möller committed
91 92
}

93 94 95 96 97 98
void verbose_safe(UINT32 length, UINT8 *msg)
{
  if (verbose_flag)
    werror_washed(length, msg);
}

Niels Möller's avatar
Niels Möller committed
99 100 101 102 103 104 105 106 107 108
void fatal(char *format, ...) 
{
  va_list args;

  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);

  abort();
}