summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-04-15 08:39:39 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-04-15 08:39:39 +0000
commit91de7c0328d1ab3f32c8b9eb4bc6c3cd9cdf0b23 (patch)
tree36dc35ce7c130b26220b191519be6e456beada9b /docs
parent58394b1e299f7207088f077c02357749c3a3d853 (diff)
downloadbusybox-w32-91de7c0328d1ab3f32c8b9eb4bc6c3cd9cdf0b23.tar.gz
busybox-w32-91de7c0328d1ab3f32c8b9eb4bc6c3cd9cdf0b23.tar.bz2
busybox-w32-91de7c0328d1ab3f32c8b9eb4bc6c3cd9cdf0b23.zip
update style-guide.txt
Diffstat (limited to 'docs')
-rw-r--r--docs/style-guide.txt133
1 files changed, 79 insertions, 54 deletions
diff --git a/docs/style-guide.txt b/docs/style-guide.txt
index ba0cdbaa4..5bb3441cd 100644
--- a/docs/style-guide.txt
+++ b/docs/style-guide.txt
@@ -20,7 +20,7 @@ in the directory, just your own.
20Declaration Order 20Declaration Order
21----------------- 21-----------------
22 22
23Here is the order in which code should be laid out in a file: 23Here is the preferred order in which code should be laid out in a file:
24 24
25 - commented program name and one-line description 25 - commented program name and one-line description
26 - commented author name and email address(es) 26 - commented author name and email address(es)
@@ -126,14 +126,15 @@ between it and the opening control block statement. Examples:
126 126
127 do { 127 do {
128 128
129Exceptions: 129If you have long logic statements that need to be wrapped, then uncuddling
130 130the bracket to improve readability is allowed. Generally, this style makes
131 - if you have long logic statements that need to be wrapped, then uncuddling 131it easier for reader to notice that 2nd and following lines are still
132 the bracket to improve readability is allowed: 132inside 'if':
133 133
134 if (some_really_long_checks && some_other_really_long_checks \ 134 if (some_really_long_checks && some_other_really_long_checks
135 && some_more_really_long_checks) 135 && some_more_really_long_checks
136 { 136 && even_more_of_long_checks
137 ) {
137 do_foo_now; 138 do_foo_now;
138 139
139Spacing around Parentheses 140Spacing around Parentheses
@@ -208,6 +209,23 @@ block. Example:
208 } 209 }
209 210
210 211
212Labels
213~~~~~~
214
215Labels should start at the beginning of the line, not indented to the block
216level (because they do not "belong" to block scope, only to whole function).
217
218 if (foo) {
219 stmt;
220 label:
221 stmt2;
222 stmt;
223 }
224
225(Putting label at position 1 prevents diff -p from confusing label for function
226name, but it's not a policy of busybox project to enforce such a minor detail).
227
228
211 229
212Variable and Function Names 230Variable and Function Names
213--------------------------- 231---------------------------
@@ -234,7 +252,7 @@ because it looks like whitespace; using lower-case is easy on the eyes.
234Exceptions: 252Exceptions:
235 253
236 - Enums, macros, and constant variables are occasionally written in all 254 - Enums, macros, and constant variables are occasionally written in all
237 upper-case with words optionally seperatedy by underscores (i.e. FIFOTYPE, 255 upper-case with words optionally seperatedy by underscores (i.e. FIFO_TYPE,
238 ISBLKDEV()). 256 ISBLKDEV()).
239 257
240 - Nobody is going to get mad at you for using 'pvar' as the name of a 258 - Nobody is going to get mad at you for using 'pvar' as the name of a
@@ -299,22 +317,21 @@ Use 'const <type> var' for declaring constants.
299 317
300 Don't do this: 318 Don't do this:
301 319
302 #define var 80 320 #define CONST 80
303 321
304 Do this instead, when the variable is in a header file and will be used in 322 Do this instead, when the variable is in a header file and will be used in
305 several source files: 323 several source files:
306 324
307 const int var = 80; 325 enum { CONST = 80 };
308
309 Or do this when the variable is used only in a single source file:
310
311 static const int var = 80;
312 326
313Declaring variables as '[static] const' gives variables an actual type and 327Although enum may look ugly to some people, it is better for code size.
314makes the compiler do type checking for you; the preprocessor does _no_ type 328With "const int" compiler may fail to optimize it out and will reserve
315checking whatsoever, making it much more error prone. Declaring variables with 329a real storage in rodata for it! (Hopefully, newer gcc will get better
316'[static] const' also makes debugging programs much easier since the value of 330at it...). With "define", you have slight risk of polluting namespace
317the variable can be easily queried and displayed. 331(#define doesn't allow you to redefine the name in the inner scopes),
332and complex "define" are evaluated each time they uesd, not once
333at declarations like enums. Also, the preprocessor does _no_ type checking
334whatsoever, making it much more error prone.
318 335
319 336
320The Folly of Macros 337The Folly of Macros
@@ -432,15 +449,16 @@ Unfortunately, the way C handles strings makes them prone to overruns when
432certain library functions are (mis)used. The following table offers a summary 449certain library functions are (mis)used. The following table offers a summary
433of some of the more notorious troublemakers: 450of some of the more notorious troublemakers:
434 451
435function overflows preferred 452function overflows preferred
436---------------------------------------- 453-------------------------------------------------
437strcpy dest string strncpy 454strcpy dest string safe_strncpy
438strcat dest string strncat 455strncpy may fail to 0-terminate dst safe_strncpy
439gets string it gets fgets 456strcat dest string strncat
440getwd buf string getcwd 457gets string it gets fgets
441[v]sprintf str buffer [v]snprintf 458getwd buf string getcwd
442realpath path buffer use with pathconf 459[v]sprintf str buffer [v]snprintf
443[vf]scanf its arguments just avoid it 460realpath path buffer use with pathconf
461[vf]scanf its arguments just avoid it
444 462
445 463
446The above is by no means a complete list. Be careful out there. 464The above is by no means a complete list. Be careful out there.
@@ -450,7 +468,7 @@ The above is by no means a complete list. Be careful out there.
450Avoid Big Static Buffers 468Avoid Big Static Buffers
451------------------------ 469------------------------
452 470
453First, some background to put this discussion in context: Static buffers look 471First, some background to put this discussion in context: static buffers look
454like this in code: 472like this in code:
455 473
456 /* in a .c file outside any functions */ 474 /* in a .c file outside any functions */
@@ -500,6 +518,9 @@ between xmalloc() and stack creation, so you can code the line in question as
500 518
501and the right thing will happen, based on your configuration. 519and the right thing will happen, based on your configuration.
502 520
521Another relatively new trick of similar nature is explained
522in keep_data_small.txt.
523
503 524
504 525
505Miscellaneous Coding Guidelines 526Miscellaneous Coding Guidelines
@@ -527,7 +548,7 @@ The only time we deviate from emulating the GNU behavior is when:
527 would be required, lots more memory would be used, etc.) 548 would be required, lots more memory would be used, etc.)
528 - The difference is minor or cosmetic 549 - The difference is minor or cosmetic
529 550
530A note on the 'cosmetic' case: Output differences might be considered 551A note on the 'cosmetic' case: output differences might be considered
531cosmetic, but if the output is significant enough to break other scripts that 552cosmetic, but if the output is significant enough to break other scripts that
532use the output, it should really be fixed. 553use the output, it should really be fixed.
533 554
@@ -577,7 +598,7 @@ like this:
577 if (foo) 598 if (foo)
578 stmt1; 599 stmt1;
579 new_line(); 600 new_line();
580 stmt2 601 stmt2;
581 stmt3; 602 stmt3;
582 603
583And the resulting behavior of your program would totally bewilder you. (Don't 604And the resulting behavior of your program would totally bewilder you. (Don't
@@ -625,7 +646,7 @@ comment too much as well as too little.
625A picture is really worth a thousand words here, the following example 646A picture is really worth a thousand words here, the following example
626illustrates how to emphasize logical blocks: 647illustrates how to emphasize logical blocks:
627 648
628 while (line = get_line_from_file(fp)) { 649 while (line = xmalloc_fgets(fp)) {
629 650
630 /* eat the newline, if any */ 651 /* eat the newline, if any */
631 chomp(line); 652 chomp(line);
@@ -649,31 +670,38 @@ illustrates how to emphasize logical blocks:
649Processing Options with getopt 670Processing Options with getopt
650~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 671~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
651 672
652If your applet needs to process command-line switches, please use getopt() to 673If your applet needs to process command-line switches, please use getopt32() to
653do so. Numerous examples can be seen in many of the existing applets, but 674do so. Numerous examples can be seen in many of the existing applets, but
654basically it boils down to two things: at the top of the .c file, have this 675basically it boils down to two things: at the top of the .c file, have this
655line in the midst of your #includes: 676line in the midst of your #includes, if you need to parse long options:
656 677
657 #include <getopt.h> 678 #include <getopt.h>
658 679
680Then have long options defined:
681
682 static const struct option <applet>_long_options[] = {
683 { "list", 0, NULL, 't' },
684 { "extract", 0, NULL, 'x' },
685 { NULL }
686 };
687
659And a code block similar to the following near the top of your applet_main() 688And a code block similar to the following near the top of your applet_main()
660routine: 689routine:
661 690
662 while ((opt = getopt(argc, argv, "abc")) > 0) { 691 char *str_b;
663 switch (opt) { 692
664 case 'a': 693 opt_complementary = "cryptic_string";
665 do_a_opt = 1; 694 applet_long_options = <applet>_long_options; /* if you have them */
666 break; 695 opt = getopt32(argc, argv, "ab:c", &str_b);
667 case 'b': 696 if (opt & 1) {
668 do_b_opt = 1; 697 handle_option_a();
669 break; 698 }
670 case 'c': 699 if (opt & 2) {
671 do_c_opt = 1; 700 handle_option_b(str_b);
672 break; 701 }
673 default: 702 if (opt & 4) {
674 show_usage(); /* in utility.c */ 703 handle_option_c();
675 } 704 }
676 }
677 705
678If your applet takes no options (such as 'init'), there should be a line 706If your applet takes no options (such as 'init'), there should be a line
679somewhere in the file reads: 707somewhere in the file reads:
@@ -683,7 +711,4 @@ somewhere in the file reads:
683That way, when people go grepping to see which applets need to be converted to 711That way, when people go grepping to see which applets need to be converted to
684use getopt, they won't get false positives. 712use getopt, they won't get false positives.
685 713
686Additional Note: Do not use the getopt_long library function and do not try to 714For more info and examples, examine getopt32.c, tar.c, wget.c etc.
687hand-roll your own long option parsing. Busybox applets should only support
688short options. Explanations and examples of the short options should be
689documented in usage.h.