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