]> git.ipfire.org Git - thirdparty/openssl.git/blame - util/openssl-format-source
Add defines to indicate if intypes.h and stdint.h are unavailable
[thirdparty/openssl.git] / util / openssl-format-source
CommitLineData
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 10#
df443918 11# openssl-format-source
849c80bc
TH
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
df443918 18# Feb-2009 so if you have an older indent the options may not
849c80bc
TH
19# match what is expected
20#
21# any marked block comment blocks have to be moved to align manually after
df443918 22# the reformatting has been completed as marking a block causes indent to
849c80bc
TH
23# not move it at all ...
24#
25
26PATH=/usr/local/bin:/bin:/usr/bin:$PATH
27export PATH
2f1ac20b 28HERE="`dirname $0`"
849c80bc
TH
29
30set -e
31
4b9cb35d
BL
32INDENT=indent
33uname -s | grep BSD > /dev/null && type gindent > /dev/null 2>&1 && INDENT=gindent
34
849c80bc
TH
35if [ $# -eq 0 ]; then
36 echo "usage: $0 [-v] [-n] [-c] [sourcefile|sourcedir] ..." >&2
37 exit 1
38fi
39
40VERBOSE=false
41DONT=false
42STOPARGS=false
43COMMENTS=false
f0983d39 44CHANGED=false
09f4d9c0 45DEBUG=""
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 49INDENT_PROFILE="$HERE/indent.pro"
d09481a1 50export INDENT_PROFILE
24a5f17b 51if [ ! -f "$INDENT_PROFILE" ]; then
d09481a1 52 echo "$0: unable to locate the openssl indent.pro file" >&2
849c80bc
TH
53 exit 1
54fi
55
56# Extra arguments; for adding the comment-formatting
57INDENT_ARGS=""
df443918 58for i
849c80bc
TH
59do
60 if [ "$STOPARGS" != "true" ]; then
61 case $i in
62 --) STOPARGS="true"; continue;;
63 -n) DONT="true"; continue;;
df443918 64 -v) VERBOSE="true";
849c80bc
TH
65 echo "INDENT_PROFILE=$INDENT_PROFILE";
66 continue;;
df443918
SR
67 -c) COMMENTS="true";
68 INDENT_ARGS="-fc1 -fca -cdb -sc";
849c80bc 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`
df443918 79 else
849c80bc
TH
80 if [ ! -f "$i" ]; then
81 echo "$0: source file not found: $i" >&2
82 exit 1
83 fi
84 LIST="$i"
85 fi
df443918 86
849c80bc
TH
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
df443918 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
df443918 111 # the process-comments options and then undo that marking, and then
849c80bc 112 # finally re-run indent without process-comments so the marked-to-
df443918
SR
113 # be-ignored comments we did automatically end up getting moved
114 # into the right position within the code as indent leaves marked
115 # comments entirely untouched - we appear to have no way to avoid
849c80bc 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
165done
166
167
f0983d39
DSH
168if [ "$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
175fi