File: dmhex.c

package info (click to toggle)
elvis 2.1i-3
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 4,120 kB
  • ctags: 5,838
  • sloc: ansic: 53,854; sh: 811; makefile: 263
file content (284 lines) | stat: -rw-r--r-- 7,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* dmhex.c */
/* Copyright 1995 by Steve Kirkendall */

char id_dmhex[] = "$Id: dmhex.c,v 2.14 1997/11/02 18:54:45 steve Exp $";

#include "elvis.h"
#ifdef DISPLAY_HEX

#ifdef CHAR16
"hex mode doesn't support 16-bit characters"
#endif

#if USE_PROTOTYPES
static DMINFO *init(WINDOW win);
static void term(DMINFO *info);
static long mark2col(WINDOW w, MARK mark, BOOLEAN cmd);
static MARK move(WINDOW w, MARK from, long linedelta, long column, BOOLEAN cmd);
static MARK setup(WINDOW win, MARK top, long cursor, MARK bottom, DMINFO *info);
static MARK image(WINDOW w, MARK line, DMINFO *info, void (*draw)(CHAR *p, long qty, _char_ font, long offset));
#endif

/* Lines look like this:
_offset____0__1__2__3___4__5__6__7___8__9__a__b___c__d__e__f__0123456789abcdef
00000000  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  0000000000000000
00000000  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  0000000000000000
00000000  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  0000000000000000
00000000  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  0000000000000000
00000000  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  0000000000000000
00000000  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  0000000000000000
*/

/* This array is used to convert a column number to an offset within a line */
static char col2offset[] = "00000000000011122233334445556667777888999aaabbbbcccdddeeeffff00123456789abcdef";

/* start the mode, and allocate dminfo */
static DMINFO *init(win)
	WINDOW	win;
{
	/* inherit some functions from dmnormal */
	if (!dmhex.wordmove)
	{
		dmhex.wordmove = dmnormal.wordmove;
		dmhex.tagatcursor = dmnormal.tagatcursor;
		dmhex.tagload = dmnormal.tagload;
		dmhex.tagnext = dmnormal.tagnext;
	}

	return NULL;
}

/* end the mode, and free the modeinfo */
static void term(info)
	DMINFO	*info;	/* window-specific info about mode */
{
}

/* Convert a mark to a screen-relative column number */
static long mark2col(w, mark, cmd)
	WINDOW	w;	/* window where buffer is shown */
	MARK	mark;	/* mark to convert */
	BOOLEAN	cmd;	/* if True, we're in command mode; else input mode */
{
	return 62 + (markoffset(mark) & 0xf);
}

/* Move vertically, and to a given column (or as close to column as possible) */
static MARK move(w, from, linedelta, column, cmd)
	WINDOW	w;		/* window where buffer is shown */
	MARK	from;		/* old location */
	long	linedelta;	/* line movement */
	long	column;		/* desired column number */
	BOOLEAN	cmd;		/* if True, we're in command mode; else input mode */
{
	static MARKBUF	mark;
	long		offset;
	long		coloff;

	/* compute line movement first */
	offset = (markoffset(from) & ~0xf) + 0x10 * linedelta;

	/* convert requested column to nearest buffer offset */
	if (column >= (int)strlen(col2offset))
	{
		coloff = 0xf;
	}
	else if (col2offset[column] >= '0' && col2offset[column] <= '9')
	{
		coloff = col2offset[column] - '0';
	}
	else
	{
		coloff = col2offset[column] - 'a' + 0xa;
	}
	offset += coloff;

	/* never return an offset past the end of the buffer, or before the
	 * beginning.
	 */
	if (offset >= o_bufchars(markbuffer(from)))
	{
		if (o_bufchars(markbuffer(from)) == 0)
		{
			offset = 0;
		}
		else
		{
			offset = o_bufchars(markbuffer(from)) - 1;
		}
	}
	else if (offset < 0)
	{
		offset = coloff;
	}

	return marktmp(mark, markbuffer(from), offset);
}

/* Choose a line to appear at the top of the screen, and return its mark.
 * Also, initialize the info for the next line.
 */
static MARK setup(win, top, cursor, bottom, info)
	WINDOW	win;	/* window to be updated */
	MARK	top;	/* where previous image started */
	long	cursor;	/* offset of cursor */
	MARK	bottom;	/* where previous image ended */
	DMINFO	*info;	/* window-specific info about mode */
{
	static MARKBUF	mark;
	long		topoff, bottomoff;

	/* extract the offsets.  Round down to multiple of 16 */
	topoff = (top ? (markoffset(top) & ~0xf) : -1);
	bottomoff = (bottom ? (markoffset(bottom) & ~0xf) : -1);
	cursor &= ~0xf;

	/* if cursor is on the screen, or very near the bottom, then
	 * keep the current top
	 */
	if (cursor >= topoff && cursor <= bottomoff + 16)
	{
		return marktmp(mark, markbuffer(top), topoff);
	}

	/* if the cursor is on the line before the top, then make the cursor's
	 * line become the new top line.
	 */
	if (cursor == topoff - 16)
	{
		return marktmp(mark, markbuffer(top), cursor);
	}

	/* else it is distantly before or after the the old screen.  Center
	 * the cursor's line in the screen.
 	 */
	topoff = (cursor - (bottomoff - topoff) / 2) & ~0xf;
	if (topoff < 0)
	{
		topoff = 0;
	}
	return marktmp(mark, markbuffer(top), topoff);
}

static MARK image(w, line, info, draw)
	WINDOW	w;		/* window where drawing will go */
	MARK	line;		/* start of line to draw */
	DMINFO	*info;		/* window-specific info about mode */
	void	(*draw)P_((CHAR *p, long qty, _char_ font, long offset));
				/* function for drawing a single character */
{
	char	*c8p;
	CHAR	*cp;
	CHAR	tmp;
	CHAR	space;	/* usually a space character, maybe bracket character */
	char	buf[10];
	int	i, j;

	/* output headings, if necessary */
	if ((markoffset(line) & 0xf0) == 0)
	{
		for (c8p = " offset    0  1  2  3   4  5  6  7   8  9  a  b   c  d  e  f  0123456789abcdef";
		     *c8p; c8p++)
		{
			tmp = *c8p;
			(*draw)(&tmp, 1, 'u', -1);
		}
		tmp = '\n';
		(*draw)(&tmp, 1, 'n', -1);
	}

	/* output the line offset */
	sprintf(buf, "%08lx", markoffset(line));
	for (i = 0; buf[i]; i++)
	{
		tmp = buf[i];
		(*draw)(&tmp, 1, 'n', -1);
	}

	/* output the hex codes of the line */
	j = markoffset(w->cursor) - markoffset(line);
	space = ' ';
	for ((void)scanalloc(&cp, line), i = 0; i < 16 && cp; scannext(&cp), i++)
	{
		if ((i & 0x03) == 0)
		{
			(*draw)(&space, 1, 'n', -1);
			space = ' ';
		}
		if (j == i)
		{
			tmp = '<';
			(*draw)(&tmp, 1, 'n', -1);
			space = '>';
		}
		else
		{
			(*draw)(&space, 1, 'n', -1);
			space = ' ';
		}
		sprintf(buf, "%02x", *cp);
		tmp = buf[0];
		(*draw)(&tmp, 1, (char)(j==i ? 'u' : 'n'), markoffset(line) + i);
		tmp = buf[1];
		(*draw)(&tmp, 1, (char)(j==i ? 'u' : 'n'), markoffset(line) + i);
	}
	(*draw)(&space, 1, 'n', -1);

	/* pad with blanks, if necessary */
	space = ' ';
	while (i < 16)
	{
		(*draw)(&space, ((i & 0x03) == 0) ? -4 : -3, 'n', -1);
		i++;
	}
	(*draw)(&space, 1, 'n', -1);

	/* output the characters */
	tmp = '.';
	for ((void)scanseek(&cp, line), i = 0; i < 16 && cp; scannext(&cp), i++)
	{
		if (*cp < ' ' || *cp == '\177')
		{
			(*draw)(&tmp, 1, 'n', markoffset(line) + i);
		}
		else
		{
			(*draw)(cp, 1, 'n', markoffset(line) + i);
		}
	}
	scanfree(&cp);
	tmp = '\n';
	(*draw)(&tmp, 1, 'n', -1);

	/* output a blank line after every 16th data line */
	if ((markoffset(line) & 0xf0) == 0xf0)
	{
		(*draw)(&tmp, 1, 'n', -1);
	}

	markoffset(line) += i;
	return line;
}

DISPMODE dmhex =
{
	"hex",
	"Binary hex dump",
	False,	/* display generating can't be optimized */
	False,	/* shouldn't do normal wordwrap */
	0,	/* no window options */
	NULL,
	0,	/* no global options */
	NULL,
	NULL,
	init,
	term,
	mark2col,
	move,
	NULL,	/* wordmove will be set to dmnormal.wordmove in init() */
	setup,
	image,
	NULL,	/* doesn't need a header */
	NULL	/* no autoindent */
};
#endif /* DISPLAY_HEX */