Code formatting using shell script
This is a little script to automatically format source code of different languages, just copy it into a directory that is in your PATH.
You can also add an alias to your ~/.bashrc
alias cstyle='cs style'
alias cclean='cs clean'
#!/bin/bash
#
# codingstyle.sh (or short cs)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# A little script to automatically apply my preferred coding style
# to all .c, .cpp, .h, .java and .py files in and below the current directory
# Just copy it into a directory that is in your PATH and rename the file to cs.
#
# Author: Christian Jann <christian 0x2e jann 0x40 ymail 0x2e com>
# URL: http://www.jann.cc/2013/02/20/code_formatting_using_shell_script.html
#
# Requirements:
#
# * astyle (http://astyle.sourceforge.net/astyle.html)
# * autopep8 (http://pypi.python.org/pypi/autopep8/)
# (only if it finds Python files)
#
case "$1" in
style)
echo "Formating source files..."
# Modified kdelibs coding style as defined in
# http://techbase.kde.org/Policies/Kdelibs_Coding_Style
find -regex ".*\.\(c\|cpp\|h\|java\)" -exec \
astyle --indent=spaces=4 --brackets=break \
--indent-labels --pad-oper --unpad-paren --pad-header \
--keep-one-line-statements --convert-tabs \
--indent-preprocessor "{}" \;
# Other variants:
# Maybe use --mode=java for java files, --mode=c
# find . -perm -200 -regex ".*[.][CHch]p*" -exec astyle \
# --suffix=none --style=ansi --convert-tabs "{}" \;
# find -regex ".*[.][CHch]p*" -exec astyle --style=attach "{}" \;
# astyle --indent=spaces=4 --brackets=break \
# --indent-labels --pad-oper --unpad-paren --pad-header \
# --keep-one-line-statements --convert-tabs \
# --indent-preprocessor \
# `find -type f -name '*.c'` \
# `find -type f -name '*.cpp'` \
# `find -type f -name '*.h'` \
# `find -type f -name '*.java'`
# Apply coding conventions for Python code
# http://www.python.org/dev/peps/pep-0008/
# https://pypi.python.org/pypi/autopep8/
# https://github.com/jcrocholl/pep8
# sudo pip-python install --upgrade autopep8
for file in $(find . -name "*.py")
do
#echo "Creating backup: $file.orig"
#cp -v $file{,.orig}
cp $file{,.orig}
#echo "Formating file: $file"
autopep8 -i "$file"
diff "$file" "$file.orig" >> /dev/null \
&& echo "Unchanged $file" || echo "Formatted $file"
done
;;
clean)
echo "Deleting temporary files and backup copies..."
#find . \( -name "*.orig" -or -name "*~" \) -exec rm -v "{}" \;
#find -regex ".*\(orig\|~\|pyc\|bak\)" -delete
find -regex ".*\(orig\|~\|pyc\|bak\)" -exec rm -v "{}" \;
;;
*)
echo "run 'cs style' to format all"\
".c .cpp .h .java and .py files recursively"
echo "run 'cs clean' to delete temporary"\
"files and backup copies (*.orig)"
;;
esac
Usage example
[chris@thinkpad raspberrypi-modio-web.git]$ cs
run 'cs style' to format all .c .cpp .h .java and .py files recursively
run 'cs clean' to delete temporary files and backup copies (*.orig)
[chris@thinkpad raspberrypi-modio-web.git]$ cs style
Formating source files...
Formatted ./run.py
Formatted ./app/app.py
Formatted ./app/hardware.py
Formatted ./app/modio.py
Unchanged ./app/__init__.py
Unchanged ./app/settings.py
Formatted ./app/users.py
Formatted ./app/gpios.py
[chris@thinkpad raspberrypi-modio-web.git]$ cs clean
Deleting temporary files and backup copies...
removed `./app/settings.py.orig'
removed `./app/__init__.py.orig'
removed `./app/app.py.orig'
removed `./app/modio.pyc'
removed `./app/settings.pyc'
removed `./app/gpios.pyc'
removed `./app/hardware.py.orig'
removed `./app/gpios.py.orig'
removed `./app/users.pyc'
removed `./app/__init__.pyc'
removed `./app/hardware.pyc'
removed `./app/modio.py.orig'
removed `./app/users.py.orig'
removed `./app/app.pyc'
removed `./run.py.orig'
[chris@thinkpad raspberrypi-modio-web.git]$
[chris@thinkpad microcli-stm32f0-discovery]$ cs clean
Deleting temporary files and backup copies...
removed `./README.md~'
[chris@thinkpad microcli-stm32f0-discovery]$ cd src/
[chris@thinkpad src]$ cs style
Formating source files...
Unchanged ./usart.c
Unchanged ./cli.c
Unchanged ./wifi.c
Unchanged ./system_stm32f0xx.c
Formatted ./main.c
Formatted ./stm32f0_discovery.c
Formatted ./stm32f0xx_it.c
[chris@thinkpad src]$ diff stm32f0xx_it.c stm32f0xx_it.c.orig
172c172
< if (EXTI_GetITStatus(USER_BUTTON_EXTI_LINE) != RESET)
---
> if(EXTI_GetITStatus(USER_BUTTON_EXTI_LINE) != RESET)
175c175
<
---
>
[chris@thinkpad src]$ git diff stm32f0xx_it.c
diff --git a/src/stm32f0xx_it.c b/src/stm32f0xx_it.c
index e0f9996..da4367f 100644
--- a/src/stm32f0xx_it.c
+++ b/src/stm32f0xx_it.c
@@ -169,10 +169,10 @@ void USART1_IRQHandler(void)
*/
void EXTI0_1_IRQHandler(void)
{
- if(EXTI_GetITStatus(USER_BUTTON_EXTI_LINE) != RESET)
+ if (EXTI_GetITStatus(USER_BUTTON_EXTI_LINE) != RESET)^M
{
UserButtonPressed = 0x01;
-
+^M
/* Clear the EXTI line pending bit */
EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE);
}
[chris@thinkpad src]$ cs clean
Deleting temporary files and backup copies...
removed `./stm32f0_discovery.c.orig'
removed `./stm32f0xx_it.c.orig'
removed `./main.c.orig'
[chris@thinkpad src]$
Download the script: codingstyle.sh