xref: /original-bsd/usr.bin/indent/args.c (revision 241757c4)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of California at Berkeley and the University of
9  * Illinois at Urbana.  The name of either University may not be used
10  * to endorse or promote products derived from this software without
11  * specific prior written permission. This software is provided
12  * ``as is'' without express or implied warranty.
13  */
14 
15 #ifndef lint
16 static char sccsid[] = "@(#)args.c	5.3 (Berkeley) 03/22/88";
17 #endif /* not lint */
18 
19 /*
20  * Argument scanning and profile reading code.  Default parameters
21  * are set here as well.
22  */
23 
24 #include "indent_globs.h"
25 #include <sys/types.h>
26 #include <ctype.h>
27 
28 char *getenv(), *index();
29 
30 /* profile types */
31 #define	PRO_SPECIAL	1	/* special case */
32 #define	PRO_BOOL	2	/* boolean */
33 #define	PRO_INT		3	/* integer */
34 
35 /* profile specials for booleans */
36 #define	ON		1	/* turn it on */
37 #define	OFF		0	/* turn it off */
38 
39 /* profile specials for specials */
40 #define	IGN		1	/* ignore it */
41 #define	CLI		2	/* case label indent (float) */
42 #define	STDIN		3	/* use stdin */
43 #define	KEY		4	/* type (keyword) */
44 
45 /*
46  * N.B.: because of the way the table here is scanned, options
47  * whose names are substrings of other options must occur later;
48  * that is, with -lp vs -l, -lp must be first.  Also, while (most)
49  * booleans occur more than once, the last default value is the
50  * one actually assigned.
51  */
52 struct pro {
53     char *p_name;		/* name, eg -bl, -cli */
54     int   p_type;		/* type (int, bool, special) */
55     int   p_default;		/* the default value (if int) */
56     int   p_special;		/* depends on type */
57     int  *p_obj;		/* the associated variable */
58 } pro[] = {
59     "npro",	PRO_SPECIAL,	0,	IGN,	0,
60     "lc",	PRO_INT,	0,	0,	&block_comment_max_col,
61     "lp",	PRO_BOOL,	true,	ON,	&lineup_to_parens,
62     "nlp",	PRO_BOOL,	true,	OFF,	&lineup_to_parens,
63     "l",	PRO_INT,	78,	0,	&max_col,
64     "psl",	PRO_BOOL,	true,	ON,	&procnames_start_line,
65     "npsl",	PRO_BOOL,	true,	OFF,	&procnames_start_line,
66     "fc1",	PRO_BOOL,	true,	ON,	&format_col1_comments,
67     "nfc1",	PRO_BOOL,	true,	OFF,	&format_col1_comments,
68     "pcs",	PRO_BOOL,	false,	ON,	&proc_calls_space,
69     "npcs",	PRO_BOOL,	false,	OFF,	&proc_calls_space,
70     "ip",	PRO_BOOL,	true,	ON,	&ps.indent_parameters,
71     "nip",	PRO_BOOL,	true,	OFF,	&ps.indent_parameters,
72  /* see set_defaults for initialization of -cli */
73     "cli",	PRO_SPECIAL,	0,	CLI,	0,
74     "ci",	PRO_INT,	0,	0,	&continuation_indent,
75     "cdb",	PRO_BOOL,	true,	ON,  &comment_delimiter_on_blankline,
76     "ncdb",	PRO_BOOL,	true,	OFF, &comment_delimiter_on_blankline,
77     "i",	PRO_INT,	8,	0,	&ps.ind_size,
78     "cd",	PRO_INT,	0,	0,	&ps.decl_com_ind,
79     "ce",	PRO_BOOL,	true,	ON,	&cuddle_else,
80     "nce",	PRO_BOOL,	true,	OFF,	&cuddle_else,
81     "c",	PRO_INT,	33,	0,	&ps.com_ind,
82     "v",	PRO_BOOL,	false,	ON,	&verbose,
83     "nv",	PRO_BOOL,	false,	OFF,	&verbose,
84     "dj",	PRO_BOOL,	false,	ON,	&ps.ljust_decl,
85     "ndj",	PRO_BOOL,	false,	OFF,	&ps.ljust_decl,
86  /* don't ask *me* why -bc/-nbc is backwards.... */
87     "bc",	PRO_BOOL,	true,	OFF,	&ps.leave_comma,
88     "nbc",	PRO_BOOL,	true,	ON,	&ps.leave_comma,
89     "di",	PRO_INT,	16,	0,	&ps.decl_indent,
90     "d",	PRO_INT,	0,	0,	&ps.unindent_displace,
91     "br",	PRO_BOOL,	true,	ON,	&btype_2,
92     "bl",	PRO_BOOL,	true,	OFF,	&btype_2,
93     "st",	PRO_SPECIAL,	0,	STDIN,	0,
94     "ei",	PRO_BOOL,	true,	ON,	&ps.else_if,
95     "nei",	PRO_BOOL,	true,	OFF,	&ps.else_if,
96     "sc",	PRO_BOOL,	true,	ON,	&star_comment_cont,
97     "nsc",	PRO_BOOL,	true,	OFF,	&star_comment_cont,
98     "bap",	PRO_BOOL,	false,	ON,	&blanklines_after_procs,
99     "nbap",	PRO_BOOL,	false,	OFF,	&blanklines_after_procs,
100     "sob",	PRO_BOOL,	false,	ON,	&swallow_optional_blanklines,
101     "nsob",	PRO_BOOL,	false,	OFF,	&swallow_optional_blanklines,
102     "bad",	PRO_BOOL,	false,	ON,  &blanklines_after_declarations,
103     "nbad",	PRO_BOOL,	false,	OFF, &blanklines_after_declarations,
104     "bbb",	PRO_BOOL,	false,	ON,  &blanklines_before_blockcomments,
105     "nbbb",	PRO_BOOL,	false,	OFF, &blanklines_before_blockcomments,
106     "ps",	PRO_BOOL,	false,	ON,	&pointer_as_binop,
107     "nps",	PRO_BOOL,	false,	OFF,	&pointer_as_binop,
108     "troff",	PRO_BOOL,	false,	ON,	&troff,
109     "T",	PRO_SPECIAL,	0,	KEY,	0,
110  /* whew! */
111     0,		0,		0,	0,	0
112 };
113 
114 /*
115  * set_profile reads $HOME/.indent.pro and ./.indent.pro and
116  * handles arguments given in these files.
117  */
118 set_profile()
119 {
120     register FILE *f;
121     char fname[BUFSIZ];
122     static char pro[] = ".indent.pro";
123 
124     sprintf(fname, "%s/%s", getenv("HOME"), pro);
125     if ((f = fopen(fname, "r")) != NULL) {
126 	scan_profile(f);
127 	(void) fclose(f);
128     }
129     if ((f = fopen(pro, "r")) != NULL) {
130 	scan_profile(f);
131 	(void) fclose(f);
132     }
133 }
134 
135 scan_profile(f)
136     register FILE *f;
137 {
138     register char *p, *arg;
139     char buf[BUFSIZ];
140 
141     while (fgets(buf, sizeof buf, f)) {
142 	if ((p = index(buf, '\n')) != NULL)
143 	    *p = 0;
144 	if (verbose)
145 	    printf("profile: %s\n", buf);
146 	p = buf;
147 	for (;;) {
148 	    while (isspace(*p))
149 		p++;
150 	    if (*p == 0)
151 		break;
152 	    arg = p;
153 	    while (*p) {
154 		if (isspace(*p)) {
155 		    *p++ = 0;
156 		    break;
157 		}
158 		p++;
159 	    }
160 	    set_option(arg);
161 	}
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
188      * from the 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)
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: unknown parameter \"%s\"\n", 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 		    addkey(param_start, 4);
234 		    break;
235 
236 		default:
237 		    fprintf(stderr, "\
238 indent: set_option: internal error: p_special %d\n", p->p_special);
239 		    exit(1);
240 	    }
241 	    break;
242 
243 	case PRO_BOOL:
244 	    if (p->p_special == OFF)
245 		*p->p_obj = false;
246 	    else
247 		*p->p_obj = true;
248 	    break;
249 
250 	case PRO_INT:
251 	    if (*param_start == 0) {
252 need_param:
253 		fprintf(stderr, "indent: ``%s'' requires a parameter\n",
254 			arg - 1);
255 		exit(1);
256 	    }
257 	    *p->p_obj = atoi(param_start);
258 	    break;
259 
260 	default:
261 	    fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
262 		    p->p_type);
263 	    exit(1);
264     }
265 }
266