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