1296465Sdelphij#!/bin/sh 2296465Sdelphij# 3296465Sdelphij# openssl-format-source 4296465Sdelphij# - format source tree according to OpenSSL coding style using indent 5296465Sdelphij# 6296465Sdelphij# usage: 7296465Sdelphij# openssl-format-source [-v] [-n] [file|directory] ... 8296465Sdelphij# 9296465Sdelphij# note: the indent options assume GNU indent v2.2.10 which was released 10296465Sdelphij# Feb-2009 so if you have an older indent the options may not 11296465Sdelphij# match what is expected 12296465Sdelphij# 13296465Sdelphij# any marked block comment blocks have to be moved to align manually after 14296465Sdelphij# the reformatting has been completed as marking a block causes indent to 15296465Sdelphij# not move it at all ... 16296465Sdelphij# 17296465Sdelphij 18296465SdelphijPATH=/usr/local/bin:/bin:/usr/bin:$PATH 19296465Sdelphijexport PATH 20296465SdelphijHERE="`dirname $0`" 21296465Sdelphij 22296465Sdelphijset -e 23296465Sdelphij 24296465Sdelphijif [ $# -eq 0 ]; then 25296465Sdelphij echo "usage: $0 [-v] [-n] [-c] [sourcefile|sourcedir] ..." >&2 26296465Sdelphij exit 1 27296465Sdelphijfi 28296465Sdelphij 29296465SdelphijVERBOSE=false 30296465SdelphijDONT=false 31296465SdelphijSTOPARGS=false 32296465SdelphijCOMMENTS=false 33296465SdelphijDEBUG="" 34296465Sdelphij 35296465Sdelphij# for this exercise, we want to force the openssl style, so we roll 36296465Sdelphij# our own indent profile, which is at a well known location 37296465SdelphijINDENT_PROFILE="$HERE/indent.pro" 38296465Sdelphijexport INDENT_PROFILE 39296465Sdelphijif [ ! -f "$INDENT_PROFILE" ]; then 40296465Sdelphij echo "$0: unable to locate the openssl indent.pro file" >&2 41296465Sdelphij exit 1 42296465Sdelphijfi 43296465Sdelphij 44296465Sdelphij# Extra arguments; for adding the comment-formatting 45296465SdelphijINDENT_ARGS="" 46296465Sdelphijfor i 47296465Sdelphijdo 48296465Sdelphij if [ "$STOPARGS" != "true" ]; then 49296465Sdelphij case $i in 50296465Sdelphij --) STOPARGS="true"; continue;; 51296465Sdelphij -n) DONT="true"; continue;; 52296465Sdelphij -v) VERBOSE="true"; 53296465Sdelphij echo "INDENT_PROFILE=$INDENT_PROFILE"; 54296465Sdelphij continue;; 55296465Sdelphij -c) COMMENTS="true"; 56296465Sdelphij INDENT_ARGS="-fc1 -fca -cdb -sc"; 57296465Sdelphij continue;; 58296465Sdelphij -nc) COMMENTS="true"; 59296465Sdelphij continue;; 60296465Sdelphij -d) DEBUG='eval tee "$j.pre" |' 61296465Sdelphij continue;; 62296465Sdelphij esac 63296465Sdelphij fi 64296465Sdelphij 65296465Sdelphij if [ -d "$i" ]; then 66296465Sdelphij LIST=`find "$i" -name '*.[ch]' -print` 67296465Sdelphij else 68296465Sdelphij if [ ! -f "$i" ]; then 69296465Sdelphij echo "$0: source file not found: $i" >&2 70296465Sdelphij exit 1 71296465Sdelphij fi 72296465Sdelphij LIST="$i" 73296465Sdelphij fi 74296465Sdelphij 75296465Sdelphij for j in $LIST 76296465Sdelphij do 77296465Sdelphij # ignore symlinks - we only ever process the base file - so if we 78296465Sdelphij # expand a directory tree we need to ignore any located symlinks 79296465Sdelphij if [ -d "$i" ]; then 80296465Sdelphij if [ -h "$j" ]; then 81296465Sdelphij continue; 82296465Sdelphij fi 83296465Sdelphij fi 84296465Sdelphij 85296465Sdelphij if [ "$VERBOSE" = "true" ]; then 86296465Sdelphij echo "$j" 87296465Sdelphij fi 88296465Sdelphij 89296465Sdelphij if [ "$DONT" = "false" ]; then 90296465Sdelphij tmp=$(mktemp /tmp/indent.XXXXXX) 91296465Sdelphij trap 'rm -f "$tmp"' HUP INT TERM EXIT 92296465Sdelphij 93296465Sdelphij case `basename $j` in 94296465Sdelphij # the list of files that indent is unable to handle correctly 95296465Sdelphij # that we simply leave alone for manual formatting now 96296465Sdelphij obj_dat.h|aes_core.c|aes_x86core.c|ecp_nistz256.c) 97296465Sdelphij echo "skipping $j" 98296465Sdelphij ;; 99296465Sdelphij *) 100296465Sdelphij if [ "$COMMENTS" = "true" ]; then 101296465Sdelphij # we have to mark single line comments as /*- ...*/ to stop indent 102296465Sdelphij # messing with them, run expand then indent as usual but with the 103296465Sdelphij # the process-comments options and then undo that marking, and then 104296465Sdelphij # finally re-run indent without process-comments so the marked-to- 105296465Sdelphij # be-ignored comments we did automatically end up getting moved 106296465Sdelphij # into the right possition within the code as indent leaves marked 107296465Sdelphij # comments entirely untouched - we appear to have no way to avoid 108296465Sdelphij # the double processing and get the desired output 109296465Sdelphij cat "$j" | \ 110296465Sdelphij expand | \ 111296465Sdelphij perl -0 -np \ 112296465Sdelphij -e 's/(\n#[ \t]*ifdef[ \t]+__cplusplus\n[^\n]*\n#[ \t]*endif\n)/\n\/**INDENT-OFF**\/$1\/**INDENT-ON**\/\n/g;' \ 113296465Sdelphij -e 's/(\n\/\*\!)/\n\/**/g;' \ 114296465Sdelphij -e 's/(STACK_OF|LHASH_OF)\(([^ \t,\)]+)\)( |\n)/$1_$2_$3/g;' \ 115296465Sdelphij | \ 116296465Sdelphij perl -np \ 117296465Sdelphij -e 's/^([ \t]*)\/\*([ \t]+.*)\*\/[ \t]*$/if (length("$1$2")<75) {$c="-"}else{$c=""}; "$1\/*$c$2*\/"/e;' \ 118296465Sdelphij -e 's/^\/\* ((Copyright|=|----).*)$/\/*-$1/;' \ 119296465Sdelphij -e 's/^((DECLARE|IMPLEMENT)_(EXTERN_ASN1|ASN1|ADB|STACK_OF|PKCS12_STACK_OF).*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ 120296465Sdelphij -e 's/^([ \t]*(make_dh|make_dh_bn|make_rfc5114_td)\(.*\)[ \t,]*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ 121296465Sdelphij -e 's/^(ASN1_ADB_TEMPLATE\(.*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \ 122296465Sdelphij -e 's/^((ASN1|ADB)_.*_(end|END)\(.*[\){=,;]+[ \t]*)$/$1\n\/**INDENT-ON**\//;' \ 123296465Sdelphij -e '/ASN1_(ITEM_ref|ITEM_ptr|ITEM_rptr|PCTX)/ || s/^((ASN1|ADB)_[^\*]*[){=,]+[ \t]*)$/\/**INDENT-OFF**\/\n$1/;' \ 124296465Sdelphij -e 's/^(} (ASN1|ADB)_[^\*]*[\){=,;]+)$/$1\n\/**INDENT-ON**\//;' \ 125296465Sdelphij | \ 126296465Sdelphij $DEBUG indent $INDENT_ARGS | \ 127296465Sdelphij perl -np \ 128296465Sdelphij -e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \ 129296465Sdelphij -e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \ 130296465Sdelphij | indent | \ 131296465Sdelphij perl -0 -np \ 132296465Sdelphij -e 's/\/\*\*INDENT-(ON|OFF)\*\*\/\n//g;' \ 133296465Sdelphij | perl -np \ 134296465Sdelphij -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_( |\/)/$1($2)$3/g;' \ 135296465Sdelphij -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_$/$1($2)/g;' \ 136296465Sdelphij | perl "$HERE"/su-filter.pl \ 137296465Sdelphij > "$tmp" 138296465Sdelphij else 139296465Sdelphij expand "$j" | indent $INDENT_ARGS > "$tmp" 140296465Sdelphij fi; 141296465Sdelphij mv "$tmp" "$j" 142296465Sdelphij ;; 143296465Sdelphij esac 144296465Sdelphij fi 145296465Sdelphij done 146296465Sdelphijdone 147296465Sdelphij 148296465Sdelphij 149