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