2010-04-21 04:56:56 +00:00
|
|
|
/* MSPDebug - debugging tool for MSP430 MCUs
|
|
|
|
* Copyright (C) 2009, 2010 Daniel Beer
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "vector.h"
|
|
|
|
#include "util.h"
|
2010-08-13 03:45:08 +00:00
|
|
|
#include "output.h"
|
2010-08-13 04:06:23 +00:00
|
|
|
#include "cmddb.h"
|
2010-08-13 04:14:52 +00:00
|
|
|
#include "stdcmd.h"
|
2010-08-13 04:40:58 +00:00
|
|
|
#include "reader.h"
|
2010-08-31 03:50:00 +00:00
|
|
|
#include "opdb.h"
|
2011-04-04 05:09:11 +00:00
|
|
|
#include "aliasdb.h"
|
2012-10-08 20:55:48 +00:00
|
|
|
#include "ctrlc.h"
|
2012-10-09 01:52:02 +00:00
|
|
|
#include "input.h"
|
2010-04-21 04:56:56 +00:00
|
|
|
|
2010-10-11 23:48:25 +00:00
|
|
|
#define MAX_READER_LINE 1024
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
static int modify_flags;
|
|
|
|
static int in_reader_loop;
|
2010-10-11 22:44:46 +00:00
|
|
|
static int want_exit;
|
2010-10-11 23:48:25 +00:00
|
|
|
static char repeat_buf[MAX_READER_LINE];
|
2010-04-21 04:56:56 +00:00
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
void mark_modified(int flags)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
2010-08-13 04:40:58 +00:00
|
|
|
modify_flags |= flags;
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
void unmark_modified(int flags)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
2010-08-13 04:40:58 +00:00
|
|
|
modify_flags &= ~flags;
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int prompt_abort(int flags)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
2012-10-09 01:52:02 +00:00
|
|
|
if (!(in_reader_loop && (modify_flags & flags)))
|
|
|
|
return 0;
|
2010-04-21 04:56:56 +00:00
|
|
|
|
2012-10-09 01:52:02 +00:00
|
|
|
return input_module->prompt_abort("Symbols have not been saved "
|
|
|
|
"since modification. Continue (y/n)?");
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
static int do_command(char *arg, int interactive)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
|
|
|
const char *cmd_text;
|
|
|
|
int len = strlen(arg);
|
|
|
|
|
|
|
|
while (len && isspace(arg[len - 1]))
|
|
|
|
len--;
|
|
|
|
arg[len] = 0;
|
|
|
|
|
|
|
|
cmd_text = get_arg(&arg);
|
|
|
|
if (cmd_text) {
|
2011-04-04 05:09:11 +00:00
|
|
|
char translated[1024];
|
2010-08-13 04:06:23 +00:00
|
|
|
struct cmddb_record cmd;
|
2010-04-21 04:56:56 +00:00
|
|
|
|
2011-04-04 05:09:11 +00:00
|
|
|
if (translate_alias(cmd_text, arg,
|
|
|
|
translated, sizeof(translated)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
arg = translated;
|
|
|
|
cmd_text = get_arg(&arg);
|
|
|
|
|
2011-07-28 10:31:34 +00:00
|
|
|
/* Allow ^[# to stash a command in history without
|
|
|
|
* attempting to execute */
|
|
|
|
if (*cmd_text == '#')
|
|
|
|
return 0;
|
|
|
|
|
2010-08-13 04:06:23 +00:00
|
|
|
if (!cmddb_get(cmd_text, &cmd)) {
|
2010-08-13 04:40:58 +00:00
|
|
|
int old = in_reader_loop;
|
2010-04-21 04:56:56 +00:00
|
|
|
int ret;
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
in_reader_loop = interactive;
|
|
|
|
ret = cmd.func(&arg);
|
|
|
|
in_reader_loop = old;
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("unknown command: %s (try \"help\")\n",
|
2010-04-21 04:56:56 +00:00
|
|
|
cmd_text);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-11 22:44:46 +00:00
|
|
|
void reader_exit(void)
|
|
|
|
{
|
|
|
|
want_exit = 1;
|
|
|
|
}
|
|
|
|
|
2010-10-11 23:48:25 +00:00
|
|
|
void reader_set_repeat(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(repeat_buf, sizeof(repeat_buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
void reader_loop(void)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
2010-08-13 04:40:58 +00:00
|
|
|
int old = in_reader_loop;
|
2010-04-30 10:20:58 +00:00
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
in_reader_loop = 1;
|
2010-04-30 10:20:58 +00:00
|
|
|
|
2010-08-31 03:50:00 +00:00
|
|
|
if (!opdb_get_boolean("quiet")) {
|
|
|
|
printc("\n");
|
|
|
|
cmd_help(NULL);
|
|
|
|
printc("\n");
|
|
|
|
}
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
do {
|
2010-10-11 22:44:46 +00:00
|
|
|
want_exit = 0;
|
|
|
|
|
2010-04-21 04:56:56 +00:00
|
|
|
for (;;) {
|
2010-10-11 23:48:25 +00:00
|
|
|
char tmpbuf[MAX_READER_LINE];
|
2012-10-09 01:52:02 +00:00
|
|
|
char *buf = tmpbuf;
|
2010-04-21 04:56:56 +00:00
|
|
|
|
2012-10-09 02:41:06 +00:00
|
|
|
printc_shell("ready\n");
|
2012-10-09 01:52:02 +00:00
|
|
|
if (input_module->read_command(tmpbuf, sizeof(tmpbuf)))
|
2010-04-21 04:56:56 +00:00
|
|
|
break;
|
2010-10-11 23:48:25 +00:00
|
|
|
|
2012-10-09 01:52:02 +00:00
|
|
|
if (*buf)
|
2010-10-11 23:48:25 +00:00
|
|
|
repeat_buf[0] = 0;
|
2012-10-09 01:52:02 +00:00
|
|
|
else
|
2010-10-11 23:48:25 +00:00
|
|
|
memcpy(tmpbuf, repeat_buf, sizeof(tmpbuf));
|
|
|
|
|
2012-10-08 20:55:48 +00:00
|
|
|
ctrlc_clear();
|
2012-10-09 02:41:06 +00:00
|
|
|
|
|
|
|
printc_shell("busy\n");
|
2010-10-11 23:48:25 +00:00
|
|
|
do_command(buf, 1);
|
2010-10-11 22:44:46 +00:00
|
|
|
|
|
|
|
if (want_exit)
|
|
|
|
break;
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
2010-08-13 04:40:58 +00:00
|
|
|
} while (prompt_abort(MODIFY_SYMS));
|
2010-04-21 04:56:56 +00:00
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
in_reader_loop = old;
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int process_command(char *cmd)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
2010-08-13 04:40:58 +00:00
|
|
|
return do_command(cmd, 0);
|
2010-04-21 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2010-10-11 22:53:46 +00:00
|
|
|
int process_file(const char *filename, int show)
|
2010-04-21 04:56:56 +00:00
|
|
|
{
|
|
|
|
FILE *in;
|
2011-07-28 15:20:44 +00:00
|
|
|
char buf[1024], *path;
|
2010-04-21 04:56:56 +00:00
|
|
|
int line_no = 0;
|
|
|
|
|
2011-07-28 15:20:44 +00:00
|
|
|
path = expand_tilde(filename);
|
|
|
|
if (!path)
|
|
|
|
return -1;
|
|
|
|
|
2011-08-01 23:56:59 +00:00
|
|
|
in = fopen(path, "r");
|
2011-07-28 15:20:44 +00:00
|
|
|
free(path);
|
2010-04-21 04:56:56 +00:00
|
|
|
if (!in) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("read: can't open %s: %s\n",
|
2011-07-26 13:51:06 +00:00
|
|
|
filename, last_error());
|
2010-04-21 04:56:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), in)) {
|
|
|
|
char *cmd = buf;
|
|
|
|
|
|
|
|
line_no++;
|
|
|
|
|
|
|
|
while (*cmd && isspace(*cmd))
|
|
|
|
cmd++;
|
|
|
|
|
|
|
|
if (*cmd == '#')
|
|
|
|
continue;
|
|
|
|
|
2010-10-11 22:53:46 +00:00
|
|
|
if (show)
|
|
|
|
printc("\x1b[1m=>\x1b[0m %s", cmd);
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
if (do_command(cmd, 0) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("read: error processing %s (line %d)\n",
|
2010-04-21 04:56:56 +00:00
|
|
|
filename, line_no);
|
|
|
|
fclose(in);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(in);
|
|
|
|
return 0;
|
|
|
|
}
|