style revision 1.44 1 1.44 jschauma /* $NetBSD: style,v 1.44 2008/09/09 19:18:41 jschauma Exp $ */
2 1.6 thorpej
3 1.1 cgd /*
4 1.12 lukem * The revision control tag appears first, with a blank line after it.
5 1.12 lukem * Copyright text appears after the revision control tag.
6 1.12 lukem */
7 1.12 lukem
8 1.12 lukem /*
9 1.12 lukem * The NetBSD source code style guide.
10 1.12 lukem * (Previously known as KNF - Kernel Normal Form).
11 1.1 cgd *
12 1.2 cgd * from: @(#)style 1.12 (Berkeley) 3/18/94
13 1.10 scottr */
14 1.10 scottr /*
15 1.10 scottr * An indent(1) profile approximating the style outlined in
16 1.10 scottr * this document lives in /usr/share/misc/indent.pro. It is a
17 1.10 scottr * useful tool to assist in converting code to KNF, but indent(1)
18 1.10 scottr * output generated using this profile must not be considered to
19 1.10 scottr * be an authoritative reference.
20 1.1 cgd */
21 1.1 cgd
22 1.1 cgd /*
23 1.12 lukem * Source code revision control identifiers appear after any copyright
24 1.12 lukem * text. Use the appropriate macros from <sys/cdefs.h>. Usually only one
25 1.12 lukem * source file per program contains a __COPYRIGHT() section.
26 1.12 lukem * Historic Berkeley code may also have an __SCCSID() section.
27 1.12 lukem * Only one instance of each of these macros can occur in each file.
28 1.43 lukem * Don't use newlines in the identifiers.
29 1.12 lukem */
30 1.12 lukem #include <sys/cdefs.h>
31 1.43 lukem __COPYRIGHT("@(#) Copyright (c) 2008\
32 1.43 lukem The NetBSD Foundation, inc. All rights reserved.");
33 1.44 jschauma __RCSID("$NetBSD: style,v 1.44 2008/09/09 19:18:41 jschauma Exp $");
34 1.12 lukem
35 1.12 lukem /*
36 1.1 cgd * VERY important single-line comments look like this.
37 1.1 cgd */
38 1.1 cgd
39 1.1 cgd /* Most single-line comments look like this. */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * Multi-line comments look like this. Make them real sentences. Fill
43 1.1 cgd * them so they look like real paragraphs.
44 1.1 cgd */
45 1.1 cgd
46 1.2 cgd /*
47 1.12 lukem * Attempt to wrap lines longer than 80 characters appropriately.
48 1.12 lukem * Refer to the examples below for more information.
49 1.12 lukem */
50 1.12 lukem
51 1.12 lukem /*
52 1.12 lukem * EXAMPLE HEADER FILE:
53 1.12 lukem *
54 1.12 lukem * A header file should protect itself against multiple inclusion.
55 1.12 lukem * E.g, <sys/socket.h> would contain something like:
56 1.12 lukem */
57 1.12 lukem #ifndef _SYS_SOCKET_H_
58 1.12 lukem #define _SYS_SOCKET_H_
59 1.12 lukem /*
60 1.12 lukem * Contents of #include file go between the #ifndef and the #endif at the end.
61 1.12 lukem */
62 1.12 lukem #endif /* !_SYS_SOCKET_H_ */
63 1.12 lukem /*
64 1.12 lukem * END OF EXAMPLE HEADER FILE.
65 1.12 lukem */
66 1.12 lukem
67 1.12 lukem /*
68 1.39 darcy * If a header file requires structures, defines, typedefs, etc. from
69 1.39 darcy * another header file it should include that header file and not depend
70 1.39 darcy * on the including file for that header including both. If there are
71 1.39 darcy * exceptions to this for specific headers it should be clearly documented
72 1.39 darcy * in the headers and, if appropriate, the documentation. Nothing in this
73 1.39 darcy * rule should suggest relaxation of the multiple inclusion rule and the
74 1.39 darcy * application programmer should be free to include both regardless.
75 1.39 darcy */
76 1.39 darcy
77 1.39 darcy /*
78 1.12 lukem * Kernel include files come first.
79 1.2 cgd */
80 1.2 cgd #include <sys/types.h> /* Non-local includes in brackets. */
81 1.2 cgd
82 1.12 lukem /*
83 1.12 lukem * If it's a network program, put the network include files next.
84 1.12 lukem * Group the includes files by subdirectory.
85 1.12 lukem */
86 1.2 cgd #include <net/if.h>
87 1.2 cgd #include <net/if_dl.h>
88 1.2 cgd #include <net/route.h>
89 1.2 cgd #include <netinet/in.h>
90 1.2 cgd #include <protocols/rwhod.h>
91 1.2 cgd
92 1.2 cgd /*
93 1.2 cgd * Then there's a blank line, followed by the /usr include files.
94 1.2 cgd * The /usr include files should be sorted!
95 1.2 cgd */
96 1.20 kleink #include <assert.h>
97 1.25 lukem #include <errno.h>
98 1.36 briggs #include <inttypes.h>
99 1.2 cgd #include <stdio.h>
100 1.18 cgd #include <stdlib.h>
101 1.1 cgd
102 1.1 cgd /*
103 1.1 cgd * Global pathnames are defined in /usr/include/paths.h. Pathnames local
104 1.1 cgd * to the program go in pathnames.h in the local directory.
105 1.1 cgd */
106 1.2 cgd #include <paths.h>
107 1.2 cgd
108 1.2 cgd /* Then, there's a blank line, and the user include files. */
109 1.12 lukem #include "pathnames.h" /* Local includes in double quotes. */
110 1.1 cgd
111 1.1 cgd /*
112 1.2 cgd * ANSI function declarations for private functions (i.e. functions not used
113 1.12 lukem * elsewhere) and the main() function go at the top of the source module.
114 1.12 lukem * Don't associate a name with the types. I.e. use:
115 1.12 lukem * void function(int);
116 1.12 lukem * Use your discretion on indenting between the return type and the name, and
117 1.12 lukem * how to wrap a prototype too long for a single line. In the latter case,
118 1.15 lukem * lining up under the initial left parenthesis may be more readable.
119 1.12 lukem * In any case, consistency is important!
120 1.12 lukem */
121 1.12 lukem static char *function(int, int, float, int);
122 1.12 lukem static int dirinfo(const char *, struct stat *, struct dirent *,
123 1.12 lukem struct statfs *, int *, char **[]);
124 1.12 lukem static void usage(void);
125 1.12 lukem int main(int, char *[]);
126 1.1 cgd
127 1.1 cgd /*
128 1.1 cgd * Macros are capitalized, parenthesized, and should avoid side-effects.
129 1.22 jhawk * Spacing before and after the macro name may be any whitespace, though
130 1.22 jhawk * use of TABs should be consistent through a file.
131 1.1 cgd * If they are an inline expansion of a function, the function is defined
132 1.12 lukem * all in lowercase, the macro has the same name all in uppercase.
133 1.12 lukem * If the macro is an expression, wrap the expression in parenthesis.
134 1.12 lukem * If the macro is more than a single statement, use ``do { ... } while (0)'',
135 1.12 lukem * so that a trailing semicolon works. Right-justify the backslashes; it
136 1.13 lukem * makes it easier to read. The CONSTCOND comment is to satisfy lint(1).
137 1.12 lukem */
138 1.12 lukem #define MACRO(v, w, x, y) \
139 1.12 lukem do { \
140 1.12 lukem v = (x) + (y); \
141 1.12 lukem w = (y) + 2; \
142 1.12 lukem } while (/* CONSTCOND */ 0)
143 1.12 lukem
144 1.15 lukem #define DOUBLE(x) ((x) * 2)
145 1.12 lukem
146 1.12 lukem /* Enum types are capitalized. No comma on the last element. */
147 1.12 lukem enum enumtype {
148 1.12 lukem ONE,
149 1.12 lukem TWO
150 1.12 lukem } et;
151 1.12 lukem
152 1.12 lukem /*
153 1.16 enami * When declaring variables in structures, declare them organized by use in
154 1.16 enami * a manner to attempt to minimize memory wastage because of compiler alignment
155 1.12 lukem * issues, then by size, and then by alphabetical order. E.g, don't use
156 1.12 lukem * ``int a; char *b; int c; char *d''; use ``int a; int b; char *c; char *d''.
157 1.12 lukem * Each variable gets its own type and line, although an exception can be made
158 1.12 lukem * when declaring bitfields (to clarify that it's part of the one bitfield).
159 1.12 lukem * Note that the use of bitfields in general is discouraged.
160 1.1 cgd *
161 1.2 cgd * Major structures should be declared at the top of the file in which they
162 1.2 cgd * are used, or in separate header files, if they are used in multiple
163 1.2 cgd * source files. Use of the structures should be by separate declarations
164 1.1 cgd * and should be "extern" if they are declared in a header file.
165 1.12 lukem *
166 1.12 lukem * It may be useful to use a meaningful prefix for each member name.
167 1.12 lukem * E.g, for ``struct softc'' the prefix could be ``sc_''.
168 1.1 cgd */
169 1.1 cgd struct foo {
170 1.12 lukem struct foo *next; /* List of active foo */
171 1.12 lukem struct mumble amumble; /* Comment for mumble */
172 1.12 lukem int bar;
173 1.12 lukem unsigned int baz:1, /* Bitfield; line up entries if desired */
174 1.12 lukem fuz:5,
175 1.12 lukem zap:2;
176 1.27 simonb uint8_t flag;
177 1.1 cgd };
178 1.1 cgd struct foo *foohead; /* Head of global foo list */
179 1.2 cgd
180 1.2 cgd /* Make the structure name match the typedef. */
181 1.12 lukem typedef struct BAR {
182 1.12 lukem int level;
183 1.2 cgd } BAR;
184 1.12 lukem
185 1.32 junyoung /* C99 uintN_t is preferred over u_intN_t. */
186 1.32 junyoung uint32_t zero;
187 1.32 junyoung
188 1.1 cgd /*
189 1.1 cgd * All major routines should have a comment briefly describing what
190 1.2 cgd * they do. The comment before the "main" routine should describe
191 1.1 cgd * what the program does.
192 1.1 cgd */
193 1.2 cgd int
194 1.12 lukem main(int argc, char *argv[])
195 1.1 cgd {
196 1.1 cgd long num;
197 1.1 cgd int ch;
198 1.1 cgd char *ep;
199 1.1 cgd
200 1.1 cgd /*
201 1.17 cgd * At the start of main(), call setprogname() to set the program
202 1.17 cgd * name. This does nothing on NetBSD, but increases portability
203 1.17 cgd * to other systems.
204 1.17 cgd */
205 1.17 cgd setprogname(argv[0]);
206 1.17 cgd
207 1.17 cgd /*
208 1.37 wiz * For consistency, getopt should be used to parse options.
209 1.37 wiz * Options should be sorted in the getopt call and the switch
210 1.37 wiz * statement, unless parts of the switch cascade. For the
211 1.37 wiz * sorting order, see the usage() example below. Don't forget
212 1.37 wiz * to add option descriptions to the usage and the manpage.
213 1.37 wiz * Elements in a switch statement that cascade should have a
214 1.37 wiz * FALLTHROUGH comment. Numerical arguments should be checked
215 1.37 wiz * for accuracy. Code that cannot be reached should have a
216 1.37 wiz * NOTREACHED comment.
217 1.1 cgd */
218 1.41 plunky while ((ch = getopt(argc, argv, "abn:")) != -1) {
219 1.1 cgd switch (ch) { /* Indent the switch. */
220 1.1 cgd case 'a': /* Don't indent the case. */
221 1.1 cgd aflag = 1;
222 1.1 cgd /* FALLTHROUGH */
223 1.1 cgd case 'b':
224 1.1 cgd bflag = 1;
225 1.1 cgd break;
226 1.1 cgd case 'n':
227 1.25 lukem errno = 0;
228 1.1 cgd num = strtol(optarg, &ep, 10);
229 1.25 lukem if (num <= 0 || *ep != '\0' || (errno == ERANGE &&
230 1.25 lukem (num == LONG_MAX || num == LONG_MIN)) )
231 1.12 lukem errx(1, "illegal number -- %s", optarg);
232 1.1 cgd break;
233 1.1 cgd case '?':
234 1.1 cgd default:
235 1.1 cgd usage();
236 1.2 cgd /* NOTREACHED */
237 1.1 cgd }
238 1.12 lukem }
239 1.1 cgd argc -= optind;
240 1.1 cgd argv += optind;
241 1.1 cgd
242 1.1 cgd /*
243 1.1 cgd * Space after keywords (while, for, return, switch). No braces are
244 1.38 christos * required for control statements with only a single statement,
245 1.12 lukem * unless it's a long statement.
246 1.1 cgd *
247 1.1 cgd * Forever loops are done with for's, not while's.
248 1.1 cgd */
249 1.12 lukem for (p = buf; *p != '\0'; ++p)
250 1.12 lukem continue; /* Explicit no-op */
251 1.1 cgd for (;;)
252 1.1 cgd stmt;
253 1.12 lukem
254 1.1 cgd /*
255 1.38 christos * Braces are required for control statements with a single statement
256 1.38 christos * that may expand to nothing.
257 1.38 christos */
258 1.38 christos #ifdef DEBUG_FOO
259 1.40 christos #define DPRINTF(a) printf a
260 1.40 christos #else
261 1.38 christos #define DPRINTF(a)
262 1.38 christos #endif
263 1.38 christos if (broken) {
264 1.38 christos DPRINTF(("broken is %d\n", broken));
265 1.38 christos }
266 1.38 christos
267 1.38 christos /*
268 1.2 cgd * Parts of a for loop may be left empty. Don't put declarations
269 1.2 cgd * inside blocks unless the routine is unusually complicated.
270 1.1 cgd */
271 1.1 cgd for (; cnt < 15; cnt++) {
272 1.1 cgd stmt1;
273 1.1 cgd stmt2;
274 1.1 cgd }
275 1.1 cgd
276 1.2 cgd /* Second level indents are four spaces. */
277 1.2 cgd while (cnt < 20)
278 1.40 christos z = a + really + long + statement + that + needs + two + lines +
279 1.1 cgd gets + indented + four + spaces + on + the + second +
280 1.7 enami and + subsequent + lines;
281 1.1 cgd
282 1.1 cgd /*
283 1.2 cgd * Closing and opening braces go on the same line as the else.
284 1.12 lukem * Don't add braces that aren't necessary except in cases where
285 1.12 lukem * there are ambiguity or readability issues.
286 1.1 cgd */
287 1.12 lukem if (test) {
288 1.12 lukem /*
289 1.12 lukem * I have a long comment here.
290 1.12 lukem */
291 1.12 lukem #ifdef zorro
292 1.12 lukem z = 1;
293 1.12 lukem #else
294 1.12 lukem b = 3;
295 1.12 lukem #endif
296 1.12 lukem } else if (bar) {
297 1.1 cgd stmt;
298 1.1 cgd stmt;
299 1.1 cgd } else
300 1.1 cgd stmt;
301 1.12 lukem
302 1.2 cgd /* No spaces after function names. */
303 1.12 lukem if ((result = function(a1, a2, a3, a4)) == NULL)
304 1.12 lukem exit(1);
305 1.1 cgd
306 1.1 cgd /*
307 1.12 lukem * Unary operators don't require spaces, binary operators do.
308 1.12 lukem * Don't excessively use parenthesis, but they should be used if
309 1.9 lukem * statement is really confusing without them, such as:
310 1.9 lukem * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
311 1.1 cgd */
312 1.9 lukem a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1);
313 1.2 cgd k = !(l & FLAGS);
314 1.1 cgd
315 1.1 cgd /*
316 1.26 jmmv * Exits should be EXIT_SUCCESS on success, and EXIT_FAILURE on
317 1.26 jmmv * failure. Don't denote all the possible exit points, using the
318 1.29 christos * integers 1 through 127. Avoid obvious comments such as "Exit
319 1.29 christos * 0 on success.". Since main is a function that returns an int,
320 1.29 christos * prefer returning from it, than calling exit.
321 1.1 cgd */
322 1.29 christos return EXIT_SUCCESS;
323 1.1 cgd }
324 1.1 cgd
325 1.1 cgd /*
326 1.8 simonb * The function type must be declared on a line by itself
327 1.16 enami * preceding the function.
328 1.1 cgd */
329 1.1 cgd static char *
330 1.12 lukem function(int a1, int a2, float fl, int a4)
331 1.1 cgd {
332 1.1 cgd /*
333 1.1 cgd * When declaring variables in functions declare them sorted by size,
334 1.12 lukem * then in alphabetical order; multiple ones per line are okay.
335 1.12 lukem * Function prototypes should go in the include file "extern.h".
336 1.1 cgd * If a line overflows reuse the type keyword.
337 1.1 cgd *
338 1.2 cgd * DO NOT initialize variables in the declarations.
339 1.1 cgd */
340 1.1 cgd extern u_char one;
341 1.1 cgd extern char two;
342 1.1 cgd struct foo three, *four;
343 1.1 cgd double five;
344 1.12 lukem int *six, seven;
345 1.12 lukem char *eight, *nine, ten, eleven, twelve, thirteen;
346 1.12 lukem char fourteen, fifteen, sixteen;
347 1.1 cgd
348 1.1 cgd /*
349 1.1 cgd * Casts and sizeof's are not followed by a space. NULL is any
350 1.1 cgd * pointer type, and doesn't need to be cast, so use NULL instead
351 1.1 cgd * of (struct foo *)0 or (struct foo *)NULL. Also, test pointers
352 1.12 lukem * against NULL. I.e. use:
353 1.1 cgd *
354 1.12 lukem * (p = f()) == NULL
355 1.1 cgd * not:
356 1.1 cgd * !(p = f())
357 1.2 cgd *
358 1.12 lukem * Don't use `!' for tests unless it's a boolean.
359 1.12 lukem * E.g. use "if (*p == '\0')", not "if (!*p)".
360 1.12 lukem *
361 1.31 christos * Routines returning ``void *'' should not have their return
362 1.31 christos * values cast to more specific pointer types.
363 1.2 cgd *
364 1.2 cgd * Use err/warn(3), don't roll your own!
365 1.1 cgd */
366 1.1 cgd if ((four = malloc(sizeof(struct foo))) == NULL)
367 1.2 cgd err(1, NULL);
368 1.1 cgd if ((six = (int *)overflow()) == NULL)
369 1.2 cgd errx(1, "Number overflowed.");
370 1.23 fvdl
371 1.23 fvdl /* No parentheses are needed around the return value. */
372 1.23 fvdl return eight;
373 1.1 cgd }
374 1.1 cgd
375 1.2 cgd /*
376 1.12 lukem * Use ANSI function declarations. ANSI function braces look like
377 1.12 lukem * old-style (K&R) function braces.
378 1.12 lukem * As per the wrapped prototypes, use your discretion on how to format
379 1.12 lukem * the subsequent lines.
380 1.12 lukem */
381 1.12 lukem static int
382 1.12 lukem dirinfo(const char *p, struct stat *sb, struct dirent *de, struct statfs *sf,
383 1.12 lukem int *rargc, char **rargv[])
384 1.12 lukem { /* Insert an empty line if the function has no local variables. */
385 1.19 kleink
386 1.19 kleink /*
387 1.19 kleink * In system libraries, catch obviously invalid function arguments
388 1.19 kleink * using _DIAGASSERT(3).
389 1.19 kleink */
390 1.19 kleink _DIAGASSERT(p != NULL);
391 1.19 kleink _DIAGASSERT(filedesc != -1);
392 1.12 lukem
393 1.14 lukem if (stat(p, sb) < 0)
394 1.14 lukem err(1, "Unable to stat %s", p);
395 1.14 lukem
396 1.14 lukem /*
397 1.36 briggs * To printf quantities that might be larger that "long", include
398 1.36 briggs * <inttypes.h>, cast quantities to intmax_t or uintmax_t and use
399 1.42 apb * PRI?MAX constants.
400 1.36 briggs */
401 1.36 briggs (void)printf("The size of %s is %" PRIdMAX " (%#" PRIxMAX ")\n", p,
402 1.36 briggs (intmax_t)sb->st_size, (uintmax_t)sb->st_size);
403 1.36 briggs
404 1.36 briggs /*
405 1.36 briggs * To printf quantities of known bit-width, use the corresponding
406 1.36 briggs * defines (generally only done within NetBSD for quantities that
407 1.36 briggs * exceed 32-bits).
408 1.36 briggs */
409 1.36 briggs (void)printf("%s uses %" PRId64 " blocks and has flags %#" PRIx32 "\n",
410 1.36 briggs p, sb->st_blocks, sb->st_flags);
411 1.36 briggs
412 1.36 briggs /*
413 1.36 briggs * There are similar constants that should be used with the *scanf(3)
414 1.36 briggs * family of functions: SCN?MAX, SCN?64, etc.
415 1.14 lukem */
416 1.2 cgd }
417 1.2 cgd
418 1.12 lukem /*
419 1.12 lukem * Functions that support variable numbers of arguments should look like this.
420 1.12 lukem * (With the #include <stdarg.h> appearing at the top of the file with the
421 1.44 jschauma * other include files.)
422 1.12 lukem */
423 1.2 cgd #include <stdarg.h>
424 1.2 cgd
425 1.2 cgd void
426 1.2 cgd vaf(const char *fmt, ...)
427 1.2 cgd {
428 1.2 cgd va_list ap;
429 1.12 lukem
430 1.2 cgd va_start(ap, fmt);
431 1.2 cgd STUFF;
432 1.12 lukem va_end(ap);
433 1.12 lukem /* No return needed for void functions. */
434 1.1 cgd }
435 1.1 cgd
436 1.1 cgd static void
437 1.12 lukem usage(void)
438 1.12 lukem {
439 1.1 cgd
440 1.1 cgd /*
441 1.1 cgd * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
442 1.1 cgd * usually cleaner, not to mention avoiding stupid bugs.
443 1.12 lukem * Use snprintf(3) or strlcpy(3)/strlcat(3) instead of sprintf(3);
444 1.12 lukem * again to avoid stupid bugs.
445 1.1 cgd *
446 1.37 wiz * Usage statements should look like the manual pages.
447 1.37 wiz * Options w/o operands come first, in alphabetical order
448 1.37 wiz * inside a single set of braces, upper case before lower case
449 1.37 wiz * (AaBbCc...). Next are options with operands, in the same
450 1.37 wiz * order, each in braces. Then required arguments in the
451 1.37 wiz * order they are specified, followed by optional arguments in
452 1.37 wiz * the order they are specified. A bar (`|') separates
453 1.37 wiz * either/or options/arguments, and multiple options/arguments
454 1.37 wiz * which are specified together are placed in a single set of
455 1.37 wiz * braces.
456 1.1 cgd *
457 1.17 cgd * Use getprogname() instead of hardcoding the program name.
458 1.12 lukem *
459 1.37 wiz * "usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
460 1.1 cgd * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
461 1.1 cgd */
462 1.17 cgd (void)fprintf(stderr, "usage: %s [-ab]\n", getprogname());
463 1.33 rillig exit(EXIT_FAILURE);
464 1.1 cgd }
465