File: hexcurse.c

package info (click to toggle)
hexcurse 1.58-1.1
  • links: PTS
  • area: main
  • in suites: buster, sid
  • size: 796 kB
  • ctags: 225
  • sloc: sh: 4,016; ansic: 2,084; makefile: 9
file content (252 lines) | stat: -rw-r--r-- 9,656 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
/******************************************************************************\
 *  Copyright (C) 2001 writen by Jewfish and Armoth                           *
 *									      *
 *  Description: this codes allows a user to view and edit the hexadecimal and*
 *		 and ascii values of a file.  The curses library is used to   *
 *		 display and manipulate the output.  See the README file      *
 *		 included for more information.				      *
 *									      *
 *  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	      *
 *  (at your option) 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
 *									      *
\******************************************************************************/

#include "hex.h"					/* custom header      */

/*#define DEBUG_LLIST*/
/*#define DEBUG_GOTO*/

int     BASE, MAXY, resize = 0;
int     MIN_ADDR_LENGTH;
hexList *head;						/* linked list struct */
WINS    *windows;					/* window structure   */
char    EBCDIC[256],
	*fpINfilename = NULL,
        *fpOUTfilename = NULL;
bool 	printHex;					/* address format     */
bool    USE_EBCDIC;
bool    IN_HELP;					/* if help displayed  */
int     hex_win_width,
        ascii_win_width,
        hex_outline_width,
        ascii_outline_width;


    /* partial EBCDIC table contributed by Ted (ted@php.net) */
    char EBCDIC[] = {
    /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D    E   F */
      '.','.','.','.','.','.','.','.','.','.','.','.','.','.' ,'.','.', /* 0 */
      '.','.','.','.','.','.','.','.','.','.','.','.','.','.' ,'.','.', /* 1 */
      '.','.','.','.','.','.','.','.','.','.','.','.','.','.' ,'.','.', /* 2 */
      '.','.','.','.','.','.','.','.','.','.','.','.','.','.' ,'.','.', /* 3 */
      ' ','.','.','.','.','.','.','.','.','.','.','.','<','(' ,'+','|', /* 4 */
      '&','.','.','.','.','.','.','.','.','.','!','$','*',')' ,';','.', /* 5 */
      '-','/','.','.','.','.','.','.','.','.','.',',','%','_' ,'>','?', /* 6 */
      '.','.','.','.','.','.','.','.','.','.',':','#','@','\'','=','"', /* 7 */
      '.','a','b','c','d','e','f','g','h','i','.','.','.','.' ,'.','.', /* 8 */
      '.','.','j','k','l','m','n','o','p','q','.','.','.','.' ,'.','.', /* 9 */
      '.','r','s','t','u','v','w','x','y','z','.','.','.','.' ,'.','.', /* A */
      '.','.','.','.','.','.','.','.','.','`','.','.','.','.' ,'.','.', /* B */
      '.','A','B','C','D','E','F','G','H','I','.','.','.','.' ,'.','.', /* C */
      '.','.','J','K','L','M','N','O','P','Q','.','.','.','.' ,'.','.', /* D */
      '.','R','S','T','U','V','W','X','Y','Z','.','.','.','.' ,'.','.', /* E */
      '0','1','2','3','4','5','6','7','8','9','.','.','.','.' ,'.','.'};/* F */

int main(int argc, char *argv[])			/* main program       */
{

    int   x, retval = 1, val;			/* counters, etc.     */
    off_t len;					/* len need to be off_t*/

    windows = (WINS *) calloc(1, sizeof(WINS));	/* malloc windows     */
    head = llalloc();							/* malloc list space  */
    fpINfilename = (char *) malloc(FN_LEN+1);	/* allocate in and    */
    fpOUTfilename = (char *) malloc(FN_LEN+1);	/* out file name ptrs */
    printHex = TRUE;							/* address format     */
    USE_EBCDIC = FALSE;							/*use ascii by default*/

							/* get cmd line args  */
    len = parseArgs(argc, argv, fpINfilename, fpOUTfilename);
    MIN_ADDR_LENGTH = getMinimumAddressLength(len);

    use_env(TRUE);					/* use env values     */
    slk_init(0);					/* init menu bar      */
    init_screen();					/* init visuals       */

    if ((COLS < MIN_COLS) || (LINES < MIN_LINES))	/* screen's too small */
    {
	endwin();
	fprintf(stderr,"\n\nThe screen size too small.\nThe minimum allowable");
	fprintf(stderr," screen size is %dx%d\n\n", MIN_COLS, MIN_LINES + 1);
	exit(-1);
    }
    
    slk_set(6, (printHex) ? "Hex Addr":"Dec Addr", 1);
    init_fkeys();					/* define menu bar    */
    

    while (retval)
    {
	free_windows(windows);
        
	/* calculate screen   */
	BASE                = (resize > 0 && resize < COLS) ? resize:((COLS-6-MIN_ADDR_LENGTH)/4);
	MAXY                = (LINES) - 3;
	hex_win_width       = BASE * 3;
	ascii_win_width     = BASE;
	hex_outline_width   = (BASE * 3) + 3 + MIN_ADDR_LENGTH;
	ascii_outline_width = BASE + 2;
            
	init_menu(windows);				/* init windows       */
	head = freeList(head);				/* free & init head   */
							/* print origin loc   */
	mvwprintw(windows->hex_outline, 0, 1, "%0*d", MIN_ADDR_LENGTH, 0);
    
	if (fpIN != NULL)				/* if no infile...    */
	{
            len = maxLoc(fpIN);				/* get last file loc  */
	    val = maxLines(len); 			/* max file lines     */
            for (x = 0; x <= MAXY && x<=val; x++)       /* output lines       */
		outline(fpIN, x);
	}

	wmove(windows->hex, 0, 0);			/* cursor to origin   */
    
	refreshall(windows);				/* refresh all wins   */
	doupdate();					/* update screen      */
        
	mvwaddch(windows->scrollbar, 1, 0, ACS_CKBOARD);/* clear scroller     */
							/* get user input     */
	retval = wacceptch(windows, len, fpINfilename, fpOUTfilename); 
    }
    
    screen_exit(0);					/* end visualizations */
    return retval;					/* return             */
}

/********************************************************\
 * Description: prints out debug info to a file         *
 * Returns:     nothing                                 *
\********************************************************/
/*
void printDebug(hexList *head, long int loc)
{
    FILE *tmpofp;
    hexList *tmpHead = head;

    tmpofp = fopen("debug_llist", "a+");
    tmpHead = head;

    fprintf(tmpofp, "location undone: %08X\n", loc);
    while (tmpHead != NULL)
    {
	fprintf(tmpofp, "head->loc: %08X   head->val: %02X (%c)\n", tmpHead->loc, tmpHead->val, tmpHead->val);

	tmpHead = tmpHead->next;
    }
    fprintf(tmpofp, "\n");

    fclose(tmpofp);
}
*/

/********************************************************\
 * Description: parses command line arguments and 	*
 *		processes them.				*
 * Returns:	length of file				*
\********************************************************/
off_t parseArgs(int argc, char *argv[], char *fpINfilename, char *fpOUTfilename)
{
    extern char *optarg;				/* extern vars for    */
    extern int optind, /*opterr,*/ optopt;		/* getopt()	      */

    int val;						/* counters, etc.     */

							/* get args           */
    while ((val = hgetopt(argc, argv, "a:i:o:r:e")) != -1) 
    {
	switch (val)					/* test args          */
        {
            case 'a':	printHex = FALSE;		/* decimal addresses  */
                        break;
							/* infile             */
	    case 'i':	strncpy(fpINfilename, optarg, FN_LEN);
			break;
							/* outfile            */
	    case 'o':   strncpy(fpOUTfilename, optarg, FN_LEN);
			break;

            case 'r':   resize = atoi(optarg);		/* don't resize screen*/
                        break;

            case 'e':   USE_EBCDIC=TRUE;		/*use instead of ascii*/
                        break;
							/* help/invalid args  */
							/* help/invalid args  */
	    case '?':	print_usage();			/* output help        */
                        if ((optopt == 'h') || (optopt == '?'))
			    exit(0);			/* exit               */
			else				/* illegal option     */
			    exit(-1);
        }
    }
    argc -= optind;
    argv += optind;

    if (argv[0])
        strncpy(fpINfilename, argv[0], FN_LEN);

    if (strcmp(fpINfilename, ""))
        if ((fpIN = fopen(fpINfilename, "r")) == NULL)
            exit_err("Could not open file");

    return ((fpIN != NULL) ? maxLoc(fpIN):0);		/* return file length */
}

/********************************************************\
 * Description: Get the minimum address length for the  *
 *              address column                          *
 * Returns:	minimum length for addresses		*
\********************************************************/
int getMinimumAddressLength(off_t len)
{
        int min_address_length;
        
        min_address_length = snprintf(NULL, 0, "%jd", (intmax_t)len);
        
        /* At least 8 characters wide */
        return min_address_length > 8 ? min_address_length : 8;
}

/********************************************************\
 * Description: in the event of a segmentation fault    *
 * 		this catches the signal and prints out  *
 *		instructions on where to send a bug     *
 * 		report.					*
 * Returns:	length of file				*
\********************************************************/
void catchSegfault(int sig)
{
    /* Avoid unused variable warning */
    UNUSED(sig);
    
    endwin();
    printf("\n\nHexcurse has encountered a segmentation fault!\n");
    printf("\tPlease submit a full bug report to devel@jewfish.net.\n");
    printf("\tInclude what you did to cause the segfault, and if possible\n");
    printf("\tinclude the core dump.  And for your troubles, we'll add you \n");
    printf("\tto the Changelog. Then you can brag to your friends about it!\n");

    exit(-1);
}