aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-11-07 17:05:57 -0500
committerFranklin Wei <git@fwei.tk>2015-11-07 17:05:57 -0500
commitacfc6dd899088fef88c075a11557b37d56f78d5d (patch)
tree523c84618821e8eb7595c07eb498d2dc13a3ad2f /src
parent36af5ceca2e73fb34c0cf28bb6e6bbce25f2b25e (diff)
downloadducky-acfc6dd899088fef88c075a11557b37d56f78d5d.zip
ducky-acfc6dd899088fef88c075a11557b37d56f78d5d.tar.gz
ducky-acfc6dd899088fef88c075a11557b37d56f78d5d.tar.bz2
ducky-acfc6dd899088fef88c075a11557b37d56f78d5d.tar.xz
fix some bugs
Diffstat (limited to 'src')
-rw-r--r--src/ducky.c283
1 files changed, 154 insertions, 129 deletions
diff --git a/src/ducky.c b/src/ducky.c
index 493aa8c..712dd2c 100644
--- a/src/ducky.c
+++ b/src/ducky.c
@@ -88,12 +88,6 @@ uint32_t var_hash(const char *str)
struct varnode_t *lookup_var(const char *name)
{
- static bool firstrun = true;
- if(firstrun)
- {
- firstrun = false;
- memset(var_map, 0, sizeof(var_map));
- }
vid_writef("lookup up variable %s", name);
uint32_t hash = var_hash(name) % VARMAP_SIZE;
@@ -227,28 +221,33 @@ void __attribute__((format(printf,1,2))) warning(const char *fmt, ...)
va_end(ap);
}
+/* grabs a line from a file, -1 on error, returns # bytes read otherwise */
int read_line(int fd, char *buf, size_t sz)
{
unsigned i = 0;
int bytes_read = 0;
+ int status = 1;
while(i < sz)
{
char c;
- if(read(fd, &c, 1) != 1)
+ status = read(fd, &c, 1);
+ if(status != 1)
break;
++bytes_read;
if(c == '\r')
continue;
- if(c == '\n')
+ if(c == '\n' || c == EOF)
+ {
break;
+ }
buf[i++] = c;
}
buf[MIN(i, sz - 1)] = '\0';
- return bytes_read;
+ return (status <= 0)?-1:bytes_read;
}
/* index_lines() precalculates the offset of each line for faster jumping */
@@ -272,12 +271,12 @@ off_t *index_lines(int fd, unsigned *numlines)
char buf[MAX_LINE_LEN];
- if(!read_line(fd, buf, sizeof(buf)))
+ if(read_line(fd, buf, sizeof(buf)) < 0)
break;
char *save = NULL;
char *tok = strtok_r(buf, " \t", &save);
- if(strcmp(tok, "LABEL") == 0 || strcmp("LBL", tok) == 0)
+ if(tok && (strcmp(tok, "LABEL") == 0 || strcmp("LBL", tok) == 0))
{
tok = strtok_r(NULL, " \t", &save);
if(tok && isValidVariable(tok))
@@ -992,141 +991,165 @@ int quit_handler(char **save, int *repeats_left)
struct token_t {
const char *tok;
int (*func)(char **save, int *repeats_left);
+ bool allow_trailing;
+ size_t len;
} tokens[] = {
- { "LET", let_handler },
- { "REPEAT", repeat_handler },
- { "JUMP", goto_handler },
- { "GOTO", goto_handler },
- { "CALL", call_handler },
- { "GOSUB", call_handler },
- { "RET", ret_handler },
- { "RETURN", ret_handler },
- { "INC", inc_handler },
- { "DEC", dec_handler },
- { "IF", if_handler },
- { "DELAY", delay_handler },
- { "LOG", log_handler },
- { "LOGVAR", logvar_handler },
- { "REM", rem_handler },
- { "//", rem_handler },
- { "LABEL", rem_handler },
- { "LBL", rem_handler },
- { "QUIT", quit_handler },
- { "EXIT", quit_handler },
+ { "LET", let_handler, },
+ { "REPEAT", repeat_handler, },
+ { "JUMP", goto_handler, },
+ { "GOTO", goto_handler, },
+ { "CALL", call_handler, },
+ { "GOSUB", call_handler, },
+ { "RET", ret_handler, },
+ { "RETURN", ret_handler, },
+ { "INC", inc_handler, },
+ { "DEC", dec_handler, },
+ { "IF", if_handler, },
+ { "DELAY", delay_handler, },
+ { "LOG", log_handler, },
+ { "LOGVAR", logvar_handler, },
+ { "REM", rem_handler, },
+ { "//", rem_handler, },
+ { "#", rem_handler, },
+ { "LABEL", rem_handler, },
+ { "LBL", rem_handler, },
+ { "QUIT", quit_handler, },
+ { "EXIT", quit_handler, },
};
/* once again, this lookup table is implemented with a perfect hash map */
-#define TOKMAP_SIZE 20
+#define TOKMAP_SIZE 21
struct token_t tokmap[TOKMAP_SIZE];
/* auto-generated with mph-1.2 */
/*
* d=3
- * n=25
- * m=20
+ * n=26
+ * m=21
* c=1.23
* maxlen=6
- * minklen=2
+ * minklen=1
* maxklen=6
- * minchar=47
+ * minchar=35
* maxchar=89
* loop=0
- * numiter=20
+ * numiter=113
* seed=
*/
static int g[] = {
- 12, 11, 0, 16, 0, -1, 9, 17, 14, 2,
- 19, 9, 11, 11, 11, 10, 18, 5, 1, 0,
- 14, 18, 7, 0, 19,
+ 5, 8, 20, 18, 19, 0, 7, 5, 14, 10,
+ 13, 0, 20, 2, 18, 19, 10, 5, 15, 0,
+ 4, 1, 0, 0, 15, 16,
};
static int T0[] = {
- 0x14, 0x15, 0x0b, 0x0e, 0x11, 0x16, 0x18, 0x11, 0x04, 0x0f,
- 0x10, 0x14, 0x0a, 0x01, 0x11, 0x05, 0x06, 0x04, 0x12, 0x17,
- 0x14, 0x01, 0x11, 0x0a, 0x04, 0x10, 0x18, 0x15, 0x0c, 0x10,
- 0x0a, 0x07, 0x0e, 0x17, 0x16, 0x06, 0x15, 0x15, 0x00, 0x02,
- 0x0e, 0x11, 0x16, 0x10, 0x15, 0x08, 0x10, 0x08, 0x0b, 0x0b,
- 0x14, 0x03, 0x0a, 0x04, 0x02, 0x0e, 0x17, 0x0b, 0x03, 0x0f,
- 0x07, 0x15, 0x05, 0x16, 0x14, 0x0c, 0x00, 0x0f, 0x0a, 0x0e,
- 0x09, 0x05, 0x12, 0x0b, 0x15, 0x11, 0x13, 0x0c, 0x02, 0x05,
- 0x00, 0x18, 0x0a, 0x0a, 0x03, 0x0f, 0x09, 0x06, 0x09, 0x0e,
- 0x16, 0x08, 0x06, 0x18, 0x11, 0x12, 0x03, 0x0f, 0x0a, 0x03,
- 0x15, 0x01, 0x00, 0x0d, 0x0e, 0x12, 0x00, 0x03, 0x06, 0x18,
- 0x13, 0x10, 0x14, 0x01, 0x01, 0x17, 0x08, 0x0a, 0x06, 0x12,
- 0x01, 0x05, 0x03, 0x0a, 0x04, 0x16, 0x03, 0x08, 0x0d, 0x14,
- 0x03, 0x0a, 0x07, 0x17, 0x0a, 0x13, 0x06, 0x09, 0x18, 0x0e,
- 0x00, 0x14, 0x01, 0x0a, 0x07, 0x18, 0x0a, 0x10, 0x07, 0x18,
- 0x0f, 0x11, 0x06, 0x0b, 0x14, 0x18, 0x12, 0x06, 0x06, 0x02,
- 0x01, 0x0a, 0x0e, 0x0b, 0x0a, 0x18, 0x05, 0x10, 0x09, 0x06,
- 0x05, 0x09, 0x14, 0x09, 0x08, 0x0e, 0x18, 0x18, 0x16, 0x0f,
- 0x11, 0x0f, 0x18, 0x03, 0x12, 0x03, 0x06, 0x12, 0x07, 0x11,
- 0x02, 0x12, 0x12, 0x0a, 0x0d, 0x18, 0x16, 0x16, 0x05, 0x15,
- 0x08, 0x0d, 0x14, 0x05, 0x16, 0x03, 0x14, 0x15, 0x05, 0x13,
- 0x0e, 0x16, 0x09, 0x0d, 0x02, 0x02, 0x0f, 0x00, 0x14, 0x09,
- 0x13, 0x0f, 0x10, 0x02, 0x0e, 0x13, 0x00, 0x14, 0x0f, 0x0e,
- 0x0b, 0x05, 0x0d, 0x0a, 0x11, 0x0b, 0x07, 0x08, 0x0d, 0x03,
- 0x0e, 0x08, 0x01, 0x0e, 0x02, 0x04, 0x12, 0x13, 0x07, 0x0f,
- 0x03, 0x01, 0x08, 0x16, 0x03, 0x18, 0x12, 0x05,
+ 0x05, 0x02, 0x10, 0x18, 0x10, 0x09, 0x0b, 0x09, 0x02, 0x00,
+ 0x0f, 0x01, 0x04, 0x0c, 0x02, 0x16, 0x09, 0x09, 0x08, 0x03,
+ 0x13, 0x06, 0x13, 0x00, 0x17, 0x16, 0x08, 0x11, 0x0b, 0x02,
+ 0x03, 0x10, 0x06, 0x16, 0x10, 0x17, 0x05, 0x03, 0x08, 0x08,
+ 0x04, 0x19, 0x0b, 0x0a, 0x0c, 0x0e, 0x06, 0x17, 0x19, 0x11,
+ 0x02, 0x14, 0x19, 0x17, 0x14, 0x14, 0x08, 0x02, 0x03, 0x0d,
+ 0x09, 0x16, 0x11, 0x05, 0x0c, 0x0e, 0x13, 0x08, 0x0b, 0x0c,
+ 0x0a, 0x0a, 0x05, 0x17, 0x0b, 0x14, 0x16, 0x04, 0x03, 0x0f,
+ 0x09, 0x07, 0x11, 0x05, 0x06, 0x0d, 0x19, 0x10, 0x11, 0x04,
+ 0x06, 0x00, 0x00, 0x17, 0x06, 0x0c, 0x0d, 0x01, 0x17, 0x18,
+ 0x10, 0x09, 0x0b, 0x15, 0x06, 0x16, 0x11, 0x02, 0x02, 0x14,
+ 0x15, 0x04, 0x07, 0x02, 0x0f, 0x09, 0x14, 0x14, 0x06, 0x12,
+ 0x02, 0x17, 0x18, 0x0a, 0x08, 0x07, 0x0f, 0x0e, 0x11, 0x04,
+ 0x0b, 0x12, 0x0c, 0x08, 0x02, 0x08, 0x18, 0x15, 0x09, 0x13,
+ 0x16, 0x04, 0x19, 0x05, 0x08, 0x0e, 0x11, 0x02, 0x08, 0x17,
+ 0x15, 0x0c, 0x16, 0x13, 0x17, 0x07, 0x03, 0x0c, 0x17, 0x14,
+ 0x10, 0x08, 0x0d, 0x05, 0x12, 0x03, 0x03, 0x11, 0x18, 0x0a,
+ 0x14, 0x09, 0x09, 0x12, 0x05, 0x14, 0x0b, 0x01, 0x0e, 0x16,
+ 0x15, 0x02, 0x0e, 0x01, 0x0a, 0x16, 0x18, 0x06, 0x00, 0x17,
+ 0x18, 0x0d, 0x05, 0x12, 0x0f, 0x10, 0x15, 0x14, 0x07, 0x15,
+ 0x04, 0x03, 0x05, 0x10, 0x16, 0x0a, 0x0a, 0x09, 0x0e, 0x18,
+ 0x07, 0x09, 0x02, 0x15, 0x0a, 0x0d, 0x13, 0x08, 0x13, 0x13,
+ 0x07, 0x08, 0x14, 0x19, 0x15, 0x0b, 0x16, 0x18, 0x0b, 0x14,
+ 0x08, 0x12, 0x13, 0x0b, 0x11, 0x03, 0x11, 0x0b, 0x04, 0x12,
+ 0x0b, 0x0a, 0x12, 0x0b, 0x07, 0x00, 0x10, 0x15, 0x06, 0x08,
+ 0x12, 0x0d, 0x12, 0x0d, 0x0f, 0x0d, 0x18, 0x0b, 0x0d, 0x0b,
+ 0x07, 0x15, 0x05, 0x02, 0x07, 0x17, 0x07, 0x18, 0x08, 0x0c,
+ 0x12, 0x13, 0x16, 0x0c, 0x07, 0x13, 0x19, 0x13, 0x11, 0x14,
+ 0x0e, 0x05, 0x16, 0x0f, 0x13, 0x03, 0x0b, 0x09, 0x16, 0x00,
+ 0x11, 0x06, 0x15, 0x0a, 0x0f, 0x0d, 0x06, 0x04, 0x11, 0x19,
+ 0x01, 0x03, 0x01, 0x0e, 0x10, 0x0d, 0x09, 0x11, 0x06, 0x01,
+ 0x0d, 0x14, 0x06, 0x0b, 0x0c, 0x02, 0x0e, 0x19, 0x0d, 0x0d,
+ 0x00, 0x04, 0x13, 0x17, 0x11, 0x08, 0x0d, 0x17, 0x0c, 0x04,
};
static int T1[] = {
- 0x0a, 0x17, 0x12, 0x16, 0x09, 0x03, 0x16, 0x00, 0x08, 0x02,
- 0x0e, 0x04, 0x07, 0x0c, 0x11, 0x11, 0x03, 0x04, 0x0f, 0x03,
- 0x0d, 0x0a, 0x06, 0x16, 0x04, 0x08, 0x11, 0x0d, 0x0f, 0x05,
- 0x0e, 0x00, 0x06, 0x09, 0x16, 0x0f, 0x0f, 0x15, 0x12, 0x17,
- 0x00, 0x07, 0x04, 0x05, 0x00, 0x0f, 0x14, 0x05, 0x02, 0x05,
- 0x04, 0x07, 0x03, 0x0b, 0x00, 0x05, 0x16, 0x0e, 0x00, 0x11,
- 0x0c, 0x06, 0x0b, 0x14, 0x06, 0x17, 0x0a, 0x01, 0x0e, 0x18,
- 0x0d, 0x16, 0x0e, 0x0f, 0x04, 0x10, 0x05, 0x00, 0x15, 0x08,
- 0x05, 0x01, 0x11, 0x0a, 0x0c, 0x13, 0x0d, 0x06, 0x09, 0x09,
- 0x03, 0x03, 0x12, 0x0c, 0x14, 0x17, 0x08, 0x04, 0x15, 0x05,
- 0x03, 0x15, 0x17, 0x17, 0x0c, 0x0e, 0x0d, 0x03, 0x13, 0x16,
- 0x08, 0x07, 0x01, 0x0c, 0x05, 0x07, 0x18, 0x14, 0x10, 0x0a,
- 0x06, 0x13, 0x10, 0x18, 0x08, 0x0b, 0x17, 0x13, 0x10, 0x0e,
- 0x14, 0x00, 0x05, 0x07, 0x18, 0x17, 0x0a, 0x14, 0x0e, 0x05,
- 0x14, 0x09, 0x05, 0x0e, 0x0a, 0x03, 0x0e, 0x0a, 0x11, 0x04,
- 0x16, 0x18, 0x02, 0x16, 0x0b, 0x01, 0x0b, 0x08, 0x17, 0x00,
- 0x18, 0x12, 0x02, 0x07, 0x01, 0x03, 0x07, 0x0d, 0x18, 0x16,
- 0x14, 0x15, 0x12, 0x11, 0x0e, 0x00, 0x01, 0x0f, 0x08, 0x00,
- 0x0a, 0x03, 0x0c, 0x13, 0x15, 0x15, 0x0f, 0x03, 0x17, 0x0d,
- 0x02, 0x0d, 0x0e, 0x0b, 0x03, 0x0c, 0x12, 0x14, 0x0f, 0x00,
- 0x08, 0x11, 0x0e, 0x01, 0x09, 0x05, 0x02, 0x0a, 0x14, 0x0c,
- 0x0d, 0x06, 0x0f, 0x00, 0x00, 0x15, 0x07, 0x14, 0x00, 0x07,
- 0x12, 0x00, 0x09, 0x17, 0x15, 0x07, 0x0f, 0x11, 0x03, 0x0f,
- 0x0e, 0x17, 0x18, 0x06, 0x06, 0x0a, 0x15, 0x11, 0x05, 0x08,
- 0x17, 0x04, 0x16, 0x11, 0x15, 0x02, 0x0f, 0x03, 0x18, 0x0f,
- 0x0c, 0x11, 0x11, 0x15, 0x11, 0x0f, 0x05, 0x07,
+ 0x16, 0x10, 0x05, 0x09, 0x08, 0x14, 0x02, 0x00, 0x01, 0x11,
+ 0x05, 0x16, 0x0f, 0x06, 0x14, 0x0b, 0x05, 0x02, 0x05, 0x13,
+ 0x15, 0x02, 0x11, 0x0c, 0x0a, 0x13, 0x05, 0x08, 0x0b, 0x06,
+ 0x0d, 0x09, 0x18, 0x14, 0x15, 0x07, 0x0f, 0x17, 0x09, 0x12,
+ 0x10, 0x11, 0x0f, 0x07, 0x19, 0x0b, 0x12, 0x04, 0x0f, 0x17,
+ 0x19, 0x0c, 0x02, 0x11, 0x18, 0x0b, 0x19, 0x01, 0x15, 0x12,
+ 0x04, 0x0c, 0x02, 0x10, 0x14, 0x03, 0x14, 0x0a, 0x19, 0x09,
+ 0x07, 0x13, 0x0c, 0x03, 0x01, 0x11, 0x18, 0x0c, 0x06, 0x07,
+ 0x01, 0x00, 0x12, 0x13, 0x08, 0x16, 0x04, 0x07, 0x17, 0x01,
+ 0x01, 0x03, 0x0e, 0x05, 0x13, 0x08, 0x09, 0x10, 0x14, 0x0a,
+ 0x01, 0x01, 0x05, 0x0f, 0x04, 0x08, 0x06, 0x04, 0x16, 0x0d,
+ 0x0d, 0x02, 0x12, 0x01, 0x01, 0x0c, 0x17, 0x08, 0x19, 0x18,
+ 0x15, 0x17, 0x0c, 0x11, 0x01, 0x00, 0x08, 0x00, 0x03, 0x02,
+ 0x16, 0x19, 0x14, 0x11, 0x10, 0x16, 0x06, 0x07, 0x0d, 0x05,
+ 0x0c, 0x00, 0x07, 0x04, 0x03, 0x0a, 0x12, 0x03, 0x12, 0x11,
+ 0x01, 0x10, 0x11, 0x0f, 0x09, 0x14, 0x11, 0x11, 0x15, 0x14,
+ 0x16, 0x13, 0x16, 0x10, 0x0a, 0x0f, 0x02, 0x0d, 0x06, 0x0b,
+ 0x01, 0x06, 0x19, 0x05, 0x18, 0x04, 0x09, 0x0a, 0x03, 0x0d,
+ 0x02, 0x0e, 0x14, 0x18, 0x0f, 0x0e, 0x07, 0x0f, 0x15, 0x0c,
+ 0x15, 0x09, 0x12, 0x07, 0x0e, 0x0b, 0x16, 0x12, 0x00, 0x03,
+ 0x04, 0x02, 0x0b, 0x05, 0x09, 0x0b, 0x0b, 0x14, 0x15, 0x0e,
+ 0x08, 0x19, 0x05, 0x04, 0x00, 0x16, 0x15, 0x09, 0x0b, 0x12,
+ 0x17, 0x18, 0x0c, 0x03, 0x0d, 0x14, 0x0e, 0x05, 0x09, 0x0f,
+ 0x0a, 0x02, 0x12, 0x08, 0x0e, 0x14, 0x0c, 0x08, 0x07, 0x12,
+ 0x17, 0x09, 0x01, 0x00, 0x07, 0x11, 0x00, 0x00, 0x04, 0x0c,
+ 0x0d, 0x03, 0x0a, 0x01, 0x08, 0x00, 0x15, 0x16, 0x05, 0x04,
+ 0x0d, 0x11, 0x07, 0x05, 0x19, 0x17, 0x00, 0x0d, 0x05, 0x09,
+ 0x08, 0x04, 0x12, 0x0b, 0x05, 0x15, 0x0d, 0x0e, 0x19, 0x0a,
+ 0x0a, 0x03, 0x10, 0x03, 0x02, 0x0d, 0x0c, 0x03, 0x02, 0x09,
+ 0x09, 0x03, 0x01, 0x08, 0x03, 0x01, 0x07, 0x17, 0x00, 0x15,
+ 0x00, 0x05, 0x03, 0x16, 0x01, 0x09, 0x11, 0x10, 0x19, 0x12,
+ 0x01, 0x09, 0x15, 0x11, 0x0c, 0x18, 0x06, 0x01, 0x01, 0x08,
+ 0x0a, 0x0c, 0x0e, 0x0c, 0x16, 0x11, 0x0f, 0x05, 0x0e, 0x11,
};
static int T2[] = {
- 0x06, 0x02, 0x17, 0x0e, 0x15, 0x01, 0x0b, 0x05, 0x0d, 0x02,
- 0x18, 0x0a, 0x03, 0x12, 0x00, 0x10, 0x17, 0x04, 0x18, 0x12,
- 0x13, 0x01, 0x18, 0x06, 0x11, 0x17, 0x09, 0x01, 0x0e, 0x06,
- 0x10, 0x15, 0x09, 0x10, 0x0c, 0x05, 0x13, 0x01, 0x0c, 0x09,
- 0x05, 0x0e, 0x14, 0x16, 0x17, 0x03, 0x08, 0x18, 0x00, 0x15,
- 0x0b, 0x14, 0x18, 0x08, 0x0e, 0x18, 0x02, 0x13, 0x07, 0x0f,
- 0x0d, 0x15, 0x07, 0x13, 0x00, 0x05, 0x03, 0x12, 0x0c, 0x06,
- 0x0a, 0x03, 0x0c, 0x05, 0x03, 0x0a, 0x08, 0x0d, 0x0b, 0x0a,
- 0x0a, 0x00, 0x05, 0x09, 0x08, 0x15, 0x12, 0x06, 0x0e, 0x04,
- 0x17, 0x0d, 0x0c, 0x02, 0x15, 0x0a, 0x12, 0x17, 0x11, 0x13,
- 0x0e, 0x13, 0x09, 0x15, 0x14, 0x10, 0x0d, 0x16, 0x09, 0x13,
- 0x09, 0x0b, 0x04, 0x18, 0x0b, 0x13, 0x0f, 0x07, 0x00, 0x06,
- 0x0b, 0x00, 0x15, 0x00, 0x03, 0x12, 0x0a, 0x15, 0x10, 0x0a,
- 0x00, 0x01, 0x0c, 0x06, 0x0c, 0x05, 0x14, 0x09, 0x08, 0x18,
- 0x18, 0x15, 0x18, 0x15, 0x0b, 0x0a, 0x16, 0x04, 0x08, 0x12,
- 0x0e, 0x04, 0x14, 0x11, 0x16, 0x0c, 0x16, 0x11, 0x05, 0x03,
- 0x03, 0x06, 0x06, 0x0f, 0x0e, 0x12, 0x16, 0x09, 0x03, 0x07,
- 0x08, 0x04, 0x05, 0x17, 0x03, 0x0a, 0x02, 0x0b, 0x02, 0x11,
- 0x0a, 0x05, 0x01, 0x0b, 0x07, 0x09, 0x0c, 0x18, 0x00, 0x0f,
- 0x0d, 0x16, 0x09, 0x06, 0x02, 0x02, 0x02, 0x06, 0x06, 0x16,
- 0x11, 0x02, 0x03, 0x16, 0x02, 0x06, 0x0a, 0x04, 0x13, 0x0c,
- 0x16, 0x06, 0x13, 0x00, 0x14, 0x00, 0x0a, 0x0f, 0x04, 0x12,
- 0x03, 0x0f, 0x0b, 0x0f, 0x11, 0x15, 0x01, 0x0c, 0x13, 0x0e,
- 0x17, 0x07, 0x0c, 0x10, 0x02, 0x00, 0x03, 0x0f, 0x06, 0x00,
- 0x0a, 0x18, 0x01, 0x12, 0x14, 0x12, 0x14, 0x05, 0x0a, 0x02,
- 0x18, 0x10, 0x11, 0x0c, 0x08, 0x09, 0x08, 0x09,
+ 0x19, 0x06, 0x18, 0x03, 0x02, 0x11, 0x12, 0x06, 0x0a, 0x0e,
+ 0x0b, 0x10, 0x0e, 0x19, 0x09, 0x0f, 0x12, 0x0c, 0x19, 0x18,
+ 0x08, 0x18, 0x07, 0x02, 0x11, 0x07, 0x15, 0x18, 0x19, 0x0f,
+ 0x06, 0x00, 0x15, 0x05, 0x03, 0x00, 0x18, 0x17, 0x06, 0x0b,
+ 0x0c, 0x11, 0x01, 0x00, 0x12, 0x0c, 0x11, 0x0c, 0x18, 0x10,
+ 0x0a, 0x06, 0x11, 0x11, 0x0b, 0x19, 0x0a, 0x00, 0x08, 0x10,
+ 0x19, 0x03, 0x05, 0x01, 0x15, 0x14, 0x06, 0x08, 0x03, 0x00,
+ 0x18, 0x14, 0x03, 0x0b, 0x11, 0x11, 0x16, 0x14, 0x0d, 0x14,
+ 0x13, 0x19, 0x01, 0x16, 0x0b, 0x07, 0x17, 0x18, 0x07, 0x06,
+ 0x10, 0x08, 0x09, 0x18, 0x09, 0x06, 0x12, 0x12, 0x10, 0x15,
+ 0x12, 0x10, 0x11, 0x15, 0x04, 0x08, 0x0d, 0x00, 0x05, 0x02,
+ 0x05, 0x04, 0x04, 0x12, 0x17, 0x08, 0x0b, 0x06, 0x05, 0x08,
+ 0x0e, 0x16, 0x04, 0x0b, 0x00, 0x14, 0x02, 0x0d, 0x10, 0x0e,
+ 0x12, 0x17, 0x11, 0x09, 0x00, 0x14, 0x16, 0x01, 0x0d, 0x03,
+ 0x18, 0x12, 0x07, 0x02, 0x0c, 0x06, 0x0c, 0x17, 0x0c, 0x12,
+ 0x07, 0x03, 0x10, 0x0b, 0x10, 0x10, 0x06, 0x12, 0x05, 0x18,
+ 0x07, 0x17, 0x15, 0x18, 0x08, 0x06, 0x04, 0x04, 0x02, 0x13,
+ 0x18, 0x0a, 0x17, 0x01, 0x02, 0x0d, 0x12, 0x17, 0x0b, 0x0f,
+ 0x0f, 0x12, 0x0e, 0x09, 0x05, 0x10, 0x12, 0x0e, 0x07, 0x13,
+ 0x13, 0x03, 0x01, 0x18, 0x00, 0x0c, 0x04, 0x04, 0x13, 0x06,
+ 0x17, 0x13, 0x10, 0x16, 0x15, 0x14, 0x0c, 0x0f, 0x11, 0x17,
+ 0x04, 0x09, 0x12, 0x15, 0x14, 0x19, 0x0b, 0x0c, 0x0d, 0x14,
+ 0x15, 0x05, 0x0d, 0x15, 0x0b, 0x11, 0x0f, 0x10, 0x04, 0x10,
+ 0x02, 0x03, 0x02, 0x0c, 0x11, 0x00, 0x04, 0x06, 0x19, 0x10,
+ 0x0e, 0x0b, 0x0c, 0x11, 0x07, 0x02, 0x02, 0x10, 0x13, 0x11,
+ 0x08, 0x0e, 0x16, 0x18, 0x0b, 0x09, 0x11, 0x02, 0x19, 0x16,
+ 0x12, 0x04, 0x01, 0x14, 0x10, 0x14, 0x16, 0x16, 0x00, 0x16,
+ 0x0f, 0x10, 0x09, 0x01, 0x08, 0x08, 0x0a, 0x18, 0x11, 0x0f,
+ 0x01, 0x12, 0x16, 0x0e, 0x15, 0x12, 0x10, 0x0c, 0x19, 0x01,
+ 0x02, 0x06, 0x13, 0x16, 0x01, 0x0f, 0x17, 0x04, 0x12, 0x12,
+ 0x17, 0x04, 0x09, 0x0f, 0x06, 0x19, 0x17, 0x10, 0x17, 0x10,
+ 0x07, 0x18, 0x09, 0x06, 0x0f, 0x04, 0x18, 0x05, 0x12, 0x00,
+ 0x08, 0x15, 0x06, 0x02, 0x11, 0x07, 0x13, 0x10, 0x0d, 0x0d,
};
#define uchar unsigned char
@@ -1138,25 +1161,25 @@ tok_hash(const uchar *key)
unsigned f0, f1, f2;
const uchar *kp = key;
- for (i=-47, f0=f1=f2=0; *kp; ++kp) {
- if (*kp < 47 || *kp > 89)
+ for (i=-35, f0=f1=f2=0; *kp; ++kp) {
+ if (*kp < 35 || *kp > 89)
return -1;
if (kp-key > 5)
return -1;
f0 += T0[i + *kp];
f1 += T1[i + *kp];
f2 += T2[i + *kp];
- i += 43;
+ i += 55;
}
- if (kp-key < 2)
+ if (kp-key < 1)
return -1;
- f0 %= 25;
- f1 %= 25;
- f2 %= 25;
+ f0 %= 26;
+ f1 %= 26;
+ f2 %= 26;
- return (g[f0] + g[f1] + g[f2]) % 20;
+ return (g[f0] + g[f1] + g[f2]) % 21;
}
void tokmap_insert(struct token_t *tok)
@@ -1172,6 +1195,7 @@ void init_tokmap(void)
memset(tokmap, 0, sizeof(tokmap));
for(unsigned int i = 0; i < ARRAYLEN(tokens); ++i)
{
+ tokens[i].len = strlen(tokens[i].tok);
tokmap_insert(tokens+i);
}
}
@@ -1229,7 +1253,7 @@ void ducky_main(int fd)
{
char instr_buf[MAX_LINE_LEN];
memset(instr_buf, 0, sizeof(instr_buf));
- if(read_line(file_des, instr_buf, sizeof(instr_buf)) == 0)
+ if(read_line(file_des, instr_buf, sizeof(instr_buf)) <= 0)
{
vid_writef("end of file");
goto done;
@@ -1251,7 +1275,8 @@ void ducky_main(int fd)
break;
int hash = tok_hash(tok) % TOKMAP_SIZE;
- if(hash >= 0 && strcmp(tokmap[hash].tok, tok) == 0)
+ struct token_t *t = tokmap+hash;
+ if(hash >= 0 && strcmp(t->tok, tok) == 0)
switch(tokmap[hash].func(&save, &repeats_left))
{
case OK:
@@ -1265,7 +1290,7 @@ void ducky_main(int fd)
default:
error("unknown return");
}
- else
+ else if(tok[0] != '#')
{
error("unknown token `%s` on line %d %d, %d", tok, current_line, hash, tok_hash("CTRL"));
goto done;