curiously, the most slimy ones turn out to be not used at all...

>From f3a2ad87e24310fe5ce278d730548d740551e435 Mon Sep 17 00:00:00 2001
From: Tamas TEVESZ <[email protected]>
Date: Wed, 17 Mar 2010 04:04:18 +0100
Subject: [PATCH] Fold many functions into one

The result is not much more convoluted than the original was, but much shorter.
Several vararg macros were added -- no idea what !gcc compilers make of this.

The messages sent to these functions are inconsistent across the source tree.
I have now decided that the logging function will add the final newline -
messages will need to be modified accordingly.

I have no idea why the original implementation was as complicated and ugly as
it was. My guess is that it was anticipated that these are be called from
sighandlers, but why no snprintf when they are all stuffed up with vsnprintf...

Still, the result is not worse in this regard either.
---
 WINGs/WINGs/WUtil.h |   21 +++++--
 WINGs/error.c       |  153 ++++++++++++---------------------------------------
 2 files changed, 52 insertions(+), 122 deletions(-)

diff --git a/WINGs/WINGs/WUtil.h b/WINGs/WINGs/WUtil.h
index 9569b32..6e1d4a5 100644
--- a/WINGs/WINGs/WUtil.h
+++ b/WINGs/WINGs/WUtil.h
@@ -219,11 +219,22 @@ waborthandler* wsetabort(waborthandler* handler);
 /* don't free the returned string */
 char* wstrerror(int errnum);
 
-void wmessage(const char *msg, ...) __attribute__((__format__(printf,1,2)));
-void wwarning(const char *msg, ...) __attribute__((__format__(printf,1,2)));
-void wfatal(const char *msg, ...) __attribute__((__format__(printf,1,2)));
-void wsyserror(const char *msg, ...) __attribute__((__format__(printf,1,2)));
-void wsyserrorwithcode(int error, const char *msg, ...) 
__attribute__((__format__(printf,2,3)));
+enum {
+       WMESSAGE_TYPE_MESSAGE,
+       WMESSAGE_TYPE_WARNING,
+       WMESSAGE_TYPE_FATAL,
+       WMESSAGE_TYPE_WSYSERROR,
+       WMESSAGE_TYPE_WSYSERRORWITHCODE
+};
+
+#define wmessage(fmt, args...) __wmessage( WMESSAGE_TYPE_MESSAGE, NULL, fmt, 
## args)
+#define wwarning(fmt, args...) __wmessage( WMESSAGE_TYPE_WARNING, NULL, fmt, 
## args)
+#define wfatal(fmt, args...) __wmessage( WMESSAGE_TYPE_FATAL, NULL, fmt, ## 
args)
+#define wsyserror(fmt, args...) __wmessage( WMESSAGE_TYPE_WSYSERROR, NULL, 
fmt, ## args)
+#define wsyserrorwithcode(errno, fmt, args...) \
+       __wmessage( WMESSAGE_TYPE_WSYSERRORWITHCODE, &errno, fmt, ## args)
+
+void __wmessage(int type, void *extra, const char *msg, ...) 
__attribute__((__format__(printf,3,4)));
 
 char* wfindfile(char *paths, char *file);
 
diff --git a/WINGs/error.c b/WINGs/error.c
index e0aa4e8..0e3597f 100644
--- a/WINGs/error.c
+++ b/WINGs/error.c
@@ -26,6 +26,8 @@
 #include <string.h>
 #include <errno.h>
 
+#include <WUtil.h>
+
 extern char *_WINGS_progname;
 
 #define MAXLINE        1024
@@ -57,130 +59,47 @@ char *wstrerror(int errnum)
 #endif
 }
 
-/*********************************************************************
- * Prints a message with variable arguments
- *
- * msg - message to print with optional formatting
- * ... - arguments to use on formatting
- *********************************************************************/
-void wmessage(const char *msg, ...)
-{
-       va_list args;
-       char buf[MAXLINE];
-
-       va_start(args, msg);
-
-       vsnprintf(buf, MAXLINE - 3, msg, args);
-       strcat(buf, "\n");
-       fflush(stdout);
-       fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-       fputs(": ", stderr);
-       fputs(buf, stderr);
-       fflush(stdout);
-       fflush(stderr);
-
-       va_end(args);
-}
-
-/*********************************************************************
- * Prints a warning message with variable arguments
- *
- * msg - message to print with optional formatting
- * ... - arguments to use on formatting
- *********************************************************************/
-void wwarning(const char *msg, ...)
+void __wmessage(int type, void *extra, const char *msg, ...)
 {
        va_list args;
        char buf[MAXLINE];
 
-       va_start(args, msg);
-
-       vsnprintf(buf, MAXLINE - 3, msg, args);
-       strcat(buf, "\n");
-       fflush(stdout);
-       fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-       fputs(_(" warning: "), stderr);
-       fputs(buf, stderr);
        fflush(stdout);
-       fflush(stderr);
-
-       va_end(args);
-}
-
-/**************************************************************************
- * Prints a fatal error message with variable arguments and terminates
- *
- * msg - message to print with optional formatting
- * ... - arguments to use on formatting
- **************************************************************************/
-void wfatal(const char *msg, ...)
-{
-       va_list args;
-       char buf[MAXLINE];
-
+       /* message format: <wings_progname>: <qualifier>: <message>[: <extra 
info>]"\n" */
+       snprintf(buf, sizeof(buf), "%s: ", _WINGS_progname ? _WINGS_progname : 
"WINGs");
        va_start(args, msg);
-
-       vsnprintf(buf, MAXLINE - 3, msg, args);
-       strcat(buf, "\n");
-       fflush(stdout);
-       fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-       fputs(_(" fatal error: "), stderr);
-       fputs(buf, stderr);
-       fflush(stdout);
-       fflush(stderr);
-
+       switch (type) {
+               case WMESSAGE_TYPE_WARNING:
+                       snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                               _("warning: "));
+                       vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                               msg, args);
+               break;
+               case WMESSAGE_TYPE_FATAL:
+                       snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                               _("fatal error: "));
+                       vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                               msg, args);
+               break;
+               case WMESSAGE_TYPE_WSYSERROR:
+               case WMESSAGE_TYPE_WSYSERRORWITHCODE:
+                       snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                               _("error: "));
+                       vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                               msg, args);
+                       snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+                               ": %s", type == WMESSAGE_TYPE_WSYSERROR ?
+                                   wstrerror(errno) : wstrerror(*(int 
*)extra));
+               break;
+               case WMESSAGE_TYPE_MESSAGE:
+                       /* FALLTHROUGH */
+               default:
+                       snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 
": ");
+                       vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 
msg, args);
+               break;
+       }
        va_end(args);
-}
-
-/*********************************************************************
- * Prints a system error message with variable arguments
- *
- * msg - message to print with optional formatting
- * ... - arguments to use on formatting
- *********************************************************************/
-void wsyserror(const char *msg, ...)
-{
-       va_list args;
-       char buf[MAXLINE];
-       int error = errno;
-
-       va_start(args, msg);
-       vsnprintf(buf, MAXLINE - 3, msg, args);
-       fflush(stdout);
-       fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-       fputs(_(" error: "), stderr);
+       strncat(buf + strlen(buf), "\n", sizeof(buf) - strlen(buf) - 1);
        fputs(buf, stderr);
-       fputs(": ", stderr);
-       fputs(wstrerror(error), stderr);
-       fputs("\n", stderr);
-       fflush(stderr);
-       fflush(stdout);
-       va_end(args);
 }
 
-/*********************************************************************
- * Prints a system error message with variable arguments, being given
- * the error code.
- *
- * error - the error code foe which to print the message
- * msg   - message to print with optional formatting
- * ...   - arguments to use on formatting
- *********************************************************************/
-void wsyserrorwithcode(int error, const char *msg, ...)
-{
-       va_list args;
-       char buf[MAXLINE];
-
-       va_start(args, msg);
-       vsnprintf(buf, MAXLINE - 3, msg, args);
-       fflush(stdout);
-       fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-       fputs(_(" error: "), stderr);
-       fputs(buf, stderr);
-       fputs(": ", stderr);
-       fputs(wstrerror(error), stderr);
-       fputs("\n", stderr);
-       fflush(stderr);
-       fflush(stdout);
-       va_end(args);
-}
-- 
1.7.0


-- 
[-]

mkdir /nonexistent
From f3a2ad87e24310fe5ce278d730548d740551e435 Mon Sep 17 00:00:00 2001
From: Tamas TEVESZ <[email protected]>
Date: Wed, 17 Mar 2010 04:04:18 +0100
Subject: [PATCH] Fold many functions into one

The result is not much more convoluted than the original was, but much shorter.
Several vararg macros were added -- no idea what !gcc compilers make of this.

The messages sent to these functions are inconsistent across the source tree.
I have now decided that the logging function will add the final newline -
messages will need to be modified accordingly.

I have no idea why the original implementation was as complicated and ugly as
it was. My guess is that it was anticipated that these are be called from
sighandlers, but why no snprintf when they are all stuffed up with vsnprintf...

Still, the result is not worse in this regard either.
---
 WINGs/WINGs/WUtil.h |   21 +++++--
 WINGs/error.c       |  153 ++++++++++++---------------------------------------
 2 files changed, 52 insertions(+), 122 deletions(-)

diff --git a/WINGs/WINGs/WUtil.h b/WINGs/WINGs/WUtil.h
index 9569b32..6e1d4a5 100644
--- a/WINGs/WINGs/WUtil.h
+++ b/WINGs/WINGs/WUtil.h
@@ -219,11 +219,22 @@ waborthandler* wsetabort(waborthandler* handler);
 /* don't free the returned string */
 char* wstrerror(int errnum);
 
-void wmessage(const char *msg, ...) __attribute__((__format__(printf,1,2)));
-void wwarning(const char *msg, ...) __attribute__((__format__(printf,1,2)));
-void wfatal(const char *msg, ...) __attribute__((__format__(printf,1,2)));
-void wsyserror(const char *msg, ...) __attribute__((__format__(printf,1,2)));
-void wsyserrorwithcode(int error, const char *msg, ...) __attribute__((__format__(printf,2,3)));
+enum {
+	WMESSAGE_TYPE_MESSAGE,
+	WMESSAGE_TYPE_WARNING,
+	WMESSAGE_TYPE_FATAL,
+	WMESSAGE_TYPE_WSYSERROR,
+	WMESSAGE_TYPE_WSYSERRORWITHCODE
+};
+
+#define wmessage(fmt, args...) __wmessage( WMESSAGE_TYPE_MESSAGE, NULL, fmt, ## args)
+#define wwarning(fmt, args...) __wmessage( WMESSAGE_TYPE_WARNING, NULL, fmt, ## args)
+#define wfatal(fmt, args...) __wmessage( WMESSAGE_TYPE_FATAL, NULL, fmt, ## args)
+#define wsyserror(fmt, args...) __wmessage( WMESSAGE_TYPE_WSYSERROR, NULL, fmt, ## args)
+#define wsyserrorwithcode(errno, fmt, args...) \
+	__wmessage( WMESSAGE_TYPE_WSYSERRORWITHCODE, &errno, fmt, ## args)
+
+void __wmessage(int type, void *extra, const char *msg, ...) __attribute__((__format__(printf,3,4)));
 
 char* wfindfile(char *paths, char *file);
 
diff --git a/WINGs/error.c b/WINGs/error.c
index e0aa4e8..0e3597f 100644
--- a/WINGs/error.c
+++ b/WINGs/error.c
@@ -26,6 +26,8 @@
 #include <string.h>
 #include <errno.h>
 
+#include <WUtil.h>
+
 extern char *_WINGS_progname;
 
 #define MAXLINE	1024
@@ -57,130 +59,47 @@ char *wstrerror(int errnum)
 #endif
 }
 
-/*********************************************************************
- * Prints a message with variable arguments
- *
- * msg - message to print with optional formatting
- * ... - arguments to use on formatting
- *********************************************************************/
-void wmessage(const char *msg, ...)
-{
-	va_list args;
-	char buf[MAXLINE];
-
-	va_start(args, msg);
-
-	vsnprintf(buf, MAXLINE - 3, msg, args);
-	strcat(buf, "\n");
-	fflush(stdout);
-	fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-	fputs(": ", stderr);
-	fputs(buf, stderr);
-	fflush(stdout);
-	fflush(stderr);
-
-	va_end(args);
-}
-
-/*********************************************************************
- * Prints a warning message with variable arguments
- *
- * msg - message to print with optional formatting
- * ... - arguments to use on formatting
- *********************************************************************/
-void wwarning(const char *msg, ...)
+void __wmessage(int type, void *extra, const char *msg, ...)
 {
 	va_list args;
 	char buf[MAXLINE];
 
-	va_start(args, msg);
-
-	vsnprintf(buf, MAXLINE - 3, msg, args);
-	strcat(buf, "\n");
-	fflush(stdout);
-	fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-	fputs(_(" warning: "), stderr);
-	fputs(buf, stderr);
 	fflush(stdout);
-	fflush(stderr);
-
-	va_end(args);
-}
-
-/**************************************************************************
- * Prints a fatal error message with variable arguments and terminates
- *
- * msg - message to print with optional formatting
- * ... - arguments to use on formatting
- **************************************************************************/
-void wfatal(const char *msg, ...)
-{
-	va_list args;
-	char buf[MAXLINE];
-
+	/* message format: <wings_progname>: <qualifier>: <message>[: <extra info>]"\n" */
+	snprintf(buf, sizeof(buf), "%s: ", _WINGS_progname ? _WINGS_progname : "WINGs");
 	va_start(args, msg);
-
-	vsnprintf(buf, MAXLINE - 3, msg, args);
-	strcat(buf, "\n");
-	fflush(stdout);
-	fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-	fputs(_(" fatal error: "), stderr);
-	fputs(buf, stderr);
-	fflush(stdout);
-	fflush(stderr);
-
+	switch (type) {
+		case WMESSAGE_TYPE_WARNING:
+			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+				_("warning: "));
+			vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+				msg, args);
+		break;
+		case WMESSAGE_TYPE_FATAL:
+			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+				_("fatal error: "));
+			vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+				msg, args);
+		break;
+		case WMESSAGE_TYPE_WSYSERROR:
+		case WMESSAGE_TYPE_WSYSERRORWITHCODE:
+			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+				_("error: "));
+			vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+				msg, args);
+			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
+				": %s", type == WMESSAGE_TYPE_WSYSERROR ?
+				    wstrerror(errno) : wstrerror(*(int *)extra));
+		break;
+		case WMESSAGE_TYPE_MESSAGE:
+			/* FALLTHROUGH */
+		default:
+			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ": ");
+			vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), msg, args);
+		break;
+	}
 	va_end(args);
-}
-
-/*********************************************************************
- * Prints a system error message with variable arguments
- *
- * msg - message to print with optional formatting
- * ... - arguments to use on formatting
- *********************************************************************/
-void wsyserror(const char *msg, ...)
-{
-	va_list args;
-	char buf[MAXLINE];
-	int error = errno;
-
-	va_start(args, msg);
-	vsnprintf(buf, MAXLINE - 3, msg, args);
-	fflush(stdout);
-	fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-	fputs(_(" error: "), stderr);
+	strncat(buf + strlen(buf), "\n", sizeof(buf) - strlen(buf) - 1);
 	fputs(buf, stderr);
-	fputs(": ", stderr);
-	fputs(wstrerror(error), stderr);
-	fputs("\n", stderr);
-	fflush(stderr);
-	fflush(stdout);
-	va_end(args);
 }
 
-/*********************************************************************
- * Prints a system error message with variable arguments, being given
- * the error code.
- *
- * error - the error code foe which to print the message
- * msg   - message to print with optional formatting
- * ...   - arguments to use on formatting
- *********************************************************************/
-void wsyserrorwithcode(int error, const char *msg, ...)
-{
-	va_list args;
-	char buf[MAXLINE];
-
-	va_start(args, msg);
-	vsnprintf(buf, MAXLINE - 3, msg, args);
-	fflush(stdout);
-	fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
-	fputs(_(" error: "), stderr);
-	fputs(buf, stderr);
-	fputs(": ", stderr);
-	fputs(wstrerror(error), stderr);
-	fputs("\n", stderr);
-	fflush(stderr);
-	fflush(stdout);
-	va_end(args);
-}
-- 
1.7.0

Reply via email to