From 8392280f83ae9ccfb76baec71f83e23db549d285 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Tue, 24 May 2011 12:01:25 +0430 Subject: [PATCH] more verbose error messages for missing files --- cpp.c | 17 ++++++++++++----- ncc.c | 9 +++------ npp.c | 4 ++-- tok.h | 4 ++-- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cpp.c b/cpp.c index 36e0950..e43f050 100644 --- a/cpp.c +++ b/cpp.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -52,8 +53,13 @@ static struct buf { } bufs[MAXBUFS]; static int nbufs; -void die(char *msg) +void die(char *fmt, ...) { + va_list ap; + char msg[512]; + va_start(ap, fmt); + vsprintf(msg, fmt, ap); + va_end(ap); write(2, msg, strlen(msg)); exit(1); } @@ -427,7 +433,7 @@ static int cpp_cmd(void) file[e - s] = '\0'; cur += e - s + 2; if (include_find(file, *e == '>') == -1) - die("cannot include file\n"); + die("cannot include <%s>\n", file); return 0; } return 1; @@ -880,8 +886,9 @@ static int buf_loc(char *s, int off) return n; } -int cpp_loc(char *s, long addr) +char *cpp_loc(long addr) { + static char loc[256]; int line = -1; int i; for (i = nbufs - 1; i > 0; i--) @@ -891,6 +898,6 @@ int cpp_loc(char *s, long addr) line = buf_loc(buf, (cur - hunk_len) + (addr - hunk_off)); else line = buf_loc(bufs[i].buf, bufs[i].cur); - sprintf(s, "%s:%d: ", bufs[i].path, line); - return strlen(s); + sprintf(loc, "%s:%d", bufs[i].path, line); + return loc; } diff --git a/ncc.c b/ncc.c index eb4cd18..ec83307 100644 --- a/ncc.c +++ b/ncc.c @@ -101,10 +101,7 @@ static void ts_pop(struct type *type) void err(char *msg) { - char err[1 << 7]; - int len = cpp_loc(err, tok_addr()); - strcpy(err + len, msg); - die(err); + die("%s: %s", cpp_loc(tok_addr()), msg); } struct name { @@ -823,7 +820,7 @@ static void readpre(void) readpre(); ts_pop(&type); if (!type.addr) - die("cannot use the address\n"); + err("cannot use the address\n"); type.ptr++; type.addr = 0; ts_push(&type); @@ -1896,7 +1893,7 @@ int main(int argc, char *argv[]) if (i == argc) die("neatcc: no file given\n"); if (cpp_init(argv[i])) - die("neatcc: cannot open input file\n"); + die("neatcc: cannot open <%s>\n", argv[i]); parse(); if (!*obj) { strcpy(obj, argv[i]); diff --git a/npp.c b/npp.c index 168c220..ce5d23e 100644 --- a/npp.c +++ b/npp.c @@ -91,10 +91,10 @@ int main(int argc, char *argv[]) return 0; } if (cpp_init(argv[i++])) - die("npp: cannot open input file\n"); + die("npp: cannot open <%s>\n", argv[i - 1]); ofd = open(argv[i++], O_WRONLY | O_TRUNC | O_CREAT, 0600); if (ofd < 0) - die("npp: cannot open output file\n"); + die("npp: cannot open <%s>\n", argv[i - 1]); s1 = malloc(OBUFSZ); s2 = malloc(OBUFSZ); if (!s1 || !s2) diff --git a/tok.h b/tok.h index 1a56a45..faac4af 100644 --- a/tok.h +++ b/tok.h @@ -50,7 +50,7 @@ void tok_jump(long addr); int cpp_init(char *path); void cpp_addpath(char *s); void cpp_define(char *name, char *def); -int cpp_loc(char *s, long offset); +char *cpp_loc(long addr); int cpp_read(char *s); -void die(char *msg); +void die(char *msg, ...); -- 2.11.4.GIT