werror.c 3.03 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 28 29
 */

#include "werror.h"

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

32 33
int debug_flag = 0;
int quiet_flag = 0;
34
int verbose_flag = 0;
35

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

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

60 61 62 63 64 65 66 67 68 69 70 71
void verbose(char *format, ...) 
{
  va_list args;

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

72
void wash_char(UINT8 c)
73
{
74
  switch(c)
75
    {
76 77 78 79 80 81 82 83
    case '\\':
      fputs("\\\\", stderr);
      break;
    case '\r':
      /* Ignore */
      break;
    default:
      if (!isprint(c))
84
	{
85
	  fprintf(stderr, "\\x%02x", c);
86 87
	  break;
	}
88 89 90 91
      /* Fall through */
    case '\n':
      putc(c, stderr);
      break;
92 93 94
    }
}

95 96 97 98 99 100 101 102 103
/* Escape non-printable characters. */
void werror_washed(UINT32 length, UINT8 *msg)
{
  int i;

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

104 105 106 107 108 109 110 111 112 113 114
/* 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
115 116
}

117 118 119 120 121 122
void verbose_safe(UINT32 length, UINT8 *msg)
{
  if (verbose_flag)
    werror_washed(length, msg);
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
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
159 160 161 162 163 164 165 166 167 168
void fatal(char *format, ...) 
{
  va_list args;

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

  abort();
}