werror.c 2.25 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
void wash_char(UINT8 c)
52
{
53
  switch(c)
54
    {
55 56 57 58 59 60 61 62
    case '\\':
      fputs("\\\\", stderr);
      break;
    case '\r':
      /* Ignore */
      break;
    default:
      if (!isprint(c))
63
	{
64
	  fprintf(stderr, "\\x%02x", c);
65 66
	  break;
	}
67 68 69 70
      /* Fall through */
    case '\n':
      putc(c, stderr);
      break;
71 72 73
    }
}

74 75 76 77 78 79 80 81 82
/* Escape non-printable characters. */
void werror_washed(UINT32 length, UINT8 *msg)
{
  int i;

  for(i = 0; i<length; i++)
    wash_char(msg[i]);
}

83 84 85 86 87 88 89 90 91 92 93
/* 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
94 95
}

96 97 98 99 100 101
void verbose_safe(UINT32 length, UINT8 *msg)
{
  if (verbose_flag)
    werror_washed(length, msg);
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
void werror_safe_utf8(UINT32 length, UINT8 *msg)
{
  /* FIXME: This function assumes that the system charset is
   * iso-8859-1, aka latin1. */

  if (!quiet_flag)
    {
      int i=0;
      
      while(i<length)
	{
	  UINT8 c = msg[i++];
	  if (!(c & 0x80))
	    wash_char(c);
	  else
	    {
	      if ( (c & 0xd0) != 0xc0)
		{
		  /* Unicode value >= 0x800 */
		  fputs("\\?", stderr);
		  while ( (i < length) & (msg[i] & 0x80) )
		    i++;
		}
	      else
		{
		  if (i == length)
		    /* String ends with a partial character! */
		    fputs("\\!", stderr);
		  else
		    wash_char( ((c & 3) << 6) || (msg[i++] & 0x3f) ); 
		}
	    }
	}
    }
} 
  
Niels Möller's avatar
Niels Möller committed
138 139 140 141 142 143 144 145 146 147
void fatal(char *format, ...) 
{
  va_list args;

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

  abort();
}