xref: /freebsd/usr.bin/indent/args.c (revision 0f8f86b71f022b803e99151c19db81b280f245dc)
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 <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "indent_globs.h"
56 #include "indent.h"
57 
58 /* profile types */
59 #define	PRO_SPECIAL	1	/* special case */
60 #define	PRO_BOOL	2	/* boolean */
61 #define	PRO_INT		3	/* integer */
62 #define PRO_FONT	4	/* troff font */
63 
64 /* profile specials for booleans */
65 #define	ON		1	/* turn it on */
66 #define	OFF		0	/* turn it off */
67 
68 /* profile specials for specials */
69 #define	IGN		1	/* ignore it */
70 #define	CLI		2	/* case label indent (float) */
71 #define	STDIN		3	/* use stdin */
72 #define	KEY		4	/* type (keyword) */
73 
74 static void scan_profile(FILE *);
75 
76 const char *option_source = "?";
77 
78 /*
79  * N.B.: because of the way the table here is scanned, options whose names are
80  * substrings of other options must occur later; that is, with -lp vs -l, -lp
81  * must be first.  Also, while (most) booleans occur more than once, the last
82  * default value is the one actually assigned.
83  */
84 struct pro {
85     const char *p_name;		/* name, e.g. -bl, -cli */
86     int         p_type;		/* type (int, bool, special) */
87     int         p_default;	/* the default value (if int) */
88     int         p_special;	/* depends on type */
89     int        *p_obj;		/* the associated variable */
90 }           pro[] = {
91 
92     {"T", PRO_SPECIAL, 0, KEY, 0},
93     {"bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation},
94     {"badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop},
95     {"bad", PRO_BOOL, false, ON, &blanklines_after_declarations},
96     {"bap", PRO_BOOL, false, ON, &blanklines_after_procs},
97     {"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments},
98     {"bc", PRO_BOOL, true, OFF, &ps.leave_comma},
99     {"bl", PRO_BOOL, true, OFF, &btype_2},
100     {"br", PRO_BOOL, true, ON, &btype_2},
101     {"bs", PRO_BOOL, false, ON, &Bill_Shannon},
102     {"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline},
103     {"cd", PRO_INT, 0, 0, &ps.decl_com_ind},
104     {"ce", PRO_BOOL, true, ON, &cuddle_else},
105     {"ci", PRO_INT, 0, 0, &continuation_indent},
106     {"cli", PRO_SPECIAL, 0, CLI, 0},
107     {"c", PRO_INT, 33, 0, &ps.com_ind},
108     {"di", PRO_INT, 16, 0, &ps.decl_indent},
109     {"dj", PRO_BOOL, false, ON, &ps.ljust_decl},
110     {"d", PRO_INT, 0, 0, &ps.unindent_displace},
111     {"eei", PRO_BOOL, false, ON, &extra_expression_indent},
112     {"ei", PRO_BOOL, true, ON, &ps.else_if},
113     {"fbc", PRO_FONT, 0, 0, (int *) &blkcomf},
114     {"fbx", PRO_FONT, 0, 0, (int *) &boxcomf},
115     {"fb", PRO_FONT, 0, 0, (int *) &bodyf},
116     {"fc1", PRO_BOOL, true, ON, &format_col1_comments},
117     {"fcb", PRO_BOOL, true, ON, &format_block_comments},
118     {"fc", PRO_FONT, 0, 0, (int *) &scomf},
119     {"fk", PRO_FONT, 0, 0, (int *) &keywordf},
120     {"fs", PRO_FONT, 0, 0, (int *) &stringf},
121     {"ip", PRO_BOOL, true, ON, &ps.indent_parameters},
122     {"i", PRO_INT, 8, 0, &ps.ind_size},
123     {"lc", PRO_INT, 0, 0, &block_comment_max_col},
124     {"ldi", PRO_INT, -1, 0, &ps.local_decl_indent},
125     {"lp", PRO_BOOL, true, ON, &lineup_to_parens},
126     {"l", PRO_INT, 78, 0, &max_col},
127     {"nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation},
128     {"nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop},
129     {"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations},
130     {"nbap", PRO_BOOL, false, OFF, &blanklines_after_procs},
131     {"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments},
132     {"nbc", PRO_BOOL, true, ON, &ps.leave_comma},
133     {"nbs", PRO_BOOL, false, OFF, &Bill_Shannon},
134     {"ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline},
135     {"nce", PRO_BOOL, true, OFF, &cuddle_else},
136     {"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl},
137     {"neei", PRO_BOOL, false, OFF, &extra_expression_indent},
138     {"nei", PRO_BOOL, true, OFF, &ps.else_if},
139     {"nfc1", PRO_BOOL, true, OFF, &format_col1_comments},
140     {"nfcb", PRO_BOOL, true, OFF, &format_block_comments},
141     {"nip", PRO_BOOL, true, OFF, &ps.indent_parameters},
142     {"nlp", PRO_BOOL, true, OFF, &lineup_to_parens},
143     {"npcs", PRO_BOOL, false, OFF, &proc_calls_space},
144     {"npro", PRO_SPECIAL, 0, IGN, 0},
145     {"npsl", PRO_BOOL, true, OFF, &procnames_start_line},
146     {"nps", PRO_BOOL, false, OFF, &pointer_as_binop},
147     {"nsc", PRO_BOOL, true, OFF, &star_comment_cont},
148     {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines},
149     {"nv", PRO_BOOL, false, OFF, &verbose},
150     {"pcs", PRO_BOOL, false, ON, &proc_calls_space},
151     {"psl", PRO_BOOL, true, ON, &procnames_start_line},
152     {"ps", PRO_BOOL, false, ON, &pointer_as_binop},
153     {"sc", PRO_BOOL, true, ON, &star_comment_cont},
154     {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines},
155     {"st", PRO_SPECIAL, 0, STDIN, 0},
156     {"troff", PRO_BOOL, false, ON, &troff},
157     {"v", PRO_BOOL, false, ON, &verbose},
158     /* whew! */
159     {0, 0, 0, 0, 0}
160 };
161 
162 /*
163  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
164  * given in these files.
165  */
166 void
167 set_profile(void)
168 {
169     FILE *f;
170     char        fname[BUFSIZ];
171     static char prof[] = ".indent.pro";
172 
173     sprintf(fname, "%s/%s", getenv("HOME"), prof);
174     if ((f = fopen(option_source = fname, "r")) != NULL) {
175 	scan_profile(f);
176 	(void) fclose(f);
177     }
178     if ((f = fopen(option_source = prof, "r")) != NULL) {
179 	scan_profile(f);
180 	(void) fclose(f);
181     }
182     option_source = "Command line";
183 }
184 
185 static void
186 scan_profile(FILE *f)
187 {
188     int i;
189     char *p;
190     char        buf[BUFSIZ];
191 
192     while (1) {
193 	for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
194 	if (p != buf) {
195 	    *p++ = 0;
196 	    if (verbose)
197 		printf("profile: %s\n", buf);
198 	    set_option(buf);
199 	}
200 	else if (i == EOF)
201 	    return;
202     }
203 }
204 
205 const char	*param_start;
206 
207 static int
208 eqin(const char *s1, const char *s2)
209 {
210     while (*s1) {
211 	if (*s1++ != *s2++)
212 	    return (false);
213     }
214     param_start = s2;
215     return (true);
216 }
217 
218 /*
219  * Set the defaults.
220  */
221 void
222 set_defaults(void)
223 {
224     struct pro *p;
225 
226     /*
227      * Because ps.case_indent is a float, we can't initialize it from the
228      * table:
229      */
230     ps.case_indent = 0.0;	/* -cli0.0 */
231     for (p = pro; p->p_name; p++)
232 	if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
233 	    *p->p_obj = p->p_default;
234 }
235 
236 void
237 set_option(char *arg)
238 {
239     struct pro *p;
240 
241     arg++;			/* ignore leading "-" */
242     for (p = pro; p->p_name; p++)
243 	if (*p->p_name == *arg && eqin(p->p_name, arg))
244 	    goto found;
245     errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
246 found:
247     switch (p->p_type) {
248 
249     case PRO_SPECIAL:
250 	switch (p->p_special) {
251 
252 	case IGN:
253 	    break;
254 
255 	case CLI:
256 	    if (*param_start == 0)
257 		goto need_param;
258 	    ps.case_indent = atof(param_start);
259 	    break;
260 
261 	case STDIN:
262 	    if (input == 0)
263 		input = stdin;
264 	    if (output == 0)
265 		output = stdout;
266 	    break;
267 
268 	case KEY:
269 	    if (*param_start == 0)
270 		goto need_param;
271 	    {
272 		char *str = (char *) malloc(strlen(param_start) + 1);
273 		if (str == NULL)
274 			err(1, NULL);
275 		strcpy(str, param_start);
276 		addkey(str, 4);
277 	    }
278 	    break;
279 
280 	default:
281 	    errx(1, "set_option: internal error: p_special %d", p->p_special);
282 	}
283 	break;
284 
285     case PRO_BOOL:
286 	if (p->p_special == OFF)
287 	    *p->p_obj = false;
288 	else
289 	    *p->p_obj = true;
290 	break;
291 
292     case PRO_INT:
293 	if (!isdigit(*param_start)) {
294     need_param:
295 	    errx(1, "%s: ``%s'' requires a parameter", option_source, arg - 1);
296 	}
297 	*p->p_obj = atoi(param_start);
298 	break;
299 
300     case PRO_FONT:
301 	parsefont((struct fstate *) p->p_obj, param_start);
302 	break;
303 
304     default:
305 	errx(1, "set_option: internal error: p_type %d", p->p_type);
306     }
307 }
308