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