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