args.c revision 1.35 1 1.35 rillig /* $NetBSD: args.c,v 1.35 2021/09/26 20:12:37 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.35 rillig __RCSID("$NetBSD: args.c,v 1.35 2021/09/26 20:12:37 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.14 kamil static void scan_profile(FILE *);
68 1.32 rillig void add_typedefs_from_file(const char *);
69 1.14 kamil
70 1.32 rillig static const char *option_source = "?";
71 1.14 kamil
72 1.25 rillig #if __STDC_VERSION__ >= 201112L
73 1.25 rillig #define assert_type(expr, type) _Generic((expr), type : (expr))
74 1.25 rillig #else
75 1.25 rillig #define assert_type(expr, type) (expr)
76 1.25 rillig #endif
77 1.25 rillig #define bool_option(name, value, var) \
78 1.32 rillig {name, true, value, assert_type(&(var), bool *)}
79 1.31 rillig #define int_option(name, var) \
80 1.32 rillig {name, false, false, assert_type(&(var), int *)}
81 1.35 rillig #define bool_options(name, var) \
82 1.35 rillig bool_option(name, true, var), \
83 1.35 rillig bool_option("n" name, false, var)
84 1.25 rillig
85 1.1 cgd /*
86 1.1 cgd * N.B.: because of the way the table here is scanned, options whose names are
87 1.29 rillig * a prefix of other options must occur later; that is, with -lp vs -l, -lp
88 1.29 rillig * must be first and -l must be last.
89 1.32 rillig *
90 1.32 rillig * See also set_special_option.
91 1.1 cgd */
92 1.29 rillig static const struct pro {
93 1.33 rillig const char p_name[6]; /* name, e.g. "bl", "cli" */
94 1.33 rillig bool p_is_bool;
95 1.33 rillig bool p_bool_value;
96 1.33 rillig void *p_obj; /* the associated variable (bool, int) */
97 1.33 rillig } pro[] = {
98 1.35 rillig bool_options("bacc", opt.blanklines_around_conditional_compilation),
99 1.35 rillig bool_options("badp", opt.blanklines_after_declarations_at_proctop),
100 1.35 rillig bool_options("bad", opt.blanklines_after_declarations),
101 1.35 rillig bool_options("bap", opt.blanklines_after_procs),
102 1.35 rillig bool_options("bbb", opt.blanklines_before_blockcomments),
103 1.35 rillig bool_options("bc", opt.break_after_comma),
104 1.31 rillig bool_option("bl", false, opt.btype_2),
105 1.31 rillig bool_option("br", true, opt.btype_2),
106 1.35 rillig bool_options("bs", opt.blank_after_sizeof),
107 1.35 rillig bool_options("cdb", opt.comment_delimiter_on_blankline),
108 1.31 rillig int_option("cd", opt.decl_comment_column),
109 1.35 rillig bool_options("ce", opt.cuddle_else),
110 1.31 rillig int_option("ci", opt.continuation_indent),
111 1.35 rillig bool_options("cs", opt.space_after_cast),
112 1.31 rillig int_option("c", opt.comment_column),
113 1.31 rillig int_option("di", opt.decl_indent),
114 1.35 rillig bool_options("dj", opt.ljust_decl),
115 1.31 rillig int_option("d", opt.unindent_displace),
116 1.35 rillig bool_options("eei", opt.extra_expression_indent),
117 1.35 rillig bool_options("ei", opt.else_if),
118 1.35 rillig bool_options("fbs", opt.function_brace_split),
119 1.35 rillig bool_options("fc1", opt.format_col1_comments),
120 1.35 rillig bool_options("fcb", opt.format_block_comments),
121 1.35 rillig bool_options("ip", opt.indent_parameters),
122 1.31 rillig int_option("i", opt.indent_size),
123 1.31 rillig int_option("lc", opt.block_comment_max_line_length),
124 1.31 rillig int_option("ldi", opt.local_decl_indent),
125 1.35 rillig bool_options("lpl", opt.lineup_to_parens_always),
126 1.35 rillig bool_options("lp", opt.lineup_to_parens),
127 1.31 rillig int_option("l", opt.max_line_length),
128 1.35 rillig bool_options("pcs", opt.proc_calls_space),
129 1.35 rillig bool_options("psl", opt.procnames_start_line),
130 1.35 rillig bool_options("sc", opt.star_comment_cont),
131 1.35 rillig bool_options("sob", opt.swallow_optional_blanklines),
132 1.31 rillig bool_option("ta", true, opt.auto_typedefs),
133 1.31 rillig int_option("ts", opt.tabsize),
134 1.35 rillig bool_options("ut", opt.use_tabs),
135 1.35 rillig bool_options("v", opt.verbose),
136 1.1 cgd };
137 1.14 kamil
138 1.1 cgd /*
139 1.1 cgd * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
140 1.1 cgd * given in these files.
141 1.1 cgd */
142 1.5 lukem void
143 1.14 kamil set_profile(const char *profile_name)
144 1.1 cgd {
145 1.14 kamil FILE *f;
146 1.14 kamil char fname[PATH_MAX];
147 1.14 kamil static char prof[] = ".indent.pro";
148 1.5 lukem
149 1.14 kamil if (profile_name == NULL)
150 1.8 itojun snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
151 1.14 kamil else
152 1.14 kamil snprintf(fname, sizeof(fname), "%s", profile_name + 2);
153 1.14 kamil if ((f = fopen(option_source = fname, "r")) != NULL) {
154 1.14 kamil scan_profile(f);
155 1.33 rillig (void)fclose(f);
156 1.14 kamil }
157 1.14 kamil if ((f = fopen(option_source = prof, "r")) != NULL) {
158 1.14 kamil scan_profile(f);
159 1.33 rillig (void)fclose(f);
160 1.14 kamil }
161 1.14 kamil option_source = "Command line";
162 1.1 cgd }
163 1.1 cgd
164 1.14 kamil static void
165 1.7 wiz scan_profile(FILE *f)
166 1.1 cgd {
167 1.33 rillig int comment_index, i;
168 1.33 rillig char *p;
169 1.33 rillig char buf[BUFSIZ];
170 1.14 kamil
171 1.22 rillig for (;;) {
172 1.14 kamil p = buf;
173 1.15 rillig comment_index = 0;
174 1.14 kamil while ((i = getc(f)) != EOF) {
175 1.25 rillig if (i == '*' && comment_index == 0 && p > buf && p[-1] == '/') {
176 1.22 rillig comment_index = (int)(p - buf);
177 1.14 kamil *p++ = i;
178 1.25 rillig } else if (i == '/' && comment_index != 0 && p > buf && p[-1] == '*') {
179 1.15 rillig p = buf + comment_index - 1;
180 1.15 rillig comment_index = 0;
181 1.14 kamil } else if (isspace((unsigned char)i)) {
182 1.25 rillig if (p > buf && comment_index == 0)
183 1.14 kamil break;
184 1.14 kamil } else {
185 1.14 kamil *p++ = i;
186 1.14 kamil }
187 1.1 cgd }
188 1.14 kamil if (p != buf) {
189 1.14 kamil *p++ = 0;
190 1.14 kamil if (opt.verbose)
191 1.14 kamil printf("profile: %s\n", buf);
192 1.14 kamil set_option(buf);
193 1.19 rillig } else if (i == EOF)
194 1.14 kamil return;
195 1.14 kamil }
196 1.1 cgd }
197 1.1 cgd
198 1.14 kamil static const char *
199 1.30 rillig skip_over(const char *s, const char *prefix)
200 1.1 cgd {
201 1.30 rillig while (*prefix != '\0') {
202 1.30 rillig if (*prefix++ != *s++)
203 1.16 rillig return NULL;
204 1.14 kamil }
205 1.30 rillig return s;
206 1.1 cgd }
207 1.14 kamil
208 1.32 rillig static bool
209 1.32 rillig set_special_option(const char *arg)
210 1.32 rillig {
211 1.32 rillig const char *arg_end;
212 1.32 rillig
213 1.32 rillig if (strncmp(arg, "-version", 8) == 0) {
214 1.32 rillig printf("FreeBSD indent %s\n", INDENT_VERSION);
215 1.32 rillig exit(0);
216 1.33 rillig /* NOTREACHED */
217 1.32 rillig }
218 1.32 rillig
219 1.32 rillig if (arg[0] == 'P' || strncmp(arg, "npro", 4) == 0)
220 1.32 rillig return true;
221 1.32 rillig
222 1.32 rillig if (strncmp(arg, "cli", 3) == 0) {
223 1.32 rillig arg_end = arg + 3;
224 1.32 rillig if (arg_end[0] == '\0')
225 1.32 rillig goto need_param;
226 1.32 rillig opt.case_indent = atof(arg_end);
227 1.32 rillig return true;
228 1.32 rillig }
229 1.32 rillig
230 1.32 rillig if (strncmp(arg, "st", 2) == 0) {
231 1.32 rillig if (input == NULL)
232 1.32 rillig input = stdin;
233 1.32 rillig if (output == NULL)
234 1.32 rillig output = stdout;
235 1.32 rillig return true;
236 1.32 rillig }
237 1.32 rillig
238 1.32 rillig if (arg[0] == 'T') {
239 1.32 rillig arg_end = arg + 1;
240 1.32 rillig if (arg_end[0] == '\0')
241 1.32 rillig goto need_param;
242 1.32 rillig add_typename(arg_end);
243 1.32 rillig return true;
244 1.32 rillig }
245 1.32 rillig
246 1.32 rillig if (arg[0] == 'U') {
247 1.32 rillig arg_end = arg + 1;
248 1.32 rillig if (arg_end[0] == '\0')
249 1.32 rillig goto need_param;
250 1.32 rillig add_typedefs_from_file(arg_end);
251 1.32 rillig return true;
252 1.32 rillig }
253 1.32 rillig
254 1.32 rillig return false;
255 1.32 rillig
256 1.32 rillig need_param:
257 1.32 rillig errx(1, "%s: ``%.*s'' requires a parameter",
258 1.32 rillig option_source, (int)(arg_end - arg), arg);
259 1.32 rillig /* NOTREACHED */
260 1.32 rillig }
261 1.32 rillig
262 1.5 lukem void
263 1.29 rillig set_option(const char *arg)
264 1.1 cgd {
265 1.17 rillig const struct pro *p;
266 1.33 rillig const char *param_start;
267 1.1 cgd
268 1.14 kamil arg++; /* ignore leading "-" */
269 1.32 rillig if (set_special_option(arg))
270 1.32 rillig return;
271 1.32 rillig
272 1.32 rillig for (p = pro; p != pro + nitems(pro); p++)
273 1.30 rillig if (p->p_name[0] == arg[0])
274 1.30 rillig if ((param_start = skip_over(arg, p->p_name)) != NULL)
275 1.30 rillig goto found;
276 1.14 kamil errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
277 1.30 rillig
278 1.1 cgd found:
279 1.32 rillig if (p->p_is_bool)
280 1.32 rillig *(bool *)p->p_obj = p->p_bool_value;
281 1.32 rillig else {
282 1.32 rillig if (!isdigit((unsigned char)*param_start))
283 1.32 rillig errx(1, "%s: ``%s'' requires a parameter",
284 1.32 rillig option_source, p->p_name);
285 1.25 rillig *(int *)p->p_obj = atoi(param_start);
286 1.14 kamil }
287 1.14 kamil }
288 1.14 kamil
289 1.14 kamil void
290 1.30 rillig add_typedefs_from_file(const char *fname)
291 1.14 kamil {
292 1.14 kamil FILE *file;
293 1.14 kamil char line[BUFSIZ];
294 1.14 kamil
295 1.30 rillig if ((file = fopen(fname, "r")) == NULL) {
296 1.30 rillig fprintf(stderr, "indent: cannot open file %s\n", fname);
297 1.14 kamil exit(1);
298 1.14 kamil }
299 1.14 kamil while ((fgets(line, BUFSIZ, file)) != NULL) {
300 1.14 kamil /* Remove trailing whitespace */
301 1.14 kamil line[strcspn(line, " \t\n\r")] = '\0';
302 1.14 kamil add_typename(line);
303 1.14 kamil }
304 1.14 kamil fclose(file);
305 1.1 cgd }
306