2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1985 Sun Microsystems, Inc.
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
40 static char sccsid[] = "@(#)args.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * Argument scanning and profile reading code. Default parameters are set
58 #include "indent_globs.h"
62 #define PRO_SPECIAL 1 /* special case */
63 #define PRO_BOOL 2 /* boolean */
64 #define PRO_INT 3 /* integer */
66 /* profile specials for booleans */
67 #define ON 1 /* turn it on */
68 #define OFF 0 /* turn it off */
70 /* profile specials for specials */
71 #define IGN 1 /* ignore it */
72 #define CLI 2 /* case label indent (float) */
73 #define STDIN 3 /* use stdin */
74 #define KEY 4 /* type (keyword) */
76 static void scan_profile(FILE *);
78 #define KEY_FILE 5 /* only used for args */
80 const char *option_source = "?";
82 void add_typedefs_from_file(const char *str);
85 * N.B.: because of the way the table here is scanned, options whose names are
86 * substrings of other options must occur later; that is, with -lp vs -l, -lp
87 * must be first. Also, while (most) booleans occur more than once, the last
88 * default value is the one actually assigned.
91 const char *p_name; /* name, e.g. -bl, -cli */
92 int p_type; /* type (int, bool, special) */
93 int p_default; /* the default value (if int) */
94 int p_special; /* depends on type */
95 int *p_obj; /* the associated variable */
98 {"T", PRO_SPECIAL, 0, KEY, 0},
99 {"U", PRO_SPECIAL, 0, KEY_FILE, 0},
100 {"P", PRO_SPECIAL, 0, IGN, 0},
101 {"bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation},
102 {"badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop},
103 {"bad", PRO_BOOL, false, ON, &blanklines_after_declarations},
104 {"bap", PRO_BOOL, false, ON, &blanklines_after_procs},
105 {"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments},
106 {"bc", PRO_BOOL, true, OFF, &ps.leave_comma},
107 {"bl", PRO_BOOL, true, OFF, &btype_2},
108 {"br", PRO_BOOL, true, ON, &btype_2},
109 {"bs", PRO_BOOL, false, ON, &Bill_Shannon},
110 {"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline},
111 {"cd", PRO_INT, 0, 0, &ps.decl_com_ind},
112 {"ce", PRO_BOOL, true, ON, &cuddle_else},
113 {"ci", PRO_INT, 0, 0, &continuation_indent},
114 {"cli", PRO_SPECIAL, 0, CLI, 0},
115 {"c", PRO_INT, 33, 0, &ps.com_ind},
116 {"di", PRO_INT, 16, 0, &ps.decl_indent},
117 {"dj", PRO_BOOL, false, ON, &ps.ljust_decl},
118 {"d", PRO_INT, 0, 0, &ps.unindent_displace},
119 {"eei", PRO_BOOL, false, ON, &extra_expression_indent},
120 {"ei", PRO_BOOL, true, ON, &ps.else_if},
121 {"fbs", PRO_BOOL, true, ON, &function_brace_split},
122 {"fc1", PRO_BOOL, true, ON, &format_col1_comments},
123 {"fcb", PRO_BOOL, true, ON, &format_block_comments},
124 {"ip", PRO_BOOL, true, ON, &ps.indent_parameters},
125 {"i", PRO_INT, 8, 0, &ps.ind_size},
126 {"lc", PRO_INT, 0, 0, &block_comment_max_col},
127 {"ldi", PRO_INT, -1, 0, &ps.local_decl_indent},
128 {"lp", PRO_BOOL, true, ON, &lineup_to_parens},
129 {"l", PRO_INT, 78, 0, &max_col},
130 {"nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation},
131 {"nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop},
132 {"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations},
133 {"nbap", PRO_BOOL, false, OFF, &blanklines_after_procs},
134 {"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments},
135 {"nbc", PRO_BOOL, true, ON, &ps.leave_comma},
136 {"nbs", PRO_BOOL, false, OFF, &Bill_Shannon},
137 {"ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline},
138 {"nce", PRO_BOOL, true, OFF, &cuddle_else},
139 {"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl},
140 {"neei", PRO_BOOL, false, OFF, &extra_expression_indent},
141 {"nei", PRO_BOOL, true, OFF, &ps.else_if},
142 {"nfbs", PRO_BOOL, true, OFF, &function_brace_split},
143 {"nfc1", PRO_BOOL, true, OFF, &format_col1_comments},
144 {"nfcb", PRO_BOOL, true, OFF, &format_block_comments},
145 {"nip", PRO_BOOL, true, OFF, &ps.indent_parameters},
146 {"nlp", PRO_BOOL, true, OFF, &lineup_to_parens},
147 {"npcs", PRO_BOOL, false, OFF, &proc_calls_space},
148 {"npro", PRO_SPECIAL, 0, IGN, 0},
149 {"npsl", PRO_BOOL, true, OFF, &procnames_start_line},
150 {"nsac", PRO_BOOL, false, OFF, &space_after_cast},
151 {"nsc", PRO_BOOL, true, OFF, &star_comment_cont},
152 {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines},
153 {"nut", PRO_BOOL, true, OFF, &use_tabs},
154 {"nv", PRO_BOOL, false, OFF, &verbose},
155 {"pcs", PRO_BOOL, false, ON, &proc_calls_space},
156 {"psl", PRO_BOOL, true, ON, &procnames_start_line},
157 {"sac", PRO_BOOL, false, ON, &space_after_cast},
158 {"sc", PRO_BOOL, true, ON, &star_comment_cont},
159 {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines},
160 {"st", PRO_SPECIAL, 0, STDIN, 0},
161 {"ta", PRO_BOOL, false, ON, &auto_typedefs},
162 {"ts", PRO_INT, 8, 0, &tabsize},
163 {"ut", PRO_BOOL, true, ON, &use_tabs},
164 {"v", PRO_BOOL, false, ON, &verbose},
170 * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
171 * given in these files.
174 set_profile(const char *profile_name)
177 char fname[PATH_MAX];
178 static char prof[] = ".indent.pro";
180 if (profile_name == NULL)
181 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
183 snprintf(fname, sizeof(fname), "%s", profile_name + 2);
184 if ((f = fopen(option_source = fname, "r")) != NULL) {
188 if ((f = fopen(option_source = prof, "r")) != NULL) {
192 option_source = "Command line";
196 scan_profile(FILE *f)
205 while ((i = getc(f)) != EOF) {
206 if (i == '*' && !comment && p > buf && p[-1] == '/') {
209 } else if (i == '/' && comment && p > buf && p[-1] == '*') {
210 p = buf + comment - 1;
212 } else if (isspace((unsigned char)i)) {
213 if (p > buf && !comment)
222 printf("profile: %s\n", buf);
231 eqin(const char *s1, const char *s2)
249 * Because ps.case_indent is a float, we can't initialize it from the
252 ps.case_indent = 0.0; /* -cli0.0 */
253 for (p = pro; p->p_name; p++)
254 if (p->p_type != PRO_SPECIAL)
255 *p->p_obj = p->p_default;
259 set_option(char *arg)
262 const char *param_start;
264 arg++; /* ignore leading "-" */
265 for (p = pro; p->p_name; p++)
266 if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL)
268 errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
273 switch (p->p_special) {
279 if (*param_start == 0)
281 ps.case_indent = atof(param_start);
292 if (*param_start == 0)
294 add_typename(param_start);
298 if (*param_start == 0)
300 add_typedefs_from_file(param_start);
304 errx(1, "set_option: internal error: p_special %d", p->p_special);
309 if (p->p_special == OFF)
316 if (!isdigit((unsigned char)*param_start)) {
318 errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
320 *p->p_obj = atoi(param_start);
324 errx(1, "set_option: internal error: p_type %d", p->p_type);
329 add_typedefs_from_file(const char *str)
334 if ((file = fopen(str, "r")) == NULL) {
335 fprintf(stderr, "indent: cannot open file %s\n", str);
338 while ((fgets(line, BUFSIZ, file)) != NULL) {
339 /* Remove trailing whitespace */
340 line[strcspn(line, " \t\n\r")] = '\0';