-
Notifications
You must be signed in to change notification settings - Fork 8
/
ired.c
163 lines (153 loc) · 3.72 KB
/
ired.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
/* MIT 2009-2021 -- pancake /at/ nopcode /dot/ org */
#define ut64 unsigned long long
#define ut8 unsigned char
static int earlyquit = 0;
static int verbose = 1;
static int scriptn = 0;
static const char **scripts = 0;
static int cmdn = 0;
static char **cmds = 0;
static ut64 oldseek, curseek = 0LL;
static int obsize, bsize = 256;
static int red_cmd(char *cmd); // XXX : recursive depenency
#if defined(__WATCOMC__)
#define HAVE_FTRUNCATE 0
#define BUFSZ 1024
#else
#define BUFSZ 128*1024
#endif
#include "ired.h"
#include "util.c"
#include "calc.c"
#include "cmd.c"
static void red_slurpin() {
ut8 buf[BUFSZ];
for(;;) {
int len = read(0, buf, sizeof(buf));
if(len<1) break;
hexdump(buf, len, 16);
curseek += len;
}
}
static int red_interpret(const char *file) {
char buf[BUFSZ];
FILE *fd = fopen(file, "r");
if(fd != NULL) {
for(;;) {
if(fgets(buf, sizeof(buf), fd) == NULL)
break;
red_cmd(buf);
}
fclose(fd);
} else if (file)
fprintf(stderr, "Cannot open script file '%s'\n", file);
return 1;
}
static int red_cmd(char *cmd) {
char *arg = cmd+1;
SKIPSPACES(arg);
switch(*cmd) {
case 'q': return 0;
case ';': case '#': break; // comment
case '>': return cmd_dump(arg); break;
case '<': return cmd_load(arg); break;
case '.': return red_interpret(arg); break;
case 's': return cmd_seek(arg); break;
case 'b': return cmd_bsize(arg); break;
case '/': return cmd_search(arg); break;
#if USE_DISASM
case 'd': return cmd_disasm(arg); break;
#else
case 'd': return cmd_system("echo X | ired -n $BLOCK | rasm2 -o $OFFSET -D - |head -n $(($LINES-1))");
#endif
case 'p': return cmd_print(arg); break;
case 'r': return cmd_resize(arg); break;
case 'x': return cmd_hexdump(arg); break;
case 'X': return cmd_bytedump(arg); break;
case 'w': return cmd_write(arg); break;
case '!': return cmd_system(arg); break;
case 'V': return cmd_system("vired $FILE"); break;
case '?': return cmd_help(arg); break;
default: fprintf(stderr, "? %s\n", cmd);
}
return 1;
}
static int red_prompt() {
char *at, *at2, line[BUFSZ];
if(verbose) {
printf("[0x%08"LLF"x]> ", curseek);
fflush(stdout);
}
if(fgets(line, sizeof(line), stdin) == NULL)
return 0;
line[strlen(line)-1] = '\0';
if(*line != '!') {
at = strchr(line, '@');
oldseek = curseek;
obsize = bsize;
if(at) {
*at = 0;
at2 = strchr(++at, ':');
if(at2) {
*at2 = 0; at2++;
if(*at2) bsize = (int)str2ut64(at2);
}
if(*at) curseek = str2ut64(at);
}
}
at = line;
SKIPSPACES(at);
return red_cmd(at);
}
static int red_open(char *file) {
int ret = io_open(file);
if(ret != -1) {
oldseek = 0;
setenv("FILE", file, 1);
if(scripts)
for (ret=0;ret<scriptn;ret++)
red_interpret(scripts[ret]);
if(cmds)
for (ret=0;ret<cmdn;ret++)
red_cmd(cmds[ret]);
if(earlyquit)
return 0;
while((ret=red_prompt())>0) {
curseek = oldseek;
bsize = obsize;
}
io_close();
} else if (file)
fprintf(stderr, "Cannot open '%s'\n", file);
return ret==-1 ?1:0;
}
static int red_help() {
puts("ired [-qhnv] [-c cmd] [-i script] [-|file ..]");
return 0;
}
int main(int argc, char **argv) {
int i, ret = 1;
argc++;
cmdn = 0;
scriptn = 0;
scripts = malloc(sizeof(const char*)*argc);
cmds = malloc(sizeof(const char*)*argc);
if(argc>1 && argv[1])
for(i=1; i<argc; i++) {
if(argv[i] && argv[i][0]=='-')
switch(argv[i][1]) {
case 'q': earlyquit = 1; break;
case 'i': scripts[scriptn++] = argv[++i]; break;
case 'c': cmds[cmdn++] = argv[++i]; break;
case 'n': verbose = 0; break;
case 'v': puts(VERSION); ret = 0; break;
case 'h': ret = red_help(); break;
case 0x0: red_slurpin(); ret = 0; break;
} else {
if (!argv[i]) break;
ret = red_open(argv[i]);
}
} else ret = red_help();
free(scripts);
return ret;
}