1 1.1 christos #!/bin/sh 2 1.1 christos # 3 1.1.1.2 christos # Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved. 4 1.1.1.2 christos # 5 1.1.1.2 christos # Licensed under the OpenSSL license (the "License"). You may not use 6 1.1.1.2 christos # this file except in compliance with the License. You can obtain a copy 7 1.1.1.2 christos # in the file LICENSE in the source distribution or at 8 1.1.1.2 christos # https://www.openssl.org/source/license.html 9 1.1.1.2 christos 10 1.1.1.2 christos # 11 1.1.1.2 christos # openssl-format-source 12 1.1 christos # - format source tree according to OpenSSL coding style using indent 13 1.1 christos # 14 1.1 christos # usage: 15 1.1 christos # openssl-format-source [-v] [-n] [file|directory] ... 16 1.1 christos # 17 1.1 christos # note: the indent options assume GNU indent v2.2.10 which was released 18 1.1.1.2 christos # Feb-2009 so if you have an older indent the options may not 19 1.1 christos # match what is expected 20 1.1 christos # 21 1.1 christos # any marked block comment blocks have to be moved to align manually after 22 1.1.1.2 christos # the reformatting has been completed as marking a block causes indent to 23 1.1 christos # not move it at all ... 24 1.1 christos # 25 1.1 christos 26 1.1 christos PATH=/usr/local/bin:/bin:/usr/bin:$PATH 27 1.1 christos export PATH 28 1.1 christos HERE="`dirname $0`" 29 1.1 christos 30 1.1 christos set -e 31 1.1 christos 32 1.1.1.2 christos INDENT=indent 33 1.1.1.2 christos uname -s | grep BSD > /dev/null && type gindent > /dev/null 2>&1 && INDENT=gindent 34 1.1.1.2 christos 35 1.1 christos if [ $# -eq 0 ]; then 36 1.1 christos echo "usage: $0 [-v] [-n] [-c] [sourcefile|sourcedir] ..." >&2 37 1.1 christos exit 1 38 1.1 christos fi 39 1.1 christos 40 1.1 christos VERBOSE=false 41 1.1 christos DONT=false 42 1.1 christos STOPARGS=false 43 1.1 christos COMMENTS=false 44 1.1.1.2 christos CHANGED=false 45 1.1 christos DEBUG="" 46 1.1 christos 47 1.1 christos # for this exercise, we want to force the openssl style, so we roll 48 1.1 christos # our own indent profile, which is at a well known location 49 1.1 christos INDENT_PROFILE="$HERE/indent.pro" 50 1.1 christos export INDENT_PROFILE 51 1.1 christos if [ ! -f "$INDENT_PROFILE" ]; then 52 1.1 christos echo "$0: unable to locate the openssl indent.pro file" >&2 53 1.1 christos exit 1 54 1.1 christos fi 55 1.1 christos 56 1.1 christos # Extra arguments; for adding the comment-formatting 57 1.1 christos INDENT_ARGS="" 58 1.1.1.2 christos for i 59 1.1 christos do 60 1.1 christos if [ "$STOPARGS" != "true" ]; then 61 1.1 christos case $i in 62 1.1 christos --) STOPARGS="true"; continue;; 63 1.1 christos -n) DONT="true"; continue;; 64 1.1.1.2 christos -v) VERBOSE="true"; 65 1.1 christos echo "INDENT_PROFILE=$INDENT_PROFILE"; 66 1.1 christos continue;; 67 1.1.1.2 christos -c) COMMENTS="true"; 68 1.1.1.2 christos INDENT_ARGS="-fc1 -fca -cdb -sc"; 69 1.1 christos continue;; 70 1.1 christos -nc) COMMENTS="true"; 71 1.1 christos continue;; 72 1.1 christos -d) DEBUG='eval tee "$j.pre" |' 73 1.1 christos continue;; 74 1.1 christos esac 75 1.1 christos fi 76 1.1 christos 77 1.1 christos if [ -d "$i" ]; then 78 1.1 christos LIST=`find "$i" -name '*.[ch]' -print` 79 1.1.1.2 christos else 80 1.1 christos if [ ! -f "$i" ]; then 81 1.1 christos echo "$0: source file not found: $i" >&2 82 1.1 christos exit 1 83 1.1 christos fi 84 1.1 christos LIST="$i" 85 1.1 christos fi 86 1.1.1.2 christos 87 1.1 christos for j in $LIST 88 1.1 christos do 89 1.1 christos # ignore symlinks - we only ever process the base file - so if we 90 1.1 christos # expand a directory tree we need to ignore any located symlinks 91 1.1 christos if [ -d "$i" ]; then 92 1.1 christos if [ -h "$j" ]; then 93 1.1 christos continue; 94 1.1 christos fi 95 1.1 christos fi 96 1.1 christos 97 1.1 christos if [ "$DONT" = "false" ]; then 98 1.1 christos tmp=$(mktemp /tmp/indent.XXXXXX) 99 1.1 christos trap 'rm -f "$tmp"' HUP INT TERM EXIT 100 1.1 christos 101 1.1.1.2 christos case `basename $j` in 102 1.1 christos # the list of files that indent is unable to handle correctly 103 1.1 christos # that we simply leave alone for manual formatting now 104 1.1 christos obj_dat.h|aes_core.c|aes_x86core.c|ecp_nistz256.c) 105 1.1 christos echo "skipping $j" 106 1.1 christos ;; 107 1.1 christos *) 108 1.1 christos if [ "$COMMENTS" = "true" ]; then 109 1.1 christos # we have to mark single line comments as /*- ...*/ to stop indent 110 1.1 christos # messing with them, run expand then indent as usual but with the 111 1.1.1.2 christos # the process-comments options and then undo that marking, and then 112 1.1 christos # finally re-run indent without process-comments so the marked-to- 113 1.1.1.2 christos # be-ignored comments we did automatically end up getting moved 114 1.1.1.2 christos # into the right position within the code as indent leaves marked 115 1.1.1.2 christos # comments entirely untouched - we appear to have no way to avoid 116 1.1 christos # the double processing and get the desired output 117 1.1 christos cat "$j" | \ 118 1.1 christos expand | \ 119 1.1 christos perl -0 -np \ 120 1.1 christos -e 's/(\n#[ \t]*ifdef[ \t]+__cplusplus\n[^\n]*\n#[ \t]*endif\n)/\n\/**INDENT-OFF**\/$1\/**INDENT-ON**\/\n/g;' \ 121 1.1 christos -e 's/(\n\/\*\!)/\n\/**/g;' \ 122 1.1 christos -e 's/(STACK_OF|LHASH_OF)\(([^ \t,\)]+)\)( |\n)/$1_$2_$3/g;' \ 123 1.1 christos | \ 124 1.1 christos perl -np \ 125 1.1.1.2 christos -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 1.1 christos -e 's/^\/\* ((Copyright|=|----).*)$/\/*-$1/;' \ 127 1.1.1.2 christos -e 's/^((DECLARE|IMPLEMENT)_.*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ 128 1.1 christos -e 's/^([ \t]*(make_dh|make_dh_bn|make_rfc5114_td)\(.*\)[ \t,]*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ 129 1.1 christos -e 's/^(ASN1_ADB_TEMPLATE\(.*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ 130 1.1 christos -e 's/^((ASN1|ADB)_.*_(end|END)\(.*[\){=,;]+[ \t]*)$/$1\n\/**INDENT-ON**\//;' \ 131 1.1 christos -e '/ASN1_(ITEM_ref|ITEM_ptr|ITEM_rptr|PCTX)/ || s/^((ASN1|ADB)_[^\*]*[){=,]+[ \t]*)$/\/**INDENT-OFF**\/\n$1/;' \ 132 1.1 christos -e 's/^(} (ASN1|ADB)_[^\*]*[\){=,;]+)$/$1\n\/**INDENT-ON**\//;' \ 133 1.1 christos | \ 134 1.1.1.2 christos $DEBUG $INDENT $INDENT_ARGS | \ 135 1.1 christos perl -np \ 136 1.1 christos -e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \ 137 1.1 christos -e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \ 138 1.1.1.2 christos | $INDENT | \ 139 1.1 christos perl -0 -np \ 140 1.1 christos -e 's/\/\*\*INDENT-(ON|OFF)\*\*\/\n//g;' \ 141 1.1 christos | perl -np \ 142 1.1 christos -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_( |\/)/$1($2)$3/g;' \ 143 1.1 christos -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_$/$1($2)/g;' \ 144 1.1 christos | perl "$HERE"/su-filter.pl \ 145 1.1 christos > "$tmp" 146 1.1 christos else 147 1.1.1.2 christos expand "$j" | $INDENT $INDENT_ARGS > "$tmp" 148 1.1 christos fi; 149 1.1.1.2 christos if cmp -s "$tmp" "$j"; then 150 1.1.1.2 christos if [ "$VERBOSE" = "true" ]; then 151 1.1.1.2 christos echo "$j unchanged" 152 1.1.1.2 christos fi 153 1.1.1.2 christos rm "$tmp" 154 1.1.1.2 christos else 155 1.1.1.2 christos if [ "$VERBOSE" = "true" ]; then 156 1.1.1.2 christos echo "$j changed" 157 1.1.1.2 christos fi 158 1.1.1.2 christos CHANGED=true 159 1.1.1.2 christos mv "$tmp" "$j" 160 1.1.1.2 christos fi 161 1.1 christos ;; 162 1.1 christos esac 163 1.1 christos fi 164 1.1 christos done 165 1.1 christos done 166 1.1 christos 167 1.1 christos 168 1.1.1.2 christos if [ "$VERBOSE" = "true" ]; then 169 1.1.1.2 christos echo 170 1.1.1.2 christos if [ "$CHANGED" = "true" ]; then 171 1.1.1.2 christos echo "SOURCE WAS MODIFIED" 172 1.1.1.2 christos else 173 1.1.1.2 christos echo "SOURCE WAS NOT MODIFIED" 174 1.1.1.2 christos fi 175 1.1.1.2 christos fi 176