Table of Contents
Linux Coding Style
Formatting Tools
Code generally has to follow the Linux coding style to be accepted. Basically there are two tools for formatting the code: indent and astyle
indent
The indent program, an excellent GNU utility found on most Linux systems, formats source according to given rules. Refer to http://linux.die.net/man/1/indent
The default settings are for the GNU coding style, which is not too pretty.
To get the utility to follow the Linux kernel style, the simple usage is :
indent -kr -i8 -ts8 -sob -l80 -ss -bs -psl < file >
Note: -l is new as of version 2.1 however and not very intelligent or flexible yet.
Maybe need to add “--ignore-newlines” to disable -l80
The Kernighan & Ritchie style corresponds to the following set of options:
-nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 -cp33 -cs -d0 -di1 -nfc1 -nfca -hnl -i4 -ip0 -l75 -lp -npcs -nprs -npsl -saf -sai -saw -nsc -nsob -nss
This Linux style (-linux, –linux-style ) is equivalent to the following settings:
-nbad -bap -nbc -bbo -hnl -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 -d0 -di1 -nfc1 -i8 -ip0 -l80 -lp -npcs -nprs -npsl -sai -saf -saw -ncs -nsc -sob -nfca -cp33 -ss -ts8 -il1
| -kr | The C Programming Language The -kr from The the author of The C Programming Language, Brian Kernighan and Dennis Ritchie (Prentice Hall, ISBN# 0-13-11-362-8) |
| -i8 | indentation level to 8 spaces |
| -ts8 | Set tab size to 8 spaces. |
| -sob –swallow-optional-blank-lines | Swallow optional blank lines. |
| -l80 | Set maximum line length for non-comment lines to 80 characters. |
| -ss\\–space-special-semicolon | On one-line for and while statements, force a blank before the semicolon. |
| -bs –Bill-Shannon –blank-before-sizeof | Put a space between sizeof and its argument. |
| -psl –procnames-start-lines | Put the type of a procedure on the line before its name. |
| -nbad | |
| -bap | |
| -nbc | |
| -bbo | |
| -hnl | |
| -br | |
| -brs | |
| -c33 | |
| -cd33 | |
| -ncdb | |
| -ce | |
| -ci4 | |
| -cli0 | |
| -d0 | |
| -di1 | |
| -nfc1 | |
| -i8 | |
| -ip0 | |
| -l80 | |
| -lp | |
| -npcs | |
| -nprs | |
| -npsl | |
| -sai | |
| -saf | |
| -saw | |
| -ncs | |
| -nsc | |
| -sob | |
| -nfca | |
| -cp33 | |
| -ss | |
| -ts8 | |
| -il1 |
Options' Cross Key
Here is a list of options alphabetized by long option, to help you find the corresponding short option.
--blank-lines-after-commas -bc --blank-lines-after-declarations -bad --blank-lines-after-procedures -bap --blank-lines-before-block-comments -bbb --braces-after-if-line -bl --braces-after-func-def-line -blf --brace-indent -bli --braces-after-struct-decl-line -bls --braces-on-if-line -br --braces-on-func-def-line -brf --braces-on-struct-decl-line -brs --break-after-boolean-operator -nbbo --break-before-boolean-operator -bbo --break-function-decl-args -bfda --break-function-decl-args-end -bfde --case-indentation -clin --case-brace-indentation -cbin --comment-delimiters-on-blank-lines -cdb --comment-indentation -cn --continuation-indentation -cin --continue-at-parentheses -lp --cuddle-do-while -cdw --cuddle-else -ce --declaration-comment-column -cdn --declaration-indentation -din --dont-break-function-decl-args -nbfda --dont-break-function-decl-args-end -nbfde --dont-break-procedure-type -npsl --dont-cuddle-do-while -ncdw --dont-cuddle-else -nce --dont-format-comments -nfca --dont-format-first-column-comments -nfc1 --dont-line-up-parentheses -nlp --dont-left-justify-declarations -ndj --dont-space-special-semicolon -nss --dont-star-comments -nsc --else-endif-column -cpn --format-all-comments -fca --format-first-column-comments -fc1 --gnu-style -gnu --honour-newlines -hnl --ignore-newlines -nhnl --ignore-profile -npro --indent-label -iln --indent-level -in --k-and-r-style -kr --leave-optional-blank-lines -nsob --leave-preprocessor-space -lps --left-justify-declarations -dj --line-comments-indentation -dn --line-length -ln --linux-style -linux --no-blank-lines-after-commas -nbc --no-blank-lines-after-declarations -nbad --no-blank-lines-after-procedures -nbap --no-blank-lines-before-block-comments -nbbb --no-comment-delimiters-on-blank-lines -ncdb --no-space-after-casts -ncs --no-parameter-indentation -nip --no-space-after-for -nsaf --no-space-after-function-call-names -npcs --no-space-after-if -nsai --no-space-after-parentheses -nprs --no-space-after-while -nsaw --no-tabs -nut --no-verbosity -nv --original -orig --parameter-indentation -ipn --paren-indentation -pin --preserve-mtime -pmt --preprocessor-indentation -ppin --procnames-start-lines -psl --space-after-cast -cs --space-after-for -saf --space-after-if -sai --space-after-parentheses -prs --space-after-procedure-calls -pcs --space-after-while -saw --space-special-semicolon -ss --standard-output -st --start-left-side-of-comments -sc --struct-brace-indentation -sbin --swallow-optional-blank-lines -sob --tab-size -tsn --use-tabs -ut --verbose -v
Astyle
astyle special usage based on V2.0.4 http://astyle.sourceforge.net/astyle.html
Example: astyle –options=none –align-pointer=name –pad=oper –unpad-paren –pad-paren-out –trim-trailing-whitespace
–options=none
Disable the default options file. Only the command-line parameters will be used.
-p, –pad=oper
Insert space padding around operators only. for example: i-j becomes i - j
-U, –unpad-paren
Remove space padding around parenthesis on the inside and out‐ side. Can be used in combination with the paren padding options. Only padding that has not been requested by other options will be removed.
-d, –pad-paren-out
Insert space paddings around parenthesies on the outside only.
This can be used with unpad=paren to remove unwanted spaces.
With -U -d two options, it will be like if (k > 0), not if( k > 0 )
-k3, –align-pointer=name
Attach a pointer or reference operator (*, &, or ^) to the variable name (right).
for example: **char* p** becomes **char *p**
Perl/Bash
Use perl to remove trailing whitespace in a string:
$str =~ s/\s+$//;
Take 1 or more white spaces (\s+) till the end of the string ($), and replace them with an empty string.
