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