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