File: printhex.c

package info (click to toggle)
netboot 0.8.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,728 kB
  • ctags: 4,740
  • sloc: ansic: 15,152; asm: 11,623; yacc: 2,248; makefile: 1,110; pascal: 1,108; lex: 748; sh: 233
file content (141 lines) | stat: -rw-r--r-- 3,349 bytes parent folder | download | duplicates (3)
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
/*
 * printhex.c  -  Print a hex memory dump
 *
 * Copyright (C) 1998 Gero Kuhlmann   <gero@gkminix.han.de>
 *
 *  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
 *  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.
 */

#include <stdio.h>
#include <dos.h>
#include <ctype.h>
#include "romcheck.h"



/*
 * Some useful defines
 */
#define MAXLINES	16	/* number of lines to print on one screen */
#define LINESIZE	16	/* number of hex values per line */



/*
 * Print one line of hex code
 */
static void printline(seg)
unsigned int seg;
{
  static unsigned char buf[LINESIZE + 1];
  int i;

  /* Fill buffer from rom memory */
  movedata(seg, 0, __get_ds(), buf, LINESIZE);

  /* Print buffer in hex and convert it into a printable string */
  printf("%04X0  ", seg);
  for (i = 0; i < LINESIZE; i++) {
	printf(" %02X", buf[i] & 0xff);
	if (buf[i] > 0x7f || !isprint(buf[i] & 0xff))
		buf[i] = '.';
  }
  buf[LINESIZE] = '\0';
  printf("   %s\n", buf);
}



/*
 * Print a rom area or the whole memory as a hex dump.
 */
void printhex(sip)
struct scaninfo *sip;
{
  unsigned int memstart, memend;
  unsigned int curpos, i;
  int index;
  char c;

  /* Ask the user about which area to print */
  printf("\n\n");
  index = readint("Enter rom area number, 0 for whole memory",
							0, sip->signum, 0);
  if (index > 0) {
	index--;
	memstart = sip->sigsegs[index];
	memend = sip->sigsegs[index] + sip->sigsize[index];
  } else {
	memstart = readhex("Enter start segment",
					STARTSEG, ENDSEG - 1, STARTSEG);
	memend = readhex("Enter end segment",
					memstart + 1, ENDSEG, memstart + 1);
  }
  curpos = memstart;

  /* Print one page of output and ask user what to do next */
  while (TRUE) {
	printf("\n\n");
	for (i = curpos; i < (curpos + MAXLINES) && i < memend; i++)
		printline(i);
	printf("\nPress <q> to exit");
	if (i < (memend - 1))
		printf(", <space> or <e> to continue");
	if (curpos > memstart)
		printf(", <b> or <t> to go back");
	do {
		c = waitchar();
		switch (c) {
			case 'q':
				putchar('\n');
				return;
			case 'b':
				if (curpos > memstart) {
					if ((curpos - memstart) < MAXLINES)
						curpos = memstart;
					else
						curpos -= MAXLINES;
				} else
					c = '\0';	/* invalid command */
				break;
			case 't':
				if (curpos > memstart)
					curpos = memstart;
				else
					c = '\0';	/* invalid command */
				break;
			case ' ':
				if (i < (memend - 1))
					curpos = i;
				else
					c = '\0';	/* invalid command */
				break;
			case 'e':
				if (i < (memend - 1)) {
					if ((memend - memstart) < MAXLINES)
						curpos = memstart;
					else
						curpos = memend - MAXLINES;
				} else
					c = '\0';	/* invalid command */
				break;
			default:
				c = '\0';		/* invalid command */
				break;
		}
	} while (!c);
  }
}