hexl.c 5.36 KB
Newer Older
1
/* Convert files for Emacs Hexl mode.
2 3
   Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005,
                 2006, 2007  Free Software Foundation, Inc.
4 5 6 7 8

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
9 10
the Free Software Foundation; either version 3, or (at your option)
any later version.
11 12 13 14 15 16 17

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
18 19 20
along with this program; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
21

22 23 24 25
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#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;

50 51 52
void usage();

int
53 54 55
main (argc, argv)
     int argc;
     char *argv[];
Jim Blandy's avatar
Jim Blandy committed
56
{
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  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
78

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

247 248 249 250 251
		      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
252

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

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

260 261
	      if (i)
		puts (string);
Jim Blandy's avatar
Jim Blandy committed
262

263 264
	      if (c == EOF)
		break;
Jim Blandy's avatar
Jim Blandy committed
265

266
	      address += 0x10;
Jim Blandy's avatar
Jim Blandy committed
267 268 269 270

	    }
	}

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

    } while (*argv != NULL);
275
  return EXIT_SUCCESS;
Jim Blandy's avatar
Jim Blandy committed
276 277
}

278
void
279
usage ()
Jim Blandy's avatar
Jim Blandy committed
280
{
281
  fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
282
  exit (EXIT_FAILURE);
Jim Blandy's avatar
Jim Blandy committed
283
}
Miles Bader's avatar
Miles Bader committed
284 285 286

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

/* hexl.c ends here */