openssl-format-source 5.3 KB

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