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