openssl-format-source 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/bin/sh
#
# openssl-format-source 
# - format source tree according to OpenSSL coding style using indent
#
# usage:
#   openssl-format-source [-v] [-n] [file|directory] ...
#
# note: the indent options assume GNU indent v2.2.10 which was released
#       Feb-2009 so if you have an older indent the options may not 
#	match what is expected
#
# any marked block comment blocks have to be moved to align manually after
# the reformatting has been completed as marking a block causes indent to 
# not move it at all ...
#

PATH=/usr/local/bin:/bin:/usr/bin:$PATH
export PATH

set -e

if [ $# -eq 0 ]; then
  echo "usage: $0 [-v] [-n] [-c] [sourcefile|sourcedir] ..." >&2
  exit 1
fi

VERBOSE=false
DONT=false
STOPARGS=false
COMMENTS=false

33 34 35 36 37 38
# for this exercise, we want to force the openssl style, so we roll
# our own indent profile, which is at a well known location
INDENT_PROFILE="`dirname $0`/indent.pro"
export INDENT_PROFILE
if [ -f "$INDENT_PROFILE" ]; then
  echo "$0: unable to locate the openssl indent.pro file" >&2
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  exit 1
fi

# Extra arguments; for adding the comment-formatting
INDENT_ARGS=""
for i 
do
  if [ "$STOPARGS" != "true" ]; then
    case $i in
      --) STOPARGS="true"; continue;;
      -n) DONT="true"; continue;;
      -v) VERBOSE="true"; 
	  echo "INDENT_PROFILE=$INDENT_PROFILE";
	  continue;;
      -c) COMMENTS="true"; 
      	  INDENT_ARGS="-fc1 -fca -cdb -sc"; 
	  continue;;
    esac
  fi

  if [ -d "$i" ]; then
    LIST=`find "$i" -name '*.[ch]' -print`
  else 
    if [ ! -f "$i" ]; then
      echo "$0: source file not found: $i" >&2
      exit 1
    fi
    LIST="$i"
  fi
  
  for j in $LIST
  do
    # ignore symlinks - we only ever process the base file - so if we
    # expand a directory tree we need to ignore any located symlinks
    if [ -d "$i" ]; then
      if [ -h "$j" ]; then
	continue;
      fi
    fi

    if [ "$VERBOSE" = "true" ]; then
      echo "$j"
    fi

    if [ "$DONT" = "false" ]; then
      tmp=$(mktemp /tmp/indent.XXXXXX)
      trap 'rm -f "$tmp"' HUP INT TERM EXIT

      case $j in 
	# the list of files that indent is unable to handle correctly
	# that we simply leave alone for manual formatting now
	*)
	  if [ "$COMMENTS" = "true" ]; then
	    # we have to mark single line comments as /*- ...*/ to stop indent
	    # messing with them, run expand then indent as usual but with the
	    # the process-comments options and then undo that marking, and then 
	    # finally re-run indent without process-comments so the marked-to-
	    # be-ignored comments we did automatically end up getting moved 
	    # into the right possition within the code as indent leaves marked 
	    # comments entirely untouched - we appear to have no way to avoid 
	    # the double processing and get the desired output
100 101
	    cat "$j" | \
	    expand | \
102
	    perl -0 -np \
D
Dr. Stephen Henson 已提交
103
	      -e 's/(\n#[ \t]*ifdef[ \t]+__cplusplus\n[^\n]*\n#[ \t]*endif\n)/\n\/**INDENT-OFF**\/$1\/**INDENT-ON**\/\n/g;' \
104 105
	      -e 's/(\n\/\*\!)/\n\/**/g;' \
	      -e 's/(STACK_OF|LHASH_OF)\(([^ \t,\)]+)\) /$1_$2_ /g;' \
106
	      | \
107 108 109 110 111 112 113
	    perl -np \
	      -e 's/^([ \t]*)\/\*([ \t]+.*)\*\/[ \t]*$/if (length("$1$2")<75) {$c="-"}else{$c=""}; "$1\/*$c$2*\/"/e;' \
	      -e 's/^\/\* ((Copyright|=|----).*)$/\/*-$1/;' \
	      -e 's/^((DECLARE|IMPLEMENT)_(EXTERN_ASN1|ASN1|ADB|STACK_OF).*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
	      -e 's/^([ \t]*(make_dh|make_dh_bn|make_rfc5114_td)\(.*\)[ \t,]*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
	      -e 's/^(ASN1_ADB_TEMPLATE\(.*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
	      -e 's/^((ASN1|ADB)_.*_END\(.*[\){=,;]+[ \t]*)$/$1\n\/**INDENT-ON**\//;' \
D
Dr. Stephen Henson 已提交
114
	      -e '/ASN1_(ITEM_ref|ITEM_ptr|ITEM_rptr|PCTX)/ || s/^((ASN1|ADB)_[^\*]*[){=,]+[ \t]*)$/\/**INDENT-OFF**\/\n$1/;' \
115 116
	      -e 's/^(} (ASN1|ADB)_[^\*]*[\){=,;]+)$/$1\n\/**INDENT-ON**\//;' \
	      | \
117
	      indent $INDENT_ARGS | \
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	      perl -np \
		-e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \
		-e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \
	      | indent | \
	      perl -0 -np \
		-e 's/\/\*\*INDENT-(ON|OFF)\*\*\/\n//g;' \
	      | perl -np \
	        -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_( |\/)/$1($2)$3/g;' \
	        -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_$/$1($2)/g;' \
	      > "$tmp"
	  else
	    expand "$j" | indent $INDENT_ARGS > "$tmp"
	  fi
	  ;;
      esac
      mv "$tmp" "$j"
    fi
  done
done