Home | History | Annotate | Line # | Download | only in indent
args.c revision 1.25
      1  1.25    rillig /*	$NetBSD: args.c,v 1.25 2021/09/25 17:11:23 rillig Exp $	*/
      2   1.3       tls 
      3  1.14     kamil /*-
      4  1.14     kamil  * SPDX-License-Identifier: BSD-4-Clause
      5  1.14     kamil  *
      6  1.14     kamil  * Copyright (c) 1985 Sun Microsystems, Inc.
      7   1.4       mrg  * Copyright (c) 1980, 1993
      8   1.4       mrg  *	The Regents of the University of California.  All rights reserved.
      9   1.1       cgd  * All rights reserved.
     10   1.1       cgd  *
     11   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1       cgd  * modification, are permitted provided that the following conditions
     13   1.1       cgd  * are met:
     14   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     20   1.1       cgd  *    must display the following acknowledgement:
     21   1.1       cgd  *	This product includes software developed by the University of
     22   1.1       cgd  *	California, Berkeley and its contributors.
     23   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     24   1.1       cgd  *    may be used to endorse or promote products derived from this software
     25   1.1       cgd  *    without specific prior written permission.
     26   1.1       cgd  *
     27   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1       cgd  * SUCH DAMAGE.
     38   1.1       cgd  */
     39   1.1       cgd 
     40  1.14     kamil #if 0
     41  1.14     kamil static char sccsid[] = "@(#)args.c	8.1 (Berkeley) 6/6/93";
     42  1.14     kamil #endif
     43  1.14     kamil 
     44   1.5     lukem #include <sys/cdefs.h>
     45  1.14     kamil #if defined(__NetBSD__)
     46  1.25    rillig __RCSID("$NetBSD: args.c,v 1.25 2021/09/25 17:11:23 rillig Exp $");
     47  1.14     kamil #elif defined(__FreeBSD__)
     48  1.14     kamil __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
     49  1.14     kamil #endif
     50   1.1       cgd 
     51   1.1       cgd /*
     52   1.1       cgd  * Argument scanning and profile reading code.  Default parameters are set
     53   1.1       cgd  * here as well.
     54   1.1       cgd  */
     55   1.1       cgd 
     56   1.5     lukem #include <ctype.h>
     57  1.12  ginsbach #include <err.h>
     58  1.14     kamil #include <limits.h>
     59   1.1       cgd #include <stdio.h>
     60   1.1       cgd #include <stdlib.h>
     61   1.1       cgd #include <string.h>
     62  1.15    rillig 
     63  1.14     kamil #include "indent.h"
     64  1.14     kamil 
     65  1.14     kamil #define INDENT_VERSION	"2.0"
     66   1.1       cgd 
     67   1.1       cgd /* profile types */
     68   1.1       cgd #define	PRO_SPECIAL	1	/* special case */
     69   1.1       cgd #define	PRO_BOOL	2	/* boolean */
     70   1.1       cgd #define	PRO_INT		3	/* integer */
     71   1.1       cgd 
     72   1.1       cgd /* profile specials for booleans */
     73   1.1       cgd #define	ON		1	/* turn it on */
     74   1.1       cgd #define	OFF		0	/* turn it off */
     75   1.1       cgd 
     76   1.1       cgd /* profile specials for specials */
     77   1.1       cgd #define	IGN		1	/* ignore it */
     78   1.1       cgd #define	CLI		2	/* case label indent (float) */
     79   1.1       cgd #define	STDIN		3	/* use stdin */
     80   1.1       cgd #define	KEY		4	/* type (keyword) */
     81   1.1       cgd 
     82  1.14     kamil static void scan_profile(FILE *);
     83  1.14     kamil 
     84  1.18    rillig #define	KEY_FILE	5	/* only used for args */
     85  1.18    rillig #define VERSION		6	/* only used for args */
     86  1.14     kamil 
     87  1.10     lukem const char *option_source = "?";
     88   1.1       cgd 
     89  1.14     kamil void add_typedefs_from_file(const char *str);
     90  1.14     kamil 
     91  1.25    rillig #if __STDC_VERSION__ >= 201112L
     92  1.25    rillig #define assert_type(expr, type) _Generic((expr), type : (expr))
     93  1.25    rillig #else
     94  1.25    rillig #define assert_type(expr, type) (expr)
     95  1.25    rillig #endif
     96  1.25    rillig #define bool_option(name, value, var) \
     97  1.25    rillig 	{name, PRO_BOOL, value, assert_type(&(var), ibool *)}
     98  1.25    rillig #define int_option(name, value, var) \
     99  1.25    rillig 	{name, PRO_INT, value, assert_type(&(var), int *)}
    100  1.25    rillig #define special_option(name, value) \
    101  1.25    rillig 	{name, PRO_SPECIAL, assert_type(value, int), NULL}
    102  1.25    rillig 
    103   1.1       cgd /*
    104   1.1       cgd  * N.B.: because of the way the table here is scanned, options whose names are
    105   1.1       cgd  * substrings of other options must occur later; that is, with -lp vs -l, -lp
    106  1.25    rillig  * must be first.
    107   1.1       cgd  */
    108  1.17    rillig const struct pro {
    109  1.14     kamil     const char *p_name;		/* name, e.g. -bl, -cli */
    110  1.14     kamil     int         p_type;		/* type (int, bool, special) */
    111  1.14     kamil     int         p_special;	/* depends on type */
    112  1.25    rillig     void        *p_obj;		/* the associated variable */
    113  1.14     kamil }           pro[] = {
    114  1.25    rillig     special_option("T", KEY),
    115  1.25    rillig     special_option("U", KEY_FILE),
    116  1.25    rillig     special_option("-version", VERSION),
    117  1.25    rillig     special_option("P", IGN),
    118  1.25    rillig     bool_option("bacc", ON, opt.blanklines_around_conditional_compilation),
    119  1.25    rillig     bool_option("badp", ON, opt.blanklines_after_declarations_at_proctop),
    120  1.25    rillig     bool_option("bad", ON, opt.blanklines_after_declarations),
    121  1.25    rillig     bool_option("bap", ON, opt.blanklines_after_procs),
    122  1.25    rillig     bool_option("bbb", ON, opt.blanklines_before_blockcomments),
    123  1.25    rillig     bool_option("bc", OFF, opt.leave_comma),
    124  1.25    rillig     bool_option("bl", OFF, opt.btype_2),
    125  1.25    rillig     bool_option("br", ON, opt.btype_2),
    126  1.25    rillig     bool_option("bs", ON, opt.Bill_Shannon),
    127  1.25    rillig     bool_option("cdb", ON, opt.comment_delimiter_on_blankline),
    128  1.25    rillig     int_option("cd", 0, opt.decl_comment_column),
    129  1.25    rillig     bool_option("ce", ON, opt.cuddle_else),
    130  1.25    rillig     int_option("ci", 0, opt.continuation_indent),
    131  1.25    rillig     special_option("cli", CLI),
    132  1.25    rillig     bool_option("cs", ON, opt.space_after_cast),
    133  1.25    rillig     int_option("c", 0, opt.comment_column),
    134  1.25    rillig     int_option("di", 0, opt.decl_indent),
    135  1.25    rillig     bool_option("dj", ON, opt.ljust_decl),
    136  1.25    rillig     int_option("d", 0, opt.unindent_displace),
    137  1.25    rillig     bool_option("eei", ON, opt.extra_expression_indent),
    138  1.25    rillig     bool_option("ei", ON, opt.else_if),
    139  1.25    rillig     bool_option("fbs", ON, opt.function_brace_split),
    140  1.25    rillig     bool_option("fc1", ON, opt.format_col1_comments),
    141  1.25    rillig     bool_option("fcb", ON, opt.format_block_comments),
    142  1.25    rillig     bool_option("ip", ON, opt.indent_parameters),
    143  1.25    rillig     int_option("i", 0, opt.indent_size),
    144  1.25    rillig     int_option("lc", 0, opt.block_comment_max_line_length),
    145  1.25    rillig     int_option("ldi", 0, opt.local_decl_indent),
    146  1.25    rillig     bool_option("lpl", ON, opt.lineup_to_parens_always),
    147  1.25    rillig     bool_option("lp", ON, opt.lineup_to_parens),
    148  1.25    rillig     int_option("l", 0, opt.max_line_length),
    149  1.25    rillig     bool_option("nbacc", OFF, opt.blanklines_around_conditional_compilation),
    150  1.25    rillig     bool_option("nbadp", OFF, opt.blanklines_after_declarations_at_proctop),
    151  1.25    rillig     bool_option("nbad", OFF, opt.blanklines_after_declarations),
    152  1.25    rillig     bool_option("nbap", OFF, opt.blanklines_after_procs),
    153  1.25    rillig     bool_option("nbbb", OFF, opt.blanklines_before_blockcomments),
    154  1.25    rillig     bool_option("nbc", ON, opt.leave_comma),
    155  1.25    rillig     bool_option("nbs", OFF, opt.Bill_Shannon),
    156  1.25    rillig     bool_option("ncdb", OFF, opt.comment_delimiter_on_blankline),
    157  1.25    rillig     bool_option("nce", OFF, opt.cuddle_else),
    158  1.25    rillig     bool_option("ncs", OFF, opt.space_after_cast),
    159  1.25    rillig     bool_option("ndj", OFF, opt.ljust_decl),
    160  1.25    rillig     bool_option("neei", OFF, opt.extra_expression_indent),
    161  1.25    rillig     bool_option("nei", OFF, opt.else_if),
    162  1.25    rillig     bool_option("nfbs", OFF, opt.function_brace_split),
    163  1.25    rillig     bool_option("nfc1", OFF, opt.format_col1_comments),
    164  1.25    rillig     bool_option("nfcb", OFF, opt.format_block_comments),
    165  1.25    rillig     bool_option("nip", OFF, opt.indent_parameters),
    166  1.25    rillig     bool_option("nlpl", OFF, opt.lineup_to_parens_always),
    167  1.25    rillig     bool_option("nlp", OFF, opt.lineup_to_parens),
    168  1.25    rillig     bool_option("npcs", OFF, opt.proc_calls_space),
    169  1.25    rillig     special_option("npro", IGN),
    170  1.25    rillig     bool_option("npsl", OFF, opt.procnames_start_line),
    171  1.25    rillig     bool_option("nsc", OFF, opt.star_comment_cont),
    172  1.25    rillig     bool_option("nsob", OFF, opt.swallow_optional_blanklines),
    173  1.25    rillig     bool_option("nut", OFF, opt.use_tabs),
    174  1.25    rillig     bool_option("nv", OFF, opt.verbose),
    175  1.25    rillig     bool_option("pcs", ON, opt.proc_calls_space),
    176  1.25    rillig     bool_option("psl", ON, opt.procnames_start_line),
    177  1.25    rillig     bool_option("sc", ON, opt.star_comment_cont),
    178  1.25    rillig     bool_option("sob", ON, opt.swallow_optional_blanklines),
    179  1.25    rillig     special_option("st", STDIN),
    180  1.25    rillig     bool_option("ta", ON, opt.auto_typedefs),
    181  1.25    rillig     int_option("ts", 0, opt.tabsize),
    182  1.25    rillig     bool_option("ut", ON, opt.use_tabs),
    183  1.25    rillig     bool_option("v", ON, opt.verbose),
    184  1.14     kamil     /* whew! */
    185  1.24    rillig     {0, 0, 0, 0}
    186   1.1       cgd };
    187  1.14     kamil 
    188   1.1       cgd /*
    189   1.1       cgd  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
    190   1.1       cgd  * given in these files.
    191   1.1       cgd  */
    192   1.5     lukem void
    193  1.14     kamil set_profile(const char *profile_name)
    194   1.1       cgd {
    195  1.14     kamil     FILE *f;
    196  1.14     kamil     char fname[PATH_MAX];
    197  1.14     kamil     static char prof[] = ".indent.pro";
    198   1.5     lukem 
    199  1.14     kamil     if (profile_name == NULL)
    200   1.8    itojun 	snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
    201  1.14     kamil     else
    202  1.14     kamil 	snprintf(fname, sizeof(fname), "%s", profile_name + 2);
    203  1.14     kamil     if ((f = fopen(option_source = fname, "r")) != NULL) {
    204  1.14     kamil 	scan_profile(f);
    205  1.14     kamil 	(void) fclose(f);
    206  1.14     kamil     }
    207  1.14     kamil     if ((f = fopen(option_source = prof, "r")) != NULL) {
    208  1.14     kamil 	scan_profile(f);
    209  1.14     kamil 	(void) fclose(f);
    210  1.14     kamil     }
    211  1.14     kamil     option_source = "Command line";
    212   1.1       cgd }
    213   1.1       cgd 
    214  1.14     kamil static void
    215   1.7       wiz scan_profile(FILE *f)
    216   1.1       cgd {
    217  1.15    rillig     int		comment_index, i;
    218  1.14     kamil     char	*p;
    219  1.14     kamil     char        buf[BUFSIZ];
    220  1.14     kamil 
    221  1.22    rillig     for (;;) {
    222  1.14     kamil 	p = buf;
    223  1.15    rillig 	comment_index = 0;
    224  1.14     kamil 	while ((i = getc(f)) != EOF) {
    225  1.25    rillig 	    if (i == '*' && comment_index == 0 && p > buf && p[-1] == '/') {
    226  1.22    rillig 		comment_index = (int)(p - buf);
    227  1.14     kamil 		*p++ = i;
    228  1.25    rillig 	    } else if (i == '/' && comment_index != 0 && p > buf && p[-1] == '*') {
    229  1.15    rillig 		p = buf + comment_index - 1;
    230  1.15    rillig 		comment_index = 0;
    231  1.14     kamil 	    } else if (isspace((unsigned char)i)) {
    232  1.25    rillig 		if (p > buf && comment_index == 0)
    233  1.14     kamil 		    break;
    234  1.14     kamil 	    } else {
    235  1.14     kamil 		*p++ = i;
    236  1.14     kamil 	    }
    237   1.1       cgd 	}
    238  1.14     kamil 	if (p != buf) {
    239  1.14     kamil 	    *p++ = 0;
    240  1.14     kamil 	    if (opt.verbose)
    241  1.14     kamil 		printf("profile: %s\n", buf);
    242  1.14     kamil 	    set_option(buf);
    243  1.19    rillig 	} else if (i == EOF)
    244  1.14     kamil 	    return;
    245  1.14     kamil     }
    246   1.1       cgd }
    247   1.1       cgd 
    248  1.14     kamil static const char *
    249  1.10     lukem eqin(const char *s1, const char *s2)
    250   1.1       cgd {
    251  1.25    rillig     while (*s1 != '\0') {
    252  1.14     kamil 	if (*s1++ != *s2++)
    253  1.16    rillig 	    return NULL;
    254  1.14     kamil     }
    255  1.16    rillig     return s2;
    256   1.1       cgd }
    257  1.14     kamil 
    258   1.5     lukem void
    259   1.7       wiz set_option(char *arg)
    260   1.1       cgd {
    261  1.17    rillig     const struct pro *p;
    262  1.14     kamil     const char	*param_start;
    263   1.1       cgd 
    264  1.14     kamil     arg++;			/* ignore leading "-" */
    265  1.25    rillig     for (p = pro; p->p_name != NULL; p++)
    266  1.14     kamil 	if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL)
    267  1.14     kamil 	    goto found;
    268  1.14     kamil     errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
    269   1.1       cgd found:
    270  1.14     kamil     switch (p->p_type) {
    271   1.1       cgd 
    272  1.14     kamil     case PRO_SPECIAL:
    273  1.14     kamil 	switch (p->p_special) {
    274   1.1       cgd 
    275  1.14     kamil 	case IGN:
    276  1.14     kamil 	    break;
    277   1.1       cgd 
    278  1.14     kamil 	case CLI:
    279  1.14     kamil 	    if (*param_start == 0)
    280  1.14     kamil 		goto need_param;
    281  1.14     kamil 	    opt.case_indent = atof(param_start);
    282  1.14     kamil 	    break;
    283  1.14     kamil 
    284  1.14     kamil 	case STDIN:
    285  1.14     kamil 	    if (input == NULL)
    286  1.14     kamil 		input = stdin;
    287  1.14     kamil 	    if (output == NULL)
    288  1.14     kamil 		output = stdout;
    289  1.14     kamil 	    break;
    290  1.14     kamil 
    291  1.14     kamil 	case KEY:
    292  1.14     kamil 	    if (*param_start == 0)
    293  1.14     kamil 		goto need_param;
    294  1.14     kamil 	    add_typename(param_start);
    295  1.14     kamil 	    break;
    296  1.14     kamil 
    297  1.14     kamil 	case KEY_FILE:
    298  1.14     kamil 	    if (*param_start == 0)
    299  1.14     kamil 		goto need_param;
    300  1.14     kamil 	    add_typedefs_from_file(param_start);
    301  1.14     kamil 	    break;
    302  1.14     kamil 
    303  1.14     kamil 	case VERSION:
    304  1.14     kamil 	    printf("FreeBSD indent %s\n", INDENT_VERSION);
    305  1.14     kamil 	    exit(0);
    306  1.22    rillig 	    /*NOTREACHED*/
    307   1.1       cgd 
    308   1.5     lukem 	default:
    309  1.14     kamil 	    errx(1, "set_option: internal error: p_special %d", p->p_special);
    310  1.14     kamil 	}
    311  1.14     kamil 	break;
    312  1.14     kamil 
    313  1.14     kamil     case PRO_BOOL:
    314  1.14     kamil 	if (p->p_special == OFF)
    315  1.25    rillig 	    *(ibool *)p->p_obj = false;
    316  1.14     kamil 	else
    317  1.25    rillig 	    *(ibool *)p->p_obj = true;
    318  1.14     kamil 	break;
    319  1.14     kamil 
    320  1.14     kamil     case PRO_INT:
    321  1.14     kamil 	if (!isdigit((unsigned char)*param_start)) {
    322  1.14     kamil     need_param:
    323  1.14     kamil 	    errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
    324   1.1       cgd 	}
    325  1.25    rillig 	*(int *)p->p_obj = atoi(param_start);
    326  1.14     kamil 	break;
    327  1.14     kamil 
    328  1.14     kamil     default:
    329  1.14     kamil 	errx(1, "set_option: internal error: p_type %d", p->p_type);
    330  1.14     kamil     }
    331  1.14     kamil }
    332  1.14     kamil 
    333  1.14     kamil void
    334  1.14     kamil add_typedefs_from_file(const char *str)
    335  1.14     kamil {
    336  1.14     kamil     FILE *file;
    337  1.14     kamil     char line[BUFSIZ];
    338  1.14     kamil 
    339  1.14     kamil     if ((file = fopen(str, "r")) == NULL) {
    340  1.14     kamil 	fprintf(stderr, "indent: cannot open file %s\n", str);
    341  1.14     kamil 	exit(1);
    342  1.14     kamil     }
    343  1.14     kamil     while ((fgets(line, BUFSIZ, file)) != NULL) {
    344  1.14     kamil 	/* Remove trailing whitespace */
    345  1.14     kamil 	line[strcspn(line, " \t\n\r")] = '\0';
    346  1.14     kamil 	add_typename(line);
    347  1.14     kamil     }
    348  1.14     kamil     fclose(file);
    349   1.1       cgd }
    350