Home | History | Annotate | Line # | Download | only in indent
args.c revision 1.4
      1 /*	$NetBSD: args.c,v 1.4 1997/10/18 16:04:26 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
      7  * Copyright (c) 1985 Sun Microsystems, Inc.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)args.c	8.1 (Berkeley) 6/6/93";
     42 #else
     43 static char rcsid[] = "$NetBSD: args.c,v 1.4 1997/10/18 16:04:26 mrg Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 /*
     48  * Argument scanning and profile reading code.  Default parameters are set
     49  * here as well.
     50  */
     51 
     52 #include <stdio.h>
     53 #include <ctype.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include "indent_globs.h"
     57 
     58 /* profile types */
     59 #define	PRO_SPECIAL	1	/* special case */
     60 #define	PRO_BOOL	2	/* boolean */
     61 #define	PRO_INT		3	/* integer */
     62 #define PRO_FONT	4	/* troff font */
     63 
     64 /* profile specials for booleans */
     65 #define	ON		1	/* turn it on */
     66 #define	OFF		0	/* turn it off */
     67 
     68 /* profile specials for specials */
     69 #define	IGN		1	/* ignore it */
     70 #define	CLI		2	/* case label indent (float) */
     71 #define	STDIN		3	/* use stdin */
     72 #define	KEY		4	/* type (keyword) */
     73 
     74 char *option_source = "?";
     75 
     76 /*
     77  * N.B.: because of the way the table here is scanned, options whose names are
     78  * substrings of other options must occur later; that is, with -lp vs -l, -lp
     79  * must be first.  Also, while (most) booleans occur more than once, the last
     80  * default value is the one actually assigned.
     81  */
     82 struct pro {
     83     char       *p_name;		/* name, eg -bl, -cli */
     84     int         p_type;		/* type (int, bool, special) */
     85     int         p_default;	/* the default value (if int) */
     86     int         p_special;	/* depends on type */
     87     int        *p_obj;		/* the associated variable */
     88 }           pro[] = {
     89 
     90     "T", PRO_SPECIAL, 0, KEY, 0,
     91     "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation,
     92     "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop,
     93     "bad", PRO_BOOL, false, ON, &blanklines_after_declarations,
     94     "bap", PRO_BOOL, false, ON, &blanklines_after_procs,
     95     "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments,
     96     "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
     97     "bl", PRO_BOOL, true, OFF, &btype_2,
     98     "br", PRO_BOOL, true, ON, &btype_2,
     99     "bs", PRO_BOOL, false, ON, &Bill_Shannon,
    100     "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline,
    101     "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
    102     "ce", PRO_BOOL, true, ON, &cuddle_else,
    103     "ci", PRO_INT, 0, 0, &continuation_indent,
    104     "cli", PRO_SPECIAL, 0, CLI, 0,
    105     "c", PRO_INT, 33, 0, &ps.com_ind,
    106     "di", PRO_INT, 16, 0, &ps.decl_indent,
    107     "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
    108     "d", PRO_INT, 0, 0, &ps.unindent_displace,
    109     "eei", PRO_BOOL, false, ON, &extra_expression_indent,
    110     "ei", PRO_BOOL, true, ON, &ps.else_if,
    111     "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
    112     "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
    113     "fb", PRO_FONT, 0, 0, (int *) &bodyf,
    114     "fc1", PRO_BOOL, true, ON, &format_col1_comments,
    115     "fc", PRO_FONT, 0, 0, (int *) &scomf,
    116     "fk", PRO_FONT, 0, 0, (int *) &keywordf,
    117     "fs", PRO_FONT, 0, 0, (int *) &stringf,
    118     "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
    119     "i", PRO_INT, 8, 0, &ps.ind_size,
    120     "lc", PRO_INT, 0, 0, &block_comment_max_col,
    121     "lp", PRO_BOOL, true, ON, &lineup_to_parens,
    122     "l", PRO_INT, 78, 0, &max_col,
    123     "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation,
    124     "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop,
    125     "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations,
    126     "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs,
    127     "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments,
    128     "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
    129     "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
    130     "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline,
    131     "nce", PRO_BOOL, true, OFF, &cuddle_else,
    132     "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
    133     "neei", PRO_BOOL, false, OFF, &extra_expression_indent,
    134     "nei", PRO_BOOL, true, OFF, &ps.else_if,
    135     "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
    136     "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
    137     "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
    138     "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
    139     "npro", PRO_SPECIAL, 0, IGN, 0,
    140     "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
    141     "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
    142     "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
    143     "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines,
    144     "nv", PRO_BOOL, false, OFF, &verbose,
    145     "pcs", PRO_BOOL, false, ON, &proc_calls_space,
    146     "psl", PRO_BOOL, true, ON, &procnames_start_line,
    147     "ps", PRO_BOOL, false, ON, &pointer_as_binop,
    148     "sc", PRO_BOOL, true, ON, &star_comment_cont,
    149     "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines,
    150     "st", PRO_SPECIAL, 0, STDIN, 0,
    151     "troff", PRO_BOOL, false, ON, &troff,
    152     "v", PRO_BOOL, false, ON, &verbose,
    153     /* whew! */
    154     0, 0, 0, 0, 0
    155 };
    156 
    157 /*
    158  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
    159  * given in these files.
    160  */
    161 set_profile()
    162 {
    163     register FILE *f;
    164     char        fname[BUFSIZ];
    165     static char prof[] = ".indent.pro";
    166 
    167     sprintf(fname, "%s/%s", getenv("HOME"), prof);
    168     if ((f = fopen(option_source = fname, "r")) != NULL) {
    169 	scan_profile(f);
    170 	(void) fclose(f);
    171     }
    172     if ((f = fopen(option_source = prof, "r")) != NULL) {
    173 	scan_profile(f);
    174 	(void) fclose(f);
    175     }
    176     option_source = "Command line";
    177 }
    178 
    179 scan_profile(f)
    180     register FILE *f;
    181 {
    182     register int i;
    183     register char *p;
    184     char        buf[BUFSIZ];
    185 
    186     while (1) {
    187 	for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
    188 	if (p != buf) {
    189 	    *p++ = 0;
    190 	    if (verbose)
    191 		printf("profile: %s\n", buf);
    192 	    set_option(buf);
    193 	}
    194 	else if (i == EOF)
    195 	    return;
    196     }
    197 }
    198 
    199 char       *param_start;
    200 
    201 eqin(s1, s2)
    202     register char *s1;
    203     register char *s2;
    204 {
    205     while (*s1) {
    206 	if (*s1++ != *s2++)
    207 	    return (false);
    208     }
    209     param_start = s2;
    210     return (true);
    211 }
    212 
    213 /*
    214  * Set the defaults.
    215  */
    216 set_defaults()
    217 {
    218     register struct pro *p;
    219 
    220     /*
    221      * Because ps.case_indent is a float, we can't initialize it from the
    222      * table:
    223      */
    224     ps.case_indent = 0.0;	/* -cli0.0 */
    225     for (p = pro; p->p_name; p++)
    226 	if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
    227 	    *p->p_obj = p->p_default;
    228 }
    229 
    230 set_option(arg)
    231     register char *arg;
    232 {
    233     register struct pro *p;
    234     extern double atof();
    235 
    236     arg++;			/* ignore leading "-" */
    237     for (p = pro; p->p_name; p++)
    238 	if (*p->p_name == *arg && eqin(p->p_name, arg))
    239 	    goto found;
    240     fprintf(stderr, "indent: %s: unknown parameter \"%s\"\n", option_source, arg - 1);
    241     exit(1);
    242 found:
    243     switch (p->p_type) {
    244 
    245     case PRO_SPECIAL:
    246 	switch (p->p_special) {
    247 
    248 	case IGN:
    249 	    break;
    250 
    251 	case CLI:
    252 	    if (*param_start == 0)
    253 		goto need_param;
    254 	    ps.case_indent = atof(param_start);
    255 	    break;
    256 
    257 	case STDIN:
    258 	    if (input == 0)
    259 		input = stdin;
    260 	    if (output == 0)
    261 		output = stdout;
    262 	    break;
    263 
    264 	case KEY:
    265 	    if (*param_start == 0)
    266 		goto need_param;
    267 	    {
    268 		register char *str = (char *) malloc(strlen(param_start) + 1);
    269 		strcpy(str, param_start);
    270 		addkey(str, 4);
    271 	    }
    272 	    break;
    273 
    274 	default:
    275 	    fprintf(stderr, "\
    276 indent: set_option: internal error: p_special %d\n", p->p_special);
    277 	    exit(1);
    278 	}
    279 	break;
    280 
    281     case PRO_BOOL:
    282 	if (p->p_special == OFF)
    283 	    *p->p_obj = false;
    284 	else
    285 	    *p->p_obj = true;
    286 	break;
    287 
    288     case PRO_INT:
    289 	if (!isdigit(*param_start)) {
    290     need_param:
    291 	    fprintf(stderr, "indent: %s: ``%s'' requires a parameter\n",
    292 		    option_source, arg - 1);
    293 	    exit(1);
    294 	}
    295 	*p->p_obj = atoi(param_start);
    296 	break;
    297 
    298     case PRO_FONT:
    299 	parsefont((struct fstate *) p->p_obj, param_start);
    300 	break;
    301 
    302     default:
    303 	fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
    304 		p->p_type);
    305 	exit(1);
    306     }
    307 }
    308