openssl-format-source 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. CHANGED=false
  30. DEBUG=""
  31. # for this exercise, we want to force the openssl style, so we roll
  32. # our own indent profile, which is at a well known location
  33. INDENT_PROFILE="$HERE/indent.pro"
  34. export INDENT_PROFILE
  35. if [ ! -f "$INDENT_PROFILE" ]; then
  36. echo "$0: unable to locate the openssl indent.pro file" >&2
  37. exit 1
  38. fi
  39. # Extra arguments; for adding the comment-formatting
  40. INDENT_ARGS=""
  41. for i
  42. do
  43. if [ "$STOPARGS" != "true" ]; then
  44. case $i in
  45. --) STOPARGS="true"; continue;;
  46. -n) DONT="true"; continue;;
  47. -v) VERBOSE="true";
  48. echo "INDENT_PROFILE=$INDENT_PROFILE";
  49. continue;;
  50. -c) COMMENTS="true";
  51. INDENT_ARGS="-fc1 -fca -cdb -sc";
  52. continue;;
  53. -nc) COMMENTS="true";
  54. continue;;
  55. -d) DEBUG='eval tee "$j.pre" |'
  56. continue;;
  57. esac
  58. fi
  59. if [ -d "$i" ]; then
  60. LIST=`find "$i" -name '*.[ch]' -print`
  61. else
  62. if [ ! -f "$i" ]; then
  63. echo "$0: source file not found: $i" >&2
  64. exit 1
  65. fi
  66. LIST="$i"
  67. fi
  68. for j in $LIST
  69. do
  70. # ignore symlinks - we only ever process the base file - so if we
  71. # expand a directory tree we need to ignore any located symlinks
  72. if [ -d "$i" ]; then
  73. if [ -h "$j" ]; then
  74. continue;
  75. fi
  76. fi
  77. if [ "$DONT" = "false" ]; then
  78. tmp=$(mktemp /tmp/indent.XXXXXX)
  79. trap 'rm -f "$tmp"' HUP INT TERM EXIT
  80. case `basename $j` in
  81. # the list of files that indent is unable to handle correctly
  82. # that we simply leave alone for manual formatting now
  83. obj_dat.h|aes_core.c|aes_x86core.c|ecp_nistz256.c)
  84. echo "skipping $j"
  85. ;;
  86. *)
  87. if [ "$COMMENTS" = "true" ]; then
  88. # we have to mark single line comments as /*- ...*/ to stop indent
  89. # messing with them, run expand then indent as usual but with the
  90. # the process-comments options and then undo that marking, and then
  91. # finally re-run indent without process-comments so the marked-to-
  92. # be-ignored comments we did automatically end up getting moved
  93. # into the right possition within the code as indent leaves marked
  94. # comments entirely untouched - we appear to have no way to avoid
  95. # the double processing and get the desired output
  96. cat "$j" | \
  97. expand | \
  98. perl -0 -np \
  99. -e 's/(\n#[ \t]*ifdef[ \t]+__cplusplus\n[^\n]*\n#[ \t]*endif\n)/\n\/**INDENT-OFF**\/$1\/**INDENT-ON**\/\n/g;' \
  100. -e 's/(\n\/\*\!)/\n\/**/g;' \
  101. -e 's/(STACK_OF|LHASH_OF)\(([^ \t,\)]+)\)( |\n)/$1_$2_$3/g;' \
  102. | \
  103. perl -np \
  104. -e 's/^([ \t]*)\/\*([ \t]+.*)\*\/[ \t]*$/if (length("$1$2")<75) {$c="-"}else{$c=""}; "$1\/*$c$2*\/"/e;' \
  105. -e 's/^\/\* ((Copyright|=|----).*)$/\/*-$1/;' \
  106. -e 's/^((DECLARE|IMPLEMENT)_(EXTERN_ASN1|ASN1|ADB|STACK_OF|PKCS12_STACK_OF).*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
  107. -e 's/^([ \t]*(make_dh|make_dh_bn|make_rfc5114_td)\(.*\)[ \t,]*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
  108. -e 's/^(ASN1_ADB_TEMPLATE\(.*)$/\/**INDENT-OFF**\/\n$1\n\/**INDENT-ON**\//;' \
  109. -e 's/^((ASN1|ADB)_.*_(end|END)\(.*[\){=,;]+[ \t]*)$/$1\n\/**INDENT-ON**\//;' \
  110. -e '/ASN1_(ITEM_ref|ITEM_ptr|ITEM_rptr|PCTX)/ || s/^((ASN1|ADB)_[^\*]*[){=,]+[ \t]*)$/\/**INDENT-OFF**\/\n$1/;' \
  111. -e 's/^(} (ASN1|ADB)_[^\*]*[\){=,;]+)$/$1\n\/**INDENT-ON**\//;' \
  112. | \
  113. $DEBUG indent $INDENT_ARGS | \
  114. perl -np \
  115. -e 's/^([ \t]*)\/\*-(.*)\*\/[ \t]*$/$1\/*$2*\//;' \
  116. -e 's/^\/\*-((Copyright|=|----).*)$/\/* $1/;' \
  117. | indent | \
  118. perl -0 -np \
  119. -e 's/\/\*\*INDENT-(ON|OFF)\*\*\/\n//g;' \
  120. | perl -np \
  121. -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_( |\/)/$1($2)$3/g;' \
  122. -e 's/(STACK_OF|LHASH_OF)_([^ \t,]+)_$/$1($2)/g;' \
  123. | perl "$HERE"/su-filter.pl \
  124. > "$tmp"
  125. else
  126. expand "$j" | indent $INDENT_ARGS > "$tmp"
  127. fi;
  128. if cmp -s "$tmp" "$j"; then
  129. if [ "$VERBOSE" = "true" ]; then
  130. echo "$j unchanged"
  131. fi
  132. rm "$tmp"
  133. else
  134. if [ "$VERBOSE" = "true" ]; then
  135. echo "$j changed"
  136. fi
  137. CHANGED=true
  138. mv "$tmp" "$j"
  139. fi
  140. ;;
  141. esac
  142. fi
  143. done
  144. done
  145. if [ "$VERBOSE" = "true" ]; then
  146. echo
  147. if [ "$CHANGED" = "true" ]; then
  148. echo "SOURCE WAS MODIFIED"
  149. else
  150. echo "SOURCE WAS NOT MODIFIED"
  151. fi
  152. fi