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