shithub: scc

Download patch

ref: c1a3310fbbad48582811bb35438f194bbab27c3e
parent: 1e3dd27b9b1dcd2c8f16a0f51effaade0e12c227
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Sep 11 04:12:36 EDT 2017

[as] Add line and file information in error messages

--- a/as/as.h
+++ b/as/as.h
@@ -96,3 +96,5 @@
 extern int pass;
 extern TUINT maxaddr;
 extern int endian;
+extern Symbol *linesym;
+extern char *filename;
--- 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 @@
 {
 	struct line line;
 	FILE *fp;
+	extern int nerrors;
 
 	if ((fp = fopen(fname, "r")) == NULL)
 		die("as: error opening '%s'", fname);
@@ -118,6 +103,7 @@
 	if (argc != 2)
 		usage();
 
+	filename = argv[1];
 	for (pass = 1; pass <= 2 && dopass(argv[1]); pass++)
 		/* nothing */;
 	writeout("a.out");
--- 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,7 +10,27 @@
 #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)
 {
@@ -134,6 +155,9 @@
 repeat:
 	if (!fgets(buff, sizeof(buff), fp))
 		return 0;
+
+	if (++lineno == 0)
+		die("as: file too long");
 
 	n = strlen(buff);
 	if (n == 0)