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