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