args.c revision 1.64 1 1.64 rillig /* $NetBSD: args.c,v 1.64 2021/10/28 21:32:48 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.64 rillig __RCSID("$NetBSD: args.c,v 1.64 2021/10/28 21:32:48 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.50 rillig /* Read options from profile files and from the command line. */
52 1.1 cgd
53 1.5 lukem #include <ctype.h>
54 1.12 ginsbach #include <err.h>
55 1.57 rillig #include <errno.h>
56 1.14 kamil #include <limits.h>
57 1.1 cgd #include <stdio.h>
58 1.1 cgd #include <stdlib.h>
59 1.1 cgd #include <string.h>
60 1.15 rillig
61 1.14 kamil #include "indent.h"
62 1.14 kamil
63 1.25 rillig #if __STDC_VERSION__ >= 201112L
64 1.25 rillig #define assert_type(expr, type) _Generic((expr), type : (expr))
65 1.25 rillig #else
66 1.25 rillig #define assert_type(expr, type) (expr)
67 1.25 rillig #endif
68 1.54 rillig
69 1.25 rillig #define bool_option(name, value, var) \
70 1.56 rillig {name, true, value, false, 0, 0, assert_type(&(opt.var), bool *)}
71 1.54 rillig #define bool_options(name, var) \
72 1.56 rillig {name, true, false, true, 0, 0, assert_type(&(opt.var), bool *)}
73 1.56 rillig #define int_option(name, var, min, max) \
74 1.56 rillig {name, false, false, false, min, max, assert_type(&(opt.var), int *)}
75 1.25 rillig
76 1.63 rillig /* See set_special_option for special options. */
77 1.29 rillig static const struct pro {
78 1.54 rillig const char p_name[5]; /* e.g. "bl", "cli" */
79 1.33 rillig bool p_is_bool;
80 1.33 rillig bool p_bool_value;
81 1.37 rillig bool p_may_negate;
82 1.56 rillig short i_min;
83 1.56 rillig short i_max;
84 1.39 rillig void *p_var; /* the associated variable */
85 1.60 rillig } pro[] = {
86 1.38 rillig bool_options("bacc", blanklines_around_conditional_compilation),
87 1.52 rillig bool_options("bad", blanklines_after_decl),
88 1.52 rillig bool_options("badp", blanklines_after_decl_at_top),
89 1.38 rillig bool_options("bap", blanklines_after_procs),
90 1.52 rillig bool_options("bbb", blanklines_before_block_comments),
91 1.38 rillig bool_options("bc", break_after_comma),
92 1.51 rillig bool_option("bl", false, brace_same_line),
93 1.51 rillig bool_option("br", true, brace_same_line),
94 1.38 rillig bool_options("bs", blank_after_sizeof),
95 1.56 rillig int_option("c", comment_column, 1, 999),
96 1.56 rillig int_option("cd", decl_comment_column, 1, 999),
97 1.38 rillig bool_options("cdb", comment_delimiter_on_blankline),
98 1.38 rillig bool_options("ce", cuddle_else),
99 1.56 rillig int_option("ci", continuation_indent, 0, 999),
100 1.36 rillig /* "cli" is special */
101 1.38 rillig bool_options("cs", space_after_cast),
102 1.56 rillig int_option("d", unindent_displace, -999, 999),
103 1.56 rillig int_option("di", decl_indent, 0, 999),
104 1.38 rillig bool_options("dj", ljust_decl),
105 1.52 rillig bool_options("eei", extra_expr_indent),
106 1.38 rillig bool_options("ei", else_if),
107 1.38 rillig bool_options("fbs", function_brace_split),
108 1.38 rillig bool_options("fc1", format_col1_comments),
109 1.38 rillig bool_options("fcb", format_block_comments),
110 1.56 rillig int_option("i", indent_size, 1, 80),
111 1.38 rillig bool_options("ip", indent_parameters),
112 1.56 rillig int_option("l", max_line_length, 1, 999),
113 1.56 rillig int_option("lc", block_comment_max_line_length, 1, 999),
114 1.56 rillig int_option("ldi", local_decl_indent, 0, 999),
115 1.38 rillig bool_options("lp", lineup_to_parens),
116 1.38 rillig bool_options("lpl", lineup_to_parens_always),
117 1.36 rillig /* "npro" is special */
118 1.36 rillig /* "P" is special */
119 1.38 rillig bool_options("pcs", proc_calls_space),
120 1.38 rillig bool_options("psl", procnames_start_line),
121 1.38 rillig bool_options("sc", star_comment_cont),
122 1.38 rillig bool_options("sob", swallow_optional_blanklines),
123 1.36 rillig /* "st" is special */
124 1.38 rillig bool_option("ta", true, auto_typedefs),
125 1.36 rillig /* "T" is special */
126 1.56 rillig int_option("ts", tabsize, 1, 80),
127 1.36 rillig /* "U" is special */
128 1.38 rillig bool_options("ut", use_tabs),
129 1.38 rillig bool_options("v", verbose),
130 1.1 cgd };
131 1.14 kamil
132 1.14 kamil
133 1.53 rillig static void
134 1.53 rillig add_typedefs_from_file(const char *fname)
135 1.53 rillig {
136 1.53 rillig FILE *file;
137 1.53 rillig char line[BUFSIZ];
138 1.53 rillig
139 1.53 rillig if ((file = fopen(fname, "r")) == NULL) {
140 1.53 rillig fprintf(stderr, "indent: cannot open file %s\n", fname);
141 1.53 rillig exit(1);
142 1.53 rillig }
143 1.53 rillig while ((fgets(line, BUFSIZ, file)) != NULL) {
144 1.53 rillig /* Remove trailing whitespace */
145 1.53 rillig line[strcspn(line, " \t\n\r")] = '\0';
146 1.53 rillig add_typename(line);
147 1.53 rillig }
148 1.53 rillig (void)fclose(file);
149 1.53 rillig }
150 1.53 rillig
151 1.32 rillig static bool
152 1.49 rillig set_special_option(const char *arg, const char *option_source)
153 1.32 rillig {
154 1.32 rillig const char *arg_end;
155 1.32 rillig
156 1.32 rillig if (strncmp(arg, "-version", 8) == 0) {
157 1.61 rillig printf("NetBSD indent 2.1\n");
158 1.32 rillig exit(0);
159 1.32 rillig }
160 1.32 rillig
161 1.32 rillig if (arg[0] == 'P' || strncmp(arg, "npro", 4) == 0)
162 1.32 rillig return true;
163 1.32 rillig
164 1.32 rillig if (strncmp(arg, "cli", 3) == 0) {
165 1.32 rillig arg_end = arg + 3;
166 1.32 rillig if (arg_end[0] == '\0')
167 1.32 rillig goto need_param;
168 1.63 rillig char *end;
169 1.63 rillig opt.case_indent = (float)strtod(arg_end, &end);
170 1.63 rillig if (*end != '\0')
171 1.63 rillig errx(1, "%s: argument \"%s\" to option \"-%.*s\" must be numeric",
172 1.63 rillig option_source, arg_end, (int)(arg_end - arg), arg);
173 1.32 rillig return true;
174 1.32 rillig }
175 1.32 rillig
176 1.32 rillig if (strncmp(arg, "st", 2) == 0) {
177 1.32 rillig if (input == NULL)
178 1.32 rillig input = stdin;
179 1.32 rillig if (output == NULL)
180 1.32 rillig output = stdout;
181 1.32 rillig return true;
182 1.32 rillig }
183 1.32 rillig
184 1.32 rillig if (arg[0] == 'T') {
185 1.32 rillig arg_end = arg + 1;
186 1.32 rillig if (arg_end[0] == '\0')
187 1.32 rillig goto need_param;
188 1.32 rillig add_typename(arg_end);
189 1.32 rillig return true;
190 1.32 rillig }
191 1.32 rillig
192 1.32 rillig if (arg[0] == 'U') {
193 1.32 rillig arg_end = arg + 1;
194 1.32 rillig if (arg_end[0] == '\0')
195 1.32 rillig goto need_param;
196 1.32 rillig add_typedefs_from_file(arg_end);
197 1.32 rillig return true;
198 1.32 rillig }
199 1.32 rillig
200 1.32 rillig return false;
201 1.32 rillig
202 1.32 rillig need_param:
203 1.64 rillig errx(1, "%s: ``%.*s'' requires an argument",
204 1.32 rillig option_source, (int)(arg_end - arg), arg);
205 1.32 rillig /* NOTREACHED */
206 1.32 rillig }
207 1.32 rillig
208 1.62 rillig static const char *
209 1.62 rillig skip_over(const char *s, bool may_negate, const char *prefix)
210 1.62 rillig {
211 1.62 rillig if (may_negate && s[0] == 'n')
212 1.62 rillig s++;
213 1.62 rillig while (*prefix != '\0') {
214 1.62 rillig if (*prefix++ != *s++)
215 1.62 rillig return NULL;
216 1.62 rillig }
217 1.62 rillig return s;
218 1.62 rillig }
219 1.62 rillig
220 1.5 lukem void
221 1.49 rillig set_option(const char *arg, const char *option_source)
222 1.1 cgd {
223 1.17 rillig const struct pro *p;
224 1.64 rillig const char *arg_arg;
225 1.1 cgd
226 1.46 rillig arg++; /* skip leading '-' */
227 1.49 rillig if (set_special_option(arg, option_source))
228 1.32 rillig return;
229 1.32 rillig
230 1.64 rillig for (p = pro + array_length(pro); p-- != pro;)
231 1.64 rillig if ((arg_arg = skip_over(arg, p->p_may_negate, p->p_name)) != NULL)
232 1.37 rillig goto found;
233 1.55 rillig errx(1, "%s: unknown option \"-%s\"", option_source, arg);
234 1.64 rillig found:
235 1.30 rillig
236 1.47 rillig if (p->p_is_bool) {
237 1.64 rillig if (arg_arg[0] != '\0')
238 1.55 rillig errx(1, "%s: unknown option \"-%s\"", option_source, arg);
239 1.64 rillig
240 1.39 rillig *(bool *)p->p_var = p->p_may_negate ? arg[0] != 'n' : p->p_bool_value;
241 1.64 rillig return;
242 1.14 kamil }
243 1.64 rillig
244 1.64 rillig errno = 0;
245 1.64 rillig char *end;
246 1.64 rillig long num = strtol(arg_arg, &end, 10);
247 1.64 rillig if (!(errno == 0 && *end == '\0'))
248 1.64 rillig errx(1, "%s: argument \"%s\" to option \"-%s\" must be an integer",
249 1.64 rillig option_source, arg_arg, p->p_name);
250 1.64 rillig
251 1.64 rillig if (!(isdigit((unsigned char)*arg_arg) &&
252 1.64 rillig p->i_min <= num && num <= p->i_max))
253 1.64 rillig errx(1,
254 1.64 rillig "%s: argument \"%s\" to option \"-%s\" must be between %d and %d",
255 1.64 rillig option_source, arg_arg, p->p_name, p->i_min, p->i_max);
256 1.64 rillig
257 1.64 rillig *(int *)p->p_var = (int)num;
258 1.14 kamil }
259 1.62 rillig
260 1.62 rillig static void
261 1.62 rillig load_profile(const char *fname, bool must_exist)
262 1.62 rillig {
263 1.62 rillig FILE *f;
264 1.62 rillig
265 1.62 rillig if ((f = fopen(fname, "r")) == NULL) {
266 1.62 rillig if (must_exist)
267 1.62 rillig err(EXIT_FAILURE, "profile %s", fname);
268 1.62 rillig return;
269 1.62 rillig }
270 1.62 rillig
271 1.62 rillig for (;;) {
272 1.62 rillig char buf[BUFSIZ];
273 1.62 rillig size_t n = 0;
274 1.62 rillig int ch, comment_ch = -1;
275 1.62 rillig
276 1.62 rillig while ((ch = getc(f)) != EOF) {
277 1.62 rillig if (ch == '*' && comment_ch < 0 && n > 0 && buf[n - 1] == '/') {
278 1.62 rillig n--;
279 1.62 rillig comment_ch = ch;
280 1.62 rillig } else if (comment_ch >= 0) {
281 1.62 rillig comment_ch = ch == '/' && comment_ch == '*' ? -1 : ch;
282 1.62 rillig } else if (isspace((unsigned char)ch)) {
283 1.62 rillig break;
284 1.62 rillig } else if (n >= array_length(buf) - 5) {
285 1.62 rillig diag(1, "buffer overflow in %s, starting with '%.10s'",
286 1.62 rillig fname, buf);
287 1.62 rillig exit(1);
288 1.62 rillig } else
289 1.62 rillig buf[n++] = (char)ch;
290 1.62 rillig }
291 1.62 rillig
292 1.62 rillig if (n > 0) {
293 1.62 rillig buf[n] = '\0';
294 1.62 rillig if (opt.verbose)
295 1.62 rillig printf("profile: %s\n", buf);
296 1.62 rillig set_option(buf, fname);
297 1.62 rillig } else if (ch == EOF)
298 1.62 rillig break;
299 1.62 rillig }
300 1.62 rillig (void)fclose(f);
301 1.62 rillig }
302 1.62 rillig
303 1.62 rillig void
304 1.62 rillig load_profiles(const char *profile_name)
305 1.62 rillig {
306 1.62 rillig char fname[PATH_MAX];
307 1.62 rillig
308 1.62 rillig if (profile_name != NULL)
309 1.62 rillig load_profile(profile_name, true);
310 1.62 rillig else {
311 1.62 rillig snprintf(fname, sizeof(fname), "%s/.indent.pro", getenv("HOME"));
312 1.62 rillig load_profile(fname, false);
313 1.62 rillig }
314 1.62 rillig load_profile(".indent.pro", false);
315 1.62 rillig }
316