Linux 代码格式化工具 indent

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

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

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

参考以下文章: 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来缩进

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/402292.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android处理ListView中的Item中的Button按钮不能点击的问题

问题描述&#xff1a;ListView列表中的Button按钮按钮不能点击 解决办法&#xff1a;在ListView中的Item项的布局文件中加上&#xff1a;android:descendantFocusability"blocksDescendants"&#xff0c;问题解决。

php curl拉取远程图片

<?php $url "图片绝对地址/thumbnail.jpg"; $filename curl.jpg; getImg($url, $filename); /**通过curl方式获取制定的图片到本地* 完整的图片地址* 要存储的文件名*/ function getImg($url "", $filename "") {if(is_dir(basename($fi…

Linux 下 NFS服务的搭建

NFS是Network File System的简写&#xff0c;即网络文件系统。 网络文件系统是许多操作系统都支持的文件系统中的一种&#xff0c;也被成为NFS。NFS允许一个系统在网络上与他人共享目录和文件。通过使用NFS&#xff0c;用户可以像访问本地文件一样访问远端系统上的文件。 NFS所…

GCC 参数详解

From: http://www.cppblog.com/SEMAN/archive/2005/11/30/1440.html 先附上三篇相关文章&#xff1a; LINUX下的C编译器GCC简介 linux上的C/C编译器gcc/egcs详解 gcc和g的区别(很详细的描述) [介绍] gcc and g分别是gnu的c & c编译器 gcc/g在执行编译工作的时候&#…

电子客票状态整理

1.OPEN FOR USE&#xff1a;客票有效 2.VOID&#xff1a;已作废 3.REFUND&#xff1a;已退票 4.CHECK IN&#xff1a;正在办理登机 5.USED/FLOWN&#xff1a;客票已使用 6.SUSPENDED&#xff1a;系统处理&#xff0c;或人为挂起禁止使用该票 7.PRINT/EXCH&#xff1a;电子客票已…

Linux 系统应用编程——线程基础

传送门&#xff1a;Linux多线程编程实例解析 . linux多线程编程——同步与互斥 . 传统多任务操作系统中一个可以独立调度的任务&#xff08;或称之为顺序执行流&#xff09;是一个进程。每个程序加载到内存后只可以唯一地对应创建一个顺序执行流&#xff0c;即传统意义的进程。…

利用indent格式化源文件的脚本

脚本一&#xff1a;格式化指定目录下的源文件(*.h, *.cpp...) #!/bin/sh# 格式化某目录下所有*.h, *.c, *.cpp, *.hh文件, 并将文件换行符转换成Linux下的格式if [ $# -lt 1 ]; thenecho "Usage: $0 <dir>"exit 1elsedir$1fi# format a source file(*.c, *.h,…

apple mach-o linker (id) error

在网上查了下&#xff0c;有网友说是因为有变量重名&#xff0c;仔细检查了下代码&#xff0c;UIGraphicsEndImageContext()写成uigraphicsendimagecontext()了&#xff0c;改过来就编译通过了。转载于:https://www.cnblogs.com/cc-Cheng/p/3341242.html

Struts入门(三)深入Struts用法讲解

访问Servlet APIAction搜索顺序动态方法调用指定多个配置文件默认ActionStruts 后缀接收参数处理结果类型1.访问Servlet API 首先我们了解什么是Servlet API httpRequest、httpResponse、servletContext  3个api对应jsp面向对象&#xff1a;request、response、application …

linux errno定义

以下内容来自于Ubuntu系统&#xff0c;请看执行情况&#xff1a; [zcmasm-generic #6]$pwd /usr/include/asm-generic [zcmasm-generic #7]$ls errno* -lh -rw-r--r-- 1 root root 1.6K Jun 19 2013 errno-base.h -rw-r--r-- 1 root root 5.2K Jun 19 2013 errno.h [zcmasm-g…

linux多线程编程——同步与互斥

一、 为什么要用多线程技术&#xff1f; 1、避免阻塞&#xff0c;大家知道&#xff0c;单个进程只有一个主线程&#xff0c;当主线程阻塞的时候&#xff0c;整个进程也就阻塞了&#xff0c;无法再去做其它的一些功能了。 2、避免CPU空转&#xff0c;应用程序经常会涉及到RPC&am…

黑马程序员_泛型

--------------------ASP.NetAndroidIOS开发、.Net培训、期待与您交流&#xff01; -------------------- 1. 泛型 1.概述 泛型是为了解决了集合中存储对象安全问题&#xff0c;如果集合中存数了不同类型的对象&#xff0c;那么读取出来后&#xff0c;操作取出的对象以为不…

菜鸟成长记(十一)----- 操蛋的2016与未知的2017

现在已经2017.1.16号了&#xff0c;早就说着要写篇总结&#xff0c;骂骂特么操蛋的自己&#xff0c;当然这两三年来在这里骂的真特么不在少数了&#xff0c;但是都是特么一拖再拖&#xff0c;刚刚明明是在看TPO阅读的&#xff0c;但是特么实在是无法集中精神的看&#xff0c;作…

VS.NET版本与VC版本对应关系

vc6 -> vc6vs2003 -> vc7vs2005 -> vc8vs2008 -> vc9vs2010 -> vc10vs2012 -> vc11vs2013 -> vc12仅供参考&#xff01;

sql2008 获取表结构说明

SELECT 表名 case when a.colorder1 then d.name else end, 表说明 case when a.colorder1 then isnull(f.value,) else end, 字段序号 a.colorder, 字段名 a.name, 标识 case when COLUMNPROPERTY( a.id,a.name,IsIdentity)1 th…

Linux ALSA声卡驱动之四:Control设备的创建

声明&#xff1a;本博内容均由http://blog.csdn.net/droidphone原创&#xff0c;转载请注明出处&#xff0c;谢谢&#xff01; Control接口 Control接口主要让用户空间的应用程序&#xff08;alsa-lib&#xff09;可以访问和控制音频codec芯片中的多路开关&#xff0c;滑动控件…

【linux】信号量的值定义

参见文件&#xff1a;/usr/include/bits/signum.h /* Signal number definitions. Linux version.Copyright (C) 1995-2013 Free Software Foundation, Inc.This file is part of the GNU C Library.The GNU C Library is free software; you can redistribute it and/ormodi…

汇编学习笔记-序章

最近突然对汇编语言开始感兴趣&#xff0c;于是说干就干了。 之前也自学过一点汇编&#xff0c;是跟着王爽老师的《汇编语言(第3版) 》这本书学习的&#xff0c;已经是有5 6前年的样子了。当时觉得这本书写的非常通俗易懂是一本非常好的启蒙书籍&#xff0c;但是最近在翻阅的时…

jQuery 入门教程(5): 显示/隐藏内容

2019独角兽企业重金招聘Python工程师标准>>> jQuery的hide()和show()可以用来显示和隐藏内容。比如下面的例子&#xff1a;jQuery的hide()和show() 可以用来显示和隐藏内容。比如下面的例子&#xff1a; [html] view plain copy print ? <!doctype html> …

键盘键值表

键盘键值表 值 描述 0x1 鼠标左键 0x2 鼠标右键 0x3 CANCEL 键 0x4 鼠标中键 0x8 BACKSPACE 键 0x9 TAB 键 0xC CLEAR 键 0xD ENTER 键 0x10 SHIFT 键 0x11 CTRL 键 0x12 MENU 键 0x13 PAUSE 键 0x14 CAPS LOCK 键 0x1B ESC 键 0x20 SPACEBAR 键 0x21 PAGE UP 键 0x22 PAGE DOW…