mspdebug/ui/reader.c

270 lines
4.9 KiB
C
Raw Normal View History

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>
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "vector.h"
#include "util.h"
#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"
#include "opdb.h"
2011-04-04 05:09:11 +00:00
#include "aliasdb.h"
2010-04-21 04:56:56 +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;
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
{
char buf[32];
2010-08-13 04:40:58 +00:00
if (!(in_reader_loop && (modify_flags & flags)))
2010-04-21 04:56:56 +00:00
return 0;
for (;;) {
2010-10-11 22:44:46 +00:00
printf("Symbols have not been saved since modification. "
2010-04-21 04:56:56 +00:00
"Continue (y/n)? ");
fflush(stdout);
if (!fgets(buf, sizeof(buf), stdin)) {
printc("\n");
2010-04-21 04:56:56 +00:00
return 1;
}
if (toupper(buf[0]) == 'Y')
return 0;
if (toupper(buf[0]) == 'N')
return 1;
printc("Please answer \"y\" or \"n\".\n");
2010-04-21 04:56:56 +00:00
}
return 0;
}
#ifndef USE_READLINE
#define LINE_BUF_SIZE 128
static char *readline(const char *prompt)
{
char *buf = malloc(LINE_BUF_SIZE);
if (!buf) {
pr_error("readline: can't allocate memory");
2010-04-21 04:56:56 +00:00
return NULL;
}
for (;;) {
printf("(mspdebug) ");
2010-04-21 04:56:56 +00:00
fflush(stdout);
if (fgets(buf, LINE_BUF_SIZE, stdin))
return buf;
if (feof(stdin))
break;
printc("\n");
2010-04-21 04:56:56 +00:00
}
free(buf);
return NULL;
}
#define add_history(x)
#endif
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);
/* 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;
}
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;
}
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
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 (;;) {
char *buf = readline("(mspdebug) ");
char tmpbuf[MAX_READER_LINE];
2010-04-21 04:56:56 +00:00
2010-10-11 22:44:46 +00:00
if (!buf) {
printc("\n");
2010-04-21 04:56:56 +00:00
break;
2010-10-11 22:44:46 +00:00
}
2010-04-21 04:56:56 +00:00
/* Copy into our local buffer and free */
strncpy(tmpbuf, buf, sizeof(tmpbuf));
tmpbuf[sizeof(tmpbuf) - 1] = 0;
2010-04-21 04:56:56 +00:00
free(buf);
buf = tmpbuf;
if (*buf) {
add_history(buf);
repeat_buf[0] = 0;
} else {
memcpy(tmpbuf, repeat_buf, sizeof(tmpbuf));
}
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
}
int process_file(const char *filename, int show)
2010-04-21 04:56:56 +00:00
{
FILE *in;
char buf[1024], *path;
2010-04-21 04:56:56 +00:00
int line_no = 0;
path = expand_tilde(filename);
if (!path)
return -1;
in = fopen(path, "r");
free(path);
2010-04-21 04:56:56 +00:00
if (!in) {
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;
if (show)
printc("\x1b[1m=>\x1b[0m %s", cmd);
2010-08-13 04:40:58 +00:00
if (do_command(cmd, 0) < 0) {
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;
}