changeset 142:26ee741196d1

Allow complain(NULL, format, ...)
author David A. Holland
date Sat, 13 Jul 2013 12:38:54 -0400
parents 63414cddf09c
children ed45f2d8d3bc
files main.c place.c utils.h
diffstat 3 files changed, 24 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Wed Jul 10 13:48:07 2013 -0400
+++ b/main.c	Sat Jul 13 12:38:54 2013 -0400
@@ -935,14 +935,10 @@
 
 DEAD static
 void
-usage(const char *argv0, const char *fmt, ...)
+usage(const char *progname, const char *fmt, ...)
 {
-	const char *progname;
 	va_list ap;
 
-	progname = strrchr(argv0, '/');
-	progname = progname == NULL ? argv0 : progname + 1;
-
 	fprintf(stderr, "%s: ", progname);
 	va_start(ap, fmt);
 	vfprintf(stderr, fmt, ap);
@@ -1030,11 +1026,16 @@
 int
 main(int argc, char *argv[])
 {
+	const char *progname;
 	const char *inputfile = NULL;
 	const char *outputfile = NULL;
 	struct place cmdplace;
 	int i;
 
+	progname = strrchr(argv[0], '/');
+	progname = progname == NULL ? argv[0] : progname + 1;
+	complain_init(progname);
+
 	init();
 
 	for (i=1; i<argc; i++) {
@@ -1059,7 +1060,7 @@
 			i++;
 			continue;
 		}
-		usage(argv[0], "Invalid option %s", argv[i]);
+		usage(progname, "Invalid option %s", argv[i]);
 	}
 	if (i < argc) {
 		inputfile = argv[i++];
@@ -1068,7 +1069,7 @@
 		outputfile = argv[i++];
 	}
 	if (i < argc) {
-		usage(argv[0], "Extra non-option argument %s", argv[i]);
+		usage(progname, "Extra non-option argument %s", argv[i]);
 	}
 
 	mode.output_file = outputfile;
--- a/place.c	Wed Jul 10 13:48:07 2013 -0400
+++ b/place.c	Sat Jul 13 12:38:54 2013 -0400
@@ -50,6 +50,8 @@
 static struct placefilearray placefiles;
 static bool overall_failure;
 
+static const char *myprogname;
+
 ////////////////////////////////////////////////////////////
 // seenfiles
 
@@ -187,12 +189,23 @@
 // complaints
 
 void
+complain_init(const char *pn)
+{
+	myprogname = pn;
+}
+
+void
 complain(const struct place *p, const char *fmt, ...)
 {
 	va_list ap;
 
-	place_printfrom(p);
-	fprintf(stderr, "%s:%u:%u: ", place_getname(p), p->line, p->column);
+	if (p != NULL) {
+		place_printfrom(p);
+		fprintf(stderr, "%s:%u:%u: ", place_getname(p),
+			p->line, p->column);
+	} else {
+		fprintf(stderr, "%s: ", myprogname);
+	}
 	va_start(ap, fmt);
 	vfprintf(stderr, fmt, ap);
 	va_end(ap);
--- a/utils.h	Wed Jul 10 13:48:07 2013 -0400
+++ b/utils.h	Sat Jul 13 12:38:54 2013 -0400
@@ -62,6 +62,7 @@
 bool is_identifier(const char *str);
 
 /* in place.c */
+void complain_init(const char *progname);
 void complain(const struct place *, const char *fmt, ...) PF(2, 3);
 void complain_fail(void);
 bool complain_failed(void);