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