werror.c 1.27 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 13
int debug_flag = 0;
int quiet_flag = 0;

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

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  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);
    }
}

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

Niels Möller's avatar
Niels Möller committed
43
  for(i = 0; i<length; i++)
44 45 46 47 48 49 50 51 52 53 54 55
    {
      switch(msg[i])
	{
	case '\\':
	  fputs("\\\\", stderr);
	  break;
	case '\r':
	  /* Ignore */
	  break;
	default:
	  if (!isprint(msg[i]))
	    {
Niels Möller's avatar
Niels Möller committed
56
	      fprintf(stderr, "\\x%2x", msg[i]);
57 58 59 60
	      break;
	    }
	  /* Fall through */
	case '\n':
Niels Möller's avatar
Niels Möller committed
61
	  putc(msg[i], stderr);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	  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
78 79 80 81 82 83 84 85 86 87 88 89
}

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

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

  abort();
}