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