xref: /netbsd-src/usr.bin/indent/indent.1 (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1.\" Copyright (c) 1980, 1990 The Regents of the University of California.
2.\" Copyright (c) 1985 Sun Microsystems, Inc.
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.\"	from: @(#)indent.1	6.11 (Berkeley) 7/24/91
35.\"	$Id: indent.1,v 1.4 1994/01/11 02:29:18 jtc Exp $
36.\"
37.Dd July 24, 1991
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 indent
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 indent
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.  The default is
141.Fl \&bc  .
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 n
165The column in which comments on code start.  The default is 33.
166.It Fl cd 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 `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.  The default
216.Fl \&d\&1
217means that such comments are placed one indentation level to the
218left of code.  Specifying
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.
242.It Fl fc1 , nfc1
243Enables (disables) the formatting of comments that start in column 1.
244Often, comments whose leading `/' is in column 1 have been carefully
245hand formatted by the programmer.  In such cases,
246.Fl nfc1
247should be
248used.  The default is
249.Fl fc1  .
250.It Fl i Ns Ar n
251The number of spaces for one indentation level.  The default is 4.
252.It Fl \&ip , nip
253Enables (disables) the indentation of parameter declarations from the left
254margin.  The default is
255.Fl \&ip  .
256.It Fl l Ns Ar n
257Maximum length of an output line.  The default is 75.
258.It Fl \&lp , nlp
259Lines up code surrounded by parenthesis in continuation lines.  If a line
260has a left paren which is not closed on that line, then continuation lines
261will be lined up to start at the character position just after the left
262paren.  For example, here is how a piece of continued code looks with
263.Fl nlp
264in effect:
265.ne 2
266.Bd -literal -offset indent
267.Li p1 = first_procedure(second_procedure(p2, p3),
268.Li \ \ third_procedure(p4,p5));
269.Ed
270.Pp
271.ne 5
272With
273.Fl lp
274in effect (the default) the code looks somewhat clearer:
275.Bd -literal -offset indent
276.Li p1\ =\ first_procedure(second_procedure(p2,\ p3),
277.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,p5));
278.Ed
279.Pp
280.ne 5
281Inserting two more newlines we get:
282.Bd -literal -offset indent
283.Li p1\ =\ first_procedure(second_procedure(p2,
284.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3),
285.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4
286.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5));
287.Ed
288.It Fl npro
289Causes the profile files,
290.Sq Pa ./.indent.pro
291and
292.Sq Pa ~/.indent.pro ,
293to be ignored.
294.It Fl pcs , npcs
295If true
296.Pq Fl pcs
297all procedure calls will have a space inserted between
298the name and the `('.  The default is
299.Fl npcs  .
300.It Fl psl , npsl
301If true
302.Pq Fl psl
303the names of procedures being defined are placed in
304column 1 \- their types, if any, will be left on the previous lines.  The
305default is
306.Fl psl  .
307.It Fl \&sc , nsc
308Enables (disables) the placement of asterisks (`*'s) at the left edge of all
309comments.
310.It Fl sob , nsob
311If
312.Fl sob
313is specified, indent will swallow optional blank lines.  You can use this to
314get rid of blank lines after declarations.  Default:
315.Fl nsob  .
316.It Fl \&st
317Causes
318.Nm indent
319to take its input from stdin, and put its output to stdout.
320.It Fl T Ns Ar typename
321Adds
322.Ar typename
323to the list of type keywords.  Names accumulate:
324.Fl T
325can be specified more than once.  You need to specify all the typenames that
326appear in your program that are defined by
327.Ic typedef
328\- nothing will be
329harmed if you miss a few, but the program won't be formatted as nicely as
330it should.  This sounds like a painful thing to have to do, but it's really
331a symptom of a problem in C:
332.Ic typedef
333causes a syntactic change in the
334language and
335.Nm indent
336can't find all
337instances of
338.Ic typedef .
339.It Fl troff
340Causes
341.Nm indent
342to format the program for processing by
343.Xr troff 1 .
344It will produce a fancy
345listing in much the same spirit as
346.Xr vgrind 1 .
347If the output file is not specified, the default is standard output,
348rather than formatting in place.
349.It Fl v , \&nv
350.Fl v
351turns on `verbose' mode;
352.Fl \&nv
353turns it off.  When in verbose mode,
354.Nm indent
355reports when it splits one line of input into two or more lines of output,
356and gives some size statistics at completion. The default is
357.Fl \&nv  .
358.El
359.Pp
360You may set up your own `profile' of defaults to
361.Nm indent
362by creating a file called
363.Pa .indent.pro
364in your login directory and/or the current directory and including
365whatever switches you like.  A `.indent.pro' in the current directory takes
366precedence over the one in your login directory.  If
367.Nm indent
368is run and a profile file exists, then it is read to set up the program's
369defaults.  Switches on the command line, though, always override profile
370switches.  The switches should be separated by spaces, tabs or newlines.
371.Pp
372.Ss Comments
373.Sq Em Box
374.Em comments .
375.Nm Indent
376assumes that any comment with a dash, star, or newline immediately after
377the start of comment (that is, `/*\-', `/**' or `/*' followed
378immediately by a newline character) is a comment surrounded
379by a box of stars.  Each line of such a comment is left unchanged, except
380that its indentation may be adjusted to account for the change in indentation
381of 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 indent
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 indent
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 indent
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