]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/style.9
Merge compiler-rt trunk r366426, resolve conflicts, and add
[FreeBSD/FreeBSD.git] / share / man / man9 / style.9
1 .\"-
2 .\" Copyright (c) 1995-2005 The FreeBSD Project
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\"
14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 .\" SUCH DAMAGE.
25 .\"
26 .\"     From: @(#)style 1.14 (Berkeley) 4/28/95
27 .\" $FreeBSD$
28 .\"
29 .Dd June 3, 2019
30 .Dt STYLE 9
31 .Os
32 .Sh NAME
33 .Nm style
34 .Nd "kernel source file style guide"
35 .Sh DESCRIPTION
36 This file specifies the preferred style for kernel source files in the
37 .Fx
38 source tree.
39 It is also a guide for the preferred userland code style.
40 Many of the style rules are implicit in the examples.
41 Be careful to check the examples before assuming that
42 .Nm
43 is silent on an issue.
44 .Bd -literal
45 /*
46  * Style guide for FreeBSD.  Based on the CSRG's KNF (Kernel Normal Form).
47  *
48  *      @(#)style       1.14 (Berkeley) 4/28/95
49  * $FreeBSD$
50  */
51
52 /*
53  * VERY important single-line comments look like this.
54  */
55
56 /* Most single-line comments look like this. */
57
58 /*
59  * Multi-line comments look like this.  Make them real sentences.  Fill
60  * them so they look like real paragraphs.
61  */
62 .Ed
63 .Pp
64 The copyright header should be a multi-line comment, with the first
65 line of the comment having a dash after the star like so:
66 .Bd -literal
67 /*-
68  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
69  *
70  * Copyright (c) 1984-2025 John Q. Public
71  *
72  * Long, boring license goes here, but trimmed for brevity
73  */
74 .Ed
75 .Pp
76 An automatic script collects license information from the tree for
77 all comments that start in the first column with
78 .Dq Li "/*-" .
79 If you desire to flag
80 .Xr indent 1
81 to not reformat a comment that starts in the first column which is not a
82 license or copyright notice, change the dash to a star for those
83 comments.
84 Comments starting in columns other than the first are never
85 considered license statements.
86 Use the appropriate SPDX-License-Identifier line before the copyright.
87 .Pp
88 After any copyright header, there is a blank line, and the
89 .Li $\&FreeBSD$
90 for non C/C++ language source files.
91 Version control system ID tags should only exist once in a file
92 (unlike in this one).
93 Non-C/C++ source files follow the example above, while C/C++ source files
94 follow the one below.
95 All VCS (version control system) revision identification in files obtained
96 from elsewhere should be maintained, including, where applicable, multiple IDs
97 showing a file's history.
98 In general, do not edit foreign IDs or their infrastructure.
99 Unless otherwise wrapped (such as
100 .Dq Li "#if defined(LIBC_SCCS)" ) ,
101 enclose both in
102 .Dq Li "#if 0 ... #endif"
103 to hide any uncompilable bits
104 and to keep the IDs out of object files.
105 Only add
106 .Dq Li "From: "
107 in front of foreign VCS IDs if the file is renamed.
108 .Bd -literal
109 /* From: @(#)style      1.14 (Berkeley) 4/28/95 */
110
111 #include <sys/cdefs.h>
112 __FBSDID("$FreeBSD$");
113 .Ed
114 .Pp
115 Leave one blank line before the header files.
116 .Pp
117 Kernel include files
118 .Pa ( sys/*.h )
119 come first.
120 If
121 .In sys/cdefs.h
122 is needed for
123 .Fn __FBSDID ,
124 include it first.
125 If either
126 .In sys/types.h
127 or
128 .In sys/param.h
129 is needed, include it before other include files.
130 .Po
131 .In sys/param.h
132 includes
133 .In sys/types.h ;
134 do not include both.
135 .Pc
136 The remaining kernel headers should be sorted alphabetically.
137 .Bd -literal
138 #include <sys/types.h>  /* Non-local includes in angle brackets. */
139 #include <sys/endian.h>
140 #include <sys/lock.h>
141 #include <sys/queue.h>
142 .Ed
143 .Pp
144 For a network program, put the network include files next.
145 .Bd -literal
146 #include <net/if.h>
147 #include <net/if_dl.h>
148 #include <net/route.h>
149 #include <netinet/in.h>
150 #include <protocols/rwhod.h>
151 .Ed
152 .Pp
153 Do not include files from
154 .Pa /usr/include
155 in the kernel.
156 .Pp
157 Leave a blank line before the next group, the
158 .Pa /usr/include
159 files,
160 which should be sorted alphabetically by name.
161 .Bd -literal
162 #include <stdio.h>
163 .Ed
164 .Pp
165 Global pathnames are defined in
166 .In paths.h .
167 Pathnames local
168 to the program go in
169 .Qq Pa pathnames.h
170 in the local directory.
171 .Bd -literal
172 #include <paths.h>
173 .Ed
174 .Pp
175 Leave another blank line before the local include files.
176 .Bd -literal
177 #include "pathnames.h"          /* Local includes in double quotes. */
178 .Ed
179 .Pp
180 Do not
181 .Ic #define
182 or declare names in the implementation namespace except
183 for implementing application interfaces.
184 .Pp
185 The names of
186 .Dq unsafe
187 macros (ones that have side effects), and the names of macros for
188 manifest constants, are all in uppercase.
189 The expansions of expression-like macros are either a single token
190 or have outer parentheses.
191 Put a single tab character between the
192 .Ic #define
193 and the macro name.
194 If a macro is an inline expansion of a function, the function name is
195 all in lowercase and the macro has the same name all in uppercase.
196 .\" XXX the above conflicts with ANSI style where the names are the
197 .\" same and you #undef the macro (if any) to get the function.
198 .\" It is not followed for MALLOC(), and not very common if inline
199 .\" functions are used.
200 Right-justify the
201 backslashes; it makes it easier to read.
202 If the macro encapsulates a compound statement, enclose it in a
203 .Ic do
204 loop,
205 so that it can safely be used in
206 .Ic if
207 statements.
208 Any final statement-terminating semicolon should be
209 supplied by the macro invocation rather than the macro, to make parsing easier
210 for pretty-printers and editors.
211 .Bd -literal
212 #define MACRO(x, y) do {                                                \e
213         variable = (x) + (y);                                           \e
214         (y) += 2;                                                       \e
215 } while (0)
216 .Ed
217 .Pp
218 When code is conditionally compiled using
219 .Ic #ifdef
220 or
221 .Ic #if ,
222 a comment may be added following the matching
223 .Ic #endif
224 or
225 .Ic #else
226 to permit the reader to easily discern where conditionally compiled code
227 regions end.
228 This comment should be used only for (subjectively) long regions, regions
229 greater than 20 lines, or where a series of nested
230 .Ic #ifdef 's
231 may be confusing to the reader.
232 The comment should be separated from the
233 .Ic #endif
234 or
235 .Ic #else
236 by a single space.
237 For short conditionally compiled regions, a closing comment should not be
238 used.
239 .Pp
240 The comment for
241 .Ic #endif
242 should match the expression used in the corresponding
243 .Ic #if
244 or
245 .Ic #ifdef .
246 The comment for
247 .Ic #else
248 and
249 .Ic #elif
250 should match the inverse of the expression(s) used in the preceding
251 .Ic #if
252 and/or
253 .Ic #elif
254 statements.
255 In the comments, the subexpression
256 .Dq Li defined(FOO)
257 is abbreviated as
258 .Dq Li FOO .
259 For the purposes of comments,
260 .Dq Ic #ifndef Li FOO
261 is treated as
262 .Dq Ic #if Li !defined(FOO) .
263 .Bd -literal
264 #ifdef KTRACE
265 #include <sys/ktrace.h>
266 #endif
267
268 #ifdef COMPAT_43
269 /* A large region here, or other conditional code. */
270 #else /* !COMPAT_43 */
271 /* Or here. */
272 #endif /* COMPAT_43 */
273
274 #ifndef COMPAT_43
275 /* Yet another large region here, or other conditional code. */
276 #else /* COMPAT_43 */
277 /* Or here. */
278 #endif /* !COMPAT_43 */
279 .Ed
280 .Pp
281 The project is slowly moving to use the
282 .St -isoC-99
283 unsigned integer identifiers of the form
284 .Vt uintXX_t
285 in preference to the older
286 .Bx Ns -style
287 integer identifiers of the form
288 .Vt u_intXX_t .
289 New code should use the former, and old code should be converted to
290 the new form if other major work is being done in that area and
291 there is no overriding reason to prefer the older
292 .Bx Ns -style .
293 Like white-space commits, care should be taken in making
294 .Vt uintXX_t
295 only commits.
296 .Pp
297 Similarly, the project is slowly moving to use the
298 .St -isoC-99
299 .Vt bool
300 in preference to the older
301 .Vt int
302 or
303 .Vt boolean_t .
304 New code should use
305 .Vt bool ,
306 and old code may be converted if it is
307 reasonable to do so.
308 Literal values are named
309 .Dv true
310 and
311 .Dv false .
312 These are preferred to the old spellings
313 .Dv TRUE
314 and
315 .Dv FALSE .
316 Userspace code should include
317 .In stdbool.h ,
318 while kernel code should include
319 .In sys/types.h .
320 .Pp
321 Likewise, the project is moving to using the
322 .St -isoC-99
323 designated initializers when it makes sense to do so.
324 .Pp
325 Enumeration values are all uppercase.
326 .Bd -literal
327 enum enumtype { ONE, TWO } et;
328 .Ed
329 .Pp
330 The use of internal_underscores in identifiers is preferred over
331 camelCase or TitleCase.
332 .Pp
333 In declarations, do not put any whitespace between asterisks and
334 adjacent tokens, except for tokens that are identifiers related to
335 types.
336 (These identifiers are the names of basic types, type
337 qualifiers, and
338 .Ic typedef Ns -names
339 other than the one being declared.)
340 Separate these identifiers from asterisks using a single space.
341 .Pp
342 When declaring variables in structures, declare them sorted by use, then
343 by size (largest to smallest), and then in alphabetical order.
344 The first category normally does not apply, but there are exceptions.
345 Each one gets its own line.
346 Try to make the structure
347 readable by aligning the member names using either one or two tabs
348 depending upon your judgment.
349 You should use one tab only if it suffices to align at least 90% of
350 the member names.
351 Names following extremely long types
352 should be separated by a single space.
353 .Pp
354 Major structures should be declared at the top of the file in which they
355 are used, or in separate header files if they are used in multiple
356 source files.
357 Use of the structures should be by separate declarations
358 and should be
359 .Ic extern
360 if they are declared in a header file.
361 .Bd -literal
362 struct foo {
363         struct foo      *next;          /* List of active foo. */
364         struct mumble   amumble;        /* Comment for mumble. */
365         int             bar;            /* Try to align the comments. */
366         struct verylongtypename *baz;   /* Does not fit in 2 tabs. */
367 };
368 struct foo *foohead;                    /* Head of global foo list. */
369 .Ed
370 .Pp
371 Use
372 .Xr queue 3
373 macros rather than rolling your own lists, whenever possible.
374 Thus,
375 the previous example would be better written:
376 .Bd -literal
377 #include <sys/queue.h>
378
379 struct foo {
380         LIST_ENTRY(foo) link;           /* Use queue macros for foo lists. */
381         struct mumble   amumble;        /* Comment for mumble. */
382         int             bar;            /* Try to align the comments. */
383         struct verylongtypename *baz;   /* Does not fit in 2 tabs. */
384 };
385 LIST_HEAD(, foo) foohead;               /* Head of global foo list. */
386 .Ed
387 .Pp
388 Avoid using typedefs for structure types.
389 Typedefs are problematic because they do not properly hide their
390 underlying type; for example you need to know if the typedef is
391 the structure itself or a pointer to the structure.
392 In addition they must be declared exactly once, whereas an
393 incomplete structure type can be mentioned as many times as
394 necessary.
395 Typedefs are difficult to use in stand-alone header files:
396 the header that defines the typedef must be included
397 before the header that uses it, or by the header that uses
398 it (which causes namespace pollution), or there must be a
399 back-door mechanism for obtaining the typedef.
400 .Pp
401 When convention requires a
402 .Ic typedef ,
403 make its name match the struct tag.
404 Avoid typedefs ending in
405 .Dq Li _t ,
406 except as specified in Standard C or by POSIX.
407 .Bd -literal
408 /* Make the structure name match the typedef. */
409 typedef struct bar {
410         int     level;
411 } BAR;
412 typedef int             foo;            /* This is foo. */
413 typedef const long      baz;            /* This is baz. */
414 .Ed
415 .Pp
416 All functions are prototyped somewhere.
417 .Pp
418 Function prototypes for private functions (i.e., functions not used
419 elsewhere) go at the top of the first source module.
420 Functions
421 local to one source module should be declared
422 .Ic static .
423 .Pp
424 Functions used from other parts of the kernel are prototyped in the
425 relevant include file.
426 Function prototypes should be listed in a logical order, preferably
427 alphabetical unless there is a compelling reason to use a different
428 ordering.
429 .Pp
430 Functions that are used locally in more than one module go into a
431 separate header file, e.g.,
432 .Qq Pa extern.h .
433 .Pp
434 Do not use the
435 .Dv __P
436 macro.
437 .Pp
438 In general code can be considered
439 .Dq "new code"
440 when it makes up about 50% or more of the file(s) involved.
441 This is enough
442 to break precedents in the existing code and use the current
443 .Nm
444 guidelines.
445 .Pp
446 The kernel has a name associated with parameter types, e.g., in the kernel
447 use:
448 .Bd -literal
449 void    function(int fd);
450 .Ed
451 .Pp
452 In header files visible to userland applications, prototypes that are
453 visible must use either
454 .Dq protected
455 names (ones beginning with an underscore)
456 or no names with the types.
457 It is preferable to use protected names.
458 E.g., use:
459 .Bd -literal
460 void    function(int);
461 .Ed
462 .Pp
463 or:
464 .Bd -literal
465 void    function(int _fd);
466 .Ed
467 .Pp
468 Prototypes may have an extra space after a tab to enable function names
469 to line up:
470 .Bd -literal
471 static char     *function(int _arg, const char *_arg2, struct foo *_arg3,
472                     struct bar *_arg4);
473 static void      usage(void);
474
475 /*
476  * All major routines should have a comment briefly describing what
477  * they do.  The comment before the "main" routine should describe
478  * what the program does.
479  */
480 int
481 main(int argc, char *argv[])
482 {
483         char *ep;
484         long num;
485         int ch;
486 .Ed
487 .Pp
488 For consistency,
489 .Xr getopt 3
490 should be used to parse options.
491 Options
492 should be sorted in the
493 .Xr getopt 3
494 call and the
495 .Ic switch
496 statement, unless
497 parts of the
498 .Ic switch
499 cascade.
500 Elements in a
501 .Ic switch
502 statement that cascade should have a
503 .Li FALLTHROUGH
504 comment.
505 Numerical arguments should be checked for accuracy.
506 Code which is unreachable for non-obvious reasons may be marked /*
507 .Li NOTREACHED
508 */.
509 .Bd -literal
510         while ((ch = getopt(argc, argv, "abNn:")) != -1)
511                 switch (ch) {           /* Indent the switch. */
512                 case 'a':               /* Do not indent the case. */
513                         aflag = 1;      /* Indent case body one tab. */
514                         /* FALLTHROUGH */
515                 case 'b':
516                         bflag = 1;
517                         break;
518                 case 'N':
519                         Nflag = 1;
520                         break;
521                 case 'n':
522                         num = strtol(optarg, &ep, 10);
523                         if (num <= 0 || *ep != '\e0') {
524                                 warnx("illegal number, -n argument -- %s",
525                                     optarg);
526                                 usage();
527                         }
528                         break;
529                 case '?':
530                 default:
531                         usage();
532                 }
533         argc -= optind;
534         argv += optind;
535 .Ed
536 .Pp
537 Space after keywords
538 .Pq Ic if , while , for , return , switch .
539 Two styles of braces
540 .Ql ( \&{
541 and
542 .Ql \&} )
543 are allowed for single line statements.
544 Either they are used for all single statements, or
545 they are used only where needed for clarity.
546 Usage within a function should be consistent.
547 Forever loops are done with
548 .Ic for Ns 's ,
549 not
550 .Ic while Ns 's .
551 .Bd -literal
552         for (p = buf; *p != '\e0'; ++p)
553                 ;       /* nothing */
554         for (;;)
555                 stmt;
556         for (;;) {
557                 z = a + really + long + statement + that + needs +
558                     two + lines + gets + indented + four + spaces +
559                     on + the + second + and + subsequent + lines;
560         }
561         for (;;) {
562                 if (cond)
563                         stmt;
564         }
565         if (val != NULL)
566                 val = realloc(val, newsize);
567 .Ed
568 .Pp
569 Parts of a
570 .Ic for
571 loop may be left empty.
572 Do not put declarations
573 inside blocks unless the routine is unusually complicated.
574 .Bd -literal
575         for (; cnt < 15; cnt++) {
576                 stmt1;
577                 stmt2;
578         }
579 .Ed
580 .Pp
581 Indentation is an 8 character tab.
582 Second level indents are four spaces.
583 If you have to wrap a long statement, put the operator at the end of the
584 line.
585 .Bd -literal
586         while (cnt < 20 && this_variable_name_is_too_long &&
587             ep != NULL)
588                 z = a + really + long + statement + that + needs +
589                     two + lines + gets + indented + four + spaces +
590                     on + the + second + and + subsequent + lines;
591 .Ed
592 .Pp
593 Do not add whitespace at the end of a line, and only use tabs
594 followed by spaces
595 to form the indentation.
596 Do not use more spaces than a tab will produce
597 and do not use spaces in front of tabs.
598 .Pp
599 Closing and opening braces go on the same line as the
600 .Ic else .
601 Braces that are not necessary may be left out.
602 .Bd -literal
603         if (test)
604                 stmt;
605         else if (bar) {
606                 stmt;
607                 stmt;
608         } else
609                 stmt;
610 .Ed
611 .Pp
612 No spaces after function names.
613 Commas have a space after them.
614 No spaces
615 after
616 .Ql \&(
617 or
618 .Ql \&[
619 or preceding
620 .Ql \&]
621 or
622 .Ql \&)
623 characters.
624 .Bd -literal
625         error = function(a1, a2);
626         if (error != 0)
627                 exit(error);
628 .Ed
629 .Pp
630 Unary operators do not require spaces, binary operators do.
631 Do not use parentheses unless they are required for precedence or unless the
632 statement is confusing without them.
633 Remember that other people may
634 confuse easier than you.
635 Do YOU understand the following?
636 .Bd -literal
637         a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
638         k = !(l & FLAGS);
639 .Ed
640 .Pp
641 Exits should be 0 on success, or 1 on failure.
642 .Bd -literal
643         exit(0);        /*
644                          * Avoid obvious comments such as
645                          * "Exit 0 on success."
646                          */
647 }
648 .Ed
649 .Pp
650 The function type should be on a line by itself
651 preceding the function.
652 The opening brace of the function body should be
653 on a line by itself.
654 .Bd -literal
655 static char *
656 function(int a1, int a2, float fl, int a4)
657 {
658 .Ed
659 .Pp
660 When declaring variables in functions declare them sorted by size,
661 then in alphabetical order; multiple ones per line are okay.
662 If a line overflows reuse the type keyword.
663 .Pp
664 Be careful to not obfuscate the code by initializing variables in
665 the declarations.
666 Use this feature only thoughtfully.
667 DO NOT use function calls in initializers.
668 .Bd -literal
669         struct foo one, *two;
670         double three;
671         int *four, five;
672         char *six, seven, eight, nine, ten, eleven, twelve;
673
674         four = myfunction();
675 .Ed
676 .Pp
677 Do not declare functions inside other functions; ANSI C says that
678 such declarations have file scope regardless of the nesting of the
679 declaration.
680 Hiding file declarations in what appears to be a local
681 scope is undesirable and will elicit complaints from a good compiler.
682 .Pp
683 Casts and
684 .Ic sizeof Ns 's
685 are not followed by a space.
686 Note that
687 .Xr indent 1
688 does not understand this rule.
689 .Ic sizeof Ns 's
690 are written with parenthesis always.
691 The redundant parenthesis rules do not apply to
692 .Fn sizeof var
693 instances.
694 .Pp
695 .Dv NULL
696 is the preferred null pointer constant.
697 Use
698 .Dv NULL
699 instead of
700 .Vt ( "type *" ) Ns 0
701 or
702 .Vt ( "type *" ) Ns Dv NULL
703 in contexts where the compiler knows the
704 type, e.g., in assignments.
705 Use
706 .Vt ( "type *" ) Ns Dv NULL
707 in other contexts,
708 in particular for all function args.
709 (Casting is essential for
710 variadic args and is necessary for other args if the function prototype
711 might not be in scope.)
712 Test pointers against
713 .Dv NULL ,
714 e.g., use:
715 .Bd -literal
716 (p = f()) == NULL
717 .Ed
718 .Pp
719 not:
720 .Bd -literal
721 !(p = f())
722 .Ed
723 .Pp
724 Do not use
725 .Ic \&!
726 for tests unless it is a boolean, e.g., use:
727 .Bd -literal
728 if (*p == '\e0')
729 .Ed
730 .Pp
731 not:
732 .Bd -literal
733 if (!*p)
734 .Ed
735 .Pp
736 Routines returning
737 .Vt "void *"
738 should not have their return values cast
739 to any pointer type.
740 .Pp
741 Values in
742 .Ic return
743 statements should be enclosed in parentheses.
744 .Pp
745 Use
746 .Xr err 3
747 or
748 .Xr warn 3 ,
749 do not roll your own.
750 .Bd -literal
751         if ((four = malloc(sizeof(struct foo))) == NULL)
752                 err(1, (char *)NULL);
753         if ((six = (int *)overflow()) == NULL)
754                 errx(1, "number overflowed");
755         return (eight);
756 }
757 .Ed
758 .Pp
759 When converting K&R style declarations to ANSI style, preserve
760 any comments about parameters.
761 .Pp
762 Long parameter lists are wrapped with a normal four space indent.
763 .Pp
764 Variable numbers of arguments should look like this:
765 .Bd -literal
766 #include <stdarg.h>
767
768 void
769 vaf(const char *fmt, ...)
770 {
771         va_list ap;
772
773         va_start(ap, fmt);
774         STUFF;
775         va_end(ap);
776         /* No return needed for void functions. */
777 }
778
779 static void
780 usage(void)
781 {
782         /* Optional blank line goes here. */
783 .Ed
784 .Pp
785 Optionally, insert a blank line at the beginning of functions with no local
786 variables.
787 Older versions of this
788 .Nm
789 document required the blank line convention, so it is widely used in existing
790 code.
791 .Pp
792 Do not insert a blank line at the beginning of functions with local variables.
793 Instead, these should have local variable declarations first, followed by one
794 blank line, followed by the first statement.
795 .Pp
796 Use
797 .Xr printf 3 ,
798 not
799 .Xr fputs 3 ,
800 .Xr puts 3 ,
801 .Xr putchar 3 ,
802 whatever; it is faster and usually cleaner, not
803 to mention avoiding stupid bugs.
804 .Pp
805 Usage statements should look like the manual pages
806 .Sx SYNOPSIS .
807 The usage statement should be structured in the following order:
808 .Bl -enum
809 .It
810 Options without operands come first,
811 in alphabetical order,
812 inside a single set of brackets
813 .Ql ( \&[
814 and
815 .Ql \&] ) .
816 .It
817 Options with operands come next,
818 also in alphabetical order,
819 with each option and its argument inside its own pair of brackets.
820 .It
821 Required arguments
822 (if any)
823 are next,
824 listed in the order they should be specified on the command line.
825 .It
826 Finally,
827 any optional arguments should be listed,
828 listed in the order they should be specified,
829 and all inside brackets.
830 .El
831 .Pp
832 A bar
833 .Pq Ql \&|
834 separates
835 .Dq either-or
836 options/arguments,
837 and multiple options/arguments which are specified together are
838 placed in a single set of brackets.
839 .Bd -literal -offset 4n
840 "usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\en"
841 "usage: f [-a | -b] [-c [-dEe] [-n number]]\en"
842 .Ed
843 .Bd -literal
844         (void)fprintf(stderr, "usage: f [-ab]\en");
845         exit(1);
846 }
847 .Ed
848 .Pp
849 Note that the manual page options description should list the options in
850 pure alphabetical order.
851 That is, without regard to whether an option takes arguments or not.
852 The alphabetical ordering should take into account the case ordering
853 shown above.
854 .Pp
855 New core kernel code should be reasonably compliant with the
856 .Nm
857 guides.
858 The guidelines for third-party maintained modules and device drivers are more
859 relaxed but at a minimum should be internally consistent with their style.
860 .Pp
861 Stylistic changes (including whitespace changes) are hard on the source
862 repository and are to be avoided without good reason.
863 Code that is approximately
864 .Fx
865 KNF
866 .Nm
867 compliant in the repository must not diverge from compliance.
868 .Pp
869 Whenever possible, code should be run through a code checker
870 (e.g., various static analyzers or
871 .Nm cc Fl Wall )
872 and produce minimal warnings.
873 .Sh FILES
874 .Bl -tag -width indent
875 .It Pa /usr/src/tools/tools/editing/freebsd.el
876 An Emacs plugin to follow the
877 .Fx
878 .Nm
879 indentation rules.
880 .It Pa /usr/src/tools/tools/editing/freebsd.vim
881 A Vim plugin to follow the
882 .Fx
883 .Nm
884 indentation rules.
885 .El
886 .Sh SEE ALSO
887 .Xr indent 1 ,
888 .Xr err 3 ,
889 .Xr warn 3 ,
890 .Xr style.Makefile 5 ,
891 .Xr style.lua 9
892 .Sh HISTORY
893 This manual page is largely based on the
894 .Pa src/admin/style/style
895 file from the
896 .Bx 4.4 Lite2
897 release, with occasional updates to reflect the current practice and
898 desire of the
899 .Fx
900 project.
901 .Pa src/admin/style/style
902 is a codification by the CSRG of the programming style of Ken Thompson and
903 Dennis Ritchie in
904 .At v6 .