summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrédéric Mangano2013-11-01 17:28:21 +0100
committerFrédéric Mangano2013-11-01 17:28:21 +0100
commitab53c52012a5242e2b4b7cdabeb633dbfdfebad4 (patch)
tree735ea1fa7f0d9ed7392aa0a0e5a94af5b0f90a81
parent3cccfd94b536a29dd743c76aa1f837c063de9e4c (diff)
downloadlastos-ab53c52012a5242e2b4b7cdabeb633dbfdfebad4.tar.gz
lastos-ab53c52012a5242e2b4b7cdabeb633dbfdfebad4.zip
better mem editor
-rw-r--r--kernel.c3
-rw-r--r--shell.c34
-rw-r--r--string.c46
-rw-r--r--string.h1
4 files changed, 65 insertions, 19 deletions
diff --git a/kernel.c b/kernel.c
index 23d84df..f8ace69 100644
--- a/kernel.c
+++ b/kernel.c
@@ -7,6 +7,8 @@ static void echo(struct irc_message *msg, void *data)
irc_privmsg(msg->prefix->nick, msg->message.privmsg.message);
}
+static uint8_t *mem_cursor;
+
static struct irc_peer echo_bot = { IRC_BOT, "echo", "echo", "kernel", { .bot = { echo } } };
extern struct irc_peer shell_bot;
extern struct irc_peer mem_bot;
@@ -28,6 +30,7 @@ void kmain()
irc_add_peer(&serial_user);
irc_add_peer(&echo_bot);
irc_add_peer(&shell_bot);
+ mem_bot.def.bot.data = &mem_cursor;
irc_add_peer(&mem_bot);
for (;;) {
line = sgetline();
diff --git a/shell.c b/shell.c
index a6e5fcd..d796416 100644
--- a/shell.c
+++ b/shell.c
@@ -13,25 +13,39 @@ struct irc_peer shell_bot = { IRC_BOT, "taco", "shell", "kernel", { .bot = { she
static void mem(struct irc_message *msg, void *data)
{
- char buffer[32];
+ char **position = (char**) data;
+ static const page = 32;
+ char buffer[4 * page];
char *cmd = msg->message.privmsg.message;
char *dst = msg->prefix->nick;
+ uint32_t x;
uint32_t arg;
- static char *debug = "I don't get it";
if (msg->type != IRC_PRIVMSG || !*cmd)
return;
arg = hextou32(cmd + 1);
switch (*cmd) {
- case '+':
- (*((uint8_t*) arg))++;
- case '*':
- irc_privmsg(dst, bstohex(buffer, (uint8_t*) arg, 8));
+ case '@': /* set or display location */
+ if (arg)
+ *position = (uint8_t*) arg;
+ else
+ irc_privmsg(dst, u32tohex(buffer, (uint32_t) *position));
break;
- case 'd':
- irc_privmsg(dst, u32tohex(buffer, (uint32_t) debug));
+ case '*': /* move and dereference */
+ if (arg)
+ *position = (uint8_t*) arg;
+ irc_privmsg(dst, bstohex(buffer, *position, page));
break;
- default:
- irc_privmsg(dst, debug);
+ case '+': /* move to next page or display nth page */
+ if (!arg)
+ *position += page;
+ irc_privmsg(dst, bstohex(buffer, *position + page * arg, page));
+ break;
+ case '&': /* call with an optional parameter */
+ x = ((uint32_t (*)(uint32_t))(*position))(arg);
+ irc_privmsg(dst, u32tohex(buffer, x));
+ break;
+ default: /* write data */
+ *position += hextobs(*position, cmd);
}
}
diff --git a/string.c b/string.c
index 74a0657..35498e3 100644
--- a/string.c
+++ b/string.c
@@ -84,20 +84,48 @@ char *bstohex(char *dst, uint8_t *src, int sz)
return dst;
}
+char hexdigit(char c)
+{
+ return
+ (c >= '0' && c <= '9') ? c - '0' :
+ (c >= 'a' && c <= 'f') ? c - 'a' :
+ (c >= 'A' && c <= 'F') ? c - 'A' : -1;
+}
+
uint32_t hextou32(char *src)
{
uint32_t n = 0;
- uint32_t digit;
+ char digit;
for (; *src; src++) {
- if (*src >= '0' && *src <= '9')
- digit = *src - '0';
- else if (*src >= 'a' && *src <= 'f')
- digit = 10 + *src - 'a';
- else if (*src >= 'A' && *src <= 'F')
- digit = 10 + *src - 'A';
- else
+ digit = hexdigit(*src);
+ if (digit == -1)
return n;
- n = n * 16 + digit;
+ n = n * 16 + (uint32_t) digit;
}
return n;
}
+
+int hextobs(uint8_t *dst, char *src)
+{
+ int count = 0;
+ int in = 0;
+ char digit;
+ for (; *src; src++) {
+ if (*src == ' ') {
+ in = 0;
+ continue;
+ }
+ digit = hexdigit(*src);
+ if (digit == -1)
+ return count;
+ if (in) {
+ *dst = *dst * 16 + (uint8_t) digit;
+ dst++;
+ } else {
+ *dst = digit;
+ count++;
+ }
+ in = !in;
+ }
+ return count;
+}
diff --git a/string.h b/string.h
index 6b503c5..1d4488b 100644
--- a/string.h
+++ b/string.h
@@ -12,3 +12,4 @@ char *u32tohex(char *dst, uint32_t n);
char *bstohex(char *dst, uint8_t *src, int sz);
uint32_t hextou32(char *src);
+int hextobs(uint8_t *dst, char *src);