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