style revision 1.9 1 1.9 lukem /* $NetBSD: style,v 1.9 1999/01/20 23:13:54 lukem Exp $ */
2 1.6 thorpej
3 1.1 cgd /*
4 1.2 cgd * Style guide for the 4BSD KNF (Kernel Normal Form).
5 1.1 cgd *
6 1.2 cgd * from: @(#)style 1.12 (Berkeley) 3/18/94
7 1.1 cgd */
8 1.1 cgd
9 1.1 cgd /*
10 1.1 cgd * VERY important single-line comments look like this.
11 1.1 cgd */
12 1.1 cgd
13 1.1 cgd /* Most single-line comments look like this. */
14 1.1 cgd
15 1.1 cgd /*
16 1.1 cgd * Multi-line comments look like this. Make them real sentences. Fill
17 1.1 cgd * them so they look like real paragraphs.
18 1.1 cgd */
19 1.1 cgd
20 1.2 cgd /*
21 1.2 cgd * Kernel include files come first; normally, you'll need <sys/types.h>
22 1.2 cgd * OR <sys/param.h>, but not both! <sys/types.h> includes <sys/cdefs.h>,
23 1.2 cgd * and it's okay to depend on that.
24 1.2 cgd */
25 1.2 cgd #include <sys/types.h> /* Non-local includes in brackets. */
26 1.2 cgd
27 1.2 cgd /* If it's a network program, put the network include files next. */
28 1.2 cgd #include <net/if.h>
29 1.2 cgd #include <net/if_dl.h>
30 1.2 cgd #include <net/route.h>
31 1.2 cgd #include <netinet/in.h>
32 1.2 cgd #include <protocols/rwhod.h>
33 1.2 cgd
34 1.2 cgd /*
35 1.2 cgd * Then there's a blank line, followed by the /usr include files.
36 1.2 cgd * The /usr include files should be sorted!
37 1.2 cgd */
38 1.2 cgd #include <stdio.h>
39 1.1 cgd
40 1.1 cgd /*
41 1.1 cgd * Global pathnames are defined in /usr/include/paths.h. Pathnames local
42 1.1 cgd * to the program go in pathnames.h in the local directory.
43 1.1 cgd */
44 1.2 cgd #include <paths.h>
45 1.2 cgd
46 1.2 cgd /* Then, there's a blank line, and the user include files. */
47 1.2 cgd #include "pathnames.h" /* Local includes in double quotes. */
48 1.1 cgd
49 1.1 cgd /*
50 1.2 cgd * ANSI function declarations for private functions (i.e. functions not used
51 1.2 cgd * elsewhere) go at the top of the source module. Use the __P macro from
52 1.2 cgd * the include file <sys/cdefs.h>. Only the kernel has a name associated with
53 1.2 cgd * the types, i.e. in the kernel use:
54 1.1 cgd *
55 1.1 cgd * void function __P((int a));
56 1.1 cgd *
57 1.1 cgd * in user land use:
58 1.1 cgd *
59 1.1 cgd * void function __P((int));
60 1.1 cgd */
61 1.2 cgd static char *function __P((int, const char *));
62 1.2 cgd static void usage __P((void));
63 1.1 cgd
64 1.1 cgd /*
65 1.1 cgd * Macros are capitalized, parenthesized, and should avoid side-effects.
66 1.1 cgd * If they are an inline expansion of a function, the function is defined
67 1.1 cgd * all in lowercase, the macro has the same name all in uppercase. If the
68 1.2 cgd * macro needs more than a single line, use braces. Right-justify the
69 1.2 cgd * backslashes, it makes it easier to read.
70 1.1 cgd */
71 1.2 cgd #define MACRO(x, y) { \
72 1.2 cgd variable = (x) + (y); \
73 1.2 cgd (y) += 2; \
74 1.1 cgd }
75 1.1 cgd
76 1.1 cgd /* Enum types are capitalized. */
77 1.1 cgd enum enumtype { ONE, TWO } et;
78 1.1 cgd
79 1.1 cgd /*
80 1.1 cgd * When declaring variables in structures, declare them sorted by use, then
81 1.1 cgd * by size, and then by alphabetical order. The first category normally
82 1.1 cgd * doesn't apply, but there are exceptions. Each one gets its own line.
83 1.1 cgd * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
84 1.1 cgd *
85 1.2 cgd * Major structures should be declared at the top of the file in which they
86 1.2 cgd * are used, or in separate header files, if they are used in multiple
87 1.2 cgd * source files. Use of the structures should be by separate declarations
88 1.1 cgd * and should be "extern" if they are declared in a header file.
89 1.1 cgd */
90 1.1 cgd struct foo {
91 1.1 cgd struct foo *next; /* List of active foo */
92 1.1 cgd struct mumble amumble; /* Comment for mumble */
93 1.1 cgd int bar;
94 1.1 cgd };
95 1.1 cgd struct foo *foohead; /* Head of global foo list */
96 1.2 cgd
97 1.2 cgd /* Make the structure name match the typedef. */
98 1.2 cgd typedef struct _bar {
99 1.2 cgd int level;
100 1.2 cgd } BAR;
101 1.1 cgd
102 1.1 cgd /*
103 1.1 cgd * All major routines should have a comment briefly describing what
104 1.2 cgd * they do. The comment before the "main" routine should describe
105 1.1 cgd * what the program does.
106 1.1 cgd */
107 1.2 cgd int
108 1.1 cgd main(argc, argv)
109 1.1 cgd int argc;
110 1.1 cgd char *argv[];
111 1.1 cgd {
112 1.1 cgd extern char *optarg;
113 1.1 cgd extern int optind;
114 1.1 cgd long num;
115 1.1 cgd int ch;
116 1.1 cgd char *ep;
117 1.1 cgd
118 1.1 cgd /*
119 1.2 cgd * For consistency, getopt should be used to parse options. Options
120 1.2 cgd * should be sorted in the getopt call and the switch statement, unless
121 1.2 cgd * parts of the switch cascade. Elements in a switch statement that
122 1.2 cgd * cascade should have a FALLTHROUGH comment. Numerical arguments
123 1.2 cgd * should be checked for accuracy. Code that cannot be reached should
124 1.2 cgd * have a NOTREACHED comment.
125 1.1 cgd */
126 1.5 scottr while ((ch = getopt(argc, argv, "abn")) != -1)
127 1.1 cgd switch (ch) { /* Indent the switch. */
128 1.1 cgd case 'a': /* Don't indent the case. */
129 1.1 cgd aflag = 1;
130 1.1 cgd /* FALLTHROUGH */
131 1.1 cgd case 'b':
132 1.1 cgd bflag = 1;
133 1.1 cgd break;
134 1.1 cgd case 'n':
135 1.1 cgd num = strtol(optarg, &ep, 10);
136 1.2 cgd if (num <= 0 || *ep != '\0')
137 1.1 cgd err("illegal number -- %s", optarg);
138 1.1 cgd break;
139 1.1 cgd case '?':
140 1.1 cgd default:
141 1.1 cgd usage();
142 1.2 cgd /* NOTREACHED */
143 1.1 cgd }
144 1.1 cgd argc -= optind;
145 1.1 cgd argv += optind;
146 1.1 cgd
147 1.1 cgd /*
148 1.1 cgd * Space after keywords (while, for, return, switch). No braces are
149 1.2 cgd * used for control statements with zero or only a single statement.
150 1.1 cgd *
151 1.1 cgd * Forever loops are done with for's, not while's.
152 1.1 cgd */
153 1.2 cgd for (p = buf; *p != '\0'; ++p);
154 1.1 cgd for (;;)
155 1.1 cgd stmt;
156 1.1 cgd
157 1.1 cgd /*
158 1.2 cgd * Parts of a for loop may be left empty. Don't put declarations
159 1.2 cgd * inside blocks unless the routine is unusually complicated.
160 1.1 cgd */
161 1.1 cgd for (; cnt < 15; cnt++) {
162 1.1 cgd stmt1;
163 1.1 cgd stmt2;
164 1.1 cgd }
165 1.1 cgd
166 1.2 cgd /* Second level indents are four spaces. */
167 1.2 cgd while (cnt < 20)
168 1.1 cgd z = a + really + long + statment + that + needs + two lines +
169 1.1 cgd gets + indented + four + spaces + on + the + second +
170 1.7 enami and + subsequent + lines;
171 1.1 cgd
172 1.1 cgd /*
173 1.2 cgd * Closing and opening braces go on the same line as the else.
174 1.2 cgd * Don't add braces that aren't necessary.
175 1.1 cgd */
176 1.1 cgd if (test)
177 1.1 cgd stmt;
178 1.1 cgd else if (bar) {
179 1.1 cgd stmt;
180 1.1 cgd stmt;
181 1.1 cgd } else
182 1.1 cgd stmt;
183 1.1 cgd
184 1.2 cgd /* No spaces after function names. */
185 1.1 cgd if (error = function(a1, a2))
186 1.1 cgd exit(error);
187 1.1 cgd
188 1.1 cgd /*
189 1.2 cgd * Unary operators don't require spaces, binary operators do. Don't
190 1.2 cgd * use parenthesis unless they're required for precedence, or the
191 1.9 lukem * statement is really confusing without them, such as:
192 1.9 lukem * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
193 1.1 cgd */
194 1.9 lukem a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1);
195 1.2 cgd k = !(l & FLAGS);
196 1.1 cgd
197 1.1 cgd /*
198 1.1 cgd * Exits should be 0 on success, and 1 on failure. Don't denote
199 1.1 cgd * all the possible exit points, using the integers 1 through 300.
200 1.1 cgd */
201 1.1 cgd exit(0); /* Avoid obvious comments such as "Exit 0 on success." */
202 1.1 cgd }
203 1.1 cgd
204 1.1 cgd /*
205 1.8 simonb * The function type must be declared on a line by itself
206 1.8 simonb * preceeding the function.
207 1.1 cgd */
208 1.1 cgd static char *
209 1.2 cgd function(a1, a2, fl, a4)
210 1.2 cgd int a1, a2, a4; /* Declare ints, too, don't default them. */
211 1.2 cgd float fl; /* List in order declared, as much as possible. */
212 1.1 cgd {
213 1.1 cgd /*
214 1.1 cgd * When declaring variables in functions declare them sorted by size,
215 1.1 cgd * then in alphabetical order; multiple ones per line are okay. Old
216 1.1 cgd * style function declarations can go on the same line. ANSI style
217 1.3 cgd * function declarations should go in the include file "extern.h".
218 1.1 cgd * If a line overflows reuse the type keyword.
219 1.1 cgd *
220 1.2 cgd * DO NOT initialize variables in the declarations.
221 1.1 cgd */
222 1.1 cgd extern u_char one;
223 1.1 cgd extern char two;
224 1.1 cgd struct foo three, *four;
225 1.1 cgd double five;
226 1.1 cgd int *six, seven, eight();
227 1.1 cgd char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen;
228 1.1 cgd char *overflow __P((void));
229 1.1 cgd void *mymalloc __P((u_int));
230 1.1 cgd
231 1.1 cgd /*
232 1.1 cgd * Casts and sizeof's are not followed by a space. NULL is any
233 1.1 cgd * pointer type, and doesn't need to be cast, so use NULL instead
234 1.1 cgd * of (struct foo *)0 or (struct foo *)NULL. Also, test pointers
235 1.1 cgd * against NULL, i.e. use:
236 1.1 cgd *
237 1.1 cgd * (p = f()) == NULL
238 1.1 cgd * not:
239 1.1 cgd * !(p = f())
240 1.2 cgd *
241 1.2 cgd * Don't use '!' for tests unless it's a boolean, e.g. use
242 1.2 cgd * "if (*p == '\0')", not "if (!*p)".
243 1.1 cgd *
244 1.1 cgd * Routines returning void * should not have their return values cast
245 1.1 cgd * to any pointer type.
246 1.2 cgd *
247 1.2 cgd * Use err/warn(3), don't roll your own!
248 1.1 cgd */
249 1.1 cgd if ((four = malloc(sizeof(struct foo))) == NULL)
250 1.2 cgd err(1, NULL);
251 1.1 cgd if ((six = (int *)overflow()) == NULL)
252 1.2 cgd errx(1, "Number overflowed.");
253 1.1 cgd return (eight);
254 1.1 cgd }
255 1.1 cgd
256 1.2 cgd /*
257 1.4 briggs * Don't use ANSI function declarations unless you absolutely have to,
258 1.2 cgd * i.e. you're declaring functions with variable numbers of arguments.
259 1.2 cgd *
260 1.2 cgd * ANSI function braces look like regular function braces.
261 1.2 cgd */
262 1.9 lukem void
263 1.1 cgd function(int a1, int a2)
264 1.1 cgd {
265 1.1 cgd ...
266 1.2 cgd }
267 1.2 cgd
268 1.2 cgd /* Variable numbers of arguments should look like this. */
269 1.2 cgd #if __STDC__
270 1.2 cgd #include <stdarg.h>
271 1.2 cgd #else
272 1.2 cgd #include <varargs.h>
273 1.2 cgd #endif
274 1.2 cgd
275 1.2 cgd void
276 1.2 cgd #if __STDC__
277 1.2 cgd vaf(const char *fmt, ...)
278 1.2 cgd #else
279 1.2 cgd vaf(fmt, va_alist)
280 1.2 cgd char *fmt;
281 1.2 cgd va_dcl
282 1.2 cgd #endif
283 1.2 cgd {
284 1.2 cgd va_list ap;
285 1.2 cgd #if __STDC__
286 1.2 cgd va_start(ap, fmt);
287 1.2 cgd #else
288 1.2 cgd va_start(ap);
289 1.2 cgd #endif
290 1.2 cgd STUFF;
291 1.2 cgd
292 1.2 cgd va_end(ap); /* No return needed for void functions. */
293 1.1 cgd }
294 1.1 cgd
295 1.1 cgd static void
296 1.1 cgd usage()
297 1.1 cgd { /* Insert an empty line if the function has no local variables. */
298 1.1 cgd
299 1.1 cgd /*
300 1.1 cgd * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
301 1.1 cgd * usually cleaner, not to mention avoiding stupid bugs.
302 1.1 cgd *
303 1.1 cgd * Usage statements should look like the manual pages. Options w/o
304 1.1 cgd * operands come first, in alphabetical order inside a single set of
305 1.1 cgd * braces. Followed by options with operands, in alphabetical order,
306 1.1 cgd * each in braces. Followed by required arguments in the order they
307 1.1 cgd * are specified, followed by optional arguments in the order they
308 1.1 cgd * are specified. A bar ('|') separates either/or options/arguments,
309 1.1 cgd * and multiple options/arguments which are specified together are
310 1.1 cgd * placed in a single set of braces.
311 1.1 cgd *
312 1.1 cgd * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
313 1.1 cgd * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
314 1.1 cgd */
315 1.1 cgd (void)fprintf(stderr, "usage: f [-ab]\n");
316 1.1 cgd exit(1);
317 1.1 cgd }
318