From 4aca87515a5083ae0e31ce3177189fd43b6d05ac Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 3 Jan 2015 13:58:15 +0100 Subject: patch to Vanilla Tomato 1.28 --- release/src/router/busybox/docs/style-guide.txt | 166 ++++++++++++++---------- 1 file changed, 100 insertions(+), 66 deletions(-) (limited to 'release/src/router/busybox/docs/style-guide.txt') diff --git a/release/src/router/busybox/docs/style-guide.txt b/release/src/router/busybox/docs/style-guide.txt index c71f1e60..7560d698 100644 --- a/release/src/router/busybox/docs/style-guide.txt +++ b/release/src/router/busybox/docs/style-guide.txt @@ -20,7 +20,7 @@ in the directory, just your own. Declaration Order ----------------- -Here is the order in which code should be laid out in a file: +Here is the preferred 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) @@ -51,13 +51,13 @@ 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 */ + \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 @@ -126,6 +126,16 @@ between it and the opening control block statement. Examples: do { +If you have long logic statements that need to be wrapped, then uncuddling +the bracket to improve readability is allowed. Generally, this style makes +it easier for reader to notice that 2nd and following lines are still +inside 'if': + + if (some_really_long_checks && some_other_really_long_checks + && some_more_really_long_checks + && even_more_of_long_checks + ) { + do_foo_now; Spacing around Parentheses ~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -199,6 +209,23 @@ block. Example: } +Labels +~~~~~~ + +Labels should start at the beginning of the line, not indented to the block +level (because they do not "belong" to block scope, only to whole function). + + if (foo) { + stmt; + label: + stmt2; + stmt; + } + +(Putting label at position 1 prevents diff -p from confusing label for function +name, but it's not a policy of busybox project to enforce such a minor detail). + + Variable and Function Names --------------------------- @@ -225,7 +252,7 @@ because it looks like whitespace; using lower-case is easy on the eyes. Exceptions: - Enums, macros, and constant variables are occasionally written in all - upper-case with words optionally seperatedy by underscores (i.e. FIFOTYPE, + upper-case with words optionally seperatedy by underscores (i.e. FIFO_TYPE, ISBLKDEV()). - Nobody is going to get mad at you for using 'pvar' as the name of a @@ -252,7 +279,7 @@ files, you can do the following in the busybox directory: 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 'scripts/mk2knr.pl files-to-convert'. This + - 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. @@ -266,10 +293,10 @@ these steps: 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 'scripts/mk2knr.pl utility.c' at first, but when you run +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]'. @@ -290,22 +317,21 @@ Use 'const var' for declaring constants. Don't do this: - #define var 80 + #define CONST 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; + enum { CONST = 80 }; - Or do this when the variable is used only in a single source file: - - 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 -checking whatsoever, making it much more error prone. Declaring variables with -'[static] const' also makes debugging programs much easier since the value of -the variable can be easily queried and displayed. +Although enum may look ugly to some people, it is better for code size. +With "const int" compiler may fail to optimize it out and will reserve +a real storage in rodata for it! (Hopefully, newer gcc will get better +at it...). With "define", you have slight risk of polluting namespace +(#define doesn't allow you to redefine the name in the inner scopes), +and complex "define" are evaluated each time they uesd, not once +at declarations like enums. Also, the preprocessor does _no_ type checking +whatsoever, making it much more error prone. The Folly of Macros @@ -336,14 +362,14 @@ The Folly of #ifdef Code cluttered with ifdefs is difficult to read and maintain. Don't do it. 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. +used in the code. 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 @@ -351,13 +377,13 @@ used in the code. (in .h header file) - #ifdef BB_FEATURE_FUNKY - static inline void maybe_do_funky_stuff (int bar, int baz) + #if ENABLE_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) {} + static inline void maybe_do_funky_stuff(int bar, int baz) {} #endif (in the .c source file) @@ -423,15 +449,16 @@ Unfortunately, the way C handles strings makes them prone to overruns when certain library functions are (mis)used. The following table offers a summary of some of the more notorious troublemakers: -function overflows preferred ----------------------------------------- -strcpy dest string strncpy -strcat dest string strncat -gets string it gets fgets -getwd buf string getcwd -[v]sprintf str buffer [v]snprintf -realpath path buffer use with pathconf -[vf]scanf its arguments just avoid it +function overflows preferred +------------------------------------------------- +strcpy dest string safe_strncpy +strncpy may fail to 0-terminate dst safe_strncpy +strcat dest string strncat +gets string it gets fgets +getwd buf string getcwd +[v]sprintf str buffer [v]snprintf +realpath path buffer use with pathconf +[vf]scanf its arguments just avoid it The above is by no means a complete list. Be careful out there. @@ -441,11 +468,11 @@ The above is by no means a complete list. Be careful out there. Avoid Big Static Buffers ------------------------ -First, some background to put this discussion in context: Static buffers look +First, some background to put this discussion in context: static buffers look 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 @@ -487,10 +514,13 @@ 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_BB_BUFFER(buffer, BUFSIZ); + RESERVE_CONFIG_BUFFER(buffer, BUFSIZ); and the right thing will happen, based on your configuration. +Another relatively new trick of similar nature is explained +in keep_data_small.txt. + Miscellaneous Coding Guidelines @@ -518,7 +548,7 @@ The only time we deviate from emulating the GNU behavior is when: would be required, lots more memory would be used, etc.) - The difference is minor or cosmetic -A note on the 'cosmetic' case: Output differences might be considered +A note on the 'cosmetic' case: output differences might be considered cosmetic, but if the output is significant enough to break other scripts that use the output, it should really be fixed. @@ -568,7 +598,7 @@ like this: if (foo) stmt1; new_line(); - stmt2 + stmt2; stmt3; And the resulting behavior of your program would totally bewilder you. (Don't @@ -610,13 +640,13 @@ begin with a C keyword, but not always. Furthermore, you should put a single comment (not necessarily one line, just 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 +There is an optimal amount 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, the following example illustrates how to emphasize logical blocks: - while (line = get_line_from_file(fp)) { + while (line = xmalloc_fgets(fp)) { /* eat the newline, if any */ chomp(line); @@ -640,31 +670,38 @@ illustrates how to emphasize logical blocks: Processing Options with getopt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -If your applet needs to process command-line switches, please use getopt() to +If your applet needs to process command-line switches, please use getopt32() 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: +line in the midst of your #includes, if you need to parse long options: #include +Then have long options defined: + + static const struct option _long_options[] = { + { "list", 0, NULL, 't' }, + { "extract", 0, NULL, 'x' }, + { NULL, 0, NULL, 0 } + }; + And a code block similar to the following near the top of your applet_main() routine: - 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 */ - } - } + char *str_b; + + opt_complementary = "cryptic_string"; + applet_long_options = _long_options; /* if you have them */ + opt = getopt32(argc, argv, "ab:c", &str_b); + if (opt & 1) { + handle_option_a(); + } + if (opt & 2) { + handle_option_b(str_b); + } + if (opt & 4) { + handle_option_c(); + } If your applet takes no options (such as 'init'), there should be a line somewhere in the file reads: @@ -674,7 +711,4 @@ somewhere in the file reads: 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. +For more info and examples, examine getopt32.c, tar.c, wget.c etc. -- cgit v1.2.3-54-g00ecf