xref: /csrg-svn/usr.bin/indent/args.c (revision 24677)
124647Smckusick /*
224647Smckusick  * Copyright (c) 1980 Regents of the University of California.
324647Smckusick  * All rights reserved.  The Berkeley software License Agreement
424647Smckusick  * specifies the terms and conditions for redistribution.
524647Smckusick  */
624647Smckusick 
724647Smckusick #ifndef lint
8*24677Smckusick static char sccsid[] = "@(#)args.c	5.2 (Berkeley) 09/10/85";
924647Smckusick #endif not lint
1024647Smckusick 
1124647Smckusick /*
1224647Smckusick  * Argument scanning and profile reading code.  Default parameters
1324647Smckusick  * are set here as well.
1424647Smckusick  */
1524647Smckusick 
1624647Smckusick #include "indent_globs.h"
1724647Smckusick #include <sys/types.h>
1824647Smckusick #include <ctype.h>
1924647Smckusick 
2024647Smckusick char *getenv(), *index();
2124647Smckusick 
2224647Smckusick /* profile types */
2324647Smckusick #define	PRO_SPECIAL	1	/* special case */
2424647Smckusick #define	PRO_BOOL	2	/* boolean */
2524647Smckusick #define	PRO_INT		3	/* integer */
2624647Smckusick 
2724647Smckusick /* profile specials for booleans */
2824647Smckusick #define	ON		1	/* turn it on */
2924647Smckusick #define	OFF		0	/* turn it off */
3024647Smckusick 
3124647Smckusick /* profile specials for specials */
3224647Smckusick #define	IGN		1	/* ignore it */
3324647Smckusick #define	CLI		2	/* case label indent (float) */
3424647Smckusick #define	STDIN		3	/* use stdin */
3524647Smckusick #define	KEY		4	/* type (keyword) */
3624647Smckusick 
3724647Smckusick /*
3824647Smckusick  * N.B.: because of the way the table here is scanned, options
3924647Smckusick  * whose names are substrings of other options must occur later;
4024647Smckusick  * that is, with -lp vs -l, -lp must be first.  Also, while (most)
4124647Smckusick  * booleans occur more than once, the last default value is the
4224647Smckusick  * one actually assigned.
4324647Smckusick  */
4424647Smckusick struct pro {
4524647Smckusick     char *p_name;		/* name, eg -bl, -cli */
4624647Smckusick     int   p_type;		/* type (int, bool, special) */
4724647Smckusick     int   p_default;		/* the default value (if int) */
4824647Smckusick     int   p_special;		/* depends on type */
4924647Smckusick     int  *p_obj;		/* the associated variable */
5024647Smckusick } pro[] = {
5124647Smckusick     "npro",	PRO_SPECIAL,	0,	IGN,	0,
5224647Smckusick     "lc",	PRO_INT,	0,	0,	&block_comment_max_col,
5324647Smckusick     "lp",	PRO_BOOL,	true,	ON,	&lineup_to_parens,
5424647Smckusick     "nlp",	PRO_BOOL,	true,	OFF,	&lineup_to_parens,
5524647Smckusick     "l",	PRO_INT,	78,	0,	&max_col,
5624647Smckusick     "psl",	PRO_BOOL,	true,	ON,	&procnames_start_line,
5724647Smckusick     "npsl",	PRO_BOOL,	true,	OFF,	&procnames_start_line,
5824647Smckusick     "fc1",	PRO_BOOL,	true,	ON,	&format_col1_comments,
5924647Smckusick     "nfc1",	PRO_BOOL,	true,	OFF,	&format_col1_comments,
6024647Smckusick     "pcs",	PRO_BOOL,	false,	ON,	&proc_calls_space,
6124647Smckusick     "npcs",	PRO_BOOL,	false,	OFF,	&proc_calls_space,
6224647Smckusick     "ip",	PRO_BOOL,	true,	ON,	&ps.indent_parameters,
6324647Smckusick     "nip",	PRO_BOOL,	true,	OFF,	&ps.indent_parameters,
6424647Smckusick  /* see set_defaults for initialization of -cli */
6524647Smckusick     "cli",	PRO_SPECIAL,	0,	CLI,	0,
6624647Smckusick     "ci",	PRO_INT,	0,	0,	&continuation_indent,
6724647Smckusick     "cdb",	PRO_BOOL,	true,	ON,  &comment_delimiter_on_blankline,
6824647Smckusick     "ncdb",	PRO_BOOL,	true,	OFF, &comment_delimiter_on_blankline,
6924647Smckusick     "i",	PRO_INT,	8,	0,	&ps.ind_size,
7024647Smckusick     "cd",	PRO_INT,	0,	0,	&ps.decl_com_ind,
7124647Smckusick     "ce",	PRO_BOOL,	true,	ON,	&cuddle_else,
7224647Smckusick     "nce",	PRO_BOOL,	true,	OFF,	&cuddle_else,
7324647Smckusick     "c",	PRO_INT,	33,	0,	&ps.com_ind,
7424647Smckusick     "v",	PRO_BOOL,	false,	ON,	&verbose,
7524647Smckusick     "nv",	PRO_BOOL,	false,	OFF,	&verbose,
7624647Smckusick     "dj",	PRO_BOOL,	false,	ON,	&ps.ljust_decl,
7724647Smckusick     "ndj",	PRO_BOOL,	false,	OFF,	&ps.ljust_decl,
7824647Smckusick  /* don't ask *me* why -bc/-nbc is backwards.... */
7924647Smckusick     "bc",	PRO_BOOL,	true,	OFF,	&ps.leave_comma,
8024647Smckusick     "nbc",	PRO_BOOL,	true,	ON,	&ps.leave_comma,
8124647Smckusick     "di",	PRO_INT,	16,	0,	&ps.decl_indent,
8224647Smckusick     "d",	PRO_INT,	0,	0,	&ps.unindent_displace,
8324647Smckusick     "br",	PRO_BOOL,	true,	ON,	&btype_2,
8424647Smckusick     "bl",	PRO_BOOL,	true,	OFF,	&btype_2,
8524647Smckusick     "st",	PRO_SPECIAL,	0,	STDIN,	0,
8624647Smckusick     "ei",	PRO_BOOL,	true,	ON,	&ps.else_if,
8724647Smckusick     "nei",	PRO_BOOL,	true,	OFF,	&ps.else_if,
8824647Smckusick     "sc",	PRO_BOOL,	true,	ON,	&star_comment_cont,
8924647Smckusick     "nsc",	PRO_BOOL,	true,	OFF,	&star_comment_cont,
9024647Smckusick     "bap",	PRO_BOOL,	false,	ON,	&blanklines_after_procs,
9124647Smckusick     "nbap",	PRO_BOOL,	false,	OFF,	&blanklines_after_procs,
9224647Smckusick     "sob",	PRO_BOOL,	false,	ON,	&swallow_optional_blanklines,
9324647Smckusick     "nsob",	PRO_BOOL,	false,	OFF,	&swallow_optional_blanklines,
9424647Smckusick     "bad",	PRO_BOOL,	false,	ON,  &blanklines_after_declarations,
9524647Smckusick     "nbad",	PRO_BOOL,	false,	OFF, &blanklines_after_declarations,
9624647Smckusick     "bbb",	PRO_BOOL,	false,	ON,  &blanklines_before_blockcomments,
9724647Smckusick     "nbbb",	PRO_BOOL,	false,	OFF, &blanklines_before_blockcomments,
98*24677Smckusick     "ps",	PRO_BOOL,	false,	ON,	&pointer_as_binop,
99*24677Smckusick     "nps",	PRO_BOOL,	false,	OFF,	&pointer_as_binop,
10024647Smckusick     "troff",	PRO_BOOL,	false,	ON,	&troff,
10124647Smckusick     "T",	PRO_SPECIAL,	0,	KEY,	0,
10224647Smckusick  /* whew! */
10324647Smckusick     0,		0,		0,	0,	0
10424647Smckusick };
10524647Smckusick 
10624647Smckusick /*
10724647Smckusick  * set_profile reads $HOME/.indent.pro and ./.indent.pro and
10824647Smckusick  * handles arguments given in these files.
10924647Smckusick  */
11024647Smckusick set_profile()
11124647Smckusick {
11224647Smckusick     register FILE *f;
11324647Smckusick     char fname[BUFSIZ];
11424647Smckusick     static char pro[] = ".indent.pro";
11524647Smckusick 
11624647Smckusick     sprintf(fname, "%s/%s", getenv("HOME"), pro);
11724647Smckusick     if ((f = fopen(fname, "r")) != NULL) {
11824647Smckusick 	scan_profile(f);
11924647Smckusick 	(void) fclose(f);
12024647Smckusick     }
12124647Smckusick     if ((f = fopen(pro, "r")) != NULL) {
12224647Smckusick 	scan_profile(f);
12324647Smckusick 	(void) fclose(f);
12424647Smckusick     }
12524647Smckusick }
12624647Smckusick 
12724647Smckusick scan_profile(f)
12824647Smckusick     register FILE *f;
12924647Smckusick {
13024647Smckusick     register char *p, *arg;
13124647Smckusick     char buf[BUFSIZ];
13224647Smckusick 
13324647Smckusick     while (fgets(buf, sizeof buf, f)) {
13424647Smckusick 	if ((p = index(buf, '\n')) != NULL)
13524647Smckusick 	    *p = 0;
13624647Smckusick 	if (verbose)
13724647Smckusick 	    printf("profile: %s\n", buf);
13824647Smckusick 	p = buf;
13924647Smckusick 	for (;;) {
14024647Smckusick 	    while (isspace(*p))
14124647Smckusick 		p++;
14224647Smckusick 	    if (*p == 0)
14324647Smckusick 		break;
14424647Smckusick 	    arg = p;
14524647Smckusick 	    while (*p) {
14624647Smckusick 		if (isspace(*p)) {
14724647Smckusick 		    *p++ = 0;
14824647Smckusick 		    break;
14924647Smckusick 		}
15024647Smckusick 		p++;
15124647Smckusick 	    }
15224647Smckusick 	    set_option(arg);
15324647Smckusick 	}
15424647Smckusick     }
15524647Smckusick }
15624647Smckusick 
15724647Smckusick char       *param_start;
15824647Smckusick 
15924647Smckusick eqin(s1, s2)
16024647Smckusick     register char *s1;
16124647Smckusick     register char *s2;
16224647Smckusick {
16324647Smckusick     while (*s1) {
16424647Smckusick 	if (*s1++ != *s2++)
16524647Smckusick 	    return (false);
16624647Smckusick     }
16724647Smckusick     param_start = s2;
16824647Smckusick     return (true);
16924647Smckusick }
17024647Smckusick 
17124647Smckusick /*
17224647Smckusick  * Set the defaults.
17324647Smckusick  */
17424647Smckusick set_defaults()
17524647Smckusick {
17624647Smckusick     register struct pro *p;
17724647Smckusick 
17824647Smckusick     /*
17924647Smckusick      * Because ps.case_indent is a float, we can't initialize it
18024647Smckusick      * from the table:
18124647Smckusick      */
18224647Smckusick     ps.case_indent = 0.0;	/* -cli0.0 */
18324647Smckusick     for (p = pro; p->p_name; p++)
18424647Smckusick 	if (p->p_type != PRO_SPECIAL)
18524647Smckusick 	    *p->p_obj = p->p_default;
18624647Smckusick }
18724647Smckusick 
18824647Smckusick set_option(arg)
18924647Smckusick     register char *arg;
19024647Smckusick {
19124647Smckusick     register struct pro *p;
19224647Smckusick     extern double atof();
19324647Smckusick 
19424647Smckusick     arg++;			/* ignore leading "-" */
19524647Smckusick     for (p = pro; p->p_name; p++)
19624647Smckusick 	if (*p->p_name == *arg && eqin(p->p_name, arg))
19724647Smckusick 	    goto found;
19824647Smckusick     fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1);
19924647Smckusick     exit(1);
20024647Smckusick found:
20124647Smckusick     switch (p->p_type) {
20224647Smckusick 
20324647Smckusick 	case PRO_SPECIAL:
20424647Smckusick 	    switch (p->p_special) {
20524647Smckusick 
20624647Smckusick 		case IGN:
20724647Smckusick 		    break;
20824647Smckusick 
20924647Smckusick 		case CLI:
21024647Smckusick 		    if (*param_start == 0)
21124647Smckusick 			goto need_param;
21224647Smckusick 		    ps.case_indent = atof(param_start);
21324647Smckusick 		    break;
21424647Smckusick 
21524647Smckusick 		case STDIN:
21624647Smckusick 		    if (input == 0)
21724647Smckusick 			input = stdin;
21824647Smckusick 		    if (output == 0)
21924647Smckusick 			output = stdout;
22024647Smckusick 		    break;
22124647Smckusick 
22224647Smckusick 		case KEY:
22324647Smckusick 		    if (*param_start == 0)
22424647Smckusick 			goto need_param;
22524647Smckusick 		    addkey(param_start, 4);
22624647Smckusick 		    break;
22724647Smckusick 
22824647Smckusick 		default:
22924647Smckusick 		    fprintf(stderr, "\
23024647Smckusick indent: set_option: internal error: p_special %d\n", p->p_special);
23124647Smckusick 		    exit(1);
23224647Smckusick 	    }
23324647Smckusick 	    break;
23424647Smckusick 
23524647Smckusick 	case PRO_BOOL:
23624647Smckusick 	    if (p->p_special == OFF)
23724647Smckusick 		*p->p_obj = false;
23824647Smckusick 	    else
23924647Smckusick 		*p->p_obj = true;
24024647Smckusick 	    break;
24124647Smckusick 
24224647Smckusick 	case PRO_INT:
24324647Smckusick 	    if (*param_start == 0) {
24424647Smckusick need_param:
24524647Smckusick 		fprintf(stderr, "indent: ``%s'' requires a parameter\n",
24624647Smckusick 			arg - 1);
24724647Smckusick 		exit(1);
24824647Smckusick 	    }
24924647Smckusick 	    *p->p_obj = atoi(param_start);
25024647Smckusick 	    break;
25124647Smckusick 
25224647Smckusick 	default:
25324647Smckusick 	    fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
25424647Smckusick 		    p->p_type);
25524647Smckusick 	    exit(1);
25624647Smckusick     }
25724647Smckusick }
258