3 # openssl-format-source
4 # - format source tree according to OpenSSL coding style using indent
7 # openssl-format-source [-v] [-n] [file|directory] ...
9 # note: the indent options assume GNU indent v2.2.10 which was released
10 # Feb-2009 so if you have an older indent the options may not
11 # match what is expected
13 # any marked block comment blocks have to be moved to align manually after
14 # the reformatting has been completed as marking a block causes indent to
15 # not move it at all ...
18 PATH=/usr/local/bin:/bin:/usr/bin:$PATH
24 echo "usage: $0 [-v] [-n] [-c] [sourcefile|sourcedir] ..." >&2
33 # locate the indent configuration file to use which generally will be
34 # in the current directory but might be elsewhere and we want consistent
35 # use of the file so we set the env var to point directly to the file in
37 if [ -z "$INDENT_PROFILE" ]; then
38 for i in . ../openssl $HOME
40 if [ -f "$i/.indent.pro" ]; then
41 INDENT_PROFILE="$i/.indent.pro"
48 if [ -z "$INDENT_PROFILE" ]; then
49 # If at the top of the source tree, try the file in util
50 if [ -f "e_os.h" ]; then
51 f=`/bin/pwd`/util/indent.pro
58 if [ -z "$INDENT_PROFILE" ]; then
59 echo "$0: unable to locate .indent.pro file " >&2
63 # Extra arguments; for adding the comment-formatting
67 if [ "$STOPARGS" != "true" ]; then
69 --) STOPARGS="true"; continue;;
70 -n) DONT="true"; continue;;
72 echo "INDENT_PROFILE=$INDENT_PROFILE";
75 INDENT_ARGS="-fc1 -fca -cdb -sc";
81 LIST=`find "$i" -name '*.[ch]' -print`
83 if [ ! -f "$i" ]; then
84 echo "$0: source file not found: $i" >&2
92 # ignore symlinks - we only ever process the base file - so if we
93 # expand a directory tree we need to ignore any located symlinks
100 if [ "$VERBOSE" = "true" ]; then
104 if [ "$DONT" = "false" ]; then
105 tmp=$(mktemp /tmp/indent.XXXXXX)
106 trap 'rm -f "$tmp"' HUP INT TERM EXIT
109 # the list of files that indent is unable to handle correctly
110 # that we simply leave alone for manual formatting now
112 if [ "$COMMENTS" = "true" ]; then
113 # we have to mark single line comments as /*- ...*/ to stop indent
114 # messing with them, run expand then indent as usual but with the
115 # the process-comments options and then undo that marking, and then
116 # finally re-run indent without process-comments so the marked-to-
117 # be-ignored comments we did automatically end up getting moved
118 # into the right possition within the code as indent leaves marked
119 # comments entirely untouched - we appear to have no way to avoid
120 # the double processing and get the desired output
122 -e 's/(\n#[ \t]*ifdef[ \t]+__cplusplus\n[^\n]*\n#[ \t]*endif\n)/\n\n\/**INDENT-OFF**\/$1\/**INDENT-ON**\/\n/g;' \
123 -e 's/(\n\/\*\!)/\n\/**/g;' \
124 -e 's/(STACK_OF|LHASH_OF)\(([^ \t,\)]+)\) /$1_$2_ /g;' \
127 -e 's/^([ \t]*)\/\*([ \t]+.*)\*\/[ \t]*$/if (length("$1$2")<75) {$c="-"}else{$c=""}; "$1\/*$c$2*\/"/e;' \
128 -e 's/^\/\* ((Copyright|=|----).*)$/\/*-$1/;' \
129 -e 's/^((DECLARE|IMPLEMENT)_(EXTERN_ASN1|ASN1|ADB|STACK_OF).*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
130 -e 's/^([ \t]*(make_dh|make_dh_bn|make_rfc5114_td)\(.*\)[ \t,]*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
131 -e 's/^(ASN1_ADB_TEMPLATE\(.*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
132 -e 's/^((ASN1|ADB)_.*_END\(.*[\){=,;]+[ \t]*)$/$1\n\/**INDENT-ON**\//;' \
133 -e '/ASN1_ITEM_(ref|ptr|rptr)/ || s/^((ASN1|ADB)_[^\*]*[){=,]+[ \t]*)$/\/**INDENT-OFF**\/\n$1/;' \
134 -e 's/^(} (ASN1|ADB)_[^\*]*[\){=,;]+)$/$1\n\/**INDENT-ON**\//;' \
136 expand | indent $INDENT_ARGS | \
138 -e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \
139 -e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \
142 -e 's/\/\*\*INDENT-(ON|OFF)\*\*\/\n//g;' \
144 -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_( |\/)/$1($2)$3/g;' \
145 -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_$/$1($2)/g;' \
148 expand "$j" | indent $INDENT_ARGS > "$tmp"