bin/67983: [patch] /usr/bin/indent bugfix and small additional features

Chip Norkus wd at teleri.net
Tue Jun 15 18:30:28 GMT 2004


>Number:         67983
>Category:       bin
>Synopsis:       [patch] /usr/bin/indent bugfix and small additional features
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jun 15 18:30:19 GMT 2004
>Closed-Date:
>Last-Modified:
>Originator:     Chip Norkus <wd at teleri.net>
>Release:        FreeBSD 5.2-CURRENT i386
>Organization:
>Environment:
System: FreeBSD teleri.net 5.2-CURRENT FreeBSD 5.2-CURRENT #4: Fri May 7 12:47:21 CDT 2004 root at teleri.net:/usr/obj/usr/src/sys/va-2u i386


>Description:
        When invoked with an indentation size that is not a multiple of 8
        indent will format variable declarations improperly.  Additionally,
        there is no way to instruct indent to produce function declarations
        with the opening brace on the same line as the declaration of
        arguments.  Finally, it is not possible to instruct indent to use
        all spaces and no tabs (a feature which exists in GNU's indent).

>How-To-Repeat:
        To reproduce the first issue one only needs to run indent on code
        using a 'non-standard' indent depth, for example 'indent -i4' will
        generate incorrectly formatted declaration blocks.

>Fix:
        The attached patch addresses all three mentioned issues.  It fixes
        the first (the bug) and adds new options for the second and third.
        The options -fbs/-nfbs control the behavior of opening brace
        placement in function declarations, and the options -ut/-nut control
        the behavior of tab-usage.  In both cases the pre-existing behavior
        has been left as the default.

        I used the script at http://telekiensis.org/indent/test.sh to verify
        that the patched indent, when run with the default option set,
        produces code identical to that produced by the existing indent.
        Furthermore I believe the bugfix to be feature correct, and that the
        two additional features function with no deficencies.

        There is no update for indent.1 in this patch because I am totally
        unfamiliar with manual page syntax, I apologise for that.

--- indent.patch begins here ---
diff -u /usr/src/usr.bin/indent/args.c /home/wd/projects/indent/args.c
--- /usr/src/usr.bin/indent/args.c	Tue Mar 23 13:33:16 2004
+++ /home/wd/projects/indent/args.c	Thu Jun 10 19:01:10 2004
@@ -111,6 +111,7 @@
     {"eei", PRO_BOOL, false, ON, &extra_expression_indent},
     {"ei", PRO_BOOL, true, ON, &ps.else_if},
     {"fbc", PRO_FONT, 0, 0, (int *) &blkcomf},
+    {"fbs", PRO_BOOL, true, ON, &function_brace_split},
     {"fbx", PRO_FONT, 0, 0, (int *) &boxcomf},
     {"fb", PRO_FONT, 0, 0, (int *) &bodyf},
     {"fc1", PRO_BOOL, true, ON, &format_col1_comments},
@@ -136,6 +137,7 @@
     {"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl},
     {"neei", PRO_BOOL, false, OFF, &extra_expression_indent},
     {"nei", PRO_BOOL, true, OFF, &ps.else_if},
+    {"nfbs", PRO_BOOL, true, OFF, &function_brace_split},
     {"nfc1", PRO_BOOL, true, OFF, &format_col1_comments},
     {"nfcb", PRO_BOOL, true, OFF, &format_block_comments},
     {"nip", PRO_BOOL, true, OFF, &ps.indent_parameters},
@@ -146,6 +148,7 @@
     {"nps", PRO_BOOL, false, OFF, &pointer_as_binop},
     {"nsc", PRO_BOOL, true, OFF, &star_comment_cont},
     {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines},
+    {"nut", PRO_BOOL, true, OFF, &use_tabs},
     {"nv", PRO_BOOL, false, OFF, &verbose},
     {"pcs", PRO_BOOL, false, ON, &proc_calls_space},
     {"psl", PRO_BOOL, true, ON, &procnames_start_line},
@@ -154,6 +157,7 @@
     {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines},
     {"st", PRO_SPECIAL, 0, STDIN, 0},
     {"troff", PRO_BOOL, false, ON, &troff},
+    {"ut", PRO_BOOL, true, ON, &use_tabs},
     {"v", PRO_BOOL, false, ON, &verbose},
     /* whew! */
     {0, 0, 0, 0, 0}
diff -u /usr/src/usr.bin/indent/indent.c /home/wd/projects/indent/indent.c
--- /usr/src/usr.bin/indent/indent.c	Tue Feb 17 02:33:36 2004
+++ /home/wd/projects/indent/indent.c	Fri Jun 11 12:21:24 2004
@@ -91,7 +91,7 @@
     int         squest;		/* when this is positive, we have seen a ?
 				 * without the matching : in a <c>?<s>:<s>
 				 * construct */
-    int		use_tabs;	/* true if using tabs to indent to var name */
+    int		loc_use_tabs;	/* true if using tabs to indent to var name */
     const char *t_ptr;		/* used for copying tokens */
     int         type_code;	/* the type of token, returned by lexi */
 
@@ -765,8 +765,13 @@
 		}
 		else if (ps.in_parameter_declaration && !ps.in_or_st) {
 		    ps.i_l_follow = 0;
-		    dump_line();
-		    ps.want_blank = false;
+		    if (function_brace_split) {
+			/* dump the line prior to the brace ... */
+			dump_line();
+			ps.want_blank = false;
+		    } else
+			/* add a space between the decl and brace */
+			ps.want_blank = true;
 		}
 	    }
 	    if (ps.in_parameter_declaration)
@@ -914,11 +919,11 @@
 	    if (ps.ind_level == 0 || ps.dec_nest > 0) {
 		/* global variable or struct member in local variable */
 		dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
-		use_tabs = ps.decl_indent > 0;
+		loc_use_tabs = (use_tabs ? ps.decl_indent > 0 : 0);
 	    } else {
 		/* local variable */
 		dec_ind = ps.local_decl_indent > 0 ? ps.local_decl_indent : i;
-		use_tabs = ps.local_decl_indent > 0;
+		loc_use_tabs = (use_tabs ? ps.local_decl_indent > 0 : 0);
 	    }
 	    goto copy_id;
 
@@ -936,17 +941,31 @@
 			    e_code += strlen(e_code);
 			} else {
 			    int pos, startpos;
+			    int loc_dec_ind;
 
+			    /* In order to get the tab math right for
+			     * indentations that are not multiples of 8 we
+			     * need to modify both startpos and dec_ind
+			     * (loc_dec_ind) here by eight minus the
+			     * remainder of the current starting column
+			     * divided by eight.  This seems to be a
+			     * properly working fix. */
 			    startpos = e_code - s_code;
+			    loc_dec_ind = dec_ind;
 			    pos = startpos;
-			    if (use_tabs) {
-				while ((pos & ~7) + 8 <= dec_ind) {
+			    if ((ps.ind_level * ps.ind_size) % 8 != 0) {
+				pos += (ps.ind_level * ps.ind_size) % 8;
+				loc_dec_ind += (ps.ind_level * ps.ind_size) % 8;
+			    }
+                            
+			    if (loc_use_tabs) {
+				while ((pos & ~7) + 8 <= loc_dec_ind) {
 				    CHECK_SIZE_CODE;
 				    *e_code++ = '\t';
 				    pos = (pos & ~7) + 8;
 				}
 			    }
-			    while (pos < dec_ind) {
+			    while (pos < loc_dec_ind) {
 				CHECK_SIZE_CODE;
 				*e_code++ = ' ';
 				pos++;
diff -u /usr/src/usr.bin/indent/indent_globs.h /home/wd/projects/indent/indent_globs.h
--- /usr/src/usr.bin/indent/indent_globs.h	Tue Feb 17 02:33:36 2004
+++ /home/wd/projects/indent/indent_globs.h	Thu Jun 10 21:43:42 2004
@@ -199,6 +199,10 @@
 					 * indented an extra tab stop so that
 					 * they don't conflict with the code
 					 * that follows */
+int	    function_brace_split;	/* split function declaration and
+					 * brace onto separate lines? */
+int	    use_tabs;			/* Set true to use tabs for spacing,
+					 * false uses all spaces. */
 
 /* -troff font state information */
 
diff -u /usr/src/usr.bin/indent/io.c /home/wd/projects/indent/io.c
--- /usr/src/usr.bin/indent/io.c	Tue Feb 17 02:33:36 2004
+++ /home/wd/projects/indent/io.c	Thu Jun 10 21:44:28 2004
@@ -472,11 +472,13 @@
 	if (current >= target)
 	    return (current);	/* line is already long enough */
 	curr = current;
-	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
-	    putc('\t', output);
-	    curr = tcur;
-	}
-	while (curr++ < target)
+        if (use_tabs) {
+            while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
+                putc('\t', output);
+                curr = tcur;
+            }
+        }
+        while (curr++ < target)
 	    putc(' ', output);	/* pad with final blanks */
     }
     return (target);
--- indent.patch ends here ---


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list