1280288Sjkim#!/bin/sh 2280288Sjkim# 3280288Sjkim# openssl-format-source 4280288Sjkim# - format source tree according to OpenSSL coding style using indent 5280288Sjkim# 6280288Sjkim# usage: 7280288Sjkim# openssl-format-source [-v] [-n] [file|directory] ... 8280288Sjkim# 9280288Sjkim# note: the indent options assume GNU indent v2.2.10 which was released 10280288Sjkim# Feb-2009 so if you have an older indent the options may not 11280288Sjkim# match what is expected 12280288Sjkim# 13280288Sjkim# any marked block comment blocks have to be moved to align manually after 14280288Sjkim# the reformatting has been completed as marking a block causes indent to 15280288Sjkim# not move it at all ... 16280288Sjkim# 17280288Sjkim 18280288SjkimPATH=/usr/local/bin:/bin:/usr/bin:$PATH 19280288Sjkimexport PATH 20280288SjkimHERE="`dirname $0`" 21280288Sjkim 22280288Sjkimset -e 23280288Sjkim 24280288Sjkimif [ $# -eq 0 ]; then 25280288Sjkim echo "usage: $0 [-v] [-n] [-c] [sourcefile|sourcedir] ..." >&2 26280288Sjkim exit 1 27280288Sjkimfi 28280288Sjkim 29280288SjkimVERBOSE=false 30280288SjkimDONT=false 31280288SjkimSTOPARGS=false 32280288SjkimCOMMENTS=false 33280288SjkimDEBUG="" 34280288Sjkim 35280288Sjkim# for this exercise, we want to force the openssl style, so we roll 36280288Sjkim# our own indent profile, which is at a well known location 37280288SjkimINDENT_PROFILE="$HERE/indent.pro" 38280288Sjkimexport INDENT_PROFILE 39280288Sjkimif [ ! -f "$INDENT_PROFILE" ]; then 40280288Sjkim echo "$0: unable to locate the openssl indent.pro file" >&2 41280288Sjkim exit 1 42280288Sjkimfi 43280288Sjkim 44280288Sjkim# Extra arguments; for adding the comment-formatting 45280288SjkimINDENT_ARGS="" 46280288Sjkimfor i 47280288Sjkimdo 48280288Sjkim if [ "$STOPARGS" != "true" ]; then 49280288Sjkim case $i in 50280288Sjkim --) STOPARGS="true"; continue;; 51280288Sjkim -n) DONT="true"; continue;; 52280288Sjkim -v) VERBOSE="true"; 53280288Sjkim echo "INDENT_PROFILE=$INDENT_PROFILE"; 54280288Sjkim continue;; 55280288Sjkim -c) COMMENTS="true"; 56280288Sjkim INDENT_ARGS="-fc1 -fca -cdb -sc"; 57280288Sjkim continue;; 58280288Sjkim -nc) COMMENTS="true"; 59280288Sjkim continue;; 60280288Sjkim -d) DEBUG='eval tee "$j.pre" |' 61280288Sjkim continue;; 62280288Sjkim esac 63280288Sjkim fi 64280288Sjkim 65280288Sjkim if [ -d "$i" ]; then 66280288Sjkim LIST=`find "$i" -name '*.[ch]' -print` 67280288Sjkim else 68280288Sjkim if [ ! -f "$i" ]; then 69280288Sjkim echo "$0: source file not found: $i" >&2 70280288Sjkim exit 1 71280288Sjkim fi 72280288Sjkim LIST="$i" 73280288Sjkim fi 74280288Sjkim 75280288Sjkim for j in $LIST 76280288Sjkim do 77280288Sjkim # ignore symlinks - we only ever process the base file - so if we 78280288Sjkim # expand a directory tree we need to ignore any located symlinks 79280288Sjkim if [ -d "$i" ]; then 80280288Sjkim if [ -h "$j" ]; then 81280288Sjkim continue; 82280288Sjkim fi 83280288Sjkim fi 84280288Sjkim 85280288Sjkim if [ "$VERBOSE" = "true" ]; then 86280288Sjkim echo "$j" 87280288Sjkim fi 88280288Sjkim 89280288Sjkim if [ "$DONT" = "false" ]; then 90280288Sjkim tmp=$(mktemp /tmp/indent.XXXXXX) 91280288Sjkim trap 'rm -f "$tmp"' HUP INT TERM EXIT 92280288Sjkim 93280288Sjkim case `basename $j` in 94280288Sjkim # the list of files that indent is unable to handle correctly 95280288Sjkim # that we simply leave alone for manual formatting now 96280288Sjkim obj_dat.h|aes_core.c|aes_x86core.c|ecp_nistz256.c) 97280288Sjkim echo "skipping $j" 98280288Sjkim ;; 99280288Sjkim *) 100280288Sjkim if [ "$COMMENTS" = "true" ]; then 101280288Sjkim # we have to mark single line comments as /*- ...*/ to stop indent 102280288Sjkim # messing with them, run expand then indent as usual but with the 103280288Sjkim # the process-comments options and then undo that marking, and then 104280288Sjkim # finally re-run indent without process-comments so the marked-to- 105280288Sjkim # be-ignored comments we did automatically end up getting moved 106280288Sjkim # into the right possition within the code as indent leaves marked 107280288Sjkim # comments entirely untouched - we appear to have no way to avoid 108280288Sjkim # the double processing and get the desired output 109280288Sjkim cat "$j" | \ 110280288Sjkim expand | \ 111280288Sjkim perl -0 -np \ 112280288Sjkim -e 's/(\n#[ \t]*ifdef[ \t]+__cplusplus\n[^\n]*\n#[ \t]*endif\n)/\n\/**INDENT-OFF**\/$1\/**INDENT-ON**\/\n/g;' \ 113280288Sjkim -e 's/(\n\/\*\!)/\n\/**/g;' \ 114280288Sjkim -e 's/(STACK_OF|LHASH_OF)\(([^ \t,\)]+)\)( |\n)/$1_$2_$3/g;' \ 115280288Sjkim | \ 116280288Sjkim perl -np \ 117280288Sjkim -e 's/^([ \t]*)\/\*([ \t]+.*)\*\/[ \t]*$/if (length("$1$2")<75) {$c="-"}else{$c=""}; "$1\/*$c$2*\/"/e;' \ 118280288Sjkim -e 's/^\/\* ((Copyright|=|----).*)$/\/*-$1/;' \ 119280288Sjkim -e 's/^((DECLARE|IMPLEMENT)_(EXTERN_ASN1|ASN1|ADB|STACK_OF|PKCS12_STACK_OF).*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ 120280288Sjkim -e 's/^([ \t]*(make_dh|make_dh_bn|make_rfc5114_td)\(.*\)[ \t,]*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ 121280288Sjkim -e 's/^(ASN1_ADB_TEMPLATE\(.*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ 122280288Sjkim -e 's/^((ASN1|ADB)_.*_(end|END)\(.*[\){=,;]+[ \t]*)$/$1\n\/**INDENT-ON**\//;' \ 123280288Sjkim -e '/ASN1_(ITEM_ref|ITEM_ptr|ITEM_rptr|PCTX)/ || s/^((ASN1|ADB)_[^\*]*[){=,]+[ \t]*)$/\/**INDENT-OFF**\/\n$1/;' \ 124280288Sjkim -e 's/^(} (ASN1|ADB)_[^\*]*[\){=,;]+)$/$1\n\/**INDENT-ON**\//;' \ 125280288Sjkim | \ 126280288Sjkim $DEBUG indent $INDENT_ARGS | \ 127280288Sjkim perl -np \ 128280288Sjkim -e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \ 129280288Sjkim -e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \ 130280288Sjkim | indent | \ 131280288Sjkim perl -0 -np \ 132280288Sjkim -e 's/\/\*\*INDENT-(ON|OFF)\*\*\/\n//g;' \ 133280288Sjkim | perl -np \ 134280288Sjkim -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_( |\/)/$1($2)$3/g;' \ 135280288Sjkim -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_$/$1($2)/g;' \ 136280288Sjkim | perl "$HERE"/su-filter.pl \ 137280288Sjkim > "$tmp" 138280288Sjkim else 139280288Sjkim expand "$j" | indent $INDENT_ARGS > "$tmp" 140280288Sjkim fi; 141280288Sjkim mv "$tmp" "$j" 142280288Sjkim ;; 143280288Sjkim esac 144280288Sjkim fi 145280288Sjkim done 146280288Sjkimdone 147280288Sjkim 148280288Sjkim 149