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