shell: remove old code

tags/0.1.0
S.J.R. van Schaik 8 years ago
parent eb41dd93db
commit 1c7f86529c
  1. 276
      source/shell.c

@ -1,276 +0,0 @@
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <limits.h>
#include <sys/types.h>
#include <flash.h>
#include <macros.h>
#include <shell.h>
static void do_probe(const char *s);
static void do_read(const char *s);
static void do_write(const char *s);
static void do_erase(const char *s);
struct cmd cmds[] = {
{ "probe", do_probe },
{ "read", do_read },
{ "write", do_write },
{ "erase", do_erase },
{ NULL, NULL },
};
static struct flash_dev *flash = NULL;
/* TODO: implement a parser for hex arrays that is more user-friendly.
* Also implement dry runs for programmers. */
static void parse_hex(FILE *fp, char *buf, size_t len)
{
size_t i;
char s[3];
for (i = 0; i < len; ++i) {
memset(s, '\0', 3);
while ((s[0] = getc(fp)) && !isxdigit(s[0]))
putchar(s[0]);
putchar(s[0]);
while ((s[1] = getc(fp)) && !isxdigit(s[1]))
putchar(s[1]);
putchar(s[1]);
*buf++ = strtoul(s, NULL, 16);
}
}
static void print_hex_ascii(const char *buf, size_t len)
{
size_t n, i;
uint8_t c;
for (; len; buf += n, len -= n) {
n = min(len, 16);
for (i = 0; i < 16; ++i) {
c = (i < n) ? buf[i] : 0;
printf("%x", c);
}
printf(" ");
for (i = 0; i < 16; ++i) {
c = (i < n) ? buf[i] : 0;
printf("%c", isalnum(c) ? c : '.');
}
printf("\n");
}
}
static char *prompt(const char *prefix)
{
char *alloc, *line, *s;
size_t nbytes = 0, nalloc_bytes = 16;
char c;
printf(prefix);
if (!(line = malloc(nalloc_bytes)))
return NULL;
s = line;
while ((c = getchar()) && c != '\n') {
putchar(c);
if (nbytes + 1 >= nalloc_bytes) {
nalloc_bytes *= 2;
if (!(alloc = realloc(line, nalloc_bytes)))
goto err_free_line;
line = alloc;
}
*s++ = c;
nbytes++;
}
printf("\n");
*s = '\0';
return line;
err_free_line:
free(line);
return NULL;
}
static void do_probe(const char *s)
{
(void)s;
flash = flash_probe();
}
static void do_read(const char *s)
{
char buf[256], *end = NULL;
size_t addr, len, nbytes;
if (!flash) {
fprintf(stderr, "error: no flash device probed.\n");
return;
}
if (strncmp(s, "0x", 2) == 0)
s += 2;
addr = strtoull(s, &end, 16);
s = end;
if (errno == EINVAL || errno == ERANGE)
return;
s += strspn(s, " \n");
if (strncmp(s, "0x", 2) == 0)
s += 2;
len = strtoull(s, &end, 16);
s = end;
if (errno == EINVAL || errno == ERANGE)
return;
if (!len)
return;
while (len) {
nbytes = min(len, 256);
flash_read(flash, addr, buf, nbytes);
print_hex_ascii(buf, nbytes);
addr += nbytes;
len -= nbytes;
}
}
static void do_write(const char *s)
{
char buf[256], *end = NULL;
size_t addr, len, nbytes;
if (!flash) {
fprintf(stderr, "error: no flash device probed.\n");
return;
}
if (strncmp(s, "0x", 2) == 0)
s += 2;
addr = strtoull(s, &end, 16);
s = end;
if (errno == EINVAL || errno == ERANGE)
return;
s += strspn(s, " \n");
if (strncmp(s, "0x", 2) == 0)
s += 2;
len = strtoull(s, &end, 16);
s = end;
if (errno == EINVAL || errno == ERANGE)
return;
if (!len)
return;
while (len) {
nbytes = (len > 256) ? 256 : len;
parse_hex(stdin, buf, nbytes);
printf("\n");
print_hex_ascii(buf, nbytes);
flash_write(flash, addr, buf, nbytes);
addr += nbytes;
len -= nbytes;
}
}
static void do_erase(const char *s)
{
char *end = NULL;
size_t addr, len;
if (!flash) {
fprintf(stderr, "error: no flash device probed.\n");
return;
}
if (strncmp(s, "0x", 2) == 0)
s += 2;
addr = strtoull(s, &end, 16);
s = end;
if (errno == EINVAL || errno == ERANGE)
return;
s += strspn(s, " \n");
if (strncmp(s, "0x", 2) == 0)
s += 2;
len = strtoull(s, &end, 16);
s = end;
if (errno == EINVAL || errno == ERANGE)
return;
if (!len)
return;
flash_erase(flash, addr, len);
}
static void parse_cmd(const char *s)
{
struct cmd *cmd;
const char *args;
char *key;
size_t n;
n = strcspn(s, " \n");
key = strndup(s, n);
args = s + n;
args += strspn(args, " \n");
for (cmd = cmds; cmd->key; cmd++) {
if (strcmp(cmd->key, key) == 0)
cmd->exec(args);
}
}
void cmd_loop(const char *show)
{
char *line;
while (1) {
line = prompt(show);
parse_cmd(line);
free(line);
}
}
Loading…
Cancel
Save