diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2023-02-04 13:10:14 -0800 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2023-02-04 13:14:41 -0800 |
commit | 56a054f35336d5de4aa9f77b7c533aceec9ab58e (patch) | |
tree | e2d635d3fc40c4a347b1f10048b6dffce2d49bc1 | |
parent | a6996f65ac6798a36709971ab5d9408e8c6c57eb (diff) |
Make argument handling a little more standard
- Allow device path after "-num" instead of requiring it before
- Add "-help" & "-version" options (with undocumented -- variants
accepted for those with GNU habits)
- Issue error message for unknown arguments or extra device paths
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r-- | COPYING | 4 | ||||
-rw-r--r-- | constype.c | 78 | ||||
-rw-r--r-- | constype.man | 20 |
3 files changed, 95 insertions, 7 deletions
@@ -14,9 +14,9 @@ Original code: Author: Doug Moran, SRI -Autotool conversion: +Further updates: - Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. + Copyright (c) 1997, 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"), @@ -14,6 +14,29 @@ * * Author: Doug Moran, SRI */ +/* + * Copyright (c) 1997, 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. + */ + /* SUN-SPOTS DIGEST Thursday, 17 March 1988 Volume 6 : Issue 31 @@ -40,17 +63,30 @@ style. #endif #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> static int wu_fbid(const char *devname, const char **fbname, int *fbtype); +static void +usage(const char *program_name, int exitval) +{ + fprintf(stderr, "usage: %s [ -num ] [ device_name ]\n" + " %s -help\n" + " %s -version\n", + program_name, program_name, program_name); + exit(exitval); +} + int main(int argc, char **argv) { + const char *program_name = argv[0]; int fbtype = -1; const char *fbname, *dev; + int dev_set = 0; int print_num = 0; int error; @@ -58,13 +94,49 @@ main(int argc, char **argv) dev = argv[1]; argc--; argv++; + dev_set++; } else dev = "/dev/fb"; - error = wu_fbid(dev, &fbname, &fbtype); - if (argc > 1 && strncmp(argv[1], "-num", strlen(argv[1])) == 0) - print_num = 1; + while (argc > 1) { + if (strncmp(argv[1], "-num", strlen(argv[1])) == 0) { + print_num = 1; + argc--; + argv++; + continue; + } + if ((strcmp(argv[1], "-help") == 0) || + (strcmp(argv[1], "--help") == 0)) { + usage(program_name, 0); + } + if ((strcmp(argv[1], "-version") == 0) || + (strcmp(argv[1], "--version") == 0)) { + puts(PACKAGE_STRING); + exit(0); + } + + /* Historical usage put the pathname before -num, + but now we accept it afterwards to match standard command style */ + if (argv[1][0] == '/') { + if (dev_set == 0) { + dev = argv[1]; + argc--; + argv++; + dev_set++; + continue; + } else { + fprintf(stderr, "%s: only one device name may be specified\n", + program_name); + exit(2); + } + } + + /* If we got here, the argument is unknown */ + usage(program_name, 2); + } + + error = wu_fbid(dev, &fbname, &fbtype); printf("%s", fbname ? fbname : "tty"); if (print_num) { printf(" %d", fbtype); diff --git a/constype.man b/constype.man index 64fb2fe..ce2a6ea 100644 --- a/constype.man +++ b/constype.man @@ -5,10 +5,14 @@ constype \- print type of Sun console .SH SYNOPSIS .B "constype" [ -.I device_name -] [ .B \-num +] [ +.I device_name ] +.LP +.B "constype -help" +.LP +.B "constype -version" .SH DESCRIPTION .B constype prints on the standard output the Sun code for the type of display @@ -55,6 +59,18 @@ option causes to follow the type keyword with the numeric value of that type, as returned by the FBIOGATTR or FBIOGTYPE ioctl and defined in fbio.h. This is useful if the type is not recognized by the program. +.LP +The +.B \-help +option causes +.I constype +to print a usage message and exit. +.LP +The +.B \-version +option causes +.I constype +to print its version and exit. .SH "EXIT STATUS" The program exits with status 0 if it identified a known console type, 1 if the type was unknown, and 2 if the device could not be opened or |