make update
[openssl.git] / util / openssl-format-source
1 #!/bin/sh
2 #
3 # openssl-format-source 
4 # - format source tree according to OpenSSL coding style using indent
5 #
6 # usage:
7 #   openssl-format-source [-v] [-n] [file|directory] ...
8 #
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
12 #
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 ...
16 #
17
18 PATH=/usr/local/bin:/bin:/usr/bin:$PATH
19 export PATH
20 HERE="`dirname $0`"
21
22 set -e
23
24 INDENT=indent
25 uname -s | grep BSD > /dev/null && type gindent > /dev/null 2>&1 && INDENT=gindent
26
27 if [ $# -eq 0 ]; then
28   echo "usage: $0 [-v] [-n] [-c] [sourcefile|sourcedir] ..." >&2
29   exit 1
30 fi
31
32 VERBOSE=false
33 DONT=false
34 STOPARGS=false
35 COMMENTS=false
36 CHANGED=false
37 DEBUG=""
38
39 # for this exercise, we want to force the openssl style, so we roll
40 # our own indent profile, which is at a well known location
41 INDENT_PROFILE="$HERE/indent.pro"
42 export INDENT_PROFILE
43 if [ ! -f "$INDENT_PROFILE" ]; then
44   echo "$0: unable to locate the openssl indent.pro file" >&2
45   exit 1
46 fi
47
48 # Extra arguments; for adding the comment-formatting
49 INDENT_ARGS=""
50 for i 
51 do
52   if [ "$STOPARGS" != "true" ]; then
53     case $i in
54       --) STOPARGS="true"; continue;;
55       -n) DONT="true"; continue;;
56       -v) VERBOSE="true"; 
57           echo "INDENT_PROFILE=$INDENT_PROFILE";
58           continue;;
59       -c) COMMENTS="true"; 
60           INDENT_ARGS="-fc1 -fca -cdb -sc"; 
61           continue;;
62       -nc) COMMENTS="true";
63           continue;;
64       -d) DEBUG='eval tee "$j.pre" |'
65           continue;;
66     esac
67   fi
68
69   if [ -d "$i" ]; then
70     LIST=`find "$i" -name '*.[ch]' -print`
71   else 
72     if [ ! -f "$i" ]; then
73       echo "$0: source file not found: $i" >&2
74       exit 1
75     fi
76     LIST="$i"
77   fi
78   
79   for j in $LIST
80   do
81     # ignore symlinks - we only ever process the base file - so if we
82     # expand a directory tree we need to ignore any located symlinks
83     if [ -d "$i" ]; then
84       if [ -h "$j" ]; then
85         continue;
86       fi
87     fi
88
89     if [ "$DONT" = "false" ]; then
90       tmp=$(mktemp /tmp/indent.XXXXXX)
91       trap 'rm -f "$tmp"' HUP INT TERM EXIT
92
93       case `basename $j` in 
94         # the list of files that indent is unable to handle correctly
95         # that we simply leave alone for manual formatting now
96         obj_dat.h|aes_core.c|aes_x86core.c|ecp_nistz256.c)
97           echo "skipping $j"
98           ;;
99         *)
100           if [ "$COMMENTS" = "true" ]; then
101             # we have to mark single line comments as /*- ...*/ to stop indent
102             # messing with them, run expand then indent as usual but with the
103             # the process-comments options and then undo that marking, and then 
104             # finally re-run indent without process-comments so the marked-to-
105             # be-ignored comments we did automatically end up getting moved 
106             # into the right possition within the code as indent leaves marked 
107             # comments entirely untouched - we appear to have no way to avoid 
108             # the double processing and get the desired output
109             cat "$j" | \
110             expand | \
111             perl -0 -np \
112               -e 's/(\n#[ \t]*ifdef[ \t]+__cplusplus\n[^\n]*\n#[ \t]*endif\n)/\n\/**INDENT-OFF**\/$1\/**INDENT-ON**\/\n/g;' \
113               -e 's/(\n\/\*\!)/\n\/**/g;' \
114               -e 's/(STACK_OF|LHASH_OF)\(([^ \t,\)]+)\)( |\n)/$1_$2_$3/g;' \
115               | \
116             perl -np \
117               -e 's/^([ \t]*)\/\*([ \t]+.*)\*\/[ \t]*$/if (length("$1$2")<75) {$c="-"}else{$c=""}; "$1\/*$c$2*\/"/e;' \
118               -e 's/^\/\* ((Copyright|=|----).*)$/\/*-$1/;' \
119               -e 's/^((DECLARE|IMPLEMENT)_(EXTERN_ASN1|ASN1|ADB|STACK_OF|PKCS12_STACK_OF).*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
120               -e 's/^([ \t]*(make_dh|make_dh_bn|make_rfc5114_td)\(.*\)[ \t,]*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
121               -e 's/^(ASN1_ADB_TEMPLATE\(.*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
122               -e 's/^((ASN1|ADB)_.*_(end|END)\(.*[\){=,;]+[ \t]*)$/$1\n\/**INDENT-ON**\//;' \
123               -e '/ASN1_(ITEM_ref|ITEM_ptr|ITEM_rptr|PCTX)/ || s/^((ASN1|ADB)_[^\*]*[){=,]+[ \t]*)$/\/**INDENT-OFF**\/\n$1/;' \
124               -e 's/^(} (ASN1|ADB)_[^\*]*[\){=,;]+)$/$1\n\/**INDENT-ON**\//;' \
125               | \
126               $DEBUG $INDENT $INDENT_ARGS | \
127               perl -np \
128                 -e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \
129                 -e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \
130               | $INDENT | \
131               perl -0 -np \
132                 -e 's/\/\*\*INDENT-(ON|OFF)\*\*\/\n//g;' \
133               | perl -np \
134                 -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_( |\/)/$1($2)$3/g;' \
135                 -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_$/$1($2)/g;' \
136               | perl "$HERE"/su-filter.pl \
137               > "$tmp"
138           else
139             expand "$j" | $INDENT $INDENT_ARGS > "$tmp"
140           fi;
141           if cmp -s "$tmp" "$j"; then
142             if [ "$VERBOSE" = "true" ]; then
143               echo "$j unchanged"
144             fi
145             rm "$tmp"
146           else
147             if [ "$VERBOSE" = "true" ]; then
148               echo "$j changed"
149             fi
150             CHANGED=true
151             mv "$tmp" "$j"
152           fi
153           ;;
154       esac
155     fi
156   done
157 done
158
159
160 if [ "$VERBOSE" = "true" ]; then
161   echo
162   if [ "$CHANGED" = "true" ]; then
163     echo "SOURCE WAS MODIFIED"
164   else
165     echo "SOURCE WAS NOT MODIFIED"
166   fi
167 fi