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