-
Notifications
You must be signed in to change notification settings - Fork 4
/
commands.c
240 lines (215 loc) · 8.13 KB
/
commands.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
/*******************************************************************************
* BSD 3-Clause License
*
* Copyright (c) 2017, Matt Davis (enferex) https://github.com/enferex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "asrepl.h"
#include "commands.h"
#include "config.h"
#include "tui.h"
/* REPL commands beginning with a leading '/' are considered prefix,
* else they are not prefixed. The reason for the two command types
* is to increase command-lookup speed, while also eliminating any
* potential asm instruction collisions.
*/
#define IS_NOT_PREFIXED(_str) ((_str)[0] != '/')
/* Utility */
#define ARRAY_LENGTH(_a) (sizeof((_a)) / sizeof((_a)[0]))
/* REPL Command */
typedef struct _repl_cmd_t {
const char *command;
const char *description;
void (*fn)(asrepl_t *asr, const struct _repl_cmd_t *, const void *);
_Bool hidden;
} repl_cmd_t;
/* REPL Command Callbacks */
#define DECL_CALLBACK(_name) \
static void cmd_ ##_name (asrepl_t *a, const repl_cmd_t *c, const void *d)
DECL_CALLBACK(dump);
DECL_CALLBACK(exit);
DECL_CALLBACK(help);
DECL_CALLBACK(tui);
DECL_CALLBACK(version);
DECL_CALLBACK(defmacro);
DECL_CALLBACK(endmacro);
DECL_CALLBACK(exemacro);
DECL_CALLBACK(listmacros);
/* Commands defined */
static const repl_cmd_t nonprefixed_cmds[] = {
{MACRO_PREFIX, "Defined macro name (see /help).", cmd_exemacro, true},
{"r", "Dump registers.", cmd_dump, true},
{"q", "Exit", cmd_exit, true},
{"x", "Exit", cmd_exit, true},
{"?", "This help message.", cmd_help, true}
};
/* Commands defined */
static const repl_cmd_t prefixed_cmds[] = {
{"/regs", "Dump registers.", cmd_dump, false},
{"/reg", "Dump registers.", cmd_dump, true},
{"/def", "Define a macro (see /help).", cmd_defmacro, false},
{"/end", "End a macro.", cmd_endmacro, false},
{"/list", "List macros.", cmd_listmacros, false},
{"/help", "This help message.", cmd_help, false},
{"/h", "This help message.", cmd_help, true},
{"/wtf", "This help message.", cmd_help, true},
{"/exit", "Exit", cmd_exit, false},
{"/quit", "Exit", cmd_exit, true},
{"/tui", "Text User Interface", cmd_tui, true},
{"/gui", "Text User Interface", cmd_tui, false},
{"/ver", "About/Version information", cmd_version, false},
{"/version", "About/Version information", cmd_version, true},
{"/about", "About/Version information", cmd_version, true},
};
static void cmd_help(asrepl_t *asr, const repl_cmd_t *cmd, const void *unused)
{
PRINT("Commands:");
for (int i=0; i<ARRAY_LENGTH(nonprefixed_cmds); ++i)
if (!nonprefixed_cmds[i].hidden)
PRINT("%8s: %s",
nonprefixed_cmds[i].command,
nonprefixed_cmds[i].description);
for (int i=0; i<ARRAY_LENGTH(prefixed_cmds); ++i)
if (!prefixed_cmds[i].hidden)
PRINT("%8s: %s",
prefixed_cmds[i].command,
prefixed_cmds[i].description);
PRINT("\nAdditional Information:");
PRINT("/def <name> ");
PRINT(" Defines a macro named <name> which represents the list of ");
PRINT(" assembly instructions following the /def macro. Each assembly");
PRINT(" instruction must begin on its own line. The list is ");
PRINT(" terminated once an /end command is issued. Once defined, a ");
PRINT(" macro can be executed as a command: @<name>");
PRINT(" Example:");
PRINT(" /def mymacro");
PRINT(" mov $0x2a, %%rax");
PRINT(" mov %%rax, %%rbp");
PRINT(" /end");
PRINT(" This macro can now be executed by issuing @mymacro in the REPL.");
}
static void cmd_exit(asrepl_t *asr, const repl_cmd_t *cmd, const void *none)
{
exit(EXIT_SUCCESS);
}
static void cmd_version(asrepl_t *asr, const repl_cmd_t *cmd, const void *none)
{
asrepl_version();
}
static void cmd_dump(asrepl_t *asr, const repl_cmd_t *cmd, const void *none)
{
asrepl_dump_registers(asr);
}
static void cmd_defmacro(
asrepl_t *asr,
const repl_cmd_t *cmd,
const void *line)
{
const char *name = (const char *)line;
/* Locate the command name, and advance past that */
name = strstr(name, cmd->command);
if (!name)
return;
name += strlen(cmd->command);
asrepl_macro_begin(asr, name);
}
static void cmd_endmacro(
asrepl_t *asr,
const repl_cmd_t *cmd,
const void *unused)
{
asrepl_macro_end(asr);
}
static void cmd_exemacro(
asrepl_t *asr,
const repl_cmd_t *cmd,
const void *line)
{
/* Strip off the prefix, asrepl macros have no concept of a prefix. */
asrepl_macro_execute(asr, (char *)line + 1);
}
static void cmd_listmacros(
asrepl_t *asr,
const repl_cmd_t *cmd,
const void *unused)
{
int i = 0;
PRINT("Macros:");
for (const macro_t *macro=asr->macros; macro; macro=macro->next)
PRINT(" %d) %s%s", ++i, MACRO_PREFIX, macro->name);
if (i == 0)
PRINT("(No macros defined)");
}
static void cmd_tui(asrepl_t *asr, const repl_cmd_t *cmd, const void *unused)
{
#ifndef HAVE_LIBNCURSES
ERR("Ncurses support was not compiled into %s", NAME);
return;
#else
static _Bool enabled = false;
enabled ^= true;
if (enabled) {
asr->mode |= MODE_TUI;
tui_init();
}
else {
asr->mode &= ~MODE_TUI;
tui_exit();
}
#endif
}
cmd_status_e asrepl_cmd_process(asrepl_t *asrepl, const char *line)
{
if (!line)
return CMD_NOT_A_COMMAND;
else if (IS_NOT_PREFIXED(line)) {
for (int i=0; i<ARRAY_LENGTH(nonprefixed_cmds); ++i) {
const repl_cmd_t *cmd = &nonprefixed_cmds[i];
if (strncmp(line, cmd->command, strlen(cmd->command)) == 0) {
nonprefixed_cmds[i].fn(asrepl, cmd, line);
return CMD_HANDLED;
}
}
}
else { /* Else: prefixed */
for (int i=0; i<ARRAY_LENGTH(prefixed_cmds); ++i) {
const repl_cmd_t *cmd = &prefixed_cmds[i];
if (strncmp(line, cmd->command, strlen(cmd->command)) == 0) {
prefixed_cmds[i].fn(asrepl, cmd, line);
return CMD_HANDLED;
}
}
}
return CMD_NOT_A_COMMAND;
}