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