b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | function sa_set_param() |
| 4 | { |
| 5 | if [ "$1" = "tel_ptk" ];then |
| 6 | PARAM="-npro -kr -i8 -ts8 -sob -l120 -ss -ncs -cp1" |
| 7 | else |
| 8 | PARAM="-npro -kr -i8 -ts8 -sob -l120 -ss -ncs -cp1 -bl -bli0 -bls -nce -cli8" |
| 9 | fi |
| 10 | local RES=`indent --version` |
| 11 | local V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1` |
| 12 | local V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2` |
| 13 | local V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3` |
| 14 | if [ $V1 -gt 2 ]; then |
| 15 | PARAM="$PARAM -il0" |
| 16 | elif [ $V1 -eq 2 ]; then |
| 17 | if [ $V2 -gt 2 ]; then |
| 18 | PARAM="$PARAM -il0"; |
| 19 | elif [ $V2 -eq 2 ]; then |
| 20 | if [ $V3 -ge 10 ]; then |
| 21 | PARAM="$PARAM -il0" |
| 22 | fi |
| 23 | fi |
| 24 | fi |
| 25 | } |
| 26 | |
| 27 | function sa_usage() |
| 28 | { |
| 29 | echo "Simple code format utility inspired by \${android_root}/kernel/kernel/scripts/Lindent" |
| 30 | echo "Usage : `basename $0` [Options] <file1> <file2> ..." |
| 31 | echo "Options:" |
| 32 | echo "-h - help" |
| 33 | echo "-a - apply format recursively on all *.[ch] files" |
| 34 | echo "-o - use old (non-default) format style" |
| 35 | echo "Examples:" |
| 36 | echo " `basename $0` -a" |
| 37 | echo " `basename $0` code.c newcode.c newcode.h" |
| 38 | echo " `basename $0` -o code.c newcode.c newcode.h" |
| 39 | exit 1 |
| 40 | } |
| 41 | |
| 42 | function sa_make_indent() |
| 43 | { |
| 44 | export SIMPLE_BACKUP_SUFFIX=.INDENT_BAK |
| 45 | echo files ${#@} |
| 46 | if [ ${#@} -eq 0 ]; then |
| 47 | echo "ABORT: no files supplied." |
| 48 | sa_usage |
| 49 | fi |
| 50 | #echo indent $PARAM "$@" |
| 51 | echo "################################" |
| 52 | for i in "$@"; do |
| 53 | fromdos "$i" |
| 54 | indent $PARAM "$i" |
| 55 | diff $i ${i}${SIMPLE_BACKUP_SUFFIX} 1>/dev/null 2>&1 |
| 56 | if [ $? -eq 1 ]; then |
| 57 | echo Changed: ${i} |
| 58 | fi |
| 59 | rm ${i}${SIMPLE_BACKUP_SUFFIX} #2>&- || true |
| 60 | done |
| 61 | } |
| 62 | |
| 63 | sa_doall=0 |
| 64 | sa_doold=0 |
| 65 | sa_set_param tel_ptk |
| 66 | while getopts ":aoh" opt; do |
| 67 | case $opt in |
| 68 | a)sa_doall=1;; |
| 69 | o)sa_set_param tel_ptk_old_style;; |
| 70 | h)sa_usage;; |
| 71 | \?)sa_usage;; |
| 72 | esac |
| 73 | done |
| 74 | |
| 75 | if [ $sa_doall -eq 1 ]; then |
| 76 | sa_make_indent `find -type f -a -name \*.[ch]` |
| 77 | else |
| 78 | sa_make_indent $@ |
| 79 | fi |
| 80 | |