Commit Larry Doolittle's buffers-on-stack/buffers-via-malloc patch.
[platform/upstream/busybox.git] / docs / style-guide.txt
1 Busybox Style Guide
2 ===================
3
4 This document describes the coding style conventions used in Busybox. If you
5 add a new file to Busybox or are editing an existing file, please format your
6 code according to this style. If you are the maintainer of a file that does
7 not follow these guidelines, please -- at your own convenience -- modify the
8 file(s) you maintain to bring them into conformance with this style guide.
9 Please note that this is a low priority task.
10
11 To help you format the whitespace of your programs, an ".indent.pro" file is
12 included in the main Busybox source directory that contains option flags to
13 format code as per this style guide. This way you can run GNU indent on your
14 files by typing 'indent myfile.c myfile.h' and it will magically apply all the
15 right formatting rules to your file. Please _do_not_ run this on all the files
16 in the directory, just your own.
17
18
19
20 Declaration Order
21 -----------------
22
23 Here is the order in which code should be laid out in a file:
24
25  - commented program name and one-line description
26  - commented author name and email address(es)
27  - commented GPL boilerplate
28  - commented longer description / notes for the program (if needed)
29  - #includes and #defines
30  - const and global variables
31  - function declarations (if necessary)
32  - function implementations
33
34
35
36 Whitespace and Formatting
37 -------------------------
38
39 This is everybody's favorite flame topic so let's get it out of the way right
40 up front.
41
42
43 Tabs vs. Spaces in Line Indentation
44 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45
46 The preference in Busybox is to indent lines with tabs. Do not indent lines
47 with spaces and do not indents lines using a mixture of tabs and spaces. (The
48 indentation style in the Apache and Postfix source does this sort of thing:
49 \s\s\s\sif (expr) {\n\tstmt; --ick.) The only exception to this rule is
50 multi-line comments that use an asterisk at the beginning of each line, i.e.:
51
52         /t/*
53         /t * This is a block comment.
54         /t * Note that it has multiple lines
55         /t * and that the beginning of each line has a tab plus a space
56         /t * except for the opening '/*' line where the slash
57         /t * is used instead of a space.
58         /t */
59
60 Furthermore, The preference is that tabs be set to display at four spaces
61 wide, but the beauty of using only tabs (and not spaces) at the beginning of
62 lines is that you can set your editor to display tabs at *whatever* number of
63 spaces is desired and the code will still look fine.
64
65
66 Operator Spacing
67 ~~~~~~~~~~~~~~~~
68
69 Put spaces between terms and operators. Example:
70
71         Don't do this:
72
73                 for(i=0;i<num_items;i++){
74
75         Do this instead:
76
77                 for (i = 0; i < num_items; i++) {
78
79         While it extends the line a bit longer, the spaced version is more
80         readable. An allowable exception to this rule is the situation where
81         excluding the spacing makes it more obvious that we are dealing with a
82         single term (even if it is a compound term) such as:
83
84                 if (str[idx] == '/' && str[idx-1] != '\\')
85
86         or
87
88                 if ((argc-1) - (optind+1) > 0)
89
90
91 Bracket Spacing
92 ~~~~~~~~~~~~~~~
93
94 If an opening bracket starts a function, it should be on the
95 next line with no spacing before it. However, if a bracket follows an opening
96 control block, it should be on the same line with a single space (not a tab)
97 between it and the opening control block statement. Examples:
98
99         Don't do this:
100
101                 while (!done)
102                 {
103
104                 do
105                 {
106
107         Don't do this either:
108
109                 while (!done){
110                 do{
111
112         And for heaven's sake, don't do this:
113
114                 while (!done)
115                   {
116                 do
117                   {
118
119         Do this instead:
120
121                 while (!done) {
122                 do {
123
124
125 Paren Spacing
126 ~~~~~~~~~~~~~
127
128 Put a space between C keywords and left parens, but not between
129 function names and the left paren that starts it's parameter list (whether it
130 is being declared or called). Examples:
131
132         Don't do this:
133
134                 while(foo) {
135                 for(i = 0; i < n; i++) {
136
137         Do this instead:
138
139                 while (foo) {
140                 for (i = 0; i < n; i++) {
141
142         But do functions like this:
143
144                 static int my_func(int foo, char bar)
145                 ...
146                 baz = my_func(1, 2);
147
148
149 Cuddled Elses
150 ~~~~~~~~~~~~~
151
152 Also, please "cuddle" your else statements by putting the else keyword on the
153 same line after the right bracket that closes an 'if' statement.
154
155         Don't do this:
156
157         if (foo) {
158                 stmt;
159         }
160         else {
161                 stmt;
162         }
163
164         Do this instead:
165
166         if (foo) {
167                 stmt;
168         } else {
169                 stmt;
170         }
171
172 The exception to this rule is if you want to include a comment before the else
173 block. Example:
174
175         if (foo) {
176                 stmts...
177         }
178         /* otherwise, we're just kidding ourselves, so re-frob the input */
179         else {
180                 other_stmts...
181         }
182
183
184
185
186 Variable and Function Names
187 ---------------------------
188
189 Use the K&R style with names in all lower-case and underscores occasionally
190 used to separate words (e.g., "variable_name" and "numchars" are both
191 acceptable). Using underscores makes variable and function names more readable
192 because it looks like whitespace; using lower-case is easy on the eyes.
193
194         Frowned upon:
195
196                 hitList
197                 TotalChars
198                 szFileName
199                 pf_Nfol_TriState
200
201         Preferred:
202
203                 hit_list
204                 total_chars
205                 file_name
206                 sensible_name
207
208 Exceptions:
209
210  - Enums, macros, and constant variables should all be in upper-case with
211    words optionally seperatedy by underscores (i.e. FIFOTYPE, ISBLKDEV()).
212
213  - Nobody is going to get mad at you for using 'pvar' as the name of a
214    variable that is a pointer to 'var'.
215
216 Note: The Busybox codebase is very much a mixture of code gathered from a
217 variety of sources. This explains why the current codebase contains such a
218 hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird,
219 etc.). The K&R guideline explained above should therefore be used on new files
220 that are added to the repository. Furthermore, the maintainer of an existing
221 file that uses alternate naming conventions should -- at his own convenience
222 -- convert those names over to K&R style; converting variable names is a very
223 low priority task. Perhaps in the future we will include some magical Perl
224 script that can go through and convert variable names, left as an exercise for
225 the reader for now.
226
227 For the time being, if you want to do a search-and-replace of a variable name
228 in different files, do the following in the busybox directory:
229
230         $ perl -pi -e 's/\bOldVar\b/new_var/g' *.[ch]
231
232
233
234 Avoid The Preprocessor
235 ----------------------
236
237 At best, the preprocessor is a necessary evil, helping us account for platform
238 and architecture differences. Using the preprocessor unnecessarily is just
239 plain evil.
240
241
242 The Folly of #define
243 ~~~~~~~~~~~~~~~~~~~~
244
245 Use 'const <type> var' for declaring constants.
246
247         Don't do this:
248
249                 #define var 80
250
251         Do this instead, when the variable is in a header file and will be used in
252         several source files: 
253
254                 const int var = 80; 
255
256         Or do this when the variable is used only in a single source file:
257
258                 static const int var = 80; 
259
260 Declaring variables as '[static] const' gives variables an actual type and
261 makes the compiler do type checking for you; the preprocessor does _no_ type
262 checking whatsoever, making it much more error prone. Declaring variables with
263 '[static] const' also makes debugging programs much easier since the value of
264 the variable can be easily queried and displayed.
265
266
267 The Folly of Macros
268 ~~~~~~~~~~~~~~~~~~~
269
270 Use 'static inline' instead of a macro.
271
272         Don't do this:
273
274                 #define mini_func(param1, param2) (param1 << param2)
275
276         Do this instead:
277
278                 static inline int mini_func(int param1, param2)
279                 {
280                         return (param1 << param2);
281                 }
282
283 Static inline functions are greatly preferred over macros. They provide type
284 safety, have no length limitations, no formatting limitations, and under gcc
285 they are as cheap as macros. Besides, really long macros with backslashes at
286 the end of each line are ugly as sin.
287
288
289 The Folly of #ifdef
290 ~~~~~~~~~~~~~~~~~~~
291
292 Code cluttered with ifdefs is difficult to read and maintain. Don't do it.
293 Instead, put your ifdefs in a header, and conditionally define 'static inline'
294 functions, (or *maybe* macros), which are used in the code.  
295
296         Don't do this:
297
298                 ret = my_func(bar, baz);
299                 if (!ret)
300                         return -1;
301                 #ifdef BB_FEATURE_FUNKY
302                         maybe_do_funky_stuff(bar, baz);
303                 #endif
304
305         Do this instead:
306
307         (in .h header file)
308
309                 #ifndef BB_FEATURE_FUNKY
310                 static inline void maybe_do_funky_stuff (int bar, int baz) {}
311                 #endif
312
313         (in the .c source file)
314
315                 ret = my_func(bar, baz);
316                 if (!ret)
317                         return -1;
318                 maybe_do_funky_stuff(bar, baz);
319
320 The great thing about this approach is that the compiler will optimize away
321 the "no-op" case when the feature is turned off.
322
323 Note also the use of the word 'maybe' in the function name to indicate
324 conditional execution.
325
326
327
328 Notes on Strings
329 ----------------
330
331 Strings in C can get a little thorny. Here's some guidelines for dealing with
332 strings in Busybox. (There is surely more that could be added to this
333 section.)
334
335
336 String Files
337 ~~~~~~~~~~~~
338
339 Put all help/usage messages in usage.c. Put other strings in messages.c.
340 Putting these strings into their own file is a calculated decision designed to
341 confine spelling errors to a single place and aid internationalization
342 efforts, if needed. (Side Note: we might want to use a single file - maybe
343 called 'strings.c' - instead of two, food for thought).
344
345
346 Testing String Equivalence
347 ~~~~~~~~~~~~~~~~~~~~~~~~~~
348
349 There's a right way and a wrong way to test for sting equivalence with
350 strcmp():
351
352         The wrong way:
353
354                 if (!strcmp(string, "foo")) {
355                         ...
356
357         The right way:
358
359                 if (strcmp(string, "foo") == 0){
360                         ...
361
362 The use of the "equals" (==) operator in the latter example makes it much more
363 obvious that you are testing for equivalence. The former example with the
364 "not" (!) operator makes it look like you are testing for an error. In a more
365 perfect world, we would have a streq() function in the string library, but
366 that ain't the world we're living in.
367
368
369 Avoid Dangerous String Functions
370 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
371
372 Unfortunately, the way C handles strings makes them prone to overruns when
373 certain library functions are (mis)used. The following table  offers a summary
374 of some of the more notorious troublemakers:
375
376 function     overflows         preferred
377 ----------------------------------------
378 strcpy       dest string       strncpy
379 strcat       dest string       strncat
380 gets         string it gets    fgets
381 getwd        buf string        getcwd
382 [v]sprintf   str buffer        [v]snprintf
383 realpath     path buffer       use with pathconf
384 [vf]scanf    its arguments     just avoid it
385
386
387 The above is by no means a complete list. Be careful out there.
388
389
390
391 Avoid Big Static Buffers
392 ------------------------
393
394 First, some background to put this discussion in context: Static buffers look
395 like this in code:
396
397         /* in a .c file outside any functions */
398         static char *buffer[BUFSIZ]; /* happily used by any function in this file,
399                                         but ick! big! */
400
401 The problem with these is that any time any busybox app is run, you pay a
402 memory penalty for this buffer, even if the applet that uses said buffer is
403 not run. This can be fixed, thusly:
404
405         static char *buffer;
406         ...
407         other_func()
408         {
409                 strcpy(buffer, lotsa_chars); /* happily uses global *buffer */
410         ...
411         foo_main()
412         {
413                 buffer = xmalloc(sizeof(char)*BUFSIZ);
414         ...
415
416 However, this approach trades bss segment for text segment. Rather than
417 mallocing the buffers (and thus growing the text size), buffers can be
418 declared on the stack in the *_main() function and made available globally by
419 assigning them to a global pointer thusly:
420
421         static char *pbuffer;
422         ...
423         other_func()
424         {
425                 strcpy(pbuffer, lotsa_chars); /* happily uses global *pbuffer */
426         ...
427         foo_main()
428         {
429                 char *buffer[BUFSIZ]; /* declared locally, on stack */
430                 pbuffer = buffer;     /* but available globally */
431         ...
432
433 This last approach has some advantages (low code size, space not used until
434 it's needed), but can be a problem in some low resource machines that have
435 very limited stack space (e.g., uCLinux).  busybox.h declares a macro that
436 implements compile-time selection between xmalloc() and stack creation, so
437 you can code the line in question as
438                 RESERVE_BB_BUFFER(buffer, BUFSIZ);
439 and the right thing will happen, based on the customer's configuration.
440
441
442
443 Miscellaneous Coding Guidelines
444 -------------------------------
445
446 The following are important items that don't fit into any of the above
447 sections.
448
449
450 Model Busybox Applets After GNU Counterparts
451 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
452
453 When in doubt about the proper behavior of a Busybox program (output,
454 formatting, options, etc.), model it after the equivalent GNU program.
455 Doesn't matter how that program behaves on some other flavor of *NIX; doesn't
456 matter what the POSIX standard says or doesn't say, just model Busybox
457 programs after their GNU counterparts and nobody has to get hurt.
458
459 The only time we deviate from emulating the GNU behavior is when:
460
461         - We are deliberately not supporting a feature (such as a command line
462           switch)
463         - Emulating the GNU behavior is prohibitively expensive (lots more code
464           would be required, lots more memory would be used, etc.)
465         - The difference is minor or cosmetic
466
467 A note on the 'cosmetic' case: Output differences might be considered
468 cosmetic, but if the output is significant enough to break other scripts that
469 use the output, it should really be fixed.
470
471
472 Scope
473 ~~~~~
474
475 If a const variable is used only in a single source file, put it in the source
476 file and not in a header file. Likewise, if a const variable is used in only
477 one function, do not make it global to the file. Instead, declare it inside
478 the function body. Bottom line: Make a conscious effort to limit declarations
479 to the smallest scope possible.
480
481 Inside applet files, all functions should be declared static so as to keep the
482 global name space clean. The only exception to this rule is the "applet_main"
483 function which must be declared extern.
484
485 If you write a function that performs a task that could be useful outside the
486 immediate file, turn it into a general-purpose function with no ties to any
487 applet and put it in the utility.c file instead.
488
489
490 Brackets Are Your Friends
491 ~~~~~~~~~~~~~~~~~~~~~~~~~
492
493 Please use brackets on all if and else statements, even if it is only one
494 line. Example:
495
496         Don't do this:
497
498                 if (foo)
499                         stmt1;
500                 stmt2
501                 stmt3;
502
503         Do this instead:
504
505                 if (foo) {
506                         stmt1;
507                 }
508                 stmt2
509                 stmt3;
510
511 The "bracketless" approach is error prone because someday you might add a line
512 like this:
513
514                 if (foo)
515                         stmt1;
516                         new_line();
517                 stmt2
518                 stmt3;
519
520 And the resulting behavior of your program would totally bewilder you. (Don't
521 laugh, it happens to us all.) Remember folks, this is C, not Python.
522
523
524 Function Declarations
525 ~~~~~~~~~~~~~~~~~~~~~
526
527 Do not use old-style function declarations that declare variable types between
528 the parameter list and opening bracket. Example:
529
530         Don't do this:
531
532                 int foo(parm1, parm2)
533                         char parm1;
534                         float parm2;
535                 {
536                         ....
537
538         Do this instead:
539
540                 int foo(char parm1, float parm2)
541                 {
542                         ....
543
544 The only time you would ever need to use the old declaration syntax is to
545 support ancient, antediluvian compilers. To our good fortune, we have access
546 to more modern compilers and the old declaration syntax is neither necessary
547 nor desired.
548
549
550 Emphasizing Logical Blocks
551 ~~~~~~~~~~~~~~~~~~~~~~~~~~
552
553 Organization and readability are improved by putting extra newlines around
554 blocks of code that perform a single task. These are typically blocks that
555 begin with a C keyword, but not always.
556
557 Furthermore, you should put a single comment (not necessarily one line, just
558 one comment) before the block, rather than commenting each and every line.
559 There is an optimal ammount of commenting that a program can have; you can
560 comment too much as well as too little.
561
562 A picture is really worth a thousand words here, so here is an example that
563 illustrates emphasizing logical blocks:
564
565         while (line = get_line_from_file(fp)) {
566
567                 /* eat the newline, if any */
568                 if (line[strlen(line)-1] == '\n') {
569                         line[strlen(line)-1] = '\0';
570                 }
571
572                 /* ignore blank lines */
573                 if (strlen(file_to_act_on) == 0) {
574                         continue;
575                 }
576
577                 /* if the search string is in this line, print it,
578                  * unless we were told to be quiet */
579                 if (strstr(line, search) && !be_quiet) {
580                         puts(line);
581                 }
582
583                 /* clean up */
584                 free(line);
585         }