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