Add --embedded option and embedded output mode.

This commit is contained in:
Daniel Beer 2012-10-09 15:41:06 +13:00
parent 61fdfd22ff
commit e4a189f177
4 changed files with 68 additions and 9 deletions

View File

@ -53,9 +53,12 @@
#include "goodfet.h" #include "goodfet.h"
#include "input.h" #include "input.h"
#define OPT_NO_RC 0x01
#define OPT_EMBEDDED 0x02
struct cmdline_args { struct cmdline_args {
const char *driver_name; const char *driver_name;
int no_rc; int flags;
struct device_args devarg; struct device_args devarg;
}; };
@ -120,6 +123,8 @@ static void usage(const char *progname)
" on the driver.\n" " on the driver.\n"
" --version\n" " --version\n"
" Show copyright and version information.\n" " Show copyright and version information.\n"
" --embedded\n"
" Run in embedded mode.\n"
"\n" "\n"
"Most drivers connect by default via USB, unless told otherwise via the\n" "Most drivers connect by default via USB, unless told otherwise via the\n"
"-d option. By default, the first USB device found is opened.\n" "-d option. By default, the first USB device found is opened.\n"
@ -207,6 +212,7 @@ static int parse_cmdline_args(int argc, char **argv,
{"force-reset", 0, 0, 'R'}, {"force-reset", 0, 0, 'R'},
{"allow-fw-update", 0, 0, 'A'}, {"allow-fw-update", 0, 0, 'A'},
{"require-fw-update", 1, 0, 'M'}, {"require-fw-update", 1, 0, 'M'},
{"embedded", 0, 0, 'E'},
{NULL, 0, 0, 0} {NULL, 0, 0, 0}
}; };
int want_usb = 0; int want_usb = 0;
@ -224,6 +230,10 @@ static int parse_cmdline_args(int argc, char **argv,
} }
break; break;
case 'E':
args->flags |= OPT_EMBEDDED;
break;
case 'A': case 'A':
args->devarg.flags |= DEVICE_FLAG_DO_FWUPDATE; args->devarg.flags |= DEVICE_FLAG_DO_FWUPDATE;
break; break;
@ -277,7 +287,7 @@ static int parse_cmdline_args(int argc, char **argv,
break; break;
case 'n': case 'n':
args->no_rc = 1; args->flags |= OPT_NO_RC;
break; break;
case 'P': case 'P':
@ -378,6 +388,8 @@ int main(int argc, char **argv)
if (input_module->init() < 0) if (input_module->init() < 0)
return -1; return -1;
output_set_embedded(args.flags & OPT_EMBEDDED);
if (sockets_init() < 0) if (sockets_init() < 0)
return -1; return -1;
@ -392,7 +404,7 @@ int main(int argc, char **argv)
simio_init(); simio_init();
if (!args.no_rc) if (!(args.flags & OPT_NO_RC))
process_rc_file(); process_rc_file();
/* Process commands */ /* Process commands */

View File

@ -137,6 +137,7 @@ void reader_loop(void)
char tmpbuf[MAX_READER_LINE]; char tmpbuf[MAX_READER_LINE];
char *buf = tmpbuf; char *buf = tmpbuf;
printc_shell("ready\n");
if (input_module->read_command(tmpbuf, sizeof(tmpbuf))) if (input_module->read_command(tmpbuf, sizeof(tmpbuf)))
break; break;
@ -146,6 +147,8 @@ void reader_loop(void)
memcpy(tmpbuf, repeat_buf, sizeof(tmpbuf)); memcpy(tmpbuf, repeat_buf, sizeof(tmpbuf));
ctrlc_clear(); ctrlc_clear();
printc_shell("busy\n");
do_command(buf, 1); do_command(buf, 1);
if (want_exit) if (want_exit)

View File

@ -31,6 +31,7 @@
static capture_func_t capture_func; static capture_func_t capture_func;
static void *capture_data; static void *capture_data;
static int is_embedded_mode;
#define LINEBUF_SIZE 4096 #define LINEBUF_SIZE 4096
@ -133,13 +134,18 @@ static void emit_ansi(const char *code, int len, int ansi_state, FILE *out)
* be nul-terminated with no line-ending characters. Embedded ANSI * be nul-terminated with no line-ending characters. Embedded ANSI
* sequences are handled appropriately. * sequences are handled appropriately.
*/ */
static void handle_line(const char *text, FILE *out) static void handle_line(const char *text, FILE *out, char sigil)
{ {
const int want_color = opdb_get_boolean("color"); const int want_color = opdb_get_boolean("color");
char cap_buf[LINEBUF_SIZE]; char cap_buf[LINEBUF_SIZE];
int cap_len = 0; int cap_len = 0;
int ansi_state = 7; int ansi_state = 7;
if (is_embedded_mode) {
out = stdout;
fputc(sigil, out);
}
while (*text) { while (*text) {
int r; int r;
@ -180,7 +186,8 @@ static void handle_line(const char *text, FILE *out)
* we're currently within an ANSI code, so pushing code fragments works * we're currently within an ANSI code, so pushing code fragments works
* correctly. * correctly.
*/ */
static int write_text(struct linebuf *ob, const char *text, FILE *out) static int write_text(struct linebuf *ob, const char *text,
FILE *out, char sigil)
{ {
int count = 0; int count = 0;
@ -192,7 +199,7 @@ static int write_text(struct linebuf *ob, const char *text, FILE *out)
ob->buf[ob->len] = 0; ob->buf[ob->len] = 0;
ob->len = 0; ob->len = 0;
ob->ansi_mode = 0; ob->ansi_mode = 0;
handle_line(ob->buf, out); handle_line(ob->buf, out, sigil);
} else { } else {
if (*text == 0x1b) if (*text == 0x1b)
ob->ansi_mode = 1; ob->ansi_mode = 1;
@ -215,6 +222,7 @@ static int write_text(struct linebuf *ob, const char *text, FILE *out)
static struct linebuf lb_normal; static struct linebuf lb_normal;
static struct linebuf lb_debug; static struct linebuf lb_debug;
static struct linebuf lb_error; static struct linebuf lb_error;
static struct linebuf lb_shell;
int printc(const char *fmt, ...) int printc(const char *fmt, ...)
{ {
@ -225,7 +233,7 @@ int printc(const char *fmt, ...)
vsnprintf(buf, sizeof(buf), fmt, ap); vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap); va_end(ap);
return write_text(&lb_normal, buf, stdout); return write_text(&lb_normal, buf, stdout, ':');
} }
int printc_dbg(const char *fmt, ...) int printc_dbg(const char *fmt, ...)
@ -240,7 +248,7 @@ int printc_dbg(const char *fmt, ...)
vsnprintf(buf, sizeof(buf), fmt, ap); vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap); va_end(ap);
return write_text(&lb_debug, buf, stdout); return write_text(&lb_debug, buf, stdout, '-');
} }
int printc_err(const char *fmt, ...) int printc_err(const char *fmt, ...)
@ -252,7 +260,27 @@ int printc_err(const char *fmt, ...)
vsnprintf(buf, sizeof(buf), fmt, ap); vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap); va_end(ap);
return write_text(&lb_error, buf, stderr); return write_text(&lb_error, buf, stderr, '!');
}
int printc_shell(const char *fmt, ...)
{
char buf[4096];
va_list ap;
if (!is_embedded_mode)
return 0;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
return write_text(&lb_shell, buf, stdout, '\\');
}
void output_set_embedded(int enable)
{
is_embedded_mode = enable;
} }
void pr_error(const char *prefix) void pr_error(const char *prefix)

View File

@ -33,9 +33,25 @@ int printc_dbg(const char *fmt, ...)
__attribute__((format (printf, 1, 2))); __attribute__((format (printf, 1, 2)));
int printc_err(const char *fmt, ...) int printc_err(const char *fmt, ...)
__attribute__((format (printf, 1, 2))); __attribute__((format (printf, 1, 2)));
int printc_shell(const char *fmt, ...)
__attribute__((format (printf, 1, 2)));
void pr_error(const char *prefix); void pr_error(const char *prefix);
/* Enable embedded output mode. When enabled, all logical streams
* are sent to stdout (not stderr), and prefixed with the following
* sigils:
*
* : normal
* ! error
* - debug
* \ shell
*
* Additionally, ANSI codes are used for colourized output on Windows
* instead of changing the console text attributes.
*/
void output_set_embedded(int enable);
/* Capture output. Capturing is started by calling capture_begin() with /* Capture output. Capturing is started by calling capture_begin() with
* a callback function. The callback is invoked for each line of output * a callback function. The callback is invoked for each line of output
* printed to either stdout or stderr (output still goes to * printed to either stdout or stderr (output still goes to