summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas E. Dickey <dickey@invisible-island.net>2020-06-19 20:23:42 -0400
committerThomas E. Dickey <dickey@invisible-island.net>2020-06-19 20:23:42 -0400
commit248be9688c23f249f1ecf1860f6f4cb09e56fab2 (patch)
tree4e26a4b75c9659120aa4e509292b341bb2f305e1
parenta9d6701d977700b18e31a70cc7982431bc702095 (diff)
add/use functions to make warning messages more consistently use the program name as a prefix
Signed-off-by: Thomas E. Dickey <dickey@invisible-island.net>
-rw-r--r--src/twm.c109
-rw-r--r--src/twm.h22
2 files changed, 100 insertions, 31 deletions
diff --git a/src/twm.c b/src/twm.c
index 405a57e..e6b78c1 100644
--- a/src/twm.c
+++ b/src/twm.c
@@ -62,6 +62,8 @@ in this Software without prior written authorization from The Open Group.
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
+#include <stdarg.h>
+
#include "twm.h"
#include "iconmgr.h"
#include "add_window.h"
@@ -76,6 +78,7 @@ in this Software without prior written authorization from The Open Group.
#include "parse.h"
#include "session.h"
#include "version.h"
+
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/SM/SMlib.h>
@@ -116,7 +119,7 @@ ScreenInfo **ScreenList; /* structures for each screen */
ScreenInfo *Scr = NULL; /* the cur and prev screens */
int PreviousScreen; /* last screen that we were on */
int FirstScreen; /* TRUE ==> first screen of display */
-static Bool PrintErrorMessages = False; /* controls error messages */
+static int verbose = 1; /* controls error messages */
static int RedirectError; /* TRUE ==> another window manager running */
static int TwmErrorHandler(Display *dpy, XErrorEvent *event); /* for settting RedirectError */
static int CatchRedirectError(Display *dpy, XErrorEvent *event); /* for everything else */
@@ -305,7 +308,7 @@ main(int argc, char *argv[])
case 'v': /* -verbose */
if (!brief_opt(argv[i], "verbose"))
usage();
- PrintErrorMessages = True;
+ verbose++;
continue;
case 'c': /* -clientId */
if (!brief_opt(argv[i], "clientId"))
@@ -324,7 +327,7 @@ main(int argc, char *argv[])
case 'q': /* -quiet */
if (!brief_opt(argv[i], "quiet"))
usage();
- PrintErrorMessages = False;
+ --verbose;
continue;
}
}
@@ -379,16 +382,11 @@ main(int argc, char *argv[])
if (!(dpy = XtOpenDisplay(appContext, display_name, "twm", "twm",
NULL, 0, &zero, NULL))) {
- fprintf(stderr, "%s: unable to open display \"%s\"\n",
- ProgramName, XDisplayName(display_name));
- exit(EXIT_FAILURE);
+ twmError("unable to open display \"%s\"", XDisplayName(display_name));
}
if (fcntl(ConnectionNumber(dpy), F_SETFD, 1) == -1) {
- fprintf(stderr,
- "%s: unable to mark display connection as close-on-exec\n",
- ProgramName);
- exit(EXIT_FAILURE);
+ twmError("unable to mark display connection as close-on-exec");
}
if (restore_filename)
@@ -425,10 +423,7 @@ main(int argc, char *argv[])
/* for simplicity, always allocate NumScreens ScreenInfo struct pointers */
ScreenList = calloc((size_t) NumScreens, sizeof(ScreenInfo *));
if (ScreenList == NULL) {
- fprintf(stderr,
- "%s: Unable to allocate memory for screen list, exiting.\n",
- ProgramName);
- exit(EXIT_FAILURE);
+ twmError("Unable to allocate memory for screen list, exiting");
}
numManaged = 0;
PreviousScreen = DefaultScreen(dpy);
@@ -438,8 +433,7 @@ main(int argc, char *argv[])
/* Ignore print screens to avoid that users accidentally warp on a
* print screen (which are not visible on video displays) */
if ((!NoPrintscreens) && IsPrintScreen(XScreenOfDisplay(dpy, scrnum))) {
- fprintf(stderr, "%s: skipping print screen %d\n",
- ProgramName, scrnum);
+ twmWarning("skipping print screen %d", scrnum);
continue;
}
#endif /* XPRINT */
@@ -457,12 +451,13 @@ main(int argc, char *argv[])
XSetErrorHandler(TwmErrorHandler);
if (RedirectError) {
- fprintf(stderr, "%s: another window manager is already running.",
- ProgramName);
- if (MultiScreen && NumScreens > 0)
- fprintf(stderr, " on screen %d?\n", scrnum);
- else
- fprintf(stderr, "?\n");
+ if (MultiScreen && NumScreens > 0) {
+ twmWarning("another window manager is already running."
+ " on screen %d?\n", scrnum);
+ }
+ else {
+ twmWarning("another window manager is already running.");
+ }
continue;
}
@@ -471,9 +466,9 @@ main(int argc, char *argv[])
/* Note: ScreenInfo struct is calloc'ed to initialize to zero. */
Scr = ScreenList[scrnum] = calloc(1, sizeof(ScreenInfo));
if (Scr == NULL) {
- fprintf(stderr,
- "%s: unable to allocate memory for ScreenInfo structure for screen %d.\n",
- ProgramName, scrnum);
+ twmWarning
+ ("unable to allocate memory for ScreenInfo structure for screen %d.",
+ scrnum);
continue;
}
@@ -706,9 +701,10 @@ main(int argc, char *argv[])
} /* for */
if (numManaged == 0) {
- if (MultiScreen && NumScreens > 0)
- fprintf(stderr, "%s: unable to find any unmanaged %sscreens.\n",
- ProgramName, NoPrintscreens ? "" : "video ");
+ if (MultiScreen && NumScreens > 0) {
+ twmError("unable to find any unmanaged %sscreens.\n",
+ NoPrintscreens ? "" : "video ");
+ }
exit(EXIT_FAILURE);
}
@@ -993,7 +989,7 @@ TwmErrorHandler(Display *dpy2, XErrorEvent *event)
LastErrorEvent = *event;
ErrorOccurred = True;
- if (PrintErrorMessages && /* don't be too obnoxious */
+ if ((verbose > 1) && /* don't be too obnoxious */
event->error_code != BadWindow && /* watch for dead puppies */
(event->request_code != X_GetGeometry && /* of all styles */
event->error_code != BadDrawable))
@@ -1009,3 +1005,58 @@ CatchRedirectError(Display *dpy2 _X_UNUSED, XErrorEvent *event)
ErrorOccurred = True;
return 0;
}
+
+void
+twmError(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ fprintf(stderr, "%s: error: ", ProgramName);
+ vfprintf(stderr, format, ap);
+ fputc('\n', stderr);
+ va_end(ap);
+ exit(EXIT_FAILURE);
+}
+
+void
+twmWarning(const char *format, ...)
+{
+ if (verbose > 0) {
+ va_list ap;
+
+ va_start(ap, format);
+ fprintf(stderr, "%s: warning: ", ProgramName);
+ vfprintf(stderr, format, ap);
+ fputc('\n', stderr);
+ va_end(ap);
+ }
+}
+
+void
+twmVerbose(const char *format, ...)
+{
+ if (verbose > 1) {
+ va_list ap;
+
+ va_start(ap, format);
+ fprintf(stderr, "%s: warning: ", ProgramName);
+ vfprintf(stderr, format, ap);
+ fputc('\n', stderr);
+ va_end(ap);
+ }
+}
+
+void
+twmMessage(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ printf("%s: ", ProgramName);
+ vprintf(format, ap);
+ putc('\n', stdout);
+ va_end(ap);
+
+ fflush(stdout);
+}
diff --git a/src/twm.h b/src/twm.h
index 1fe106d..d39abdb 100644
--- a/src/twm.h
+++ b/src/twm.h
@@ -60,6 +60,7 @@ from The Open Group.
#ifndef _TWM_
#define _TWM_
+/* *INDENT-OFF* */
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -73,6 +74,18 @@ from The Open Group.
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
+#ifndef GCC_PRINTFLIKE
+#if defined(GCC_PRINTF) && !defined(printf)
+#define GCC_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
+#else
+#define GCC_PRINTFLIKE(fmt,var) /*nothing*/
+#endif
+#endif
+
+#ifndef GCC_NORETURN
+#define GCC_NORETURN _X_NORETURN
+#endif
+
#ifndef WithdrawnState
#define WithdrawnState 0
#endif
@@ -335,8 +348,7 @@ extern void CreateFonts(void);
extern void RestoreWithdrawnLocation(TwmWindow *tmp);
extern void Reborder(Time time);
extern void
-Done(XtPointer, XtSignalId *)
- _X_NORETURN;
+Done(XtPointer, XtSignalId *) _X_NORETURN;
extern void
ComputeCommonTitleOffsets(void);
extern void
@@ -392,6 +404,11 @@ NewBitmapCursor(Cursor *cp, char *source, char *mask);
extern Pixmap
CreateMenuIcon(int height, unsigned int *widthp, unsigned int *heightp);
+extern void twmError(const char *, ...) GCC_PRINTFLIKE(1,2) GCC_NORETURN;
+extern void twmWarning(const char *, ...) GCC_PRINTFLIKE(1,2);
+extern void twmVerbose(const char *, ...) GCC_PRINTFLIKE(1,2);
+extern void twmMessage(const char *, ...) GCC_PRINTFLIKE(1,2);
+
extern Bool ErrorOccurred;
extern XErrorEvent LastErrorEvent;
@@ -433,4 +450,5 @@ extern int XrandrErrorBase;
#define _XA_WM_CLIENT_LEADER TwmAtoms[9]
#define _XA_WM_WINDOW_ROLE TwmAtoms[10]
+/* *INDENT-ON* */
#endif /* _TWM_ */