2000-07-25 04:06:06 +05:30
|
|
|
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.
|
|
|
|
|
2000-08-22 05:50:21 +05:30
|
|
|
|
2000-07-25 04:06:06 +05:30
|
|
|
Declaration Order
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Here is the order in which code should be laid out in a file:
|
|
|
|
|
2000-11-18 02:58:39 +05:30
|
|
|
- commented program name and one-line description
|
2000-07-25 04:06:06 +05:30
|
|
|
- commented author name and email address(es)
|
|
|
|
- commented GPL boilerplate
|
2000-11-18 02:58:39 +05:30
|
|
|
- commented longer description / notes for the program (if needed)
|
2000-07-25 04:06:06 +05:30
|
|
|
- #includes and #defines
|
2000-11-18 02:58:39 +05:30
|
|
|
- const and global variables
|
2000-07-25 04:06:06 +05:30
|
|
|
- function declarations (if necessary)
|
|
|
|
- function implementations
|
|
|
|
|
2000-08-22 05:50:21 +05:30
|
|
|
|
2000-07-25 04:06:06 +05:30
|
|
|
Whitespace
|
|
|
|
----------
|
|
|
|
|
2000-08-22 05:50:21 +05:30
|
|
|
This is everybody's favorite flame topic so let's get it out of the way right
|
|
|
|
up front.
|
|
|
|
|
|
|
|
|
2000-11-18 02:58:39 +05:30
|
|
|
Tabs vs. Spaces in Line Indentation
|
2000-08-22 05:50:21 +05:30
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
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.:
|
2000-07-25 04:06:06 +05:30
|
|
|
|
|
|
|
/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
|
2000-11-18 02:58:39 +05:30
|
|
|
lines is that you can set your editor to display tabs at *whatever* number of
|
2000-07-25 04:06:06 +05:30
|
|
|
spaces is desired and the code will still look fine.
|
|
|
|
|
|
|
|
|
2000-08-22 05:50:21 +05:30
|
|
|
Operator Spacing
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Put spaces between terms and operators. Example:
|
2000-07-25 04:06:06 +05:30
|
|
|
|
|
|
|
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
|
2000-11-18 02:58:39 +05:30
|
|
|
single term (even if it is a compound term) such as:
|
2000-07-25 04:06:06 +05:30
|
|
|
|
|
|
|
if (str[idx] == '/' && str[idx-1] != '\\')
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
if ((argc-1) - (optind+1) > 0)
|
|
|
|
|
|
|
|
|
2000-08-22 05:50:21 +05:30
|
|
|
Bracket Spacing
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
If an opening bracket starts a function, it should be on the
|
2000-11-18 02:58:39 +05:30
|
|
|
next line with no spacing before it. However, if a bracket follows an opening
|
2000-07-25 04:06:06 +05:30
|
|
|
control block, it should be on the same line with a single space (not a tab)
|
2000-11-18 02:58:39 +05:30
|
|
|
between it and the opening control block statement. Examples:
|
2000-07-25 04:06:06 +05:30
|
|
|
|
|
|
|
Don't do this:
|
|
|
|
|
2000-11-18 02:58:39 +05:30
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
|
|
|
|
Don't do this either:
|
|
|
|
|
2000-07-25 04:06:06 +05:30
|
|
|
while (!done){
|
|
|
|
do{
|
|
|
|
|
|
|
|
Do this instead:
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
do {
|
|
|
|
|
|
|
|
|
2000-08-22 05:50:21 +05:30
|
|
|
Paren Spacing
|
|
|
|
~~~~~~~~~~~~~
|
2000-07-25 04:06:06 +05:30
|
|
|
|
2000-08-22 05:50:21 +05:30
|
|
|
Put a space between C keywords and left parens, but not between
|
2000-07-25 04:06:06 +05:30
|
|
|
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++) {
|
|
|
|
|
2000-11-18 02:58:39 +05:30
|
|
|
But do functions like this:
|
2000-07-25 04:06:06 +05:30
|
|
|
|
|
|
|
static int my_func(int foo, char bar)
|
|
|
|
...
|
|
|
|
baz = my_func(1, 2);
|
|
|
|
|
2000-08-22 05:50:21 +05:30
|
|
|
|
|
|
|
Cuddled Elses
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
2000-11-18 02:58:39 +05:30
|
|
|
Also, please "cuddle" your else statements by putting the else keyword on the
|
|
|
|
same line after the right bracket that closes an 'if' statement.
|
2000-08-22 05:50:21 +05:30
|
|
|
|
|
|
|
Don't do this:
|
|
|
|
|
|
|
|
if (foo) {
|
|
|
|
stmt;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
Do this instead:
|
|
|
|
|
|
|
|
if (foo) {
|
|
|
|
stmt;
|
|
|
|
} else {
|
|
|
|
stmt;
|
|
|
|
}
|
|
|
|
|
2000-11-18 02:58:39 +05:30
|
|
|
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...
|
|
|
|
}
|
|
|
|
|
2000-08-22 05:50:21 +05:30
|
|
|
|
2000-07-25 04:06:06 +05:30
|
|
|
Variable and Function Names
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
Use the K&R style with names in all lower-case and underscores occasionally
|
2000-11-18 02:58:39 +05:30
|
|
|
used to separate words (e.g., "variable_name" and "numchars" are both
|
2000-07-25 04:06:06 +05:30
|
|
|
acceptable). Using underscores makes variable and function names more readable
|
|
|
|
because it looks like whitespace; using lower-case is easy on the eyes.
|
|
|
|
|
|
|
|
Note: The Busybox codebase is very much a mixture of code gathered from a
|
2000-11-18 02:58:39 +05:30
|
|
|
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,
|
2000-07-25 04:06:06 +05:30
|
|
|
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
|
2000-11-18 02:58:39 +05:30
|
|
|
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. Perhaps in the future we will include some magical Perl script
|
|
|
|
that can go through and convert files -- left as an exercise to the reader for
|
|
|
|
now.
|
2000-07-25 04:06:06 +05:30
|
|
|
|
|
|
|
|
|
|
|
Tip and Pointers
|
|
|
|
----------------
|
|
|
|
|
|
|
|
The following are simple coding guidelines that should be followed:
|
|
|
|
|
2000-11-18 02:58:39 +05:30
|
|
|
- When in doubt about the proper behavior of a Busybox program (output,
|
2000-08-22 05:50:21 +05:30
|
|
|
formatting, options, etc.), model it after the equivalent GNU program.
|
|
|
|
Doesn't matter how that program behaves on some other flavor of *NIX;
|
|
|
|
doesn't matter what the POSIX standard says or doesn't say, just model
|
2000-11-18 02:58:39 +05:30
|
|
|
Busybox programs after their GNU counterparts and nobody has to get hurt.
|
2000-08-22 05:50:21 +05:30
|
|
|
|
2000-07-25 04:06:06 +05:30
|
|
|
- Don't use a '#define var 80' when you can use 'static const int var 80'
|
2000-11-18 02:58:39 +05:30
|
|
|
instead. This makes the compiler do type checking for you (rather than
|
2000-07-25 04:06:06 +05:30
|
|
|
relying on the more error-prone preprocessor) and it makes debugging
|
2000-11-18 02:58:39 +05:30
|
|
|
programs much easier since the value of the variable can be easily
|
|
|
|
displayed.
|
2000-07-25 04:06:06 +05:30
|
|
|
|
|
|
|
- If a const variable is used in only one function, do not make it global to
|
|
|
|
the file. Instead, declare it inside the function body.
|
|
|
|
|
|
|
|
- Inside applet files, all functions should be declared static so as to keep
|
2000-11-18 02:58:39 +05:30
|
|
|
the global name space clean. The only exception to this rule is the
|
2000-07-25 04:06:06 +05:30
|
|
|
"applet_main" function which must be declared extern.
|
|
|
|
|
|
|
|
- If you write a function that performs a task that could be useful outside
|
|
|
|
the immediate file, turn it into a general-purpose function with no ties to
|
|
|
|
any applet and put it in the utility.c file instead.
|
|
|
|
|
2000-11-18 02:58:39 +05:30
|
|
|
- Put all help/usage messages in usage.c. Put other strings in messages.c.
|
|
|
|
Putting these strings into their own file is a calculated decision designed
|
|
|
|
to confine spelling errors to a single place and aid internationalization
|
|
|
|
efforts, if needed. (Side Note: we might want to use a single file instead
|
|
|
|
of two, food for thought).
|
2000-07-25 04:06:06 +05:30
|
|
|
|
2000-07-26 02:00:00 +05:30
|
|
|
- There's a right way and a wrong way to test for sting equivalence with
|
|
|
|
strcmp:
|
|
|
|
|
|
|
|
The wrong way:
|
|
|
|
|
|
|
|
if (!strcmp(string, "foo")) {
|
|
|
|
...
|
|
|
|
|
|
|
|
The right way:
|
|
|
|
|
|
|
|
if (strcmp(string, "foo") == 0){
|
|
|
|
...
|
|
|
|
|
|
|
|
The use of the "equals" (==) operator in the latter example makes it much
|
|
|
|
more obvious that you are testing for equivalence. The former example with
|
2000-11-18 02:58:39 +05:30
|
|
|
the "not" (!) operator makes it look like you are testing for an error. In
|
|
|
|
a more perfect world, we would have a streq() function in the string
|
|
|
|
library, but that ain't the world we're living in.
|
2000-07-26 02:00:00 +05:30
|
|
|
|
2000-07-25 04:06:06 +05:30
|
|
|
- Do not use old-style function declarations that declare variable types
|
|
|
|
between the parameter list and opening bracket. Example:
|
|
|
|
|
|
|
|
Don't do this:
|
|
|
|
|
|
|
|
int foo(parm1, parm2)
|
|
|
|
char parm1;
|
|
|
|
float parm2;
|
|
|
|
{
|
|
|
|
....
|
|
|
|
|
|
|
|
Do this instead:
|
|
|
|
|
|
|
|
int foo(char parm1, float parm2)
|
|
|
|
{
|
|
|
|
....
|
|
|
|
|
|
|
|
- Please use brackets on all if and else statements, even if it is only one
|
|
|
|
line. Example:
|
|
|
|
|
|
|
|
Don't do this:
|
|
|
|
|
|
|
|
if (foo)
|
|
|
|
stmt;
|
|
|
|
else
|
|
|
|
stmt;
|
|
|
|
|
|
|
|
Do this instead:
|
|
|
|
|
|
|
|
if (foo) {
|
|
|
|
stmt;
|
|
|
|
} else {
|
|
|
|
stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
The "bracketless" approach is error prone because someday you might add a
|
|
|
|
line like this:
|
|
|
|
|
|
|
|
if (foo)
|
|
|
|
stmt;
|
|
|
|
new_line();
|
|
|
|
else
|
|
|
|
stmt;
|
|
|
|
|
|
|
|
And the resulting behavior of your program would totally bewilder you.
|
|
|
|
(Don't laugh, it happens to us all.) Remember folks, this is C, not
|
|
|
|
Python.
|