style guide

This commit is contained in:
cgd 1994-03-26 03:24:50 +00:00
parent eac55eb1b3
commit c9621c279c
3 changed files with 152 additions and 48 deletions

View File

@ -1,7 +1,7 @@
# from: @(#)Makefile 5.13 (Berkeley) 5/7/91
# $Id: Makefile,v 1.6 1994/03/08 07:15:21 cgd Exp $
# $Id: Makefile,v 1.7 1994/03/26 03:24:50 cgd Exp $
FILES= airport ascii birthtoken eqnchar inter.phone man.template \
FILES= airport ascii birthtoken eqnchar getopt inter.phone man.template \
mdoc.template na.phone operator style zipcodes
NOOBJ= noobj

38
share/misc/getopt Normal file
View File

@ -0,0 +1,38 @@
/*
* Main/getopt(3) fragment.
*
* from: @(#)getopt 5.2 (Berkeley) 3/17/94
* $Id: getopt,v 1.1 1994/03/26 03:24:53 cgd Exp $
*/
#include <sys/types.h>
#include <stdlib.h>
void usage __P((void));
int
main(argc, argv)
int argc;
char *argv[];
{
int ch;
while ((ch = getopt(argc, argv, "")) != EOF)
switch (ch) {
case '':
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
}
void
usage()
{
(void)fprintf(stderr, "usage: program:\n");
exit(1);
}

View File

@ -1,8 +1,8 @@
/*
* Style guide for BSD's KNF (Kernel Normal Form).
* Style guide for the 4BSD KNF (Kernel Normal Form).
*
* from: @(#)style 1.10 (Berkeley) 2/11/92
* $Id: style,v 1.1 1993/08/06 07:30:52 cgd Exp $
* from: @(#)style 1.12 (Berkeley) 3/18/94
* $Id: style,v 1.2 1994/03/26 03:24:54 cgd Exp $
*/
/*
@ -16,20 +16,40 @@
* them so they look like real paragraphs.
*/
/* Include files go at the top of the source module. */
#include <stdio.h> /* Non-local includes in brackets. */
/*
* Kernel include files come first; normally, you'll need <sys/types.h>
* OR <sys/param.h>, but not both! <sys/types.h> includes <sys/cdefs.h>,
* and it's okay to depend on that.
*/
#include <sys/types.h> /* Non-local includes in brackets. */
/* If it's a network program, put the network include files next. */
#include <net/if.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <netinet/in.h>
#include <protocols/rwhod.h>
/*
* Then there's a blank line, followed by the /usr include files.
* The /usr include files should be sorted!
*/
#include <stdio.h>
/*
* Global pathnames are defined in /usr/include/paths.h. Pathnames local
* to the program go in pathnames.h in the local directory.
*/
#include <paths.h> /* Non-local includes in brackets. */
#include "pathnames.h" /* Local includes in quotes. */
#include <paths.h>
/* Then, there's a blank line, and the user include files. */
#include "pathnames.h" /* Local includes in double quotes. */
/*
* All ANSI function decls go at the top of the source module. Use the
* __P macro from include file <sys/cdefs.h>. Only the kernel has a name
* associated with the types, i.e. in the kernel use:
* ANSI function declarations for private functions (i.e. functions not used
* elsewhere) go at the top of the source module. Use the __P macro from
* the include file <sys/cdefs.h>. Only the kernel has a name associated with
* the types, i.e. in the kernel use:
*
* void function __P((int a));
*
@ -37,18 +57,19 @@
*
* void function __P((int));
*/
void function __P((int, const char *));
static char *function __P((int, const char *));
static void usage __P((void));
/*
* Macros are capitalized, parenthesized, and should avoid side-effects.
* If they are an inline expansion of a function, the function is defined
* all in lowercase, the macro has the same name all in uppercase. If the
* macro needs more than a single line, use braces. Put a space before
* the backslashes.
* macro needs more than a single line, use braces. Right-justify the
* backslashes, it makes it easier to read.
*/
#define MACRO(x, y) { \
variable = (x) + (y); \
line two; \
#define MACRO(x, y) { \
variable = (x) + (y); \
(y) += 2; \
}
/* Enum types are capitalized. */
@ -60,9 +81,9 @@ enum enumtype { ONE, TWO } et;
* doesn't apply, but there are exceptions. Each one gets its own line.
* Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
*
* Major structures should be declared at the top of the file they are
* used in, or in separate header files, if they are used in multiple
* source files. Use of the structures should be by separate declarations
* Major structures should be declared at the top of the file in which they
* are used, or in separate header files, if they are used in multiple
* source files. Use of the structures should be by separate declarations
* and should be "extern" if they are declared in a header file.
*/
struct foo {
@ -71,12 +92,18 @@ struct foo {
int bar;
};
struct foo *foohead; /* Head of global foo list */
/* Make the structure name match the typedef. */
typedef struct _bar {
int level;
} BAR;
/*
* All major routines should have a comment briefly describing what
* they do. The comment before the "main" routine should describe
* they do. The comment before the "main" routine should describe
* what the program does.
*/
int
main(argc, argv)
int argc;
char *argv[];
@ -88,11 +115,12 @@ main(argc, argv)
char *ep;
/*
* For consistency, getopt should be used to parse options.
* Options should be sorted in the getopt call and the switch
* statement, unless they fall through. Elements in a switch
* statement that fall through should have a FALLTHROUGH comment.
* Numerical arguments should be checked for accuracy.
* For consistency, getopt should be used to parse options. Options
* should be sorted in the getopt call and the switch statement, unless
* parts of the switch cascade. Elements in a switch statement that
* cascade should have a FALLTHROUGH comment. Numerical arguments
* should be checked for accuracy. Code that cannot be reached should
* have a NOTREACHED comment.
*/
while ((ch = getopt(argc, argv, "abn")) != EOF)
switch (ch) { /* Indent the switch. */
@ -104,44 +132,45 @@ main(argc, argv)
break;
case 'n':
num = strtol(optarg, &ep, 10);
if (num <= 0 || *ep)
if (num <= 0 || *ep != '\0')
err("illegal number -- %s", optarg);
break;
case '?':
default:
usage();
/* NOTREACHED */
}
argc -= optind;
argv += optind;
/*
* Space after keywords (while, for, return, switch). No braces are
* used for single statement block.
* used for control statements with zero or only a single statement.
*
* Forever loops are done with for's, not while's.
*/
for (p = buf; *p != '\0'; ++p);
for (;;)
stmt;
/*
* Parts of a for loop may be left empty. Avoid declarations in
* blocks unless the routine is unusually complicated.
* Parts of a for loop may be left empty. Don't put declarations
* inside blocks unless the routine is unusually complicated.
*/
for (; cnt < 15; cnt++) {
stmt1;
stmt2;
}
while (cnt < 20) {
stmt1; /* Second level indents are four spaces. */
/* Second level indents are four spaces. */
while (cnt < 20)
z = a + really + long + statment + that + needs + two lines +
gets + indented + four + spaces + on + the + second +
and + subsequent + lines.
}
/*
* Try to put shorter part first. The closing and opening braces
* go on the same line as the else.
* Closing and opening braces go on the same line as the else.
* Don't add braces that aren't necessary.
*/
if (test)
stmt;
@ -151,17 +180,17 @@ main(argc, argv)
} else
stmt;
/* No space after function names. */
/* No spaces after function names. */
if (error = function(a1, a2))
exit(error);
/*
* Unary operators do not require spaces, binary operators do.
* Try not to use too many parenthesis unless the statement is
* really confusing without them.
* Unary operators don't require spaces, binary operators do. Don't
* use parenthesis unless they're required for precedence, or the
* statement is really confusing without them.
*/
a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
k = l & FLAGS;
k = !(l & FLAGS);
/*
* Exits should be 0 on success, and 1 on failure. Don't denote
@ -175,9 +204,9 @@ main(argc, argv)
* by itself preceeding the function.
*/
static char *
function(a1, a2, a3, a4)
int a1, a2, a4; /* Declare ints too. */
float a3; /* List in order declared, as much as possible. */
function(a1, a2, fl, a4)
int a1, a2, a4; /* Declare ints, too, don't default them. */
float fl; /* List in order declared, as much as possible. */
{
/*
* When declaring variables in functions declare them sorted by size,
@ -186,7 +215,7 @@ function(a1, a2, a3, a4)
* function declarations should go in the include file "externs.h".
* If a line overflows reuse the type keyword.
*
* Try not to initialize variables in the declarations.
* DO NOT initialize variables in the declarations.
*/
extern u_char one;
extern char two;
@ -206,23 +235,60 @@ function(a1, a2, a3, a4)
* (p = f()) == NULL
* not:
* !(p = f())
*
* Don't use '!' for tests unless it's a boolean, e.g. use
* "if (*p == '\0')", not "if (!*p)".
*
* Routines returning void * should not have their return values cast
* to any pointer type.
*
* Use err/warn(3), don't roll your own!
*/
if ((four = malloc(sizeof(struct foo))) == NULL)
return (NULL);
err(1, NULL);
if ((six = (int *)overflow()) == NULL)
return (NULL);
errx(1, "Number overflowed.");
return (eight);
}
/* ANSI function braces look like regular function braces. */
/*
* Don't use ANSI function declarations unless you absolutely have too,
* i.e. you're declaring functions with variable numbers of arguments.
*
* ANSI function braces look like regular function braces.
*/
function(int a1, int a2)
{
...
}
/* Variable numbers of arguments should look like this. */
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
void
#if __STDC__
vaf(const char *fmt, ...)
#else
vaf(fmt, va_alist)
char *fmt;
va_dcl
#endif
{
va_list ap;
#if __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
STUFF;
va_end(ap); /* No return needed for void functions. */
}
static void
usage()
{ /* Insert an empty line if the function has no local variables. */