Thomas Schuerger wrote:
> You're right about that. Could you please fix this (should be around 5
> lines)?
 
> How about providing a script that checks a source file for violations
> of the GTKG style guide? ;)

Get yourself a big chunk of salt and have fun with bug fixing. For GNU awk
you'll probably need --posix (or -E?). It gives a lot of false alerts.
Some of the patterns are simply wrong but since it actually finds a good
amount of style violations it might be better than nothing. I'm too
tired to do anything better ... and it's somewhat silly anyway.

Christian
#! /bin/sh
# vi: set ts=4:

awk '
function warn(str) {
                print str
                printf("\t%s(%d): %s\n\n", FILENAME, FNR, $0)
}

/.*/ {
        if (length($0) > 80) {
                warn("Line is too long")
                next
        }
        gsub("\"[^\"]*\"", "")
        gsub("/[*].*[*]/", "")
        if (($0 ~ "/[*]") && ($0 !~ "[*]/"))
                gsub("/[*].*", "")
}

/^[ \t]*\/\*/ {
        # A comment
        next
}

/^[ \t]*\*/ {
        # Probably a comment
        next
}

/[a-zA-Z]/ {
        # Probably a definition or declaration
}

/for\(/         { keyword="for"         }
/if\(/          { keyword="if"          }
/switch\(/      { keyword="switch"      }
/while\(/       { keyword="while"       }

/(for|if|switch|while)\(/ {
        warn("Missing space between \"" keyword "\" and \"\(\"")
        next
}

/[a-zA-Z][a-zA-Z0-9]*[ ]\(/ {
        if ($0 !~ "(for|if|switch|while|return)[ ]\\(") {
                warn("Unnecessary space between identifier and \"\(\"")
                next
        }
}

/[\ \t][\-][\>]/ {
        # Spaces around . or ->
        warn("Spaces around structure access operator")
        next
}

/,[^ \t]+$/ {
        warn("Comma not followed by space")
        printf("\t%s(%d): %s\n", FILENAME, FNR, $0)
}

/[^\ \t](=[\=\-\+\*\/\&\|\^!\<\>]|&&|\|\|)[^\ \t]+$/ {
        # Missing space around operator
        warn("Missing spaces around operator")
        next
}

' ${1+"$@"}

Attachment: pgpRmkchGC8Z4.pgp
Description: PGP signature

Reply via email to