Busybox Style Guide
===================
This document describes the coding style conventions used in Busybox. If you
add a new file to Busybox or are editing an existing file, please format your
code according to this style. If you are the maintainer of a file that does
not follow these guidelines, please -- at your own convenience -- modify the
file(s) you maintain to bring them into conformance with this style guide.
Please note that this is a low priority task.
To help you format the whitespace of your programs, an ".indent.pro" file is
included in the main Busybox source directory that contains option flags to
format code as per this style guide. This way you can run GNU indent on your
files by typing 'indent myfile.c myfile.h' and it will magically apply all the
right formatting rules to your file. Please _do_not_ run this on all the files
in the directory, just your own.
Declaration Order
-----------------
Here is the order in which code should be laid out in a file:
- commented program name and one-line description
- commented author name and email address(es)
- commented GPL boilerplate
- commented longer description / notes for the program (if needed)
- #includes of .h files with angle brackets (<>) around them
- #includes of .h files with quotes ("") around them
- #defines (if any, note the section below titled "Avoid the Preprocessor")
- const and global variables
- function declarations (if necessary)
- function implementations
Whitespace and Formatting
-------------------------
This is everybody's favorite flame topic so let's get it out of the way right
up front.
Tabs vs. Spaces in Line Indentation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The preference in Busybox is to indent lines with tabs. Do not indent lines
with spaces and do not indents lines using a mixture of tabs and spaces. (The
indentation style in the Apache and Postfix source does this sort of thing:
\s\s\s\sif (expr) {\n\tstmt; --ick.) The only exception to this rule is
multi-line comments that use an asterisk at the beginning of each line, i.e.:
/t/*
/t * This is a block comment.
/t * Note that it has multiple lines
/t * and that the beginning of each line has a tab plus a space
/t * except for the opening '/*' line where the slash
/t * is used instead of a space.
/t */
Furthermore, The preference is that tabs be set to display at four spaces
wide, but the beauty of using only tabs (and not spaces) at the beginning of
lines is that you can set your editor to display tabs at *whatever* number of
spaces is desired and the code will still look fine.
Operator Spacing
~~~~~~~~~~~~~~~~
Put spaces between terms and operators. Example:
Don't do this:
for(i=0;i<num_items;i++){
Do this instead:
for (i = 0; i < num_items; i++) {
While it extends the line a bit longer, the spaced version is more
readable. An allowable exception to this rule is the situation where
excluding the spacing makes it more obvious that we are dealing with a
single term (even if it is a compound term) such as:
if (str[idx] == '/' && str[idx-1] != '\\')
or
if ((argc-1) - (optind+1) > 0)
Bracket Spacing
~~~~~~~~~~~~~~~
If an opening bracket starts a function, it should be on the
next line with no spacing before it. However, if a bracket follows an opening
control block, it should be on the same line with a single space (not a tab)
between it and the opening control block statement. Examples:
Don't do this:
while (!done)
{
do
{
Don't do this either:
while (!done){
do{
And for heaven's sake, don't do this:
while (!done)
{
do
{
Do this instead:
while (!done) {
do {
Exceptions:
- if you have long logic statements that need to be wrapped, then uncuddling
the bracket to improve readability is allowed:
if (some_really_long_checks && some_other_really_long_checks \
&& some_more_really_long_checks)
{
do_foo_now;
Spacing around Parentheses
~~~~~~~~~~~~~~~~~~~~~~~~~~
Put a space between C keywords and left parens, but not between function names
and the left paren that starts it's parameter list (whether it is being
declared or called). Examples:
Don't do this:
while(foo) {
for(i = 0; i < n; i++) {
Do this instead:
while (foo) {
for (i = 0; i < n; i++) {
But do functions like this:
static int my_func(int foo, char bar)
...
baz = my_func(1, 2);
Also, don't put a space between the left paren and the first term, nor between
the last arg and the right paren.
Don't do this:
if ( x < 1 )
strcmp( thisstr, thatstr )
Do this instead:
if (x < 1)
strcmp(thisstr, thatstr)
Cuddled Elses
~~~~~~~~~~~~~
Also, please "cuddle" your else statements by putting the else keyword on the
same line after the right bracket that closes an 'if' statement.
Don't do this:
if (foo) {
stmt;
}
else {
stmt;
}
Do this instead:
if (foo) {
stmt;
} else {
stmt;
}
The exception to this rule is if you want to include a comment before the else
block. Example:
if (foo) {
stmts...
}
/* otherwise, we're just kidding ourselves, so re-frob the input */
else {
other_stmts...
}
Variable and Function Names
---------------------------
Use the K&R style with names in all lower-case and underscores occasionally
used to separate words (e.g., "variable_name" and "numchars" are both
acceptable). Using underscores makes variable and function names more readable
because it looks like whitespace; using lower-case is easy on the eyes.
Frowned upon:
hitList
TotalChars
szFileName
pf_Nfol_TriState
Preferred:
hit_list
total_chars
file_name
sensible_name
Exceptions:
- Enums, macros, and constant variables are occasionally written in all
upper-case with words optionally seperatedy by underscores (i.e. FIFOTYPE,
ISBLKDEV()).
- Nobody is going to get mad at you for using 'pvar' as the name of a
variable that is a pointer to 'var'.
Converting to K&R
~~~~~~~~~~~~~~~~~
The Busybox codebase is very much a mixture of code gathered from a variety of
sources. This explains why the current codebase contains such a hodge-podge of
different naming styles (Java, Pascal, K&R, just-plain-weird, etc.). The K&R
guideline explained above should therefore be used on new files that are added
to the repository. Furthermore, the maintainer of an existing file that uses
alternate naming conventions should, at his own convenience, convert those
names over to K&R style. Converting variable names is a very low priority
task.
If you want to do a search-and-replace of a single variable name in different
files, you can do the following in the busybox directory:
$ perl -pi -e 's/\bOldVar\b/new_var/g' *.[ch]
If you want to convert all the non-K&R vars in your file all at once, follow
these steps:
- In the busybox directory type 'examples/mk2knr.pl files-to-convert'. This
does not do the actual conversion, rather, it generates a script called
'convertme.pl' that shows what will be converted, giving you a chance to
review the changes beforehand.
- Review the 'convertme.pl' script that gets generated in the busybox
directory and remove / edit any of the substitutions in there. Please
especially check for false positives (strings that should not be
converted).
- Type './convertme.pl same-files-as-before' to perform the actual
conversion.
- Compile and see if everything still works.
Please be aware of changes that have cascading effects into other files. For
example, if you're changing the name of something in, say utility.c, you
should probably run 'examples/mk2knr.pl utility.c' at first, but when you run
the 'convertme.pl' script you should run it on _all_ files like so:
'./convertme.pl *.[ch]'.
Avoid The Preprocessor
----------------------
At best, the preprocessor is a necessary evil, helping us account for platform
and architecture differences. Using the preprocessor unnecessarily is just
plain evil.
The Folly of #define
~~~~~~~~~~~~~~~~~~~~
没有合适的资源?快使用搜索试试~ 我知道了~
busybox-1.1.3.tar
2星 需积分: 5 12 下载量 197 浏览量
2009-12-08
14:56:32
上传
评论
收藏 1.66MB GZ 举报
温馨提示
共912个文件
c:501个
h:73个
in:42个
busybox-1.1.3.tar busybox-1.1.3.tar busybox-1.1.3.tar
资源推荐
资源详情
资源评论
收起资源包目录
busybox-1.1.3.tar (912个子文件)
AUTHORS 5KB
AUTHORS 286B
basename-does-not-remove-identical-extension 40B
basename-works 56B
sample.bound 619B
bunzip2-reads-from-standard-input 68B
bunzip2-removes-compressed-file 68B
bzcat-does-not-remove-compressed-file 64B
e2fsck.c 412KB
ash.c 281KB
fdisk.c 155KB
vi.c 114KB
insmod.c 103KB
msh.c 102KB
hush.c 80KB
hdparm.c 78KB
gzip.c 77KB
devfsd.c 59KB
httpd.c 58KB
awk.c 57KB
dpkg.c 56KB
inetd.c 46KB
interface.c 45KB
cmdedit.c 44KB
bb_mkdep.c 43KB
lash.c 41KB
traceroute.c 39KB
stty.c 37KB
fsck_minix.c 35KB
mke2fs.c 35KB
ifupdown.c 33KB
sed.c 32KB
ls.c 30KB
fsck.c 28KB
init.c 28KB
decompress_unzip.c 28KB
nfsmount.c 26KB
getty.c 26KB
tar.c 26KB
mconf.c 25KB
less.c 25KB
expr.c 25KB
pwd_grp.c 25KB
decompress_bunzip2.c 22KB
modprobe.c 22KB
wget.c 21KB
crond.c 21KB
mkfs_minix.c 21KB
inode.c 20KB
probe.c 20KB
iproute.c 20KB
tune2fs.c 20KB
ipaddress.c 19KB
dump.c 19KB
ipcs.c 18KB
route.c 18KB
syslogd.c 17KB
netstat.c 17KB
unix_io.c 17KB
ifconfig.c 17KB
telnetd.c 16KB
symbol.c 16KB
dhcpc.c 16KB
time.c 15KB
stat.c 15KB
getopt_ulflags.c 15KB
top.c 15KB
textbox.c 15KB
ping6.c 14KB
md5.c 14KB
xargs.c 14KB
mount.c 14KB
telnet.c 13KB
tftp.c 13KB
iptunnel.c 13KB
zcip.c 12KB
applets.c 12KB
rpm.c 12KB
grep.c 12KB
menubox.c 12KB
fbset.c 12KB
initialize.c 11KB
icount.c 11KB
test.c 11KB
unzip.c 11KB
dnsd.c 11KB
arping.c 11KB
block.c 11KB
login.c 11KB
mkswap.c 11KB
conf.c 11KB
test_io.c 10KB
confdata.c 10KB
getopt.c 10KB
expr.c 10KB
ping.c 10KB
files.c 10KB
read.c 10KB
cal.c 10KB
tag.c 10KB
共 912 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
- yyh7011cn2012-06-06下载后交叉编译可用,但不是我想要的,还是谢谢。
- junjun742012-11-29载后交叉编译可用,但不是我想要的,还是谢谢
tanandwang
- 粉丝: 1
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功