From 4706199a81dc631b6969927e1a6ad27591852b20 Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Mon, 16 Apr 2012 16:02:51 -0700 Subject: [PATCH] began support for DAT assembler directive initial support for DAT in assembler moved some common routines out to shared non-exported module --- .gitignore | 1 + Makefile | 13 ++- as-dcpu16.c | 317 ++++++++++++++++++++++++++-------------------------- common.c | 202 +++++++++++++++++++++++++++++++++ common.h | 24 ++++ vm-dcpu16.c | 40 ++----- 6 files changed, 404 insertions(+), 193 deletions(-) create mode 100644 common.c create mode 100644 common.h diff --git a/.gitignore b/.gitignore index 73763e4..d1abffb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.depend as-dcpu16 vm-dcpu16 *.o diff --git a/Makefile b/Makefile index a0745cf..1bc867c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,12 @@ #!make # + +ifeq "$(origin CC)" "default" +CC = clang +endif + PROGRAMS = as-dcpu16 vm-dcpu16 -SOURCES = dcpu16.c as-dcpu16.c vm-dcpu16.c +SOURCES = common.c dcpu16.c as-dcpu16.c vm-dcpu16.c CFLAGS = -g -Wall -Wextra -pedantic -std=c99 LDFLAGS = -lreadline @@ -18,11 +23,9 @@ depend: .depend include .depend -vm-dcpu16: vm-dcpu16.o dcpu16.o - -as-dcpu16: as-dcpu16.o +vm-dcpu16: vm-dcpu16.o dcpu16.o common.o -dcpu16: dcpu16.o +as-dcpu16: as-dcpu16.o common.o clean: @rm -rf $(PROGRAMS) *.o *.dSYM .depend diff --git a/as-dcpu16.c b/as-dcpu16.c index 0131ba4..4ed6050 100644 --- a/as-dcpu16.c +++ b/as-dcpu16.c @@ -7,6 +7,7 @@ #include #include "dcpu16.h" +#include "common.h" /* * quick and dirty assembler for dcpu16 @@ -14,6 +15,7 @@ * Justin Wind * 2012 04 07 - implementation started * 2012 04 10 - functional + * 2012 04 16 - support dat statements * * TODO * needs ability to specify location for code or data @@ -92,82 +94,9 @@ struct label_ { }; -/* routines to support generic grow-able arrays */ - -struct dynamic_array_ { - size_t entry_size; - size_t grow_size; - size_t allocated; - size_t entries; - void *a; -}; - -#define DYNARRAY_ITEM(da, index) ( (char *)(da).a + ( (da).entry_size * index ) ) - -/* allocate and initialize a new generic dynamic array */ -static -struct dynamic_array_ *dynarray_new_(size_t entry_size, size_t grow_size) { - struct dynamic_array_ *da; - - if (entry_size == 0 || grow_size == 0) { - fprintf(stderr, "%s: internal error: sizes cannot be zero\n", __func__); - exit(EX_SOFTWARE); - } - - da = calloc(1, sizeof *da); - if (da == NULL) { - fprintf(stderr, "%s():%s\n", "calloc", strerror(errno)); - return NULL; - } - - da->entry_size = entry_size; - da->grow_size = grow_size; - - da->a = malloc(da->entry_size * da->grow_size); - if (da->a == NULL) { - fprintf(stderr, "%s():%s\n", "malloc", strerror(errno)); - } - - da->allocated = grow_size; - - DEBUG_PRINTF("allocated new dynarray:%p a:%p entry_size:%zu\n", da, da->a, da->entry_size); - - return da; -} - -/* copy item onto end of array */ -static -void *dynarray_add_(struct dynamic_array_ *da, void *item) { - void *dst; - - /* make room, make room */ - if (da->entries == da->allocated) { - size_t new_allocated = da->allocated + da->grow_size; - void *tmp_ptr = realloc(da->a, new_allocated * da->entry_size); - if (tmp_ptr == NULL) { - fprintf(stderr, "%s():%s\n", "realloc", strerror(errno)); - return NULL; - } - da->a = tmp_ptr; - da->allocated = new_allocated; - - DEBUG_PRINTF("grew dynarray:%p\n", da); - } - - dst = DYNARRAY_ITEM(*da, da->entries); - memcpy(dst, item, da->entry_size); - - da->entries++; - - DEBUG_PRINTF("added dynarray:%p entry:%zu item:%p\n", da, da->entries, item); - - return dst; -} - - /* locate and return the label entry matching name */ static -struct label_ *label_find_(struct dynamic_array_ *labels, char *name) { +struct label_ *label_find_(struct dynamic_array *labels, char *name) { size_t x; for (x = 0; x < labels->entries; x++) { @@ -181,7 +110,7 @@ struct label_ *label_find_(struct dynamic_array_ *labels, char *name) { /* if a label has a validly-calculated address, fetch it */ static -int label_addr_(struct dynamic_array_ *labels, char *name, DCPU16_WORD *addr) { +int label_addr_(struct dynamic_array *labels, char *name, DCPU16_WORD *addr) { struct label_ *l; if ( (l = label_find_(labels, name)) == NULL ) @@ -195,7 +124,7 @@ int label_addr_(struct dynamic_array_ *labels, char *name, DCPU16_WORD *addr) { /* attempt to determine the addresses of all labels */ static -void label_addr_calculate_(struct dynamic_array_ *instructionps, struct dynamic_array_ *labels) { +void label_addr_calculate_(struct dynamic_array *instructionps, struct dynamic_array *labels) { size_t i; /* for each label.. */ @@ -220,6 +149,8 @@ void label_addr_calculate_(struct dynamic_array_ *instructionps, struct dynamic_ instr >= (struct instruction_ **)DYNARRAY_ITEM(*instructionps, 0); instr--) { + if ((*instr)->ready) + DEBUG_PRINTF("%s: instr not ready\n", __func__); word_count += (*instr)->length; /* have we come across an instruction which a label points to? @@ -245,21 +176,6 @@ void label_addr_calculate_(struct dynamic_array_ *instructionps, struct dynamic_ } } -static -void instr_free_(struct instruction_ *i) { - if (i->label) - free(i->label); - if (i->opcode) - free(i->opcode); - while (i->operands) { - struct operand_ *o = i->operands; - - i->operands = o->next; - free(o); - } - - free(i); -} /* generate the nibble for a given basic opcode */ static @@ -355,6 +271,7 @@ void buf_strip_chars_(char *buf, char *chars) { *d = *s; } + /* value_bits_ * generate the six bits for a given operand string * returns -1 if it could not parse the operand @@ -362,7 +279,7 @@ void buf_strip_chars_(char *buf, char *chars) { * notes: nextword may be overwritten even if it's not used in final instruction */ static -int value_bits_(struct dynamic_array_ *labels, char *operand_orig, DCPU16_WORD *nextword, unsigned int *nextwordused, unsigned int allow_short_labels) { +int value_bits_(struct dynamic_array *labels, const char *operand_orig, DCPU16_WORD *nextword, unsigned int *nextwordused, unsigned int allow_short_labels) { static char *operand = NULL; static size_t operand_sz = 0; @@ -597,114 +514,145 @@ int instruction_print_(struct instruction_ *i, unsigned int with_label) { return r; } -/* parse an instruction out of buf, create new instruction struct if seemingly valid */ -/* does not actually check if instruction is valid yet */ -/* buf must be 0-terminated */ +/* buf_tokenize_ + * Parses the zero-terminated line of input 'buf' into a newly-allocated struct instruction_. + * [label] opcode [operand[,operand[,...]]] + * Does not yet validate if labels, opcodes, or operands are valid... + */ static int buf_tokenize_(char *buf, struct instruction_ **next_instr) { const char const *sep = " \t\n"; + const char const *quot = "'`\""; struct instruction_ *instr = NULL; + struct operand_ *operand_list = NULL; char *label = NULL, *opcode = NULL, *operand = NULL; - char *x, *y, - *st; + *st, *qt; + size_t instr_words_needed = 1; assert(buf != NULL); assert(next_instr != NULL); *next_instr = NULL; - /* kill comments */ - if ((x = strchr(buf, ';')) != NULL) - *x = '\0'; /* kill leading whitespace */ buf += strspn(buf, " \t\n"); - /* kill trailing whitespace */ - if (*buf) { - x = buf + strlen(buf); - while (strchr(" \t\n", *x)) { - *x = '\0'; - x--; - } - } - if ((x = strrchr(buf, '\n')) != NULL) + /* kill trailing whitespace */ + for (x = buf + strlen(buf); *x && strchr(sep, *x); x--) *x = '\0'; - /* determine if first token is label, opcode, or we just have a blank line to ignore */ - x = strtok_r(buf, sep, &st); + /* split on first non-quoted ';', ignore following comment */ + x = strqtok_r(buf, ";", '\\', quot, &qt, &st); + if (x == NULL) + return 0; + if (qt) { + fprintf(stderr, "unmatched %c-quote\n", *qt); + return -1; + } - /* empty line? nothing to do here. */ + /* determine if first token is label, opcode, or we just have a blank line to ignore */ + x = strqtok_r(x, sep, '\\', quot, &qt, &st); if (x == NULL) return 0; + if (qt) { + fprintf(stderr, "unmatched %c-quote\n", *qt); + return -1; + } -#ifdef OTHER_LABELS +/* I want c-style labels in my asm, but example in spec uses : in prefix rather than postfix */ +#ifdef NON_SPEC_LABELS /* labels end with :, otherwise its an opcode */ y = x + strlen(x) - 1; if (*y == ':') { *y = '\0'; label = x; - opcode = strtok_r(NULL, sep, &st); + opcode = strqtok_r(NULL, sep, '\\', quot, &qt, &st); + } else { + label = NULL; + opcode = x; } -#else /* OTHER_LABELS */ +#else /* NON_SPEC_LABELS */ /* labels.. begin? with ':' ? okay, I guess. Whatever. */ /* otherwise, it's an opcode */ if (*x == ':') { label = x + 1; - opcode = strtok_r(NULL, sep, &st); + opcode = strqtok_r(NULL, sep, '\\', quot, &qt, &st); } else { label = NULL; opcode = x; } -#endif /* OTHER_LABELS */ +#endif /* NON_SPEC_LABELS */ if (opcode) { operand = st; } - /* extra room for assembled words */ - instr = calloc(1, 3 + sizeof *instr); - if (instr == NULL) { - fprintf(stderr, "%s():%s\n", "calloc", strerror(errno)); - return -1; - } - - instr->label = label ? strdup(label) : NULL; - instr->opcode = opcode ? strdup(opcode) : NULL; + /* + While normal instructions just have comma-separated operands, + DAT can be followed by comma-separated list of: + label, to be resolved to address + value, like 0xffff + string, "quoted", characters to be rendered into low-byte of words + */ if (operand) { - struct operand_ **o_next = &instr->operands; + struct operand_ **o_next = &operand_list; - for (x = strtok_r(operand, ",", &st); + for (x = strqtok_r(operand, ",", '\\', quot, &qt, &st); x; - x = strtok_r(NULL, ",", &st) ) { - *o_next = malloc(3 + sizeof **o_next); /* FIXME: handle this on the fly later */ + x = strqtok_r(NULL, ",", '\\', quot, &qt, &st) ) { + + /* trim leading whitespace */ + x += strspn(x, " \t\n"); + if (*x == '\0') { + fprintf(stderr, "encountered empty operand\n"); + return -1; + } + /* trim trailing whitespace */ + y = x + strlen(x) - 1; + while (strchr(" \t\n", *y)) { + *y = '\0'; + y--; + } + + /* new operand to append to list */ + *o_next = malloc(sizeof **o_next); if (*o_next == NULL) { fprintf(stderr, "%s():%s\n", "calloc", strerror(errno)); - instr_free_(instr); return -1; } - /* trim */ - x += strspn(x, " \t\n"); - if (*x) { - y = x + strlen(x) - 1; - while (strchr(" \t\n", *y)) { - *y = '\0'; - y--; - } - } + /* assume an operand uses one word, unless it's a string */ + instr_words_needed += (*x == '"') ? strlen(x) : 1; (*o_next)->operand = strdup(x); + if ((*o_next)->operand == NULL) { + fprintf(stderr, "%s():%s\n", "strdup", strerror(errno)); + return -1; + } (*o_next)->next = NULL; o_next = &((*o_next)->next); } } + DEBUG_PRINTF("allocating instr with room for %zu words\n", instr_words_needed); + + /* extra room for assembled words */ + instr = calloc(1, (instr_words_needed * sizeof *instr->instr_words) + sizeof *instr); + if (instr == NULL) { + fprintf(stderr, "%s():%s\n", "calloc", strerror(errno)); + return -1; + } + + instr->label = label ? strdup(label) : NULL; + instr->opcode = opcode ? strdup(opcode) : NULL; + instr->operands = operand_list; + *next_instr = instr; return 0; @@ -712,7 +660,7 @@ int buf_tokenize_(char *buf, struct instruction_ **next_instr) { /* try to generate bytecode for an instruction */ static -int instr_assemble_(struct dynamic_array_ *labels, struct instruction_ *i, unsigned int allow_short_labels) { +int instr_assemble_(struct dynamic_array *labels, struct instruction_ *i, unsigned int allow_short_labels) { unsigned int nwu = 0; /* number of words used */ unsigned int incomplete = 0; int bits; @@ -731,11 +679,57 @@ int instr_assemble_(struct dynamic_array_ *labels, struct instruction_ *i, unsig /* special case DAT */ if (strncasecmp(i->opcode, "DAT", 3) == 0) { - /* just dump operands into words, I guess */ - fprintf(stderr, "FIXME unhandled raw data\n"); - /* count total length of data.. */ - /* realloc instruction */ - /* populate words */ + DEBUG_PRINTF("processing DAT...\n"); + + i->length = 0; + + while (o) { + size_t j, dat_len; + char *x; + unsigned long l; + + DEBUG_PRINTF("DAT operand:'%s' next:%p\n", o->operand, o->next); + + /* is this a string? */ + if ( (x = strchr("\"'`", o->operand[0])) ) { + dat_len = strlen(o->operand) - 1; + if (o->operand[dat_len] == *x) { + /* it is a string */ + DEBUG_PRINTF("DAT string operand: %s\n", o->operand); + + for (j = 0, x = o->operand + 1; + j < dat_len - 1; + j++, x++) { + i->instr_words[i->length] = (DCPU16_WORD)*x; + i->length++; + } + } + o = o->next; + continue; + } + + char *ep; + errno = 0; + l = strtoul(o->operand, &ep, 0); + if (errno == 0 + && (*o->operand && (*ep == '\0')) ) { + /* conversion succeeded */ + if (l > 0xffff) { + fprintf(stderr, "value '%lu' out of range\n", l); + return -1; + } + } + + fprintf(stderr, "FIXME finish implementing DAT\n"); + /* check if it's a parsable number */ + + /* otherwise assume it's a label */ + + + + o = o->next; + } + return 0; } @@ -781,7 +775,7 @@ int instr_assemble_(struct dynamic_array_ *labels, struct instruction_ *i, unsig return -1; } - bits = value_bits_(labels, o->operand, i->instr_words + nwu + 1, &nwu, allow_short_labels); + bits = value_bits_(labels, o->operand, i->instr_words + 1 + nwu, &nwu, allow_short_labels); if (bits == -1) { fprintf(stderr, "couldn't assemble instruction\n"); return -1; @@ -823,7 +817,7 @@ int instr_assemble_(struct dynamic_array_ *labels, struct instruction_ *i, unsig * break each line into parts, populate parts into structures */ static -int parse_stream_(FILE *f, const char *src, struct dynamic_array_ *instructionps, struct dynamic_array_ *labels, unsigned int allow_short_labels) { +int parse_stream_(FILE *f, const char *src, struct dynamic_array *instructionps, struct dynamic_array *labels, unsigned int allow_short_labels) { struct instruction_ *instr, **instr_list_entry; unsigned int line = 0; int retval = 0; @@ -848,7 +842,7 @@ int parse_stream_(FILE *f, const char *src, struct dynamic_array_ *instructionps if (instr) { /* add to list of instructions */ - instr_list_entry = dynarray_add_(instructionps, &instr); + instr_list_entry = dynarray_add(instructionps, &instr); if (instr_list_entry == NULL) { fprintf(stderr, "%s:%u:%s", src, line, "could not populate instruction list\n"); break; @@ -866,7 +860,7 @@ int parse_stream_(FILE *f, const char *src, struct dynamic_array_ *instructionps break; } - if (dynarray_add_(labels, &new_label) == NULL) { + if (dynarray_add(labels, &new_label) == NULL) { fprintf(stderr, "%s:%u:%s", src, line, "could not populate label list\n"); break; } @@ -892,10 +886,21 @@ int parse_stream_(FILE *f, const char *src, struct dynamic_array_ *instructionps * make a full pass over instruction list to resolve labels */ static -int assemble_check_(struct dynamic_array_ *instructionps, struct dynamic_array_ *labels, unsigned int allow_short_labels) { +int assemble_check_(struct dynamic_array *instructionps, struct dynamic_array *labels, unsigned int allow_short_labels) { int retval = 0; size_t x; + /* fixing short labels .... */ + /* by here we have our list of instructions and their maximum instruction lengths */ + /* and we have a list of addresses, based on those maximum lengths */ + /* So, if doing short labels, all label addresses are now suspect, so recompute them all... */ + /* and reassemble.. */ + /* uh.. what else am I forgetting.. this method won't work for labels approaching the limit */ + /* of short form addresses, when there are more than the difference number of short form labels used previous to those addresses */ + + /* try this? keep another list of locations a label address is used */ + /* as we step forward, and recompute an address, back up to first occurence of address, make sure nothing else has changed */ + DEBUG_PRINTF(" final pass of assembler...\n"); for (x = 0; x < instructionps->entries; x++) { struct instruction_ **instrp = (struct instruction_ **)DYNARRAY_ITEM(*instructionps, x); @@ -929,7 +934,7 @@ int assemble_check_(struct dynamic_array_ *instructionps, struct dynamic_array_ } static -int output_(struct dynamic_array_ *instructionps, const char *filename) { +int output_(struct dynamic_array *instructionps, const char *filename) { FILE *of = NULL; struct instruction_ **instrp; size_t i, r, total_words = 0; @@ -974,8 +979,8 @@ int output_(struct dynamic_array_ *instructionps, const char *filename) { return 0; } -static struct dynamic_array_ *instructionps_; -static struct dynamic_array_ *labels_; +static struct dynamic_array *instructionps_; +static struct dynamic_array *labels_; int main(int argc, char *argv[]) { const char *out_filename = NULL; @@ -1021,8 +1026,8 @@ int main(int argc, char *argv[]) { out_filename = out_filename_default_; /* init tables */ - instructionps_ = dynarray_new_(sizeof (struct instruction_ *), 1024); - labels_ = dynarray_new_(sizeof(struct label_), 256); + instructionps_ = dynarray_new(sizeof (struct instruction_ *), 1024); + labels_ = dynarray_new(sizeof(struct label_), 256); if (instructionps_ == NULL || labels_ == NULL) { fprintf(stderr, "failed to initialize\n"); diff --git a/common.c b/common.c new file mode 100644 index 0000000..9dbadf3 --- /dev/null +++ b/common.c @@ -0,0 +1,202 @@ +/* common.c + * Utility functions shared between modules, but not exported. + */ + +#include +#include +#include +#include +#include + +#include "common.h" +#include "dcpu16.h" + +/* initialize a generic dynamic array struct */ +int dynarray_init(struct dynamic_array *da, size_t entry_size, size_t grow_size) { + assert(da); + assert(entry_size); + assert(grow_size); + + if (entry_size == 0 || grow_size == 0) { + fprintf(stderr, "%s: internal error: sizes cannot be zero\n", __func__); + errno = EINVAL; + return -1; + } + + da->allocated = 0; + da->entries = 0; + da->entry_size = entry_size; + da->grow_size = grow_size; + da->a = malloc(da->entry_size * da->grow_size); + if (da->a == NULL) { + fprintf(stderr, "%s():%s\n", "malloc", strerror(errno)); + return -1; + } + da->allocated = da->grow_size; + return 0; +} + +/* allocate and initialize a new generic dynamic array */ +struct dynamic_array *dynarray_new(size_t entry_size, size_t grow_size) { + struct dynamic_array *da; + + assert(entry_size); + assert(grow_size); + + da = calloc(1, sizeof *da); + if (da == NULL) { + fprintf(stderr, "%s():%s\n", "calloc", strerror(errno)); + return NULL; + } + + if (dynarray_init(da, entry_size, grow_size)) { + fprintf(stderr, "%s():%s\n", "dynarray_init", strerror(errno)); + free(da); + return NULL; + } + + return da; +} + +/* copy item onto end of array */ +void *dynarray_add(struct dynamic_array *da, void *item) { + void *dst; + + assert(da); + assert(item); + + /* make room, make room */ + if (da->entries == da->allocated) { + size_t new_allocated = da->allocated + da->grow_size; + void *tmp_ptr = realloc(da->a, new_allocated * da->entry_size); + if (tmp_ptr == NULL) { + fprintf(stderr, "%s():%s\n", "realloc", strerror(errno)); + return NULL; + } + da->a = tmp_ptr; + da->allocated = new_allocated; + + } + + dst = DYNARRAY_ITEM(*da, da->entries); + memcpy(dst, item, da->entry_size); + + da->entries++; + + return dst; +} + +/* simplified strtoul with range checking */ +int str_to_word(char *s) { + unsigned long l; + char *ep; + + assert(s); + + errno = 0; + l = strtoul(s, &ep, 0); + + if (errno + || !(*s && *ep == '\0') ) { + /* out of range of conversion, or invalid character encountered */ + return -1; + } + + if (l >= DCPU16_RAM) { + /* out of range for our needs */ + errno = ERANGE; + return -1; + } + + return l; +} + +/* just like strtok_r, but ignores separators within quotes */ +char *strqtok_r(char *str, const char *sep, int esc, const char *quote, char **lastq, char **lasts) { + int escaped = 0; + int retry; + char *tok, + *lastq_ret = NULL, + *src, + *dst; + + if (str) { + *lasts = str; + *lastq = NULL; + } + + /* next token starts after any leading seps */ + *lasts += strspn(*lasts, sep); + tok = *lasts; + if (*tok == '\0') + return NULL; + + do { + retry = 0; + while (**lasts) { + /* the previous character was the escape, do not consider this character any further */ + if (escaped) { + escaped = 0; + (*lasts)++; + continue; + } + + /* this character is the escape, do not consider the next character */ + if (**lasts == esc) { + escaped = 1; + (*lasts)++; + continue; + } + + /* we have a quote open, only consider matching quote to close */ + if (*lastq) { + if (**lasts == **lastq) + *lastq = NULL; + (*lasts)++; + continue; + } + + /* this character is an opening quote, remember what it is */ + if (strchr(quote, **lasts)) { + *lastq = *lasts; + (*lasts)++; + continue; + } + + /* this character is a separator, separate and be done */ + if (strchr(sep, **lasts)) { + **lasts = '\0'; + (*lasts)++; + break; + } + (*lasts)++; + } + + /* were we left with an unmatched quote? + remember where we had trouble + try everything following lonely quote again, but pretend quote is there */ + if (*lastq) { + lastq_ret = *lastq; + *lasts = *lastq + 1; + *lastq = NULL; + retry = 1; + } + } while (retry); + + /* now strip escape characters */ + for (src = dst = tok; *src; src++, dst++) { + if (*src == esc) { + src++; + if (*src == '\0') + break; + } + *dst = *src; + } + *dst = *src; + + /* remember where we had trouble */ + if (lastq_ret) + *lastq = lastq_ret; + + return tok; +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..80ed333 --- /dev/null +++ b/common.h @@ -0,0 +1,24 @@ +#ifndef COMMON_H_QPRCB1BH +#define COMMON_H_QPRCB1BH + +#include + +struct dynamic_array { + size_t entry_size; + size_t grow_size; + size_t allocated; + size_t entries; + void *a; +}; + +#define DYNARRAY_ITEM(da, index) ( (char *)(da).a + ( (da).entry_size * index ) ) + +int dynarray_init(struct dynamic_array *da, size_t entry_size, size_t grow_size); +struct dynamic_array *dynarray_new(size_t entry_size, size_t grow_size); +void *dynarray_add(struct dynamic_array *da, void *item); + +int str_to_word(char *s); + +char *strqtok_r(char *str, const char *sep, int esc, const char *quote, char **lastq, char **lasts); + +#endif /* COMMON_H_QPRCB1BH */ diff --git a/vm-dcpu16.c b/vm-dcpu16.c index b59c253..d60c340 100644 --- a/vm-dcpu16.c +++ b/vm-dcpu16.c @@ -9,6 +9,7 @@ #include #include "dcpu16.h" +#include "common.h" /* * shell-like driver for dcpu16 core @@ -68,40 +69,15 @@ void usage_(char *prog, unsigned int full) { } } -/* simplified strtoul with range checking */ -static -int str_to_word_(char *s) { - unsigned long l; - char *ep; - - assert(s); - - errno = 0; - l = strtoul(s, &ep, 0); - - if (errno - || !(*s && *ep == '\0') ) { - /* out of range of conversion, or invalid character encountered */ - return -1; - } - - if (l >= DCPU16_RAM) { - /* out of range for our needs */ - errno = ERANGE; - return -1; - } - - return l; -} /* flense a buffer into a newly-allocated argument list */ -/* FIXME: handle quotes */ static int buf_tok_vect_(char ***v, int *c, char *buf) { const char *sep = " \t"; + const char *quot = "\"'`"; const size_t v_grow = 32; size_t v_sz = 32; - char *st; + char *st, *qt; *c = 0; *v = malloc(v_sz * sizeof **v); @@ -110,9 +86,9 @@ int buf_tok_vect_(char ***v, int *c, char *buf) { return -1; } - for ( (*v)[*c] = strtok_r(buf, sep, &st); + for ( (*v)[*c] = strqtok_r(buf, sep, '\\', quot, &qt, &st); (*v)[*c]; - (*v)[*c] = strtok_r(NULL, sep, &st) + (*v)[*c] = strqtok_r(NULL, sep, '\\', quot, &qt, &st) ) { (*c)++; @@ -220,7 +196,7 @@ COMMAND_IMPL(load) { int addr = 0; if (arg_count > 2) { - addr = str_to_word_(arg_vector[2]); + addr = str_to_word(arg_vector[2]); if (addr < 0) { fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[2], strerror(errno)); return 0; @@ -250,7 +226,7 @@ COMMAND_IMPL(dump) { int i; for (i = 1; i < arg_count; i++) { - addr[i-1] = str_to_word_(arg_vector[i]); + addr[i-1] = str_to_word(arg_vector[i]); if (addr[i-1] < 0) { fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[i], strerror(errno)); return 0; @@ -281,7 +257,7 @@ COMMAND_IMPL(disassemble) { int i; for (i = 1; i < arg_count; i++) { - addr[i-1] = str_to_word_(arg_vector[i]); + addr[i-1] = str_to_word(arg_vector[i]); if (addr[i-1] < 0) { fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[i], strerror(errno)); return 0; -- 2.45.2