Module Name:    src
Committed By:   rillig
Date:           Sat Sep 25 21:42:43 UTC 2021

Modified Files:
        src/usr.bin/indent: args.c indent.c indent.h

Log Message:
indent: clean up argument handling

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/usr.bin/indent/args.c
cvs rdiff -u -r1.79 -r1.80 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.23 -r1.24 src/usr.bin/indent/indent.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/indent/args.c
diff -u src/usr.bin/indent/args.c:1.28 src/usr.bin/indent/args.c:1.29
--- src/usr.bin/indent/args.c:1.28	Sat Sep 25 21:20:59 2021
+++ src/usr.bin/indent/args.c	Sat Sep 25 21:42:43 2021
@@ -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 @@ static char sccsid[] = "@(#)args.c	8.1 (
 
 #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 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/
 #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 @@ void add_typedefs_from_file(const char *
 
 /*
  * 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 @@ eqin(const char *s1, const char *s2)
 }
 
 void
-set_option(char *arg)
+set_option(const char *arg)
 {
     const struct pro *p;
     const char	*param_start;
@@ -311,10 +312,7 @@ found:
 	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:

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.79 src/usr.bin/indent/indent.c:1.80
--- src/usr.bin/indent/indent.c:1.79	Sat Sep 25 20:56:53 2021
+++ src/usr.bin/indent/indent.c	Sat Sep 25 21:42:43 2021
@@ -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 @@ static char sccsid[] = "@(#)indent.c	5.1
 
 #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 @@ main_parse_command_line(int argc, char *
 	set_profile(profile_name);
 
     for (i = 1; i < argc; ++i) {
+	if (argv[i][0] == '-') {
+	    set_option(argv[i]);
+
+	} 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);
 
-	/*
-	 * 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;
-	    }
-	    errx(1, "unknown parameter: %s", argv[i]);
 	} else
-	    set_option(argv[i]);
-    }				/* end of for */
+	    errx(1, "unknown parameter: %s", argv[i]);
+    }
+
     if (input == NULL)
 	input = stdin;
     if (output == NULL) {

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.23 src/usr.bin/indent/indent.h:1.24
--- src/usr.bin/indent/indent.h:1.23	Sat Sep 25 19:49:13 2021
+++ src/usr.bin/indent/indent.h	Sat Sep 25 21:42:43 2021
@@ -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		dump_line(void);
 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);

Reply via email to