Once more, with fewer inappropriate semicolons.
[oweals/busybox.git] / docs / style-guide.txt
index ee7547f280f392fecfa15207e6695a0ca8866b47..d7d8e5ec7801e9defda951e04ee06a8175db2497 100644 (file)
@@ -26,7 +26,9 @@ Here is the order in which code should be laid out in a file:
  - commented author name and email address(es)
  - commented GPL boilerplate
  - commented longer description / notes for the program (if needed)
  - commented author name and email address(es)
  - commented GPL boilerplate
  - commented longer description / notes for the program (if needed)
- - #includes and #defines
+ - #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
  - const and global variables
  - function declarations (if necessary)
  - function implementations
@@ -128,9 +130,9 @@ between it and the opening control block statement. Examples:
 Spacing around Parentheses
 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 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:
+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:
 
 
        Don't do this:
 
@@ -198,7 +200,6 @@ block. Example:
 
 
 
 
 
 
-
 Variable and Function Names
 ---------------------------
 
 Variable and Function Names
 ---------------------------
 
@@ -223,28 +224,55 @@ because it looks like whitespace; using lower-case is easy on the eyes.
 
 Exceptions:
 
 
 Exceptions:
 
- - Enums, macros, and constant variables should all be in upper-case with
-   words optionally seperatedy by underscores (i.e. FIFOTYPE, ISBLKDEV()).
+ - 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'.
 
 
  - Nobody is going to get mad at you for using 'pvar' as the name of a
    variable that is a pointer to 'var'.
 
-Note: 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. Perhaps in the future we will include some magical Perl
-script that can go through and convert variable names, left as an exercise for
-the reader for now.
 
 
-For the time being, if you want to do a search-and-replace of a variable name
-in different files, do the following in the busybox directory:
+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]
 
 
        $ 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
 
 
 Avoid The Preprocessor
@@ -265,13 +293,13 @@ Use 'const <type> var' for declaring constants.
                #define var 80
 
        Do this instead, when the variable is in a header file and will be used in
                #define var 80
 
        Do this instead, when the variable is in a header file and will be used in
-       several source files: 
+       several source files:
 
 
-               const int var = 80; 
+               const int var = 80;
 
        Or do this when the variable is used only in a single source file:
 
 
        Or do this when the variable is used only in a single source file:
 
-               static const int var = 80; 
+               static const int var = 80;
 
 Declaring variables as '[static] const' gives variables an actual type and
 makes the compiler do type checking for you; the preprocessor does _no_ type
 
 Declaring variables as '[static] const' gives variables an actual type and
 makes the compiler do type checking for you; the preprocessor does _no_ type
@@ -297,24 +325,25 @@ Use 'static inline' instead of a macro.
                }
 
 Static inline functions are greatly preferred over macros. They provide type
                }
 
 Static inline functions are greatly preferred over macros. They provide type
-safety, have no length limitations, no formatting limitations, and under gcc
-they are as cheap as macros. Besides, really long macros with backslashes at
-the end of each line are ugly as sin.
+safety, have no length limitations, no formatting limitations, have an actual
+return value, and under gcc they are as cheap as macros. Besides, really long
+macros with backslashes at the end of each line are ugly as sin.
 
 
 The Folly of #ifdef
 ~~~~~~~~~~~~~~~~~~~
 
 Code cluttered with ifdefs is difficult to read and maintain. Don't do it.
 
 
 The Folly of #ifdef
 ~~~~~~~~~~~~~~~~~~~
 
 Code cluttered with ifdefs is difficult to read and maintain. Don't do it.
-Instead, put your ifdefs in a header, and conditionally define 'static inline'
-functions, (or *maybe* macros), which are used in the code.  
+Instead, put your ifdefs at the top of your .c file (or in a header), and
+conditionally define 'static inline' functions, (or *maybe* macros), which are
+used in the code.
 
        Don't do this:
 
                ret = my_func(bar, baz);
                if (!ret)
                        return -1;
 
        Don't do this:
 
                ret = my_func(bar, baz);
                if (!ret)
                        return -1;
-               #ifdef BB_FEATURE_FUNKY
+               #ifdef CONFIG_FEATURE_FUNKY
                        maybe_do_funky_stuff(bar, baz);
                #endif
 
                        maybe_do_funky_stuff(bar, baz);
                #endif
 
@@ -322,7 +351,12 @@ functions, (or *maybe* macros), which are used in the code.
 
        (in .h header file)
 
 
        (in .h header file)
 
-               #ifndef BB_FEATURE_FUNKY
+               #ifdef CONFIG_FEATURE_FUNKY
+               static inline void maybe_do_funky_stuff (int bar, int baz)
+               {
+                       /* lotsa code in here */
+               }
+               #else
                static inline void maybe_do_funky_stuff (int bar, int baz) {}
                #endif
 
                static inline void maybe_do_funky_stuff (int bar, int baz) {}
                #endif
 
@@ -334,7 +368,7 @@ functions, (or *maybe* macros), which are used in the code.
                maybe_do_funky_stuff(bar, baz);
 
 The great thing about this approach is that the compiler will optimize away
                maybe_do_funky_stuff(bar, baz);
 
 The great thing about this approach is that the compiler will optimize away
-the "no-op" case when the feature is turned off.
+the "no-op" case (the empty function) when the feature is turned off.
 
 Note also the use of the word 'maybe' in the function name to indicate
 conditional execution.
 
 Note also the use of the word 'maybe' in the function name to indicate
 conditional execution.
@@ -411,7 +445,7 @@ First, some background to put this discussion in context: Static buffers look
 like this in code:
 
        /* in a .c file outside any functions */
 like this in code:
 
        /* in a .c file outside any functions */
-       static char *buffer[BUFSIZ]; /* happily used by any function in this file,
+       static char buffer[BUFSIZ]; /* happily used by any function in this file,
                                        but ick! big! */
 
 The problem with these is that any time any busybox app is run, you pay a
                                        but ick! big! */
 
 The problem with these is that any time any busybox app is run, you pay a
@@ -448,11 +482,14 @@ assigning them to a global pointer thusly:
 
 This last approach has some advantages (low code size, space not used until
 it's needed), but can be a problem in some low resource machines that have
 
 This last approach has some advantages (low code size, space not used until
 it's needed), but can be a problem in some low resource machines that have
-very limited stack space (e.g., uCLinux).  busybox.h declares a macro that
-implements compile-time selection between xmalloc() and stack creation, so
-you can code the line in question as
-               RESERVE_BB_BUFFER(buffer, BUFSIZ);
-and the right thing will happen, based on the customer's configuration.
+very limited stack space (e.g., uCLinux).
+
+A macro is declared in busybox.h that implements compile-time selection
+between xmalloc() and stack creation, so you can code the line in question as
+
+               RESERVE_CONFIG_BUFFER(buffer, BUFSIZ);
+
+and the right thing will happen, based on your configuration.
 
 
 
 
 
 
@@ -470,7 +507,8 @@ When in doubt about the proper behavior of a Busybox program (output,
 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 Busybox
 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 Busybox
-programs after their GNU counterparts and nobody has to get hurt.
+programs after their GNU counterparts and it will make life easier on (nearly)
+everyone.
 
 The only time we deviate from emulating the GNU behavior is when:
 
 
 The only time we deviate from emulating the GNU behavior is when:
 
@@ -575,15 +613,13 @@ one comment) before the block, rather than commenting each and every line.
 There is an optimal ammount of commenting that a program can have; you can
 comment too much as well as too little.
 
 There is an optimal ammount of commenting that a program can have; you can
 comment too much as well as too little.
 
-A picture is really worth a thousand words here, so here is an example that
-illustrates emphasizing logical blocks:
+A picture is really worth a thousand words here, the following example
+illustrates how to emphasize logical blocks:
 
        while (line = get_line_from_file(fp)) {
 
                /* eat the newline, if any */
 
        while (line = get_line_from_file(fp)) {
 
                /* eat the newline, if any */
-               if (line[strlen(line)-1] == '\n') {
-                       line[strlen(line)-1] = '\0';
-               }
+               chomp(line);
 
                /* ignore blank lines */
                if (strlen(file_to_act_on) == 0) {
 
                /* ignore blank lines */
                if (strlen(file_to_act_on) == 0) {
@@ -601,21 +637,44 @@ illustrates emphasizing logical blocks:
        }
 
 
        }
 
 
-Testing Guidelines
-~~~~~~~~~~~~~~~~~~
+Processing Options with getopt
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+If your applet needs to process  command-line switches, please use getopt() to
+do so. Numerous examples can be seen in many of the existing applets, but
+basically it boils down to two things: at the top of the .c file, have this
+line in the midst of your #includes:
+
+       #include <getopt.h>
+
+And a code block similar to the following near the top of your applet_main()
+routine:
 
 
-It's considered good form to test your new feature before you submit a patch
-to the mailing list, and especially before you commit a change to CVS. Here
-are some guildlines on testing your changes.
+    while ((opt = getopt(argc, argv, "abc")) > 0) {
+            switch (opt) {
+            case 'a':
+                do_a_opt = 1;
+                break;
+            case 'b':
+                do_b_opt = 1;
+                break;
+            case 'c':
+                do_c_opt = 1;
+                break;
+            default:
+                show_usage();    /* in utility.c */
+            }
+    }
 
 
- - Always test busybox grep against GNU grep and make sure the behavior /
-   output is identical between the two.
+If your applet takes no options (such as 'init'), there should be a line
+somewhere in the file reads:
 
 
- - Try several different permutations and combinations of the features you're
-   adding and make sure they all work. (Make sure one feature does not
-   interfere with another, etc.)
+       /* no options, no getopt */
 
 
- - Make sure you test compiling against the source both with the feature
-   turned on and turned off in Config.h and make sure busybox compiles cleanly
-   both ways.
+That way, when people go grepping to see which applets need to be converted to
+use getopt, they won't get false positives.
 
 
+Additional Note: Do not use the getopt_long library function and do not try to
+hand-roll your own long option parsing. Busybox applets should only support
+short options. Explanations and examples of the short options should be
+documented in usage.h.