* mdocml: Use getprogname(3) rather than __progname.
@ 2015-11-06 16:31 schwarze
0 siblings, 0 replies; only message in thread
From: schwarze @ 2015-11-06 16:31 UTC (permalink / raw)
To: source
Log Message:
-----------
Use getprogname(3) rather than __progname.
Suggested by Joerg@ Sonnenberger (NetBSD).
Last year, deraadt@ confirmed on tech@ that this "has the potential
to be more portable", and micro-optimizing for speed is not relevant
here. Also gets rid of one global variable.
Modified Files:
--------------
mdocml:
Makefile
compat_err.c
configure
main.c
mandoc_aux.c
mandocdb.c
test-progname.c
Added Files:
-----------
mdocml:
compat_progname.c
Revision Data
-------------
--- /dev/null
+++ compat_progname.c
@@ -0,0 +1,42 @@
+#include "config.h"
+
+#if HAVE_PROGNAME
+
+int dummy;
+
+#else
+
+/* $Id: compat_progname.c,v 1.1 2015/11/06 16:30:33 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.
+ */
+
+static const char *progname;
+
+void
+setprogname(const char *name)
+{
+
+ progname = name;
+}
+
+const char *
+getprogname(void)
+{
+
+ return progname;
+}
+
+#endif
Index: Makefile
===================================================================
RCS file: /home/cvs/mdocml/mdocml/Makefile,v
retrieving revision 1.472
retrieving revision 1.473
diff -LMakefile -LMakefile -u -p -r1.472 -r1.473
--- Makefile
+++ Makefile
@@ -52,6 +52,7 @@ SRCS = att.c \
compat_isblank.c \
compat_mkdtemp.c \
compat_ohash.c \
+ compat_progname.c \
compat_reallocarray.c \
compat_sqlite3_errstr.c \
compat_strcasestr.c \
@@ -211,6 +212,7 @@ COMPAT_OBJS = compat_err.o \
compat_isblank.o \
compat_mkdtemp.o \
compat_ohash.o \
+ compat_progname.o \
compat_reallocarray.o \
compat_sqlite3_errstr.o \
compat_strcasestr.o \
Index: configure
===================================================================
RCS file: /home/cvs/mdocml/mdocml/configure,v
retrieving revision 1.28
retrieving revision 1.29
diff -Lconfigure -Lconfigure -u -p -r1.28 -r1.29
--- configure
+++ configure
@@ -290,7 +290,6 @@ 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__
@@ -358,6 +357,11 @@ fi
[ ${HAVE_MKDTEMP} -eq 0 ] && \
echo "extern char *mkdtemp(char *);"
+
+if [ ${HAVE_PROGNAME} -eq 0 ]; then
+ echo "extern const char *getprogname(void);"
+ echo "extern void setprogname(const char *);"
+fi
[ ${HAVE_REALLOCARRAY} -eq 0 ] && \
echo "extern void *reallocarray(void *, size_t, size_t);"
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/main.c,v
retrieving revision 1.253
retrieving revision 1.254
diff -Lmain.c -Lmain.c -u -p -r1.253 -r1.254
--- main.c
+++ main.c
@@ -101,8 +101,6 @@ 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};
@@ -116,6 +114,7 @@ main(int argc, char *argv[])
struct curparse curp;
struct mansearch search;
struct tag_files *tag_files;
+ const char *progname;
char *auxpaths;
char *defos;
unsigned char *uc;
@@ -132,17 +131,21 @@ main(int argc, char *argv[])
int use_pager;
int c;
-#if !HAVE_PROGNAME
+#if HAVE_PROGNAME
+ progname = getprogname();
+#else
if (argc < 1)
- __progname = mandoc_strdup("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;
+ setprogname(progname);
#endif
#if HAVE_SQLITE3
- if (strcmp(__progname, BINM_MAKEWHATIS) == 0)
+ if (strncmp(progname, "mandocdb", 8) == 0 ||
+ strcmp(progname, BINM_MAKEWHATIS) == 0)
return mandocdb(argc, argv);
#endif
@@ -155,13 +158,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;
@@ -299,7 +302,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;
@@ -591,7 +594,7 @@ fs_lookup(const struct manpaths *paths,
found:
#if HAVE_SQLITE3
- warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s\n",
+ warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s",
name, sec, paths->paths[ipath]);
#endif
*res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
@@ -931,7 +934,7 @@ mmsg(enum mandocerr t, enum mandoclevel
{
const char *mparse_msg;
- fprintf(stderr, "%s: %s:", __progname, file);
+ fprintf(stderr, "%s: %s:", getprogname(), file);
if (line)
fprintf(stderr, "%d:%d:", line, col + 1);
Index: mandoc_aux.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandoc_aux.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -Lmandoc_aux.c -Lmandoc_aux.c -u -p -r1.7 -r1.8
--- mandoc_aux.c
+++ mandoc_aux.c
@@ -28,9 +28,6 @@
#include "mandoc.h"
#include "mandoc_aux.h"
-#if !HAVE_PROGNAME
-const char *mandoc_progname;
-#endif
int
mandoc_asprintf(char **dest, const char *fmt, ...)
Index: mandocdb.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandocdb.c,v
retrieving revision 1.205
retrieving revision 1.206
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.205 -r1.206
--- mandocdb.c
+++ mandocdb.c
@@ -183,8 +183,6 @@ 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 int nodb; /* no database changes */
static int mparse_options; /* abort the parse early */
@@ -335,7 +333,7 @@ mandocdb(int argc, char *argv[])
{
struct manconf conf;
struct mparse *mp;
- const char *path_arg;
+ const char *path_arg, *progname;
size_t j, sz;
int ch, i;
@@ -526,13 +524,13 @@ out:
ohash_delete(&mlinks);
return exitcode;
usage:
+ progname = getprogname();
fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
" %s [-aDnpQ] [-Tutf8] dir ...\n"
" %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: compat_err.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/compat_err.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lcompat_err.c -Lcompat_err.c -u -p -r1.1 -r1.2
--- compat_err.c
+++ compat_err.c
@@ -43,15 +43,13 @@ int dummy;
#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);
+ fprintf(stderr, "%s: ", getprogname());
if (fmt != NULL)
vfprintf(stderr, fmt, ap);
}
Index: test-progname.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/test-progname.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -Ltest-progname.c -Ltest-progname.c -u -p -r1.1 -r1.2
--- test-progname.c
+++ test-progname.c
@@ -1,9 +1,10 @@
-#include <string.h>
-
-extern char *__progname;
+#include <stdlib.h>
int
main(void)
{
- return !!strcmp(__progname, "test-progname");
+ const char * progname;
+
+ progname = getprogname();
+ return progname == NULL;
}
--
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-11-06 16:31 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-06 16:31 mdocml: Use getprogname(3) rather than __progname 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).