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