xref: /csrg-svn/usr.bin/indent/args.c (revision 35508)
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.6 (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 prof[] = ".indent.pro";
142 
143     sprintf(fname, "%s/%s", getenv("HOME"), prof);
144     if ((f = fopen(fname, "r")) != NULL) {
145 	scan_profile(f);
146 	(void) fclose(f);
147     }
148     if ((f = fopen(prof, "r")) != NULL) {
149 	scan_profile(f);
150 	(void) fclose(f);
151     }
152 }
153 
154 scan_profile(f)
155     register FILE *f;
156 {
157     register int i;
158     register char *p;
159     char        buf[BUFSIZ];
160 
161     while (1) {
162 	for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
163 	if (p != buf) {
164 	    *p++ = 0;
165 	    if (verbose)
166 		printf("profile: %s\n", buf);
167 	    set_option(buf);
168 	}
169 	else if (i == EOF)
170 	    return;
171     }
172 }
173 
174 char       *param_start;
175 
176 eqin(s1, s2)
177     register char *s1;
178     register char *s2;
179 {
180     while (*s1) {
181 	if (*s1++ != *s2++)
182 	    return (false);
183     }
184     param_start = s2;
185     return (true);
186 }
187 
188 /*
189  * Set the defaults.
190  */
191 set_defaults()
192 {
193     register struct pro *p;
194 
195     /*
196      * Because ps.case_indent is a float, we can't initialize it from the
197      * table:
198      */
199     ps.case_indent = 0.0;	/* -cli0.0 */
200     for (p = pro; p->p_name; p++)
201 	if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
202 	    *p->p_obj = p->p_default;
203 }
204 
205 set_option(arg)
206     register char *arg;
207 {
208     register struct pro *p;
209     extern double atof();
210 
211     arg++;			/* ignore leading "-" */
212     for (p = pro; p->p_name; p++)
213 	if (*p->p_name == *arg && eqin(p->p_name, arg))
214 	    goto found;
215     fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1);
216     exit(1);
217 found:
218     switch (p->p_type) {
219 
220     case PRO_SPECIAL:
221 	switch (p->p_special) {
222 
223 	case IGN:
224 	    break;
225 
226 	case CLI:
227 	    if (*param_start == 0)
228 		goto need_param;
229 	    ps.case_indent = atof(param_start);
230 	    break;
231 
232 	case STDIN:
233 	    if (input == 0)
234 		input = stdin;
235 	    if (output == 0)
236 		output = stdout;
237 	    break;
238 
239 	case KEY:
240 	    if (*param_start == 0)
241 		goto need_param;
242 	    {
243 		register char *str = (char *) malloc(strlen(param_start) + 1);
244 		strcpy(str, param_start);
245 		addkey(str, 4);
246 	    }
247 	    break;
248 
249 	default:
250 	    fprintf(stderr, "\
251 indent: set_option: internal error: p_special %d\n", p->p_special);
252 	    exit(1);
253 	}
254 	break;
255 
256     case PRO_BOOL:
257 	if (p->p_special == OFF)
258 	    *p->p_obj = false;
259 	else
260 	    *p->p_obj = true;
261 	break;
262 
263     case PRO_INT:
264 	if (*param_start == 0) {
265     need_param:
266 	    fprintf(stderr, "indent: ``%s'' requires a parameter\n",
267 		    arg - 1);
268 	    exit(1);
269 	}
270 	*p->p_obj = atoi(param_start);
271 	break;
272 
273     case PRO_FONT:
274 	parsefont((struct fstate *) p->p_obj, param_start);
275 	break;
276 
277     default:
278 	fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
279 		p->p_type);
280 	exit(1);
281     }
282 }
283