args.c revision 1.31 1 /* $NetBSD: args.c,v 1.31 2021/09/25 23:38:45 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.31 2021/09/25 23:38:45 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 <stdint.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63
64 #include "indent.h"
65
66 #define INDENT_VERSION "2.0"
67
68 /* profile types */
69 #define PRO_SPECIAL 1 /* special case */
70 #define PRO_BOOL 2 /* boolean */
71 #define PRO_INT 3 /* integer */
72
73 /* profile specials for specials */
74 #define IGN 1 /* ignore it */
75 #define CLI 2 /* case label indent (float) */
76 #define STDIN 3 /* use stdin */
77 #define KEY 4 /* type (keyword) */
78 #define KEY_FILE 5 /* only used for args */
79 #define VERSION 6 /* only used for args */
80
81 static void scan_profile(FILE *);
82
83 const char *option_source = "?";
84
85 void add_typedefs_from_file(const char *);
86
87 #if __STDC_VERSION__ >= 201112L
88 #define assert_type(expr, type) _Generic((expr), type : (expr))
89 #else
90 #define assert_type(expr, type) (expr)
91 #endif
92 #define bool_option(name, value, var) \
93 {name, PRO_BOOL, /*CONSTCOND*/(value) ? 1 : 0, \
94 assert_type(&(var), bool *)}
95 #define int_option(name, var) \
96 {name, PRO_INT, 0, assert_type(&(var), int *)}
97 #define special_option(name, value) \
98 {name, PRO_SPECIAL, assert_type(value, int), NULL}
99
100 /*
101 * N.B.: because of the way the table here is scanned, options whose names are
102 * a prefix of other options must occur later; that is, with -lp vs -l, -lp
103 * must be first and -l must be last.
104 */
105 static const struct pro {
106 const char p_name[9]; /* name, e.g. "bl", "cli" */
107 uint8_t p_type; /* type (int, bool, special) */
108 int p_special; /* depends on type */
109 void *p_obj; /* the associated variable (bool, int) */
110 } pro[] = {
111 special_option("T", KEY),
112 special_option("U", KEY_FILE),
113 special_option("-version", VERSION),
114 special_option("P", IGN),
115 bool_option("bacc", true, opt.blanklines_around_conditional_compilation),
116 bool_option("badp", true, opt.blanklines_after_declarations_at_proctop),
117 bool_option("bad", true, opt.blanklines_after_declarations),
118 bool_option("bap", true, opt.blanklines_after_procs),
119 bool_option("bbb", true, opt.blanklines_before_blockcomments),
120 bool_option("bc", false, opt.leave_comma),
121 bool_option("bl", false, opt.btype_2),
122 bool_option("br", true, opt.btype_2),
123 bool_option("bs", true, opt.blank_after_sizeof),
124 bool_option("cdb", true, opt.comment_delimiter_on_blankline),
125 int_option("cd", opt.decl_comment_column),
126 bool_option("ce", true, opt.cuddle_else),
127 int_option("ci", opt.continuation_indent),
128 special_option("cli", CLI),
129 bool_option("cs", true, opt.space_after_cast),
130 int_option("c", opt.comment_column),
131 int_option("di", opt.decl_indent),
132 bool_option("dj", true, opt.ljust_decl),
133 int_option("d", opt.unindent_displace),
134 bool_option("eei", true, opt.extra_expression_indent),
135 bool_option("ei", true, opt.else_if),
136 bool_option("fbs", true, opt.function_brace_split),
137 bool_option("fc1", true, opt.format_col1_comments),
138 bool_option("fcb", true, opt.format_block_comments),
139 bool_option("ip", true, opt.indent_parameters),
140 int_option("i", opt.indent_size),
141 int_option("lc", opt.block_comment_max_line_length),
142 int_option("ldi", opt.local_decl_indent),
143 bool_option("lpl", true, opt.lineup_to_parens_always),
144 bool_option("lp", true, opt.lineup_to_parens),
145 int_option("l", opt.max_line_length),
146 bool_option("nbacc", false, opt.blanklines_around_conditional_compilation),
147 bool_option("nbadp", false, opt.blanklines_after_declarations_at_proctop),
148 bool_option("nbad", false, opt.blanklines_after_declarations),
149 bool_option("nbap", false, opt.blanklines_after_procs),
150 bool_option("nbbb", false, opt.blanklines_before_blockcomments),
151 bool_option("nbc", true, opt.leave_comma),
152 bool_option("nbs", false, opt.blank_after_sizeof),
153 bool_option("ncdb", false, opt.comment_delimiter_on_blankline),
154 bool_option("nce", false, opt.cuddle_else),
155 bool_option("ncs", false, opt.space_after_cast),
156 bool_option("ndj", false, opt.ljust_decl),
157 bool_option("neei", false, opt.extra_expression_indent),
158 bool_option("nei", false, opt.else_if),
159 bool_option("nfbs", false, opt.function_brace_split),
160 bool_option("nfc1", false, opt.format_col1_comments),
161 bool_option("nfcb", false, opt.format_block_comments),
162 bool_option("nip", false, opt.indent_parameters),
163 bool_option("nlpl", false, opt.lineup_to_parens_always),
164 bool_option("nlp", false, opt.lineup_to_parens),
165 bool_option("npcs", false, opt.proc_calls_space),
166 special_option("npro", IGN),
167 bool_option("npsl", false, opt.procnames_start_line),
168 bool_option("nsc", false, opt.star_comment_cont),
169 bool_option("nsob", false, opt.swallow_optional_blanklines),
170 bool_option("nut", false, opt.use_tabs),
171 bool_option("nv", false, opt.verbose),
172 bool_option("pcs", true, opt.proc_calls_space),
173 bool_option("psl", true, opt.procnames_start_line),
174 bool_option("sc", true, opt.star_comment_cont),
175 bool_option("sob", true, opt.swallow_optional_blanklines),
176 special_option("st", STDIN),
177 bool_option("ta", true, opt.auto_typedefs),
178 int_option("ts", opt.tabsize),
179 bool_option("ut", true, opt.use_tabs),
180 bool_option("v", true, opt.verbose),
181 /* whew! */
182 {"", 0, 0, 0}
183 };
184
185 /*
186 * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
187 * given in these files.
188 */
189 void
190 set_profile(const char *profile_name)
191 {
192 FILE *f;
193 char fname[PATH_MAX];
194 static char prof[] = ".indent.pro";
195
196 if (profile_name == NULL)
197 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
198 else
199 snprintf(fname, sizeof(fname), "%s", profile_name + 2);
200 if ((f = fopen(option_source = fname, "r")) != NULL) {
201 scan_profile(f);
202 (void) fclose(f);
203 }
204 if ((f = fopen(option_source = prof, "r")) != NULL) {
205 scan_profile(f);
206 (void) fclose(f);
207 }
208 option_source = "Command line";
209 }
210
211 static void
212 scan_profile(FILE *f)
213 {
214 int comment_index, i;
215 char *p;
216 char buf[BUFSIZ];
217
218 for (;;) {
219 p = buf;
220 comment_index = 0;
221 while ((i = getc(f)) != EOF) {
222 if (i == '*' && comment_index == 0 && p > buf && p[-1] == '/') {
223 comment_index = (int)(p - buf);
224 *p++ = i;
225 } else if (i == '/' && comment_index != 0 && p > buf && p[-1] == '*') {
226 p = buf + comment_index - 1;
227 comment_index = 0;
228 } else if (isspace((unsigned char)i)) {
229 if (p > buf && comment_index == 0)
230 break;
231 } else {
232 *p++ = i;
233 }
234 }
235 if (p != buf) {
236 *p++ = 0;
237 if (opt.verbose)
238 printf("profile: %s\n", buf);
239 set_option(buf);
240 } else if (i == EOF)
241 return;
242 }
243 }
244
245 static const char *
246 skip_over(const char *s, const char *prefix)
247 {
248 while (*prefix != '\0') {
249 if (*prefix++ != *s++)
250 return NULL;
251 }
252 return s;
253 }
254
255 void
256 set_option(const char *arg)
257 {
258 const struct pro *p;
259 const char *param_start;
260
261 arg++; /* ignore leading "-" */
262 for (p = pro; p->p_name[0] != '\0'; p++)
263 if (p->p_name[0] == arg[0])
264 if ((param_start = skip_over(arg, p->p_name)) != NULL)
265 goto found;
266 errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
267
268 found:
269 switch (p->p_type) {
270
271 case PRO_SPECIAL:
272 switch (p->p_special) {
273
274 case IGN:
275 break;
276
277 case CLI:
278 if (param_start[0] == '\0')
279 goto need_param;
280 opt.case_indent = atof(param_start);
281 break;
282
283 case STDIN:
284 if (input == NULL)
285 input = stdin;
286 if (output == NULL)
287 output = stdout;
288 break;
289
290 case KEY:
291 if (param_start[0] == '\0')
292 goto need_param;
293 add_typename(param_start);
294 break;
295
296 case KEY_FILE:
297 if (param_start[0] == '\0')
298 goto need_param;
299 add_typedefs_from_file(param_start);
300 break;
301
302 case VERSION:
303 printf("FreeBSD indent %s\n", INDENT_VERSION);
304 exit(0);
305 /*NOTREACHED*/
306
307 default:
308 errx(1, "set_option: internal error: p_special %d", p->p_special);
309 }
310 break;
311
312 case PRO_BOOL:
313 *(bool *)p->p_obj = p->p_special != 0;
314 break;
315
316 case PRO_INT:
317 if (!isdigit((unsigned char)*param_start)) {
318 need_param:
319 errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
320 }
321 *(int *)p->p_obj = atoi(param_start);
322 break;
323
324 default:
325 errx(1, "set_option: internal error: p_type %d", p->p_type);
326 }
327 }
328
329 void
330 add_typedefs_from_file(const char *fname)
331 {
332 FILE *file;
333 char line[BUFSIZ];
334
335 if ((file = fopen(fname, "r")) == NULL) {
336 fprintf(stderr, "indent: cannot open file %s\n", fname);
337 exit(1);
338 }
339 while ((fgets(line, BUFSIZ, file)) != NULL) {
340 /* Remove trailing whitespace */
341 line[strcspn(line, " \t\n\r")] = '\0';
342 add_typename(line);
343 }
344 fclose(file);
345 }
346