3 # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 # openssl-format-source
12 # - format source tree according to OpenSSL coding style using indent
15 # openssl-format-source [-v] [-n] [file|directory] ...
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
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 ...
26 PATH=/usr/local/bin:/bin:/usr/bin:$PATH
33 uname -s | grep BSD > /dev/null && type gindent > /dev/null 2>&1 && INDENT=gindent
36 echo "usage: $0 [-v] [-n] [-c] [sourcefile|sourcedir] ..." >&2
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"
51 if [ ! -f "$INDENT_PROFILE" ]; then
52 echo "$0: unable to locate the openssl indent.pro file" >&2
56 # Extra arguments; for adding the comment-formatting
60 if [ "$STOPARGS" != "true" ]; then
62 --) STOPARGS="true"; continue;;
63 -n) DONT="true"; continue;;
65 echo "INDENT_PROFILE=$INDENT_PROFILE";
68 INDENT_ARGS="-fc1 -fca -cdb -sc";
72 -d) DEBUG='eval tee "$j.pre" |'
78 LIST=`find "$i" -name '*.[ch]' -print`
80 if [ ! -f "$i" ]; then
81 echo "$0: source file not found: $i" >&2
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
97 if [ "$DONT" = "false" ]; then
98 tmp=$(mktemp /tmp/indent.XXXXXX)
99 trap 'rm -f "$tmp"' HUP INT TERM EXIT
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)
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 possition 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
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;' \
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**\//;' \
134 $DEBUG $INDENT $INDENT_ARGS | \
136 -e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \
137 -e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \
140 -e 's/\/\*\*INDENT-(ON|OFF)\*\*\/\n//g;' \
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 \
147 expand "$j" | $INDENT $INDENT_ARGS > "$tmp"
149 if cmp -s "$tmp" "$j"; then
150 if [ "$VERBOSE" = "true" ]; then
155 if [ "$VERBOSE" = "true" ]; then
168 if [ "$VERBOSE" = "true" ]; then
170 if [ "$CHANGED" = "true" ]; then
171 echo "SOURCE WAS MODIFIED"
173 echo "SOURCE WAS NOT MODIFIED"