aboutsummaryrefslogtreecommitdiffstats
path: root/output.c
blob: e81ef2949796ec5c201cff7dec8511347c97d29e (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <ncurses.h>
#include "machine_type.h"
#include "output.h"
#include "buffers.h"
void initcolors(tOutput* output)
{

	output->colors[COLOR_BRACKETS].fg	=COLOR_BLACK;	output->colors[COLOR_BRACKETS].bg	=COLOR_BLACK;	output->colors[COLOR_BRACKETS].attrs	=A_BOLD;
	output->colors[COLOR_HEXFIELD].fg	=COLOR_WHITE;	output->colors[COLOR_HEXFIELD].bg	=COLOR_BLACK;	output->colors[COLOR_HEXFIELD].attrs	=0;
	output->colors[COLOR_INPUT].fg		=COLOR_BLACK;	output->colors[COLOR_INPUT].bg		=COLOR_WHITE;	output->colors[COLOR_INPUT].attrs	=0;
	output->colors[COLOR_CURSOR].fg		=COLOR_WHITE;	output->colors[COLOR_CURSOR].bg		=COLOR_BLACK;	output->colors[COLOR_CURSOR].attrs	=0;
	output->colors[COLOR_TEXT].fg		=COLOR_CYAN;	output->colors[COLOR_TEXT].bg		=COLOR_BLACK;	output->colors[COLOR_TEXT].attrs	=A_BOLD;
	output->colors[COLOR_MENUNORMAL].fg	=COLOR_BLUE;	output->colors[COLOR_MENUNORMAL].bg	=COLOR_BLACK;	output->colors[COLOR_MENUNORMAL].attrs	=A_BOLD;
	output->colors[COLOR_MENUACTIVE].fg	=COLOR_BLUE;	output->colors[COLOR_MENUACTIVE].bg	=COLOR_BLUE;	output->colors[COLOR_MENUACTIVE].attrs	=A_BOLD;
	output->colors[COLOR_MENUHOTKEY].fg	=COLOR_CYAN;	output->colors[COLOR_MENUHOTKEY].bg	=COLOR_BLACK;	output->colors[COLOR_MENUHOTKEY].attrs	=0;
	output->colors[COLOR_MENUHOTKEYACTIVE].fg=COLOR_CYAN;	output->colors[COLOR_MENUHOTKEYACTIVE].bg=COLOR_BLUE;	output->colors[COLOR_MENUHOTKEYACTIVE].attrs=0;
	output->colors[COLOR_FRAME].fg		=COLOR_BLUE;	output->colors[COLOR_FRAME].bg		=COLOR_BLACK;	output->colors[COLOR_FRAME].attrs	=0;
	output->colors[COLOR_DIFF].fg		=COLOR_YELLOW;	output->colors[COLOR_DIFF].bg		=COLOR_BLACK;	output->colors[COLOR_DIFF].attrs	=A_BOLD;
	output->colors[COLOR_HEADLINE].fg	=COLOR_BLUE;	output->colors[COLOR_HEADLINE].bg	=COLOR_BLACK;	output->colors[COLOR_HEADLINE].attrs	=0;
//	output->colors[COLOR_INFO].fg		=COLOR_WHITE;	output->colors[COLOR_INFO].bg		=COLOR_BLACK;	output->colors[COLOR_INFO].attrs	=A_BOLD;
	output->colors[COLOR_HEADER].fg		=COLOR_BLACK;	output->colors[COLOR_HEADER].bg		=COLOR_CYAN;	output->colors[COLOR_HEADER].attrs	=0;
}	
void colorpair(tOutput* output,uicolors uicol,short fg,short bg,int attr)
{
	//init_pair(uicol,fg,bg);attrs[uicol]=COLOR_PAIR(uicol)+attr;
	output->colors[uicol].fg=fg;
	output->colors[uicol].bg=bg;
	output->colors[uicol].attrs=attr;
}
void pairsinit(tOutput* output)
{
	int i;
	start_color();
	for (i=0;i<UICOLORNUM;i++)
	{
		init_pair(i,output->colors[i].fg,output->colors[i].bg);output->attrs[i]=COLOR_PAIR(i)+output->colors[i].attrs;
	}
}
void setcolor(tOutput* output,uicolors col)
{
	wattrset(output->win,output->attrs[col]);
}

void drawframe(tOutput* output,tInt16 y,tInt16 x,tInt8 h,tInt8 w,char* header)
{
	int i;
	int j;
	
	setcolor(output,COLOR_FRAME);
	wmove(output->win,y,x);
	waddch(output->win,ACS_ULCORNER);
	for (i=0;i<w-2;i++) waddch(output->win,ACS_HLINE);
	waddch(output->win,ACS_URCORNER);
	for (j=1;j<h;j++)
	{
		wmove(output->win,y+j,x);
		waddch(output->win,ACS_VLINE);
		for (i=0;i<w-2;i++) waddch(output->win,' ');
		waddch(output->win,ACS_VLINE);
	}
	wmove(output->win,y+h,x);
	waddch(output->win,ACS_LLCORNER);
	for (i=0;i<w-2;i++) waddch(output->win,ACS_HLINE);
	waddch(output->win,ACS_LRCORNER);
	if (header)
	{
		setcolor(output,COLOR_BRACKETS);
		mvwprintw(output->win,y,x+1,"[");
		mvwprintw(output->win,y,x+1+strlen(header)+1,"]");
		setcolor(output,COLOR_HEADER);
		mvwprintw(output->win,y,x+2,"%s",header);
	}
}
void drawcenterframe(tOutput* output,tInt8 h,tInt8 w,char* header)
{
	drawframe(output,LINES/2-h/2,COLS/2-w/2,h,w,header);
}
void printbuffersingle(tOutput* output,tBuffer* hBuf1,tInt64 cursorpos1,tUInt64 firstpos1,tUInt8 windowfield)
{
	int i;
	int j;
	int k;
	int addrwidth;
	int bytesperline;
	tInt32 intpos1;
	tInt32 charcnt;



	addrwidth=((hBuf1->bufsize+hBuf1->baseaddr)>0xffffffffull)?16:8;
	bytesperline=(COLS-(addrwidth+3+3))*8/(8*3+8+1);			// this many bytes can be printed in one line. every 8 bytes there is an extra space in the hex field.
	
	setcolor(output,COLOR_HEADLINE);
	wmove(output->win,0,0);
	for (i=0;i<COLS;i++)
	{
		waddch(output->win,ACS_HLINE);
	}
	setcolor(output,COLOR_BRACKETS);
	mvwprintw(output->win,0,COLS-3-strlen(hBuf1->filename),"[");
	mvwprintw(output->win,0,COLS-2,"]");
	if (addrwidth==16)
	{
		mvwprintw(output->win,0,1,"[                /                ]");
		setcolor(output,COLOR_TEXT);
		mvwprintw(output->win,0,2,"%16llX",cursorpos1+hBuf1->baseaddr);
		mvwprintw(output->win,0,19,"%16llX",hBuf1->bufsize+hBuf1->baseaddr);
	} else {
		mvwprintw(output->win,0,1,"[        /        ]");
		setcolor(output,COLOR_TEXT);
		mvwprintw(output->win,0,2,"%8X",(tUInt32)(cursorpos1+hBuf1->baseaddr));
		mvwprintw(output->win,0,11,"%8X",(tUInt32)(hBuf1->bufsize+hBuf1->baseaddr));
	}
	setcolor(output,COLOR_HEADER);
	mvwprintw(output->win,0,COLS-2-strlen(hBuf1->filename),"%s",hBuf1->filename);
	intpos1=getbufferidx(hBuf1,firstpos1);
	if (intpos1>=0)
	{
		charcnt=0;
		for (i=0;i<LINES-2;i++)
		{
			tBool colhex;
			setcolor(output,COLOR_HEXFIELD);
			if (addrwidth==8)	mvwprintw(output->win,i+1,0, "% 8X    ",(tUInt32)(firstpos1+hBuf1->baseaddr));
			else			mvwprintw(output->win,i+1,0,"% 16llX    ",firstpos1+hBuf1->baseaddr);

			mvwprintw(output->win,i+1,COLS-bytesperline-5,"      ");
			wmove(output->win,i+1,addrwidth+3);
			colhex=1;
			for (j=0;j<bytesperline;j++)
			{
				tInt16	c;
				if ((intpos1+j)<hBuf1->filesize) c=hBuf1->data[intpos1+j];
				else	c=-1;
				if (!(j&7)) wprintw(output->win," ");
				wprintw(output->win," ");
				for (k=0;k<hBuf1->changesnum;k++)
				{
					if (hBuf1->changes[k].pos==(firstpos1+j))	
					{
						setcolor(output,COLOR_DIFF);
						c=hBuf1->changes[k].after;
						colhex=0;
					}
				}	
				if (cursorpos1==(firstpos1+j) && windowfield==0) 
				{
					setcolor(output,COLOR_INPUT);
					colhex=0;
				}
				//  TODO: setcolor(hBuf1->data[intpos1]!=buf2->data[intpos2]?COLOR_DIFF:COLOR_HEXFIELD);
				if (c>=0)
				{
					if (hBuf1->nibble && hBuf1->changepos==(firstpos1+j))
					
						wprintw(output->win,"%01x ",hBuf1->nexthex&0xf);
					else
						wprintw(output->win,"%02x",c&0xff);
				}
				else
					wprintw(output->win,"  ");

				if (!colhex) setcolor(output,COLOR_HEXFIELD);
				colhex=1;
//				mvwprintw(output->win,i+1,addrwidth+5+j*3+j/8,"   ");
			}
			for (j=0;j<bytesperline;j++)
			{
				unsigned char c;
				if (intpos1<hBuf1->filesize)
					c=hBuf1->data[intpos1];
				else c=' ';
				for (k=0;k<hBuf1->changesnum;k++)
				{
					if (hBuf1->changes[k].pos==(firstpos1))
					{
						setcolor(output,COLOR_DIFF);
						c=hBuf1->changes[k].after;
					}
				}	
				if (cursorpos1==firstpos1 && windowfield==1) setcolor(output,COLOR_INPUT);
				//  TODO: setcolor(buf1->data[intpos1]!=buf2->data[intpos2]?COLOR_DIFF:COLOR_HEXFIELD);
				mvwprintw(output->win,i+1,COLS-bytesperline+j,"%c",(c>=32 && c<127)?c:'.');
				intpos1++;
				firstpos1++;
 				setcolor(output,COLOR_HEXFIELD);
			}
		}
	}
}
void printbufferdiff(tOutput* output,tBuffer* hBuf1,tBuffer* hBuf2,tInt64 cursorpos1,tInt64 cursorpos2)
{
	int i;
	int j;
	int addrwidth;
	int bytesperline;
	tInt32 intpos1;
	tInt32 intpos1b;
	tInt32 intpos2;
	tInt32 intpos2b;
	tInt32 charcnt;
	uicolors oldcolor;


	addrwidth=((hBuf1->bufsize+hBuf1->baseaddr)>0xffffffffull || (hBuf2->bufsize+hBuf2->baseaddr)>0xffffffffull)?16:8;
	bytesperline=(COLS-(addrwidth+3+3))*8/(8*3+8+1);			// this many bytes can be printed in one line. every 8 bytes there is an extra space in the hex field.
	
	setcolor(output,COLOR_HEADLINE);
	wmove(output->win,0,0);
	for (i=0;i<COLS;i++)
	{
		waddch(output->win,ACS_HLINE);
	}
	wmove(output->win,LINES/2,0);
	for (i=0;i<COLS;i++)
	{
		waddch(output->win,ACS_HLINE);
	}
	setcolor(output,COLOR_BRACKETS);
	mvwprintw(output->win,0,COLS-3-strlen(hBuf1->filename),"[");
	mvwprintw(output->win,0,COLS-2,"]");
	mvwprintw(output->win,LINES/2,COLS-3-strlen(hBuf2->filename),"[");
	mvwprintw(output->win,LINES/2,COLS-2,"]");
	if (addrwidth==16)
	{
		mvwprintw(output->win,0,1,"[                /                ]");
		mvwprintw(output->win,LINES/2,1,"[                /                ]");
		setcolor(output,COLOR_TEXT);
		mvwprintw(output->win,0,2,"%16llX",cursorpos1+hBuf1->baseaddr);
		mvwprintw(output->win,0,19,"%16llX",hBuf1->bufsize+hBuf1->baseaddr);
		mvwprintw(output->win,LINES/2,2,"%16llX",cursorpos2+hBuf2->baseaddr);
		mvwprintw(output->win,LINES/2,19,"%16llX",hBuf2->bufsize+hBuf2->baseaddr);
		
	} else {
		mvwprintw(output->win,0,1,"[        /        ]");
		mvwprintw(output->win,LINES/2,1,"[        /        ]");
		setcolor(output,COLOR_TEXT);
		mvwprintw(output->win,0,2,"%8X",(tUInt32)cursorpos1+hBuf1->baseaddr);
		mvwprintw(output->win,0,11,"%8X",(tUInt32)hBuf1->bufsize+hBuf1->baseaddr);
		mvwprintw(output->win,LINES/2,2,"%8X",(tUInt32)cursorpos2+hBuf2->baseaddr);
		mvwprintw(output->win,LINES/2,11,"%8X",(tUInt32)hBuf2->bufsize+hBuf2->baseaddr);
	}
	setcolor(output,COLOR_HEADER);
	mvwprintw(output->win,0,COLS-2-strlen(hBuf1->filename),"%s",hBuf1->filename);
	mvwprintw(output->win,LINES/2,COLS-2-strlen(hBuf2->filename),"%s",hBuf2->filename);
	if (cursorpos1<0)
	{
		getbufferidx(hBuf1,0);
		intpos1=cursorpos1;
	} else {
		intpos1=getbufferidx(hBuf1,cursorpos1);
	}
	if (cursorpos2<0)
	{
		getbufferidx(hBuf2,0);
		intpos2=cursorpos2;
	} else {
		intpos2=getbufferidx(hBuf2,cursorpos2);
	}
	if (intpos1>=0 || intpos2>=0)
	{
		charcnt=0;
		intpos1b=intpos1;
		intpos2b=intpos2;

		for (i=0;i<(LINES+1)/2-2;i++)
		{
			setcolor(output,COLOR_HEXFIELD);
			if (addrwidth==8)	mvwprintw(output->win,i+1,0, "% 8X    ",(tUInt32)(cursorpos1+hBuf1->baseaddr));
			else			mvwprintw(output->win,i+1,0,"% 16llX    ",cursorpos1+hBuf1->baseaddr);

			mvwprintw(output->win,i+1,COLS-bytesperline-5,"      ");
			oldcolor=COLOR_HEXFIELD;
			wmove(output->win,i+1,addrwidth+3);
			for (j=0;j<bytesperline;j++)
			{
				tInt16	c1,c2;
				if ((intpos1+j)>=0 && (intpos1+j)<hBuf1->filesize) c1=hBuf1->data[intpos1+j];
				else	c1=-1;
				if ((intpos2+j)>=0 && (intpos2+j)<hBuf2->filesize) c2=hBuf2->data[intpos2+j];
				else	c2=-1;
				if (c1!=c2 && oldcolor==COLOR_HEXFIELD)
				{
					setcolor(output,COLOR_DIFF);
					oldcolor=COLOR_DIFF;
				} else if (c1==c2) {
					setcolor(output,COLOR_HEXFIELD);
					oldcolor=COLOR_HEXFIELD;
				}
				wprintw(output->win," ");
				if (j!=(bytesperline-1))
				{
					if (!(j&7)) wprintw(output->win," ");
				}
				if (c1>=0)
				{
					wprintw(output->win,"%02x",(c1&0xff));
				}
				else
					wprintw(output->win,"  ");

			}
			wmove(output->win,i+1,COLS-bytesperline);
			for (j=0;j<bytesperline;j++)
			{
				unsigned char c1,c2;
				if (intpos1>=0 && intpos1<hBuf1->filesize)
					c1=hBuf1->data[intpos1];
				else c1=' ';
				if (intpos2>=0 && intpos2<hBuf2->filesize)
					c2=hBuf2->data[intpos2];
				else c2=' ';

				if (c1!=c2 && oldcolor==COLOR_HEXFIELD)
				{
					setcolor(output,COLOR_DIFF);
					oldcolor=COLOR_DIFF;
				} else if (c1==c2) {
					setcolor(output,COLOR_HEXFIELD);
					oldcolor=COLOR_HEXFIELD;
				}
				wprintw(output->win,"%c",(c1>=32 && c1<127)?c1:'.');
				intpos1++;
				cursorpos1++;
				intpos2++;
			}
		}
		oldcolor=COLOR_HEXFIELD;
		setcolor(output,COLOR_HEXFIELD);
		if (!(LINES&1))
		{
			wmove(output->win,LINES/2-1,0);
			for (i=0;i<COLS;i++) wprintw(output->win," ");
		}

		intpos1=intpos1b;
		intpos2=intpos2b;
		for (i=0;i<(LINES+1)/2-2;i++)
		{
			setcolor(output,COLOR_HEXFIELD);
			if (addrwidth==8)	mvwprintw(output->win,i+1+LINES/2,0, "% 8X    ",(tUInt32)(cursorpos2+hBuf2->baseaddr));
			else			mvwprintw(output->win,i+1+LINES/2,0,"% 16llX    ",cursorpos2+hBuf2->baseaddr);

			mvwprintw(output->win,i+1+LINES/2,COLS-bytesperline-5,"      ");
			oldcolor=COLOR_HEXFIELD;
			wmove(output->win,i+1+LINES/2,addrwidth+3);
			for (j=0;j<bytesperline;j++)
			{
				tInt16	c1,c2;
				if ((intpos1+j)>=0 && (intpos1+j)<hBuf1->filesize) c1=hBuf1->data[intpos1+j];
				else	c1=-1;
				if ((intpos2+j)>=0 && (intpos2+j)<hBuf2->filesize) c2=hBuf2->data[intpos2+j];
				else	c2=-1;
				if (c1!=c2 && oldcolor==COLOR_HEXFIELD)
				{
					setcolor(output,COLOR_DIFF);
					oldcolor=COLOR_DIFF;
				} else if (c1==c2) {
					setcolor(output,COLOR_HEXFIELD);
					oldcolor=COLOR_HEXFIELD;
				}
				wprintw(output->win," ");
				if (j!=(bytesperline-1))
				{
					if (!(j&7)) wprintw(output->win," ");
				}
				if (c2>=0)
				{
					wprintw(output->win,"%02x",(c2&0xff));
				}
				else
					wprintw(output->win,"  ");

			}
			wmove(output->win,i+1+LINES/2,COLS-bytesperline);
			for (j=0;j<bytesperline;j++)
			{
				unsigned char c1,c2;
				if (intpos1>=0 && intpos1<hBuf1->filesize)
					c1=hBuf1->data[intpos1];
				else c1=' ';
				if (intpos2>=0 && intpos2<hBuf2->filesize)
					c2=hBuf2->data[intpos2];
				else c2=' ';

				if (c1!=c2 && oldcolor==COLOR_HEXFIELD)
				{
					setcolor(output,COLOR_DIFF);
					oldcolor=COLOR_DIFF;
				} else if (c1==c2) {
					setcolor(output,COLOR_HEXFIELD);
					oldcolor=COLOR_HEXFIELD;
				}
				wprintw(output->win,"%c",(c2>=32 && c2<127)?c2:'.');
				intpos1++;
				intpos2++;
				cursorpos2++;
			}
		}
	}
}
void printmainmenu(tOutput* output,tBool diffmode)
{
	int i;
	int x;
	unsigned char* menutextsnodiff[]={
"Goto  ",	// F1
"Search",	// F2
"Next  ",	// F3
"Prev  ",	// F4
"HexCal",	// F5
"      ",	// F6
"      ",	// F7
"      ",	// F8
"Undo  ",	// F9
"Quit  "};	// F10

	unsigned char* menutextsdiff[]={
"      ",	// F1
"      ",	// F2
"Next  ",	// F3
"Prev  ",	// F4
"HexCal",	// F5
"Corr. ",	// F6
"      ",	// F7
"      ",	// F8
"       ",	// F9
"Quit  "};	// F10

	setcolor(output,COLOR_MENUHOTKEY);
	
	x=0;
	for (i=0;i<10;i++)
	{
		mvwprintw(output->win,LINES-1,x," %i",(i+1)%10);
		x+=8;
	}
	setcolor(output,COLOR_MENUNORMAL);
	x=2;
	for (i=0;i<10;i++)
	{
		mvwprintw(output->win,LINES-1,x,"%s",diffmode?menutextsdiff[i]:menutextsnodiff[i]);
		x+=8;
	}
	
}
tInt32 movepositions(tInt64* cursorpos,tInt64* firstpos,tInt64 maxbufsize,tInt32 chars,tInt32 lines,tInt32 pages,tBool diffmode)
{
	tInt32 bytesperline;
	tInt32 bytesperhalfpage;
	tInt32 bytesperpage;
	tInt32 addrwidth;
	
	tInt64 newfirstpos=*firstpos;
	tInt64 newcursorpos=*cursorpos;
	

	addrwidth=(maxbufsize>0xffffffffull)?16:8;
	bytesperline=(COLS-(addrwidth+3+3))*8/(8*3+8+1);			// this many bytes can be printed in one line. every 8 bytes there is an extra space in the hex field.
	bytesperhalfpage=bytesperline*(LINES/2-2);
	bytesperpage=bytesperline*(LINES-2);
	

	newcursorpos+=(chars+lines*bytesperline+pages*(diffmode?bytesperhalfpage:bytesperpage));
	if (newcursorpos>=(newfirstpos+(diffmode?bytesperhalfpage:bytesperpage)) ||  (newcursorpos<newfirstpos)) newfirstpos+=(chars+lines*bytesperline+pages*(diffmode?bytesperhalfpage:bytesperpage));
		
	*firstpos=newfirstpos;	
	*cursorpos=newcursorpos;
	if (newcursorpos<0 || newfirstpos<0 || newcursorpos>maxbufsize || newfirstpos>maxbufsize) return RETNOK;
	return RETOK;

}