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