xref: /csrg-svn/usr.bin/indent/args.c (revision 38011)
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  *
733767Sbostic  * Redistribution and use in source and binary forms are permitted
834885Sbostic  * provided that the above copyright notice and this paragraph are
934885Sbostic  * duplicated in all such forms and that any documentation,
1034885Sbostic  * advertising materials, and other materials related to such
1134885Sbostic  * distribution and use acknowledge that the software was developed
1235500Sbostic  * by the University of California, Berkeley, the University of Illinois,
1335500Sbostic  * Urbana, and Sun Microsystems, Inc.  The name of either University
1435500Sbostic  * or Sun Microsystems may not be used to endorse or promote products
1535500Sbostic  * derived from this software without specific prior written permission.
1634885Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734885Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1834885Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1924647Smckusick  */
2024647Smckusick 
2124647Smckusick #ifndef lint
22*38011Sbostic static char sccsid[] = "@(#)args.c	5.8 (Berkeley) 05/15/89";
2333767Sbostic #endif /* not lint */
2424647Smckusick 
2524647Smckusick /*
2635500Sbostic  * Argument scanning and profile reading code.  Default parameters are set
2735500Sbostic  * here as well.
2824647Smckusick  */
2924647Smckusick 
3024647Smckusick #include "indent_globs.h"
3124647Smckusick #include <ctype.h>
3224647Smckusick 
3335500Sbostic char       *getenv(), *index();
3424647Smckusick 
3524647Smckusick /* profile types */
3624647Smckusick #define	PRO_SPECIAL	1	/* special case */
3724647Smckusick #define	PRO_BOOL	2	/* boolean */
3824647Smckusick #define	PRO_INT		3	/* integer */
3935500Sbostic #define PRO_FONT	4	/* troff font */
4024647Smckusick 
4124647Smckusick /* profile specials for booleans */
4224647Smckusick #define	ON		1	/* turn it on */
4324647Smckusick #define	OFF		0	/* turn it off */
4424647Smckusick 
4524647Smckusick /* profile specials for specials */
4624647Smckusick #define	IGN		1	/* ignore it */
4724647Smckusick #define	CLI		2	/* case label indent (float) */
4824647Smckusick #define	STDIN		3	/* use stdin */
4924647Smckusick #define	KEY		4	/* type (keyword) */
5024647Smckusick 
51*38011Sbostic char *option_source = "?";
52*38011Sbostic 
5324647Smckusick /*
5435500Sbostic  * N.B.: because of the way the table here is scanned, options whose names are
5535500Sbostic  * substrings of other options must occur later; that is, with -lp vs -l, -lp
5635500Sbostic  * must be first.  Also, while (most) booleans occur more than once, the last
5735500Sbostic  * default value is the one actually assigned.
5824647Smckusick  */
5924647Smckusick struct pro {
6035500Sbostic     char       *p_name;		/* name, eg -bl, -cli */
6135500Sbostic     int         p_type;		/* type (int, bool, special) */
6235500Sbostic     int         p_default;	/* the default value (if int) */
6335500Sbostic     int         p_special;	/* depends on type */
6435500Sbostic     int        *p_obj;		/* the associated variable */
6535500Sbostic }           pro[] = {
6635500Sbostic 
6735500Sbostic     "T", PRO_SPECIAL, 0, KEY, 0,
6835500Sbostic     "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation,
6935500Sbostic     "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop,
7035500Sbostic     "bad", PRO_BOOL, false, ON, &blanklines_after_declarations,
7135500Sbostic     "bap", PRO_BOOL, false, ON, &blanklines_after_procs,
7235500Sbostic     "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments,
7335500Sbostic     "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
7435500Sbostic     "bl", PRO_BOOL, true, OFF, &btype_2,
7535500Sbostic     "br", PRO_BOOL, true, ON, &btype_2,
7635500Sbostic     "bs", PRO_BOOL, false, ON, &Bill_Shannon,
7735500Sbostic     "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline,
7835500Sbostic     "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
7935500Sbostic     "ce", PRO_BOOL, true, ON, &cuddle_else,
8035500Sbostic     "ci", PRO_INT, 0, 0, &continuation_indent,
8135500Sbostic     "cli", PRO_SPECIAL, 0, CLI, 0,
8235500Sbostic     "c", PRO_INT, 33, 0, &ps.com_ind,
8335500Sbostic     "di", PRO_INT, 16, 0, &ps.decl_indent,
8435500Sbostic     "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
8535500Sbostic     "d", PRO_INT, 0, 0, &ps.unindent_displace,
8635500Sbostic     "eei", PRO_BOOL, false, ON, &extra_expression_indent,
8735500Sbostic     "ei", PRO_BOOL, true, ON, &ps.else_if,
8835500Sbostic     "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
8935500Sbostic     "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
9035500Sbostic     "fb", PRO_FONT, 0, 0, (int *) &bodyf,
9135500Sbostic     "fc1", PRO_BOOL, true, ON, &format_col1_comments,
9235500Sbostic     "fc", PRO_FONT, 0, 0, (int *) &scomf,
9335500Sbostic     "fk", PRO_FONT, 0, 0, (int *) &keywordf,
9435500Sbostic     "fs", PRO_FONT, 0, 0, (int *) &stringf,
9535500Sbostic     "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
9635500Sbostic     "i", PRO_INT, 8, 0, &ps.ind_size,
9735500Sbostic     "lc", PRO_INT, 0, 0, &block_comment_max_col,
9835500Sbostic     "lp", PRO_BOOL, true, ON, &lineup_to_parens,
9935500Sbostic     "l", PRO_INT, 78, 0, &max_col,
10035500Sbostic     "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation,
10135500Sbostic     "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop,
10235500Sbostic     "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations,
10335500Sbostic     "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs,
10435500Sbostic     "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments,
10535500Sbostic     "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
10635500Sbostic     "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
10735500Sbostic     "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline,
10835500Sbostic     "nce", PRO_BOOL, true, OFF, &cuddle_else,
10935500Sbostic     "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
11035500Sbostic     "neei", PRO_BOOL, false, OFF, &extra_expression_indent,
11135500Sbostic     "nei", PRO_BOOL, true, OFF, &ps.else_if,
11235500Sbostic     "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
11335500Sbostic     "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
11435500Sbostic     "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
11535500Sbostic     "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
11635500Sbostic     "npro", PRO_SPECIAL, 0, IGN, 0,
11735500Sbostic     "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
11835500Sbostic     "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
11935500Sbostic     "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
12035500Sbostic     "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines,
12135500Sbostic     "nv", PRO_BOOL, false, OFF, &verbose,
12235500Sbostic     "pcs", PRO_BOOL, false, ON, &proc_calls_space,
12335500Sbostic     "psl", PRO_BOOL, true, ON, &procnames_start_line,
12435500Sbostic     "ps", PRO_BOOL, false, ON, &pointer_as_binop,
12535500Sbostic     "sc", PRO_BOOL, true, ON, &star_comment_cont,
12635500Sbostic     "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines,
12735500Sbostic     "st", PRO_SPECIAL, 0, STDIN, 0,
12835500Sbostic     "troff", PRO_BOOL, false, ON, &troff,
12935500Sbostic     "v", PRO_BOOL, false, ON, &verbose,
13035500Sbostic     /* whew! */
13135500Sbostic     0, 0, 0, 0, 0
13224647Smckusick };
13324647Smckusick 
13424647Smckusick /*
13535500Sbostic  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
13635500Sbostic  * given in these files.
13724647Smckusick  */
13824647Smckusick set_profile()
13924647Smckusick {
14024647Smckusick     register FILE *f;
14135500Sbostic     char        fname[BUFSIZ];
14235508Sbostic     static char prof[] = ".indent.pro";
14324647Smckusick 
14435508Sbostic     sprintf(fname, "%s/%s", getenv("HOME"), prof);
145*38011Sbostic     if ((f = fopen(option_source = fname, "r")) != NULL) {
14624647Smckusick 	scan_profile(f);
14724647Smckusick 	(void) fclose(f);
14824647Smckusick     }
149*38011Sbostic     if ((f = fopen(option_source = prof, "r")) != NULL) {
15024647Smckusick 	scan_profile(f);
15124647Smckusick 	(void) fclose(f);
15224647Smckusick     }
153*38011Sbostic     option_source = "Command line";
15424647Smckusick }
15524647Smckusick 
15624647Smckusick scan_profile(f)
15724647Smckusick     register FILE *f;
15824647Smckusick {
15935508Sbostic     register int i;
16035500Sbostic     register char *p;
16135500Sbostic     char        buf[BUFSIZ];
16224647Smckusick 
16335500Sbostic     while (1) {
16435508Sbostic 	for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
16535500Sbostic 	if (p != buf) {
16635500Sbostic 	    *p++ = 0;
16735500Sbostic 	    if (verbose)
16835500Sbostic 		printf("profile: %s\n", buf);
16935500Sbostic 	    set_option(buf);
17024647Smckusick 	}
17135508Sbostic 	else if (i == EOF)
17235500Sbostic 	    return;
17324647Smckusick     }
17424647Smckusick }
17524647Smckusick 
17624647Smckusick char       *param_start;
17724647Smckusick 
17824647Smckusick eqin(s1, s2)
17924647Smckusick     register char *s1;
18024647Smckusick     register char *s2;
18124647Smckusick {
18224647Smckusick     while (*s1) {
18324647Smckusick 	if (*s1++ != *s2++)
18424647Smckusick 	    return (false);
18524647Smckusick     }
18624647Smckusick     param_start = s2;
18724647Smckusick     return (true);
18824647Smckusick }
18924647Smckusick 
19024647Smckusick /*
19124647Smckusick  * Set the defaults.
19224647Smckusick  */
19324647Smckusick set_defaults()
19424647Smckusick {
19524647Smckusick     register struct pro *p;
19624647Smckusick 
19724647Smckusick     /*
19835500Sbostic      * Because ps.case_indent is a float, we can't initialize it from the
19935500Sbostic      * table:
20024647Smckusick      */
20124647Smckusick     ps.case_indent = 0.0;	/* -cli0.0 */
20224647Smckusick     for (p = pro; p->p_name; p++)
20335500Sbostic 	if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
20424647Smckusick 	    *p->p_obj = p->p_default;
20524647Smckusick }
20624647Smckusick 
20724647Smckusick set_option(arg)
20824647Smckusick     register char *arg;
20924647Smckusick {
21024647Smckusick     register struct pro *p;
21124647Smckusick     extern double atof();
21224647Smckusick 
21324647Smckusick     arg++;			/* ignore leading "-" */
21424647Smckusick     for (p = pro; p->p_name; p++)
21524647Smckusick 	if (*p->p_name == *arg && eqin(p->p_name, arg))
21624647Smckusick 	    goto found;
217*38011Sbostic     fprintf(stderr, "indent: %s: unknown parameter \"%s\"\n", option_source, arg - 1);
21824647Smckusick     exit(1);
21924647Smckusick found:
22024647Smckusick     switch (p->p_type) {
22124647Smckusick 
22235500Sbostic     case PRO_SPECIAL:
22335500Sbostic 	switch (p->p_special) {
22424647Smckusick 
22535500Sbostic 	case IGN:
22635500Sbostic 	    break;
22724647Smckusick 
22835500Sbostic 	case CLI:
22935500Sbostic 	    if (*param_start == 0)
23035500Sbostic 		goto need_param;
23135500Sbostic 	    ps.case_indent = atof(param_start);
23224647Smckusick 	    break;
23324647Smckusick 
23435500Sbostic 	case STDIN:
23535500Sbostic 	    if (input == 0)
23635500Sbostic 		input = stdin;
23735500Sbostic 	    if (output == 0)
23835500Sbostic 		output = stdout;
23924647Smckusick 	    break;
24024647Smckusick 
24135500Sbostic 	case KEY:
24235500Sbostic 	    if (*param_start == 0)
24335500Sbostic 		goto need_param;
24435500Sbostic 	    {
24535500Sbostic 		register char *str = (char *) malloc(strlen(param_start) + 1);
24635500Sbostic 		strcpy(str, param_start);
24735500Sbostic 		addkey(str, 4);
24824647Smckusick 	    }
24924647Smckusick 	    break;
25024647Smckusick 
25124647Smckusick 	default:
25235500Sbostic 	    fprintf(stderr, "\
25335500Sbostic indent: set_option: internal error: p_special %d\n", p->p_special);
25424647Smckusick 	    exit(1);
25535500Sbostic 	}
25635500Sbostic 	break;
25735500Sbostic 
25835500Sbostic     case PRO_BOOL:
25935500Sbostic 	if (p->p_special == OFF)
26035500Sbostic 	    *p->p_obj = false;
26135500Sbostic 	else
26235500Sbostic 	    *p->p_obj = true;
26335500Sbostic 	break;
26435500Sbostic 
26535500Sbostic     case PRO_INT:
266*38011Sbostic 	if (!isdigit(*param_start)) {
26735500Sbostic     need_param:
268*38011Sbostic 	    fprintf(stderr, "indent: %s: ``%s'' requires a parameter\n",
269*38011Sbostic 		    option_source, arg - 1);
27035500Sbostic 	    exit(1);
27135500Sbostic 	}
27235500Sbostic 	*p->p_obj = atoi(param_start);
27335500Sbostic 	break;
27435500Sbostic 
27535500Sbostic     case PRO_FONT:
27635500Sbostic 	parsefont((struct fstate *) p->p_obj, param_start);
27735500Sbostic 	break;
27835500Sbostic 
27935500Sbostic     default:
28035500Sbostic 	fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
28135500Sbostic 		p->p_type);
28235500Sbostic 	exit(1);
28324647Smckusick     }
28424647Smckusick }
285