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
|
/* Copyright 2019
* Ramon Fried <ramon.fried@gmail.com>
*/
#include <string.h>
#include <inttypes.h>
#include <ctype.h>
#include "bitwise.h"
#include "shunting-yard.h"
#define MAX_TOKENS 4
static int cmd_clear(char **argv, int argc);
static int cmd_quit(char **argv, int argc);
static int cmd_help(char **argv, int argc);
static int cmd_set_width(char **argv, int argc);
static int cmd_set_output(char **argv, int argc);
struct cmd {
const char *name;
int min_args;
int max_args;
int (*func)(char **argv, int argc);
};
static struct cmd cmds[] = {
{"clear", 0, 0, cmd_clear},
{"c", 0, 0, cmd_clear},
{"help", 0, 0, cmd_help},
{"h", 0, 0, cmd_help},
{"quit", 0, 0, cmd_quit},
{"q", 0, 0, cmd_quit},
{"width ", 1, 1, cmd_set_width},
{"w ", 1, 1, cmd_set_width},
{"output ", 1, 1, cmd_set_output},
{"o ", 1, 1, cmd_set_output},
};
static int get_cmd(const char *cmd_name)
{
size_t i = 0;
for (i = 0; i < ARRAY_SIZE(cmds); i++) {
LOG("comparing %s to command %s\n", cmd_name, cmds[i].name);
if (cmds[i].max_args == 0) {
/* These commands do not have arguments, compare exact */
if (!strcmp(cmds[i].name, cmd_name))
return i;
} else {
/*
* Commands with arguments end with a trailing space,
* compare just the beginning of the command including
* the space
*/
if (!strncmp(cmds[i].name, cmd_name, strlen(cmds[i].name)))
return i;
}
}
return -1;
}
void show_error(Status status)
{
char *message = NULL;
switch (status) {
case ERROR_SYNTAX:
message = "Syntax error";
break;
case ERROR_OPEN_PARENTHESIS:
message = "Missing parenthesis";
break;
case ERROR_CLOSE_PARENTHESIS:
message = "Extra parenthesis";
break;
case ERROR_UNRECOGNIZED:
message = "Unknown character";
break;
case ERROR_NO_INPUT:
message = "Empty expression";
break;
case ERROR_UNDEFINED_FUNCTION:
message = "Unknown function";
break;
case ERROR_FUNCTION_ARGUMENTS:
message = "Missing function arguments";
break;
case ERROR_UNDEFINED_CONSTANT:
message = "Unknown constant";
break;
case ERROR_WRONG_ARGUMENTS:
message = "Wrong arguments";
break;
case ERROR_DIVIDE_BY_ZERO:
message = "Divide by zero";
break;
default:
message = "Unknown error";
}
werase(cmd_win);
mvwprintw(cmd_win, 0, 0, "%s", message);
append_to_history(message, TYPE_OUTPUT_ERROR);
wrefresh(cmd_win);
}
/* Remove trailing and leading white spaces, and NULL if all-white */
static char *trim_whitespace(char *str)
{
char *str_end = &str[strlen(str) - 1];
size_t i;
/* Remove leding white characters */
while (isspace(str[0]))
str++;
/* There are only white characters */
if (str[0] == '\0')
return NULL;
/* There is at least one non-white character, find the last one */
while (isspace(str_end[0]))
str_end--;
str_end[1] = '\0';
return str;
}
static int parse_cmd(char *cmdline)
{
static char *tokens[MAX_TOKENS];
int cmd_entry;
int i = 0;
int rc = 0;
uint64_t result;
cmdline = trim_whitespace(cmdline);
if (!cmdline)
return 0;
LOG("got command: %s\n", cmdline);
/* First let's see if this is a command */
cmd_entry = get_cmd(cmdline);
if (cmd_entry >= 0) {
/* It looks like a command, let's tokenize this */
append_to_history(cmdline, TYPE_INPUT_COMMAND);
tokens[i] = strtok(cmdline, " ");
LOG("%s\n", tokens[i]);
do {
i++;
tokens[i] = strtok(NULL, " ");
LOG("%s\n", tokens[i]);
} while (tokens[i] != NULL && (i) < MAX_TOKENS);
LOG("Finished tokenizing %d tokens\n", i);
if ((i - 1 >= cmds[cmd_entry].min_args) &&
(i - 1 <= cmds[cmd_entry].max_args))
rc = cmds[cmd_entry].func(&tokens[1], i - 1);
else {
werase(cmd_win);
mvwprintw(cmd_win, 0, 0, "%s: Invalid parameter(s)", tokens[0]);
wrefresh(cmd_win);
LOG("Invalid parameters\n");
return -1;
}
if (rc) {
show_error(rc);
return -1;
}
} else {
append_to_history(cmdline, TYPE_INPUT_EXPRESSION);
Status status = shunting_yard(cmdline, &result);
if (status != OK) {
show_error(status);
return -1;
} else {
char result_string[256];
if (result > MASK(g_width)) {
result &= MASK(g_width);
append_to_history("Overflow!", TYPE_OUTPUT_ERROR);
}
g_val = result;
if (g_output == CMD_OUTPUT_ALL) {
sprintf_type(result, result_string,
CMD_OUTPUT_DECIMAL);
append_to_history(result_string,
TYPE_OUTPUT_RESULT);
sprintf_type(result, result_string,
CMD_OUTPUT_HEXADECIMAL);
append_to_history(result_string,
TYPE_OUTPUT_RESULT);
sprintf_type(result, result_string,
CMD_OUTPUT_OCTAL);
append_to_history(result_string,
TYPE_OUTPUT_RESULT);
sprintf_type(result, result_string,
CMD_OUTPUT_BINARY);
append_to_history(result_string,
TYPE_OUTPUT_RESULT);
update_binary();
update_fields(-1);
} else {
sprintf_type(result, result_string, g_output);
update_binary();
update_fields(-1);
append_to_history(result_string,
TYPE_OUTPUT_RESULT);
}
}
}
return 0;
}
static int readline_input_avail(void)
{
return g_input_avail;
}
static int readline_getc(FILE *dummy)
{
g_input_avail = false;
return g_input;
}
static void got_command(char *line)
{
int rc;
/* Handle exit commands immediately */
if (!line) {
/* Handle CTL-D */
g_leave_req = true;
return;
}
if (*line)
add_history(line);
rc = parse_cmd(line);
free(line);
if (!rc) {
keypad(stdscr, FALSE);
werase(cmd_win);
wrefresh(cmd_win);
}
return;
}
void readline_redisplay(void)
{
if (active_win != COMMAND_WIN)
return;
werase(cmd_win);
mvwprintw(cmd_win, 0, 0, "%s%s", rl_display_prompt, rl_line_buffer);
wmove(cmd_win, 0, rl_point + 1);
wrefresh(cmd_win);
}
void init_readline(void)
{
rl_catch_signals = 0;
rl_catch_sigwinch = 0;
rl_deprep_term_function = NULL;
rl_prep_term_function = NULL;
rl_change_environment = 0;
rl_getc_function = readline_getc;
rl_input_available_hook = readline_input_avail;
rl_redisplay_function = readline_redisplay;
//rl_bind_key('\t', rl_insert);
rl_callback_handler_install(":", got_command);
}
void deinit_readline(void)
{
rl_callback_handler_remove();
}
void process_cmd(int ch)
{
int new_char;
LOG("Process cmd %u\n", ch);
if (ch == 27) {
nodelay(get_win(active_win), true);
new_char = wgetch(get_win(active_win));
nodelay(get_win(active_win), false);
if (new_char == ERR) {
LOG("IT's a real escape\n");
active_win = last_win;
// Re-enable mouse events
mousemask(BUTTON1_CLICKED, NULL);
if (active_win == FIELDS_WIN) {
set_active_field(false);
form_driver(form, REQ_END_LINE);
form_driver(form, REQ_VALIDATION);
curs_set(1);
wrefresh(fields_win);
} else if (active_win == BINARY_WIN) {
curs_set(0);
position_binary_curser(0, bit_pos);
wrefresh(binary_win);
}
keypad(stdscr, FALSE);
werase(cmd_win);
wrefresh(cmd_win);
return;
} else
ungetch(new_char);
}
/* If TAB || BACKSPACE (to -1) */
if (ch == 127 && !rl_point) {
LOG("Detected exit cmd\n");
active_win = last_win;
if (active_win == FIELDS_WIN) {
set_active_field(false);
form_driver(form, REQ_END_LINE);
form_driver(form, REQ_VALIDATION);
curs_set(1);
wrefresh(fields_win);
} else if (active_win == BINARY_WIN) {
curs_set(0);
position_binary_curser(0, bit_pos);
wrefresh(binary_win);
}
keypad(stdscr, FALSE);
werase(cmd_win);
wrefresh(cmd_win);
return;
}
g_input = ch;
g_input_avail = true;
rl_callback_read_char();
}
static int cmd_clear(char **argv, int argc)
{
LOG("%s: argc %d\n", __func__, argc);
flush_history();
update_history_win();
return 0;
}
static int cmd_quit(char **argv, int argc)
{
LOG("%s: argc %d\n", __func__, argc);
g_leave_req = true;
return 0;
}
static int cmd_set_width(char **argv, int argc)
{
LOG("%s: argc %d\n", __func__, argc);
if (!strcmp("64", argv[0]))
set_fields_width(64);
else if (!strcmp("32", argv[0]))
set_fields_width(32);
else if (!strcmp("16", argv[0]))
set_fields_width(16);
else if (!strcmp("8", argv[0]))
set_fields_width(8);
else
return ERROR_WRONG_ARGUMENTS;
unpaint_screen();
paint_screen();
return 0;
}
static int cmd_set_output(char **argv, int argc)
{
LOG("%s: argc %d\n", __func__, argc);
if (!strcmp("hexadecimal", argv[0]) || !strcmp("hex", argv[0]))
g_output = CMD_OUTPUT_HEXADECIMAL;
else if (!strcmp("decimal", argv[0]) || !strcmp("dec", argv[0]))
g_output = CMD_OUTPUT_DECIMAL;
else if (!strcmp("octal", argv[0]) || !strcmp("oct", argv[0]))
g_output = CMD_OUTPUT_OCTAL;
else if (!strcmp("binary", argv[0]) || !strcmp("bin", argv[0]))
g_output = CMD_OUTPUT_BINARY;
else if (!strcmp("all", argv[0]))
g_output = CMD_OUTPUT_ALL;
else
return ERROR_WRONG_ARGUMENTS;
return 0;
}
static int cmd_help(char **argv, int argc)
{
unpaint_screen();
curs_set(0);
show_help();
curs_set(2);
paint_screen();
return 0;
}
|