]> git.ipfire.org Git - thirdparty/openssl.git/blob - util/openssl-format-source
c1aada7d372d04c1264664218c9de31ec199c5dd
[thirdparty/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
21 set -e
22
23 if [ $# -eq 0 ]; then
24 echo "usage: $0 [-v] [-n] [-c] [sourcefile|sourcedir] ..." >&2
25 exit 1
26 fi
27
28 VERBOSE=false
29 DONT=false
30 STOPARGS=false
31 COMMENTS=false
32
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
36 # our priority order
37 if [ -z "$INDENT_PROFILE" ]; then
38 for i in . ../openssl $HOME
39 do
40 if [ -f "$i/.indent.pro" ]; then
41 INDENT_PROFILE="$i/.indent.pro"
42 export INDENT_PROFILE
43 break
44 fi
45 done
46 fi
47
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
52 if [ -f $f ]; then
53 INDENT_PROFILE=$f
54 export INDENT_PROFILE
55 fi
56 fi
57 fi
58 if [ -z "$INDENT_PROFILE" ]; then
59 echo "$0: unable to locate .indent.pro file " >&2
60 exit 1
61 fi
62
63 # Extra arguments; for adding the comment-formatting
64 INDENT_ARGS=""
65 for i
66 do
67 if [ "$STOPARGS" != "true" ]; then
68 case $i in
69 --) STOPARGS="true"; continue;;
70 -n) DONT="true"; continue;;
71 -v) VERBOSE="true";
72 echo "INDENT_PROFILE=$INDENT_PROFILE";
73 continue;;
74 -c) COMMENTS="true";
75 INDENT_ARGS="-fc1 -fca -cdb -sc";
76 continue;;
77 esac
78 fi
79
80 if [ -d "$i" ]; then
81 LIST=`find "$i" -name '*.[ch]' -print`
82 else
83 if [ ! -f "$i" ]; then
84 echo "$0: source file not found: $i" >&2
85 exit 1
86 fi
87 LIST="$i"
88 fi
89
90 for j in $LIST
91 do
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
94 if [ -d "$i" ]; then
95 if [ -h "$j" ]; then
96 continue;
97 fi
98 fi
99
100 if [ "$VERBOSE" = "true" ]; then
101 echo "$j"
102 fi
103
104 if [ "$DONT" = "false" ]; then
105 tmp=$(mktemp /tmp/indent.XXXXXX)
106 trap 'rm -f "$tmp"' HUP INT TERM EXIT
107
108 case $j in
109 # the list of files that indent is unable to handle correctly
110 # that we simply leave alone for manual formatting now
111 *)
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
121 perl -0 -np \
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;' \
125 < "$j" | \
126 perl -np \
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**\//;' \
135 | \
136 expand | indent $INDENT_ARGS | \
137 perl -np \
138 -e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \
139 -e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \
140 | indent | \
141 perl -0 -np \
142 -e 's/\/\*\*INDENT-(ON|OFF)\*\*\/\n//g;' \
143 | perl -np \
144 -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_( |\/)/$1($2)$3/g;' \
145 -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_$/$1($2)/g;' \
146 > "$tmp"
147 else
148 expand "$j" | indent $INDENT_ARGS > "$tmp"
149 fi
150 ;;
151 esac
152 mv "$tmp" "$j"
153 fi
154 done
155 done
156
157