forked from pixel/hexedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexedit.c
280 lines (233 loc) · 7.45 KB
/
hexedit.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
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
/* hexedit -- Hexadecimal Editor for Binary Files
Copyright (C) 1998 Pixel (Pascal Rigaux)
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, 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 "hexedit.h"
/*******************************************************************************/
/* Global variables */
/*******************************************************************************/
INT lastEditedLoc, biggestLoc, fileSize;
INT mark_min, mark_max, mark_set;
INT base, oldbase;
int normalSpaces, cursor, cursorOffset, hexOrAscii;
int cursor, blocSize, lineLength, colsUsed, page;
int fd, nbBytes, oldcursor, oldattr, oldcursorOffset;
int sizeCopyBuffer, *bufferAttr;
char *progName, *fileName, *baseName;
unsigned char *buffer, *copyBuffer;
typePage *edited;
char *lastFindFile = NULL, *lastYankToAFile = NULL, *lastAskHexString = NULL, *lastAskAsciiString = NULL, *lastFillWithStringHexa = NULL, *lastFillWithStringAscii = NULL;
const modeParams modes[LAST] = {
{ 8, 16, 256 },
{ 4, 0, 0 },
};
modeType mode = maximized;
int colored = FALSE;
int isReadOnly = FALSE;
int disableOpts = FALSE;
const char * const usage = "usage: %s [-s | --sector] [-m | --maximize] [-l<n> | --linelength <n>] [-r | --readonly]"
#ifdef HAVE_COLORS
" [-c | --color]"
#endif
" [-h | --help] filename\n";
typedef void (*ParseArgCallbackFn)(const char *nextArg);
static void pacallback_sector (const char *unused);
static void pacallback_readOnly (const char *unused);
static void pacallback_maximize (const char *unused);
#ifdef HAVE_COLORS
static void pacallback_color (const char *unused);
#endif
static void pacallback_lineLength (const char *nextArg);
static void pacallback_disableOpts (const char *unused);
static void pacallback_usage (const char *unused);
static inline int parseArg_matchOpt (const char *arg, const char *shortName, const char *longName);
static inline int parseArg (const char **argv);
/* name: pacallback_*
* nextArg: argument for option, if applicable
* desc: these are callbacks per ParseArgCallbackFn, used by parseArg
*/
static void pacallback_sector(const char *unused)
{
(void) unused;
mode = bySector;
}
static void pacallback_readOnly(const char *unused)
{
(void) unused;
isReadOnly = TRUE;
}
static void pacallback_maximize(const char *unused)
{
(void) unused;
mode = maximized;
lineLength = 0;
}
#ifdef HAVE_COLORS
static void pacallback_color(const char *unused)
{
(void) unused;
colored = TRUE;
}
#endif
static void pacallback_lineLength(const char *nextArg)
{
lineLength = atoi(nextArg);
if (lineLength < 0 || lineLength > 4096)
DIE("%s: illegal line length\n")
}
static void pacallback_disableOpts(const char *unused)
{
(void) unused;
disableOpts = TRUE;
}
static void pacallback_usage(const char *unused)
{
(void) unused;
DIE(usage);
}
/* name: parseArg_matchOpt
* arg: current argument
* shortName: short name to check against
* longName: long name to check against
* return: number indicating status
* 0: no match
* 1: short match
* 2: long match
*/
static inline int parseArg_matchOpt(const char *arg, const char *shortName, const char *longName)
{
if (shortName != NULL && strbeginswith(arg, shortName))
return 1;
if (longName != NULL && strbeginswith(arg, longName))
return 2;
return 0;
}
/* name: parseArg
* argvp: pointer to current argv offset
* return: number of arguments to advance
* desc: parses arguments according to embedded argument list.
* Execution is handled by callback functions.
* notes:
*
* 1. This can be canonized as a type in a future update, but is left
* anonymous for now
*
* 2. Adds support for the --longopt=value convention
*
* 3. if a filename was already selected, free the existing one and
* replace it. Note that file scope variables are guaranteed to be
* zero initialized per the standard.
*
* 4. Fixes issue #70 -- if no options match the argument, it is assumed
* to be the filename. The effect is that the most recent "naked"
* argument is used as the filename.
*/
static inline int parseArg(const char **argvp)
{
static const struct { // 1
const char * shortName;
const char * longName;
int hasArg;
ParseArgCallbackFn callback;
} opts[] = {
{"-s", "--sector", FALSE, pacallback_sector},
{"-r", "--readonly", FALSE, pacallback_readOnly},
{"-m", "--maximize", FALSE, pacallback_maximize},
#ifdef HAVE_COLORS
{"-c", "--color", FALSE, pacallback_color},
#endif
{"-l", "--linelength", TRUE, pacallback_lineLength},
{"-h", "--help", FALSE, pacallback_usage},
{"--", NULL, FALSE, pacallback_disableOpts},
{"-", NULL, FALSE, pacallback_usage},
};
static const size_t opts_len = sizeof opts / sizeof *opts;
const char *curArg = argvp[0];
const char *nextArg = NULL;
int advanceBy = 1;
if (disableOpts)
goto exit_disableOpts;
for (size_t i = 0; i < opts_len; ++i)
{
int matchOpt = parseArg_matchOpt(curArg, opts[i].shortName, opts[i].longName);
const char *strchrMatch;
if (matchOpt == 0)
continue;
if (opts[i].hasArg)
{
if (matchOpt == 1 && strlen(curArg) > 2)
nextArg = curArg + 2;
else if (matchOpt == 2 && (strchrMatch = strchr(curArg, '=')) != NULL) // 2
nextArg = strchrMatch + 1;
else
{
nextArg = argvp[1];
advanceBy = 2;
}
}
opts[i].callback(nextArg);
return advanceBy;
}
exit_disableOpts:
if (fileName != NULL) // 3
free(fileName);
fileName = strdup(curArg); // 4
return 1;
}
/*******************************************************************************/
/* main */
/*******************************************************************************/
int main(int argc, char **argv)
{
progName = basename(argv[0]);
argv++; argc--;
for (; argc > 0;)
{
int advanceBy = parseArg((const char **)argv);
argv += advanceBy;
argc -= advanceBy;
}
init();
initCurses();
if (fileName == NULL && !findFile()) {
exitCurses();
DIE("%s: No such file\n");
}
openFile();
readFile();
do display();
while (key_to_function(getch()));
quit();
return 0; /* for no warning */
}
/*******************************************************************************/
/* other functions */
/*******************************************************************************/
void init(void)
{
page = 0; /* page == 0 means initCurses is not done */
normalSpaces = 3;
hexOrAscii = TRUE;
copyBuffer = NULL;
edited = NULL;
}
void quit(void)
{
exitCurses();
free(fileName);
free(buffer);
free(bufferAttr);
FREE(copyBuffer);
discardEdited();
FREE(lastFindFile); FREE(lastYankToAFile); FREE(lastAskHexString); FREE(lastAskAsciiString); FREE(lastFillWithStringHexa); FREE(lastFillWithStringAscii);
exit(0);
}