aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/cc/cc.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-05-17 18:28:05 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-05-17 18:28:05 -0700
commit23ac1f4f98accc3bb84e81be264d8408be372028 (patch)
treed3876d6d5ce73c9f0669ec3f0c04c3a7524ec91e /sys/cmd/cc/cc.c
parent9ec5bed6a7d715ffa69851569485a685dd69db2e (diff)
fix: bugs associated to map reallocating
Diffstat (limited to 'sys/cmd/cc/cc.c')
-rw-r--r--sys/cmd/cc/cc.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/sys/cmd/cc/cc.c b/sys/cmd/cc/cc.c
index 39ad5f2..3dae0fd 100644
--- a/sys/cmd/cc/cc.c
+++ b/sys/cmd/cc/cc.c
@@ -12,7 +12,7 @@ hash_string(byte* s)
int32 h;
h = 0;
- if (h != 0) {
+ if (s != nil) {
for (; *s; ++s) {
h += *s;
h = (h << 10);
@@ -58,7 +58,7 @@ static
int
morestrtab(StrTab *tab, int n)
{
- MAP_GROW(tab, string, int32, n, HASH, mem·sys.alloc, mem·sys.free, nil);
+ MAP_GROW(tab, string, int32, n, HASH, ·calloc, ·free, nil);
}
static
@@ -106,7 +106,7 @@ openio(byte *path)
// See if we have already opened file;
// If so, and it hasn't been flagged return it
- for (it = C.iostk; it != C.io + 1; ++it) {
+ for (it = C.iostk; it != C.io; ++it) {
if ((uintptr)it->path == (uintptr)path) {
if (it->kind & IOonce) {
return nil;
@@ -119,6 +119,9 @@ openio(byte *path)
panicf("out of I/O space!");
C.io->f = io·open(path, "r");
+ if (!C.io->f) {
+ panicf("file %s not found", path);
+ }
C.io->path = path;
bufio·initreader(&C.io->buf, asrdr(io·read), C.io->f);
@@ -176,19 +179,19 @@ byte *directives[NUM_DIRECTIVES] = { DIRECTIVES };
struct Compiler C = { 0 };
// -----------------------------------------------------------------------
-// flag handlers
+// cli flag handlers
void
pushinclude(byte *dirs)
{
string d, s, *it, *end;
- while (*dirs != 0) {
+ while (*dirs != '\0') {
d = strchr(dirs, ' ');
if (d != nil)
*d = '\0';
- s = d;
+ s = dirs;
intern(&s);
for (it = C.inc.dir, end = it + C.inc.len; it != end; ++it) {
if ((uintptr)s == (uintptr)(*it))
@@ -198,15 +201,13 @@ pushinclude(byte *dirs)
if (C.inc.len == C.inc.cap) {
C.inc.cap += 20;
C.inc.dir = realloc(C.inc.dir, C.inc.cap*sizeof(*C.inc.dir));
- C.inc.dir[C.inc.len++] = s;
}
-
+ C.inc.dir[C.inc.len++] = s;
Nextdir:
if (d == nil)
break;
dirs = d + 1;
}
-
}
// -----------------------------------------------------------------------
@@ -233,6 +234,9 @@ init(void)
C.inc.dir[C.inc.len++] = ".";
C.outfile = nil;
+ C.io = C.iostk;
+ memset(C.iostk, 0, sizeof(C.iostk));
+
C.lxr = (Lexer){ 0 };
}
@@ -241,14 +245,14 @@ compile(byte *path)
{
Io *io;
Token tok;
- byte *p, file[400];
+ byte *p, out[400];
- strcpy(file, path);
- p = utf8·findrrune(file, '/');
+ strcpy(out, path);
+ p = utf8·findrrune(out, '/');
if (p)
*p++ = '\0';
else
- p = file;
+ p = out;
if (!C.outfile) {
C.outfile = p;
@@ -263,9 +267,9 @@ compile(byte *path)
}
}
- C.lxr.io = openio(file);
+ C.lxr.io = openio(path);
while (tok = lex(&C.lxr), tok.kind > Aeof) {
- ;
+ puttok(tok);
}
freeio(C.lxr.io);
@@ -305,6 +309,10 @@ main(int argc, byte *argv[])
exit(1);
}
+ // NOTE: This is just for my comfort during debugging.
+ pushinclude("/home/nolln/root/include");
+ pushinclude("/home/nolln/root/include/vendor/libc");
+
src = (argc == 0) ? "<stdin>" : argv[0];
intern(&src);