indent Command

Purpose

Reformats a C language program.

Syntax

indent InputFile [ OutputFile ] [ -nbad | -bad ] [ -nbap | -bap ] [ -nbbb | -bbb ] [ -nbc | -bc ] [ -br | -bl] [ -cn] [ -cdn ] [ -ncdb | -cdb ] [ -nce | -ce ] [ -cin ] [ -clin ] [ -dn ] [ -din ] [ -ndj | -dj ] [ -nei | -ei ] [ -fa ] [ -nfa ] [ -nfc1 | -fc1 ] [ -in ] [ -nip | -ip ] [ -ln ] [ -lcn ] [ -nlp | -lp ] [ -npro ] [ -npcs | -pcs ] [ -nps | -ps ] [ -npsl | -psl ] [ -nsc | -sc ] [ -nsob | -sob ] [ -nslb | -slb ] [ -st ] [ -troff ] [ -nv | -v ] [ -TType ] ...

Description

The indent command reformats a C program as specified by flags entered with the command.

If you only specify the InputFile parameter, the reformatted file is written back into the InputFile parameter and a backup copy of the InputFile parameter is written in the current directory with a .BAK filename suffix.

If you specify the OutputFile parameter, the indent command checks to make sure its name is different from the InputFile parameter.

To set up your own profile of defaults for the indent command, create a file called .indent.pro in your login directory or the current directory. In this file, include as many flags as desired, separated by spaces, tabs, or new lines.

Flags in the .indent.pro file in the current directory override those in your login directory (with the exception of -TType flags, which accumulate). If the indent command is run and a profile file exists, the profile file is read to set up the defaults of the program. Flags on the command line, however, override profile flags.

Comment Handling

The indent command assumes that any comment with a - (dash) or * (asterisk) immediately after the start of a comment marker (/*- or /**) is a comment surrounded by asterisks. Each line of the comment is left unchanged, except for its indentation. This indentation can be adjusted to account for the change in indentation of the first line of the comment.

All other comments are treated as text. The indent command fits as many words (separated by blanks, tabs, or new-lines) on a line as possible. Blank lines break paragraphs.

A block comment is a comment that is not to the right of the code, and extends for more than one line.

If a comment is on a line with code, it is started in the comment column set by the -cn flag. Otherwise, the comment is started at n indentation levels less than where code is currently being placed, where n is specified by the -dn flag. If the code on a line extends past the comment column, the comment starts further to the right. The right margin can be extended automatically in extreme cases.

Preprocessor Lines Handling

In general, the indent command leaves preprocessor lines alone. The only reformatting it does is to straighten up trailing comments. It leaves embedded comments alone. Conditional compilation (code between #ifdef and #endif lines) is recognized and the indent command attempts to compensate correctly for the syntactic peculiarities introduced.

C Syntax Handling

The parser built into the indent command attempts to cope with incomplete and misformed syntax. In particular, the use of macros like:

#define forever for(;;)

is handled properly. For best results, use the indent command on source that is syntactically correct.

Flags

Note: Flags can appear before or after file names.
Item Description
-bad Forces a blank line after every block of declarations.
-nbad Suppresses a blank line after every block of declarations; active unless turned off with the -bad flag.
-bap Forces a blank line after every procedure body.
-nbap Suppresses a blank line after every procedure body; active unless turned off with the -bap flag.
-bbb Forces a blank line before every block comment.
-nbbb Suppresses a blank line before every block comment; active unless turned off with the -bbb flag.
-bc Forces a new line after each comma in a declaration.
-nbc Suppresses a new line after each comma in a declaration; active unless turned off with the -bc flag.
-bl Formats compound statements, structure initializations, and enum initializations, as follows:
if (...)
{
   code
}
-br Formats compound statements, structure initializations, and enum initializations, as follows:
 if (...) {
    code
}

This flag is active unless turned off with the -bl flag.

-cn Sets the initial tab position for comments on code to the n variable. The default value is 33.
-cdn Sets the initial tab position for comments on declarations to the n variable. By default, this flag uses the value defined with the -c flag.
-cdb Enables placing comment delimiters on blank lines; active unless turned off with the -ncdb flag. The -cdb flag affects only block comments, not comments to the right of code. Resulting comments look like the following:
/*
 * this is a comment
 */
-ncdb Disables placing comment delimiters on blank lines. The -ncdb flag affects only block comments, not comments to the right of code. Resulting comments look like the following:
/* this is a comment */
-ce Enables forcing else statements to follow the immediately preceding } (left bracket); active unless turned off with the -nce flag.
-nce Disables forcing else statements to follow the immediately preceding } (left bracket).
-cin Indents the continuation lines n positions from the beginning of the first line of the statement. Expressions in parentheses have extra indentation added to indicate the nesting, unless the -lp flag is in effect. By default, this flag uses the value defined by the -i flag.
-clin Indents the case labels n positions to the right of the containing flag statement. Entering -cli0.5 causes case labels to be indented half a tab stop. This option is the only one that takes a fractional argument. By default, the value is -cli0.
-dn Controls the placement of comments that are not to the right of code with the n variable. Specifying the -d1 flag causes such comments to appear one indention level to the left of code. By default, this flag uses -d0 and comments are aligned with code. The location of comment lines relative to program code affects the comment indention.
-din Specifies the number of positions to indent an identifier from a preceding declaration keyword with the n variable. By default, this flag uses -di16.
-dj Left-justifies declarations.
-ndj Indents declarations; active unless turned off with the -dj flag.
-ei Enables special else-if processing; active unless turned off with the -nei flag. The -ei flag causes if statements following else statements to have the same indentation as the preceding if statement.
-nei Disables special else-if processing.
-fa Flips assign operators from old style C code to the ANSI format. This flag remains active unless turned off with the -nfa flag.
Attention: The possibility of changing the meaning of the code exists if the code was meant for the ANSI compiler. For example, A=-B becomes A-=B.
Note: Use no spaces between operators. If the user means subtraction, then the flipping is necessary; on the other hand, if the user means A equals the negative of B, the flipping alters the meaning.
-nfa Suppresses flipping the operators. Use this flag if the code is written for an ANSI compiler.
-fc1 Enables formatting comments that start in column 1; active unless turned off with the -nfc1 flag.
-nfc1 Disables formatting comments that start in column 1.
-in Sets the indentation level size. By default, the level size is 8 positions.
-ip Enables indenting parameter declarations; active unless turned off with the -nip flag.
-nip Disables indenting parameter declarations.
Item Description
-ln Sets the maximum column position of comments that are to the right of the code. If the comment does not fit on a line, a maximum of 25 characters are printed.
-lcn Sets the maximum line length for block comments to the n variable. By default, this flag uses the length specified with the -l flag.
-lp Aligns code surrounded by parentheses in continuation lines; active unless turned off with the -nlp flag. If a line has a left parenthesis with no matching right parenthesis on that line, continuation lines start at the position following the left parenthesis.

With the -lp flag in effect, such lines appear as follows:

p1 = first_procedure(second_procedure(p2,p3),
                     third_procedure(p4,p5));

Inserting two more new lines yields the following:

p1 = first_procedure(second_procedure(p2,
                                      p3),
                     third_procedure(p4,
                                     p5));
-nlp Leaves code surrounded by parentheses in continuation lines unaligned. With the -nlp flag in effect, such lines appear as follows:
p1 = first_procedure(second_procedure(p2,p3),
         third_procedure(p4, p5));
-npro Causes the profile files ./.indent.pro and $HOME/.indent.pro to be ignored.
-pcs Inserts a space between each procedure call name and the following ( (left parenthesis).
-npcs Suppresses a space between each procedure call name and the following ( (left parenthesis); active unless turned off with the -pcs flag.
-ps Inserts spaces on both sides of the pointer following the -> operator.
-nps Suppresses spaces on both sides of the pointer following the -> operator; active unless turned off with the -ps flag.
-psl Left-justifies the names of procedures being defined; active unless turned off with the -npsl flag. The procedure types, if any, remain on the previous lines.
-npsl Disables left-justification of names of defined procedures.
-sc Enables the placement of * (asterisks) to the left of comments; active unless turned off with the -nsc flag.
-nsc Disables the placement of * (asterisks) to the left of comments.
-slb Treats any single-line comment that is not to the right of the code as a block comment.
-nslb Disables treating any single-line comment that is not to the right of the code as a block comment; active unless turned off with the -slb flag.
-sob Removes optional blank lines. Works in combination with any of the following flags: -nbad, -nbap, or -nbbb. Removes only blank lines that were inserted by the -bad, -bap, or -bbb flags.
-nsob Retains optional blank lines; active unless turned off with the -sob flag.
-st Causes the indent command to take its input from stdin and output to stdout.
-TType Adds the Type variable to the list of type keywords. Names accumulate so -T can be specified more than once. You should specify all the types appearing in your program defined by typedef statements to produce the best output from the indent command.
-troff Formats the C program for processing by troff. Produces a listing similar to listings produced by the vgrind command. If no output file is specified, the default is standard output, rather than formatting in place.
-v Turns on verbose mode, which reports when one line of input is split into two or more lines of output and gives size statistics at completion.
-nv Turns off verbose mode; active unless turned off with the -v flag.

Examples

  1. To format the test.c file using the default settings of the indent command and place the output into the newtest.c file, enter:
    indent test.c newtest.c
  2. To format the test.c file so that a blank line is forced after every block of declarations and procedure body, use all other default settings, and store the output in the newtest.c file, enter:
    indent test.c newtest.c -bad -bap
  3. To format the test.c file using the default settings of the indent command and to define uint as a type keyword recognizable to the indent command, enter:
    indent test.c newtest.c -Tuint

Files

Item Description
./.indent.pro Contains the profile file.
$HOME/.indent.pro Contains the profile file.
/usr/ccs/bin/indent Contains the indent command.