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