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