diff options
-rw-r--r-- | docs/style-guide.txt | 87 |
1 files changed, 57 insertions, 30 deletions
diff --git a/docs/style-guide.txt b/docs/style-guide.txt index b4c3bac02..c71f1e609 100644 --- a/docs/style-guide.txt +++ b/docs/style-guide.txt | |||
@@ -130,9 +130,9 @@ between it and the opening control block statement. Examples: | |||
130 | Spacing around Parentheses | 130 | Spacing around Parentheses |
131 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | 131 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
132 | 132 | ||
133 | Put a space between C keywords and left parens, but not between | 133 | Put a space between C keywords and left parens, but not between function names |
134 | function names and the left paren that starts it's parameter list (whether it | 134 | and the left paren that starts it's parameter list (whether it is being |
135 | is being declared or called). Examples: | 135 | declared or called). Examples: |
136 | 136 | ||
137 | Don't do this: | 137 | Don't do this: |
138 | 138 | ||
@@ -200,7 +200,6 @@ block. Example: | |||
200 | 200 | ||
201 | 201 | ||
202 | 202 | ||
203 | |||
204 | Variable and Function Names | 203 | Variable and Function Names |
205 | --------------------------- | 204 | --------------------------- |
206 | 205 | ||
@@ -225,28 +224,55 @@ because it looks like whitespace; using lower-case is easy on the eyes. | |||
225 | 224 | ||
226 | Exceptions: | 225 | Exceptions: |
227 | 226 | ||
228 | - Enums, macros, and constant variables should all be in upper-case with | 227 | - Enums, macros, and constant variables are occasionally written in all |
229 | words optionally seperatedy by underscores (i.e. FIFOTYPE, ISBLKDEV()). | 228 | upper-case with words optionally seperatedy by underscores (i.e. FIFOTYPE, |
229 | ISBLKDEV()). | ||
230 | 230 | ||
231 | - Nobody is going to get mad at you for using 'pvar' as the name of a | 231 | - Nobody is going to get mad at you for using 'pvar' as the name of a |
232 | variable that is a pointer to 'var'. | 232 | variable that is a pointer to 'var'. |
233 | 233 | ||
234 | Note: The Busybox codebase is very much a mixture of code gathered from a | ||
235 | variety of sources. This explains why the current codebase contains such a | ||
236 | hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird, | ||
237 | etc.). The K&R guideline explained above should therefore be used on new files | ||
238 | that are added to the repository. Furthermore, the maintainer of an existing | ||
239 | file that uses alternate naming conventions should -- at his own convenience | ||
240 | -- convert those names over to K&R style; converting variable names is a very | ||
241 | low priority task. Perhaps in the future we will include some magical Perl | ||
242 | script that can go through and convert variable names, left as an exercise for | ||
243 | the reader for now. | ||
244 | 234 | ||
245 | For the time being, if you want to do a search-and-replace of a variable name | 235 | Converting to K&R |
246 | in different files, do the following in the busybox directory: | 236 | ~~~~~~~~~~~~~~~~~ |
237 | |||
238 | The Busybox codebase is very much a mixture of code gathered from a variety of | ||
239 | sources. This explains why the current codebase contains such a hodge-podge of | ||
240 | different naming styles (Java, Pascal, K&R, just-plain-weird, etc.). The K&R | ||
241 | guideline explained above should therefore be used on new files that are added | ||
242 | to the repository. Furthermore, the maintainer of an existing file that uses | ||
243 | alternate naming conventions should, at his own convenience, convert those | ||
244 | names over to K&R style. Converting variable names is a very low priority | ||
245 | task. | ||
246 | |||
247 | If you want to do a search-and-replace of a single variable name in different | ||
248 | files, you can do the following in the busybox directory: | ||
247 | 249 | ||
248 | $ perl -pi -e 's/\bOldVar\b/new_var/g' *.[ch] | 250 | $ perl -pi -e 's/\bOldVar\b/new_var/g' *.[ch] |
249 | 251 | ||
252 | If you want to convert all the non-K&R vars in your file all at once, follow | ||
253 | these steps: | ||
254 | |||
255 | - In the busybox directory type 'scripts/mk2knr.pl files-to-convert'. This | ||
256 | does not do the actual conversion, rather, it generates a script called | ||
257 | 'convertme.pl' that shows what will be converted, giving you a chance to | ||
258 | review the changes beforehand. | ||
259 | |||
260 | - Review the 'convertme.pl' script that gets generated in the busybox | ||
261 | directory and remove / edit any of the substitutions in there. Please | ||
262 | especially check for false positives (strings that should not be | ||
263 | converted). | ||
264 | |||
265 | - Type './convertme.pl same-files-as-before' to perform the actual | ||
266 | conversion. | ||
267 | |||
268 | - Compile and see if everything still works. | ||
269 | |||
270 | Please be aware of changes that have cascading effects into other files. For | ||
271 | example, if you're changing the name of something in, say utility.c, you | ||
272 | should probably run 'scripts/mk2knr.pl utility.c' at first, but when you run | ||
273 | the 'convertme.pl' script you should run it on _all_ files like so: | ||
274 | './convertme.pl *.[ch]'. | ||
275 | |||
250 | 276 | ||
251 | 277 | ||
252 | Avoid The Preprocessor | 278 | Avoid The Preprocessor |
@@ -299,17 +325,18 @@ Use 'static inline' instead of a macro. | |||
299 | } | 325 | } |
300 | 326 | ||
301 | Static inline functions are greatly preferred over macros. They provide type | 327 | Static inline functions are greatly preferred over macros. They provide type |
302 | safety, have no length limitations, no formatting limitations, and under gcc | 328 | safety, have no length limitations, no formatting limitations, have an actual |
303 | they are as cheap as macros. Besides, really long macros with backslashes at | 329 | return value, and under gcc they are as cheap as macros. Besides, really long |
304 | the end of each line are ugly as sin. | 330 | macros with backslashes at the end of each line are ugly as sin. |
305 | 331 | ||
306 | 332 | ||
307 | The Folly of #ifdef | 333 | The Folly of #ifdef |
308 | ~~~~~~~~~~~~~~~~~~~ | 334 | ~~~~~~~~~~~~~~~~~~~ |
309 | 335 | ||
310 | Code cluttered with ifdefs is difficult to read and maintain. Don't do it. | 336 | Code cluttered with ifdefs is difficult to read and maintain. Don't do it. |
311 | Instead, put your ifdefs in a header, and conditionally define 'static inline' | 337 | Instead, put your ifdefs at the top of your .c file (or in a header), and |
312 | functions, (or *maybe* macros), which are used in the code. | 338 | conditionally define 'static inline' functions, (or *maybe* macros), which are |
339 | used in the code. | ||
313 | 340 | ||
314 | Don't do this: | 341 | Don't do this: |
315 | 342 | ||
@@ -480,7 +507,8 @@ When in doubt about the proper behavior of a Busybox program (output, | |||
480 | formatting, options, etc.), model it after the equivalent GNU program. | 507 | formatting, options, etc.), model it after the equivalent GNU program. |
481 | Doesn't matter how that program behaves on some other flavor of *NIX; doesn't | 508 | Doesn't matter how that program behaves on some other flavor of *NIX; doesn't |
482 | matter what the POSIX standard says or doesn't say, just model Busybox | 509 | matter what the POSIX standard says or doesn't say, just model Busybox |
483 | programs after their GNU counterparts and nobody has to get hurt. | 510 | programs after their GNU counterparts and it will make life easier on (nearly) |
511 | everyone. | ||
484 | 512 | ||
485 | The only time we deviate from emulating the GNU behavior is when: | 513 | The only time we deviate from emulating the GNU behavior is when: |
486 | 514 | ||
@@ -585,15 +613,13 @@ one comment) before the block, rather than commenting each and every line. | |||
585 | There is an optimal ammount of commenting that a program can have; you can | 613 | There is an optimal ammount of commenting that a program can have; you can |
586 | comment too much as well as too little. | 614 | comment too much as well as too little. |
587 | 615 | ||
588 | A picture is really worth a thousand words here, so here is an example that | 616 | A picture is really worth a thousand words here, the following example |
589 | illustrates emphasizing logical blocks: | 617 | illustrates how to emphasize logical blocks: |
590 | 618 | ||
591 | while (line = get_line_from_file(fp)) { | 619 | while (line = get_line_from_file(fp)) { |
592 | 620 | ||
593 | /* eat the newline, if any */ | 621 | /* eat the newline, if any */ |
594 | if (line[strlen(line)-1] == '\n') { | 622 | chomp(line); |
595 | line[strlen(line)-1] = '\0'; | ||
596 | } | ||
597 | 623 | ||
598 | /* ignore blank lines */ | 624 | /* ignore blank lines */ |
599 | if (strlen(file_to_act_on) == 0) { | 625 | if (strlen(file_to_act_on) == 0) { |
@@ -650,4 +676,5 @@ use getopt, they won't get false positives. | |||
650 | 676 | ||
651 | Additional Note: Do not use the getopt_long library function and do not try to | 677 | Additional Note: Do not use the getopt_long library function and do not try to |
652 | hand-roll your own long option parsing. Busybox applets should only support | 678 | hand-roll your own long option parsing. Busybox applets should only support |
653 | short options, plus explanations and examples in usage.h. | 679 | short options. Explanations and examples of the short options should be |
680 | documented in usage.h. | ||