style revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Style guide for BSD's KNF (Kernel Normal Form).
3 1.1 cgd *
4 1.1 cgd * from: @(#)style 1.10 (Berkeley) 2/11/92
5 1.1 cgd * $Id: style,v 1.1 1993/08/06 07:30:52 cgd Exp $
6 1.1 cgd */
7 1.1 cgd
8 1.1 cgd /*
9 1.1 cgd * VERY important single-line comments look like this.
10 1.1 cgd */
11 1.1 cgd
12 1.1 cgd /* Most single-line comments look like this. */
13 1.1 cgd
14 1.1 cgd /*
15 1.1 cgd * Multi-line comments look like this. Make them real sentences. Fill
16 1.1 cgd * them so they look like real paragraphs.
17 1.1 cgd */
18 1.1 cgd
19 1.1 cgd /* Include files go at the top of the source module. */
20 1.1 cgd #include <stdio.h> /* Non-local includes in brackets. */
21 1.1 cgd
22 1.1 cgd /*
23 1.1 cgd * Global pathnames are defined in /usr/include/paths.h. Pathnames local
24 1.1 cgd * to the program go in pathnames.h in the local directory.
25 1.1 cgd */
26 1.1 cgd #include <paths.h> /* Non-local includes in brackets. */
27 1.1 cgd #include "pathnames.h" /* Local includes in quotes. */
28 1.1 cgd
29 1.1 cgd /*
30 1.1 cgd * All ANSI function decls go at the top of the source module. Use the
31 1.1 cgd * __P macro from include file <sys/cdefs.h>. Only the kernel has a name
32 1.1 cgd * associated with the types, i.e. in the kernel use:
33 1.1 cgd *
34 1.1 cgd * void function __P((int a));
35 1.1 cgd *
36 1.1 cgd * in user land use:
37 1.1 cgd *
38 1.1 cgd * void function __P((int));
39 1.1 cgd */
40 1.1 cgd void function __P((int, const char *));
41 1.1 cgd
42 1.1 cgd /*
43 1.1 cgd * Macros are capitalized, parenthesized, and should avoid side-effects.
44 1.1 cgd * If they are an inline expansion of a function, the function is defined
45 1.1 cgd * all in lowercase, the macro has the same name all in uppercase. If the
46 1.1 cgd * macro needs more than a single line, use braces. Put a space before
47 1.1 cgd * the backslashes.
48 1.1 cgd */
49 1.1 cgd #define MACRO(x, y) { \
50 1.1 cgd variable = (x) + (y); \
51 1.1 cgd line two; \
52 1.1 cgd }
53 1.1 cgd
54 1.1 cgd /* Enum types are capitalized. */
55 1.1 cgd enum enumtype { ONE, TWO } et;
56 1.1 cgd
57 1.1 cgd /*
58 1.1 cgd * When declaring variables in structures, declare them sorted by use, then
59 1.1 cgd * by size, and then by alphabetical order. The first category normally
60 1.1 cgd * doesn't apply, but there are exceptions. Each one gets its own line.
61 1.1 cgd * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
62 1.1 cgd *
63 1.1 cgd * Major structures should be declared at the top of the file they are
64 1.1 cgd * used in, or in separate header files, if they are used in multiple
65 1.1 cgd * source files. Use of the structures should be by separate declarations
66 1.1 cgd * and should be "extern" if they are declared in a header file.
67 1.1 cgd */
68 1.1 cgd struct foo {
69 1.1 cgd struct foo *next; /* List of active foo */
70 1.1 cgd struct mumble amumble; /* Comment for mumble */
71 1.1 cgd int bar;
72 1.1 cgd };
73 1.1 cgd struct foo *foohead; /* Head of global foo list */
74 1.1 cgd
75 1.1 cgd /*
76 1.1 cgd * All major routines should have a comment briefly describing what
77 1.1 cgd * they do. The comment before the "main" routine should describe
78 1.1 cgd * what the program does.
79 1.1 cgd */
80 1.1 cgd main(argc, argv)
81 1.1 cgd int argc;
82 1.1 cgd char *argv[];
83 1.1 cgd {
84 1.1 cgd extern char *optarg;
85 1.1 cgd extern int optind;
86 1.1 cgd long num;
87 1.1 cgd int ch;
88 1.1 cgd char *ep;
89 1.1 cgd
90 1.1 cgd /*
91 1.1 cgd * For consistency, getopt should be used to parse options.
92 1.1 cgd * Options should be sorted in the getopt call and the switch
93 1.1 cgd * statement, unless they fall through. Elements in a switch
94 1.1 cgd * statement that fall through should have a FALLTHROUGH comment.
95 1.1 cgd * Numerical arguments should be checked for accuracy.
96 1.1 cgd */
97 1.1 cgd while ((ch = getopt(argc, argv, "abn")) != EOF)
98 1.1 cgd switch (ch) { /* Indent the switch. */
99 1.1 cgd case 'a': /* Don't indent the case. */
100 1.1 cgd aflag = 1;
101 1.1 cgd /* FALLTHROUGH */
102 1.1 cgd case 'b':
103 1.1 cgd bflag = 1;
104 1.1 cgd break;
105 1.1 cgd case 'n':
106 1.1 cgd num = strtol(optarg, &ep, 10);
107 1.1 cgd if (num <= 0 || *ep)
108 1.1 cgd err("illegal number -- %s", optarg);
109 1.1 cgd break;
110 1.1 cgd case '?':
111 1.1 cgd default:
112 1.1 cgd usage();
113 1.1 cgd }
114 1.1 cgd argc -= optind;
115 1.1 cgd argv += optind;
116 1.1 cgd
117 1.1 cgd /*
118 1.1 cgd * Space after keywords (while, for, return, switch). No braces are
119 1.1 cgd * used for single statement block.
120 1.1 cgd *
121 1.1 cgd * Forever loops are done with for's, not while's.
122 1.1 cgd */
123 1.1 cgd for (;;)
124 1.1 cgd stmt;
125 1.1 cgd
126 1.1 cgd /*
127 1.1 cgd * Parts of a for loop may be left empty. Avoid declarations in
128 1.1 cgd * blocks unless the routine is unusually complicated.
129 1.1 cgd */
130 1.1 cgd for (; cnt < 15; cnt++) {
131 1.1 cgd stmt1;
132 1.1 cgd stmt2;
133 1.1 cgd }
134 1.1 cgd
135 1.1 cgd while (cnt < 20) {
136 1.1 cgd stmt1; /* Second level indents are four spaces. */
137 1.1 cgd z = a + really + long + statment + that + needs + two lines +
138 1.1 cgd gets + indented + four + spaces + on + the + second +
139 1.1 cgd and + subsequent + lines.
140 1.1 cgd }
141 1.1 cgd
142 1.1 cgd /*
143 1.1 cgd * Try to put shorter part first. The closing and opening braces
144 1.1 cgd * go on the same line as the else.
145 1.1 cgd */
146 1.1 cgd if (test)
147 1.1 cgd stmt;
148 1.1 cgd else if (bar) {
149 1.1 cgd stmt;
150 1.1 cgd stmt;
151 1.1 cgd } else
152 1.1 cgd stmt;
153 1.1 cgd
154 1.1 cgd /* No space after function names. */
155 1.1 cgd if (error = function(a1, a2))
156 1.1 cgd exit(error);
157 1.1 cgd
158 1.1 cgd /*
159 1.1 cgd * Unary operators do not require spaces, binary operators do.
160 1.1 cgd * Try not to use too many parenthesis unless the statement is
161 1.1 cgd * really confusing without them.
162 1.1 cgd */
163 1.1 cgd a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
164 1.1 cgd k = l & FLAGS;
165 1.1 cgd
166 1.1 cgd /*
167 1.1 cgd * Exits should be 0 on success, and 1 on failure. Don't denote
168 1.1 cgd * all the possible exit points, using the integers 1 through 300.
169 1.1 cgd */
170 1.1 cgd exit(0); /* Avoid obvious comments such as "Exit 0 on success." */
171 1.1 cgd }
172 1.1 cgd
173 1.1 cgd /*
174 1.1 cgd * If a function type is declared, it should be on a line
175 1.1 cgd * by itself preceeding the function.
176 1.1 cgd */
177 1.1 cgd static char *
178 1.1 cgd function(a1, a2, a3, a4)
179 1.1 cgd int a1, a2, a4; /* Declare ints too. */
180 1.1 cgd float a3; /* List in order declared, as much as possible. */
181 1.1 cgd {
182 1.1 cgd /*
183 1.1 cgd * When declaring variables in functions declare them sorted by size,
184 1.1 cgd * then in alphabetical order; multiple ones per line are okay. Old
185 1.1 cgd * style function declarations can go on the same line. ANSI style
186 1.1 cgd * function declarations should go in the include file "externs.h".
187 1.1 cgd * If a line overflows reuse the type keyword.
188 1.1 cgd *
189 1.1 cgd * Try not to initialize variables in the declarations.
190 1.1 cgd */
191 1.1 cgd extern u_char one;
192 1.1 cgd extern char two;
193 1.1 cgd struct foo three, *four;
194 1.1 cgd double five;
195 1.1 cgd int *six, seven, eight();
196 1.1 cgd char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen;
197 1.1 cgd char *overflow __P((void));
198 1.1 cgd void *mymalloc __P((u_int));
199 1.1 cgd
200 1.1 cgd /*
201 1.1 cgd * Casts and sizeof's are not followed by a space. NULL is any
202 1.1 cgd * pointer type, and doesn't need to be cast, so use NULL instead
203 1.1 cgd * of (struct foo *)0 or (struct foo *)NULL. Also, test pointers
204 1.1 cgd * against NULL, i.e. use:
205 1.1 cgd *
206 1.1 cgd * (p = f()) == NULL
207 1.1 cgd * not:
208 1.1 cgd * !(p = f())
209 1.1 cgd *
210 1.1 cgd * Routines returning void * should not have their return values cast
211 1.1 cgd * to any pointer type.
212 1.1 cgd */
213 1.1 cgd if ((four = malloc(sizeof(struct foo))) == NULL)
214 1.1 cgd return (NULL);
215 1.1 cgd if ((six = (int *)overflow()) == NULL)
216 1.1 cgd return (NULL);
217 1.1 cgd return (eight);
218 1.1 cgd }
219 1.1 cgd
220 1.1 cgd /* ANSI function braces look like regular function braces. */
221 1.1 cgd function(int a1, int a2)
222 1.1 cgd {
223 1.1 cgd ...
224 1.1 cgd }
225 1.1 cgd
226 1.1 cgd static void
227 1.1 cgd usage()
228 1.1 cgd { /* Insert an empty line if the function has no local variables. */
229 1.1 cgd
230 1.1 cgd /*
231 1.1 cgd * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
232 1.1 cgd * usually cleaner, not to mention avoiding stupid bugs.
233 1.1 cgd *
234 1.1 cgd * Usage statements should look like the manual pages. Options w/o
235 1.1 cgd * operands come first, in alphabetical order inside a single set of
236 1.1 cgd * braces. Followed by options with operands, in alphabetical order,
237 1.1 cgd * each in braces. Followed by required arguments in the order they
238 1.1 cgd * are specified, followed by optional arguments in the order they
239 1.1 cgd * are specified. A bar ('|') separates either/or options/arguments,
240 1.1 cgd * and multiple options/arguments which are specified together are
241 1.1 cgd * placed in a single set of braces.
242 1.1 cgd *
243 1.1 cgd * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
244 1.1 cgd * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
245 1.1 cgd */
246 1.1 cgd (void)fprintf(stderr, "usage: f [-ab]\n");
247 1.1 cgd exit(1);
248 1.1 cgd }
249