1 /* $NetBSD: args.c,v 1.9 2003/08/07 11:14:07 agc Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
34 * Copyright (c) 1985 Sun Microsystems, Inc.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 #include <sys/cdefs.h>
69 static char sccsid
[] = "@(#)args.c 8.1 (Berkeley) 6/6/93";
71 __RCSID("$NetBSD: args.c,v 1.9 2003/08/07 11:14:07 agc Exp $");
76 * Argument scanning and profile reading code. Default parameters are set
84 #include "indent_globs.h"
87 #define PRO_SPECIAL 1 /* special case */
88 #define PRO_BOOL 2 /* boolean */
89 #define PRO_INT 3 /* integer */
90 #define PRO_FONT 4 /* troff font */
92 /* profile specials for booleans */
93 #define ON 1 /* turn it on */
94 #define OFF 0 /* turn it off */
96 /* profile specials for specials */
97 #define IGN 1 /* ignore it */
98 #define CLI 2 /* case label indent (float) */
99 #define STDIN 3 /* use stdin */
100 #define KEY 4 /* type (keyword) */
102 const char *option_source
= "?";
105 * N.B.: because of the way the table here is scanned, options whose names are
106 * substrings of other options must occur later; that is, with -lp vs -l, -lp
107 * must be first. Also, while (most) booleans occur more than once, the last
108 * default value is the one actually assigned.
111 const char *p_name
; /* name, eg -bl, -cli */
112 int p_type
; /* type (int, bool, special) */
113 int p_default
; /* the default value (if int) */
114 int p_special
; /* depends on type */
115 int *p_obj
; /* the associated variable */
118 "T", PRO_SPECIAL
, 0, KEY
, 0
121 "bacc", PRO_BOOL
, false, ON
, &blanklines_around_conditional_compilation
124 "badp", PRO_BOOL
, false, ON
, &blanklines_after_declarations_at_proctop
127 "bad", PRO_BOOL
, false, ON
, &blanklines_after_declarations
130 "bap", PRO_BOOL
, false, ON
, &blanklines_after_procs
133 "bbb", PRO_BOOL
, false, ON
, &blanklines_before_blockcomments
136 "bc", PRO_BOOL
, true, OFF
, &ps
.leave_comma
139 "bl", PRO_BOOL
, true, OFF
, &btype_2
142 "br", PRO_BOOL
, true, ON
, &btype_2
145 "bs", PRO_BOOL
, false, ON
, &Bill_Shannon
148 "cdb", PRO_BOOL
, true, ON
, &comment_delimiter_on_blankline
151 "cd", PRO_INT
, 0, 0, &ps
.decl_com_ind
154 "ce", PRO_BOOL
, true, ON
, &cuddle_else
157 "ci", PRO_INT
, 0, 0, &continuation_indent
160 "cli", PRO_SPECIAL
, 0, CLI
, 0
163 "c", PRO_INT
, 33, 0, &ps
.com_ind
166 "di", PRO_INT
, 16, 0, &ps
.decl_indent
169 "dj", PRO_BOOL
, false, ON
, &ps
.ljust_decl
172 "d", PRO_INT
, 0, 0, &ps
.unindent_displace
175 "eei", PRO_BOOL
, false, ON
, &extra_expression_indent
178 "ei", PRO_BOOL
, true, ON
, &ps
.else_if
181 "fbc", PRO_FONT
, 0, 0, (int *) &blkcomf
184 "fbx", PRO_FONT
, 0, 0, (int *) &boxcomf
187 "fb", PRO_FONT
, 0, 0, (int *) &bodyf
190 "fc1", PRO_BOOL
, true, ON
, &format_col1_comments
193 "fc", PRO_FONT
, 0, 0, (int *) &scomf
196 "fk", PRO_FONT
, 0, 0, (int *) &keywordf
199 "fs", PRO_FONT
, 0, 0, (int *) &stringf
202 "ip", PRO_BOOL
, true, ON
, &ps
.indent_parameters
205 "i", PRO_INT
, 8, 0, &ps
.ind_size
208 "lc", PRO_INT
, 0, 0, &block_comment_max_col
211 "lp", PRO_BOOL
, true, ON
, &lineup_to_parens
214 "l", PRO_INT
, 78, 0, &max_col
217 "nbacc", PRO_BOOL
, false, OFF
, &blanklines_around_conditional_compilation
220 "nbadp", PRO_BOOL
, false, OFF
, &blanklines_after_declarations_at_proctop
223 "nbad", PRO_BOOL
, false, OFF
, &blanklines_after_declarations
226 "nbap", PRO_BOOL
, false, OFF
, &blanklines_after_procs
229 "nbbb", PRO_BOOL
, false, OFF
, &blanklines_before_blockcomments
232 "nbc", PRO_BOOL
, true, ON
, &ps
.leave_comma
235 "nbs", PRO_BOOL
, false, OFF
, &Bill_Shannon
238 "ncdb", PRO_BOOL
, true, OFF
, &comment_delimiter_on_blankline
241 "nce", PRO_BOOL
, true, OFF
, &cuddle_else
244 "ndj", PRO_BOOL
, false, OFF
, &ps
.ljust_decl
247 "neei", PRO_BOOL
, false, OFF
, &extra_expression_indent
250 "nei", PRO_BOOL
, true, OFF
, &ps
.else_if
253 "nfc1", PRO_BOOL
, true, OFF
, &format_col1_comments
256 "nip", PRO_BOOL
, true, OFF
, &ps
.indent_parameters
259 "nlp", PRO_BOOL
, true, OFF
, &lineup_to_parens
262 "npcs", PRO_BOOL
, false, OFF
, &proc_calls_space
265 "npro", PRO_SPECIAL
, 0, IGN
, 0
268 "npsl", PRO_BOOL
, true, OFF
, &procnames_start_line
271 "nps", PRO_BOOL
, false, OFF
, &pointer_as_binop
274 "nsc", PRO_BOOL
, true, OFF
, &star_comment_cont
277 "nsob", PRO_BOOL
, false, OFF
, &swallow_optional_blanklines
280 "nv", PRO_BOOL
, false, OFF
, &verbose
283 "pcs", PRO_BOOL
, false, ON
, &proc_calls_space
286 "psl", PRO_BOOL
, true, ON
, &procnames_start_line
289 "ps", PRO_BOOL
, false, ON
, &pointer_as_binop
292 "sc", PRO_BOOL
, true, ON
, &star_comment_cont
295 "sob", PRO_BOOL
, false, ON
, &swallow_optional_blanklines
298 "st", PRO_SPECIAL
, 0, STDIN
, 0
301 "troff", PRO_BOOL
, false, ON
, &troff
304 "v", PRO_BOOL
, false, ON
, &verbose
312 * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
313 * given in these files.
320 static char prof
[] = ".indent.pro";
322 snprintf(fname
, sizeof(fname
), "%s/%s", getenv("HOME"), prof
);
323 if ((f
= fopen(option_source
= fname
, "r")) != NULL
) {
327 if ((f
= fopen(option_source
= prof
, "r")) != NULL
) {
331 option_source
= "Command line";
335 scan_profile(FILE *f
)
342 for (p
= buf
; (i
= getc(f
)) != EOF
&& (*p
= i
) > ' '; ++p
);
346 printf("profile: %s\n", buf
);
354 const char *param_start
;
357 eqin(const char *s1
, const char *s2
)
375 * Because ps.case_indent is a float, we can't initialize it from the
378 ps
.case_indent
= 0.0; /* -cli0.0 */
379 for (p
= pro
; p
->p_name
; p
++)
380 if (p
->p_type
!= PRO_SPECIAL
&& p
->p_type
!= PRO_FONT
)
381 *p
->p_obj
= p
->p_default
;
385 set_option(char *arg
)
389 arg
++; /* ignore leading "-" */
390 for (p
= pro
; p
->p_name
; p
++)
391 if (*p
->p_name
== *arg
&& eqin(p
->p_name
, arg
))
393 fprintf(stderr
, "indent: %s: unknown parameter \"%s\"\n", option_source
, arg
- 1);
399 switch (p
->p_special
) {
405 if (*param_start
== 0)
407 ps
.case_indent
= atof(param_start
);
418 if (*param_start
== 0)
423 str
= strdup(param_start
);
430 indent: set_option: internal error: p_special %d\n", p
->p_special
);
436 if (p
->p_special
== OFF
)
443 if (!isdigit((unsigned char)*param_start
)) {
445 fprintf(stderr
, "indent: %s: ``%s'' requires a parameter\n",
446 option_source
, arg
- 1);
449 *p
->p_obj
= atoi(param_start
);
453 parsefont((struct fstate
*) p
->p_obj
, param_start
);
457 fprintf(stderr
, "indent: set_option: internal error: p_type %d\n",