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