xref: /freebsd/usr.bin/indent/indent.1 (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
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.Dd June 28, 2023
31.Dt INDENT 1
32.Os
33.Sh NAME
34.Nm indent
35.Nd indent and format C program source
36.Sh SYNOPSIS
37.Nm
38.Op Ar input-file Op Ar output-file
39.Op Fl bacc | Fl nbacc
40.Op Fl bad | Fl nbad
41.Op Fl badp | Fl nbadp
42.Op Fl bap | Fl nbap
43.Op Fl bbb | Fl nbbb
44.Op Fl \&bc | Fl nbc
45.Op Fl \&bl | Fl \&br
46.Op Fl bs | Fl nbs
47.Op Fl c Ns Ar n
48.Op Fl \&cd Ns Ar n
49.Bk -words
50.Op Fl cdb | Fl ncdb
51.Ek
52.Op Fl \&ce | Fl nce
53.Op Fl \&ci Ns Ar n
54.Op Fl cli Ns Ar n
55.Op Fl cs | Fl ncs
56.Op Fl d Ns Ar n
57.Op Fl \&di Ns Ar n
58.Op Fl dj | Fl ndj
59.Bk -words
60.Op Fl ei | Fl nei
61.Op Fl eei | Fl neei
62.Ek
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 \&lpl | Fl nlpl
75.Op Fl npro
76.Op Fl P Ns Ar file
77.Op Fl pcs | Fl npcs
78.Op Fl ps | Fl nps
79.Op Fl psl | Fl npsl
80.Op Fl \&sc | Fl nsc
81.Bk -words
82.Op Fl sob | Fl nsob
83.Ek
84.Op Fl \&st
85.Op Fl \&ta
86.Op Fl T Ns Ar typename
87.Op Fl ts Ns Ar n
88.Op Fl U Ns Ar file
89.Op Fl ut | Fl nut
90.Op Fl v | Fl \&nv
91.Op Fl -version
92.Sh DESCRIPTION
93The
94.Nm
95utility is a
96.Em C
97program formatter.
98It reformats the
99.Em C
100program in the
101.Ar input-file
102according to the switches.
103The switches which can be
104specified are described below.
105They may appear before or after the file
106names.
107.Pp
108.Sy NOTE :
109If you only specify an
110.Ar input-file ,
111the formatting is
112done `in-place', that is, the formatted file is written back into
113.Ar input-file
114and a backup copy of
115.Ar input-file
116is written in the current directory.
117If
118.Ar input-file
119is named
120.Sq Pa /blah/blah/file ,
121the backup file is named
122.Sq Pa file.BAK
123by default.
124The 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 cs , ncs
262Control whether parenthesized type names in casts are followed by a space or
263not.
264The default is
265.Fl ncs .
266.It Fl d Ns Ar n
267Controls the placement of comments which are not to the
268right of code.
269For example,
270.Fl \&d\&1
271means that such comments are placed one indentation level to the
272left of code.
273Specifying the default
274.Fl \&d\&0
275lines up these comments with the code.
276See the section on comment
277indentation below.
278.It Fl \&di Ns Ar n
279Specifies the indentation, in character positions,
280of global variable names and all struct/union member names
281relative to the beginning of their type declaration.
282The default is
283.Fl di16 .
284.It Fl dj , ndj
285.Fl \&dj
286left justifies declarations.
287.Fl ndj
288indents declarations the same as code.
289The default is
290.Fl ndj .
291.It Fl \&ei , nei
292Enables (disables) special
293.Ic else-if
294processing.
295If it is enabled, an
296.Ic if
297following an
298.Ic else
299will have the same indentation as the preceding
300.Ic \&if
301statement.
302The default is
303.Fl ei .
304.It Fl eei , neei
305Enables (disables) extra indentation on continuation lines of
306the expression part of
307.Ic if
308and
309.Ic while
310statements.
311These continuation lines will be indented one extra level.
312The default is
313.Fl neei .
314.It Fl fbs , nfbs
315Enables (disables) splitting the function declaration and opening brace
316across two lines.
317The default is
318.Fl fbs .
319.It Fl fc1 , nfc1
320Enables (disables) the formatting of comments that start in column 1.
321Often, comments whose leading `/' is in column 1 have been carefully
322hand formatted by the programmer.
323In such cases,
324.Fl nfc1
325should be
326used.
327The default is
328.Fl fc1 .
329.It Fl fcb , nfcb
330Enables (disables) the formatting of block comments (ones that begin
331with `/*\\n').
332Often, block comments have been not so carefully hand formatted by the
333programmer, but reformatting that would just change the line breaks is not
334wanted.
335In such cases,
336.Fl nfcb
337should be used.
338Block comments are then handled like box comments.
339The default is
340.Fl fcb .
341.It Fl i Ns Ar n
342The number of columns for one indentation level.
343The default is 8.
344.It Fl \&ip , nip
345Enables (disables) the indentation of parameter declarations from the left
346margin.
347The default is
348.Fl \&ip .
349.It Fl l Ns Ar n
350Maximum length of an output line.
351The default is 78.
352.It Fl lc Ns Ar n
353Maximum length of an output line in a block comment.
354The default is 0, which means to limit block comment lines in accordance with
355.Fl l .
356.It Fl \&ldi Ns Ar n
357Specifies the indentation, in character positions,
358of local variable names
359relative to the beginning of their type declaration.
360The default is for local variable names to be indented
361by the same amount as global ones.
362.It Fl \&lp , nlp
363Lines up code surrounded by parentheses in continuation lines.
364With
365.Fl \&lp ,
366if a line
367has a left paren which is not closed on that line, then continuation lines
368will be lined up to start at the character position just after the left
369paren.
370For example, here is how a piece of continued code looks with
371.Fl nlp
372in effect:
373.Bd -literal -offset indent
374p1 = first_procedure(second_procedure(p2, p3),
375\ \ third_procedure(p4, p5));
376.Ed
377.Pp
378With
379.Fl lp
380in effect (the default) the code looks somewhat clearer:
381.Bd -literal -offset indent
382p1\ =\ first_procedure(second_procedure(p2,\ p3),
383\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,\ p5));
384.Ed
385.Pp
386Inserting two more newlines we get:
387.Bd -literal -offset indent
388p1\ =\ first_procedure(second_procedure(p2,
389\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3),
390\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,
391\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5));
392.Ed
393.It Fl \&lpl , nlpl
394With
395.Fl \&lpl ,
396code surrounded by parentheses in continuation lines is lined up even if it
397would extend past the right margin.
398With
399.Fl \&nlpl
400(the default), such a line that would extend past the right margin is moved
401left to keep it within the margin, if that does not require placing it to
402the left of the prevailing indentation level.
403These switches have no effect if
404.Fl nlp
405is selected.
406.It Fl npro
407Causes the profile files,
408.Sq Pa ./.indent.pro
409and
410.Sq Pa ~/.indent.pro ,
411to be ignored.
412.It Fl P Ns Ar file
413Read profile from
414.Ar file .
415.It Fl pcs , npcs
416If true
417.Pq Fl pcs
418all procedure calls will have a space inserted between
419the name and the `('.
420The default is
421.Fl npcs .
422.It Fl ps , nps
423If true
424.Pq Fl ps
425the pointer dereference operator (`->') is treated like any other
426binary operator.
427The default is
428.Fl nps .
429.It Fl psl , npsl
430If true
431.Pq Fl psl
432the names of procedures being defined are placed in
433column 1 \- their types, if any, will be left on the previous lines.
434The
435default is
436.Fl psl .
437.It Fl \&sc , nsc
438Enables (disables) the placement of asterisks (`*'s) at the left edge of all
439comments.
440The default is
441.Fl sc .
442.It Fl sob , nsob
443If
444.Fl sob
445is specified, indent will swallow optional blank lines.
446You can use this to
447get rid of blank lines after declarations.
448Default:
449.Fl nsob .
450.It Fl \&st
451Causes
452.Nm
453to take its input from stdin and put its output to stdout.
454.It Fl ta
455Automatically add all identifiers ending in "_t" to the list
456of type keywords.
457.It Fl T Ns Ar typename
458Adds
459.Ar typename
460to the list of type keywords.
461Names accumulate:
462.Fl T
463can be specified more than once.
464You need to specify all the typenames that
465appear in your program that are defined by
466.Ic typedef
467\- nothing will be
468harmed if you miss a few, but the program will not be formatted as nicely as
469it should.
470This sounds like a painful thing to have to do, but it is really
471a symptom of a problem in C:
472.Ic typedef
473causes a syntactic change in the
474language and
475.Nm
476cannot find all
477instances of
478.Ic typedef .
479.It Fl ts Ns Ar n
480Assumed distance between tab stops.
481The default is 8.
482.It Fl U Ns Ar file
483Adds type names from
484.Ar file
485to the list of type keywords.
486.It Fl ut , nut
487Enables (disables) the use of tab characters in the output.
488The default is
489.Fl ut .
490.It Fl v , \&nv
491.Fl v
492turns on `verbose' mode;
493.Fl \&nv
494turns it off.
495When in verbose mode,
496.Nm
497reports when it splits one line of input into two or more lines of output,
498and gives some size statistics at completion.
499The default is
500.Fl \&nv .
501.It Fl -version
502Causes
503.Nm
504to print its version number and exit.
505.El
506.Pp
507You may set up your own `profile' of defaults to
508.Nm
509by creating a file called
510.Pa .indent.pro
511in your login directory and/or the current directory and including
512whatever switches you like.
513A `.indent.pro' in the current directory takes
514precedence over the one in your login directory.
515If
516.Nm
517is run and a profile file exists, then it is read to set up the program's
518defaults.
519Switches on the command line, though, always override profile
520switches.
521The switches should be separated by spaces, tabs or newlines.
522.Pp
523.Ss Comments
524.Sq Em Box
525.Em comments .
526The
527.Nm
528utility
529assumes that any comment with a dash or star immediately after the start of
530comment (that is, `/*\-' or `/**') is a comment surrounded by a box of stars.
531Each line of such a comment is left unchanged, except that its indentation
532may be adjusted to account for the change in indentation of the first line
533of the comment.
534.Pp
535.Em Straight text .
536All other comments are treated as straight text.
537The
538.Nm
539utility fits as many words (separated by blanks, tabs, or newlines) on a
540line as possible.
541Blank lines break paragraphs.
542.Ss Comment indentation
543If a comment is on a line with code it is started in the `comment column',
544which is set by the
545.Fl c Ns Ns Ar n
546command line parameter.
547Otherwise, the comment is started at
548.Ar n
549indentation levels less than where code is currently being placed, where
550.Ar n
551is specified by the
552.Fl d Ns Ns Ar n
553command line parameter.
554If the code on a line extends past the comment
555column, the comment starts further to the right, and the right margin may be
556automatically extended in extreme cases.
557.Ss Preprocessor lines
558In general,
559.Nm
560leaves preprocessor lines alone.
561The only
562reformatting that it will do is to straighten up trailing comments.
563It
564leaves embedded comments alone.
565Conditional compilation
566.Pq Ic #ifdef...#endif
567is recognized and
568.Nm
569attempts to correctly
570compensate for the syntactic peculiarities introduced.
571.Ss C syntax
572The
573.Nm
574utility understands a substantial amount about the syntax of C, but it
575has a `forgiving' parser.
576It attempts to cope with the usual sorts of
577incomplete and malformed syntax.
578In particular, the use of macros like:
579.Pp
580.Dl #define forever for(;;)
581.Pp
582is handled properly.
583.Sh ENVIRONMENT
584The
585.Nm
586utility uses the
587.Ev HOME
588environment variable.
589.Sh FILES
590.Bl -tag -width "./.indent.pro" -compact
591.It Pa ./.indent.pro
592profile file
593.It Pa ~/.indent.pro
594profile file
595.El
596.Sh HISTORY
597The
598.Nm
599command appeared in
600.Bx 4.2 .
601.Sh BUGS
602The
603.Nm
604utility has even more switches than
605.Xr ls 1 .
606.Pp
607A common mistake is to try to indent all the
608.Em C
609programs in a directory by typing:
610.Pp
611.Dl indent *.c
612.Pp
613This is probably a bug, not a feature.
614