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