xref: /freebsd/usr.bin/indent/indent.1 (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
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 June 29, 2004
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 bad | Fl nbad
47.Op Fl bap | Fl nbap
48.Bk -words
49.Op Fl bbb | Fl nbbb
50.Ek
51.Op Fl \&bc | Fl nbc
52.Op Fl \&bl
53.Op Fl \&br
54.Op Fl c Ns Ar n
55.Op Fl \&cd Ns Ar n
56.Bk -words
57.Op Fl cdb | Fl ncdb
58.Ek
59.Op Fl \&ce | Fl nce
60.Op Fl \&ci Ns Ar n
61.Op Fl cli Ns Ar n
62.Op Fl d Ns Ar n
63.Op Fl \&di Ns Ar n
64.Bk -words
65.Op Fl fbs | Fl nfbs
66.Op Fl fc1 | Fl nfc1
67.Op Fl fcb | Fl nfcb
68.Ek
69.Op Fl i Ns Ar n
70.Op Fl \&ip | Fl nip
71.Op Fl l Ns Ar n
72.Op Fl \&lc Ns Ar n
73.Op Fl \&ldi Ns Ar n
74.Op Fl \&lp | Fl nlp
75.Op Fl npro
76.Op Fl pcs | Fl npcs
77.Op Fl psl | Fl npsl
78.Op Fl \&sc | Fl nsc
79.Bk -words
80.Op Fl sob | Fl nsob
81.Ek
82.Op Fl \&st
83.Op Fl troff
84.Op Fl ut | Fl nut
85.Op Fl v | Fl \&nv
86.Sh DESCRIPTION
87The
88.Nm
89utility is a
90.Em C
91program formatter.  It reformats the
92.Em C
93program in the
94.Ar input-file
95according to the switches.  The switches which can be
96specified are described below.  They may appear before or after the file
97names.
98.Pp
99.Sy NOTE  :
100If you only specify an
101.Ar input-file  ,
102the formatting is
103done `in-place', that is, the formatted file is written back into
104.Ar input-file
105and a backup copy of
106.Ar input-file
107is written in the current directory.  If
108.Ar input-file
109is named
110.Sq Pa /blah/blah/file ,
111the backup file is named
112.Pa file.BAK .
113.Pp
114If
115.Ar output-file
116is specified,
117.Nm
118checks to make sure it is different from
119.Ar input-file  .
120.Pp
121The options listed below control the formatting style imposed by
122.Nm .
123.Bl -tag -width Op
124.It Fl bad , nbad
125If
126.Fl bad
127is specified, a blank line is forced after every block of
128declarations.  Default:
129.Fl nbad  .
130.It Fl bap , nbap
131If
132.Fl bap
133is specified, a blank line is forced after every procedure body.  Default:
134.Fl nbap .
135.It Fl bbb , nbbb
136If
137.Fl bbb
138is specified, a blank line is forced before every block comment.  Default:
139.Fl nbbb .
140.It Fl \&bc , nbc
141If
142.Fl \&bc
143is specified, then a newline is forced after each comma in a declaration.
144.Fl nbc
145turns off this option.  Default:
146.Fl \&nbc  .
147.It Fl \&br , \&bl
148Specifying
149.Fl \&bl
150lines-up compound statements like this:
151.Bd -literal -offset indent
152if (...)
153{
154  code
155}
156.Ed
157.Pp
158Specifying
159.Fl \&br
160(the default) makes them look like this:
161.Bd -literal -offset indent
162if (...) {
163  code
164}
165.Ed
166.Pp
167.It Fl c Ns Ar n
168The column in which comments on code start.  The default is 33.
169.It Fl cd Ns Ar n
170The column in which comments on declarations start.  The default
171is for these comments to start in the same column as those on code.
172.It Fl cdb , ncdb
173Enables (disables) the placement of comment delimiters on blank lines.  With
174this option enabled, comments look like this:
175.Bd -literal -offset indent
176	/*
177	 * this is a comment
178	 */
179.Ed
180.Pp
181Rather than like this:
182.Bd -literal -offset indent
183	/* this is a comment */
184.Ed
185.Pp
186This only affects block comments, not comments to the right of
187code.  The default is
188.Fl cdb  .
189.It Fl ce , nce
190Enables (disables) forcing of `else's to cuddle up to the immediately preceding
191`}'.  The default is
192.Fl \&ce  .
193.It Fl \&ci Ns Ar n
194Sets the continuation indent to be
195.Ar n  .
196Continuation
197lines will be indented that far from the beginning of the first line of the
198statement.  Parenthesized expressions have extra indentation added to
199indicate the nesting, unless
200.Fl \&lp
201is in effect
202or the contination indent is exactly half of the main indent.
203.Fl \&ci
204defaults to the same value as
205.Fl i  .
206.It Fl cli Ns Ar n
207Causes case labels to be indented
208.Ar n
209tab stops to the right of the containing
210.Ic switch
211statement.
212.Fl cli0.5
213causes case labels to be indented half a tab stop.  The
214default is
215.Fl cli0  .
216.It Fl d Ns Ar n
217Controls the placement of comments which are not to the
218right of code.  For example,
219.Fl \&d\&1
220means that such comments are placed one indentation level to the
221left of code.  Specifying the default
222.Fl \&d\&0
223lines-up these comments with the code.  See the section on comment
224indentation below.
225.It Fl \&di Ns Ar n
226Specifies the indentation, in character positions,
227of global variable names and all struct/union member names
228relative to the beginning of their type declaration.
229The default is
230.Fl di16  .
231.It Fl dj , ndj
232.Fl \&dj
233left justifies declarations.
234.Fl ndj
235indents declarations the same as code.  The default is
236.Fl ndj  .
237.It Fl \&ei , nei
238Enables (disables) special
239.Ic else-if
240processing.  If it's enabled, an
241.Ic if
242following an
243.Ic else
244will have the same indentation as the preceding
245.Ic \&if
246statement.  The default is
247.Fl ei  .
248.It Fl fbs , nfbs
249Enables (disables) splitting the function declaration and opening brace
250across two lines.
251The default is
252.Fl fbs  .
253.It Fl fc1 , nfc1
254Enables (disables) the formatting of comments that start in column 1.
255Often, comments whose leading `/' is in column 1 have been carefully
256hand formatted by the programmer.  In such cases,
257.Fl nfc1
258should be
259used.  The default is
260.Fl fc1  .
261.It Fl fcb , nfcb
262Enables (disables) the formatting of block comments (ones that begin
263with `/*\\n').
264Often, block comments have been not so carefully hand formatted by the
265programmer, but reformatting that would just change the line breaks is not
266wanted.
267In such cases,
268.Fl nfcb
269should be used.
270Block comments are then handled like box comments.
271The default is
272.Fl fcb  .
273.It Fl i Ns Ar n
274The number of spaces for one indentation level.  The default is 8.
275.It Fl \&ip , nip
276Enables (disables) the indentation of parameter declarations from the left
277margin.  The default is
278.Fl \&ip  .
279.It Fl l Ns Ar n
280Maximum length of an output line.  The default is 78.
281.It Fl \&ldi Ns Ar n
282Specifies the indentation, in character positions,
283of local variable names
284relative to the beginning of their type declaration.
285The default is for local variable names to be indented
286by the same amount as global ones.
287.It Fl \&lp , nlp
288Lines-up code surrounded by parenthesis in continuation lines.  If a line
289has a left paren which is not closed on that line, then continuation lines
290will be lined up to start at the character position just after the left
291paren.  For example, here is how a piece of continued code looks with
292.Fl nlp
293in effect:
294.Bd -literal -offset indent
295p1 = first_procedure(second_procedure(p2, p3),
296\ \ third_procedure(p4, p5));
297.Ed
298.Pp
299With
300.Fl lp
301in effect (the default) the code looks somewhat clearer:
302.Bd -literal -offset indent
303p1\ =\ first_procedure(second_procedure(p2,\ p3),
304\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,\ p5));
305.Ed
306.Pp
307Inserting two more newlines we get:
308.Bd -literal -offset indent
309p1\ =\ first_procedure(second_procedure(p2,
310\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3),
311\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,
312\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5));
313.Ed
314.It Fl npro
315Causes the profile files,
316.Sq Pa ./.indent.pro
317and
318.Sq Pa ~/.indent.pro ,
319to be ignored.
320.It Fl pcs , npcs
321If true
322.Pq Fl pcs
323all procedure calls will have a space inserted between
324the name and the `('.  The default is
325.Fl npcs  .
326.It Fl psl , npsl
327If true
328.Pq Fl psl
329the names of procedures being defined are placed in
330column 1 \- their types, if any, will be left on the previous lines.  The
331default is
332.Fl psl  .
333.It Fl \&sc , nsc
334Enables (disables) the placement of asterisks (`*'s) at the left edge of all
335comments.  The default is
336.Fl sc  .
337.It Fl sob , nsob
338If
339.Fl sob
340is specified, indent will swallow optional blank lines.  You can use this to
341get rid of blank lines after declarations.  Default:
342.Fl nsob  .
343.It Fl \&st
344Causes
345.Nm
346to take its input from stdin and put its output to stdout.
347.It Fl T Ns Ar typename
348Adds
349.Ar typename
350to the list of type keywords.  Names accumulate:
351.Fl T
352can be specified more than once.  You need to specify all the typenames that
353appear in your program that are defined by
354.Ic typedef
355\- nothing will be
356harmed if you miss a few, but the program won't be formatted as nicely as
357it should.  This sounds like a painful thing to have to do, but it's really
358a symptom of a problem in C:
359.Ic typedef
360causes a syntactic change in the
361language and
362.Nm
363can't find all
364instances of
365.Ic typedef .
366.It Fl troff
367Causes
368.Nm
369to format the program for processing by
370.Xr troff 1 .
371It will produce a fancy
372listing in much the same spirit as
373.Xr vgrind 1 .
374If the output file is not specified, the default is standard output,
375rather than formatting in place.
376.It Fl ut , nut
377Enables (disables) the use of tab characters in the output.
378Tabs are assumed to be aligned on columns divisble by 8.
379The default is
380.Fl ut  .
381.It Fl v , \&nv
382.Fl v
383turns on `verbose' mode;
384.Fl \&nv
385turns it off.  When in verbose mode,
386.Nm
387reports when it splits one line of input into two or more lines of output,
388and gives some size statistics at completion.  The default is
389.Fl \&nv  .
390.El
391.Pp
392You may set up your own `profile' of defaults to
393.Nm
394by creating a file called
395.Pa .indent.pro
396in your login directory and/or the current directory and including
397whatever switches you like.  A `.indent.pro' in the current directory takes
398precedence over the one in your login directory.  If
399.Nm
400is run and a profile file exists, then it is read to set up the program's
401defaults.  Switches on the command line, though, always override profile
402switches.  The switches should be separated by spaces, tabs or newlines.
403.Pp
404.Ss Comments
405.Sq Em Box
406.Em comments .
407The
408.Nm
409utility
410assumes that any comment with a dash or star immediately after the start of
411comment (that is, `/*\-' or `/**') is a comment surrounded by a box of stars.
412Each line of such a comment is left unchanged, except that its indentation
413may be adjusted to account for the change in indentation of the first line
414of the comment.
415.Pp
416.Em Straight text .
417All other comments are treated as straight text.
418The
419.Nm
420utility fits as many words (separated by blanks, tabs, or newlines) on a
421line as possible.  Blank lines break paragraphs.
422.Pp
423.Ss Comment indentation
424If a comment is on a line with code it is started in the `comment column',
425which is set by the
426.Fl c Ns Ns Ar n
427command line parameter.  Otherwise, the comment is started at
428.Ar n
429indentation levels less than where code is currently being placed, where
430.Ar n
431is specified by the
432.Fl d Ns Ns Ar n
433command line parameter.  If the code on a line extends past the comment
434column, the comment starts further to the right, and the right margin may be
435automatically extended in extreme cases.
436.Pp
437.Ss Preprocessor lines
438In general,
439.Nm
440leaves preprocessor lines alone.  The only
441reformatting that it will do is to straighten up trailing comments.  It
442leaves embedded comments alone.  Conditional compilation
443.Pq Ic #ifdef...#endif
444is recognized and
445.Nm
446attempts to correctly
447compensate for the syntactic peculiarities introduced.
448.Pp
449.Ss C syntax
450The
451.Nm
452utility understands a substantial amount about the syntax of C, but it
453has a `forgiving' parser.  It attempts to cope with the usual sorts of
454incomplete and misformed syntax.  In particular, the use of macros like:
455.Pp
456.Dl #define forever for(;;)
457.Pp
458is handled properly.
459.Sh ENVIRONMENT
460The
461.Nm
462utility uses the
463.Ev HOME
464environment variable.
465.Sh FILES
466.Bl -tag -width "./.indent.pro" -compact
467.It Pa ./.indent.pro
468profile file
469.It Pa ~/.indent.pro
470profile file
471.El
472.Sh HISTORY
473The
474.Nm
475command appeared in
476.Bx 4.2 .
477.Sh BUGS
478The
479.Nm
480utility has even more switches than
481.Xr ls 1 .
482.Pp
483A common mistake that often causes grief is typing:
484.Pp
485.Dl indent *.c
486.Pp
487to the shell in an attempt to indent all the
488.Em C
489programs in a directory.
490This is probably a bug, not a feature.
491