werror.c 4.02 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2
/* werror.c
 *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Niels Möller's avatar
Niels Möller committed
24 25 26 27
 */

#include "werror.h"

Niels Möller's avatar
Niels Möller committed
28
#include "charset.h"
29
#include "parse.h"
Niels Möller's avatar
Niels Möller committed
30

Niels Möller's avatar
Niels Möller committed
31 32
#include <stdio.h>
#include <stdarg.h>
Niels Möller's avatar
Niels Möller committed
33
#include <ctype.h>
Niels Möller's avatar
Niels Möller committed
34

35 36
int debug_flag = 0;
int quiet_flag = 0;
37
int verbose_flag = 0;
38

Niels Möller's avatar
Niels Möller committed
39 40 41 42
void werror(char *format, ...) 
{
  va_list args;

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  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);
    }
}

63 64 65 66 67 68 69 70 71 72 73 74
void verbose(char *format, ...) 
{
  va_list args;

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

Niels Möller's avatar
Niels Möller committed
75
static void wash_char(UINT8 c)
76
{
77
  switch(c)
78
    {
79 80 81 82 83 84 85 86
    case '\\':
      fputs("\\\\", stderr);
      break;
    case '\r':
      /* Ignore */
      break;
    default:
      if (!isprint(c))
87
	{
88
	  fprintf(stderr, "\\x%02x", c);
89 90
	  break;
	}
91 92 93 94
      /* Fall through */
    case '\n':
      putc(c, stderr);
      break;
95 96 97
    }
}

98
/* Escape non-printable characters. */
99
static void write_washed(UINT32 length, UINT8 *msg)
100 101 102 103 104 105 106
{
  int i;

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

107 108 109 110
/* For outputting data recieved from the other end */
void werror_safe(UINT32 length, UINT8 *msg)
{
  if (!quiet_flag)
111
    write_washed(length, msg);
112 113 114 115 116
}

void debug_safe(UINT32 length, UINT8 *msg)
{
  if (debug_flag)
117
    write_washed(length, msg);
Niels Möller's avatar
Niels Möller committed
118 119
}

120 121 122
void verbose_safe(UINT32 length, UINT8 *msg)
{
  if (verbose_flag)
123
    write_washed(length, msg);
124 125
}

126
static void write_utf8(UINT32 length, UINT8 *msg)
127
{
Niels Möller's avatar
Niels Möller committed
128
  struct simple_buffer buffer;
129 130
  
  simple_buffer_init(&buffer, length, msg);
Niels Möller's avatar
Niels Möller committed
131 132
  
  while(1)
133
    {
Niels Möller's avatar
Niels Möller committed
134 135 136 137 138 139 140
      UINT32 ucs4;
      
      switch (parse_utf8(&buffer, &ucs4))
	{
	case -1:
	  return;
	case 0:
141
	  fputs("\\!", stderr);
Niels Möller's avatar
Niels Möller committed
142 143 144 145 146 147 148 149 150 151 152 153
	  return;
	case 1:
	  {
	    int local = ucs4_to_local(ucs4);
	    if (local < 0)
	      fputs("\\?", stderr);
	    else
	      wash_char(local);
	  }
	default:
	  fatal("Internal error");
	}
154
    }
Niels Möller's avatar
Niels Möller committed
155
}
156 157

void werror_utf8(UINT32 length, UINT8 *msg)
Niels Möller's avatar
Niels Möller committed
158
{
159 160 161
  if (!quiet_flag)
    write_utf8(length, msg);
}
Niels Möller's avatar
Niels Möller committed
162

163 164 165 166 167
void verbose_utf8(UINT32 length, UINT8 *msg)
{
  if (verbose_flag)
    write_utf8(length, msg);
}
Niels Möller's avatar
Niels Möller committed
168

169 170 171 172
void debug_utf8(UINT32 length, UINT8 *msg)
{
  if (debug_flag)
    write_utf8(length, msg);
Niels Möller's avatar
Niels Möller committed
173
}
Niels Möller's avatar
Niels Möller committed
174

175
/* Bignums */
Niels Möller's avatar
Niels Möller committed
176 177
void werror_mpz(mpz_t n)
{
Niels Möller's avatar
Niels Möller committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191
  if (!quiet_flag)
    mpz_out_str(stderr, 16, n);
}

void debug_mpz(mpz_t n)
{
  if (debug_flag)
    mpz_out_str(stderr, 16, n);
}

void verbose_mpz(mpz_t n)
{
  if (verbose_flag)
    mpz_out_str(stderr, 16, n);
Niels Möller's avatar
Niels Möller committed
192
}
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

/* hex dumps */
static void write_hex(UINT32 length, UINT8 *data)
{
  int i;
  
  fprintf(stderr, "(size %d = 0x%x)",
	  length, length);

  for(i=0; i<length; i++)
  {
    if (! (i%16))
      fprintf(stderr, "\n%08x: ", i);
    
    fprintf(stderr, "%02x ", data[i]);
  }
  fprintf(stderr, "\n");
}

void werror_hex(UINT32 length, UINT8 *data)
{
  if (!quiet_flag)
    write_hex(length, data);
}

void debug_hex(UINT32 length, UINT8 *data)
{
  if (debug_flag)
    write_hex(length, data);
}

void verbose_hex(UINT32 length, UINT8 *data)
{
  if (verbose_flag)
    write_hex(length, data);
}

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

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

  abort();
}