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