werror.c 4 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 29
#include "charset.h"

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

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

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

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

62 63 64 65 66 67 68 69 70 71 72 73
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
74
static void wash_char(UINT8 c)
75
{
76
  switch(c)
77
    {
78 79 80 81 82 83 84 85
    case '\\':
      fputs("\\\\", stderr);
      break;
    case '\r':
      /* Ignore */
      break;
    default:
      if (!isprint(c))
86
	{
87
	  fprintf(stderr, "\\x%02x", c);
88 89
	  break;
	}
90 91 92 93
      /* Fall through */
    case '\n':
      putc(c, stderr);
      break;
94 95 96
    }
}

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

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

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

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

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

125
static void write_utf8(UINT32 length, UINT8 *msg)
126
{
Niels Möller's avatar
Niels Möller committed
127
  struct simple_buffer buffer;
128

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

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

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

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

174
/* Bignums */
Niels Möller's avatar
Niels Möller committed
175 176
void werror_mpz(mpz_t n)
{
Niels Möller's avatar
Niels Möller committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190
  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
191
}
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

/* 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();
}