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