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