args.c revision 1.32 1 /* $NetBSD: args.c,v 1.32 2021/09/26 00:57:28 rillig Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #if 0
41 static char sccsid[] = "@(#)args.c 8.1 (Berkeley) 6/6/93";
42 #endif
43
44 #include <sys/cdefs.h>
45 #if defined(__NetBSD__)
46 __RCSID("$NetBSD: args.c,v 1.32 2021/09/26 00:57:28 rillig Exp $");
47 #elif defined(__FreeBSD__)
48 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
49 #endif
50
51 /*
52 * Argument scanning and profile reading code. Default parameters are set
53 * here as well.
54 */
55
56 #include <ctype.h>
57 #include <err.h>
58 #include <limits.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #include "indent.h"
64
65 #define INDENT_VERSION "2.0"
66
67 static void scan_profile(FILE *);
68 void add_typedefs_from_file(const char *);
69
70 static const char *option_source = "?";
71
72 #if __STDC_VERSION__ >= 201112L
73 #define assert_type(expr, type) _Generic((expr), type : (expr))
74 #else
75 #define assert_type(expr, type) (expr)
76 #endif
77 #define bool_option(name, value, var) \
78 {name, true, value, assert_type(&(var), bool *)}
79 #define int_option(name, var) \
80 {name, false, false, assert_type(&(var), int *)}
81
82 /*
83 * N.B.: because of the way the table here is scanned, options whose names are
84 * a prefix of other options must occur later; that is, with -lp vs -l, -lp
85 * must be first and -l must be last.
86 *
87 * See also set_special_option.
88 */
89 static const struct pro {
90 const char p_name[6]; /* name, e.g. "bl", "cli" */
91 bool p_is_bool;
92 bool p_bool_value;
93 void *p_obj; /* the associated variable (bool, int) */
94 } pro[] = {
95 bool_option("bacc", true, opt.blanklines_around_conditional_compilation),
96 bool_option("badp", true, opt.blanklines_after_declarations_at_proctop),
97 bool_option("bad", true, opt.blanklines_after_declarations),
98 bool_option("bap", true, opt.blanklines_after_procs),
99 bool_option("bbb", true, opt.blanklines_before_blockcomments),
100 bool_option("bc", false, opt.leave_comma),
101 bool_option("bl", false, opt.btype_2),
102 bool_option("br", true, opt.btype_2),
103 bool_option("bs", true, opt.blank_after_sizeof),
104 bool_option("cdb", true, opt.comment_delimiter_on_blankline),
105 int_option("cd", opt.decl_comment_column),
106 bool_option("ce", true, opt.cuddle_else),
107 int_option("ci", opt.continuation_indent),
108 bool_option("cs", true, opt.space_after_cast),
109 int_option("c", opt.comment_column),
110 int_option("di", opt.decl_indent),
111 bool_option("dj", true, opt.ljust_decl),
112 int_option("d", opt.unindent_displace),
113 bool_option("eei", true, opt.extra_expression_indent),
114 bool_option("ei", true, opt.else_if),
115 bool_option("fbs", true, opt.function_brace_split),
116 bool_option("fc1", true, opt.format_col1_comments),
117 bool_option("fcb", true, opt.format_block_comments),
118 bool_option("ip", true, opt.indent_parameters),
119 int_option("i", opt.indent_size),
120 int_option("lc", opt.block_comment_max_line_length),
121 int_option("ldi", opt.local_decl_indent),
122 bool_option("lpl", true, opt.lineup_to_parens_always),
123 bool_option("lp", true, opt.lineup_to_parens),
124 int_option("l", opt.max_line_length),
125 bool_option("nbacc", false, opt.blanklines_around_conditional_compilation),
126 bool_option("nbadp", false, opt.blanklines_after_declarations_at_proctop),
127 bool_option("nbad", false, opt.blanklines_after_declarations),
128 bool_option("nbap", false, opt.blanklines_after_procs),
129 bool_option("nbbb", false, opt.blanklines_before_blockcomments),
130 bool_option("nbc", true, opt.leave_comma),
131 bool_option("nbs", false, opt.blank_after_sizeof),
132 bool_option("ncdb", false, opt.comment_delimiter_on_blankline),
133 bool_option("nce", false, opt.cuddle_else),
134 bool_option("ncs", false, opt.space_after_cast),
135 bool_option("ndj", false, opt.ljust_decl),
136 bool_option("neei", false, opt.extra_expression_indent),
137 bool_option("nei", false, opt.else_if),
138 bool_option("nfbs", false, opt.function_brace_split),
139 bool_option("nfc1", false, opt.format_col1_comments),
140 bool_option("nfcb", false, opt.format_block_comments),
141 bool_option("nip", false, opt.indent_parameters),
142 bool_option("nlpl", false, opt.lineup_to_parens_always),
143 bool_option("nlp", false, opt.lineup_to_parens),
144 bool_option("npcs", false, opt.proc_calls_space),
145 bool_option("npsl", false, opt.procnames_start_line),
146 bool_option("nsc", false, opt.star_comment_cont),
147 bool_option("nsob", false, opt.swallow_optional_blanklines),
148 bool_option("nut", false, opt.use_tabs),
149 bool_option("nv", false, opt.verbose),
150 bool_option("pcs", true, opt.proc_calls_space),
151 bool_option("psl", true, opt.procnames_start_line),
152 bool_option("sc", true, opt.star_comment_cont),
153 bool_option("sob", true, opt.swallow_optional_blanklines),
154 bool_option("ta", true, opt.auto_typedefs),
155 int_option("ts", opt.tabsize),
156 bool_option("ut", true, opt.use_tabs),
157 bool_option("v", true, opt.verbose),
158 };
159
160 /*
161 * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
162 * given in these files.
163 */
164 void
165 set_profile(const char *profile_name)
166 {
167 FILE *f;
168 char fname[PATH_MAX];
169 static char prof[] = ".indent.pro";
170
171 if (profile_name == NULL)
172 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
173 else
174 snprintf(fname, sizeof(fname), "%s", profile_name + 2);
175 if ((f = fopen(option_source = fname, "r")) != NULL) {
176 scan_profile(f);
177 (void) fclose(f);
178 }
179 if ((f = fopen(option_source = prof, "r")) != NULL) {
180 scan_profile(f);
181 (void) fclose(f);
182 }
183 option_source = "Command line";
184 }
185
186 static void
187 scan_profile(FILE *f)
188 {
189 int comment_index, i;
190 char *p;
191 char buf[BUFSIZ];
192
193 for (;;) {
194 p = buf;
195 comment_index = 0;
196 while ((i = getc(f)) != EOF) {
197 if (i == '*' && comment_index == 0 && p > buf && p[-1] == '/') {
198 comment_index = (int)(p - buf);
199 *p++ = i;
200 } else if (i == '/' && comment_index != 0 && p > buf && p[-1] == '*') {
201 p = buf + comment_index - 1;
202 comment_index = 0;
203 } else if (isspace((unsigned char)i)) {
204 if (p > buf && comment_index == 0)
205 break;
206 } else {
207 *p++ = i;
208 }
209 }
210 if (p != buf) {
211 *p++ = 0;
212 if (opt.verbose)
213 printf("profile: %s\n", buf);
214 set_option(buf);
215 } else if (i == EOF)
216 return;
217 }
218 }
219
220 static const char *
221 skip_over(const char *s, const char *prefix)
222 {
223 while (*prefix != '\0') {
224 if (*prefix++ != *s++)
225 return NULL;
226 }
227 return s;
228 }
229
230 static bool
231 set_special_option(const char *arg)
232 {
233 const char *arg_end;
234
235 if (strncmp(arg, "-version", 8) == 0) {
236 printf("FreeBSD indent %s\n", INDENT_VERSION);
237 exit(0);
238 /*NOTREACHED*/
239 }
240
241 if (arg[0] == 'P' || strncmp(arg, "npro", 4) == 0)
242 return true;
243
244 if (strncmp(arg, "cli", 3) == 0) {
245 arg_end = arg + 3;
246 if (arg_end[0] == '\0')
247 goto need_param;
248 opt.case_indent = atof(arg_end);
249 return true;
250 }
251
252 if (strncmp(arg, "st", 2) == 0) {
253 if (input == NULL)
254 input = stdin;
255 if (output == NULL)
256 output = stdout;
257 return true;
258 }
259
260 if (arg[0] == 'T') {
261 arg_end = arg + 1;
262 if (arg_end[0] == '\0')
263 goto need_param;
264 add_typename(arg_end);
265 return true;
266 }
267
268 if (arg[0] == 'U') {
269 arg_end = arg + 1;
270 if (arg_end[0] == '\0')
271 goto need_param;
272 add_typedefs_from_file(arg_end);
273 return true;
274 }
275
276 return false;
277
278 need_param:
279 errx(1, "%s: ``%.*s'' requires a parameter",
280 option_source, (int)(arg_end - arg), arg);
281 /* NOTREACHED */
282 }
283
284 void
285 set_option(const char *arg)
286 {
287 const struct pro *p;
288 const char *param_start;
289
290 arg++; /* ignore leading "-" */
291 if (set_special_option(arg))
292 return;
293
294 for (p = pro; p != pro + nitems(pro); p++)
295 if (p->p_name[0] == arg[0])
296 if ((param_start = skip_over(arg, p->p_name)) != NULL)
297 goto found;
298 errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
299
300 found:
301 if (p->p_is_bool)
302 *(bool *)p->p_obj = p->p_bool_value;
303 else {
304 if (!isdigit((unsigned char)*param_start))
305 errx(1, "%s: ``%s'' requires a parameter",
306 option_source, p->p_name);
307 *(int *)p->p_obj = atoi(param_start);
308 }
309 }
310
311 void
312 add_typedefs_from_file(const char *fname)
313 {
314 FILE *file;
315 char line[BUFSIZ];
316
317 if ((file = fopen(fname, "r")) == NULL) {
318 fprintf(stderr, "indent: cannot open file %s\n", fname);
319 exit(1);
320 }
321 while ((fgets(line, BUFSIZ, file)) != NULL) {
322 /* Remove trailing whitespace */
323 line[strcspn(line, " \t\n\r")] = '\0';
324 add_typename(line);
325 }
326 fclose(file);
327 }
328