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