Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: clean up argument handling



details:   https://anonhg.NetBSD.org/src/rev/731c863c5a8e
branches:  trunk
changeset: 987403:731c863c5a8e
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Sep 25 21:42:43 2021 +0000

description:
indent: clean up argument handling

No functional change.

diffstat:

 usr.bin/indent/args.c   |  22 ++++++++++------------
 usr.bin/indent/indent.c |  47 +++++++++++++++++++++--------------------------
 usr.bin/indent/indent.h |   4 ++--
 3 files changed, 33 insertions(+), 40 deletions(-)

diffs (155 lines):

diff -r 83c30cc81449 -r 731c863c5a8e usr.bin/indent/args.c
--- a/usr.bin/indent/args.c     Sat Sep 25 21:26:03 2021 +0000
+++ b/usr.bin/indent/args.c     Sat Sep 25 21:42:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: args.c,v 1.28 2021/09/25 21:20:59 rillig Exp $ */
+/*     $NetBSD: args.c,v 1.29 2021/09/25 21:42:43 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: args.c,v 1.28 2021/09/25 21:20:59 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.29 2021/09/25 21:42:43 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
 #endif
@@ -56,6 +56,7 @@
 #include <ctype.h>
 #include <err.h>
 #include <limits.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -102,14 +103,14 @@
 
 /*
  * N.B.: because of the way the table here is scanned, options whose names are
- * substrings of other options must occur later; that is, with -lp vs -l, -lp
- * must be first.
+ * a prefix of other options must occur later; that is, with -lp vs -l, -lp
+ * must be first and -l must be last.
  */
-const struct pro {
+static const struct pro {
     const char  p_name[9];     /* name, e.g. "bl", "cli" */
-    int         p_type;                /* type (int, bool, special) */
+    uint8_t     p_type;                /* type (int, bool, special) */
     int         p_special;     /* depends on type */
-    void        *p_obj;                /* the associated variable */
+    void        *p_obj;                /* the associated variable (bool, int) */
 }           pro[] = {
     special_option("T", KEY),
     special_option("U", KEY_FILE),
@@ -256,7 +257,7 @@
 }
 
 void
-set_option(char *arg)
+set_option(const char *arg)
 {
     const struct pro *p;
     const char *param_start;
@@ -311,10 +312,7 @@
        break;
 
     case PRO_BOOL:
-       if (p->p_special == OFF)
-           *(bool *)p->p_obj = false;
-       else
-           *(bool *)p->p_obj = true;
+       *(bool *)p->p_obj = p->p_special == ON;
        break;
 
     case PRO_INT:
diff -r 83c30cc81449 -r 731c863c5a8e usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Sep 25 21:26:03 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Sep 25 21:42:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.79 2021/09/25 20:56:53 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.80 2021/09/25 21:42:43 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.79 2021/09/25 20:56:53 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.80 2021/09/25 21:42:43 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -427,32 +427,27 @@
        set_profile(profile_name);
 
     for (i = 1; i < argc; ++i) {
+       if (argv[i][0] == '-') {
+           set_option(argv[i]);
 
-       /*
-        * look thru args (if any) for changes to defaults
-        */
-       if (argv[i][0] != '-') {/* no flag on parameter */
-           if (input == NULL) {        /* we must have the input file */
-               in_name = argv[i];      /* remember name of input file */
-               input = fopen(in_name, "r");
-               if (input == NULL)      /* check for open error */
-                       err(1, "%s", in_name);
-               continue;
-           } else if (output == NULL) {        /* we have the output file */
-               out_name = argv[i];     /* remember name of output file */
-               if (strcmp(in_name, out_name) == 0) {   /* attempt to overwrite
-                                                        * the file */
-                   errx(1, "input and output files must be different");
-               }
-               output = fopen(out_name, "w");
-               if (output == NULL)     /* check for create error */
-                       err(1, "%s", out_name);
-               continue;
-           }
+       } else if (input == NULL) {
+           in_name = argv[i];
+           input = fopen(in_name, "r");
+           if (input == NULL)
+               err(1, "%s", in_name);
+
+       } else if (output == NULL) {
+           out_name = argv[i];
+           if (strcmp(in_name, out_name) == 0)
+               errx(1, "input and output files must be different");
+           output = fopen(out_name, "w");
+           if (output == NULL)
+               err(1, "%s", out_name);
+
+       } else
            errx(1, "unknown parameter: %s", argv[i]);
-       } else
-           set_option(argv[i]);
-    }                          /* end of for */
+    }
+
     if (input == NULL)
        input = stdin;
     if (output == NULL) {
diff -r 83c30cc81449 -r 731c863c5a8e usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat Sep 25 21:26:03 2021 +0000
+++ b/usr.bin/indent/indent.h   Sat Sep 25 21:42:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.23 2021/09/25 19:49:13 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.24 2021/09/25 21:42:43 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -64,7 +64,7 @@
 void           fill_buffer(void);
 void           parse(token_type);
 void           process_comment(void);
-void           set_option(char *);
+void           set_option(const char *);
 void           set_profile(const char *);
 
 void           *xmalloc(size_t);



Home | Main Index | Thread Index | Old Index