]>
Commit | Line | Data |
---|---|---|
849c80bc | 1 | #!/bin/sh |
44c8a5e2 RS |
2 | # |
3 | # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. | |
4 | # | |
9059ab42 | 5 | # Licensed under the Apache License 2.0 (the "License"). You may not use |
44c8a5e2 RS |
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 | ||
849c80bc TH |
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 | |
2f1ac20b | 28 | HERE="`dirname $0`" |
849c80bc TH |
29 | |
30 | set -e | |
31 | ||
4b9cb35d BL |
32 | INDENT=indent |
33 | uname -s | grep BSD > /dev/null && type gindent > /dev/null 2>&1 && INDENT=gindent | |
34 | ||
849c80bc TH |
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 | |
f0983d39 | 44 | CHANGED=false |
09f4d9c0 | 45 | DEBUG="" |
849c80bc | 46 | |
d09481a1 RL |
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 | |
2f1ac20b | 49 | INDENT_PROFILE="$HERE/indent.pro" |
d09481a1 | 50 | export INDENT_PROFILE |
24a5f17b | 51 | if [ ! -f "$INDENT_PROFILE" ]; then |
d09481a1 | 52 | echo "$0: unable to locate the openssl indent.pro file" >&2 |
849c80bc TH |
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;; | |
f8180c63 DSH |
70 | -nc) COMMENTS="true"; |
71 | continue;; | |
09f4d9c0 DSH |
72 | -d) DEBUG='eval tee "$j.pre" |' |
73 | continue;; | |
849c80bc TH |
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 | ||
849c80bc TH |
97 | if [ "$DONT" = "false" ]; then |
98 | tmp=$(mktemp /tmp/indent.XXXXXX) | |
99 | trap 'rm -f "$tmp"' HUP INT TERM EXIT | |
100 | ||
572cee38 | 101 | case `basename $j` in |
849c80bc TH |
102 | # the list of files that indent is unable to handle correctly |
103 | # that we simply leave alone for manual formatting now | |
46648620 | 104 | obj_dat.h|aes_core.c|aes_x86core.c|ecp_nistz256.c) |
572cee38 MC |
105 | echo "skipping $j" |
106 | ;; | |
849c80bc TH |
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 | |
44e69951 | 114 | # into the right position within the code as indent leaves marked |
849c80bc TH |
115 | # comments entirely untouched - we appear to have no way to avoid |
116 | # the double processing and get the desired output | |
ff7ca7a3 RL |
117 | cat "$j" | \ |
118 | expand | \ | |
849c80bc | 119 | perl -0 -np \ |
6f08264e | 120 | -e 's/(\n#[ \t]*ifdef[ \t]+__cplusplus\n[^\n]*\n#[ \t]*endif\n)/\n\/**INDENT-OFF**\/$1\/**INDENT-ON**\/\n/g;' \ |
849c80bc | 121 | -e 's/(\n\/\*\!)/\n\/**/g;' \ |
51428a12 | 122 | -e 's/(STACK_OF|LHASH_OF)\(([^ \t,\)]+)\)( |\n)/$1_$2_$3/g;' \ |
ff7ca7a3 | 123 | | \ |
849c80bc | 124 | perl -np \ |
931b160a | 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;' \ |
849c80bc | 126 | -e 's/^\/\* ((Copyright|=|----).*)$/\/*-$1/;' \ |
a3a0b410 | 127 | -e 's/^((DECLARE|IMPLEMENT)_.*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ |
849c80bc TH |
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**\//;' \ | |
437b14b5 | 130 | -e 's/^((ASN1|ADB)_.*_(end|END)\(.*[\){=,;]+[ \t]*)$/$1\n\/**INDENT-ON**\//;' \ |
6f08264e | 131 | -e '/ASN1_(ITEM_ref|ITEM_ptr|ITEM_rptr|PCTX)/ || s/^((ASN1|ADB)_[^\*]*[){=,]+[ \t]*)$/\/**INDENT-OFF**\/\n$1/;' \ |
849c80bc TH |
132 | -e 's/^(} (ASN1|ADB)_[^\*]*[\){=,;]+)$/$1\n\/**INDENT-ON**\//;' \ |
133 | | \ | |
4b9cb35d | 134 | $DEBUG $INDENT $INDENT_ARGS | \ |
849c80bc TH |
135 | perl -np \ |
136 | -e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \ | |
137 | -e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \ | |
4b9cb35d | 138 | | $INDENT | \ |
849c80bc TH |
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;' \ | |
2f1ac20b | 144 | | perl "$HERE"/su-filter.pl \ |
849c80bc TH |
145 | > "$tmp" |
146 | else | |
4b9cb35d | 147 | expand "$j" | $INDENT $INDENT_ARGS > "$tmp" |
572cee38 | 148 | fi; |
f0983d39 DSH |
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 | |
849c80bc TH |
161 | ;; |
162 | esac | |
849c80bc TH |
163 | fi |
164 | done | |
165 | done | |
166 | ||
167 | ||
f0983d39 DSH |
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 |