source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Finally use __progname, err(3) and warn(3).
@ 2015-10-11 21:13 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2015-10-11 21:13 UTC (permalink / raw)
  To: source

Log Message:
-----------
Finally use __progname, err(3) and warn(3).
That's more readable and less error-prone than fumbling around
with argv[0], fprintf(3), strerror(3), perror(3), and exit(3).

It's a bad idea to boycott good interfaces merely because standards
committees ignore them.  Instead, let's provide compatibility modules 
for archaic systems (like commercial Solaris) that still don't have
them.  The compat module has an UCB Copyright (c) 1993...

Modified Files:
--------------
    mdocml:
        LICENSE
        Makefile
        Makefile.depend
        configure
        main.c
        mandoc_aux.c
        mandocdb.c
        manpath.c
        mansearch.c
        read.c
        term_ps.c

Added Files:
-----------
    mdocml:
        compat_err.c
        test-err.c
        test-progname.c

Revision Data
-------------
--- /dev/null
+++ test-progname.c
@@ -0,0 +1,9 @@
+#include <string.h>
+
+extern char *__progname;
+
+int
+main(void)
+{
+	return !!strcmp(__progname, "test-progname");
+}
Index: configure
===================================================================
RCS file: /home/cvs/mdocml/mdocml/configure,v
retrieving revision 1.27
retrieving revision 1.28
diff -Lconfigure -Lconfigure -u -p -r1.27 -r1.28
--- configure
+++ configure
@@ -43,12 +43,14 @@ BUILD_DB=1
 BUILD_CGI=0
 
 HAVE_DIRENT_NAMLEN=
+HAVE_ERR=
 HAVE_FGETLN=
 HAVE_FTS=
 HAVE_GETSUBOPT=
 HAVE_ISBLANK=
 HAVE_MKDTEMP=
 HAVE_MMAP=
+HAVE_PROGNAME=
 HAVE_REALLOCARRAY=
 HAVE_STRCASESTR=
 HAVE_STRINGLIST=
@@ -171,12 +173,14 @@ runtest() {
 
 # --- library functions ---
 runtest dirent-namlen	DIRENT_NAMLEN	|| true
+runtest err		ERR		|| true
 runtest fgetln		FGETLN		|| true
 runtest fts		FTS		|| true
 runtest getsubopt	GETSUBOPT	|| true
 runtest isblank		ISBLANK		|| true
 runtest mkdtemp		MKDTEMP		|| true
 runtest mmap		MMAP		|| true
+runtest progname	PROGNAME	|| true
 runtest reallocarray	REALLOCARRAY	|| true
 runtest strcasestr	STRCASESTR	|| true
 runtest stringlist	STRINGLIST	|| true
@@ -286,16 +290,19 @@ echo
 echo "#define MAN_CONF_FILE \"/etc/${MANM_MANCONF}\""
 echo "#define MANPATH_DEFAULT \"${MANPATH_DEFAULT}\""
 [ -n "${OSNAME}" ] && echo "#define OSNAME \"${OSNAME}\""
+[ ${HAVE_PROGNAME} -eq 0 ] && echo "#define __progname mandoc_progname"
 [ -n "${HOMEBREWDIR}" ] && echo "#define HOMEBREWDIR \"${HOMEBREWDIR}\""
 
 cat << __HEREDOC__
 #define HAVE_DIRENT_NAMLEN ${HAVE_DIRENT_NAMLEN}
+#define HAVE_ERR ${HAVE_ERR}
 #define HAVE_FGETLN ${HAVE_FGETLN}
 #define HAVE_FTS ${HAVE_FTS}
 #define HAVE_GETSUBOPT ${HAVE_GETSUBOPT}
 #define HAVE_ISBLANK ${HAVE_ISBLANK}
 #define HAVE_MKDTEMP ${HAVE_MKDTEMP}
 #define HAVE_MMAP ${HAVE_MMAP}
+#define HAVE_PROGNAME ${HAVE_PROGNAME}
 #define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
 #define HAVE_STRCASESTR ${HAVE_STRCASESTR}
 #define HAVE_STRINGLIST ${HAVE_STRINGLIST}
@@ -333,6 +340,12 @@ cat << __HEREDOC__
 #endif
 
 __HEREDOC__
+
+if [ ${HAVE_ERR} -eq 0 ]; then
+	echo "extern	void	  err(int, const char *, ...);"
+	echo "extern	void	  warn(const char *, ...);"
+	echo "extern	void	  warnx(const char *, ...);"
+fi
 
 [ ${HAVE_FGETLN} -eq 0 ] && \
 	echo "extern	char	 *fgetln(FILE *, size_t *);"
Index: mandoc_aux.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandoc_aux.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -Lmandoc_aux.c -Lmandoc_aux.c -u -p -r1.5 -r1.6
--- mandoc_aux.c
+++ mandoc_aux.c
@@ -19,6 +19,7 @@
 
 #include <sys/types.h>
 
+#include <err.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -27,6 +28,10 @@
 #include "mandoc.h"
 #include "mandoc_aux.h"
 
+#if !HAVE_PROGNAME
+const char *mandoc_progname;
+#endif
+
 int
 mandoc_asprintf(char **dest, const char *fmt, ...)
 {
@@ -37,10 +42,8 @@ mandoc_asprintf(char **dest, const char 
 	ret = vasprintf(dest, fmt, ap);
 	va_end(ap);
 
-	if (-1 == ret) {
-		perror(NULL);
-		exit((int)MANDOCLEVEL_SYSERR);
-	}
+	if (ret == -1)
+		err((int)MANDOCLEVEL_SYSERR, NULL);
 	return ret;
 }
 
@@ -50,10 +53,8 @@ mandoc_calloc(size_t num, size_t size)
 	void	*ptr;
 
 	ptr = calloc(num, size);
-	if (NULL == ptr) {
-		perror(NULL);
-		exit((int)MANDOCLEVEL_SYSERR);
-	}
+	if (ptr == NULL)
+		err((int)MANDOCLEVEL_SYSERR, NULL);
 	return ptr;
 }
 
@@ -63,10 +64,8 @@ mandoc_malloc(size_t size)
 	void	*ptr;
 
 	ptr = malloc(size);
-	if (NULL == ptr) {
-		perror(NULL);
-		exit((int)MANDOCLEVEL_SYSERR);
-	}
+	if (ptr == NULL)
+		err((int)MANDOCLEVEL_SYSERR, NULL);
 	return ptr;
 }
 
@@ -75,10 +74,8 @@ mandoc_realloc(void *ptr, size_t size)
 {
 
 	ptr = realloc(ptr, size);
-	if (NULL == ptr) {
-		perror(NULL);
-		exit((int)MANDOCLEVEL_SYSERR);
-	}
+	if (ptr == NULL)
+		err((int)MANDOCLEVEL_SYSERR, NULL);
 	return ptr;
 }
 
@@ -87,10 +84,8 @@ mandoc_reallocarray(void *ptr, size_t nu
 {
 
 	ptr = reallocarray(ptr, num, size);
-	if (NULL == ptr) {
-		perror(NULL);
-		exit((int)MANDOCLEVEL_SYSERR);
-	}
+	if (ptr == NULL)
+		err((int)MANDOCLEVEL_SYSERR, NULL);
 	return ptr;
 }
 
@@ -100,10 +95,8 @@ mandoc_strdup(const char *ptr)
 	char	*p;
 
 	p = strdup(ptr);
-	if (NULL == p) {
-		perror(NULL);
-		exit((int)MANDOCLEVEL_SYSERR);
-	}
+	if (ptr == NULL)
+		err((int)MANDOCLEVEL_SYSERR, NULL);
 	return p;
 }
 
--- /dev/null
+++ compat_err.c
@@ -0,0 +1,103 @@
+#include "config.h"
+
+#if HAVE_ERR
+
+int dummy;
+
+#else
+
+/* $Id: compat_err.c,v 1.1 2015/10/11 21:12:54 schwarze Exp $ */
+/*
+ * Copyright (c) 1993
+ *      The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <err.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+extern char *__progname;
+
+static void vwarni(const char *, va_list);
+static void vwarnxi(const char *, va_list);
+
+static void
+vwarnxi(const char *fmt, va_list ap)
+{
+	fprintf(stderr, "%s: ", __progname);
+	if (fmt != NULL)
+		vfprintf(stderr, fmt, ap);
+}
+
+static void
+vwarni(const char *fmt, va_list ap)
+{
+	int sverrno;
+
+	sverrno = errno;
+	vwarnxi(fmt, ap);
+	if (fmt != NULL)
+		fputs(": ", stderr);
+	fprintf(stderr, "%s\n", strerror(sverrno));
+}
+
+void
+err(int eval, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vwarni(fmt, ap);
+	va_end(ap);
+	exit(eval);
+}
+
+void
+warn(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vwarni(fmt, ap);
+	va_end(ap);
+}
+
+void
+warnx(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vwarnxi(fmt, ap);
+	va_end(ap);
+	fputc('\n', stderr);
+}
+
+#endif
Index: mansearch.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mansearch.c,v
retrieving revision 1.58
retrieving revision 1.59
diff -Lmansearch.c -Lmansearch.c -u -p -r1.58 -r1.59
--- mansearch.c
+++ mansearch.c
@@ -21,6 +21,7 @@
 #include <sys/types.h>
 
 #include <assert.h>
+#include <err.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
@@ -55,17 +56,17 @@ extern const char *const mansearch_keyna
 #define	SQL_BIND_TEXT(_db, _s, _i, _v) \
 	do { if (SQLITE_OK != sqlite3_bind_text \
 		((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
-		fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
+		warnx("%s", sqlite3_errmsg((_db))); \
 	} while (0)
 #define	SQL_BIND_INT64(_db, _s, _i, _v) \
 	do { if (SQLITE_OK != sqlite3_bind_int64 \
 		((_s), (_i)++, (_v))) \
-		fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
+		warnx("%s", sqlite3_errmsg((_db))); \
 	} while (0)
 #define	SQL_BIND_BLOB(_db, _s, _i, _v) \
 	do { if (SQLITE_OK != sqlite3_bind_blob \
 		((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
-		fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
+		warnx("%s", sqlite3_errmsg((_db))); \
 	} while (0)
 
 struct	expr {
@@ -120,7 +121,7 @@ mansearch_setup(int start)
 
 	if (start) {
 		if (NULL != pagecache) {
-			fprintf(stderr, "pagecache already enabled\n");
+			warnx("pagecache already enabled");
 			return (int)MANDOCLEVEL_BADARG;
 		}
 
@@ -140,10 +141,10 @@ mansearch_setup(int start)
 		if (SQLITE_OK == c)
 			return (int)MANDOCLEVEL_OK;
 
-		fprintf(stderr, "pagecache: %s\n", sqlite3_errstr(c));
+		warnx("pagecache: %s", sqlite3_errstr(c));
 
 	} else if (NULL == pagecache) {
-		fprintf(stderr, "pagecache missing\n");
+		warnx("pagecache missing");
 		return (int)MANDOCLEVEL_BADARG;
 	}
 
@@ -233,8 +234,7 @@ mansearch(const struct mansearch *search
 	for (i = 0; i < paths->sz; i++) {
 		if (chdir_status && paths->paths[i][0] != '/') {
 			if ( ! getcwd_status) {
-				fprintf(stderr, "%s: getcwd: %s\n",
-				    paths->paths[i], buf);
+				warnx("%s: getcwd: %s", paths->paths[i], buf);
 				continue;
 			} else if (chdir(buf) == -1) {
 				perror(buf);
@@ -251,8 +251,7 @@ mansearch(const struct mansearch *search
 		    SQLITE_OPEN_READONLY, NULL);
 
 		if (SQLITE_OK != c) {
-			fprintf(stderr, "%s/%s: %s\n",
-			    paths->paths[i], MANDOC_DB, strerror(errno));
+			warn("%s/%s", paths->paths[i], MANDOC_DB);
 			sqlite3_close(db);
 			continue;
 		}
@@ -274,7 +273,7 @@ mansearch(const struct mansearch *search
 		j = 1;
 		c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
 		if (SQLITE_OK != c)
-			fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+			warnx("%s", sqlite3_errmsg(db));
 
 		for (ep = e; NULL != ep; ep = ep->next) {
 			if (NULL == ep->substr) {
@@ -316,7 +315,7 @@ mansearch(const struct mansearch *search
 		}
 
 		if (SQLITE_DONE != c)
-			fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+			warnx("%s", sqlite3_errmsg(db));
 
 		sqlite3_finalize(s);
 
@@ -325,14 +324,14 @@ mansearch(const struct mansearch *search
 		    "WHERE pageid=? ORDER BY sec, arch, name",
 		    -1, &s, NULL);
 		if (SQLITE_OK != c)
-			fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+			warnx("%s", sqlite3_errmsg(db));
 
 		c = sqlite3_prepare_v2(db,
 		    "SELECT bits, key, pageid FROM keys "
 		    "WHERE pageid=? AND bits & ?",
 		    -1, &s2, NULL);
 		if (SQLITE_OK != c)
-			fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+			warnx("%s", sqlite3_errmsg(db));
 
 		for (mp = ohash_first(&htab, &idx);
 				NULL != mp;
@@ -513,7 +512,7 @@ buildnames(const struct mansearch *searc
 		globfree(&globinfo);
 	}
 	if (c != SQLITE_DONE)
-		fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+		warnx("%s", sqlite3_errmsg(db));
 	sqlite3_reset(s);
 
 	/* If none of the files is usable, use the first name. */
@@ -563,7 +562,7 @@ buildoutput(sqlite3 *db, sqlite3_stmt *s
 		output = newoutput;
 	}
 	if (SQLITE_DONE != c)
-		fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+		warnx("%s", sqlite3_errmsg(db));
 	sqlite3_reset(s);
 	return output;
 }
@@ -805,7 +804,7 @@ exprterm(const struct mansearch *search,
 			free(val);
 		if (irc) {
 			regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
-			fprintf(stderr, "regcomp: %s\n", errbuf);
+			warnx("regcomp: %s", errbuf);
 			free(e);
 			return NULL;
 		}
Index: manpath.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/manpath.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -Lmanpath.c -Lmanpath.c -u -p -r1.26 -r1.27
--- manpath.c
+++ manpath.c
@@ -21,6 +21,7 @@
 #include <sys/stat.h>
 
 #include <ctype.h>
+#include <err.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -165,10 +166,8 @@ manpath_add(struct manpaths *dirs, const
 	size_t		 i;
 
 	if (NULL == (cp = realpath(dir, buf))) {
-		if (complain) {
-			fputs("manpath: ", stderr);
-			perror(dir);
-		}
+		if (complain)
+			warn("manpath: %s", dir);
 		return;
 	}
 
@@ -177,10 +176,8 @@ manpath_add(struct manpaths *dirs, const
 			return;
 
 	if (stat(cp, &sb) == -1) {
-		if (complain) {
-			fputs("manpath: ", stderr);
-			perror(dir);
-		}
+		if (complain)
+			warn("manpath: %s", dir);
 		return;
 	}
 
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/main.c,v
retrieving revision 1.246
retrieving revision 1.247
diff -Lmain.c -Lmain.c -u -p -r1.246 -r1.247
--- main.c
+++ main.c
@@ -24,7 +24,7 @@
 
 #include <assert.h>
 #include <ctype.h>
-#include <errno.h>
+#include <err.h>
 #include <fcntl.h>
 #include <glob.h>
 #include <signal.h>
@@ -109,10 +109,11 @@ static	int		  toptions(struct curparse *
 static	void		  usage(enum argmode) __attribute__((noreturn));
 static	int		  woptions(struct curparse *, char *);
 
+extern	char		 *__progname;
+
 static	const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
 static	char		  help_arg[] = "help";
 static	char		 *help_argv[] = {help_arg, NULL};
-static	const char	 *progname;
 static	enum mandoclevel  rc;
 
 
@@ -139,15 +140,17 @@ main(int argc, char *argv[])
 	int		 use_pager;
 	int		 c;
 
+#if !HAVE_PROGNAME
 	if (argc < 1)
-		progname = "mandoc";
-	else if ((progname = strrchr(argv[0], '/')) == NULL)
-		progname = argv[0];
+		__progname = mandoc_strdup("mandoc");
+	else if ((__progname = strrchr(argv[0], '/')) == NULL)
+		__progname = argv[0];
 	else
-		++progname;
+		++__progname;
+#endif
 
 #if HAVE_SQLITE3
-	if (strcmp(progname, BINM_MAKEWHATIS) == 0)
+	if (strcmp(__progname, BINM_MAKEWHATIS) == 0)
 		return mandocdb(argc, argv);
 #endif
 
@@ -160,13 +163,13 @@ main(int argc, char *argv[])
 	memset(&search, 0, sizeof(struct mansearch));
 	search.outkey = "Nd";
 
-	if (strcmp(progname, BINM_MAN) == 0)
+	if (strcmp(__progname, BINM_MAN) == 0)
 		search.argmode = ARG_NAME;
-	else if (strcmp(progname, BINM_APROPOS) == 0)
+	else if (strcmp(__progname, BINM_APROPOS) == 0)
 		search.argmode = ARG_EXPR;
-	else if (strcmp(progname, BINM_WHATIS) == 0)
+	else if (strcmp(__progname, BINM_WHATIS) == 0)
 		search.argmode = ARG_WORD;
-	else if (strncmp(progname, "help", 4) == 0)
+	else if (strncmp(__progname, "help", 4) == 0)
 		search.argmode = ARG_NAME;
 	else
 		search.argmode = ARG_FILE;
@@ -207,15 +210,11 @@ main(int argc, char *argv[])
 			break;
 		case 'I':
 			if (strncmp(optarg, "os=", 3)) {
-				fprintf(stderr,
-				    "%s: -I %s: Bad argument\n",
-				    progname, optarg);
+				warnx("-I %s: Bad argument", optarg);
 				return (int)MANDOCLEVEL_BADARG;
 			}
 			if (defos) {
-				fprintf(stderr,
-				    "%s: -I %s: Duplicate argument\n",
-				    progname, optarg);
+				warnx("-I %s: Duplicate argument", optarg);
 				return (int)MANDOCLEVEL_BADARG;
 			}
 			defos = mandoc_strdup(optarg + 3);
@@ -308,7 +307,7 @@ main(int argc, char *argv[])
 	 */
 
 	if (search.argmode == ARG_NAME) {
-		if (*progname == 'h') {
+		if (*__progname == 'h') {
 			if (argc == 0) {
 				argv = help_argv;
 				argc = 1;
@@ -364,9 +363,7 @@ main(int argc, char *argv[])
 				fs_search(&search, &conf.manpath,
 				    argc, argv, &res, &sz);
 			else
-				fprintf(stderr,
-				    "%s: nothing appropriate\n",
-				    progname);
+				warnx("nothing appropriate");
 		}
 
 		if (sz == 0) {
@@ -577,8 +574,7 @@ fs_lookup(const struct manpaths *paths, 
 	    paths->paths[ipath], sec, name);
 	globres = glob(file, 0, NULL, &globinfo);
 	if (globres != 0 && globres != GLOB_NOMATCH)
-		fprintf(stderr, "%s: %s: glob: %s\n",
-		    progname, file, strerror(errno));
+		warn("%s: glob", file);
 	free(file);
 	if (globres == 0)
 		file = mandoc_strdup(*globinfo.gl_pathv);
@@ -588,8 +584,8 @@ fs_lookup(const struct manpaths *paths, 
 
 found:
 #if HAVE_SQLITE3
-	fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry, run "
-	    "makewhatis %s\n", progname, name, sec, paths->paths[ipath]);
+	warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s\n",
+	    name, sec, paths->paths[ipath]);
 #endif
 	*res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
 	page = *res + (*ressz - 1);
@@ -631,9 +627,7 @@ fs_search(const struct mansearch *cfg, c
 					return;
 		}
 		if (*ressz == lastsz)
-			fprintf(stderr,
-			    "%s: No entry for %s in the manual.\n",
-			    progname, *argv);
+			warnx("No entry for %s in the manual.", *argv);
 		lastsz = *ressz;
 		argv++;
 		argc--;
@@ -804,8 +798,7 @@ done:
 	return;
 
 fail:
-	fprintf(stderr, "%s: %s: SYSERR: %s: %s",
-	    progname, file, syscall, strerror(errno));
+	warn("%s: SYSERR: %s", file, syscall);
 	if (rc < MANDOCLEVEL_SYSERR)
 		rc = MANDOCLEVEL_SYSERR;
 }
@@ -823,8 +816,7 @@ koptions(int *options, char *arg)
 	} else if ( ! strcmp(arg, "us-ascii")) {
 		*options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
 	} else {
-		fprintf(stderr, "%s: -K %s: Bad argument\n",
-		    progname, arg);
+		warnx("-K %s: Bad argument", arg);
 		return 0;
 	}
 	return 1;
@@ -843,8 +835,7 @@ moptions(int *options, char *arg)
 	else if (0 == strcmp(arg, "an"))
 		*options |= MPARSE_MAN;
 	else {
-		fprintf(stderr, "%s: -m %s: Bad argument\n",
-		    progname, arg);
+		warnx("-m %s: Bad argument", arg);
 		return 0;
 	}
 
@@ -877,8 +868,7 @@ toptions(struct curparse *curp, char *ar
 	else if (0 == strcmp(arg, "pdf"))
 		curp->outtype = OUTT_PDF;
 	else {
-		fprintf(stderr, "%s: -T %s: Bad argument\n",
-		    progname, arg);
+		warnx("-T %s: Bad argument", arg);
 		return 0;
 	}
 
@@ -920,8 +910,7 @@ woptions(struct curparse *curp, char *ar
 			curp->wlevel = MANDOCLEVEL_BADARG;
 			break;
 		default:
-			fprintf(stderr, "%s: -W %s: Bad argument\n",
-			    progname, o);
+			warnx("-W %s: Bad argument", o);
 			return 0;
 		}
 	}
@@ -935,7 +924,7 @@ mmsg(enum mandocerr t, enum mandoclevel 
 {
 	const char	*mparse_msg;
 
-	fprintf(stderr, "%s: %s:", progname, file);
+	fprintf(stderr, "%s: %s:", __progname, file);
 
 	if (line)
 		fprintf(stderr, "%d:%d:", line, col + 1);
@@ -1001,9 +990,7 @@ spawn_pager(struct tag_files *tag_files)
 
 	switch (pager_pid = fork()) {
 	case -1:
-		fprintf(stderr, "%s: fork: %s\n",
-		    progname, strerror(errno));
-		exit((int)MANDOCLEVEL_SYSERR);
+		err((int)MANDOCLEVEL_SYSERR, "fork");
 	case 0:
 		break;
 	default:
@@ -1012,14 +999,10 @@ spawn_pager(struct tag_files *tag_files)
 
 	/* The child process becomes the pager. */
 
-	if (dup2(tag_files->ofd, STDOUT_FILENO) == -1) {
-		fprintf(stderr, "pager: stdout: %s\n", strerror(errno));
-		exit((int)MANDOCLEVEL_SYSERR);
-	}
+	if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
+		err((int)MANDOCLEVEL_SYSERR, "pager stdout");
 	close(tag_files->ofd);
 	close(tag_files->tfd);
 	execvp(argv[0], argv);
-	fprintf(stderr, "%s: exec %s: %s\n",
-	    progname, argv[0], strerror(errno));
-	exit((int)MANDOCLEVEL_SYSERR);
+	err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);
 }
Index: Makefile.depend
===================================================================
RCS file: /home/cvs/mdocml/mdocml/Makefile.depend,v
retrieving revision 1.14
retrieving revision 1.15
diff -LMakefile.depend -LMakefile.depend -u -p -r1.14 -r1.15
--- Makefile.depend
+++ Makefile.depend
@@ -1,6 +1,7 @@
 att.o: att.c config.h roff.h mdoc.h libmdoc.h
 cgi.o: cgi.c config.h mandoc_aux.h mandoc.h roff.h main.h manconf.h mansearch.h cgi.h
 chars.o: chars.c config.h mandoc.h mandoc_aux.h libmandoc.h chars.in
+compat_err.o: compat_err.c config.h
 compat_fgetln.o: compat_fgetln.c config.h
 compat_fts.o: compat_fts.c config.h compat_fts.h
 compat_getsubopt.o: compat_getsubopt.c config.h
@@ -51,7 +52,7 @@ read.o: read.c config.h mandoc_aux.h man
 roff.o: roff.c config.h mandoc.h mandoc_aux.h roff.h libmandoc.h roff_int.h libroff.h predefs.in
 soelim.o: soelim.c config.h compat_stringlist.h
 st.o: st.c config.h roff.h mdoc.h libmdoc.h st.in
-tag.o: tag.c compat_ohash.h mandoc_aux.h tag.h
+tag.o: tag.c config.h compat_ohash.h mandoc_aux.h tag.h
 tbl.o: tbl.c config.h mandoc.h mandoc_aux.h libmandoc.h libroff.h
 tbl_data.o: tbl_data.c config.h mandoc.h mandoc_aux.h libmandoc.h libroff.h
 tbl_html.o: tbl_html.c config.h mandoc.h out.h html.h
Index: read.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/read.c,v
retrieving revision 1.142
retrieving revision 1.143
diff -Lread.c -Lread.c -u -p -r1.142 -r1.143
--- read.c
+++ read.c
@@ -26,6 +26,7 @@
 
 #include <assert.h>
 #include <ctype.h>
+#include <err.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdarg.h>
@@ -612,10 +613,9 @@ read_whole_file(struct mparse *curp, con
 
 #if HAVE_MMAP
 	struct stat	 st;
-	if (-1 == fstat(fd, &st)) {
-		perror(file);
-		exit((int)MANDOCLEVEL_SYSERR);
-	}
+
+	if (fstat(fd, &st) == -1)
+		err((int)MANDOCLEVEL_SYSERR, "%s", file);
 
 	/*
 	 * If we're a regular file, try just reading in the whole entry
@@ -638,10 +638,8 @@ read_whole_file(struct mparse *curp, con
 #endif
 
 	if (curp->gzip) {
-		if ((gz = gzdopen(fd, "rb")) == NULL) {
-			perror(file);
-			exit((int)MANDOCLEVEL_SYSERR);
-		}
+		if ((gz = gzdopen(fd, "rb")) == NULL)
+			err((int)MANDOCLEVEL_SYSERR, "%s", file);
 	} else
 		gz = NULL;
 
@@ -670,10 +668,8 @@ read_whole_file(struct mparse *curp, con
 			fb->sz = off;
 			return 1;
 		}
-		if (ssz == -1) {
-			perror(file);
-			exit((int)MANDOCLEVEL_SYSERR);
-		}
+		if (ssz == -1)
+			err((int)MANDOCLEVEL_SYSERR, "%s", file);
 		off += (size_t)ssz;
 	}
 
Index: mandocdb.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandocdb.c,v
retrieving revision 1.197
retrieving revision 1.198
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.197 -r1.198
--- mandocdb.c
+++ mandocdb.c
@@ -23,6 +23,7 @@
 
 #include <assert.h>
 #include <ctype.h>
+#include <err.h>
 #include <errno.h>
 #include <fcntl.h>
 #if HAVE_FTS
@@ -189,8 +190,9 @@ static	int	 set_basedir(const char *, in
 static	int	 treescan(void);
 static	size_t	 utf8(unsigned int, char [7]);
 
+extern	char		*__progname;
+
 static	char		 tempfilename[32];
-static	char		*progname;
 static	int		 nodb; /* no database changes */
 static	int		 mparse_options; /* abort the parse early */
 static	int		 use_all; /* use all found files */
@@ -357,12 +359,6 @@ mandocdb(int argc, char *argv[])
 	mpages_info.key_offset = offsetof(struct mpage, inodev);
 	mlinks_info.key_offset = offsetof(struct mlink, file);
 
-	progname = strrchr(argv[0], '/');
-	if (progname == NULL)
-		progname = argv[0];
-	else
-		++progname;
-
 	/*
 	 * We accept a few different invocations.
 	 * The CHECKOP macro makes sure that invocation styles don't
@@ -370,8 +366,7 @@ mandocdb(int argc, char *argv[])
 	 */
 #define	CHECKOP(_op, _ch) do \
 	if (OP_DEFAULT != (_op)) { \
-		fprintf(stderr, "%s: -%c: Conflicting option\n", \
-		    progname, (_ch)); \
+		warnx("-%c: Conflicting option", (_ch)); \
 		goto usage; \
 	} while (/*CONSTCOND*/0)
 
@@ -407,9 +402,8 @@ mandocdb(int argc, char *argv[])
 			break;
 		case 'T':
 			if (strcmp(optarg, "utf8")) {
-				fprintf(stderr, "%s: -T%s: "
-				    "Unsupported output format\n",
-				    progname, optarg);
+				warnx("-T%s: Unsupported output format",
+				    optarg);
 				goto usage;
 			}
 			write_utf8 = 1;
@@ -436,8 +430,7 @@ mandocdb(int argc, char *argv[])
 	argv += optind;
 
 	if (OP_CONFFILE == op && argc > 0) {
-		fprintf(stderr, "%s: -C: Too many arguments\n",
-		    progname);
+		warnx("-C: Too many arguments");
 		goto usage;
 	}
 
@@ -554,8 +547,8 @@ usage:
 			"       %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
 			"       %s [-Dnp] -u dir [file ...]\n"
 			"       %s [-Q] -t file ...\n",
-		       progname, progname, progname,
-		       progname, progname);
+		       __progname, __progname, __progname,
+		       __progname, __progname);
 
 	return (int)MANDOCLEVEL_BADARG;
 }
Index: term_ps.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/term_ps.c,v
retrieving revision 1.75
retrieving revision 1.76
diff -Lterm_ps.c -Lterm_ps.c -u -p -r1.75 -r1.76
--- term_ps.c
+++ term_ps.c
@@ -20,6 +20,7 @@
 #include <sys/types.h>
 
 #include <assert.h>
+#include <err.h>
 #include <stdarg.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -582,7 +583,7 @@ pspdf_alloc(const struct mchars *mchars,
 			pagex = 216;
 			pagey = 356;
 		} else if (2 != sscanf(pp, "%ux%u", &pagex, &pagey))
-			fprintf(stderr, "%s: Unknown paper\n", pp);
+			warnx("%s: Unknown paper", pp);
 	}
 
 	/*
Index: LICENSE
===================================================================
RCS file: /home/cvs/mdocml/mdocml/LICENSE,v
retrieving revision 1.9
retrieving revision 1.10
diff -LLICENSE -LLICENSE -u -p -r1.9 -r1.10
--- LICENSE
+++ LICENSE
@@ -43,7 +43,7 @@ and 3-clause BSD licenses; see these ind
 soelim.c, soelim.1:
 Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
 
-compat_fts.c, compat_fts.h,
+compat_err.c, compat_fts.c, compat_fts.h,
 compat_getsubopt.c, compat_strcasestr.c, compat_strsep.c,
 man.1:
 Copyright (c) 1989,1990,1993,1994 The Regents of the University of California
--- /dev/null
+++ test-err.c
@@ -0,0 +1,28 @@
+/*	$Id: test-err.c,v 1.1 2015/10/11 21:12:55 schwarze Exp $	*/
+/*
+ * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <err.h>
+
+int
+main(void)
+{
+	warnx("%d. warnx", 1);
+	warn("%d. warn", 2);
+	err(0, "%d. err", 3);
+	/* NOTREACHED */
+	return 1;
+}
Index: Makefile
===================================================================
RCS file: /home/cvs/mdocml/mdocml/Makefile,v
retrieving revision 1.466
retrieving revision 1.467
diff -LMakefile -LMakefile -u -p -r1.466 -r1.467
--- Makefile
+++ Makefile
@@ -20,6 +20,7 @@ VERSION = 1.13.3
 # === LIST OF FILES ====================================================
 
 TESTSRCS	 = test-dirent-namlen.c \
+		   test-err.c \
 		   test-fgetln.c \
 		   test-fts.c \
 		   test-getsubopt.c \
@@ -27,6 +28,7 @@ TESTSRCS	 = test-dirent-namlen.c \
 		   test-mkdtemp.c \
 		   test-mmap.c \
 		   test-ohash.c \
+		   test-progname.c \
 		   test-reallocarray.c \
 		   test-sqlite3.c \
 		   test-sqlite3_errstr.c \
@@ -43,6 +45,7 @@ TESTSRCS	 = test-dirent-namlen.c \
 SRCS		 = att.c \
 		   cgi.c \
 		   chars.c \
+		   compat_err.c \
 		   compat_fgetln.c \
 		   compat_fts.c \
 		   compat_getsubopt.c \
@@ -199,7 +202,8 @@ LIBMANDOC_OBJS	 = $(LIBMAN_OBJS) \
 		   preconv.o \
 		   read.o
 
-COMPAT_OBJS	 = compat_fgetln.o \
+COMPAT_OBJS	 = compat_err.o \
+		   compat_fgetln.o \
 		   compat_fts.o \
 		   compat_getsubopt.o \
 		   compat_isblank.o \
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-10-11 21:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-11 21:13 mdocml: Finally use __progname, err(3) and warn(3) schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).