style revision 1.18
11.18Scgd/* $NetBSD: style,v 1.18 2001/02/21 00:04:43 cgd 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#ifndef __lint
311.12Slukem__COPYRIGHT("@(#) Copyright (c) 2000\n\
321.12Slukem	The NetBSD Foundation, inc. All rights reserved.\n");
331.18Scgd__RCSID("$NetBSD: style,v 1.18 2001/02/21 00:04:43 cgd Exp $");
341.12Slukem#endif /* !__lint */
351.12Slukem
361.12Slukem/*
371.1Scgd * VERY important single-line comments look like this.
381.1Scgd */
391.1Scgd
401.1Scgd/* Most single-line comments look like this. */
411.1Scgd
421.1Scgd/*
431.1Scgd * Multi-line comments look like this.  Make them real sentences.  Fill
441.1Scgd * them so they look like real paragraphs.
451.1Scgd */
461.1Scgd
471.2Scgd/*
481.12Slukem * Attempt to wrap lines longer than 80 characters appropriately.
491.12Slukem * Refer to the examples below for more information.
501.12Slukem */
511.12Slukem
521.12Slukem/*
531.12Slukem * EXAMPLE HEADER FILE:
541.12Slukem *
551.12Slukem * A header file should protect itself against multiple inclusion.
561.12Slukem * E.g, <sys/socket.h> would contain something like:
571.12Slukem */
581.12Slukem#ifndef _SYS_SOCKET_H_
591.12Slukem#define _SYS_SOCKET_H_
601.12Slukem/*
611.12Slukem * Contents of #include file go between the #ifndef and the #endif at the end.
621.12Slukem */
631.12Slukem#endif /* !_SYS_SOCKET_H_ */
641.12Slukem/*
651.12Slukem * END OF EXAMPLE HEADER FILE.
661.12Slukem */
671.12Slukem
681.12Slukem/*
691.12Slukem * Kernel include files come first.
701.2Scgd */
711.2Scgd#include <sys/types.h>		/* Non-local includes in brackets. */
721.2Scgd
731.12Slukem/*
741.12Slukem * If it's a network program, put the network include files next.
751.12Slukem * Group the includes files by subdirectory.
761.12Slukem */
771.2Scgd#include <net/if.h>
781.2Scgd#include <net/if_dl.h>
791.2Scgd#include <net/route.h>
801.2Scgd#include <netinet/in.h>
811.2Scgd#include <protocols/rwhod.h>
821.2Scgd
831.2Scgd/*
841.2Scgd * Then there's a blank line, followed by the /usr include files.
851.2Scgd * The /usr include files should be sorted!
861.2Scgd */
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.1Scgd * If they are an inline expansion of a function, the function is defined
1181.12Slukem * all in lowercase, the macro has the same name all in uppercase.
1191.12Slukem * If the macro is an expression, wrap the expression in parenthesis.
1201.12Slukem * If the macro is more than a single statement, use ``do { ... } while (0)'',
1211.12Slukem * so that a trailing semicolon works.  Right-justify the backslashes; it
1221.13Slukem * makes it easier to read. The CONSTCOND comment is to satisfy lint(1).
1231.12Slukem */
1241.12Slukem#define	MACRO(v, w, x, y)						\
1251.12Slukemdo {									\
1261.12Slukem	v = (x) + (y);							\
1271.12Slukem	w = (y) + 2;							\
1281.12Slukem} while (/* CONSTCOND */ 0)
1291.12Slukem
1301.15Slukem#define	DOUBLE(x) ((x) * 2)
1311.12Slukem
1321.12Slukem/* Enum types are capitalized.  No comma on the last element. */
1331.12Slukemenum enumtype {
1341.12Slukem	ONE,
1351.12Slukem	TWO
1361.12Slukem} et;
1371.12Slukem
1381.12Slukem/*
1391.16Senami * When declaring variables in structures, declare them organized by use in
1401.16Senami * a manner to attempt to minimize memory wastage because of compiler alignment
1411.12Slukem * issues, then by size, and then by alphabetical order. E.g, don't use
1421.12Slukem * ``int a; char *b; int c; char *d''; use ``int a; int b; char *c; char *d''.
1431.12Slukem * Each variable gets its own type and line, although an exception can be made
1441.12Slukem * when declaring bitfields (to clarify that it's part of the one bitfield).
1451.12Slukem * Note that the use of bitfields in general is discouraged.
1461.1Scgd *
1471.2Scgd * Major structures should be declared at the top of the file in which they
1481.2Scgd * are used, or in separate header files, if they are used in multiple
1491.2Scgd * source files.  Use of the structures should be by separate declarations
1501.1Scgd * and should be "extern" if they are declared in a header file.
1511.12Slukem *
1521.12Slukem * It may be useful to use a meaningful prefix for each member name.
1531.12Slukem * E.g, for ``struct softc'' the prefix could be ``sc_''.
1541.1Scgd */
1551.1Scgdstruct foo {
1561.12Slukem	struct foo *next;	/* List of active foo */
1571.12Slukem	struct mumble amumble;	/* Comment for mumble */
1581.12Slukem	int bar;
1591.12Slukem	unsigned int baz:1,	/* Bitfield; line up entries if desired */
1601.12Slukem		     fuz:5,
1611.12Slukem		     zap:2;
1621.12Slukem	u_int8_t flag;
1631.1Scgd};
1641.1Scgdstruct foo *foohead;		/* Head of global foo list */
1651.2Scgd
1661.2Scgd/* Make the structure name match the typedef. */
1671.12Slukemtypedef struct BAR {
1681.12Slukem	int level;
1691.2Scgd} BAR;
1701.12Slukem
1711.1Scgd/*
1721.1Scgd * All major routines should have a comment briefly describing what
1731.2Scgd * they do.  The comment before the "main" routine should describe
1741.1Scgd * what the program does.
1751.1Scgd */
1761.2Scgdint
1771.12Slukemmain(int argc, char *argv[])
1781.1Scgd{
1791.1Scgd	long num;
1801.1Scgd	int ch;
1811.1Scgd	char *ep;
1821.1Scgd
1831.1Scgd	/*
1841.17Scgd	 * At the start of main(), call setprogname() to set the program
1851.17Scgd	 * name.  This does nothing on NetBSD, but increases portability
1861.17Scgd	 * to other systems.
1871.17Scgd	 */
1881.17Scgd	setprogname(argv[0]);
1891.17Scgd
1901.17Scgd	/*
1911.2Scgd	 * For consistency, getopt should be used to parse options.  Options
1921.2Scgd	 * should be sorted in the getopt call and the switch statement, unless
1931.2Scgd	 * parts of the switch cascade.  Elements in a switch statement that
1941.2Scgd	 * cascade should have a FALLTHROUGH comment.  Numerical arguments
1951.2Scgd	 * should be checked for accuracy.  Code that cannot be reached should
1961.2Scgd	 * have a NOTREACHED comment.
1971.1Scgd	 */
1981.12Slukem	while ((ch = getopt(argc, argv, "abn")) != -1) {
1991.1Scgd		switch (ch) {		/* Indent the switch. */
2001.1Scgd		case 'a':		/* Don't indent the case. */
2011.1Scgd			aflag = 1;
2021.1Scgd			/* FALLTHROUGH */
2031.1Scgd		case 'b':
2041.1Scgd			bflag = 1;
2051.1Scgd			break;
2061.1Scgd		case 'n':
2071.1Scgd			num = strtol(optarg, &ep, 10);
2081.12Slukem			if (num <= 0 || *ep != '\0')
2091.12Slukem				errx(1, "illegal number -- %s", optarg);
2101.1Scgd			break;
2111.1Scgd		case '?':
2121.1Scgd		default:
2131.1Scgd			usage();
2141.2Scgd			/* NOTREACHED */
2151.1Scgd		}
2161.12Slukem	}
2171.1Scgd	argc -= optind;
2181.1Scgd	argv += optind;
2191.1Scgd
2201.1Scgd	/*
2211.1Scgd	 * Space after keywords (while, for, return, switch).  No braces are
2221.12Slukem	 * used for control statements with zero or only a single statement,
2231.12Slukem	 * unless it's a long statement.
2241.1Scgd	 *
2251.1Scgd	 * Forever loops are done with for's, not while's.
2261.1Scgd	 */
2271.12Slukem	for (p = buf; *p != '\0'; ++p)
2281.12Slukem		continue;		/* Explicit no-op */
2291.1Scgd	for (;;)
2301.1Scgd		stmt;
2311.12Slukem
2321.1Scgd	/*
2331.2Scgd	 * Parts of a for loop may be left empty.  Don't put declarations
2341.2Scgd	 * inside blocks unless the routine is unusually complicated.
2351.1Scgd	 */
2361.1Scgd	for (; cnt < 15; cnt++) {
2371.1Scgd		stmt1;
2381.1Scgd		stmt2;
2391.1Scgd	}
2401.1Scgd
2411.2Scgd	/* Second level indents are four spaces. */
2421.2Scgd	while (cnt < 20)
2431.16Senami		z = a + really + long + statement + that + needs + two lines +
2441.1Scgd		    gets + indented + four + spaces + on + the + second +
2451.7Senami		    and + subsequent + lines;
2461.1Scgd
2471.1Scgd	/*
2481.2Scgd	 * Closing and opening braces go on the same line as the else.
2491.12Slukem	 * Don't add braces that aren't necessary except in cases where
2501.12Slukem	 * there are ambiguity or readability issues.
2511.1Scgd	 */
2521.12Slukem	if (test) {
2531.12Slukem		/*
2541.12Slukem		 * I have a long comment here.
2551.12Slukem		 */
2561.12Slukem#ifdef zorro
2571.12Slukem		z = 1;
2581.12Slukem#else
2591.12Slukem		b = 3;
2601.12Slukem#endif
2611.12Slukem	} else if (bar) {
2621.1Scgd		stmt;
2631.1Scgd		stmt;
2641.1Scgd	} else
2651.1Scgd		stmt;
2661.12Slukem
2671.2Scgd	/* No spaces after function names. */
2681.12Slukem	if ((result = function(a1, a2, a3, a4)) == NULL)
2691.12Slukem		exit(1);
2701.1Scgd
2711.1Scgd	/*
2721.12Slukem	 * Unary operators don't require spaces, binary operators do.
2731.12Slukem	 * Don't excessively use parenthesis, but they should be used if
2741.9Slukem	 * statement is really confusing without them, such as:
2751.9Slukem	 * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
2761.1Scgd	 */
2771.9Slukem	a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1);
2781.2Scgd	k = !(l & FLAGS);
2791.1Scgd
2801.1Scgd	/*
2811.1Scgd	 * Exits should be 0 on success, and 1 on failure.  Don't denote
2821.1Scgd	 * all the possible exit points, using the integers 1 through 300.
2831.12Slukem	 * Avoid obvious comments such as "Exit 0 on success."
2841.1Scgd	 */
2851.12Slukem	exit(0);
2861.1Scgd}
2871.1Scgd
2881.1Scgd/*
2891.8Ssimonb * The function type must be declared on a line by itself
2901.16Senami * preceding the function.
2911.1Scgd */
2921.1Scgdstatic char *
2931.12Slukemfunction(int a1, int a2, float fl, int a4)
2941.1Scgd{
2951.1Scgd	/*
2961.1Scgd	 * When declaring variables in functions declare them sorted by size,
2971.12Slukem	 * then in alphabetical order; multiple ones per line are okay.
2981.12Slukem	 * Function prototypes should go in the include file "extern.h".
2991.1Scgd	 * If a line overflows reuse the type keyword.
3001.1Scgd	 *
3011.2Scgd	 * DO NOT initialize variables in the declarations.
3021.1Scgd	 */
3031.1Scgd	extern u_char one;
3041.1Scgd	extern char two;
3051.1Scgd	struct foo three, *four;
3061.1Scgd	double five;
3071.12Slukem	int *six, seven;
3081.12Slukem	char *eight, *nine, ten, eleven, twelve, thirteen;
3091.12Slukem	char fourteen, fifteen, sixteen;
3101.1Scgd
3111.1Scgd	/*
3121.1Scgd	 * Casts and sizeof's are not followed by a space.  NULL is any
3131.1Scgd	 * pointer type, and doesn't need to be cast, so use NULL instead
3141.1Scgd	 * of (struct foo *)0 or (struct foo *)NULL.  Also, test pointers
3151.12Slukem	 * against NULL.  I.e. use:
3161.1Scgd	 *
3171.12Slukem	 *	(p = f()) == NULL
3181.1Scgd	 * not:
3191.1Scgd	 *	!(p = f())
3201.2Scgd	 *
3211.12Slukem	 * Don't use `!' for tests unless it's a boolean.
3221.12Slukem	 * E.g. use "if (*p == '\0')", not "if (!*p)".
3231.12Slukem	 *
3241.1Scgd	 * Routines returning void * should not have their return values cast
3251.1Scgd	 * to any pointer type.
3261.2Scgd	 *
3271.2Scgd	 * Use err/warn(3), don't roll your own!
3281.1Scgd	 */
3291.1Scgd	if ((four = malloc(sizeof(struct foo))) == NULL)
3301.2Scgd		err(1, NULL);
3311.1Scgd	if ((six = (int *)overflow()) == NULL)
3321.2Scgd		errx(1, "Number overflowed.");
3331.1Scgd	return (eight);
3341.1Scgd}
3351.1Scgd
3361.2Scgd/*
3371.12Slukem * Use ANSI function declarations.  ANSI function braces look like
3381.12Slukem * old-style (K&R) function braces.
3391.12Slukem * As per the wrapped prototypes, use your discretion on how to format
3401.12Slukem * the subsequent lines.
3411.12Slukem */
3421.12Slukemstatic int
3431.12Slukemdirinfo(const char *p, struct stat *sb, struct dirent *de, struct statfs *sf,
3441.12Slukem	int *rargc, char **rargv[])
3451.12Slukem{	/* Insert an empty line if the function has no local variables. */
3461.12Slukem
3471.14Slukem	if (stat(p, sb) < 0)
3481.14Slukem		err(1, "Unable to stat %s", p);
3491.14Slukem
3501.14Slukem	/*
3511.14Slukem	 * To printf 64 bit quantities, use %ll and cast to (long long).
3521.14Slukem	 */
3531.14Slukem	printf("The size of %s is %lld\n", p, (long long)sb->st_size);
3541.2Scgd}
3551.2Scgd
3561.12Slukem/*
3571.12Slukem * Functions that support variable numbers of arguments should look like this.
3581.12Slukem * (With the #include <stdarg.h> appearing at the top of the file with the
3591.12Slukem * other include files).
3601.12Slukem */
3611.2Scgd#include <stdarg.h>
3621.2Scgd
3631.2Scgdvoid
3641.2Scgdvaf(const char *fmt, ...)
3651.2Scgd{
3661.2Scgd	va_list ap;
3671.12Slukem
3681.2Scgd	va_start(ap, fmt);
3691.2Scgd	STUFF;
3701.12Slukem	va_end(ap);	
3711.12Slukem				/* No return needed for void functions. */
3721.1Scgd}
3731.1Scgd
3741.1Scgdstatic void
3751.12Slukemusage(void)
3761.12Slukem{
3771.1Scgd
3781.1Scgd	/*
3791.1Scgd	 * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
3801.1Scgd	 * usually cleaner, not to mention avoiding stupid bugs.
3811.12Slukem	 * Use snprintf(3) or strlcpy(3)/strlcat(3) instead of sprintf(3);
3821.12Slukem	 * again to avoid stupid bugs.
3831.1Scgd	 *
3841.1Scgd	 * Usage statements should look like the manual pages.  Options w/o
3851.1Scgd	 * operands come first, in alphabetical order inside a single set of
3861.1Scgd	 * braces.  Followed by options with operands, in alphabetical order,
3871.1Scgd	 * each in braces.  Followed by required arguments in the order they
3881.1Scgd	 * are specified, followed by optional arguments in the order they
3891.12Slukem	 * are specified.  A bar (`|') separates either/or options/arguments,
3901.1Scgd	 * and multiple options/arguments which are specified together are
3911.1Scgd	 * placed in a single set of braces.
3921.1Scgd	 *
3931.17Scgd	 * Use getprogname() instead of hardcoding the program name.
3941.12Slukem	 *
3951.1Scgd	 * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
3961.1Scgd	 * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
3971.1Scgd	 */
3981.17Scgd	(void)fprintf(stderr, "usage: %s [-ab]\n", getprogname());
3991.1Scgd	exit(1);
4001.1Scgd}
401