commit 3bad86cddca7c1d74b935a4ffda164a24bda4a7a
parent 7664fa6cfcf646b2df07649c2c62ae38fb88a750
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 11 Sep 2017 08:12:36 +0100
[as] Add line and file information in error messages
Diffstat:
3 files changed, 28 insertions(+), 16 deletions(-)
diff --git a/as/as.h b/as/as.h
@@ -96,3 +96,5 @@ extern Op optab[];
extern int pass;
extern TUINT maxaddr;
extern int endian;
+extern Symbol *linesym;
+extern char *filename;
diff --git a/as/main.c b/as/main.c
@@ -1,5 +1,4 @@
-#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -7,21 +6,6 @@
#include "../inc/scc.h"
#include "as.h"
-int nerrors;
-
-void
-error(char *msg, ...)
-{
- va_list va;
-
- va_start(va, msg);
- fputs("as: ", stderr);
- vfprintf(stderr, msg, va);
- putc('\n', stderr);
- nerrors++;
- va_end(va);
-}
-
int
cmp(const void *f1, const void *f2)
{
@@ -92,6 +76,7 @@ dopass(char *fname)
{
struct line line;
FILE *fp;
+ extern int nerrors;
if ((fp = fopen(fname, "r")) == NULL)
die("as: error opening '%s'", fname);
@@ -118,6 +103,7 @@ main(int argc, char *argv[])
if (argc != 2)
usage();
+ filename = argv[1];
for (pass = 1; pass <= 2 && dopass(argv[1]); pass++)
/* nothing */;
writeout("a.out");
diff --git a/as/parser.c b/as/parser.c
@@ -1,4 +1,5 @@
#include <ctype.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -9,6 +10,26 @@
#define NARGS 20
#define MAXLINE 100
+char *filename;
+int nerrors;
+
+static unsigned lineno;
+
+void
+error(char *msg, ...)
+{
+ va_list va;
+
+ va_start(va, msg);
+ fprintf(stderr, "as:%s:%u: ", filename, lineno);
+ vfprintf(stderr, msg, va);
+ putc('\n', stderr);
+ nerrors++;
+ va_end(va);
+
+ if (nerrors == 10)
+ die("as: too many errors");
+}
Arg
number(char *s, int base)
@@ -135,6 +156,9 @@ repeat:
if (!fgets(buff, sizeof(buff), fp))
return 0;
+ if (++lineno == 0)
+ die("as: file too long");
+
n = strlen(buff);
if (n == 0)
goto repeat;