xref: /netbsd-src/usr.bin/indent/indent.1 (revision 17dd36da8292193180754d5047c0926dbb56818c)
1.\"	$NetBSD: indent.1,v 1.13 2001/03/23 20:12:41 fair 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.Bd -literal -offset indent
225	/*
226	 * this is a comment
227	 */
228.Ed
229.Pp
230Rather than like this:
231.Bd -literal -offset indent
232	/* this is a comment */
233.Ed
234.Pp
235This only affects block comments, not comments to the right of
236code.
237Default:
238.Fl cdb  .
239.It Fl ce , nce
240Enables (disables) forcing `else's to cuddle up to the immediately preceding
241`}'.
242Default:
243.Fl \&ce  .
244.It Fl \&ci Ns Ar n
245Sets the continuation indent to be
246.Ar n  .
247Continuation
248lines will be indented that far from the beginning of the first line of the
249statement.
250Parenthesized expressions have extra indentation added to
251indicate the nesting, unless
252.Fl \&lp
253is in effect.
254.Fl \&ci
255defaults to the same value as
256.Fl i  .
257.It Fl cli Ns Ar n
258Causes case labels to be indented
259.Ar n
260tab stops to the right of the containing
261.Ic switch
262statement.
263.Fl cli0.5
264causes case labels to be indented half a tab stop.
265Default:
266.Fl cli0  .
267.It Fl d Ns Ar n
268Controls the placement of comments which are not to the right of code.
269For example,
270.Fl \&d\&1
271means that such comments are placed one indentation level to the left of code.
272Specifying the default
273.Fl \&d\&0
274lines up these comments with the code.
275See the section on comment
276indentation below.
277.It Fl \&di Ns Ar n
278Specifies the indentation, in character positions, from a declaration keyword
279to the following identifier.
280Default:
281.Fl di16  .
282.It Fl dj , ndj
283.Fl \&dj
284left justifies declarations.
285.Fl ndj
286indents declarations the same as code.
287Default:
288.Fl ndj  .
289.It Fl \&ei , nei
290Enables (disables) special
291.Ic else-if
292processing.
293If it's enabled, an
294.Ic if
295following an
296.Ic else
297will have the same indentation as the preceding
298.Ic \&if
299statement.
300Default:
301.Fl ei  .
302.It Fl eei , neei
303Enables (disables) extra indentation on continuation lines of
304the expression part of
305.Ic if
306and
307.Ic while
308statements.
309These continuation lines will be indented one extra level.
310Default:
311.Fl neei  .
312.It Fl fc1 , nfc1
313Enables (disables) the formatting of comments that start in column 1.
314Often, comments whose leading `/' is in column 1 have been carefully
315hand formatted by the programmer.
316In such cases,
317.Fl nfc1
318should be used.
319Default:
320.Fl fc1  .
321.It Fl i Ns Ar n
322The number of spaces for one indentation level.
323Default:
324.Fl i8 .
325.It Fl \&ip , nip
326Enables (disables) the indentation of parameter declarations from the left
327margin.
328Default:
329.Fl \&ip  .
330.It Fl l Ns Ar n
331Maximum length of an output line.
332Default:
333.Fl l78  .
334.It Fl \&lp , nlp
335Lines up code surrounded by parenthesis in continuation lines.
336If a line has a left paren which is not closed on that line, then
337continuation lines will be lined up to start at the character
338position just after the left paren.
339For example, here is how a piece of continued code looks with
340.Fl nlp
341in effect:
342.ne 2
343.Bd -literal -offset indent
344p1 = first_procedure(second_procedure(p2, p3),
345\ \ third_procedure(p4,p5));
346.Ed
347.Pp
348.ne 5
349With
350.Fl lp
351in effect (the default) the code looks somewhat clearer:
352.Bd -literal -offset indent
353p1\ =\ first_procedure(second_procedure(p2,\ p3),
354\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,p5));
355.Ed
356.Pp
357.ne 5
358Inserting two more newlines we get:
359.Bd -literal -offset indent
360p1\ =\ first_procedure(second_procedure(p2,
361\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3),
362\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4
363\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5));
364.Ed
365.It Fl npro
366Causes the profile files,
367.Sq Pa ./.indent.pro
368and
369.Sq Pa ~/.indent.pro ,
370to be ignored.
371.It Fl pcs , npcs
372If true
373.Pq Fl pcs
374all procedure calls will have a space inserted between
375the name and the `('.
376Default:
377.Fl npcs  .
378.It Fl psl , npsl
379If true
380.Pq Fl psl
381the names of procedures being defined are placed in
382column 1 \- their types, if any, will be left on the previous lines.
383Default:
384.Fl psl  .
385.It Fl \&sc , nsc
386Enables (disables) the placement of asterisks (`*'s) at the left edge of all
387comments.
388Default:
389.Fl sc  .
390.It Fl sob , nsob
391If
392.Fl sob
393is specified, indent will swallow optional blank lines.
394You can use this to get rid of blank lines after declarations.
395Default:
396.Fl nsob  .
397.It Fl \&st
398Causes
399.Nm
400to take its input from stdin, and put its output to stdout.
401.It Fl T Ns Ar typename
402Adds
403.Ar typename
404to the list of type keywords.
405Names accumulate:
406.Fl T
407can be specified more than once.
408You need to specify all the typenames that
409appear in your program that are defined by
410.Ic typedef
411\- nothing will be
412harmed if you miss a few, but the program won't be formatted as nicely as
413it should.
414This sounds like a painful thing to have to do, but it's really
415a symptom of a problem in C:
416.Ic typedef
417causes a syntactic change in the
418language and
419.Nm
420can't find all
421instances of
422.Ic typedef .
423.It Fl troff
424Causes
425.Nm
426to format the program for processing by
427.Xr troff 1 .
428It will produce a fancy
429listing in much the same spirit as
430.Xr vgrind 1 .
431If the output file is not specified, the default is standard output,
432rather than formatting in place.
433.It Fl v , \&nv
434.Fl v
435turns on `verbose' mode;
436.Fl \&nv
437turns it off.
438When in verbose mode,
439.Nm
440reports when it splits one line of input into two or more lines of output,
441and gives some size statistics at completion.
442Default:
443.Fl \&nv  .
444.El
445.Pp
446You may set up your own `profile' of defaults to
447.Nm
448by creating a file called
449.Pa .indent.pro
450in your login directory and/or the current directory and including
451whatever switches you like.
452A `.indent.pro' in the current directory takes
453precedence over the one in your login directory.
454If
455.Nm
456is run and a profile file exists, then it is read to set up the program's
457defaults.
458Switches on the command line, though, always override profile switches.
459The switches should be separated by spaces, tabs or newlines.
460.Pp
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.Pp
478.Ss Comment indentation
479If a comment is on a line with code it is started in the `comment column',
480which is set by the
481.Fl c Ns Ns Ar n
482command line parameter.
483Otherwise, the comment is started at
484.Ar n
485indentation levels less than where code is currently being placed, where
486.Ar n
487is specified by the
488.Fl d Ns Ns Ar n
489command line parameter.
490If the code on a line extends past the comment
491column, the comment starts further to the right, and the right margin may be
492automatically extended in extreme cases.
493.Pp
494.Ss Preprocessor lines
495In general,
496.Nm
497leaves preprocessor lines alone.
498The only reformatting that it will do is to straighten up trailing comments.
499It leaves embedded comments alone.
500Conditional compilation
501.Pq Ic #ifdef...#endif
502is recognized and
503.Nm
504attempts to correctly
505compensate for the syntactic peculiarities introduced.
506.Pp
507.Ss C syntax
508.Nm
509understands a substantial amount about the syntax of C, but it
510has a `forgiving' parser.
511It attempts to cope with the usual sorts of incomplete and misformed syntax.
512In particular, the use of macros like:
513.Pp
514.Dl #define forever for(;;)
515.Pp
516is handled properly.
517.Sh ENVIRONMENT
518.Nm
519uses the
520.Ev HOME
521environment variable.
522.Sh FILES
523.Bl -tag -width "./.indent.pro" -compact
524.It Pa ./.indent.pro
525profile file
526.It Pa ~/.indent.pro
527profile file
528.El
529.Sh HISTORY
530The
531.Nm
532command appeared in
533.Bx 4.2 .
534.Sh BUGS
535.Nm
536has even more switches than
537.Xr ls 1 .
538.Pp
539.ne 5
540A common mistake that often causes grief is typing:
541.Pp
542.Dl indent *.c
543.Pp
544to the shell in an attempt to indent all the
545.Em C
546programs in a directory.
547This is probably a bug, not a feature.
548