From 18d485fcbce0f9f587b683756ee3fe78d7fd3372 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sat, 11 Feb 2023 14:11:21 -0800 Subject: Print which arguments were unknown before giving usage message Signed-off-by: Alan Coopersmith --- configure.ac | 3 +++ xlogo.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 778ab76..d5f04b2 100644 --- a/configure.ac +++ b/configure.ac @@ -36,6 +36,9 @@ m4_ifndef([XORG_MACROS_VERSION], XORG_MACROS_VERSION(1.8) XORG_DEFAULT_OPTIONS +# Checks for library functions. +AC_CHECK_FUNCS([strlcat strlcpy]) + # Base set of required pkg-config packages XLOGO_DEPS="sm xaw7 xmu xt >= 1.0 xext x11" diff --git a/xlogo.c b/xlogo.c index 1c41f8b..62bc2a3 100644 --- a/xlogo.c +++ b/xlogo.c @@ -24,6 +24,30 @@ in this Software without prior written authorization from The Open Group. */ +/* + * Copyright (c) 2023, Oracle and/or its affiliates. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + + #ifdef HAVE_CONFIG_H #include #endif @@ -38,6 +62,21 @@ in this Software without prior written authorization from The Open Group. #endif #include #include +#include +#include + +#ifndef HAVE_STRLCAT +#define strlcat(dst, src, len) do { \ + strncat(dst, src, len - strlen(dst) - 1); \ + dst[len] = '\0'; \ +} while(0) +#endif +#ifndef HAVE_STRLCPY +#define strlcpy(dst, src, len) do { \ + strncpy(dst, src, len - 1); \ + dst[len] = '\0'; \ +} while(0) +#endif /* Global vars*/ const char *ProgramName; /* program name (from argv[0]) */ @@ -96,13 +135,16 @@ save(Widget w, XtPointer client_data, XtPointer call_data) */ static void -Syntax(Widget toplevel) +Syntax(Widget toplevel, String message) { Arg arg; SmcConn connection; String reasons[10]; int i, n = 0; + if (message) + reasons[n++] = message; + reasons[n++] = "Usage: "; reasons[n++] = (String)ProgramName; reasons[n++] = " [-fg ] [-bg ] [-rv] [-bw ] [-bd ]\n"; @@ -113,6 +155,7 @@ Syntax(Widget toplevel) reasons[n++] = " [-render] [-sharp]\n"; #endif /* XRENDER */ reasons[n++] = " [-shape]\n\n"; + assert(n <= XtNumber(reasons)); XtSetArg(arg, XtNconnection, &connection); XtGetValues(toplevel, &arg, (Cardinal)1); @@ -137,8 +180,24 @@ main(int argc, char *argv[]) options, XtNumber(options), &argc, argv, fallback_resources, sessionShellWidgetClass, NULL, ZERO); - if (argc != 1) - Syntax(toplevel); + if (argc != 1) { + const char *header = "Unknown argument(s):"; + char *message; + size_t len = strlen(header) + 3; /* 3 for "\n\n\0" */ + for (int n = 1; n < argc; n++) { + len += strlen(argv[n]) + 1; + } + message = malloc(len); + if (message != NULL) { + strlcpy(message, header, len); + for (int n = 1; n < argc; n++) { + strlcat(message, " ", len); + strlcat(message, argv[n], len); + } + strlcat(message, "\n\n", len); + } + Syntax(toplevel, message); + } XtGetApplicationResources(toplevel, (XtPointer)&userOptions, resources, XtNumber(resources), NULL, 0); -- cgit v1.2.3