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