style revision 1.33
11.33Srillig/* $NetBSD: style,v 1.33 2005/08/20 08:58:57 rillig Exp $ */
21.6Sthorpej
31.1Scgd/*
41.12Slukem * The revision control tag appears first, with a blank line after it.
51.12Slukem * Copyright text appears after the revision control tag.
61.12Slukem */
71.12Slukem
81.12Slukem/*
91.12Slukem * The NetBSD source code style guide.
101.12Slukem * (Previously known as KNF - Kernel Normal Form).
111.1Scgd *
121.2Scgd *	from: @(#)style	1.12 (Berkeley) 3/18/94
131.10Sscottr */
141.10Sscottr/*
151.10Sscottr * An indent(1) profile approximating the style outlined in
161.10Sscottr * this document lives in /usr/share/misc/indent.pro.  It is a
171.10Sscottr * useful tool to assist in converting code to KNF, but indent(1)
181.10Sscottr * output generated using this profile must not be considered to
191.10Sscottr * be an authoritative reference.
201.1Scgd */
211.1Scgd
221.1Scgd/*
231.12Slukem * Source code revision control identifiers appear after any copyright
241.12Slukem * text.  Use the appropriate macros from <sys/cdefs.h>.  Usually only one
251.12Slukem * source file per program contains a __COPYRIGHT() section.
261.12Slukem * Historic Berkeley code may also have an __SCCSID() section.
271.12Slukem * Only one instance of each of these macros can occur in each file.
281.12Slukem */
291.12Slukem#include <sys/cdefs.h>
301.12Slukem__COPYRIGHT("@(#) Copyright (c) 2000\n\
311.12Slukem	The NetBSD Foundation, inc. All rights reserved.\n");
321.33Srillig__RCSID("$NetBSD: style,v 1.33 2005/08/20 08:58:57 rillig Exp $");
331.12Slukem
341.12Slukem/*
351.1Scgd * VERY important single-line comments look like this.
361.1Scgd */
371.1Scgd
381.1Scgd/* Most single-line comments look like this. */
391.1Scgd
401.1Scgd/*
411.1Scgd * Multi-line comments look like this.  Make them real sentences.  Fill
421.1Scgd * them so they look like real paragraphs.
431.1Scgd */
441.1Scgd
451.2Scgd/*
461.12Slukem * Attempt to wrap lines longer than 80 characters appropriately.
471.12Slukem * Refer to the examples below for more information.
481.12Slukem */
491.12Slukem
501.12Slukem/*
511.12Slukem * EXAMPLE HEADER FILE:
521.12Slukem *
531.12Slukem * A header file should protect itself against multiple inclusion.
541.12Slukem * E.g, <sys/socket.h> would contain something like:
551.12Slukem */
561.12Slukem#ifndef _SYS_SOCKET_H_
571.12Slukem#define _SYS_SOCKET_H_
581.12Slukem/*
591.12Slukem * Contents of #include file go between the #ifndef and the #endif at the end.
601.12Slukem */
611.12Slukem#endif /* !_SYS_SOCKET_H_ */
621.12Slukem/*
631.12Slukem * END OF EXAMPLE HEADER FILE.
641.12Slukem */
651.12Slukem
661.12Slukem/*
671.12Slukem * Kernel include files come first.
681.2Scgd */
691.2Scgd#include <sys/types.h>		/* Non-local includes in brackets. */
701.2Scgd
711.12Slukem/*
721.12Slukem * If it's a network program, put the network include files next.
731.12Slukem * Group the includes files by subdirectory.
741.12Slukem */
751.2Scgd#include <net/if.h>
761.2Scgd#include <net/if_dl.h>
771.2Scgd#include <net/route.h>
781.2Scgd#include <netinet/in.h>
791.2Scgd#include <protocols/rwhod.h>
801.2Scgd
811.2Scgd/*
821.2Scgd * Then there's a blank line, followed by the /usr include files.
831.2Scgd * The /usr include files should be sorted!
841.2Scgd */
851.20Skleink#include <assert.h>
861.25Slukem#include <errno.h>
871.2Scgd#include <stdio.h>
881.18Scgd#include <stdlib.h>
891.1Scgd
901.1Scgd/*
911.1Scgd * Global pathnames are defined in /usr/include/paths.h.  Pathnames local
921.1Scgd * to the program go in pathnames.h in the local directory.
931.1Scgd */
941.2Scgd#include <paths.h>
951.2Scgd
961.2Scgd/* Then, there's a blank line, and the user include files. */
971.12Slukem#include "pathnames.h"		/* Local includes in double quotes. */
981.1Scgd
991.1Scgd/*
1001.2Scgd * ANSI function declarations for private functions (i.e. functions not used
1011.12Slukem * elsewhere) and the main() function go at the top of the source module. 
1021.12Slukem * Don't associate a name with the types.  I.e. use:
1031.12Slukem *	void function(int);
1041.12Slukem * Use your discretion on indenting between the return type and the name, and
1051.12Slukem * how to wrap a prototype too long for a single line.  In the latter case,
1061.15Slukem * lining up under the initial left parenthesis may be more readable.
1071.12Slukem * In any case, consistency is important!
1081.12Slukem */
1091.12Slukemstatic char *function(int, int, float, int);
1101.12Slukemstatic int dirinfo(const char *, struct stat *, struct dirent *,
1111.12Slukem		   struct statfs *, int *, char **[]);
1121.12Slukemstatic void usage(void);
1131.12Slukemint main(int, char *[]);
1141.1Scgd
1151.1Scgd/*
1161.1Scgd * Macros are capitalized, parenthesized, and should avoid side-effects.
1171.22Sjhawk * Spacing before and after the macro name may be any whitespace, though
1181.22Sjhawk * use of TABs should be consistent through a file.
1191.1Scgd * If they are an inline expansion of a function, the function is defined
1201.12Slukem * all in lowercase, the macro has the same name all in uppercase.
1211.12Slukem * If the macro is an expression, wrap the expression in parenthesis.
1221.12Slukem * If the macro is more than a single statement, use ``do { ... } while (0)'',
1231.12Slukem * so that a trailing semicolon works.  Right-justify the backslashes; it
1241.13Slukem * makes it easier to read. The CONSTCOND comment is to satisfy lint(1).
1251.12Slukem */
1261.12Slukem#define	MACRO(v, w, x, y)						\
1271.12Slukemdo {									\
1281.12Slukem	v = (x) + (y);							\
1291.12Slukem	w = (y) + 2;							\
1301.12Slukem} while (/* CONSTCOND */ 0)
1311.12Slukem
1321.15Slukem#define	DOUBLE(x) ((x) * 2)
1331.12Slukem
1341.12Slukem/* Enum types are capitalized.  No comma on the last element. */
1351.12Slukemenum enumtype {
1361.12Slukem	ONE,
1371.12Slukem	TWO
1381.12Slukem} et;
1391.12Slukem
1401.12Slukem/*
1411.16Senami * When declaring variables in structures, declare them organized by use in
1421.16Senami * a manner to attempt to minimize memory wastage because of compiler alignment
1431.12Slukem * issues, then by size, and then by alphabetical order. E.g, don't use
1441.12Slukem * ``int a; char *b; int c; char *d''; use ``int a; int b; char *c; char *d''.
1451.12Slukem * Each variable gets its own type and line, although an exception can be made
1461.12Slukem * when declaring bitfields (to clarify that it's part of the one bitfield).
1471.12Slukem * Note that the use of bitfields in general is discouraged.
1481.1Scgd *
1491.2Scgd * Major structures should be declared at the top of the file in which they
1501.2Scgd * are used, or in separate header files, if they are used in multiple
1511.2Scgd * source files.  Use of the structures should be by separate declarations
1521.1Scgd * and should be "extern" if they are declared in a header file.
1531.12Slukem *
1541.12Slukem * It may be useful to use a meaningful prefix for each member name.
1551.12Slukem * E.g, for ``struct softc'' the prefix could be ``sc_''.
1561.1Scgd */
1571.1Scgdstruct foo {
1581.12Slukem	struct foo *next;	/* List of active foo */
1591.12Slukem	struct mumble amumble;	/* Comment for mumble */
1601.12Slukem	int bar;
1611.12Slukem	unsigned int baz:1,	/* Bitfield; line up entries if desired */
1621.12Slukem		     fuz:5,
1631.12Slukem		     zap:2;
1641.27Ssimonb	uint8_t flag;
1651.1Scgd};
1661.1Scgdstruct foo *foohead;		/* Head of global foo list */
1671.2Scgd
1681.2Scgd/* Make the structure name match the typedef. */
1691.12Slukemtypedef struct BAR {
1701.12Slukem	int level;
1711.2Scgd} BAR;
1721.12Slukem
1731.32Sjunyoung/* C99 uintN_t is preferred over u_intN_t. */
1741.32Sjunyounguint32_t zero;
1751.32Sjunyoung
1761.1Scgd/*
1771.1Scgd * All major routines should have a comment briefly describing what
1781.2Scgd * they do.  The comment before the "main" routine should describe
1791.1Scgd * what the program does.
1801.1Scgd */
1811.2Scgdint
1821.12Slukemmain(int argc, char *argv[])
1831.1Scgd{
1841.1Scgd	long num;
1851.1Scgd	int ch;
1861.1Scgd	char *ep;
1871.1Scgd
1881.1Scgd	/*
1891.17Scgd	 * At the start of main(), call setprogname() to set the program
1901.17Scgd	 * name.  This does nothing on NetBSD, but increases portability
1911.17Scgd	 * to other systems.
1921.17Scgd	 */
1931.17Scgd	setprogname(argv[0]);
1941.17Scgd
1951.17Scgd	/*
1961.2Scgd	 * For consistency, getopt should be used to parse options.  Options
1971.2Scgd	 * should be sorted in the getopt call and the switch statement, unless
1981.2Scgd	 * parts of the switch cascade.  Elements in a switch statement that
1991.2Scgd	 * cascade should have a FALLTHROUGH comment.  Numerical arguments
2001.2Scgd	 * should be checked for accuracy.  Code that cannot be reached should
2011.2Scgd	 * have a NOTREACHED comment.
2021.1Scgd	 */
2031.12Slukem	while ((ch = getopt(argc, argv, "abn")) != -1) {
2041.1Scgd		switch (ch) {		/* Indent the switch. */
2051.1Scgd		case 'a':		/* Don't indent the case. */
2061.1Scgd			aflag = 1;
2071.1Scgd			/* FALLTHROUGH */
2081.1Scgd		case 'b':
2091.1Scgd			bflag = 1;
2101.1Scgd			break;
2111.1Scgd		case 'n':
2121.25Slukem			errno = 0;
2131.1Scgd			num = strtol(optarg, &ep, 10);
2141.25Slukem			if (num <= 0 || *ep != '\0' || (errno == ERANGE &&
2151.25Slukem			    (num == LONG_MAX || num == LONG_MIN)) )
2161.12Slukem				errx(1, "illegal number -- %s", optarg);
2171.1Scgd			break;
2181.1Scgd		case '?':
2191.1Scgd		default:
2201.1Scgd			usage();
2211.2Scgd			/* NOTREACHED */
2221.1Scgd		}
2231.12Slukem	}
2241.1Scgd	argc -= optind;
2251.1Scgd	argv += optind;
2261.1Scgd
2271.1Scgd	/*
2281.1Scgd	 * Space after keywords (while, for, return, switch).  No braces are
2291.12Slukem	 * used for control statements with zero or only a single statement,
2301.12Slukem	 * unless it's a long statement.
2311.1Scgd	 *
2321.1Scgd	 * Forever loops are done with for's, not while's.
2331.1Scgd	 */
2341.12Slukem	for (p = buf; *p != '\0'; ++p)
2351.12Slukem		continue;		/* Explicit no-op */
2361.1Scgd	for (;;)
2371.1Scgd		stmt;
2381.12Slukem
2391.1Scgd	/*
2401.2Scgd	 * Parts of a for loop may be left empty.  Don't put declarations
2411.2Scgd	 * inside blocks unless the routine is unusually complicated.
2421.1Scgd	 */
2431.1Scgd	for (; cnt < 15; cnt++) {
2441.1Scgd		stmt1;
2451.1Scgd		stmt2;
2461.1Scgd	}
2471.1Scgd
2481.2Scgd	/* Second level indents are four spaces. */
2491.2Scgd	while (cnt < 20)
2501.16Senami		z = a + really + long + statement + that + needs + two lines +
2511.1Scgd		    gets + indented + four + spaces + on + the + second +
2521.7Senami		    and + subsequent + lines;
2531.1Scgd
2541.1Scgd	/*
2551.2Scgd	 * Closing and opening braces go on the same line as the else.
2561.12Slukem	 * Don't add braces that aren't necessary except in cases where
2571.12Slukem	 * there are ambiguity or readability issues.
2581.1Scgd	 */
2591.12Slukem	if (test) {
2601.12Slukem		/*
2611.12Slukem		 * I have a long comment here.
2621.12Slukem		 */
2631.12Slukem#ifdef zorro
2641.12Slukem		z = 1;
2651.12Slukem#else
2661.12Slukem		b = 3;
2671.12Slukem#endif
2681.12Slukem	} else if (bar) {
2691.1Scgd		stmt;
2701.1Scgd		stmt;
2711.1Scgd	} else
2721.1Scgd		stmt;
2731.12Slukem
2741.2Scgd	/* No spaces after function names. */
2751.12Slukem	if ((result = function(a1, a2, a3, a4)) == NULL)
2761.12Slukem		exit(1);
2771.1Scgd
2781.1Scgd	/*
2791.12Slukem	 * Unary operators don't require spaces, binary operators do.
2801.12Slukem	 * Don't excessively use parenthesis, but they should be used if
2811.9Slukem	 * statement is really confusing without them, such as:
2821.9Slukem	 * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
2831.1Scgd	 */
2841.9Slukem	a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1);
2851.2Scgd	k = !(l & FLAGS);
2861.1Scgd
2871.1Scgd	/*
2881.26Sjmmv	 * Exits should be EXIT_SUCCESS on success, and EXIT_FAILURE on
2891.26Sjmmv	 * failure.  Don't denote all the possible exit points, using the
2901.29Schristos	 * integers 1 through 127.  Avoid obvious comments such as "Exit
2911.29Schristos	 * 0 on success.". Since main is a function that returns an int,
2921.29Schristos	 * prefer returning from it, than calling exit.
2931.1Scgd	 */
2941.29Schristos	return EXIT_SUCCESS;
2951.1Scgd}
2961.1Scgd
2971.1Scgd/*
2981.8Ssimonb * The function type must be declared on a line by itself
2991.16Senami * preceding the function.
3001.1Scgd */
3011.1Scgdstatic char *
3021.12Slukemfunction(int a1, int a2, float fl, int a4)
3031.1Scgd{
3041.1Scgd	/*
3051.1Scgd	 * When declaring variables in functions declare them sorted by size,
3061.12Slukem	 * then in alphabetical order; multiple ones per line are okay.
3071.12Slukem	 * Function prototypes should go in the include file "extern.h".
3081.1Scgd	 * If a line overflows reuse the type keyword.
3091.1Scgd	 *
3101.2Scgd	 * DO NOT initialize variables in the declarations.
3111.1Scgd	 */
3121.1Scgd	extern u_char one;
3131.1Scgd	extern char two;
3141.1Scgd	struct foo three, *four;
3151.1Scgd	double five;
3161.12Slukem	int *six, seven;
3171.12Slukem	char *eight, *nine, ten, eleven, twelve, thirteen;
3181.12Slukem	char fourteen, fifteen, sixteen;
3191.1Scgd
3201.1Scgd	/*
3211.1Scgd	 * Casts and sizeof's are not followed by a space.  NULL is any
3221.1Scgd	 * pointer type, and doesn't need to be cast, so use NULL instead
3231.1Scgd	 * of (struct foo *)0 or (struct foo *)NULL.  Also, test pointers
3241.12Slukem	 * against NULL.  I.e. use:
3251.1Scgd	 *
3261.12Slukem	 *	(p = f()) == NULL
3271.1Scgd	 * not:
3281.1Scgd	 *	!(p = f())
3291.2Scgd	 *
3301.12Slukem	 * Don't use `!' for tests unless it's a boolean.
3311.12Slukem	 * E.g. use "if (*p == '\0')", not "if (!*p)".
3321.12Slukem	 *
3331.31Schristos	 * Routines returning ``void *'' should not have their return
3341.31Schristos	 * values cast to more specific pointer types.
3351.2Scgd	 *
3361.2Scgd	 * Use err/warn(3), don't roll your own!
3371.1Scgd	 */
3381.1Scgd	if ((four = malloc(sizeof(struct foo))) == NULL)
3391.2Scgd		err(1, NULL);
3401.1Scgd	if ((six = (int *)overflow()) == NULL)
3411.2Scgd		errx(1, "Number overflowed.");
3421.23Sfvdl
3431.23Sfvdl	/* No parentheses are needed around the return value. */
3441.23Sfvdl	return eight;
3451.1Scgd}
3461.1Scgd
3471.2Scgd/*
3481.12Slukem * Use ANSI function declarations.  ANSI function braces look like
3491.12Slukem * old-style (K&R) function braces.
3501.12Slukem * As per the wrapped prototypes, use your discretion on how to format
3511.12Slukem * the subsequent lines.
3521.12Slukem */
3531.12Slukemstatic int
3541.12Slukemdirinfo(const char *p, struct stat *sb, struct dirent *de, struct statfs *sf,
3551.12Slukem	int *rargc, char **rargv[])
3561.12Slukem{	/* Insert an empty line if the function has no local variables. */
3571.19Skleink
3581.19Skleink	/*
3591.19Skleink	 * In system libraries, catch obviously invalid function arguments
3601.19Skleink	 * using _DIAGASSERT(3).
3611.19Skleink	 */
3621.19Skleink	_DIAGASSERT(p != NULL);
3631.19Skleink	_DIAGASSERT(filedesc != -1);
3641.12Slukem
3651.14Slukem	if (stat(p, sb) < 0)
3661.14Slukem		err(1, "Unable to stat %s", p);
3671.14Slukem
3681.14Slukem	/*
3691.14Slukem	 * To printf 64 bit quantities, use %ll and cast to (long long).
3701.14Slukem	 */
3711.29Schristos	(void)printf("The size of %s is %lld\n", p, (long long)sb->st_size);
3721.2Scgd}
3731.2Scgd
3741.12Slukem/*
3751.12Slukem * Functions that support variable numbers of arguments should look like this.
3761.12Slukem * (With the #include <stdarg.h> appearing at the top of the file with the
3771.12Slukem * other include files).
3781.12Slukem */
3791.2Scgd#include <stdarg.h>
3801.2Scgd
3811.2Scgdvoid
3821.2Scgdvaf(const char *fmt, ...)
3831.2Scgd{
3841.2Scgd	va_list ap;
3851.12Slukem
3861.2Scgd	va_start(ap, fmt);
3871.2Scgd	STUFF;
3881.12Slukem	va_end(ap);	
3891.12Slukem				/* No return needed for void functions. */
3901.1Scgd}
3911.1Scgd
3921.1Scgdstatic void
3931.12Slukemusage(void)
3941.12Slukem{
3951.1Scgd
3961.1Scgd	/*
3971.1Scgd	 * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
3981.1Scgd	 * usually cleaner, not to mention avoiding stupid bugs.
3991.12Slukem	 * Use snprintf(3) or strlcpy(3)/strlcat(3) instead of sprintf(3);
4001.12Slukem	 * again to avoid stupid bugs.
4011.1Scgd	 *
4021.1Scgd	 * Usage statements should look like the manual pages.  Options w/o
4031.1Scgd	 * operands come first, in alphabetical order inside a single set of
4041.1Scgd	 * braces.  Followed by options with operands, in alphabetical order,
4051.1Scgd	 * each in braces.  Followed by required arguments in the order they
4061.1Scgd	 * are specified, followed by optional arguments in the order they
4071.12Slukem	 * are specified.  A bar (`|') separates either/or options/arguments,
4081.1Scgd	 * and multiple options/arguments which are specified together are
4091.1Scgd	 * placed in a single set of braces.
4101.1Scgd	 *
4111.17Scgd	 * Use getprogname() instead of hardcoding the program name.
4121.12Slukem	 *
4131.1Scgd	 * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
4141.1Scgd	 * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
4151.1Scgd	 */
4161.17Scgd	(void)fprintf(stderr, "usage: %s [-ab]\n", getprogname());
4171.33Srillig	exit(EXIT_FAILURE);
4181.1Scgd}
419