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