From 89e35bddfb92c6c37177d9830bf0664e9abb29d9 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Sun, 8 Nov 2015 12:42:29 -0500 Subject: stuff --- src/ducky.c | 354 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 187 insertions(+), 167 deletions(-) (limited to 'src') diff --git a/src/ducky.c b/src/ducky.c index 712dd2c..e3b00b6 100644 --- a/src/ducky.c +++ b/src/ducky.c @@ -19,8 +19,10 @@ * * "LET X=;" - loads the value of into variable X. Greedy. * - * "LOG ..." - outputs any remaining text to the device's screen. Greedy. + * "LOG ..." - outputs any remaining text to the device's screen. * "LOGVAR ;" - outputs variable in decimal to the device's screen + * "NEWLINE" - outputs a newline + * "LOGCHAR ;" - outputs as an ASCII character ******************************************************************************/ /*** Defines ***/ @@ -35,14 +37,14 @@ #define CALL_STACK_SZ 64 #define VARMAP_SIZE 256 -#define VARFORMAT "%d" +#define VARFORMAT "%lld" #define VARNAME_MAX 24 #define ARRAYLEN(x) (sizeof(x)/sizeof(x[0])) #define MIN(x,y) ((x 89) + for (i=-47, f0=f1=f2=0; *kp; ++kp) { + if (*kp < 47 || *kp > 89) return -1; - if (kp-key > 5) + if (kp-key > 6) return -1; f0 += T0[i + *kp]; f1 += T1[i + *kp]; f2 += T2[i + *kp]; - i += 55; + i += 43; } - if (kp-key < 1) + if (kp-key < 2) return -1; - f0 %= 26; - f1 %= 26; - f2 %= 26; + f0 %= 28; + f1 %= 28; + f2 %= 28; - return (g[f0] + g[f1] + g[f2]) % 21; + return (g[f0] + g[f1] + g[f2]) % 22; } void tokmap_insert(struct token_t *tok) { uint32_t hash = tok_hash(tok->tok) % TOKMAP_SIZE; - if(tokmap[hash].tok) - error("hash map collision %lu: %s VS %s", hash, tok->tok, tokmap[hash].tok); + if(hash < 0 || tokmap[hash].tok) + error("FIXME: hash map collision"); memcpy(tokmap+hash, tok, sizeof(*tok)); } @@ -1195,7 +1206,6 @@ 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); } } @@ -1211,13 +1221,16 @@ void init_globals(void) memset(var_map, 0, sizeof(var_map)); } -void ducky_main(int fd) +void ducky_main(int fd, bool verbose) { init_globals(); - vid_write("*** DS-2 INIT ***"); - vid_write("QUACK AT YOUR OWN RISK!"); - vid_write("The author assumes no liability for any damages caused by this program."); + if(verbose) + { + vid_write("*** DS-2 INIT ***"); + vid_write("QUACK AT YOUR OWN RISK!"); + vid_write("The author assumes no liability for any damages caused by this program."); + } file_des = fd; @@ -1243,10 +1256,12 @@ void ducky_main(int fd) setConst("false", true); line_offset = index_lines(file_des, &num_lines); - vid_writef("Indexing complete (%u lines).", num_lines); - - vid_write("Executing..."); + if(verbose) + { + vid_writef("Indexing complete (%u lines).", num_lines); + vid_write("Executing..."); + } int repeats_left = 0; while(1) @@ -1255,7 +1270,8 @@ void ducky_main(int fd) memset(instr_buf, 0, sizeof(instr_buf)); if(read_line(file_des, instr_buf, sizeof(instr_buf)) <= 0) { - vid_writef("end of file"); + if(verbose) + vid_writef("end of file"); goto done; } char *tok = NULL, *save = NULL; @@ -1288,11 +1304,11 @@ void ducky_main(int fd) case NEXT: goto next_line; default: - error("unknown return"); + error("FIXME: invalid return value"); } else if(tok[0] != '#') { - error("unknown token `%s` on line %d %d, %d", tok, current_line, hash, tok_hash("CTRL")); + error("unknown token `%s` on line %d %d", tok, current_line); goto done; } } while(tok); @@ -1304,7 +1320,11 @@ void ducky_main(int fd) if(repeats_left) jump_line(file_des, current_line); else + { + if(current_line + 2 > num_lines) + goto done; jump_line(file_des, current_line + 2); + } } next_line: ; -- cgit v1.1