Linux内核文件中的indent脚本 » 荒野无灯weblog

Keep It Simple, Stupid.

荒野无灯weblog

Linux内核文件中的indent脚本

今天在博客园看到有人写C语言代码美化工具,于是让我想起了indent和splint…

[admin@huangye scripts]$ pwd
/usr/src/linux-3.1.4-1-ARCH/scripts
[admin@huangye scripts]$ cat Lindent 
#!/bin/sh
PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
RES=`indent --version`
V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
if [ $V1 -gt 2 ]; then
  PARAM="$PARAM -il0"
elif [ $V1 -eq 2 ]; then
  if [ $V2 -gt 2 ]; then
    PARAM="$PARAM -il0";
  elif [ $V2 -eq 2 ]; then
    if [ $V3 -ge 10 ]; then
      PARAM="$PARAM -il0"
    fi
  fi
fi
indent $PARAM "$@"
[admin@huangye scripts]$ 

参数解释:

-bad, –blank-lines-after-declarations
声明之后加空行

-nbad, –no-blank-lines-after-declarations
Do not force blank lines after declarations.
声明之后不加空行

-bap, –blank-lines-after-procedures
函数体之后加空行

-nbap, –no-blank-lines-after-procedures
Do not force blank lines after procedure bodies.
函数体之后不加空行

-bbb, –blank-lines-before-block-comments
在块注释之前加空行
(块注释: /* xxx */)

-bbo, –break-before-boolean-operator
在boolean操作符(||, &&)之前换行(长语句)

-nbbo, –break-after-boolean-operator
Do not prefer to break long lines before boolean operators.
在boolean操作符(||, &&)之前不换行(长语句)

-hnl, –honour-newlines
Prefer to break long lines at the position of newlines in the input.
新行优先

-nhnl, –ignore-newlines
Do not prefer to break long lines at the position of newlines in the input.
非新行优先

-bc, –blank-lines-after-commas
在声明中, 逗号之后换行(非函数声明)

-nbc, –no-blank-lines-after-commas
Do not force newlines after commas in declarations.
在声明中, 逗号之后不换行(非函数声明)(建议使用)

-bfda, –break-function-decl-args
在函数的声明中, 所有参数之前换行

-nbfda, –dont-break-function-decl-args
Don’t put each argument in a function declaration on a seperate line.
在函数的声明中, 不把参数单独放置一行

-bfde, –break-function-decl-args-end
Break the line after the last argument in a declaration.
在函数参数列表中, 最后一个参数后换行

-bl, –braces-after-if-line
在if所在行的下一行加’{‘

-blin, –brace-indentn
在’{‘前缩进n个空格

-br, –braces-on-if-line
在if所在的行最后加’{‘

-brs, –braces-on-struct-decl-line
Put braces on struct declaration line.
See DECLARATIONS.

-bs, –Bill-Shannon, –blank-before-sizeof
在sizeof和它的参数之间加空格
e.g. sizeof(INT) => sizeof (INT)

-cn, –comment-indentationn
Put comments to the right of code in column n.
把注释放在代码右方, n列的地方(仅对代码后的注释有效)

-cdn, –declaration-comment-columnn
Put comments to the right of the declarations in column n.
把注释放在声明右方, n列的地方(仅对声明后的注释有效)

-cpn, –else-endif-columnn
Put comments to the right of #else and #endif statements in column n.
把注释放在#else/#endif右方, n列的地方(仅对#else/#endif后的注释有效)

-cbin, –case-brace-indentationn
Indent braces after a case label N spaces.
case后(case下一行)的’{‘缩进n空格

-cdb, –comment-delimiters-on-blank-lines
Put comment delimiters on blank lines.
把注释符放在空行.
/* comment */
=>
/*
comment
*/
(建议不使用)

-ncdb, –no-comment-delimiters-on-blank-lines
Do not put comment delimiters on blank lines.
不把注释符放在空行.

-cdw, –cuddle-do-while
do {
x–;
} while (x);

-ncdw, –dont-cuddle-do-while
do {
x–;
}
while (x);
(建议不使用)

-ce, –cuddle-else
Cuddle else and preceeding ‘}’.
else紧靠’{‘
(建议不使用)

-nce, –dont-cuddle-else
Do not cuddle } and else.
else不紧靠’{‘(建议 -bli0 和 -nce一起使用)

-cin, –continuation-indentationn
Continuation indent of n spaces.
长语句换行时, 第二行相对于第一行缩进n个空格

-clin, –case-indentationn
case之前缩进n个空格

-cs, –space-after-cast
Put a space after a cast operator.
类型转换后加空格

-ncs, –no-space-after-casts
Do not put a space after cast operators.
类型转换后不加空格

-dn, –line-comments-indentationn
Set indentation of comments not to the right of code to n spaces.
If the comment begins a line (i.e., there is no program text to its left),
it will be indented to the column it was found in unless the comment
is within a block of code. In that case, such a comment will be
aligned with the indented code of that block (unless the comment
began in the first column). This alignment may be affected by the `-d’
option, which specifies an amount by which such comments are moved to
the left, or unindented. For example, `-d2′ places comments two spaces
to the left of code. By default, comments are aligned with code, unless
they begin in the first column, in which case they are left there by
default — to get them aligned with the code, specify `-fc1′.

-din, –declaration-indentationn
Put variables in column n.
把变量放在第n列. 如果n很小(例如1或者2), 变量将被放在下一行第n列
(建议 -di7, 因为关键字static的长度是6)

-fc1, –format-first-column-comments
Format comments in the first column.
格式化第一列的注释

-nfc1, –dont-format-first-column-comments
Do not format comments in the first column as normal.
不格式化第一列的注释

-fca, –format-all-comments
Do not disable all formatting of comments.
不屏蔽所有注释的格式化

-nfca, –dont-format-comments
Do not format any comments.
不格式化任何注释

-gnu, –gnu-style
Use GNU coding style. This is the default.
GNU风格, 这也是默认的风格

-in, –indent-leveln
Set indentation level to n spaces.
嵌套if/for, 或者’{}’之间语句的缩进

-ipn, –parameter-indentationn
Indent parameter types in old-style function definitions by n spaces.
老式函数definition参数的缩进. 以及长if语句. (建议用 -ip8)

-nip, –no-parameter-indentation
Zero width indentation for parameters.
参数0缩进

-kr, –k-and-r-style
Use Kernighan & Ritchie coding style.
KR风格

-ln, –line-lengthn
Set maximum line length for non-comment lines to n.
每行最大长度(非注释行)

-lcn, –comment-line-lengthn
Set maximum line length for comment formatting to n.
注释行的最大长度

-lp, –continue-at-parentheses
Line up continued lines at parentheses.
当语句过长, 左圆括号不能和右圆括号一起出现在一行时, 剩余的语句将会在下一行
和左圆括号对齐.
-nlp -ci3: (建议-nlp -ci8)
p1 = first_procedure (second_procedure (p2, p3),
third_procedure (p4, p5));
-lp:
p1 = first_procedure (second_procedure (p2, p3),
third_procedure (p4, p5));

-nlp, –dont-line-up-parentheses
Do not line up parentheses.
不对齐圆括号

-lps, –leave-preprocessor-space
Leave space between ‘#’ and preprocessor directive.
‘#’与预编译指示符加空格 (建议使用-ppi3)

-pcs, –space-after-procedure-calls
Insert a space between the name of the procedure being called and the ‘(’.
函数名和’(‘之间加空格

-npcs, –no-space-after-function-call-names
Do not put space after the function in function calls.
函数名和’(‘之间没有空格

-prs, –space-after-parentheses
Put a space after every ’(’ and before every ’)’.
每个’(‘之后和每个’)'之前加空格

-nprs, –no-space-after-parentheses
Do not put a space after every ’(’ and before every ’)’.
每个’(‘之后和每个’)'之前不加空格

-psl, –procnames-start-lines
Put the type of a procedure on the line before its name.
函数返回值与函数名分两行
(建议使用)

-npsl, –dont-break-procedure-type
Put the type of a procedure on the same line as its name.
函数返回值与函数名同行

-saf, –space-after-for
Put a space after each for.
for之后加空格
(建议使用)

-nsaf, –no-space-after-for
Do not put a space after every for.
for之后不加空格

-sai, –space-after-if
Put a space after each if.
if之后加空格
(建议使用)

-nsai, –no-space-after-if
Do not put a space after every if.
if之后不加空格

-saw, –space-after-while
Put a space after each while.
while之后加空格
(建议使用)

-nsaw, –no-space-after-while
Do not put a space after every while.
while之后不加空格

-sc, –start-left-side-of-comments
Put the ‘*’ character at the left of comments.
块注释左边加’*’
(建议使用)

-nsc, –dont-star-comments
Do not put the ‘*’ character at the left of comments.
块注释左边不加’*’

-sob, –swallow-optional-blank-lines
Swallow optional blank lines.
吞(去)掉多余的空行
(建议使用)

-nsob, –leave-optional-blank-lines
Do not swallow optional blank lines.
不吞(去)掉多余的空行

-ss, –space-special-semicolon
On one-line for and while statments, force a blank before the semicolon.
单行的for或者while语句里面, 分号后加空格
(建议使用)

-nss, –dont-space-special-semicolon
Do not force a space before the semicolon after certain statements.Disables ‘-ss’
单行的for或者while语句里面, 分号后不加空格

-ut, –use-tabs
Use tabs. This is the default.
使用tab

-nut, –no-tabs
Use spaces instead of tabs.
用空格代替tab
(建议使用)

-v, –verbose
Enable verbose mode.
冗余模式.

-nv, –no-verbosity
Disable verbose mode.
禁用冗余模式

-orig, –original
Use the original Berkeley coding style.
Berkeley风格

-npro, –ignore-profile
Do not read ‘.indent.pro’ files.
不读取’.indent.pro’文件

-pin, –paren-indentationn
Specify the extra indentation per open parentheses ’(’ when a statement is broken.

-pmt, –preserve-mtime
Preserve access and modification times on output files.
保护文件, 修改输出文件的时间戳

-sbin, –struct-brace-indentationn
Indent braces of a struct, union or enum N spaces.
struct, union, enum 之后’{‘的缩进, 建议 -sbi0

-st, –standard-output
Write to standard output.
写到标准输出

-T
Tell indent the name of typenames.
告诉indent所typedef的类型

-tsn, –tab-sizen
Set tab size to n spaces.
设置tab为n个空格

-version
Output the version number of indent.
查看版本

参数中文翻译来自:http://blog.csdn.net/yoic/article/details/627041

Tagged in :

All Comments (0)
Gravatar image
No Comments