xref: /openbsd/usr.bin/indent/indent.1 (revision d485f761)
1.\"	$OpenBSD: indent.1,v 1.11 2001/05/01 17:58:03 aaron Exp $
2.\"
3.\" Copyright (c) 1980, 1990, 1993
4.\"	The Regents of the University of California.
5.\" Copyright (c) 1985 Sun Microsystems, Inc.
6.\" Copyright (c) 1976 Board of Trustees of the University of Illinois.
7.\" All rights reserved.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"	This product includes software developed by the University of
20.\"	California, Berkeley and its contributors.
21.\" 4. Neither the name of the University nor the names of its contributors
22.\"    may be used to endorse or promote products derived from this software
23.\"    without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35.\" SUCH DAMAGE.
36.\"
37.\"	from: @(#)indent.1	8.1 (Berkeley) 7/1/93
38.\"
39.Dd July 1, 1993
40.Dt INDENT 1
41.Os
42.Sh NAME
43.Nm indent
44.Nd indent and format C program source
45.Sh SYNOPSIS
46.Nm indent
47.Op Ar input-file Op Ar output-file
48.Op Fl bad | Fl nbad
49.Op Fl bap | Fl nbap
50.Bk -words
51.Op Fl bbb | Fl nbbb
52.Ek
53.Op Fl \&bc | Fl nbc
54.Op Fl \&bl
55.Op Fl \&br
56.Op Fl c Ns Ar n
57.Op Fl \&cd Ns Ar n
58.Bk -words
59.Op Fl cdb | Fl ncdb
60.Ek
61.Op Fl \&ce | Fl nce
62.Op Fl \&ci Ns Ar n
63.Op Fl cli Ns Ar n
64.Op Fl d Ns Ar n
65.Op Fl \&di Ns Ar n
66.Bk -words
67.Op Fl fc1 | Fl nfc1
68.Ek
69.Op Fl i Ns Ar n
70.Op Fl \&ip | Fl nip
71.Op Fl l Ns Ar n
72.Op Fl \&lc Ns Ar n
73.Op Fl \&lp | Fl nlp
74.Op Fl npro
75.Op Fl pcs | Fl npcs
76.Op Fl psl | Fl npsl
77.Op Fl \&sc | Fl nsc
78.Bk -words
79.Op Fl sob | Fl nsob
80.Ek
81.Op Fl \&st
82.Op Fl troff
83.Op Fl v | Fl \&nv
84.Sh DESCRIPTION
85.Nm
86is a
87.Ar C
88program formatter.
89It reformats the
90.Ar C
91program in the
92.Ar input-file
93according to the switches.
94The switches which can be specified are described below.
95They may appear before or after the file names.
96.Pp
97.Sy NOTE :
98If you only specify an
99.Ar input-file ,
100the formatting is
101done
102.Dq in-place ,
103that is, the formatted file is written back into
104.Ar input-file
105and a backup copy of
106.Ar input-file
107is written in the current directory.
108If
109.Ar input-file
110is named
111.Pa /blah/blah/file ,
112the backup file is named
113.Pa file.BAK .
114If
115.Pa file.BAK
116exists, it is overwritten.
117.Pp
118If
119.Ar output-file
120is specified,
121.Nm
122checks to make sure it is different from
123.Ar input-file .
124.Pp
125The options listed below control the formatting style imposed by
126.Nm indent .
127.Bl -tag -width Op
128.It Fl bad , nbad
129If
130.Fl bad
131is specified, a blank line is forced after every block of
132declarations.
133Default:
134.Fl nbad .
135.It Fl bap , nbap
136If
137.Fl bap
138is specified, a blank line is forced after every procedure body.
139Default:
140.Fl nbap .
141.It Fl bbb , nbbb
142If
143.Fl bbb
144is specified, a blank line is forced before every block comment.
145Default:
146.Fl nbbb .
147.It Fl \&bc , nbc
148If
149.Fl \&bc
150is specified, then a newline is forced after each comma in a declaration.
151.Fl nbc
152turns off this option.
153The default is
154.Fl \&nbc .
155.It Fl \&br , \&bl
156Specifying
157.Fl \&bl
158lines up compound statements like this:
159.ne 4
160.Bd -literal -offset indent
161if (...)
162{
163  code
164}
165.Ed
166.Pp
167Specifying
168.Fl \&br
169(the default) makes them look like this:
170.ne 3
171.Bd -literal -offset indent
172if (...) {
173  code
174}
175.Ed
176.Pp
177.It Fl c Ns Ar n
178The column in which comments on code start.
179The default is 33.
180.It Fl cd Ns Ar n
181The column in which comments on declarations start.
182The default
183is for these comments to start in the same column as those on code.
184.It Fl cdb , ncdb
185Enables (disables) the placement of comment delimiters on blank lines.
186With this option enabled, comments look like this:
187.Bd -literal -offset indent
188.ne 3
189	/*
190	 * this is a comment
191	 */
192.Ed
193.Pp
194Rather than like this:
195.Bd -literal -offset indent
196	/* this is a comment */
197.Ed
198.Pp
199This only affects block comments, not comments to the right of
200code.
201The default is
202.Fl cdb .
203.It Fl ce , nce
204Enables (disables) forcing
205.Do Li else Dc Ns s
206to cuddle up to the immediately preceding
207.Ql } .
208The default is
209.Fl \&ce .
210.It Fl \&ci Ns Ar n
211Sets the continuation indent to be
212.Ar n .
213Continuation
214lines will be indented that far from the beginning of the first line of the
215statement.
216Parenthesized expressions have extra indentation added to
217indicate the nesting, unless
218.Fl \&lp
219is in effect.
220.Fl \&ci
221defaults to the same value as
222.Fl i .
223.It Fl cli Ns Ar n
224Causes case labels to be indented
225.Ar n
226tab stops to the right of the containing
227.Ic switch
228statement.
229.Fl cli0.5
230causes case labels to be indented half a tab stop.
231The default is
232.Fl cli0 .
233.It Fl d Ns Ar n
234Controls the placement of comments which are not to the
235right of code.
236The default
237.Fl \&d\&1
238means that such comments are placed one indentation level to the
239left of code.
240Specifying
241.Fl \&d\&0
242lines up these comments with the code.
243See the section on comment indentation below.
244.It Fl \&di Ns Ar n
245Specifies the indentation, in character positions, from a declaration keyword
246to the following identifier.
247The default is
248.Fl di16 .
249.It Fl dj , ndj
250.Fl \&dj
251left justifies declarations.
252.Fl ndj
253indents declarations the same as code.
254The default is
255.Fl ndj .
256.It Fl \&ei , nei
257Enables (disables) special
258.Ic else-if
259processing.
260If it's enabled, an
261.Ic if
262following an
263.Ic else
264will have the same indentation as the preceding
265.Ic \&if
266statement.
267.It Fl fc1 , nfc1
268Enables (disables) the formatting of comments that start in column 1.
269Often, comments whose leading
270.Ql /
271is in column 1 have been carefully have formatted by the programmer.
272In such cases,
273.Fl nfc1
274should be
275used.
276The default is
277.Fl fc1 .
278.It Fl i Ns Ar n
279The number of spaces for one indentation level.
280The default is 8.
281.It Fl \&ip , nip
282Enables (disables) the indentation of parameter declarations from the left
283margin.
284The default is
285.Fl \&ip .
286.It Fl l Ns Ar n
287Maximum length of an output line.
288The default is 75.
289.It Fl \&lp , nlp
290Lines up code surrounded by parenthesis in continuation lines.
291If a line
292has a left paren which is not closed on that line, then continuation lines
293will be lined up to start at the character position just after the left
294paren.
295For example, here is how a piece of continued code looks with
296.Fl nlp
297in effect:
298.ne 2
299.Bd -literal -offset indent
300p1 = first_procedure(second_procedure(p2, p3),
301\ \ third_procedure(p4,p5));
302.Ed
303.Pp
304.ne 5
305With
306.Fl lp
307in effect (the default) the code looks somewhat clearer:
308.Bd -literal -offset indent
309p1\ =\ first_procedure(second_procedure(p2,\ p3),
310\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,p5));
311.Ed
312.Pp
313.ne 5
314Inserting two more newlines we get:
315.Bd -literal -offset indent
316p1\ =\ first_procedure(second_procedure(p2,
317\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3),
318\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,
319\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5));
320.Ed
321.It Fl npro
322Causes the profile files,
323.Pa ./.indent.pro
324and
325.Pa ~/.indent.pro ,
326to be ignored.
327.It Fl pcs , npcs
328If true
329.Pq Fl pcs
330all procedure calls will have a space inserted between
331the name and the
332.Ql ( .
333The default is
334.Fl npcs .
335.It Fl psl , npsl
336If true
337.Pq Fl psl
338the names of procedures being defined are placed in
339column 1 \- their types, if any, will be left on the previous lines.
340The default is
341.Fl psl .
342.It Fl \&sc , nsc
343Enables (disables) the placement of asterisks
344.Pq Ql *
345at the left edge of all comments.
346.It Fl sob , nsob
347If
348.Fl sob
349is specified, indent will swallow optional blank lines.
350You can use this to get rid of blank lines after declarations.
351Default:
352.Fl nsob .
353.It Fl \&st
354Causes
355.Nm
356to take its input from stdin, and put its output to stdout.
357.It Fl T Ns Ar typename
358Adds
359.Ar typename
360to the list of type keywords.
361Names accumulate:
362.Fl T
363can be specified more than once.
364You need to specify all the typenames that
365appear in your program that are defined by
366.Ic typedef
367\- nothing will be
368harmed if you miss a few, but the program won't be formatted as nicely as
369it should.
370This sounds like a painful thing to have to do, but it's really
371a symptom of a problem in C:
372.Ic typedef
373causes a syntactic change in the
374language and
375.Nm
376can't find all
377instances of
378.Ic typedef .
379.It Fl troff
380Causes
381.Nm
382to format the program for processing by
383.Xr troff 1 .
384It will produce a fancy
385listing in much the same spirit as
386.Xr vgrind 1 .
387If the output file is not specified, the default is standard output,
388rather than formatting in place.
389.It Fl v , \&nv
390.Fl v
391turns on
392.Dq verbose
393mode;
394.Fl \&nv
395turns it off.
396When in verbose mode,
397.Nm
398reports when it splits one line of input into two or more lines of output,
399and gives some size statistics at completion.
400The default is
401.Fl \&nv .
402.El
403.Pp
404You may set up your own
405.Dq profile
406of defaults to
407.Nm
408by creating a file called
409.Pa .indent.pro
410in your login directory and/or the current directory and including
411whatever switches you like.
412An
413.Pa \&.indent.pro
414file in the current directory takes
415precedence over the one in your login directory.
416If
417.Nm
418is run and a profile file exists, then it is read to set up the program's
419defaults.
420Switches on the command line, though, always override profile
421switches.
422The switches should be separated by spaces, tabs or newlines.
423.Pp
424.Ss Comments
425.Sq Em Box
426.Em comments .
427.Nm
428assumes that any comment with a dash, star, or newline immediately after
429the start of comment (that is,
430.Ql /*\- ,
431.Ql /** ,
432or
433.Ql /*
434followed immediately by a newline character) is a comment surrounded
435by a box of stars.
436Each line of such a comment is left unchanged, except
437that its indentation may be adjusted to account for the change in indentation
438of the first line
439of the comment.
440.Pp
441.Em Straight text .
442All other comments are treated as straight text.
443.Nm
444fits as many words (separated by blanks, tabs, or newlines) on a
445line as possible.
446Blank lines break paragraphs.
447.Pp
448.Ss Comment indentation
449If a comment is on a line with code it is started in the
450.Dq comment column ,
451which is set by the
452.Fl c Ns Ns Ar n
453command line parameter.
454Otherwise, the comment is started at
455.Ar n
456indentation levels less than where code is currently being placed, where
457.Ar n
458is specified by the
459.Fl d Ns Ns Ar n
460command line parameter.
461If the code on a line extends past the comment
462column, the comment starts further to the right, and the right margin may be
463automatically extended in extreme cases.
464.Pp
465.Ss Preprocessor lines
466In general,
467.Nm
468leaves preprocessor lines alone.
469The only
470reformatting that it will do is to straighten up trailing comments.
471It leaves embedded comments alone.
472Conditional compilation
473.Pq Ic #ifdef...#endif
474is recognized and
475.Nm
476attempts to correctly
477compensate for the syntactic peculiarities introduced.
478.Pp
479.Ss C syntax
480.Nm
481understands a substantial amount about the syntax of C, but it
482has a
483.Dq forgiving
484parser.
485It attempts to cope with the usual sorts of
486incomplete and misformed syntax.
487In particular, the use of macros like:
488.Pp
489.Dl #define forever for(;;)
490.Pp
491is handled properly.
492.Sh ENVIRONMENT
493.Bl -tag -width Ds
494.It Ev HOME
495Used to locate the full path to
496.Pa ~/.indent.pro .
497.El
498.Sh FILES
499.Bl -tag -width "./.indent.pro" -compact
500.It Pa ./.indent.pro
501profile file
502.It Pa ~/.indent.pro
503profile file
504.El
505.Sh HISTORY
506The
507.Nm
508command appeared in
509.Bx 4.2 .
510.Sh BUGS
511.Nm
512has even more switches than
513.Xr ls 1 .
514.Pp
515.ne 5
516A common mistake that often causes grief is typing:
517.Pp
518.Dl indent *.c
519.Pp
520to the shell in an attempt to indent all the
521.Nm C
522programs in a directory.
523This is probably a bug, not a feature.
524