openssl-format-source 4.5 KB

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