--- cstyle.pl.orig Sa Aug 21 15:58:08 2010 +++ cstyle.pl.new Sa Aug 21 22:29:28 2010 @@ -23,8 +23,10 @@ # Copyright 2008 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # +# @(#)cstyle 1.9 10/08/21 J. Schilling +# +# # @(#)cstyle 1.58 98/09/09 (from shannon) -#ident "%Z%%M% %I% %E% SMI" # # cstyle - check for some common stylistic errors. # @@ -56,7 +58,7 @@ use strict; my $usage = -"usage: cstyle [-chpvCP] [-o constructs] file ... +"usage: cstyle [-chpvCPbKB] [-o constructs] [-l #] file ... -c check continuation indentation inside functions -h perform heuristic checks that are sometimes wrong -p perform some of the more picky checks @@ -67,11 +69,15 @@ allow a comma-seperated list of optional constructs: doxygen allow doxygen-style block comments (/** /*!) splint allow splint-style lint comments (/*@ ... @*/) + -l # set maxline length (default is 80) + -b do not check for blank after cpp # + -K do not check for blank at /* */ comment bounds + -B allow /*------- box comments "; my %opts; -if (!getopts("cho:pvCP", \%opts)) { +if (!getopts("cho:pvCPl:bKB", \%opts)) { print $usage; exit 2; } @@ -82,7 +88,19 @@ my $verbose = $opts{'v'}; my $ignore_hdr_comment = $opts{'C'}; my $check_posix_types = $opts{'P'}; +my $maxlinelen = $opts{'l'}; +my $blank_after_cpp = $opts{'b'}; +my $Kommentar = $opts{'K'}; +my $Boxcomment = $opts{'B'}; +if (!$maxlinelen) { + $maxlinelen = 80; +} +# printf "linelen %d\n", $maxlinelen; + +my $toolong = sprintf("line > %d characters", $maxlinelen); +# printf "%s\n", $toolong; + my $doxygen_comments = 0; my $splint_comments = 0; @@ -317,14 +335,14 @@ # check length of line. # first, a quick check to see if there is any chance of being too long. - if (($line =~ tr/\t/\t/) * 7 + length($line) > 80) { + if (($line =~ tr/\t/\t/) * 7 + length($line) > $maxlinelen) { # yes, there is a chance. # replace tabs with spaces and check again. my $eline = $line; 1 while $eline =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e; - if (length($eline) > 80) { - err("line > 80 characters"); + if (length($eline) > $maxlinelen) { + err($toolong); } } @@ -458,7 +476,10 @@ next line; } if (/^\s*\/\*./ && !/^\s*\/\*.*\*\// && !/$hdr_comment_start/) { - err("improper first line of block comment"); + if (!$Boxcomment || + !/^\s*\/\*----------------------------------------------------------------------/) { + err("improper first line of block comment"); + } } if ($in_comment) { # still in comment, don't do further checks @@ -468,11 +489,15 @@ if ((/[^(]\/\*\S/ || /^\/\*\S/) && !(/$lint_re/ || ($splint_comments && /$splint_re/))) { - err("missing blank after open comment"); + if (!$Kommentar) { + err("missing blank after open comment"); + } } if (/\S\*\/[^)]|\S\*\/$/ && !(/$lint_re/ || ($splint_comments && /$splint_re/))) { - err("missing blank before close comment"); + if (!$Kommentar) { + err("missing blank before close comment"); + } } if (/\/\/\S/) { # C++ comments err("missing blank after start comment"); @@ -504,6 +529,13 @@ next line; } + if ($blank_after_cpp) { + if (/^#[ ]*(else|endif|include)/) { + $prev = $line; + next line; + } + } + # # delete any comments and check everything else. Note that # ".*?" is a non-greedy match, so that we don't get confused by @@ -627,7 +659,9 @@ err("preprocessor statement not in column 1"); } if (/^#\s/) { - err("blank after preprocessor #"); + if (!$blank_after_cpp) { + err("blank after preprocessor #"); + } } if (/!\s*(strcmp|strncmp|bcmp)\s*\(/) { err("don't use boolean ! with comparison functions"); --- cstyle.1.orig Sa Aug 21 21:09:27 2010 +++ cstyle.1.new Sa Aug 21 22:27:01 2010 @@ -25,7 +25,7 @@ .I cstyle \- check for some common stylistic errors in C source files .SH SYNOPSIS -\fBcstyle [-chpvCP] [-o constructs] [file...]\fP +\fBcstyle [-chpvCPbKB] [-o constructs] [-l #] [file...]\fP .LP .SH DESCRIPTION .IX "OS-Net build tools" "cstyle" "" "\fBcstyle\fP" @@ -43,6 +43,14 @@ .LP The following options are supported: .TP 4 +.B \-b +Do not check for white space after the cpp directive '#'. +This allows to have indented cpp directives that give better readability +in special for portable software. +.TP 4 +.B \-B +Allow boxed comments. This are comments that start with \fB/*-------\fP. +.TP 4 .B \-c Check continuation line indentation inside of functions. Sun's C style states that all statements must be indented to an appropriate tab stop, @@ -54,8 +62,17 @@ .TP 4 .B \-h Performs heuristic checks that are sometimes wrong. Not generally used. +.TP 4 +.B \-K +Do not check for white space at /* */ comment bounds. +This allows to tolerate code that was commented out by editor macros. .LP .TP 4 +.BI \-l " # +Set the maximum line length to #. The default line length is 80 characters. +It is recommended not to use a maximum line length above 132 characters. +.LP +.TP 4 .B \-p Performs some of the more picky checks. Includes ANSI #else and #endif rules, and tries to detect spaces after casts. Used as part of the