From wd@teleri.net  Tue Jun 15 18:27:19 2004
Return-Path: <wd@teleri.net>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id B3ECB16A4CF
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 15 Jun 2004 18:27:19 +0000 (GMT)
Received: from mail.teleri.net (teleri.net [216.193.194.32])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 6074E43D2D
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 15 Jun 2004 18:27:19 +0000 (GMT)
	(envelope-from wd@teleri.net)
Received: by mail.teleri.net (Postfix, from userid 501)
	id 27F3F11448; Tue, 15 Jun 2004 11:27:19 -0700 (PDT)
Message-Id: <20040615182719.27F3F11448@mail.teleri.net>
Date: Tue, 15 Jun 2004 11:27:19 -0700 (PDT)
From: Chip Norkus <wd@teleri.net>
Reply-To: Chip Norkus <wd@teleri.net>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [patch] /usr/bin/indent bugfix and small additional features
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         67983
>Category:       bin
>Synopsis:       [patch] /usr/bin/indent bugfix and small additional features
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    schweikh
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jun 15 18:30:19 GMT 2004
>Closed-Date:    Sun Jul 11 19:26:55 GMT 2004
>Last-Modified:  Sun Jul 11 19:26:55 GMT 2004
>Originator:     Chip Norkus <wd@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@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:
Responsible-Changed-From-To: freebsd-bugs->schweikh 
Responsible-Changed-By: schweikh 
Responsible-Changed-When: Thu Jun 17 20:58:19 GMT 2004 
Responsible-Changed-Why:  
I will take care of this. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=67983 
State-Changed-From-To: open->patched 
State-Changed-By: schweikh 
State-Changed-When: Sun Jun 27 10:58:53 GMT 2004 
State-Changed-Why:  
Committed a slightly different version maintaining style-bug compatibility 
as requested by bde@. Thanks, Chip! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=67983 

From: Chip Norkus <wd@teleri.net>
To: freebsd-gnats-submit@FreeBSD.org, wd@teleri.net
Cc:  
Subject: Re: bin/67983: [patch] /usr/bin/indent bugfix and small additional features
Date: Mon, 28 Jun 2004 04:59:45 -0500

 --2fHTh5uZTiUOsy+g
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 
 Here's that manpage patch I promised to write. ;)  Sorry if there's
 anything wrong with it, I'm not really well-versed in manpage-ese.
 
 -chip
 --
 personal: chip norkus; renaissance hacker;        wd@teleri.net
 work:     systems engineer @ lunarpages, inc.;    chip@lunarpages.com
 info:     finger wd@teleri.net for plan or keys;  http://telekinesis.org
 
 --2fHTh5uZTiUOsy+g
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment; filename="indent.1.patch"
 
 --- /usr/src/usr.bin/indent/indent.1	Sun May 23 23:14:09 2004
 +++ indent.1	Mon Jun 28 04:47:50 2004
 @@ -62,6 +62,7 @@
  .Op Fl d Ns Ar n
  .Op Fl \&di Ns Ar n
  .Bk -words
 +.Op Fl fbs | Fl nfbs
  .Op Fl fc1 | Fl nfc1
  .Op Fl fcb | Fl nfcb
  .Ek
 @@ -80,6 +81,7 @@
  .Ek
  .Op Fl \&st
  .Op Fl troff
 +.Op Fl ut | Fl nut
  .Op Fl v | Fl \&nv
  .Sh DESCRIPTION
  The
 @@ -243,6 +245,10 @@
  .Ic \&if
  statement.  The default is
  .Fl ei  .
 +.It Fl fbs , nfbs
 +Enables (disbales) splitting the function declaration and opening brace
 +across two lines.  The default is
 +.Fl fbs  .
  .It Fl fc1 , nfc1
  Enables (disables) the formatting of comments that start in column 1.
  Often, comments whose leading `/' is in column 1 have been carefully
 @@ -366,6 +372,10 @@
  .Xr vgrind 1 .
  If the output file is not specified, the default is standard output,
  rather than formatting in place.
 +.It Fl ut , nut
 +Enables (disables) the use of tab characters in the output.  Tabs are
 +assumed to be aligned on columns divisble by 8.  The default is
 +.Fl ut  .
  .It Fl v , \&nv
  .Fl v
  turns on `verbose' mode;
 
 --2fHTh5uZTiUOsy+g--
State-Changed-From-To: patched->closed 
State-Changed-By: schweikh 
State-Changed-When: Sun Jul 11 19:26:19 GMT 2004 
State-Changed-Why:  
Committed to RELENG_4. Thanks again! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=67983 
>Unformatted:
