The @command{indent} program can be used to make code easier to read. It can also convert from one style of writing C to another.
@command{indent} understands a substantial amount about the syntax of C, but it also attempts to cope with incomplete and misformed syntax.
In version 1.2 and more recent versions, the GNU style of indenting is the default.
As of version 1.3, the format of the @command{indent} command is:
indent [options] [input-files] indent [options] [single-input-file] [-o output-file]
This format is different from earlier versions and other versions of @command{indent}.
In the first form, one or more input files are specified. @command{indent} makes a backup copy of each file, and the original file is replaced with its indented version. See section Backup Files, for an explanation of how backups are made.
In the second form, only one input file is specified. In this case, or when the standard input is used, you may specify an output file after the @option{-o} option.
To cause @command{indent} to write to standard output, use the @option{-st} option. This is only allowed when there is only one input file, or when the standard input is used.
If no input files are named, the standard input is read for input. Also, if a filename named @option{-} is specified, then the standard input is read.
As an example, each of the following commands will input the program `slithy_toves.c' and write its indented text to `slithy_toves.out':
indent slithy_toves.c -o slithy_toves.out indent -st slithy_toves.c > slithy_toves.out cat slithy_toves.c | indent -o slithy_toves.out
Most other options to @command{indent} control how programs are formatted. As of version 1.2, @command{indent} also recognizes a long name for each option name. Long options are prefixed by either @option{--} or @option{+}.(1) is being superseded by @option{--} to maintain consistency with the POSIX standard.} In most of this document, the traditional, short names are used for the sake of brevity. See section Option Summary, for a list of options, including both long and short names.
Here is another example:
indent -br test/metabolism.c -l85
This will indent the program `test/metabolism.c' using the @option{-br} and @option{-l85} options, write the output back to `test/metabolism.c', and write the original contents of `test/metabolism.c' to a backup file in the directory `test'.
Equivalent invocations using long option names for this example would be:
indent --braces-on-if-line --line-length185 test/metabolism.c indent +braces-on-if-line +line-length185 test/metabolism.c
If you find that you often use @command{indent} with the same options, you may put those options into a file named `.indent.pro'. @command{indent} will first look for `.indent.pro' in the current directory and use that if found. Otherwise, @command{indent} will search your home directory for `.indent.pro' and use that file if it is found. This behaviour is different from that of other versions of @command{indent}, which load both files if they both exist.
The format of `.indent.pro' is simply a list of options, just as they would appear on the command line, separated by white space (tabs, spaces, and newlines). Options in `.indent.pro' may be surrounded by C or C++ comments, in which case they are ignored.
Command line switches are handled after processing `.indent.pro'. Options specified later override arguments specified earlier, with one exception: Explicitly specified options always override background options (see section Common styles). You can prevent @command{indent} from reading an `.indent.pro' file by specifying the @option{-npro} option.
As of version 1.3, GNU @command{indent} makes GNU-style backup files, the same way GNU Emacs does. This means that either simple or numbered backup filenames may be made.
Simple backup file names are generated by appending a suffix to the original file name. The default for this suffix is the one-character string `~' (tilde). Thus, the backup file for `python.c' would be `python.c~'.
Instead of the default, you may specify any string as a suffix by setting the environment variable @env{SIMPLE_BACKUP_SUFFIX} to your preferred suffix.
Numbered backup versions of a file `momeraths.c' look like `momeraths.c.~23~', where 23 is the version of this particular backup. When making a numbered backup of the file `src/momeraths.c', the backup file will be named `src/momeraths.c.~V~', where V is one greater than the highest version currently existing in the directory `src'. The environment variable @env{VERSION_WIDTH} controls the number of digits, using left zero padding when necessary. For instance, setting this variable to "2" will lead to the backup file being named `momeraths.c.~04~'.
The type of backup file made is controlled by the value of the environment variable @env{VERSION_CONTROL}. If it is the string `simple', then only simple backups will be made. If its value is the string `numbered', then numbered backups will be made. If its value is `numbered-existing', then numbered backups will be made if there already exist numbered backups for the file being indented; otherwise, a simple backup is made. If @env{VERSION_CONTROL} is not set, then @command{indent} assumes the behaviour of `numbered-existing'.
Other versions of @command{indent} use the suffix `.BAK' in naming backup files. This behaviour can be emulated by setting @env{SIMPLE_BACKUP_SUFFIX} to `.BAK'.
Note also that other versions of @command{indent} make backups in the current directory, rather than in the directory of the source file as GNU @command{indent} now does.
There are several common styles of C code, including the GNU style, the Kernighan & Ritchie style, and the original Berkeley style. A style may be selected with a single background option, which specifies a set of values for all other options. However, explicitly specified options always override options implied by a background option.
As of version 1.2, the default style of GNU @command{indent} is the GNU style. Thus, it is no longer necessary to specify the option @option{-gnu} to obtain this format, although doing so will not cause an error. Option settings which correspond to the GNU style are:
-nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2 -ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -psl -nsc -nsob
The GNU coding style is that preferred by the GNU project. It is the style that the GNU Emacs C mode encourages and which is used in the C portions of GNU Emacs. (People interested in writing programs for Project GNU should get a copy of The GNU Coding Standards, which also covers semantic and portability issues such as memory usage, the size of integers, etc.)
The Kernighan & Ritchie style is used throughout their well-known book The C Programming Language. It is enabled with the @option{-kr} option. The Kernighan & Ritchie style corresponds to the following set of options:
-nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 -cp33 -cs -d0 -di1 -nfc1 -nfca -hnl -i4 -ip0 -l75 -lp -npcs -npsl -nsc -nsob -nss
Kernighan & Ritchie style does not put comments to the right of code in the same column at all times (nor does it use only one space to the right of the code), so for this style @command{indent} has arbitrarily chosen column 33.
The style of the original Berkeley @command{indent} may be obtained by specifying @option{-orig} (or by specifying @option{--original}, using the long option name). This style is equivalent to the following settings:
-nbad -nbap -bbo -bc -br -brs -c33 -cd33 -cdb -ce -ci4 -cli0 -cp33 -di16 -fc1 -fca -hnl -i4 -ip4 -l75 -lp -npcs -psl -sc -nsob -nss -ts8
Various programming styles use blank lines in different places. @command{indent} has a number of options to insert or delete blank lines in specific places.
The @option{-bad} option causes @command{indent} to force a blank line after every block of declarations. The @option{-nbad} option causes @command{indent} not to force such blank lines.
The @option{-bap} option forces a blank line after every procedure body. The @option{-nbap} option forces no such blank line.
The @option{-bbb} option forces a blank line before every boxed comment, See section Comments. The @option{-nbbb} option does not force such blank lines.
The @option{-sob} option causes @command{indent} to swallow optional blank lines (that is, any optional blank lines present in the input will be removed from the output). If the @option{-nsob} is specified, any blank lines present in the input file will be copied to the output file.
The @option{-bad} option forces a blank line after every block of declarations. The @option{-nbad} option does not add any such blank lines.
For example, given the input
char *foo; char *bar; /* This separates blocks of declarations. */ int baz;
char *foo; char *bar; /* This separates blocks of declarations. */ int baz;
and indent -nbad
produces
char *foo; char *bar; /* This separates blocks of declarations. */ int baz;
The @option{-bap} option forces a blank line after every procedure body.
For example, given the input
int foo () { puts("Hi"); } /* The procedure bar is even less interesting. */ char * bar () { puts("Hello"); }
indent -bap
produces
int foo () { puts ("Hi"); } /* The procedure bar is even less interesting. */ char * bar () { puts ("Hello"); }
and indent -nbap
produces
int foo () { puts ("Hi"); } /* The procedure bar is even less interesting. */ char * bar () { puts ("Hello"); }
No blank line will be added after the procedure foo
.
@command{indent} formats both C and C++ comments. C comments are begun with `/*', terminated with `*/' and may contain newline characters. C++ comments begin with the delimiter `//' and end at the newline.
@command{indent} handles comments differently depending upon their context. @command{indent} attempts to distinguish between comments which follow statements, comments which follow declarations, comments following preprocessor directives, and comments which are not preceded by code of any sort, i.e., they begin the text of the line (although not neccessarily in column 1).
@command{indent} further distinguishes between comments found outside of procedures and aggregates, and those found within them. In particular, comments beginning a line found within a procedure will be indented to the column at which code is currently indented. The exception to this a comment beginning in the leftmost column; such a comment is output at that column.
@command{indent} attempts to leave boxed comments unmodified. The general idea of such a comment is that it is enclosed in a rectangle or "box" of stars or dashes to visually set it apart. More precisely, boxed comments are defined as those in which the initial `/*' is followed immediately by the character `*', `=', `_', or `-', or those in which the beginning comment delimiter (`/*') is on a line by itself, and the following line begins with a `*' in the same column as the star of the opening delimiter.
Examples of boxed comments are:
/********************** * Comment in a box!! * **********************/ /* * A different kind of scent, * for a different kind of comment. */
@command{indent} attempts to leave boxed comments exactly as they are found in the source file. Thus the indentation of the comment is unchanged, and its length is not checked in any way. The only alteration made is that an embedded tab character may be converted into the appropriate number of spaces.
If the @option{-bbb} option is specified, all such boxed comments will be preceded by a blank line, unless such a comment is preceded by code.
Comments which are not boxed comments may be formatted, which means that the line is broken to fit within a right margin and left-filled with whitespace. Single newlines are equivalent to a space, but blank lines (two or more newlines in a row) are taken to mean a paragraph break. Formatting of comments which begin after the first column is enabled with the @option{-fca} option. To format those beginning in column one, specify @option{-fc1}. Such formatting is disabled by default.
The right margin for formatting defaults to 78, but may be changed with the @option{-lc} option. If the margin specified does not allow the comment to be printed, the margin will be automatically extended for the duration of that comment. The margin is not respected if the comment is not being formatted.
If the comment begins a line (i.e., there is no program text to its left), it will be indented to the column it was found in unless the comment is within a block of code. In that case, such a comment will be aligned with the indented code of that block (unless the comment began in the first column). This alignment may be affected by the @option{-d} option, which specifies an amount by which such comments are moved to the left, or unindented. For example, @option{-d2} places comments two spaces to the left of code. By default, comments are aligned with code, unless they begin in the first column, in which case they are left there by default -- to get them aligned with the code, specify @option{-fc1}.
Comments to the right of code will appear by default in column 33.
This may be changed with one of three options. @option{-c} will specify
the column for comments following code, @option{-cd} specifies the
column for comments following declarations, and @option{-cp} specifies
the column for comments following preprocessor directives #else
and #endif
.
If the code to the left of the comment exceeds the beginning column, the comment column will be extended to the next tabstop column past the end of the code, or in the case of preprocessor directives, to one space past the end of the directive. This extension lasts only for the output of that particular comment.
The @option{-cdb} option places the comment delimiters on blank lines.
Thus, a single line comment like /* Loving hug */
can be
transformed into:
/* Loving hug */
Stars can be placed at the beginning of multi-line comments with the @option{-sc} option. Thus, the single-line comment above can be transformed (with @option{-cdb -sc}) into:
/* * Loving hug */
The @option{-br} or @option{-bl} option specifies how to format braces.
The @option{-br} option formats statement braces like this:
if (x > 0) { x--; }
The @option{-bl} option formats them like this:
if (x > 0) { x--; }
If you use the @option{-bl} option, you may also want to specify the @option{-bli} option. This option specifies the number of spaces by which braces are indented. @option{-bli2}, the default, gives the result shown above. @option{-bli0} results in the following:
if (x > 0) { x--; }
If you are using the @option{-br} option, you probably want to also use
the @option{-ce} option. This causes the else
in an if-then-else
construct to cuddle up to the immediately preceding `}'. For
example, with @option{-br -ce} you get the following:
if (x > 0) { x--; } else { fprintf (stderr, "...something wrong?\n"); }
With @option{-br -nce} that code would appear as
if (x > 0) { x--; } else { fprintf (stderr, "...something wrong?\n"); }
The @option{-cli} option specifies the number of spaces that case labels
should be indented to the right of the containing switch
statement.
The default gives code like:
switch (i) { case 0: break; case 1: { ++i; } default: break; }
Using the @option{-cli2} that would become:
switch (i) { case 0: break; case 1: { ++i; } default: break; }
The indentation of the braces below a case statement can be controlled with the @option{-cbin} option. For example, using @option{-cli2 -cbi0} results in:
switch (i) { case 0: break; case 1: { ++i; } default: break; }
If a semicolon is on the same line as a for
or while
statement, the @option{-ss} option will cause a space to be placed before
the semicolon. This emphasizes the semicolon, making it clear that the
body of the for
or while
statement is an empty statement.
@option{-nss} disables this feature.
The @option{-pcs} option causes a space to be placed between the name of
the procedure being called and the `(' (for example, puts
("Hi");
. The @option{-npcs} option would give puts("Hi");
).
If the @option{-cs} option is specified, @command{indent} puts a space after a cast operator.
The @option{-bs} option ensures that there is a space between the
keyword sizeof
and its argument. In some versions, this is
known as the `Bill_Shannon' option.
By default @command{indent} will line up identifiers, in the column specified by the @option{-di} option. For example, @option{-di16} makes things look like:
int foo; char *bar;
Using a small value (such as one or two) for the @option{-di} option can be used to cause the identifiers to be placed in the first available position; for example:
int foo; char *bar;
The value given to the @option{-di} option will still affect variables which are put on separate lines from their types, for example @option{-di2} will lead to:
int foo;
If the @option{-bc} option is specified, a newline is forced after each comma in a declaration. For example,
int a, b, c;
With the @option{-nbc} option this would look like
int a, b, c;
The @option{-psl} option causes the type of a procedure being defined to
be placed on the line before the name of the procedure. This style is
required for the @command{etags} program to work correctly, as well as some
of the c-mode
functions of Emacs.
You must use the @option{-T}
option to tell @command{indent} the name of all the typenames in your
program that are defined by typedef
. @option{-T} can be specified
more than once, and all names specified are used. For example, if your
program contains
typedef unsigned long CODE_ADDR; typedef enum {red, blue, green} COLOR;
you would use the options @option{-T CODE_ADDR -T COLOR}.
The @option{-brs} or @option{-bls} option specifies how to format braces in struct declarations. The @option{-brs} option formats braces like this:
struct foo { int x; };
The @option{-bls} option formats them like this:
struct foo { int x; };
One issue in the formatting of code is how far each line should be
indented from the left margin. When the beginning of a statement such
as if
or for
is encountered, the indentation level is
increased by the value specified by the @option{-i} option. For example,
use @option{-i8} to specify an eight character indentation for each
level. When a statement is broken across two lines, the second line is
indented by a number of additional spaces specified by the @option{-ci}
option. @option{-ci} defaults to 0. However, if the @option{-lp} option is
specified, and a line has a left parenthesis which is not closed on that
line, then continuation lines will be lined up to start at the character
position just after the left parenthesis. This processing also applies
to `[' and applies to `{' when it occurs in initialization
lists. For example, a piece of continued code might look like this with
@option{-nlp -ci3} in effect:
p1 = first_procedure (second_procedure (p2, p3), third_procedure (p4, p5));
With @option{-lp} in effect the code looks somewhat clearer:
p1 = first_procedure (second_procedure (p2, p3), third_procedure (p4, p5));
When a statement is broken in between two or more paren pairs (...), each extra pair causes the indentation level extra indentation:
if ((((i < 2 && k > 0) || p == 0) && q == 1) || n = 0)
The option @option{-ipN} can be used to set the extra offset per paren. For instance, @option{-ip0} would format the above as:
if ((((i < 2 && k > 0) || p == 0) && q == 1) || n = 0)
@command{indent} assumes that tabs are placed at regular intervals of both input and output character streams. These intervals are by default 8 columns wide, but (as of version 1.2) may be changed by the @option{-ts} option. Tabs are treated as the equivalent number of spaces.
The indentation of type declarations in old-style function definitions is controlled by the @option{-ip} parameter. This is a numeric parameter specifying how many spaces to indent type declarations. For example, the default @option{-ip5} makes definitions look like this:
char * create_world (x, y, scale) int x; int y; float scale; { . . . }
For compatibility with other versions of indent, the option @option{-nip} is provided, which is equivalent to @option{-ip0}.
ANSI C allows white space to be placed on preprocessor command lines between the character `#' and the command name. By default, @command{indent} removes this space, but specifying the @option{-lps} option directs @command{indent} to leave this space unmodified.
With the option @option{-ln}, or @option{--line-lengthn}, it is possible to specify the maximum length of a line of C code, not including possible comments that follow it.
When lines become longer then the specified line length, GNU @command{indent} tries to break the line at a logical place. This is new as of version 2.1 however and not very intelligent or flexible yet.
Currently there are two options that allows one to interfere with the algorithm that determines where to break a line.
The @option{-bbo} option causes GNU @command{indent} to prefer to break
long lines before the boolean operators &&
and ||
. The
@option{-nbbo} option causes GNU @command{indent} not have that
preference. For example, the default option @option{-bbo} (together
with @option{--line-length60} and @option{--ignore-newlines}) makes code
look like this:
if (mask && ((mask[0] == '\0') || (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
Using the option @option{-nbbo} will make it look like this:
if (mask && ((mask[0] == '\0') || (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
The default @option{-hnl}, however, honours newlines in the input file by giving them the highest possible priority to break lines at. For example, when the input file looks like this:
if (mask && ((mask[0] == '\0') || (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
then using the option @option{-hnl}, or @option{--honour-newlines}, together with the previously mentioned @option{-nbbo} and @option{--line-length60}, will cause the output not to be what is given in the last example but instead will prefer to break at the positions where the code was broken in the input file:
if (mask && ((mask[0] == '\0') || (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
The idea behind this option is that lines which are too long, but are already broken up, will not be touched by GNU @command{indent}. Really messy code should be run through @command{indent} at least once using the @option{--ignore-newlines} option though.
Formatting of C code may be disabled for portions of a program by
embedding special control comments in the program. To turn off
formatting for a section of a program, place the disabling control
comment /* *INDENT-OFF* */
on a line by itself just before that
section. Program text scanned after this control comment is output
precisely as input with no modifications until the corresponding
enabling comment is scanned on a line by itself. The disabling control
comment is /* *INDENT-ON* */
, and any text following the comment
on the line is also output unformatted. Formatting begins again with
the input line following the enabling control comment.
More precisely, @command{indent} does not attempt to verify the closing
delimiter (*/
) for these C comments, and any whitespace on the
line is totally transparent.
These control comments also function in their C++ formats, namely
// *INDENT-OFF*
and // *INDENT-ON*
.
It should be noted that the internal state of @command{indent} remains unchanged over the course of the unformatted section. Thus, for example, turning off formatting in the middle of a function and continuing it after the end of the function may lead to bizarre results. It is therefore wise to be somewhat modular in selecting code to be left unformatted.
As a historical note, some earlier versions of @command{indent} produced
error messages beginning with *INDENT**
. These versions of
@command{indent} were written to ignore any input text lines which began
with such error messages. I have removed this incestuous feature from
GNU @command{indent}.
To find out what version of @command{indent} you have, use the command
indent -version
. This will report the version number of
@command{indent}, without doing any of the normal processing.
The @option{-v} option can be used to turn on verbose mode. When in verbose mode, @command{indent} reports when it splits one line of input into two more more lines of output, and gives some size statistics at completion.
The @option{-pmt} option causes @command{indent} to preserve the access
and modification times on the output files. Using this option
has the advantage that running indent on all source and header
files in a project won't cause make
to rebuild all targets.
This option is only available on Operating Systems that have the
POSIX utime(2)
function.
When @command{indent} is run twice on a file, with the same profile, it should never change that file the second time. With the current design of @command{indent}, this can not be guaranteed, however, and it has not been extensively tested.
@command{indent} does not understand C. In some cases this leads to the inability to join lines. The result is that running a file through @command{indent} is irreversible, even if the used input file was the result of running @command{indent} with a given profile (`.indent.pro').
While an attempt was made to get @command{indent} working for C++, is will not do a good job on any C++ source except the very simple.
@command{indent} does not look at the given @option{--line-length} option when writing comments to the output file. This results often in comments being put far to the right. In order to prohibit @command{indent} from joining a broken line that has a comment at the end, make sure that the comments start on the first line of the break.
@command{indent} does not count lines and comments (see the @option{-v}
option) when @command{indent} is turned off with
/* *INDENT-OFF* */
.
Comments of the form /*UPPERCASE*/
are not treated as comment but as an
identifier, causing them to be joined with the next line. This renders
comments of this type useless, unless they are embedded in the code to
begin with.
The following copyright notice applies to the @command{indent} program. The copyright and copying permissions for this manual appear near the beginning of `indent.texinfo' and `indent.info', and near the end of `indent.1'.
Copyright (C) 1999 Carlo Wood. Copyright (C) 1995, 1996 Joseph Arceneaux. Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996 Free Software Foundation Copyright (C) 1985 Sun Microsystems, Inc. Copyright (C) 1980 The Regents of the University of California. Copyright (C) 1976 Board of Trustees of the University of Illinois. All rights reserved. Redistribution and use in source and binary forms are permitted provided that the above copyright notice and this paragraph are duplicated in all such forms and that any documentation, advertising materials, and other materials related to such distribution and use acknowledge that the software was developed by the University of California, Berkeley, the University of Illinois, Urbana, and Sun Microsystems, Inc. The name of either University or Sun Microsystems may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Here is a list of all the options for @command{indent}, alphabetized by short option. It is followed by a cross key alphabetized by long option.
if
, etc.struct
declaration lines.if
, etc. and structure declarations.struct
declaration line.sizeof
and its argument.#else
and
#endif
statements in column n.}
and else
.for
and while
statments,
force a blank before the semicolon.Here is a list of options alphabetized by long option, to help you find the corresponding short option.
--blank-lines-after-block-comments -bbb --blank-lines-after-commas -bc --blank-lines-after-declarations -bad --blank-lines-after-procedures -bap --braces-after-if-line -bl --brace-indent -bli --braces-after-struct-decl-line -bls --braces-on-if-line -br --braces-on-struct-decl-line -brs --break-after-boolean-operator -nbbo --break-before-boolean-operator -bbo --case-indentation -clin --case-brace-indentation -cbin --comment-delimiters-on-blank-lines -cdb --comment-indentation -cn --continuation-indentation -cin --continue-at-parentheses -lp --cuddle-else -ce --declaration-comment-column -cdn --declaration-indentation -din --dont-break-procedure-type -npsl --dont-cuddle-else -nce --dont-format-comments -nfca --dont-format-first-column-comments -nfc1 --dont-line-up-parentheses -nlp --dont-space-special-semicolon -nss --dont-star-comments -nsc --else-endif-column -cpn --format-all-comments -fca --format-first-column-comments -fc1 --gnu-style -gnu --honour-newlines -hnl --ignore-newlines -nhnl --ignore-profile -npro --indent-level -in --k-and-r-style -kr --leave-optional-blank-lines -nsob --leave-preprocessor-space -lps --line-comments-indentation -dn --line-length -ln --no-blank-lines-after-commas -nbc --no-blank-lines-after-declarations -nbad --no-blank-lines-after-procedures -nbap --no-comment-delimiters-on-blank-lines -ncdb --no-space-after-casts -ncs --no-parameter-indentation -nip --no-space-after-function-call-names -npcs --no-verbosity -nv --original -orig --parameter-indentation -ipn --paren-indentation -pin --preserve-mtime -pmt --procnames-start-lines -psl --space-after-cast -cs --space-after-procedure-calls -pcs --space-special-semicolon -ss --standard-output -st --start-left-side-of-comments -sc --struct-brace-indentation -sbin --swallow-optional-blank-lines -sob --tab-size -tsn --verbose -v
Jump to: - - . - @ - b - c - g - i - k - l - o - s - t - u
This document was generated on 4 November 1999 using the texi2html translator version 1.52.