style-guide.txt 19 KB

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