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