ulas_preprocfree(&ulas.pp);
}
+char *ulas_strdup(const char *s) {
+ int len = strlen(s);
+ return ulas_strndup(s, len);
+}
+
+char *ulas_strndup(const char *s, unsigned int max) {
+ int len = ulas_strnlen(s, max);
+ char *dup = malloc(len+1);
+ memcpy(dup, s, len+1);
+ return dup;
+}
+
+unsigned int ulas_strnlen(const char *s, unsigned int max) {
+ unsigned int i = 0;
+ while (i < max && s[i]) {
+ i++;
+ }
+ return i;
+}
+
+unsigned int ulas_strlcat(char *dst, const char *src, unsigned int max) {
+ unsigned int dst_len = ulas_strnlen(dst, max);
+ unsigned int src_len = ulas_strnlen(src, max);
+
+ for (int i = 0; i < src_len; i++) {
+ dst[dst_len+i] = src[i];
+ }
+
+ dst[src_len + dst_len] = '\0';
+
+ return src_len + dst_len;
+}
+
FILE *ulas_incpathfopen(const char *path, const char *mode) {
char pathbuf[ULAS_PATHMAX];
memset(pathbuf, 0, ULAS_PATHMAX);
continue;
}
- strlcat(pathbuf, ip, ULAS_PATHMAX);
+ ulas_strlcat(pathbuf, ip, ULAS_PATHMAX);
if (ip[len - 1] != ULAS_PATHSEP[0]) {
- strlcat(pathbuf, ULAS_PATHSEP, ULAS_PATHMAX);
+ ulas_strlcat(pathbuf, ULAS_PATHSEP, ULAS_PATHMAX);
}
- strlcat(pathbuf, path, ULAS_PATHMAX);
+ ulas_strlcat(pathbuf, path, ULAS_PATHMAX);
FILE *f = fopen(pathbuf, mode);
if (f != NULL) {
if (!existing || (name[0] == '\0' && len == 1)) {
// def new symbol
- struct ulas_sym new_sym = {strndup(name, len), tok, scope, ulas.pass,
+ struct ulas_sym new_sym = {ulas_strndup(name, len), tok, scope, ulas.pass,
constant};
ulas_symbufpush(&ulas.syms, new_sym);
// literal token
// we resolve it later, will need to malloc here for now
tok.type = ULAS_SYMBOL;
- tok.val.strv = strndup(buf - 1, n);
+ tok.val.strv = ulas_strndup(buf - 1, n);
buf += n - 1;
} else {
ULASERR("Unexpected token: %s\n", buf);
}
struct ulas_str ulas_strreq(struct ulas_str *s, unsigned long n) {
- return ulas_strensr(s, strnlen(s->buf, s->maxlen) + n);
+ return ulas_strensr(s, ulas_strnlen(s->buf, s->maxlen) + n);
}
void ulas_strfree(struct ulas_str *s) {
}
void ulas_trimend(char c, char *buf, unsigned long n) {
- unsigned long buflen = strnlen(buf, n);
+ unsigned long buflen = ulas_strnlen(buf, n);
if (buflen == 0) {
return;
}
// to ensure expansion are separated if we are in a recursive call
// this fixes new lines in recursive macros being consumed
if (recursive) {
- int len = strnlen(pp->line.buf, pp->line.maxlen) + 1;
+ int len = ulas_strnlen(pp->line.buf, pp->line.maxlen) + 1;
ulas_strensr(&pp->line,
len + 1);
strncat(pp->line.buf, "\n", len);
const char *name = macro_argname[mi];
if (pp->macroparam[mi].buf[0] &&
strncmp((name), pp->macrobuf.buf, pp->macrobuf.maxlen) == 0) {
- ulas_strensr(&pp->line, strnlen(pp->line.buf, pp->line.maxlen) +
- strnlen(pp->macroparam[mi].buf,
+ ulas_strensr(&pp->line, ulas_strnlen(pp->line.buf, pp->line.maxlen) +
+ ulas_strnlen(pp->macroparam[mi].buf,
pp->macroparam[mi].maxlen) +
1);
if (linelen &&
strncmp("$0", pp->macrobuf.buf, pp->macrobuf.maxlen) == 0) {
ulas_strensr(&pp->line,
- strnlen(pp->line.buf, pp->line.maxlen) + linelen + 1);
+ ulas_strnlen(pp->line.buf, pp->line.maxlen) + linelen + 1);
if (linelen > 1) {
// this skips the separating token which is usually a space
pp->macrobuf.maxlen) == 0) {
sprintf(numbuf, "%x", ulas_icntr());
ulas_strensr(&pp->line,
- strnlen(pp->line.buf, pp->line.maxlen) + 128 + 1);
+ ulas_strnlen(pp->line.buf, pp->line.maxlen) + 128 + 1);
tocat = numbuf;
tocatlen = 128;
}
} else {
// make sure to include leading white space
int wsi = ulas_preproclws(pp, val - valread, vallen);
- ulas_strensr(&pp->line, strnlen(pp->line.buf, pp->line.maxlen) +
+ ulas_strensr(&pp->line, ulas_strnlen(pp->line.buf, pp->line.maxlen) +
tocatlen + wsi + 1);
strncat(pp->line.buf, tocat, tocatlen);
}
return -1;
}
- struct ulas_ppdef def = {ULAS_PPDEF, strdup(pp->tok.buf), strdup(pline),
+ struct ulas_ppdef def = {ULAS_PPDEF, ulas_strdup(pp->tok.buf),
+ ulas_strdup(pline),
0};
ulas_preprocdef(pp, def);
// define short-circuits the rest of the logic
ULASERR("'%s' is not a valid #macro name!\n", pp->tok.buf);
return -1;
}
- char *name = strdup(pp->tok.buf);
+ char *name = ulas_strdup(pp->tok.buf);
struct ulas_str val = ulas_str(32);
memset(val.buf, 0, 32);
break;
}
- unsigned long len = strnlen(pp->line.buf, pp->line.maxlen);
- ulas_strensr(&val, strnlen(val.buf, val.maxlen) + len + 1);
+ unsigned long len = ulas_strnlen(pp->line.buf, pp->line.maxlen);
+ ulas_strensr(&val, ulas_strnlen(val.buf, val.maxlen) + len + 1);
strncat(val.buf, pp->line.buf, val.maxlen);
}
char *prev_path = ulas.filename;
unsigned long prev_lines = ulas.line;
- ulas.filename = strdup(path);
+ ulas.filename = ulas_strdup(path);
ulas.line = 0;
FILE *tmp = tmpfile();
int ulas_last_print_is_escape(char *buf, int n) {
int found_escape = 0;
// check if last printable char is an escape char
- unsigned int buf_len = strnlen(buf, n);
+ unsigned int buf_len = ulas_strnlen(buf, n);
if (buf_len == 0) {
return 0;
}
// set up initial defs
for (int i = 0; i < ulascfg.defslen; i++) {
- struct ulas_ppdef def = {ULAS_PPDEF, strdup(ulascfg.defs[i]), strdup(""),
+ struct ulas_ppdef def = {ULAS_PPDEF, ulas_strdup(ulascfg.defs[i]),
+ ulas_strdup(""),
0};
ulas_preprocdef(&pp, def);
}
*/
int ulas_istokend(struct ulas_str *tok) {
- long len = (int)strnlen(tok->buf, tok->maxlen);
+ long len = (int)ulas_strnlen(tok->buf, tok->maxlen);
// skip comments though, they are not trailing tokens!
if (len > 0 && tok->buf[0] != ULAS_TOK_COMMENT) {
return 0;
}
// empty tokens are going to be ignored
- if (strnlen(ulas.tok.buf, ulas.tok.maxlen) == 0) {
+ if (ulas_strnlen(ulas.tok.buf, ulas.tok.maxlen) == 0) {
continue;
}
// interpret the token
struct ulas_tok tok = ulas_totok(
- ulas.tok.buf, strnlen(ulas.tok.buf, ulas.tok.maxlen), &tokrc);
+ ulas.tok.buf, ulas_strnlen(ulas.tok.buf, ulas.tok.maxlen), &tokrc);
if (tokrc == -1) {
goto end;
}
written++;
if (ulas_tok(&ulas.tok, line, n) > 0) {
- t = ulas_totok(ulas.tok.buf, strnlen(ulas.tok.buf, ulas.tok.maxlen), rc);
+ t = ulas_totok(ulas.tok.buf, ulas_strnlen(ulas.tok.buf, ulas.tok.maxlen), rc);
} else {
break;
}
ulas_tok(&ulas.tok, line, n);
struct ulas_tok t =
- ulas_totok(ulas.tok.buf, strnlen(ulas.tok.buf, ulas.tok.maxlen), rc);
+ ulas_totok(ulas.tok.buf, ulas_strnlen(ulas.tok.buf, ulas.tok.maxlen), rc);
if (*rc == -1 || t.type != ',') {
ULASERR("Expected ,\n");
written += len;
if (ulas_tok(&ulas.tok, line, n) > 0) {
- t = ulas_totok(ulas.tok.buf, strnlen(ulas.tok.buf, ulas.tok.maxlen), rc);
+ t = ulas_totok(ulas.tok.buf, ulas_strnlen(ulas.tok.buf, ulas.tok.maxlen), rc);
} else {
break;
}
}
int ulas_asmdiradv(FILE *dst, const char **line, unsigned long n, int *rc) {
- ULAS_EVALEXPRS(ulas.address += ulas_intexpr(line, strnlen(*line, n), rc));
+ ULAS_EVALEXPRS(ulas.address += ulas_intexpr(line, ulas_strnlen(*line, n), rc));
return 0;
}
int ulas_asmdirsetenum(FILE *dst, const char **line, unsigned long n, int *rc) {
- ULAS_EVALEXPRS(ulas.enumv = ulas_intexpr(line, strnlen(*line, n), rc));
+ ULAS_EVALEXPRS(ulas.enumv = ulas_intexpr(line, ulas_strnlen(*line, n), rc));
return 0;
}
ulas_tok(&ulas.tok, line, n);
struct ulas_tok t =
- ulas_totok(ulas.tok.buf, strnlen(ulas.tok.buf, ulas.tok.maxlen), &rc);
+ ulas_totok(ulas.tok.buf, ulas_strnlen(ulas.tok.buf, ulas.tok.maxlen), &rc);
if (rc == -1 || t.type != '=') {
ULASERR("Expected =\n");
ULAS_EVALEXPRS(repval = ulas_intexpr(line, n, &rc));
ulas_tok(&ulas.tok, line, n);
struct ulas_tok t =
- ulas_totok(ulas.tok.buf, strnlen(ulas.tok.buf, ulas.tok.maxlen), &rc);
+ ulas_totok(ulas.tok.buf, ulas_strnlen(ulas.tok.buf, ulas.tok.maxlen), &rc);
if (rc == -1 || t.type != ',') {
ULASERR("Expected ,\n");
return 0;
int step = 0;
ULAS_EVALEXPRS(step = ulas_intexpr(line, n, &rc));
ulas_tok(&ulas.tok, line, n);
- t = ulas_totok(ulas.tok.buf, strnlen(ulas.tok.buf, ulas.tok.maxlen), &rc);
+ t = ulas_totok(ulas.tok.buf, ulas_strnlen(ulas.tok.buf, ulas.tok.maxlen), &rc);
if (rc == -1 || t.type != ',') {
ULASERR("Expected ,\n");
return 0;
int ulas_asmdirbank(FILE *dst, FILE *src, const char **line, unsigned long n,
int *rc) {
- ULAS_EVALEXPRS(ulas.bank = ulas_intexpr(line, strnlen(*line, n), rc));
+ ULAS_EVALEXPRS(ulas.bank = ulas_intexpr(line, ulas_strnlen(*line, n), rc));
return *rc;
}
switch (dir) {
case ULAS_ASMDIR_ORG: {
ULAS_EVALEXPRS(ulas.address =
- ulas_intexpr(&line, strnlen(start, n), &rc));
+ ulas_intexpr(&line, ulas_strnlen(start, n), &rc));
break;
}
case ULAS_ASMDIR_DEF: