-
Notifications
You must be signed in to change notification settings - Fork 0
/
hxdisp.c
203 lines (174 loc) · 5.48 KB
/
hxdisp.c
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
/* @(#) hxdisp.c - display functions for hx */
#include "hx.h"
static char oldname[61] = { 0};
disppage(fname)
char *fname;
{
char nrows = MAXROWS;
char headbuf[80];
if ( strcmp ( oldname, fname) ){
/* set headwindow static strings */
strcpy ( oldname, fname);
memset ( headbuf, '-', 79 );
headbuf[79] = '\0';
wmove ( headwin, 0,0 );
waddstr ( headwin, headbuf );
wmove ( headwin, 1, 0 );
wdeleteln ( headwin ); /* wclear is buggy */
wdeleteln ( headwin );
waddstr(headwin,"File: ");
waddstr(headwin,fname);
wmove ( headwin, 2,0 );
waddstr ( headwin, headbuf );
}
if ( readonly ){
wmove ( headwin, 1, 53 );
waddstr(headwin,"READONLY");
}
retry:
homepos = fpos;
/* display heading position */
hxfsect (fpos );
wrefresh ( headwin );
wmove ( datawin, 0, 0 );
while ( nrows-- ){
if ( doneflag ){
doneflag = FALSE;
break;
}
if ( displine () == -1 ){
if ( homepos == 0 ){ /* EOF and can't go back */
while ( nrows-- ) wdeleteln(datawin);
break;
}
/* EOF - repaint screen without hitting EOF */
fpos = homepos - ((nrows+1) * CHARS_ROW);
if ( fpos < 0 ) fpos = 0;
nrows = MAXROWS;
goto retry;
}
}
wrefresh(datawin);
}
/* returns -1 for end of file or current pos in file */
displine ()
{
register char nread;
char buffer[CHARS_ROW];
char outbuf[81];
if (!fp || fseek ( fp, fpos, 0 ))
return ( -1 );
nread = fread(buffer,1,CHARS_ROW,fp);
hxfline ( fpos, buffer, outbuf, nread );
waddstr ( datawin, outbuf );
waddch ( datawin, '\n' );
fpos += CHARS_ROW;
if ( nread == 0 ) /* EOF - no characters read */
return ( -1 );
return ( 0 );
}
putx ( c ) /* put a byte in hex format and append a blank */
register unsigned char c;
{
char buf[3];
sprintf ( buf, "%2.2x ", c );
waddstr ( datawin, buf );
}
ascrepl ( c, buffer )
unsigned char c;
char *buffer;
{
int x,y;
getyx ( datawin, y, x );
/* replace char in buffer adjusting for middle space */
buffer[(((x>=ASC_MID)?x-1:x)-ASC_COL0) + y*CHARS_ROW] = c;
waddch ( datawin, c ); /* replace ascii character */
/* replace hex field adjusting for middle space */
wmove ( datawin, y, HEX_COL0 + ((x-ASC_COL0)*3)-(x>=ASC_MID?2:0) );
putx ( c );
wrefresh( datawin );
/* reposition cursor */
if ( ++x == (ASC_MID-1) ) ++x; /*adjust for middle space */
if ( x == ASC_COL0 +CHARS_ROW +1){ /* check for end of line */
if ( y == MAXROWS-1 ){ /* or end of screen */
y = 0;
x = ASC_COL0;
} else {
++y;
x = ASC_COL0;
}
}
wmove ( datawin, y, x );
}
hexrepl ( c, buffer )
unsigned char c;
char *buffer;
{
char x,y;
char buf[3];
int temp;
getyx ( datawin, y, x );
c=toupper( c );
waddch ( datawin, c );
wrefresh( datawin );
/* get second character */
while (1) {
buf[1] = wgetch ( datawin );
if ( ishex ( buf[1] )) break;
}
buf[1] = toupper (buf[1] );
waddch ( datawin, buf[1] );
wrefresh( datawin );
/* replace ascii character */
buf[0] = c;
buf[2] ='\0';
sscanf ( buf, "%x", &temp );
c = temp;
temp=(x-HEX_COL0)/3; /* rounding will adjust for middle space */
buffer[(temp + y*CHARS_ROW)] = c;
wmove ( datawin, y, temp + ASC_COL0 + (x>=HEX_MID?1:0));
if ( isprint ( c ))
waddch ( datawin, c );
else waddch ( datawin, '.' );
wrefresh( datawin );
x += 3;
if ( x == HEX_COL0+1 + (CHARS_ROW*3) ){
if ( y == MAXROWS-1 ){
y = 0;
x = HEX_COL0;
} else {
++y;
x = HEX_COL0;
}
} else if ( x == HEX_MID ) ++x;
wmove ( datawin, y, x );
}
/* convert hex string to binary string
len = length of output, ie input length / 2
returns ( -1 ) on error during conversion
*/
htos(out,in,len)
register char *in;
register char *out;
{
char temp[3];
register char *ptr;
int c;
temp[2] = '\0';
for ( ptr = in; len-- ; ptr +=2 ){
strncpy ( temp, ptr, 2 );
if ( sscanf ( temp, "%x", &c ) != 1 )
return ( -1 );
*out++ = c;
}
*out = '\0';
return ( 0 );
}
wcommwin ( str )
char *str;
{
wmove ( commwin, 0 , 0 );
wdeleteln ( commwin );
waddstr ( commwin, str );
wrefresh ( commwin );
}