hexl.c 5.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Convert files for Emacs Hexl mode.
   Copyright (C) 1989 Free Software Foundation, Inc

This file is not considered part of GNU Emacs.

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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

20 21 22 23
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jim Blandy's avatar
Jim Blandy committed
24 25
#include <stdio.h>
#include <ctype.h>
26
#ifdef DOS_NT
27
#include <fcntl.h>
28 29 30
#if __DJGPP__ >= 2
#include <io.h>
#endif
31
#endif
32 33 34
#ifdef WINDOWSNT
#include <io.h>
#endif
Jim Blandy's avatar
Jim Blandy committed
35 36 37 38 39 40 41 42 43 44 45 46 47

#define DEFAULT_GROUPING	0x01
#define DEFAULT_BASE		16

#undef TRUE
#undef FALSE
#define TRUE  (1)
#define FALSE (0)

int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
int group_by = DEFAULT_GROUPING;
char *progname;

48 49 50
void usage();

int
51 52 53
main (argc, argv)
     int argc;
     char *argv[];
Jim Blandy's avatar
Jim Blandy committed
54
{
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  register long address;
  char string[18];
  FILE *fp;

  progname = *argv++; --argc;

  /*
   ** -hex		hex dump
   ** -oct		Octal dump
   ** -group-by-8-bits
   ** -group-by-16-bits
   ** -group-by-32-bits
   ** -group-by-64-bits
   ** -iso		iso character set.
   ** -big-endian	Big Endian
   ** -little-endian	Little Endian
   ** -un || -de	from hexl format to binary.
   ** --		End switch list.
   ** <filename>	dump filename
   ** -		(as filename == stdin)
   */
Pavel Janík's avatar
Pavel Janík committed
76

77
  while (*argv && *argv[0] == '-' && (*argv)[1])
Jim Blandy's avatar
Jim Blandy committed
78
    {
79 80
      /* A switch! */
      if (!strcmp (*argv, "--"))
Jim Blandy's avatar
Jim Blandy committed
81
	{
82 83 84 85
	  --argc; argv++;
	  break;
	}
      else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
Jim Blandy's avatar
Jim Blandy committed
86
	{
87 88 89 90
	  un_flag = TRUE;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-hex"))
Jim Blandy's avatar
Jim Blandy committed
91
	{
92 93 94 95
	  base = 16;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-iso"))
Jim Blandy's avatar
Jim Blandy committed
96
	{
97 98 99 100
	  iso_flag = TRUE;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-oct"))
Jim Blandy's avatar
Jim Blandy committed
101
	{
102 103 104 105
	  base = 8;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-big-endian"))
Jim Blandy's avatar
Jim Blandy committed
106
	{
107 108 109 110
	  endian = 1;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-little-endian"))
Jim Blandy's avatar
Jim Blandy committed
111
	{
112 113 114 115
	  endian = 0;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-group-by-8-bits"))
Jim Blandy's avatar
Jim Blandy committed
116
	{
117 118 119 120
	  group_by = 0x00;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-group-by-16-bits"))
Jim Blandy's avatar
Jim Blandy committed
121
	{
122 123 124 125
	  group_by = 0x01;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-group-by-32-bits"))
Jim Blandy's avatar
Jim Blandy committed
126
	{
127 128 129 130
	  group_by = 0x03;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-group-by-64-bits"))
Jim Blandy's avatar
Jim Blandy committed
131
	{
132 133 134 135 136
	  group_by = 0x07;
	  endian = 0;
	  --argc; argv++;
	}
      else
Jim Blandy's avatar
Jim Blandy committed
137
	{
138 139 140
	  fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
		   *argv);
	  usage ();
Jim Blandy's avatar
Jim Blandy committed
141 142 143
	}
    }

144
  do
Jim Blandy's avatar
Jim Blandy committed
145
    {
146 147 148
      if (*argv == NULL)
	fp = stdin;
      else
Jim Blandy's avatar
Jim Blandy committed
149
	{
150
	  char *filename = *argv++;
Jim Blandy's avatar
Jim Blandy committed
151

152 153 154 155 156 157 158
	  if (!strcmp (filename, "-"))
	    fp = stdin;
	  else if ((fp = fopen (filename, "r")) == NULL)
	    {
	      perror (filename);
	      continue;
	    }
Jim Blandy's avatar
Jim Blandy committed
159 160
	}

161
      if (un_flag)
Jim Blandy's avatar
Jim Blandy committed
162
	{
163
	  char buf[18];
Jim Blandy's avatar
Jim Blandy committed
164

165 166
#ifdef DOS_NT
#if (__DJGPP__ >= 2) || (defined WINDOWSNT)
167 168
          if (!isatty (fileno (stdout)))
	    setmode (fileno (stdout), O_BINARY);
169
#else
170 171
	  (stdout)->_flag &= ~_IOTEXT; /* print binary */
	  _setmode (fileno (stdout), O_BINARY);
172
#endif
173 174
#endif
	  for (;;)
Jim Blandy's avatar
Jim Blandy committed
175
	    {
176
	      register int i, c, d;
Jim Blandy's avatar
Jim Blandy committed
177

178
#define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
Jim Blandy's avatar
Jim Blandy committed
179

180
	      fread (buf, 1, 10, fp); /* skip 10 bytes */
Jim Blandy's avatar
Jim Blandy committed
181

182
	      for (i=0; i < 16; ++i)
Jim Blandy's avatar
Jim Blandy committed
183
		{
184 185
		  if ((c = getc (fp)) == ' ' || c == EOF)
		    break;
Jim Blandy's avatar
Jim Blandy committed
186

187 188 189
		  d = getc (fp);
		  c = hexchar (c) * 0x10 + hexchar (d);
		  putchar (c);
Jim Blandy's avatar
Jim Blandy committed
190

191 192
		  if ((i&group_by) == group_by)
		    getc (fp);
Jim Blandy's avatar
Jim Blandy committed
193 194
		}

195
	      if (c == ' ')
Jim Blandy's avatar
Jim Blandy committed
196
		{
197 198
		  while ((c = getc (fp)) != '\n' && c != EOF)
		    ;
Jim Blandy's avatar
Jim Blandy committed
199

200 201
		  if (c == EOF)
		    break;
Jim Blandy's avatar
Jim Blandy committed
202
		}
203
	      else
Jim Blandy's avatar
Jim Blandy committed
204
		{
205 206
		  if (i < 16)
		    break;
Jim Blandy's avatar
Jim Blandy committed
207

208
		  fread (buf, 1, 18, fp); /* skip 18 bytes */
Jim Blandy's avatar
Jim Blandy committed
209 210 211
		}
	    }
	}
212
      else
Jim Blandy's avatar
Jim Blandy committed
213
	{
214 215
#ifdef DOS_NT
#if (__DJGPP__ >= 2) || (defined WINDOWSNT)
216 217
          if (!isatty (fileno (fp)))
	    setmode (fileno (fp), O_BINARY);
218
#else
219 220
	  (fp)->_flag &= ~_IOTEXT; /* read binary */
	  _setmode (fileno (fp), O_BINARY);
221
#endif
222 223 224 225 226
#endif
	  address = 0;
	  string[0] = ' ';
	  string[17] = '\0';
	  for (;;)
Jim Blandy's avatar
Jim Blandy committed
227
	    {
228
	      register int i, c;
Jim Blandy's avatar
Jim Blandy committed
229

230
	      for (i=0; i < 16; ++i)
Jim Blandy's avatar
Jim Blandy committed
231
		{
232
		  if ((c = getc (fp)) == EOF)
Jim Blandy's avatar
Jim Blandy committed
233
		    {
234 235
		      if (!i)
			break;
Jim Blandy's avatar
Jim Blandy committed
236

237 238
		      fputs ("  ", stdout);
		      string[i+1] = '\0';
Jim Blandy's avatar
Jim Blandy committed
239
		    }
240
		  else
Jim Blandy's avatar
Jim Blandy committed
241
		    {
242
		      if (!i)
243
			printf ("%08lx: ", address);
Jim Blandy's avatar
Jim Blandy committed
244

245 246 247 248 249
		      if (iso_flag)
			string[i+1] =
			  (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
		      else
			string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
Jim Blandy's avatar
Jim Blandy committed
250

251
		      printf ("%02x", c);
Jim Blandy's avatar
Jim Blandy committed
252 253
		    }

254 255
		  if ((i&group_by) == group_by)
		    putchar (' ');
Jim Blandy's avatar
Jim Blandy committed
256 257
		}

258 259
	      if (i)
		puts (string);
Jim Blandy's avatar
Jim Blandy committed
260

261 262
	      if (c == EOF)
		break;
Jim Blandy's avatar
Jim Blandy committed
263

264
	      address += 0x10;
Jim Blandy's avatar
Jim Blandy committed
265 266 267 268

	    }
	}

269 270
      if (fp != stdin)
	fclose (fp);
Jim Blandy's avatar
Jim Blandy committed
271 272

    } while (*argv != NULL);
273
  return 0;
Jim Blandy's avatar
Jim Blandy committed
274 275
}

276
void
277
usage ()
Jim Blandy's avatar
Jim Blandy committed
278
{
279 280
  fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
  exit (1);
Jim Blandy's avatar
Jim Blandy committed
281
}
Kenichi Handa's avatar
Kenichi Handa committed
282 283 284

/* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
   (do not change this comment) */