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