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