xref: /freebsd/usr.bin/indent/indent.1 (revision a9e8641da961bcf3d24afc85fd657f2083a872a2)
1.\" Copyright (c) 1980, 1990, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\" Copyright (c) 1976 Board of Trustees of the University of Illinois.
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"	@(#)indent.1	8.1 (Berkeley) 7/1/93
35.\" $FreeBSD$
36.\"
37.Dd March 3, 2012
38.Dt INDENT 1
39.Os
40.Sh NAME
41.Nm indent
42.Nd indent and format C program source
43.Sh SYNOPSIS
44.Nm
45.Op Ar input-file Op Ar output-file
46.Op Fl bacc | Fl nbacc
47.Op Fl bad | Fl nbad
48.Op Fl bap | Fl nbap
49.Bk -words
50.Op Fl ei | Fl ei
51.Op Fl eei | Fl eei
52.Op Fl bbb | Fl nbbb
53.Ek
54.Op Fl \&bc | Fl nbc
55.Op Fl \&bl
56.Op Fl \&br
57.Op Fl c Ns Ar n
58.Op Fl \&cd Ns Ar n
59.Bk -words
60.Op Fl cdb | Fl ncdb
61.Ek
62.Op Fl \&ce | Fl nce
63.Op Fl \&ci Ns Ar n
64.Op Fl cli Ns Ar n
65.Op Fl d Ns Ar n
66.Op Fl \&di Ns Ar n
67.Bk -words
68.Op Fl fbs | Fl nfbs
69.Op Fl fc1 | Fl nfc1
70.Op Fl fcb | Fl nfcb
71.Ek
72.Op Fl i Ns Ar n
73.Op Fl \&ip | Fl nip
74.Op Fl l Ns Ar n
75.Op Fl \&lc Ns Ar n
76.Op Fl \&ldi Ns Ar n
77.Op Fl \&lp | Fl nlp
78.Op Fl npro
79.Op Fl pcs | Fl npcs
80.Op Fl psl | Fl npsl
81.Op Fl \&sc | Fl nsc
82.Bk -words
83.Op Fl sob | Fl nsob
84.Ek
85.Op Fl \&st
86.Op Fl \&ta
87.Op Fl troff
88.Op Fl ut | Fl nut
89.Op Fl v | Fl \&nv
90.Sh DESCRIPTION
91The
92.Nm
93utility is a
94.Em C
95program formatter.
96It reformats the
97.Em C
98program in the
99.Ar input-file
100according to the switches.
101The switches which can be
102specified are described below.
103They may appear before or after the file
104names.
105.Pp
106.Sy NOTE :
107If you only specify an
108.Ar input-file ,
109the formatting is
110done `in-place', that is, the formatted file is written back into
111.Ar input-file
112and a backup copy of
113.Ar input-file
114is written in the current directory.
115If
116.Ar input-file
117is named
118.Sq Pa /blah/blah/file ,
119the backup file is named
120.Sq Pa file.BAK .
121.Pp
122If
123.Ar output-file
124is specified,
125.Nm
126checks to make sure that it is different from
127.Ar input-file .
128.Pp
129The options listed below control the formatting style imposed by
130.Nm .
131.Bl -tag -width Op
132.It Fl bacc , nbacc
133If
134.Fl bacc
135is specified, a blank line is forced around every conditional
136compilation block.
137For example, in front of every #ifdef and after every #endif.
138Other blank lines surrounding such blocks will be swallowed.
139Default:
140.Fl nbacc  .
141.It Fl bad , nbad
142If
143.Fl bad
144is specified, a blank line is forced after every block of
145declarations.
146Default:
147.Fl nbad .
148.It Fl bap , nbap
149If
150.Fl bap
151is specified, a blank line is forced after every procedure body.
152Default:
153.Fl nbap .
154.It Fl bbb , nbbb
155If
156.Fl bbb
157is specified, a blank line is forced before every block comment.
158Default:
159.Fl nbbb .
160.It Fl \&bc , nbc
161If
162.Fl \&bc
163is specified, then a newline is forced after each comma in a declaration.
164.Fl nbc
165turns off this option.
166Default:
167.Fl \&nbc .
168.It Fl \&br , \&bl
169Specifying
170.Fl \&bl
171lines-up compound statements like this:
172.Bd -literal -offset indent
173if (...)
174{
175  code
176}
177.Ed
178.Pp
179Specifying
180.Fl \&br
181(the default) makes them look like this:
182.Bd -literal -offset indent
183if (...) {
184  code
185}
186.Ed
187.It Fl c Ns Ar n
188The column in which comments on code start.
189The default is 33.
190.It Fl cd Ns Ar n
191The column in which comments on declarations start.
192The default
193is for these comments to start in the same column as those on code.
194.It Fl cdb , ncdb
195Enables (disables) the placement of comment delimiters on blank lines.
196With
197this option enabled, comments look like this:
198.Bd -literal -offset indent
199	/*
200	 * this is a comment
201	 */
202.Ed
203.Pp
204Rather than like this:
205.Bd -literal -offset indent
206	/* this is a comment */
207.Ed
208.Pp
209This only affects block comments, not comments to the right of
210code.
211The default is
212.Fl cdb .
213.It Fl ce , nce
214Enables (disables) forcing of `else's to cuddle up to the immediately preceding
215`}'.
216The default is
217.Fl \&ce .
218.It Fl \&ci Ns Ar n
219Sets the continuation indent to be
220.Ar n .
221Continuation
222lines will be indented that far from the beginning of the first line of the
223statement.
224Parenthesized expressions have extra indentation added to
225indicate the nesting, unless
226.Fl \&lp
227is in effect
228or the continuation indent is exactly half of the main indent.
229.Fl \&ci
230defaults to the same value as
231.Fl i .
232.It Fl cli Ns Ar n
233Causes case labels to be indented
234.Ar n
235tab stops to the right of the containing
236.Ic switch
237statement.
238.Fl cli0.5
239causes case labels to be indented half a tab stop.
240The
241default is
242.Fl cli0 .
243.It Fl d Ns Ar n
244Controls the placement of comments which are not to the
245right of code.
246For example,
247.Fl \&d\&1
248means that such comments are placed one indentation level to the
249left of code.
250Specifying the default
251.Fl \&d\&0
252lines-up these comments with the code.
253See the section on comment
254indentation below.
255.It Fl \&di Ns Ar n
256Specifies the indentation, in character positions,
257of global variable names and all struct/union member names
258relative to the beginning of their type declaration.
259The default is
260.Fl di16 .
261.It Fl dj , ndj
262.Fl \&dj
263left justifies declarations.
264.Fl ndj
265indents declarations the same as code.
266The default is
267.Fl ndj .
268.It Fl \&ei , nei
269Enables (disables) special
270.Ic else-if
271processing.
272If it is enabled, an
273.Ic if
274following an
275.Ic else
276will have the same indentation as the preceding
277.Ic \&if
278statement.
279The default is
280.Fl ei .
281.It Fl eei , neei
282Enables (disables) extra indentation on continuation lines of
283the expression part of
284.Ic if
285and
286.Ic while
287statements.
288These continuation lines will be indented one extra level.
289The default is
290.Fl neei .
291.It Fl fbs , nfbs
292Enables (disables) splitting the function declaration and opening brace
293across two lines.
294The default is
295.Fl fbs .
296.It Fl fc1 , nfc1
297Enables (disables) the formatting of comments that start in column 1.
298Often, comments whose leading `/' is in column 1 have been carefully
299hand formatted by the programmer.
300In such cases,
301.Fl nfc1
302should be
303used.
304The default is
305.Fl fc1 .
306.It Fl fcb , nfcb
307Enables (disables) the formatting of block comments (ones that begin
308with `/*\\n').
309Often, block comments have been not so carefully hand formatted by the
310programmer, but reformatting that would just change the line breaks is not
311wanted.
312In such cases,
313.Fl nfcb
314should be used.
315Block comments are then handled like box comments.
316The default is
317.Fl fcb .
318.It Fl i Ns Ar n
319The number of spaces for one indentation level.
320The default is 8.
321.It Fl \&ip , nip
322Enables (disables) the indentation of parameter declarations from the left
323margin.
324The default is
325.Fl \&ip .
326.It Fl l Ns Ar n
327Maximum length of an output line.
328The default is 78.
329.It Fl \&ldi Ns Ar n
330Specifies the indentation, in character positions,
331of local variable names
332relative to the beginning of their type declaration.
333The default is for local variable names to be indented
334by the same amount as global ones.
335.It Fl \&lp , nlp
336Lines-up code surrounded by parenthesis in continuation lines.
337If a line
338has a left paren which is not closed on that line, then continuation lines
339will be lined up to start at the character position just after the left
340paren.
341For example, here is how a piece of continued code looks with
342.Fl nlp
343in effect:
344.Bd -literal -offset indent
345p1 = first_procedure(second_procedure(p2, p3),
346\ \ third_procedure(p4, p5));
347.Ed
348.Pp
349With
350.Fl lp
351in effect (the default) the code looks somewhat clearer:
352.Bd -literal -offset indent
353p1\ =\ first_procedure(second_procedure(p2,\ p3),
354\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,\ p5));
355.Ed
356.Pp
357Inserting two more newlines we get:
358.Bd -literal -offset indent
359p1\ =\ first_procedure(second_procedure(p2,
360\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3),
361\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,
362\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5));
363.Ed
364.It Fl npro
365Causes the profile files,
366.Sq Pa ./.indent.pro
367and
368.Sq Pa ~/.indent.pro ,
369to be ignored.
370.It Fl pcs , npcs
371If true
372.Pq Fl pcs
373all procedure calls will have a space inserted between
374the name and the `('.
375The default is
376.Fl npcs .
377.It Fl psl , npsl
378If true
379.Pq Fl psl
380the names of procedures being defined are placed in
381column 1 \- their types, if any, will be left on the previous lines.
382The
383default is
384.Fl psl .
385.It Fl \&sc , nsc
386Enables (disables) the placement of asterisks (`*'s) at the left edge of all
387comments.
388The default is
389.Fl sc .
390.It Fl sob , nsob
391If
392.Fl sob
393is specified, indent will swallow optional blank lines.
394You can use this to
395get rid of blank lines after declarations.
396Default:
397.Fl nsob .
398.It Fl \&st
399Causes
400.Nm
401to take its input from stdin and put its output to stdout.
402.It Fl ta
403Automatically add all identifiers ending in "_t" to the list
404of type keywords.
405.It Fl T Ns Ar typename
406Adds
407.Ar typename
408to the list of type keywords.
409Names accumulate:
410.Fl T
411can be specified more than once.
412You need to specify all the typenames that
413appear in your program that are defined by
414.Ic typedef
415\- nothing will be
416harmed if you miss a few, but the program will not be formatted as nicely as
417it should.
418This sounds like a painful thing to have to do, but it is really
419a symptom of a problem in C:
420.Ic typedef
421causes a syntactic change in the
422language and
423.Nm
424cannot find all
425instances of
426.Ic typedef .
427.It Fl troff
428Causes
429.Nm
430to format the program for processing by
431.Xr troff 1 .
432It will produce a fancy
433listing in much the same spirit as
434.Xr vgrind 1 .
435If the output file is not specified, the default is standard output,
436rather than formatting in place.
437.It Fl ut , nut
438Enables (disables) the use of tab characters in the output.
439Tabs are assumed to be aligned on columns divisible by 8.
440The default is
441.Fl ut .
442.It Fl v , \&nv
443.Fl v
444turns on `verbose' mode;
445.Fl \&nv
446turns it off.
447When in verbose mode,
448.Nm
449reports when it splits one line of input into two or more lines of output,
450and gives some size statistics at completion.
451The default is
452.Fl \&nv .
453.El
454.Pp
455You may set up your own `profile' of defaults to
456.Nm
457by creating a file called
458.Pa .indent.pro
459in your login directory and/or the current directory and including
460whatever switches you like.
461A `.indent.pro' in the current directory takes
462precedence over the one in your login directory.
463If
464.Nm
465is run and a profile file exists, then it is read to set up the program's
466defaults.
467Switches on the command line, though, always override profile
468switches.
469The switches should be separated by spaces, tabs or newlines.
470.Pp
471.Ss Comments
472.Sq Em Box
473.Em comments .
474The
475.Nm
476utility
477assumes that any comment with a dash or star immediately after the start of
478comment (that is, `/*\-' or `/**') is a comment surrounded by a box of stars.
479Each line of such a comment is left unchanged, except that its indentation
480may be adjusted to account for the change in indentation of the first line
481of the comment.
482.Pp
483.Em Straight text .
484All other comments are treated as straight text.
485The
486.Nm
487utility fits as many words (separated by blanks, tabs, or newlines) on a
488line as possible.
489Blank lines break paragraphs.
490.Ss Comment indentation
491If a comment is on a line with code it is started in the `comment column',
492which is set by the
493.Fl c Ns Ns Ar n
494command line parameter.
495Otherwise, the comment is started at
496.Ar n
497indentation levels less than where code is currently being placed, where
498.Ar n
499is specified by the
500.Fl d Ns Ns Ar n
501command line parameter.
502If the code on a line extends past the comment
503column, the comment starts further to the right, and the right margin may be
504automatically extended in extreme cases.
505.Ss Preprocessor lines
506In general,
507.Nm
508leaves preprocessor lines alone.
509The only
510reformatting that it will do is to straighten up trailing comments.
511It
512leaves embedded comments alone.
513Conditional compilation
514.Pq Ic #ifdef...#endif
515is recognized and
516.Nm
517attempts to correctly
518compensate for the syntactic peculiarities introduced.
519.Ss C syntax
520The
521.Nm
522utility understands a substantial amount about the syntax of C, but it
523has a `forgiving' parser.
524It attempts to cope with the usual sorts of
525incomplete and misformed syntax.
526In particular, the use of macros like:
527.Pp
528.Dl #define forever for(;;)
529.Pp
530is handled properly.
531.Sh ENVIRONMENT
532The
533.Nm
534utility uses the
535.Ev HOME
536environment variable.
537.Sh FILES
538.Bl -tag -width "./.indent.pro" -compact
539.It Pa ./.indent.pro
540profile file
541.It Pa ~/.indent.pro
542profile file
543.El
544.Sh HISTORY
545The
546.Nm
547command appeared in
548.Bx 4.2 .
549.Sh BUGS
550The
551.Nm
552utility has even more switches than
553.Xr ls 1 .
554.Pp
555A common mistake is to try to indent all the
556.Em C
557programs in a directory by typing:
558.Pp
559.Dl indent *.c
560.Pp
561This is probably a bug, not a feature.
562