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