summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2023-02-11 14:11:21 -0800
committerAlan Coopersmith <alan.coopersmith@oracle.com>2023-02-11 14:11:40 -0800
commit18d485fcbce0f9f587b683756ee3fe78d7fd3372 (patch)
tree414ec5402500537111e75095ac3dccfea3c2cd43
parenta03055d5b3385f41a50ba40fcc00e8cdb8758947 (diff)
Print which arguments were unknown before giving usage message
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--configure.ac3
-rw-r--r--xlogo.c65
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 <config.h>
#endif
@@ -38,6 +62,21 @@ in this Software without prior written authorization from The Open Group.
#endif
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#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 <color>] [-bg <color>] [-rv] [-bw <pixels>] [-bd <color>]\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);