xref: /csrg-svn/usr.bin/indent/args.c (revision 42687)
124647Smckusick /*
235500Sbostic  * Copyright (c) 1985 Sun Microsystems, Inc.
335500Sbostic  * Copyright (c) 1980 The Regents of the University of California.
433767Sbostic  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
533767Sbostic  * All rights reserved.
633767Sbostic  *
7*42687Sbostic  * %sccs.include.redist.c%
824647Smckusick  */
924647Smckusick 
1024647Smckusick #ifndef lint
11*42687Sbostic static char sccsid[] = "@(#)args.c	5.9 (Berkeley) 06/01/90";
1233767Sbostic #endif /* not lint */
1324647Smckusick 
1424647Smckusick /*
1535500Sbostic  * Argument scanning and profile reading code.  Default parameters are set
1635500Sbostic  * here as well.
1724647Smckusick  */
1824647Smckusick 
1924647Smckusick #include "indent_globs.h"
2024647Smckusick #include <ctype.h>
2124647Smckusick 
2235500Sbostic char       *getenv(), *index();
2324647Smckusick 
2424647Smckusick /* profile types */
2524647Smckusick #define	PRO_SPECIAL	1	/* special case */
2624647Smckusick #define	PRO_BOOL	2	/* boolean */
2724647Smckusick #define	PRO_INT		3	/* integer */
2835500Sbostic #define PRO_FONT	4	/* troff font */
2924647Smckusick 
3024647Smckusick /* profile specials for booleans */
3124647Smckusick #define	ON		1	/* turn it on */
3224647Smckusick #define	OFF		0	/* turn it off */
3324647Smckusick 
3424647Smckusick /* profile specials for specials */
3524647Smckusick #define	IGN		1	/* ignore it */
3624647Smckusick #define	CLI		2	/* case label indent (float) */
3724647Smckusick #define	STDIN		3	/* use stdin */
3824647Smckusick #define	KEY		4	/* type (keyword) */
3924647Smckusick 
4038011Sbostic char *option_source = "?";
4138011Sbostic 
4224647Smckusick /*
4335500Sbostic  * N.B.: because of the way the table here is scanned, options whose names are
4435500Sbostic  * substrings of other options must occur later; that is, with -lp vs -l, -lp
4535500Sbostic  * must be first.  Also, while (most) booleans occur more than once, the last
4635500Sbostic  * default value is the one actually assigned.
4724647Smckusick  */
4824647Smckusick struct pro {
4935500Sbostic     char       *p_name;		/* name, eg -bl, -cli */
5035500Sbostic     int         p_type;		/* type (int, bool, special) */
5135500Sbostic     int         p_default;	/* the default value (if int) */
5235500Sbostic     int         p_special;	/* depends on type */
5335500Sbostic     int        *p_obj;		/* the associated variable */
5435500Sbostic }           pro[] = {
5535500Sbostic 
5635500Sbostic     "T", PRO_SPECIAL, 0, KEY, 0,
5735500Sbostic     "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation,
5835500Sbostic     "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop,
5935500Sbostic     "bad", PRO_BOOL, false, ON, &blanklines_after_declarations,
6035500Sbostic     "bap", PRO_BOOL, false, ON, &blanklines_after_procs,
6135500Sbostic     "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments,
6235500Sbostic     "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
6335500Sbostic     "bl", PRO_BOOL, true, OFF, &btype_2,
6435500Sbostic     "br", PRO_BOOL, true, ON, &btype_2,
6535500Sbostic     "bs", PRO_BOOL, false, ON, &Bill_Shannon,
6635500Sbostic     "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline,
6735500Sbostic     "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
6835500Sbostic     "ce", PRO_BOOL, true, ON, &cuddle_else,
6935500Sbostic     "ci", PRO_INT, 0, 0, &continuation_indent,
7035500Sbostic     "cli", PRO_SPECIAL, 0, CLI, 0,
7135500Sbostic     "c", PRO_INT, 33, 0, &ps.com_ind,
7235500Sbostic     "di", PRO_INT, 16, 0, &ps.decl_indent,
7335500Sbostic     "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
7435500Sbostic     "d", PRO_INT, 0, 0, &ps.unindent_displace,
7535500Sbostic     "eei", PRO_BOOL, false, ON, &extra_expression_indent,
7635500Sbostic     "ei", PRO_BOOL, true, ON, &ps.else_if,
7735500Sbostic     "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
7835500Sbostic     "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
7935500Sbostic     "fb", PRO_FONT, 0, 0, (int *) &bodyf,
8035500Sbostic     "fc1", PRO_BOOL, true, ON, &format_col1_comments,
8135500Sbostic     "fc", PRO_FONT, 0, 0, (int *) &scomf,
8235500Sbostic     "fk", PRO_FONT, 0, 0, (int *) &keywordf,
8335500Sbostic     "fs", PRO_FONT, 0, 0, (int *) &stringf,
8435500Sbostic     "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
8535500Sbostic     "i", PRO_INT, 8, 0, &ps.ind_size,
8635500Sbostic     "lc", PRO_INT, 0, 0, &block_comment_max_col,
8735500Sbostic     "lp", PRO_BOOL, true, ON, &lineup_to_parens,
8835500Sbostic     "l", PRO_INT, 78, 0, &max_col,
8935500Sbostic     "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation,
9035500Sbostic     "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop,
9135500Sbostic     "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations,
9235500Sbostic     "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs,
9335500Sbostic     "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments,
9435500Sbostic     "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
9535500Sbostic     "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
9635500Sbostic     "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline,
9735500Sbostic     "nce", PRO_BOOL, true, OFF, &cuddle_else,
9835500Sbostic     "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
9935500Sbostic     "neei", PRO_BOOL, false, OFF, &extra_expression_indent,
10035500Sbostic     "nei", PRO_BOOL, true, OFF, &ps.else_if,
10135500Sbostic     "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
10235500Sbostic     "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
10335500Sbostic     "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
10435500Sbostic     "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
10535500Sbostic     "npro", PRO_SPECIAL, 0, IGN, 0,
10635500Sbostic     "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
10735500Sbostic     "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
10835500Sbostic     "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
10935500Sbostic     "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines,
11035500Sbostic     "nv", PRO_BOOL, false, OFF, &verbose,
11135500Sbostic     "pcs", PRO_BOOL, false, ON, &proc_calls_space,
11235500Sbostic     "psl", PRO_BOOL, true, ON, &procnames_start_line,
11335500Sbostic     "ps", PRO_BOOL, false, ON, &pointer_as_binop,
11435500Sbostic     "sc", PRO_BOOL, true, ON, &star_comment_cont,
11535500Sbostic     "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines,
11635500Sbostic     "st", PRO_SPECIAL, 0, STDIN, 0,
11735500Sbostic     "troff", PRO_BOOL, false, ON, &troff,
11835500Sbostic     "v", PRO_BOOL, false, ON, &verbose,
11935500Sbostic     /* whew! */
12035500Sbostic     0, 0, 0, 0, 0
12124647Smckusick };
12224647Smckusick 
12324647Smckusick /*
12435500Sbostic  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
12535500Sbostic  * given in these files.
12624647Smckusick  */
12724647Smckusick set_profile()
12824647Smckusick {
12924647Smckusick     register FILE *f;
13035500Sbostic     char        fname[BUFSIZ];
13135508Sbostic     static char prof[] = ".indent.pro";
13224647Smckusick 
13335508Sbostic     sprintf(fname, "%s/%s", getenv("HOME"), prof);
13438011Sbostic     if ((f = fopen(option_source = fname, "r")) != NULL) {
13524647Smckusick 	scan_profile(f);
13624647Smckusick 	(void) fclose(f);
13724647Smckusick     }
13838011Sbostic     if ((f = fopen(option_source = prof, "r")) != NULL) {
13924647Smckusick 	scan_profile(f);
14024647Smckusick 	(void) fclose(f);
14124647Smckusick     }
14238011Sbostic     option_source = "Command line";
14324647Smckusick }
14424647Smckusick 
14524647Smckusick scan_profile(f)
14624647Smckusick     register FILE *f;
14724647Smckusick {
14835508Sbostic     register int i;
14935500Sbostic     register char *p;
15035500Sbostic     char        buf[BUFSIZ];
15124647Smckusick 
15235500Sbostic     while (1) {
15335508Sbostic 	for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
15435500Sbostic 	if (p != buf) {
15535500Sbostic 	    *p++ = 0;
15635500Sbostic 	    if (verbose)
15735500Sbostic 		printf("profile: %s\n", buf);
15835500Sbostic 	    set_option(buf);
15924647Smckusick 	}
16035508Sbostic 	else if (i == EOF)
16135500Sbostic 	    return;
16224647Smckusick     }
16324647Smckusick }
16424647Smckusick 
16524647Smckusick char       *param_start;
16624647Smckusick 
16724647Smckusick eqin(s1, s2)
16824647Smckusick     register char *s1;
16924647Smckusick     register char *s2;
17024647Smckusick {
17124647Smckusick     while (*s1) {
17224647Smckusick 	if (*s1++ != *s2++)
17324647Smckusick 	    return (false);
17424647Smckusick     }
17524647Smckusick     param_start = s2;
17624647Smckusick     return (true);
17724647Smckusick }
17824647Smckusick 
17924647Smckusick /*
18024647Smckusick  * Set the defaults.
18124647Smckusick  */
18224647Smckusick set_defaults()
18324647Smckusick {
18424647Smckusick     register struct pro *p;
18524647Smckusick 
18624647Smckusick     /*
18735500Sbostic      * Because ps.case_indent is a float, we can't initialize it from the
18835500Sbostic      * table:
18924647Smckusick      */
19024647Smckusick     ps.case_indent = 0.0;	/* -cli0.0 */
19124647Smckusick     for (p = pro; p->p_name; p++)
19235500Sbostic 	if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
19324647Smckusick 	    *p->p_obj = p->p_default;
19424647Smckusick }
19524647Smckusick 
19624647Smckusick set_option(arg)
19724647Smckusick     register char *arg;
19824647Smckusick {
19924647Smckusick     register struct pro *p;
20024647Smckusick     extern double atof();
20124647Smckusick 
20224647Smckusick     arg++;			/* ignore leading "-" */
20324647Smckusick     for (p = pro; p->p_name; p++)
20424647Smckusick 	if (*p->p_name == *arg && eqin(p->p_name, arg))
20524647Smckusick 	    goto found;
20638011Sbostic     fprintf(stderr, "indent: %s: unknown parameter \"%s\"\n", option_source, arg - 1);
20724647Smckusick     exit(1);
20824647Smckusick found:
20924647Smckusick     switch (p->p_type) {
21024647Smckusick 
21135500Sbostic     case PRO_SPECIAL:
21235500Sbostic 	switch (p->p_special) {
21324647Smckusick 
21435500Sbostic 	case IGN:
21535500Sbostic 	    break;
21624647Smckusick 
21735500Sbostic 	case CLI:
21835500Sbostic 	    if (*param_start == 0)
21935500Sbostic 		goto need_param;
22035500Sbostic 	    ps.case_indent = atof(param_start);
22124647Smckusick 	    break;
22224647Smckusick 
22335500Sbostic 	case STDIN:
22435500Sbostic 	    if (input == 0)
22535500Sbostic 		input = stdin;
22635500Sbostic 	    if (output == 0)
22735500Sbostic 		output = stdout;
22824647Smckusick 	    break;
22924647Smckusick 
23035500Sbostic 	case KEY:
23135500Sbostic 	    if (*param_start == 0)
23235500Sbostic 		goto need_param;
23335500Sbostic 	    {
23435500Sbostic 		register char *str = (char *) malloc(strlen(param_start) + 1);
23535500Sbostic 		strcpy(str, param_start);
23635500Sbostic 		addkey(str, 4);
23724647Smckusick 	    }
23824647Smckusick 	    break;
23924647Smckusick 
24024647Smckusick 	default:
24135500Sbostic 	    fprintf(stderr, "\
24235500Sbostic indent: set_option: internal error: p_special %d\n", p->p_special);
24324647Smckusick 	    exit(1);
24435500Sbostic 	}
24535500Sbostic 	break;
24635500Sbostic 
24735500Sbostic     case PRO_BOOL:
24835500Sbostic 	if (p->p_special == OFF)
24935500Sbostic 	    *p->p_obj = false;
25035500Sbostic 	else
25135500Sbostic 	    *p->p_obj = true;
25235500Sbostic 	break;
25335500Sbostic 
25435500Sbostic     case PRO_INT:
25538011Sbostic 	if (!isdigit(*param_start)) {
25635500Sbostic     need_param:
25738011Sbostic 	    fprintf(stderr, "indent: %s: ``%s'' requires a parameter\n",
25838011Sbostic 		    option_source, arg - 1);
25935500Sbostic 	    exit(1);
26035500Sbostic 	}
26135500Sbostic 	*p->p_obj = atoi(param_start);
26235500Sbostic 	break;
26335500Sbostic 
26435500Sbostic     case PRO_FONT:
26535500Sbostic 	parsefont((struct fstate *) p->p_obj, param_start);
26635500Sbostic 	break;
26735500Sbostic 
26835500Sbostic     default:
26935500Sbostic 	fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
27035500Sbostic 		p->p_type);
27135500Sbostic 	exit(1);
27224647Smckusick     }
27324647Smckusick }
274