]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - share/man/man9/style.9
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 7, 2010
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 Enumeration values are all uppercase.
292 .Bd -literal
293 enum enumtype { ONE, TWO } et;
294 .Ed
295 .Pp
296 The use of internal_underscores in identifiers is preferred over
297 camelCase or TitleCase.
298 .Pp
299 In declarations, do not put any whitespace between asterisks and
300 adjacent tokens, except for tokens that are identifiers related to
301 types.
302 (These identifiers are the names of basic types, type
303 qualifiers, and
304 .Ic typedef Ns -names
305 other than the one being declared.)
306 Separate these identifiers from asterisks using a single space.
307 .Pp
308 When declaring variables in structures, declare them sorted by use, then
309 by size (largest to smallest), and then in alphabetical order.
310 The first category normally does not apply, but there are exceptions.
311 Each one gets its own line.
312 Try to make the structure
313 readable by aligning the member names using either one or two tabs
314 depending upon your judgment.
315 You should use one tab only if it suffices to align at least 90% of
316 the member names.
317 Names following extremely long types
318 should be separated by a single space.
319 .Pp
320 Major structures should be declared at the top of the file in which they
321 are used, or in separate header files if they are used in multiple
322 source files.
323 Use of the structures should be by separate declarations
324 and should be
325 .Ic extern
326 if they are declared in a header file.
327 .Bd -literal
328 struct foo {
329         struct foo      *next;          /* List of active foo. */
330         struct mumble   amumble;        /* Comment for mumble. */
331         int             bar;            /* Try to align the comments. */
332         struct verylongtypename *baz;   /* Won't fit in 2 tabs. */
333 };
334 struct foo *foohead;                    /* Head of global foo list. */
335 .Ed
336 .Pp
337 Use
338 .Xr queue 3
339 macros rather than rolling your own lists, whenever possible.
340 Thus,
341 the previous example would be better written:
342 .Bd -literal
343 #include <sys/queue.h>
344
345 struct foo {
346         LIST_ENTRY(foo) link;           /* Use queue macros for foo lists. */
347         struct mumble   amumble;        /* Comment for mumble. */
348         int             bar;            /* Try to align the comments. */
349         struct verylongtypename *baz;   /* Won't fit in 2 tabs. */
350 };
351 LIST_HEAD(, foo) foohead;               /* Head of global foo list. */
352 .Ed
353 .Pp
354 Avoid using typedefs for structure types.
355 Typedefs are problematic because they do not properly hide their
356 underlying type; for example you need to know if the typedef is
357 the structure itself or a pointer to the structure.
358 In addition they must be declared exactly once, whereas an
359 incomplete structure type can be mentioned as many times as
360 necessary.
361 Typedefs are difficult to use in stand-alone header files:
362 the header that defines the typedef must be included
363 before the header that uses it, or by the header that uses
364 it (which causes namespace pollution), or there must be a
365 back-door mechanism for obtaining the typedef.
366 .Pp
367 When convention requires a
368 .Ic typedef ,
369 make its name match the struct tag.
370 Avoid typedefs ending in
371 .Dq Li _t ,
372 except as specified in Standard C or by
373 .Tn POSIX .
374 .Bd -literal
375 /* Make the structure name match the typedef. */
376 typedef struct bar {
377         int     level;
378 } BAR;
379 typedef int             foo;            /* This is foo. */
380 typedef const long      baz;            /* This is baz. */
381 .Ed
382 .Pp
383 All functions are prototyped somewhere.
384 .Pp
385 Function prototypes for private functions (i.e., functions not used
386 elsewhere) go at the top of the first source module.
387 Functions
388 local to one source module should be declared
389 .Ic static .
390 .Pp
391 Functions used from other parts of the kernel are prototyped in the
392 relevant include file.
393 Function prototypes should be listed in a logical order, preferably
394 alphabetical unless there is a compelling reason to use a different
395 ordering.
396 .Pp
397 Functions that are used locally in more than one module go into a
398 separate header file, e.g.\&
399 .Qq Pa extern.h .
400 .Pp
401 Do not use the
402 .Dv __P
403 macro.
404 .Pp
405 In general code can be considered
406 .Dq "new code"
407 when it makes up about 50% or more of the file(s) involved.
408 This is enough
409 to break precedents in the existing code and use the current
410 .Nm
411 guidelines.
412 .Pp
413 The kernel has a name associated with parameter types, e.g., in the kernel
414 use:
415 .Bd -literal
416 void    function(int fd);
417 .Ed
418 .Pp
419 In header files visible to userland applications, prototypes that are
420 visible must use either
421 .Dq protected
422 names (ones beginning with an underscore)
423 or no names with the types.
424 It is preferable to use protected names.
425 E.g., use:
426 .Bd -literal
427 void    function(int);
428 .Ed
429 .Pp
430 or:
431 .Bd -literal
432 void    function(int _fd);
433 .Ed
434 .Pp
435 Prototypes may have an extra space after a tab to enable function names
436 to line up:
437 .Bd -literal
438 static char     *function(int _arg, const char *_arg2, struct foo *_arg3,
439                     struct bar *_arg4);
440 static void      usage(void);
441
442 /*
443  * All major routines should have a comment briefly describing what
444  * they do.  The comment before the "main" routine should describe
445  * what the program does.
446  */
447 int
448 main(int argc, char *argv[])
449 {
450         char *ep;
451         long num;
452         int ch;
453 .Ed
454 .Pp
455 For consistency,
456 .Xr getopt 3
457 should be used to parse options.
458 Options
459 should be sorted in the
460 .Xr getopt 3
461 call and the
462 .Ic switch
463 statement, unless
464 parts of the
465 .Ic switch
466 cascade.
467 Elements in a
468 .Ic switch
469 statement that cascade should have a
470 .Li FALLTHROUGH
471 comment.
472 Numerical arguments should be checked for accuracy.
473 Code which is unreachable for non-obvious reasons may be marked /*
474 .Li NOTREACHED
475 */.
476 .Bd -literal
477         while ((ch = getopt(argc, argv, "abNn:")) != -1)
478                 switch (ch) {           /* Indent the switch. */
479                 case 'a':               /* Don't indent the case. */
480                         aflag = 1;      /* Indent case body one tab. */
481                         /* FALLTHROUGH */
482                 case 'b':
483                         bflag = 1;
484                         break;
485                 case 'N':
486                         Nflag = 1;
487                         break;
488                 case 'n':
489                         num = strtol(optarg, &ep, 10);
490                         if (num <= 0 || *ep != '\e0') {
491                                 warnx("illegal number, -n argument -- %s",
492                                     optarg);
493                                 usage();
494                         }
495                         break;
496                 case '?':
497                 default:
498                         usage();
499                 }
500         argc -= optind;
501         argv += optind;
502 .Ed
503 .Pp
504 Space after keywords
505 .Pq Ic if , while , for , return , switch .
506 No braces
507 .Ql ( \&{
508 and
509 .Ql \&} )
510 are
511 used for control statements with zero or only a single statement unless that
512 statement is more than a single line in which case they are permitted.
513 Forever loops are done with
514 .Ic for Ns 's ,
515 not
516 .Ic while Ns 's .
517 .Bd -literal
518         for (p = buf; *p != '\e0'; ++p)
519                 ;       /* nothing */
520         for (;;)
521                 stmt;
522         for (;;) {
523                 z = a + really + long + statement + that + needs +
524                     two + lines + gets + indented + four + spaces +
525                     on + the + second + and + subsequent + lines;
526         }
527         for (;;) {
528                 if (cond)
529                         stmt;
530         }
531         if (val != NULL)
532                 val = realloc(val, newsize);
533 .Ed
534 .Pp
535 Parts of a
536 .Ic for
537 loop may be left empty.
538 Do not put declarations
539 inside blocks unless the routine is unusually complicated.
540 .Bd -literal
541         for (; cnt < 15; cnt++) {
542                 stmt1;
543                 stmt2;
544         }
545 .Ed
546 .Pp
547 Indentation is an 8 character tab.
548 Second level indents are four spaces.
549 If you have to wrap a long statement, put the operator at the end of the
550 line.
551 .Bd -literal
552         while (cnt < 20 && this_variable_name_is_too_long &&
553             ep != NULL)
554                 z = a + really + long + statement + that + needs +
555                     two + lines + gets + indented + four + spaces +
556                     on + the + second + and + subsequent + lines;
557 .Ed
558 .Pp
559 Do not add whitespace at the end of a line, and only use tabs
560 followed by spaces
561 to form the indentation.
562 Do not use more spaces than a tab will produce
563 and do not use spaces in front of tabs.
564 .Pp
565 Closing and opening braces go on the same line as the
566 .Ic else .
567 Braces that are not necessary may be left out.
568 .Bd -literal
569         if (test)
570                 stmt;
571         else if (bar) {
572                 stmt;
573                 stmt;
574         } else
575                 stmt;
576 .Ed
577 .Pp
578 No spaces after function names.
579 Commas have a space after them.
580 No spaces
581 after
582 .Ql \&(
583 or
584 .Ql \&[
585 or preceding
586 .Ql \&]
587 or
588 .Ql \&)
589 characters.
590 .Bd -literal
591         error = function(a1, a2);
592         if (error != 0)
593                 exit(error);
594 .Ed
595 .Pp
596 Unary operators do not require spaces, binary operators do.
597 Do not use parentheses unless they are required for precedence or unless the
598 statement is confusing without them.
599 Remember that other people may
600 confuse easier than you.
601 Do YOU understand the following?
602 .Bd -literal
603         a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
604         k = !(l & FLAGS);
605 .Ed
606 .Pp
607 Exits should be 0 on success, or 1 on failure.
608 .Bd -literal
609         exit(0);        /*
610                          * Avoid obvious comments such as
611                          * "Exit 0 on success."
612                          */
613 }
614 .Ed
615 .Pp
616 The function type should be on a line by itself
617 preceding the function.
618 The opening brace of the function body should be
619 on a line by itself.
620 .Bd -literal
621 static char *
622 function(int a1, int a2, float fl, int a4)
623 {
624 .Ed
625 .Pp
626 When declaring variables in functions declare them sorted by size,
627 then in alphabetical order; multiple ones per line are okay.
628 If a line overflows reuse the type keyword.
629 .Pp
630 Be careful to not obfuscate the code by initializing variables in
631 the declarations.
632 Use this feature only thoughtfully.
633 DO NOT use function calls in initializers.
634 .Bd -literal
635         struct foo one, *two;
636         double three;
637         int *four, five;
638         char *six, seven, eight, nine, ten, eleven, twelve;
639
640         four = myfunction();
641 .Ed
642 .Pp
643 Do not declare functions inside other functions; ANSI C says that
644 such declarations have file scope regardless of the nesting of the
645 declaration.
646 Hiding file declarations in what appears to be a local
647 scope is undesirable and will elicit complaints from a good compiler.
648 .Pp
649 Casts and
650 .Ic sizeof Ns 's
651 are not followed by a space.
652 Note that
653 .Xr indent 1
654 does not understand this rule.
655 .Ic sizeof Ns 's
656 are written with parenthesis always.
657 The redundant parenthesis rules do not apply to
658 .Fn sizeof var
659 instances.
660 .Pp
661 .Dv NULL
662 is the preferred null pointer constant.
663 Use
664 .Dv NULL
665 instead of
666 .Vt ( "type *" ) Ns 0
667 or
668 .Vt ( "type *" ) Ns Dv NULL
669 in contexts where the compiler knows the
670 type, e.g., in assignments.
671 Use
672 .Vt ( "type *" ) Ns Dv NULL
673 in other contexts,
674 in particular for all function args.
675 (Casting is essential for
676 variadic args and is necessary for other args if the function prototype
677 might not be in scope.)
678 Test pointers against
679 .Dv NULL ,
680 e.g., use:
681 .Bd -literal
682 (p = f()) == NULL
683 .Ed
684 .Pp
685 not:
686 .Bd -literal
687 !(p = f())
688 .Ed
689 .Pp
690 Do not use
691 .Ic \&!
692 for tests unless it is a boolean, e.g.\& use:
693 .Bd -literal
694 if (*p == '\e0')
695 .Ed
696 .Pp
697 not:
698 .Bd -literal
699 if (!*p)
700 .Ed
701 .Pp
702 Routines returning
703 .Vt "void *"
704 should not have their return values cast
705 to any pointer type.
706 .Pp
707 Values in
708 .Ic return
709 statements should be enclosed in parentheses.
710 .Pp
711 Use
712 .Xr err 3
713 or
714 .Xr warn 3 ,
715 do not roll your own.
716 .Bd -literal
717         if ((four = malloc(sizeof(struct foo))) == NULL)
718                 err(1, (char *)NULL);
719         if ((six = (int *)overflow()) == NULL)
720                 errx(1, "number overflowed");
721         return (eight);
722 }
723 .Ed
724 .Pp
725 Old-style function declarations look like this:
726 .Bd -literal
727 static char *
728 function(a1, a2, fl, a4)
729         int a1, a2;     /* Declare ints, too, don't default them. */
730         float fl;       /* Beware double vs. float prototype differences. */
731         int a4;         /* List in order declared. */
732 {
733 .Ed
734 .Pp
735 Use ANSI function declarations unless you explicitly need K&R compatibility.
736 Long parameter lists are wrapped with a normal four space indent.
737 .Pp
738 Variable numbers of arguments should look like this:
739 .Bd -literal
740 #include <stdarg.h>
741
742 void
743 vaf(const char *fmt, ...)
744 {
745         va_list ap;
746
747         va_start(ap, fmt);
748         STUFF;
749         va_end(ap);
750         /* No return needed for void functions. */
751 }
752
753 static void
754 usage()
755 {
756         /* Insert an empty line if the function has no local variables. */
757 .Ed
758 .Pp
759 Use
760 .Xr printf 3 ,
761 not
762 .Xr fputs 3 ,
763 .Xr puts 3 ,
764 .Xr putchar 3 ,
765 whatever; it is faster and usually cleaner, not
766 to mention avoiding stupid bugs.
767 .Pp
768 Usage statements should look like the manual pages
769 .Sx SYNOPSIS .
770 The usage statement should be structured in the following order:
771 .Bl -enum
772 .It
773 Options without operands come first,
774 in alphabetical order,
775 inside a single set of brackets
776 .Ql ( \&[
777 and
778 .Ql \&] ) .
779 .It
780 Options with operands come next,
781 also in alphabetical order,
782 with each option and its argument inside its own pair of brackets.
783 .It
784 Required arguments
785 (if any)
786 are next,
787 listed in the order they should be specified on the command line.
788 .It
789 Finally,
790 any optional arguments should be listed,
791 listed in the order they should be specified,
792 and all inside brackets.
793 .El
794 .Pp
795 A bar
796 .Pq Ql \&|
797 separates
798 .Dq either-or
799 options/arguments,
800 and multiple options/arguments which are specified together are
801 placed in a single set of brackets.
802 .Bd -literal -offset 4n
803 "usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\en"
804 "usage: f [-a | -b] [-c [-dEe] [-n number]]\en"
805 .Ed
806 .Bd -literal
807         (void)fprintf(stderr, "usage: f [-ab]\en");
808         exit(1);
809 }
810 .Ed
811 .Pp
812 Note that the manual page options description should list the options in
813 pure alphabetical order.
814 That is, without regard to whether an option takes arguments or not.
815 The alphabetical ordering should take into account the case ordering
816 shown above.
817 .Pp
818 New core kernel code should be reasonably compliant with the
819 .Nm
820 guides.
821 The guidelines for third-party maintained modules and device drivers are more
822 relaxed but at a minimum should be internally consistent with their style.
823 .Pp
824 Stylistic changes (including whitespace changes) are hard on the source
825 repository and are to be avoided without good reason.
826 Code that is approximately
827 .Fx
828 KNF
829 .Nm
830 compliant in the repository must not diverge from compliance.
831 .Pp
832 Whenever possible, code should be run through a code checker
833 (e.g.,
834 .Xr lint 1
835 or
836 .Nm gcc Fl Wall )
837 and produce minimal warnings.
838 .Sh SEE ALSO
839 .Xr indent 1 ,
840 .Xr lint 1 ,
841 .Xr err 3 ,
842 .Xr warn 3 ,
843 .Xr style.Makefile 5
844 .Sh HISTORY
845 This manual page is largely based on the
846 .Pa src/admin/style/style
847 file from the
848 .Bx 4.4 Lite2
849 release, with occasional updates to reflect the current practice and
850 desire of the
851 .Fx
852 project.
853 .Pa src/admin/style/style
854 is a codification by the CSRG of the programming style of Ken Thompson and
855 Dennis Ritchie in
856 .At v6 .