aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Whitley <markw@lineo.com>2001-03-14 21:04:53 +0000
committerMark Whitley <markw@lineo.com>2001-03-14 21:04:53 +0000
commit7ddaf7caaeb2b13d3ddf66ab6715a2eaa6cc9ed3 (patch)
tree46d625f9dadd083c7303e46763025a59800e901e
parentbac75ac2d5f859d357820e10100efa07a6bd22cd (diff)
downloadbusybox-w32-7ddaf7caaeb2b13d3ddf66ab6715a2eaa6cc9ed3.tar.gz
busybox-w32-7ddaf7caaeb2b13d3ddf66ab6715a2eaa6cc9ed3.tar.bz2
busybox-w32-7ddaf7caaeb2b13d3ddf66ab6715a2eaa6cc9ed3.zip
Added a section to describe how to convert variables to K&R style using the
mk2knr.pl script. Also some minor cleanups.
-rw-r--r--docs/style-guide.txt87
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:
130Spacing around Parentheses 130Spacing around Parentheses
131~~~~~~~~~~~~~~~~~~~~~~~~~~ 131~~~~~~~~~~~~~~~~~~~~~~~~~~
132 132
133Put a space between C keywords and left parens, but not between 133Put a space between C keywords and left parens, but not between function names
134function names and the left paren that starts it's parameter list (whether it 134and the left paren that starts it's parameter list (whether it is being
135is being declared or called). Examples: 135declared 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
204Variable and Function Names 203Variable 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
226Exceptions: 225Exceptions:
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
234Note: The Busybox codebase is very much a mixture of code gathered from a
235variety of sources. This explains why the current codebase contains such a
236hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird,
237etc.). The K&R guideline explained above should therefore be used on new files
238that are added to the repository. Furthermore, the maintainer of an existing
239file 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
241low priority task. Perhaps in the future we will include some magical Perl
242script that can go through and convert variable names, left as an exercise for
243the reader for now.
244 234
245For the time being, if you want to do a search-and-replace of a variable name 235Converting to K&R
246in different files, do the following in the busybox directory: 236~~~~~~~~~~~~~~~~~
237
238The Busybox codebase is very much a mixture of code gathered from a variety of
239sources. This explains why the current codebase contains such a hodge-podge of
240different naming styles (Java, Pascal, K&R, just-plain-weird, etc.). The K&R
241guideline explained above should therefore be used on new files that are added
242to the repository. Furthermore, the maintainer of an existing file that uses
243alternate naming conventions should, at his own convenience, convert those
244names over to K&R style. Converting variable names is a very low priority
245task.
246
247If you want to do a search-and-replace of a single variable name in different
248files, 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
252If you want to convert all the non-K&R vars in your file all at once, follow
253these 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
270Please be aware of changes that have cascading effects into other files. For
271example, if you're changing the name of something in, say utility.c, you
272should probably run 'scripts/mk2knr.pl utility.c' at first, but when you run
273the 'convertme.pl' script you should run it on _all_ files like so:
274'./convertme.pl *.[ch]'.
275
250 276
251 277
252Avoid The Preprocessor 278Avoid The Preprocessor
@@ -299,17 +325,18 @@ Use 'static inline' instead of a macro.
299 } 325 }
300 326
301Static inline functions are greatly preferred over macros. They provide type 327Static inline functions are greatly preferred over macros. They provide type
302safety, have no length limitations, no formatting limitations, and under gcc 328safety, have no length limitations, no formatting limitations, have an actual
303they are as cheap as macros. Besides, really long macros with backslashes at 329return value, and under gcc they are as cheap as macros. Besides, really long
304the end of each line are ugly as sin. 330macros with backslashes at the end of each line are ugly as sin.
305 331
306 332
307The Folly of #ifdef 333The Folly of #ifdef
308~~~~~~~~~~~~~~~~~~~ 334~~~~~~~~~~~~~~~~~~~
309 335
310Code cluttered with ifdefs is difficult to read and maintain. Don't do it. 336Code cluttered with ifdefs is difficult to read and maintain. Don't do it.
311Instead, put your ifdefs in a header, and conditionally define 'static inline' 337Instead, put your ifdefs at the top of your .c file (or in a header), and
312functions, (or *maybe* macros), which are used in the code. 338conditionally define 'static inline' functions, (or *maybe* macros), which are
339used 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,
480formatting, options, etc.), model it after the equivalent GNU program. 507formatting, options, etc.), model it after the equivalent GNU program.
481Doesn't matter how that program behaves on some other flavor of *NIX; doesn't 508Doesn't matter how that program behaves on some other flavor of *NIX; doesn't
482matter what the POSIX standard says or doesn't say, just model Busybox 509matter what the POSIX standard says or doesn't say, just model Busybox
483programs after their GNU counterparts and nobody has to get hurt. 510programs after their GNU counterparts and it will make life easier on (nearly)
511everyone.
484 512
485The only time we deviate from emulating the GNU behavior is when: 513The 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.
585There is an optimal ammount of commenting that a program can have; you can 613There is an optimal ammount of commenting that a program can have; you can
586comment too much as well as too little. 614comment too much as well as too little.
587 615
588A picture is really worth a thousand words here, so here is an example that 616A picture is really worth a thousand words here, the following example
589illustrates emphasizing logical blocks: 617illustrates 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
651Additional Note: Do not use the getopt_long library function and do not try to 677Additional Note: Do not use the getopt_long library function and do not try to
652hand-roll your own long option parsing. Busybox applets should only support 678hand-roll your own long option parsing. Busybox applets should only support
653short options, plus explanations and examples in usage.h. 679short options. Explanations and examples of the short options should be
680documented in usage.h.