xref: /freebsd/usr.bin/indent/indent.1 (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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.\"
32.Dd June 28, 2023
33.Dt INDENT 1
34.Os
35.Sh NAME
36.Nm indent
37.Nd indent and format C program source
38.Sh SYNOPSIS
39.Nm
40.Op Ar input-file Op Ar output-file
41.Op Fl bacc | Fl nbacc
42.Op Fl bad | Fl nbad
43.Op Fl badp | Fl nbadp
44.Op Fl bap | Fl nbap
45.Op Fl bbb | Fl nbbb
46.Op Fl \&bc | Fl nbc
47.Op Fl \&bl | Fl \&br
48.Op Fl bs | Fl nbs
49.Op Fl c Ns Ar n
50.Op Fl \&cd Ns Ar n
51.Bk -words
52.Op Fl cdb | Fl ncdb
53.Ek
54.Op Fl \&ce | Fl nce
55.Op Fl \&ci Ns Ar n
56.Op Fl cli Ns Ar n
57.Op Fl cs | Fl ncs
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 ps | Fl nps
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 ps , nps
425If true
426.Pq Fl ps
427the pointer dereference operator (`->') is treated like any other
428binary operator.
429The default is
430.Fl nps .
431.It Fl psl , npsl
432If true
433.Pq Fl psl
434the names of procedures being defined are placed in
435column 1 \- their types, if any, will be left on the previous lines.
436The
437default is
438.Fl psl .
439.It Fl \&sc , nsc
440Enables (disables) the placement of asterisks (`*'s) at the left edge of all
441comments.
442The default is
443.Fl sc .
444.It Fl sob , nsob
445If
446.Fl sob
447is specified, indent will swallow optional blank lines.
448You can use this to
449get rid of blank lines after declarations.
450Default:
451.Fl nsob .
452.It Fl \&st
453Causes
454.Nm
455to take its input from stdin and put its output to stdout.
456.It Fl ta
457Automatically add all identifiers ending in "_t" to the list
458of type keywords.
459.It Fl T Ns Ar typename
460Adds
461.Ar typename
462to the list of type keywords.
463Names accumulate:
464.Fl T
465can be specified more than once.
466You need to specify all the typenames that
467appear in your program that are defined by
468.Ic typedef
469\- nothing will be
470harmed if you miss a few, but the program will not be formatted as nicely as
471it should.
472This sounds like a painful thing to have to do, but it is really
473a symptom of a problem in C:
474.Ic typedef
475causes a syntactic change in the
476language and
477.Nm
478cannot find all
479instances of
480.Ic typedef .
481.It Fl ts Ns Ar n
482Assumed distance between tab stops.
483The default is 8.
484.It Fl U Ns Ar file
485Adds type names from
486.Ar file
487to the list of type keywords.
488.It Fl ut , nut
489Enables (disables) the use of tab characters in the output.
490The default is
491.Fl ut .
492.It Fl v , \&nv
493.Fl v
494turns on `verbose' mode;
495.Fl \&nv
496turns it off.
497When in verbose mode,
498.Nm
499reports when it splits one line of input into two or more lines of output,
500and gives some size statistics at completion.
501The default is
502.Fl \&nv .
503.It Fl -version
504Causes
505.Nm
506to print its version number and exit.
507.El
508.Pp
509You may set up your own `profile' of defaults to
510.Nm
511by creating a file called
512.Pa .indent.pro
513in your login directory and/or the current directory and including
514whatever switches you like.
515A `.indent.pro' in the current directory takes
516precedence over the one in your login directory.
517If
518.Nm
519is run and a profile file exists, then it is read to set up the program's
520defaults.
521Switches on the command line, though, always override profile
522switches.
523The switches should be separated by spaces, tabs or newlines.
524.Pp
525.Ss Comments
526.Sq Em Box
527.Em comments .
528The
529.Nm
530utility
531assumes that any comment with a dash or star immediately after the start of
532comment (that is, `/*\-' or `/**') is a comment surrounded by a box of stars.
533Each line of such a comment is left unchanged, except that its indentation
534may be adjusted to account for the change in indentation of the first line
535of the comment.
536.Pp
537.Em Straight text .
538All other comments are treated as straight text.
539The
540.Nm
541utility fits as many words (separated by blanks, tabs, or newlines) on a
542line as possible.
543Blank lines break paragraphs.
544.Ss Comment indentation
545If a comment is on a line with code it is started in the `comment column',
546which is set by the
547.Fl c Ns Ns Ar n
548command line parameter.
549Otherwise, the comment is started at
550.Ar n
551indentation levels less than where code is currently being placed, where
552.Ar n
553is specified by the
554.Fl d Ns Ns Ar n
555command line parameter.
556If the code on a line extends past the comment
557column, the comment starts further to the right, and the right margin may be
558automatically extended in extreme cases.
559.Ss Preprocessor lines
560In general,
561.Nm
562leaves preprocessor lines alone.
563The only
564reformatting that it will do is to straighten up trailing comments.
565It
566leaves embedded comments alone.
567Conditional compilation
568.Pq Ic #ifdef...#endif
569is recognized and
570.Nm
571attempts to correctly
572compensate for the syntactic peculiarities introduced.
573.Ss C syntax
574The
575.Nm
576utility understands a substantial amount about the syntax of C, but it
577has a `forgiving' parser.
578It attempts to cope with the usual sorts of
579incomplete and malformed syntax.
580In particular, the use of macros like:
581.Pp
582.Dl #define forever for(;;)
583.Pp
584is handled properly.
585.Sh ENVIRONMENT
586The
587.Nm
588utility uses the
589.Ev HOME
590environment variable.
591.Sh FILES
592.Bl -tag -width "./.indent.pro" -compact
593.It Pa ./.indent.pro
594profile file
595.It Pa ~/.indent.pro
596profile file
597.El
598.Sh HISTORY
599The
600.Nm
601command appeared in
602.Bx 4.2 .
603.Sh BUGS
604The
605.Nm
606utility has even more switches than
607.Xr ls 1 .
608.Pp
609A common mistake is to try to indent all the
610.Em C
611programs in a directory by typing:
612.Pp
613.Dl indent *.c
614.Pp
615This is probably a bug, not a feature.
616