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