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