xref: /freebsd/usr.bin/indent/args.c (revision fd45b686f9d92f583366c75b22c04c7ee49709c0)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1985 Sun Microsystems, Inc.
5  * Copyright (c) 1980, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 /*
40  * Argument scanning and profile reading code.  Default parameters are set
41  * here as well.
42  */
43 
44 #include <ctype.h>
45 #include <err.h>
46 #include <limits.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include "indent_globs.h"
51 #include "indent.h"
52 
53 #define INDENT_VERSION	"2.0"
54 
55 /* profile types */
56 #define	PRO_SPECIAL	1	/* special case */
57 #define	PRO_BOOL	2	/* boolean */
58 #define	PRO_INT		3	/* integer */
59 
60 /* profile specials for booleans */
61 #define	ON		1	/* turn it on */
62 #define	OFF		0	/* turn it off */
63 
64 /* profile specials for specials */
65 #define	IGN		1	/* ignore it */
66 #define	CLI		2	/* case label indent (float) */
67 #define	STDIN		3	/* use stdin */
68 #define	KEY		4	/* type (keyword) */
69 
70 static void scan_profile(FILE *);
71 
72 #define	KEY_FILE		5	/* only used for args */
73 #define VERSION			6	/* only used for args */
74 
75 const char *option_source = "?";
76 
77 void add_typedefs_from_file(const char *str);
78 
79 /*
80  * N.B.: because of the way the table here is scanned, options whose names are
81  * substrings of other options must occur later; that is, with -lp vs -l, -lp
82  * must be first.  Also, while (most) booleans occur more than once, the last
83  * default value is the one actually assigned.
84  */
85 struct pro {
86     const char *p_name;		/* name, e.g. -bl, -cli */
87     int         p_type;		/* type (int, bool, special) */
88     int         p_default;	/* the default value (if int) */
89     int         p_special;	/* depends on type */
90     int        *p_obj;		/* the associated variable */
91 }           pro[] = {
92 
93     {"T", PRO_SPECIAL, 0, KEY, 0},
94     {"U", PRO_SPECIAL, 0, KEY_FILE, 0},
95     {"-version", PRO_SPECIAL, 0, VERSION, 0},
96     {"P", PRO_SPECIAL, 0, IGN, 0},
97     {"bacc", PRO_BOOL, false, ON, &opt.blanklines_around_conditional_compilation},
98     {"badp", PRO_BOOL, false, ON, &opt.blanklines_after_declarations_at_proctop},
99     {"bad", PRO_BOOL, false, ON, &opt.blanklines_after_declarations},
100     {"bap", PRO_BOOL, false, ON, &opt.blanklines_after_procs},
101     {"bbb", PRO_BOOL, false, ON, &opt.blanklines_before_blockcomments},
102     {"bc", PRO_BOOL, true, OFF, &opt.leave_comma},
103     {"bl", PRO_BOOL, true, OFF, &opt.btype_2},
104     {"br", PRO_BOOL, true, ON, &opt.btype_2},
105     {"bs", PRO_BOOL, false, ON, &opt.Bill_Shannon},
106     {"cdb", PRO_BOOL, true, ON, &opt.comment_delimiter_on_blankline},
107     {"cd", PRO_INT, 0, 0, &opt.decl_com_ind},
108     {"ce", PRO_BOOL, true, ON, &opt.cuddle_else},
109     {"ci", PRO_INT, 0, 0, &opt.continuation_indent},
110     {"cli", PRO_SPECIAL, 0, CLI, 0},
111     {"cs", PRO_BOOL, false, ON, &opt.space_after_cast},
112     {"c", PRO_INT, 33, 0, &opt.com_ind},
113     {"di", PRO_INT, 16, 0, &opt.decl_indent},
114     {"dj", PRO_BOOL, false, ON, &opt.ljust_decl},
115     {"d", PRO_INT, 0, 0, &opt.unindent_displace},
116     {"eei", PRO_BOOL, false, ON, &opt.extra_expression_indent},
117     {"ei", PRO_BOOL, true, ON, &opt.else_if},
118     {"fbs", PRO_BOOL, true, ON, &opt.function_brace_split},
119     {"fc1", PRO_BOOL, true, ON, &opt.format_col1_comments},
120     {"fcb", PRO_BOOL, true, ON, &opt.format_block_comments},
121     {"ip", PRO_BOOL, true, ON, &opt.indent_parameters},
122     {"i", PRO_INT, 8, 0, &opt.ind_size},
123     {"lc", PRO_INT, 0, 0, &opt.block_comment_max_col},
124     {"ldi", PRO_INT, -1, 0, &opt.local_decl_indent},
125     {"lpl", PRO_BOOL, false, ON, &opt.lineup_to_parens_always},
126     {"lp", PRO_BOOL, true, ON, &opt.lineup_to_parens},
127     {"l", PRO_INT, 78, 0, &opt.max_col},
128     {"nbacc", PRO_BOOL, false, OFF, &opt.blanklines_around_conditional_compilation},
129     {"nbadp", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations_at_proctop},
130     {"nbad", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations},
131     {"nbap", PRO_BOOL, false, OFF, &opt.blanklines_after_procs},
132     {"nbbb", PRO_BOOL, false, OFF, &opt.blanklines_before_blockcomments},
133     {"nbc", PRO_BOOL, true, ON, &opt.leave_comma},
134     {"nbs", PRO_BOOL, false, OFF, &opt.Bill_Shannon},
135     {"ncdb", PRO_BOOL, true, OFF, &opt.comment_delimiter_on_blankline},
136     {"nce", PRO_BOOL, true, OFF, &opt.cuddle_else},
137     {"ncs", PRO_BOOL, false, OFF, &opt.space_after_cast},
138     {"ndj", PRO_BOOL, false, OFF, &opt.ljust_decl},
139     {"neei", PRO_BOOL, false, OFF, &opt.extra_expression_indent},
140     {"nei", PRO_BOOL, true, OFF, &opt.else_if},
141     {"nfbs", PRO_BOOL, true, OFF, &opt.function_brace_split},
142     {"nfc1", PRO_BOOL, true, OFF, &opt.format_col1_comments},
143     {"nfcb", PRO_BOOL, true, OFF, &opt.format_block_comments},
144     {"nip", PRO_BOOL, true, OFF, &opt.indent_parameters},
145     {"nlpl", PRO_BOOL, false, OFF, &opt.lineup_to_parens_always},
146     {"nlp", PRO_BOOL, true, OFF, &opt.lineup_to_parens},
147     {"npcs", PRO_BOOL, false, OFF, &opt.proc_calls_space},
148     {"npro", PRO_SPECIAL, 0, IGN, 0},
149     {"npsl", PRO_BOOL, true, OFF, &opt.procnames_start_line},
150     {"nps", PRO_BOOL, false, OFF, &opt.pointer_as_binop},
151     {"nsc", PRO_BOOL, true, OFF, &opt.star_comment_cont},
152     {"nsob", PRO_BOOL, false, OFF, &opt.swallow_optional_blanklines},
153     {"nut", PRO_BOOL, true, OFF, &opt.use_tabs},
154     {"nv", PRO_BOOL, false, OFF, &opt.verbose},
155     {"pcs", PRO_BOOL, false, ON, &opt.proc_calls_space},
156     {"psl", PRO_BOOL, true, ON, &opt.procnames_start_line},
157     {"ps", PRO_BOOL, false, ON, &opt.pointer_as_binop},
158     {"sc", PRO_BOOL, true, ON, &opt.star_comment_cont},
159     {"sob", PRO_BOOL, false, ON, &opt.swallow_optional_blanklines},
160     {"st", PRO_SPECIAL, 0, STDIN, 0},
161     {"ta", PRO_BOOL, false, ON, &opt.auto_typedefs},
162     {"ts", PRO_INT, 8, 0, &opt.tabsize},
163     {"ut", PRO_BOOL, true, ON, &opt.use_tabs},
164     {"v", PRO_BOOL, false, ON, &opt.verbose},
165     /* whew! */
166     {0, 0, 0, 0, 0}
167 };
168 
169 /*
170  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
171  * given in these files.
172  */
173 void
174 set_profile(const char *profile_name)
175 {
176     FILE *f;
177     char fname[PATH_MAX];
178     static char prof[] = ".indent.pro";
179 
180     if (profile_name == NULL)
181 	snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
182     else
183 	snprintf(fname, sizeof(fname), "%s", profile_name + 2);
184     if ((f = fopen(option_source = fname, "r")) != NULL) {
185 	scan_profile(f);
186 	(void) fclose(f);
187     }
188     if ((f = fopen(option_source = prof, "r")) != NULL) {
189 	scan_profile(f);
190 	(void) fclose(f);
191     }
192     option_source = "Command line";
193 }
194 
195 static void
196 scan_profile(FILE *f)
197 {
198     int		comment, i;
199     char	*p;
200     char        buf[BUFSIZ];
201 
202     while (1) {
203 	p = buf;
204 	comment = 0;
205 	while ((i = getc(f)) != EOF) {
206 	    if (i == '*' && !comment && p > buf && p[-1] == '/') {
207 		comment = p - buf;
208 		*p++ = i;
209 	    } else if (i == '/' && comment && p > buf && p[-1] == '*') {
210 		p = buf + comment - 1;
211 		comment = 0;
212 	    } else if (isspace((unsigned char)i)) {
213 		if (p > buf && !comment)
214 		    break;
215 	    } else {
216 		*p++ = i;
217 	    }
218 	}
219 	if (p != buf) {
220 	    *p++ = 0;
221 	    if (opt.verbose)
222 		printf("profile: %s\n", buf);
223 	    set_option(buf);
224 	}
225 	else if (i == EOF)
226 	    return;
227     }
228 }
229 
230 static const char *
231 eqin(const char *s1, const char *s2)
232 {
233     while (*s1) {
234 	if (*s1++ != *s2++)
235 	    return (NULL);
236     }
237     return (s2);
238 }
239 
240 /*
241  * Set the defaults.
242  */
243 void
244 set_defaults(void)
245 {
246     struct pro *p;
247 
248     /*
249      * Because ps.case_indent is a float, we can't initialize it from the
250      * table:
251      */
252     opt.case_indent = 0.0;	/* -cli0.0 */
253     for (p = pro; p->p_name; p++)
254 	if (p->p_type != PRO_SPECIAL)
255 	    *p->p_obj = p->p_default;
256 }
257 
258 void
259 set_option(char *arg)
260 {
261     struct	pro *p;
262     const char	*param_start;
263 
264     arg++;			/* ignore leading "-" */
265     for (p = pro; p->p_name; p++)
266 	if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL)
267 	    goto found;
268     errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
269 found:
270     switch (p->p_type) {
271 
272     case PRO_SPECIAL:
273 	switch (p->p_special) {
274 
275 	case IGN:
276 	    break;
277 
278 	case CLI:
279 	    if (*param_start == 0)
280 		goto need_param;
281 	    opt.case_indent = atof(param_start);
282 	    break;
283 
284 	case STDIN:
285 	    if (input == NULL)
286 		input = stdin;
287 	    if (output == NULL)
288 		output = stdout;
289 	    break;
290 
291 	case KEY:
292 	    if (*param_start == 0)
293 		goto need_param;
294 	    add_typename(param_start);
295 	    break;
296 
297 	case KEY_FILE:
298 	    if (*param_start == 0)
299 		goto need_param;
300 	    add_typedefs_from_file(param_start);
301 	    break;
302 
303 	case VERSION:
304 	    printf("FreeBSD indent %s\n", INDENT_VERSION);
305 	    exit(0);
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 	if (p->p_special == OFF)
314 	    *p->p_obj = false;
315 	else
316 	    *p->p_obj = true;
317 	break;
318 
319     case PRO_INT:
320 	if (!isdigit((unsigned char)*param_start)) {
321     need_param:
322 	    errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
323 	}
324 	*p->p_obj = atoi(param_start);
325 	break;
326 
327     default:
328 	errx(1, "set_option: internal error: p_type %d", p->p_type);
329     }
330 }
331 
332 void
333 add_typedefs_from_file(const char *str)
334 {
335     FILE *file;
336     char line[BUFSIZ];
337 
338     if ((file = fopen(str, "r")) == NULL) {
339 	fprintf(stderr, "indent: cannot open file %s\n", str);
340 	exit(1);
341     }
342     while ((fgets(line, BUFSIZ, file)) != NULL) {
343 	/* Remove trailing whitespace */
344 	line[strcspn(line, " \t\n\r")] = '\0';
345 	add_typename(line);
346     }
347     fclose(file);
348 }
349