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