Linux 代码格式化工具 indent

以下是我最喜欢的代码格式化脚本:

#!/bin/shif [ $# -lt 1 ]; thenecho "Usage: $0 <file>"exit 1fi# format a source file(*.c, *.h)formatSrcfile(){indent -npro -nip -lp -npsl -npcs -i4 -ts4 -sob -l140 -ss -nsaf -nsai -nsaw -bl -bli 0 $1rm -f *~}for i in $*doformatSrcfile $1shiftdone

=========================================================================================================

参考以下文章: http://blog.csdn.net/httphttpcn/article/details/5940848

linux kernel有一个脚本Lindent,可以帮助你将自己的代码做一个好的代码风格格式化。如:/usr/src/linux-headers-2.6.22-14/scripts/Lindent

常用的设置:indent -npro -kr -i4 -ts4 -sob -l80 -ss -bl -bli 0

(这个风格我喜欢)

参数说明:-npro或–ignore-profile  不要读取indent的配置文件.indent.pro。-kr  指定使用Kernighan&Ritchie的格式。可以换为 -orig,BSD风格-i4  –indent-level 设置缩排的格数为4。-ts4 设置tab的长度-sob或–swallow-optional-blank-lines  删除多余的空白行。-l80 代码超过80换行-ss或–space-special-semicolon  若for区段只有一行时,在分号前加上空格。 -ncs或–no-space-after-casts  不要在cast之后空一格。-bl {分行显示-bli 0 括号缩进为0

我较常用的是:

indent -npro -nip -lp -npsl -npcs -i4 -ts4 -sob -l140 -ss -nsaf -nsai -nsaw -bl -bli 0 代码文件名

功能说明:调整C原始代码文件的格式。语  法:indent [参数][源文件] 或 indent [参数][源文件][-o 目标文件]补充说明:indent可辨识C的原始代码文件,并加以格式化,以方便程序设计师阅读。参  数:-bad或–blank-lines-after-declarations  在声明区段或加上空白行。-bap或–blank-lines-after-procedures  在程序或加上空白行。-bbb或–blank-lines-after-block-comments  在注释区段后加上空白行。-bc或–blank-lines-after-commas  在声明区段中,若出现逗号即换行。-bl或–braces-after-if-line  if(或是else,for等等)与后面执行区段的"{"不同行,且"}"自成一行。-bli<缩排格数>或–brace-indent<缩排格数>  设置{ }缩排的格数。-br或–braces-on-if-line  if(或是else,for等等)与后面执行跛段的"{"不同行,且"}"自成一行。-bs或–blank-before-sizeof  在sizeof之后空一格。-c<栏数>或–comment-indentation<栏数>  将注释置于程序码右侧指定的栏位。-cd<栏数>或–declaration-comment-column<栏数>  将注释置于声明右侧指定的栏位。-cdb或–comment-delimiters-on-blank-lines  注释符号自成一行。-ce或–cuddle-else  将else置于"}"(if执行区段的结尾)之后。-ci<缩排格数>或–continuation-indentation<缩排格数>  叙述过长而换行时,指定换行后缩排的格数。-cli<缩排格数>或–case-indentation-<缩排格数>  使用case时,switch缩排的格数。-cp<栏数>或-else-endif-column<栏数>  将注释置于else与elseif叙述右侧定的栏位。-cs或–space-after-cast  在cast之后空一格。-d<缩排格数>或-line-comments-indentation<缩排格数>  针对不是放在程序码右侧的注释,设置其缩排格数。-di<栏数>或–declaration-indentation<栏数>  将声明区段的变量置于指定的栏位。-fc1或–format-first-column-comments  针对放在每行最前端的注释,设置其格式。-fca或–format-all-comments  设置所有注释的格式。-gnu或–gnu-style.  指定使用GNU的格式,此为预设值。-i<格数>或–indent-level<格数>  设置缩排的格数。-ip<格数>或–parameter-indentation<格数>  设置参数的缩排格数。-kr或–k-and-r-style.  指定使用Kernighan&Ritchie的格式。-lp或–continue-at-parentheses  叙述过长而换行,且叙述中包含了括弧时,将括弧中的每行起始栏位内容垂直对其排列。-nbad或–no-blank-lines-after-declarations  在声明区段后不要加上空白行。-nbap或–no-blank-lines-after-procedures  在程序后不要加上空白行。-nbbb或–no-blank-lines-after-block-comments  在注释区段后不要加上空白行。-nbc或–no-blank-lines-after-commas  在声明区段中,即使出现逗号,仍旧不要换行。-ncdb或–no-comment-delimiters-on-blank-lines  注释符号不要自成一行。-nce或–dont-cuddle-else  不要将else置于"}"之后。-ncs或–no-space-after-casts  不要在cast之后空一格。-nfc1或–dont-format-first-column-comments  不要格式化放在每行最前端的注释。-nfca或–dont-format-comments  不要格式化任何的注释。-nip或–no-parameter-indentation  参数不要缩排。-nlp或–dont-line-up-parentheses  叙述过长而换行,且叙述中包含了括弧时,不用将括弧中的每行起始栏位垂直对其排列。-npcs或–no-space-after-function-call-names  在调用的函数名称之后,不要加上空格。-npro或–ignore-profile  不要读取indent的配置文件.indent.pro。-npsl或–dont-break-procedure-type  程序类型与程序名称放在同一行。-nsc或–dont-star-comments  注解左侧不要加上星号(*)。-nsob或–leave-optional-semicolon  不用处理多余的空白行。-nss或–dont-space-special-semicolon  若for或while区段仅有一行时,在分号前不加上空格。-nv或–no-verbosity  不显示详细的信息。-orig或–original  使用Berkeley的格式。-pcs或–space-after-procedure-calls  在调用的函数名称与"{"之间加上空格。-psl或–procnames-start-lines  程序类型置于程序名称的前一行。-sc或–start-left-side-of-comments  在每行注释左侧加上星号(*)。-sob或–swallow-optional-blank-lines  删除多余的空白行。-ss或–space-special-semicolon  若for或swile区段今有一行时,在分号前加上空格。-st或–standard-output  将结果显示在标准输出设备。-T  数据类型名称缩排。-ts<格数>或–tab-size<格数>  设置tab的长度。-v或–verbose  执行时显示详细的信息。-version  显示版本信息。

alias myindent=’indent -npro -kr -bl -nce -bli0 -i2 -ts2 -sob -l80 -nfc1 -ss -ncs’

Common styles

There are several common styles of C code, including the GNU style, the Kernighan & Ritchie style, and the original Berkeley style. A style may be selected with a singlebackground option, which specifies a set of values for all other options. However, explicitly specified options always override options implied by a background option.

As of version 1.2, the default style of GNU indent is the GNU style. Thus, it is no longer necessary to specify the option ‘-gnu’ to obtain this format, although doing so will not cause an error. Option settings which correspond to the GNU style are:

<strong><em><span style="font-size:12px;">-nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2-ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -nprs -psl -saf -sai-saw -nsc -nsob</span></em></strong>

The GNU coding style is that preferred by the GNU project. It is the style that the GNU Emacs C mode encourages and which is used in the C portions of GNU Emacs. (People interested in writing programs for Project GNU should get a copy ofThe GNU Coding Standards, which also covers semantic and portability issues such as memory usage, the size of integers, etc.)

The Kernighan & Ritchie style is used throughout their well-known book The C Programming Language. It is enabled with the ‘-kr’ option. The Kernighan & Ritchie style corresponds to the following set of options:

<strong><em><span style="font-size:12px;">-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</span></em></strong>

Kernighan & Ritchie style does not put comments to the right of code in the same column at all times (nor does it use only one space to the right of the code), so for this styleindent has arbitrarily chosen column 33.

The style of the original Berkeley indent may be obtained by specifying ‘-orig’ (or by specifying ‘–original’, using the long option name). This style is equivalent to the following settings:

<em><strong><span style="font-size:12px;">-nbad -nbap -bbo -bc -br -brs -c33 -cd33 -cdb -ce -ci4 -cli0-cp33 -di16 -fc1 -fca -hnl -i4 -ip4 -l75 -lp -npcs -nprs -psl-saf -sai -saw -sc -nsob -nss -ts8</span></strong></em>

The Linux style is used in the linux kernel code and drivers. Code generally has to follow the Linux coding style to be accepted. This style is equivalent to the following settings:

<em><strong><span style="font-size:12px;">-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</span></strong></em>

表 1. Indent代码格式化说明

使用的indent参数 值 含义 –blank-lines-after-declarationsbad变量声明后加空行–blank-lines-after-proceduresbap函数结束后加空行–blank-lines-before-block-commentsbbb块注释前加空行–break-before-boolean-operatorbbo较长的行,在逻辑运算符前分行–blank-lines-after-commasnbc变量声明中,逗号分隔的变量不分行–braces-after-if-linebl"if"和"{"分做两行–brace-indent 0bli0"{"不继续缩进–braces-after-struct-decl-linebls定义结构,"struct"和"{"分行–comment-indentationnc33语句后注释开始于行33–declaration-comment-columnncd33变量声明后注释开始于行33–comment-delimiters-on-blank-linesncdb不将单行注释变为块注释–cuddle-do-whilencdw"do — while"的"while"和其前面的"}"另起一行–cuddle-elsence"else"和其前面的"}"另起一行–case-indentation 0cli0switch中的case语句所进0个空格–else-endif-columnncp33#else, #endif后面的注释开始于行33–space-after-castcs在类型转换后面加空格–line-comments-indentation nd0单行注释(不从1列开始的),不向左缩进–break-function-decl-argsnbfda关闭:函数的参数一个一行–declaration-indentationndi2变量声明,变量开始于2行,即不必对齐–format-first-column-commentsnfc1不格式化起于第一行的注释–format-all-commentsnfca不开启全部格式化注释的开关–honour-newlineshnlPrefer to break long lines at the position of newlines in the input.–indent-levelni4设置缩进多少字符,如果为tab的整数倍,用tab来缩进,否则用空格填充。–parameter-indentationnip5旧风格的函数定义中参数说明缩进5个空格–line-length 75l75非注释行最长75–continue-at-parentheseslp续行从上一行出现的括号开始–space-after-procedure-callspcs函数和"("之间插入一个空格–space-after-parenthesesnprs在"("后")"前不插入空格–procnames-start-linespsl将函数名和返回类型放在两行定义–space-after-forsaffor后面有空格–space-after-ifsaiif后面有空格–space-after-whilesawwhile后面有空格–start-left-side-of-commentsnsc不在生成的块注释中加*–swallow-optional-blank-linesnsob不去掉可添加的空行–space-special-semicolonnss一行的for或while语句,在";"前不加空。–tab-sizets4一个tab为4个空格(要能整除"-in")–use-tabsut使用tab来缩进

生活若剥去理想、梦想、幻想,那生命便只是一堆空架子

Linux 代码格式化工具 indent

相关文章:

你感兴趣的文章:

标签云: