]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/uncrusticracy.sh
Start of uncrusticracy script, and kill trailing whitespace in all sources.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / uncrusticracy.sh
1 FILES=`find client common menu server warpzonelib -name \*.c -o -name \*.h -o -name \*.qc -o -name \*.qh`
2 out_raw()
3 {
4         printf "%s\n" "$LINE"
5 }
6 out()
7 {
8         printf "%-40s = %-8s # %s\n" "$KEY" "$VAL" "$COMMENT"
9 }
10 decide()
11 {
12         verybestchoice=
13         verybestscore=2147483647
14         bestchoice=
15         bestscore=2147483647
16         secondbestchoice=
17         secondbestscore=2147483647
18         worstscore=0
19         haveignore=false
20         havefalse=false
21         have0=false
22         seen=
23         for choice in "$VAL" "$@"; do
24                 case " $seen " in
25                         *" $choice "*)
26                                 continue
27                                 ;;
28                         *)
29                                 seen=$seen" $VAL"
30                                 ;;
31                 esac
32                 if [ x"$choice" = x"force" ]; then
33                         continue
34                 fi
35                 if [ x"$choice" = x"ignore" ]; then
36                         haveignore=true
37                 fi
38                 if [ x"$choice" = x"false" ]; then
39                         havefalse=true
40                 fi
41                 if [ x"$choice" = x"0" ]; then
42                         have0=true
43                 fi
44                 if [ x"$MODE" = x"initialize" ]; then
45                         if [ x"$choice" = x"ignore" ]; then
46                                 score=0
47                         else
48                                 score=2147483647
49                         fi
50                 else
51                         {
52                                 cat uncrustify.cfg
53                                 printf "%s = %s\n" "$KEY" "$choice"
54                         } > uncrustify.cfg.test
55                         uncrustify -c uncrustify.cfg.test --replace --no-backup $FILES >/dev/null 2>&1
56                         score=0
57                         git diff --numstat > diffstat.tmp
58                         while read -r add del rest; do
59                                 if [ x"$add" != x"-" ]; then
60                                         score=$(($score + $add))
61                                 fi
62                                 if [ x"$del" != x"-" ]; then
63                                         score=$(($score + $del))
64                                 fi
65                         done < diffstat.tmp
66                         git reset --hard >/dev/null 2>&1
67                 fi
68                 echo >&2 "$KEY = $choice: $score"
69                 if [ x"$choice" != x"ignore" ]; then
70                         if [ $score -lt $bestscore ]; then
71                                 secondbestscore=$bestscore
72                                 secondbestchoice=$bestchoice
73                                 bestscore=$score
74                                 bestchoice=$choice
75                         elif [ $score -lt $secondbestscore ]; then
76                                 secondbestscore=$score
77                                 secondbestchoice=$choice
78                         fi
79                 fi
80                 if [ $score -lt $verybestscore ]; then
81                         verybestscore=$score
82                         verybestchoice=$choice
83                 fi
84                 if [ $score -gt $worstscore ]; then
85                         worstscore=$score
86                         worstchoice=$choice
87                 fi
88         done
89         if [ -z "$bestchoice" ]; then
90                 echo "# WARNING: No best choice identified"
91         elif [ $verybestscore -ge $worstscore ]; then
92                 echo "# WARNING: Code doesn't seem to use this feature - delete from the config?"
93                 if $haveignore; then
94                         VAL=ignore
95                 elif $havefalse; then
96                         VAL=false
97                 elif $have0; then
98                         VAL=0
99                 fi
100         elif [ $bestscore -ge $worstscore ]; then
101                 echo "# WARNING: Indifferent... please decide manually."
102         elif [ $bestscore -ge $secondbestscore ]; then
103                 echo "# WARNING: Best is not unique ($bestchoice $secondbestchoice)"
104         elif [ $bestscore -gt $verybestscore ]; then
105                 echo "# NOTE: is $(($bestscore - $verybestscore)) worse than $verybestchoice"
106                 VAL=$bestchoice
107         else
108                 VAL=$bestchoice
109         fi
110 }
111 while read -r LINE; do
112         case "$LINE" in
113                 "# NOTE: "*)
114                         continue
115                         ;;
116                 "# WARNING: "*)
117                         continue
118                         ;;
119                 "# ERROR: "*)
120                         continue
121                         ;;
122                 "#"*)
123                         out_raw
124                         continue
125                         ;;
126                 *"#force"*)
127                         out_raw
128                         continue
129                         ;;
130         esac
131         printf "%s\n" "$LINE" | while read KEY EQ VAL DELIM COMMENT; do
132                 if \
133                         [ x"$EQ" != x"=" ] || \
134                         [ x"$DELIM" != x"#" ]; then
135                         out_raw
136                         continue
137                 fi
138                 case "$COMMENT" in
139                         number)
140                                 case "$KEY" in
141                                         indent_columns|*tab*)
142                                                 decide 1 2 4 8
143                                                 ;;
144                                         *)
145                                                 decide 0 1 2 3 4 indent_columns
146                                                 ;;
147                                 esac
148                                 out
149                                 ;;
150                         string)
151                                 printf "# WARNING: unsupported %s\n" "$COMMENT"
152                                 out_raw
153                                 ;;
154                         *[!a-z/_]*)
155                                 printf "# ERROR: invalid characters %s\n" "$COMMENT"
156                                 out_raw
157                                 ;;
158                         */*)
159                                 decide `echo "$COMMENT" | tr / ' '`
160                                 out
161                                 ;;
162                         *)
163                                 printf "# ERROR: only once choice %s\n" "$COMMENT"
164                                 out_raw
165                                 ;;
166                 esac
167         done
168 done < uncrustify.cfg