The script itself is:
#!/bin/bash # ListBase for loop cleanup # # "Usage: ./listbase_foreach.sh FILE [dryrun]"; # # Note: It runs clang-format in the files so some harmless # false positive changes may occur. if [[ $# -eq 0 ]]; then echo "Replace for loops with LISTBASE_FOREACH" echo "Usage: ./listbase_foreach.sh FILE [dryrun]"; exit 1 fi DRYRUN=0 if [[ $# -gt 1 ]]; then DRYRUN=1; fi FILE="$1" TMP_FILE="/tmp/LISTBASE_FOREACH" if [ ! -f "$FILE" ]; then echo "File invalid: \"$FILE\"" exit 1 fi # The main regex < $FILE tr '\n' '\f' | sed "s/for (\([^;]\+\);[[:space:]]\+\([^;]\+\);[[:space:]]\+\([^;]\+\)) {/for (\1; \2; \3) {/g" | tr '\f' '\n' | sed "s/for (\(const \)\?\(\b\w*\b\) \*\(\b\w*\b\) = \(.*\)\.first; \3; \3 = \3->next) {/LISTBASE_FOREACH (\1\2 *, \3, \&\4) {/" | sed "s/for (\(const \)\?\(\b\w*\b\) \*\(\b\w*\b\) = \(.*\)\->first; \3; \3 = \3->next) {/LISTBASE_FOREACH (\1\2 *, \3, \4) {/" > $TMP_FILE if [[ $DRYRUN -eq 1 ]]; then grep LISTBASE_FOREACH $TMP_FILE else cp $TMP_FILE $FILE clang-format -i $FILE fi
And I ran it with for i in find . -name *.c`; do echo $i; listbase_foreach.sh $i; done`