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