Module Name:    src
Committed By:   rillig
Date:           Sun May 14 11:29:23 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: t_errors.sh
        src/usr.bin/indent: args.c indent.c indent.h io.c lexi.c

Log Message:
indent: miscellaneous cleanups


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/tests/usr.bin/indent/t_errors.sh
cvs rdiff -u -r1.75 -r1.76 src/usr.bin/indent/args.c
cvs rdiff -u -r1.264 -r1.265 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.128 -r1.129 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.161 -r1.162 src/usr.bin/indent/io.c
cvs rdiff -u -r1.180 -r1.181 src/usr.bin/indent/lexi.c

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

Modified files:

Index: src/tests/usr.bin/indent/t_errors.sh
diff -u src/tests/usr.bin/indent/t_errors.sh:1.27 src/tests/usr.bin/indent/t_errors.sh:1.28
--- src/tests/usr.bin/indent/t_errors.sh:1.27	Sat May 13 16:40:18 2023
+++ src/tests/usr.bin/indent/t_errors.sh	Sun May 14 11:29:23 2023
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_errors.sh,v 1.27 2023/05/13 16:40:18 rillig Exp $
+# $NetBSD: t_errors.sh,v 1.28 2023/05/14 11:29:23 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -181,15 +181,15 @@ atf_test_case 'option_special_missing_pa
 option_special_missing_param_body()
 {
 	expect_error \
-	    'indent: Command line: ``-cli'\'\'' requires an argument' \
+	    'indent: Command line: option "-cli" requires an argument' \
 	    -cli
 
 	expect_error \
-	    'indent: Command line: ``-T'\'\'' requires an argument' \
+	    'indent: Command line: option "-T" requires an argument' \
 	    -T
 
 	expect_error \
-	    'indent: Command line: ``-U'\'\'' requires an argument' \
+	    'indent: Command line: option "-U" requires an argument' \
 	    -U
 }
 

Index: src/usr.bin/indent/args.c
diff -u src/usr.bin/indent/args.c:1.75 src/usr.bin/indent/args.c:1.76
--- src/usr.bin/indent/args.c:1.75	Sat May 13 13:48:54 2023
+++ src/usr.bin/indent/args.c	Sun May 14 11:29:23 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: args.c,v 1.75 2023/05/13 13:48:54 rillig Exp $	*/
+/*	$NetBSD: args.c,v 1.76 2023/05/14 11:29:23 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.75 2023/05/13 13:48:54 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.76 2023/05/14 11:29:23 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
 #endif
@@ -125,6 +125,7 @@ static const struct pro {
     /* "U" is special */
     bool_options("ut", use_tabs),
     bool_options("v", verbose),
+    /* "-version" is special */
 };
 
 
@@ -138,7 +139,7 @@ add_typedefs_from_file(const char *fname
 	fprintf(stderr, "indent: cannot open file %s\n", fname);
 	exit(1);
     }
-    while ((fgets(line, BUFSIZ, file)) != NULL) {
+    while ((fgets(line, sizeof(line), file)) != NULL) {
 	/* Only keep the first word of the line. */
 	line[strcspn(line, " \t\n\r")] = '\0';
 	register_typename(line);
@@ -157,7 +158,7 @@ set_special_option(const char *arg, cons
     }
 
     if (arg[0] == 'P' || strcmp(arg, "npro") == 0)
-	return true;
+	return true;		/* see main_load_profiles */
 
     if (strncmp(arg, "cli", 3) == 0) {
 	arg_end = arg + 3;
@@ -198,7 +199,7 @@ set_special_option(const char *arg, cons
     return false;
 
 need_arg:
-    errx(1, "%s: ``-%.*s'' requires an argument",
+    errx(1, "%s: option \"-%.*s\" requires an argument",
 	option_source, (int)(arg_end - arg), arg);
     /* NOTREACHED */
 }
@@ -277,7 +278,7 @@ load_profile(const char *fname, bool mus
 		comment_ch = ch == '/' && comment_ch == '*' ? -1 : ch;
 	    } else if (ch_isspace((char)ch)) {
 		break;
-	    } else if (n >= array_length(buf) - 5) {
+	    } else if (n >= array_length(buf) - 2) {
 		errx(1, "buffer overflow in %s, starting with '%.10s'",
 		    fname, buf);
 	    } else

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.264 src/usr.bin/indent/indent.c:1.265
--- src/usr.bin/indent/indent.c:1.264	Sat May 13 17:54:34 2023
+++ src/usr.bin/indent/indent.c	Sun May 14 11:29:23 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.264 2023/05/13 17:54:34 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.265 2023/05/14 11:29:23 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.264 2023/05/13 17:54:34 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.265 2023/05/14 11:29:23 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -330,9 +330,9 @@ main_parse_command_line(int argc, char *
 	opt.comment_column = 2;	/* don't put normal comments before column 2 */
     if (opt.block_comment_max_line_length <= 0)
 	opt.block_comment_max_line_length = opt.max_line_length;
-    if (opt.local_decl_indent < 0)	/* if not specified by user, set this */
+    if (opt.local_decl_indent < 0)
 	opt.local_decl_indent = opt.decl_indent;
-    if (opt.decl_comment_column <= 0)	/* if not specified by user, set this */
+    if (opt.decl_comment_column <= 0)
 	opt.decl_comment_column = opt.ljust_decl
 	    ? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
 	    : opt.comment_column;
@@ -355,8 +355,7 @@ main_prepare_parsing(void)
 	    break;
     }
 
-    if (ind >= opt.indent_size)
-	ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
+    ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
 }
 
 static void
@@ -443,6 +442,17 @@ stay_in_line:
 }
 
 static bool
+is_function_pointer_declaration(void)
+{
+    return token.s[0] == '('
+	&& ps.in_decl
+	&& !ps.block_init
+	&& !ps.decl_indent_done
+	&& !ps.is_function_definition
+	&& ps.line_start_nparen == 0;
+}
+
+static bool
 want_blank_before_lparen(void)
 {
     if (!ps.want_blank)
@@ -469,10 +479,7 @@ process_lparen_or_lbracket(void)
 	ps.nparen--;
     }
 
-    if (token.s[0] == '(' && ps.in_decl
-	&& !ps.block_init && !ps.decl_indent_done &&
-	!ps.is_function_definition && ps.line_start_nparen == 0) {
-	/* function pointer declarations */
+    if (is_function_pointer_declaration()) {
 	code_add_decl_indent(ps.decl_ind, ps.tabs_to_var);
 	ps.decl_indent_done = true;
     } else if (want_blank_before_lparen())
@@ -499,7 +506,6 @@ process_lparen_or_lbracket(void)
 	ps.init_or_struct = false;
     }
 
-    /* parenthesized type following sizeof or offsetof is not a cast */
     if (ps.prev_token == lsym_offsetof || ps.prev_token == lsym_sizeof)
 	ps.paren[ps.nparen - 1].no_cast = true;
 }
@@ -854,14 +860,6 @@ process_ident(lexer_symbol lsym)
 }
 
 static void
-copy_token(void)
-{
-    if (ps.want_blank)
-	buf_add_char(&code, ' ');
-    buf_add_buf(&code, &token);
-}
-
-static void
 process_period(void)
 {
     if (code.e > code.s && code.e[-1] == ',')
@@ -888,11 +886,10 @@ process_comma(void)
     if (ps.nparen == 0) {
 	if (ps.block_init_level <= 0)
 	    ps.block_init = false;
-	int varname_len = 8;	/* rough estimate for the length of a typical
-				 * variable name */
+	int typical_varname_length = 8;
 	if (break_comma && (opt.break_after_comma ||
 		ind_add(compute_code_indent(), code.s, code.e)
-		>= opt.max_line_length - varname_len))
+		>= opt.max_line_length - typical_varname_length))
 	    ps.force_nl = true;
     }
 }
@@ -903,12 +900,10 @@ read_preprocessing_line(void)
 {
     enum {
 	PLAIN, STR, CHR, COMM
-    } state;
+    } state = PLAIN;
 
     buf_add_char(&lab, '#');
 
-    state = PLAIN;
-
     while (ch_isblank(inp_peek()))
 	buf_add_char(&lab, inp_next());
 
@@ -1036,12 +1031,12 @@ main_loop(void)
     for (;;) {			/* loop until we reach eof */
 	lexer_symbol lsym = lexi();
 
-	if (lsym == lsym_if && ps.prev_token == lsym_else && opt.else_if)
-	    ps.force_nl = false;
-
 	if (lsym == lsym_eof)
 	    return process_eof();
 
+	if (lsym == lsym_if && ps.prev_token == lsym_else && opt.else_if)
+	    ps.force_nl = false;
+
 	if (lsym == lsym_newline || lsym == lsym_form_feed ||
 		lsym == lsym_preprocessing)
 	    ps.force_nl = false;
@@ -1153,7 +1148,9 @@ main_loop(void)
 	case lsym_return:
 	    process_ident(lsym);
     copy_token:
-	    copy_token();
+	    if (ps.want_blank)
+		buf_add_char(&code, ' ');
+	    buf_add_buf(&code, &token);
 	    if (lsym != lsym_funcname)
 		ps.want_blank = true;
 	    break;

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.128 src/usr.bin/indent/indent.h:1.129
--- src/usr.bin/indent/indent.h:1.128	Sat May 13 15:34:22 2023
+++ src/usr.bin/indent/indent.h	Sun May 14 11:29:23 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.128 2023/05/13 15:34:22 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.129 2023/05/14 11:29:23 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -188,13 +188,12 @@ extern struct options {
 				 * "for (e; e; e)" should be indented an extra
 				 * tab stop so that they don't conflict with
 				 * the code that follows */
-    bool else_if;		/* whether else-if pairs should be handled
-				 * specially */
+    bool else_if;		/* whether else-if pairs use the same line */
     bool function_brace_split;	/* split function declaration and brace onto
 				 * separate lines */
     bool format_col1_comments;	/* If comments which start in column 1 are to
-				 * be magically reformatted (just like
-				 * comments that begin in later columns) */
+				 * be reformatted (just like comments that
+				 * begin in later columns) */
     bool format_block_comments;	/* whether comments beginning with '/ * \n'
 				 * are to be reformatted */
     bool indent_parameters;
@@ -247,11 +246,26 @@ typedef struct paren_level_props {
 				 * form a type cast */
 } paren_level_props;
 
+/*
+ * The parser state determines the layout of the formatted text.
+ *
+ * In a function body, the number of block braces determines the indentation
+ * of statements and declarations.
+ *
+ * In a statement, the number of parentheses or brackets determines the
+ * indentation of follow-up lines.
+ *
+ * In an expression, the token type determine whether to put spaces around.
+ *
+ * In a source file, the types of line determine the vertical spacing, such as
+ * around preprocessing directives or function bodies, or above block
+ * comments.
+ */
 extern struct parser_state {
     lexer_symbol prev_token;	/* the previous token, but never comment,
 				 * newline or preprocessing line */
     bool curr_col_1;		/* whether the current token started in column
-				 * 1 of the unformatted input */
+				 * 1 of the original input */
     bool next_col_1;
     bool next_unary;		/* whether the following operator should be
 				 * unary; is used in declarations for '*', as
@@ -263,9 +277,8 @@ extern struct parser_state {
 				 * prefixed by a blank. (Said prefixing is
 				 * ignored in some cases.) */
 
-    bool force_nl;		/* when true, the following token goes to the
-				 * next line, unless it is a '{' and
-				 * opt.brace_same_line is set. */
+    bool force_nl;		/* whether the next token goes to a new
+				 * line */
 
     int line_start_nparen;	/* the number of parentheses or brackets that
 				 * were already open at the beginning of the
@@ -286,13 +299,12 @@ extern struct parser_state {
     int com_ind;		/* indentation of the current comment */
 
     bool block_init;		/* whether inside a block initialization */
-    int block_init_level;	/* The level of brace nesting in an
+    int block_init_level;	/* the level of brace nesting in an
 				 * initialization */
-    bool init_or_struct;	/* whether there has been a declarator (e.g.
-				 * int or char) and no left parenthesis since
-				 * the last semicolon. When true, a '{' is
-				 * starting a structure definition or an
-				 * initialization list */
+    bool init_or_struct;	/* whether there has been a type name and no
+				 * left parenthesis since the last semicolon.
+				 * When true, a '{' starts a structure
+				 * definition or an initialization list */
 
     int ind_level;		/* the indentation level for the line that is
 				 * currently prepared for output */
@@ -332,10 +344,10 @@ extern struct parser_state {
     bool in_stmt_cont;		/* whether the next line should have an extra
 				 * indentation level because we are in the
 				 * middle of a statement */
-    bool is_case_label;		/* 'case' and 'default' labels are indented
-				 * differently from regular labels */
     bool seen_case;		/* set to true when we see a 'case', so we
 				 * know what to do with the following colon */
+    bool is_case_label;		/* 'case' and 'default' labels are indented
+				 * differently from regular labels */
 
     int tos;			/* pointer to top of stack */
     parser_symbol s_sym[STACKSIZE];
@@ -390,7 +402,7 @@ void inp_skip(void);
 char inp_next(void);
 
 lexer_symbol lexi(void);
-void diag(int, const char *, ...)__printflike(2, 3);
+void diag(int, const char *, ...) __printflike(2, 3);
 void output_line(void);
 void output_line_ff(void);
 void inp_read_line(void);

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.161 src/usr.bin/indent/io.c:1.162
--- src/usr.bin/indent/io.c:1.161	Sat May 13 17:20:41 2023
+++ src/usr.bin/indent/io.c	Sun May 14 11:29:23 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.161 2023/05/13 17:20:41 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.162 2023/05/14 11:29:23 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,14 +43,13 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.161 2023/05/13 17:20:41 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.162 2023/05/14 11:29:23 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
 
 #include <assert.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
 #include "indent.h"
@@ -350,19 +349,19 @@ output_line_ff(void)
 static int
 compute_code_indent_lineup(int base_ind)
 {
-    int ti = paren_indent;
-    int overflow = ind_add(ti, code.s, code.e) - opt.max_line_length;
+    int ind = paren_indent;
+    int overflow = ind_add(ind, code.s, code.e) - opt.max_line_length;
     if (overflow < 0)
-	return ti;
+	return ind;
 
     if (ind_add(base_ind, code.s, code.e) < opt.max_line_length) {
-	ti -= overflow + 2;
-	if (ti > base_ind)
-	    return ti;
+	ind -= overflow + 2;
+	if (ind > base_ind)
+	    return ind;
 	return base_ind;
     }
 
-    return ti;
+    return ind;
 }
 
 int

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.180 src/usr.bin/indent/lexi.c:1.181
--- src/usr.bin/indent/lexi.c:1.180	Sun May 14 11:02:53 2023
+++ src/usr.bin/indent/lexi.c	Sun May 14 11:29:23 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.180 2023/05/14 11:02:53 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.181 2023/05/14 11:29:23 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c	8.1 (
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.180 2023/05/14 11:02:53 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.181 2023/05/14 11:29:23 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -53,10 +53,7 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/
 
 #include "indent.h"
 
-/*
- * While inside lexi_alnum, this constant just marks a type, independently of
- * the parentheses level.
- */
+/* In lexi_alnum, this constant marks a type, independent of parentheses. */
 #define lsym_type lsym_type_outside_parentheses
 
 /* must be sorted alphabetically, is used in binary search */

Reply via email to