Home | History | Annotate | Line # | Download | only in indent
args.c revision 1.31
      1  1.31    rillig /*	$NetBSD: args.c,v 1.31 2021/09/25 23:38:45 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.31    rillig __RCSID("$NetBSD: args.c,v 1.31 2021/09/25 23:38:45 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.29    rillig #include <stdint.h>
     60   1.1       cgd #include <stdio.h>
     61   1.1       cgd #include <stdlib.h>
     62   1.1       cgd #include <string.h>
     63  1.15    rillig 
     64  1.14     kamil #include "indent.h"
     65  1.14     kamil 
     66  1.14     kamil #define INDENT_VERSION	"2.0"
     67   1.1       cgd 
     68   1.1       cgd /* profile types */
     69   1.1       cgd #define	PRO_SPECIAL	1	/* special case */
     70   1.1       cgd #define	PRO_BOOL	2	/* boolean */
     71   1.1       cgd #define	PRO_INT		3	/* integer */
     72   1.1       cgd 
     73   1.1       cgd /* profile specials for specials */
     74   1.1       cgd #define	IGN		1	/* ignore it */
     75   1.1       cgd #define	CLI		2	/* case label indent (float) */
     76   1.1       cgd #define	STDIN		3	/* use stdin */
     77   1.1       cgd #define	KEY		4	/* type (keyword) */
     78  1.31    rillig #define	KEY_FILE	5	/* only used for args */
     79  1.31    rillig #define VERSION		6	/* only used for args */
     80   1.1       cgd 
     81  1.14     kamil static void scan_profile(FILE *);
     82  1.14     kamil 
     83  1.10     lukem const char *option_source = "?";
     84   1.1       cgd 
     85  1.30    rillig void add_typedefs_from_file(const char *);
     86  1.14     kamil 
     87  1.25    rillig #if __STDC_VERSION__ >= 201112L
     88  1.25    rillig #define assert_type(expr, type) _Generic((expr), type : (expr))
     89  1.25    rillig #else
     90  1.25    rillig #define assert_type(expr, type) (expr)
     91  1.25    rillig #endif
     92  1.25    rillig #define bool_option(name, value, var) \
     93  1.31    rillig 	{name, PRO_BOOL, /*CONSTCOND*/(value) ? 1 : 0, \
     94  1.31    rillig 	    assert_type(&(var), bool *)}
     95  1.31    rillig #define int_option(name, var) \
     96  1.31    rillig 	{name, PRO_INT, 0, assert_type(&(var), int *)}
     97  1.25    rillig #define special_option(name, value) \
     98  1.25    rillig 	{name, PRO_SPECIAL, assert_type(value, int), NULL}
     99  1.25    rillig 
    100   1.1       cgd /*
    101   1.1       cgd  * N.B.: because of the way the table here is scanned, options whose names are
    102  1.29    rillig  * a prefix of other options must occur later; that is, with -lp vs -l, -lp
    103  1.29    rillig  * must be first and -l must be last.
    104   1.1       cgd  */
    105  1.29    rillig static const struct pro {
    106  1.28    rillig     const char  p_name[9];	/* name, e.g. "bl", "cli" */
    107  1.29    rillig     uint8_t     p_type;		/* type (int, bool, special) */
    108  1.14     kamil     int         p_special;	/* depends on type */
    109  1.29    rillig     void        *p_obj;		/* the associated variable (bool, int) */
    110  1.14     kamil }           pro[] = {
    111  1.25    rillig     special_option("T", KEY),
    112  1.25    rillig     special_option("U", KEY_FILE),
    113  1.25    rillig     special_option("-version", VERSION),
    114  1.25    rillig     special_option("P", IGN),
    115  1.31    rillig     bool_option("bacc", true, opt.blanklines_around_conditional_compilation),
    116  1.31    rillig     bool_option("badp", true, opt.blanklines_after_declarations_at_proctop),
    117  1.31    rillig     bool_option("bad", true, opt.blanklines_after_declarations),
    118  1.31    rillig     bool_option("bap", true, opt.blanklines_after_procs),
    119  1.31    rillig     bool_option("bbb", true, opt.blanklines_before_blockcomments),
    120  1.31    rillig     bool_option("bc", false, opt.leave_comma),
    121  1.31    rillig     bool_option("bl", false, opt.btype_2),
    122  1.31    rillig     bool_option("br", true, opt.btype_2),
    123  1.31    rillig     bool_option("bs", true, opt.blank_after_sizeof),
    124  1.31    rillig     bool_option("cdb", true, opt.comment_delimiter_on_blankline),
    125  1.31    rillig     int_option("cd", opt.decl_comment_column),
    126  1.31    rillig     bool_option("ce", true, opt.cuddle_else),
    127  1.31    rillig     int_option("ci", opt.continuation_indent),
    128  1.25    rillig     special_option("cli", CLI),
    129  1.31    rillig     bool_option("cs", true, opt.space_after_cast),
    130  1.31    rillig     int_option("c", opt.comment_column),
    131  1.31    rillig     int_option("di", opt.decl_indent),
    132  1.31    rillig     bool_option("dj", true, opt.ljust_decl),
    133  1.31    rillig     int_option("d", opt.unindent_displace),
    134  1.31    rillig     bool_option("eei", true, opt.extra_expression_indent),
    135  1.31    rillig     bool_option("ei", true, opt.else_if),
    136  1.31    rillig     bool_option("fbs", true, opt.function_brace_split),
    137  1.31    rillig     bool_option("fc1", true, opt.format_col1_comments),
    138  1.31    rillig     bool_option("fcb", true, opt.format_block_comments),
    139  1.31    rillig     bool_option("ip", true, opt.indent_parameters),
    140  1.31    rillig     int_option("i", opt.indent_size),
    141  1.31    rillig     int_option("lc", opt.block_comment_max_line_length),
    142  1.31    rillig     int_option("ldi", opt.local_decl_indent),
    143  1.31    rillig     bool_option("lpl", true, opt.lineup_to_parens_always),
    144  1.31    rillig     bool_option("lp", true, opt.lineup_to_parens),
    145  1.31    rillig     int_option("l", opt.max_line_length),
    146  1.31    rillig     bool_option("nbacc", false, opt.blanklines_around_conditional_compilation),
    147  1.31    rillig     bool_option("nbadp", false, opt.blanklines_after_declarations_at_proctop),
    148  1.31    rillig     bool_option("nbad", false, opt.blanklines_after_declarations),
    149  1.31    rillig     bool_option("nbap", false, opt.blanklines_after_procs),
    150  1.31    rillig     bool_option("nbbb", false, opt.blanklines_before_blockcomments),
    151  1.31    rillig     bool_option("nbc", true, opt.leave_comma),
    152  1.31    rillig     bool_option("nbs", false, opt.blank_after_sizeof),
    153  1.31    rillig     bool_option("ncdb", false, opt.comment_delimiter_on_blankline),
    154  1.31    rillig     bool_option("nce", false, opt.cuddle_else),
    155  1.31    rillig     bool_option("ncs", false, opt.space_after_cast),
    156  1.31    rillig     bool_option("ndj", false, opt.ljust_decl),
    157  1.31    rillig     bool_option("neei", false, opt.extra_expression_indent),
    158  1.31    rillig     bool_option("nei", false, opt.else_if),
    159  1.31    rillig     bool_option("nfbs", false, opt.function_brace_split),
    160  1.31    rillig     bool_option("nfc1", false, opt.format_col1_comments),
    161  1.31    rillig     bool_option("nfcb", false, opt.format_block_comments),
    162  1.31    rillig     bool_option("nip", false, opt.indent_parameters),
    163  1.31    rillig     bool_option("nlpl", false, opt.lineup_to_parens_always),
    164  1.31    rillig     bool_option("nlp", false, opt.lineup_to_parens),
    165  1.31    rillig     bool_option("npcs", false, opt.proc_calls_space),
    166  1.25    rillig     special_option("npro", IGN),
    167  1.31    rillig     bool_option("npsl", false, opt.procnames_start_line),
    168  1.31    rillig     bool_option("nsc", false, opt.star_comment_cont),
    169  1.31    rillig     bool_option("nsob", false, opt.swallow_optional_blanklines),
    170  1.31    rillig     bool_option("nut", false, opt.use_tabs),
    171  1.31    rillig     bool_option("nv", false, opt.verbose),
    172  1.31    rillig     bool_option("pcs", true, opt.proc_calls_space),
    173  1.31    rillig     bool_option("psl", true, opt.procnames_start_line),
    174  1.31    rillig     bool_option("sc", true, opt.star_comment_cont),
    175  1.31    rillig     bool_option("sob", true, opt.swallow_optional_blanklines),
    176  1.25    rillig     special_option("st", STDIN),
    177  1.31    rillig     bool_option("ta", true, opt.auto_typedefs),
    178  1.31    rillig     int_option("ts", opt.tabsize),
    179  1.31    rillig     bool_option("ut", true, opt.use_tabs),
    180  1.31    rillig     bool_option("v", true, opt.verbose),
    181  1.14     kamil     /* whew! */
    182  1.28    rillig     {"", 0, 0, 0}
    183   1.1       cgd };
    184  1.14     kamil 
    185   1.1       cgd /*
    186   1.1       cgd  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
    187   1.1       cgd  * given in these files.
    188   1.1       cgd  */
    189   1.5     lukem void
    190  1.14     kamil set_profile(const char *profile_name)
    191   1.1       cgd {
    192  1.14     kamil     FILE *f;
    193  1.14     kamil     char fname[PATH_MAX];
    194  1.14     kamil     static char prof[] = ".indent.pro";
    195   1.5     lukem 
    196  1.14     kamil     if (profile_name == NULL)
    197   1.8    itojun 	snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
    198  1.14     kamil     else
    199  1.14     kamil 	snprintf(fname, sizeof(fname), "%s", profile_name + 2);
    200  1.14     kamil     if ((f = fopen(option_source = fname, "r")) != NULL) {
    201  1.14     kamil 	scan_profile(f);
    202  1.14     kamil 	(void) fclose(f);
    203  1.14     kamil     }
    204  1.14     kamil     if ((f = fopen(option_source = prof, "r")) != NULL) {
    205  1.14     kamil 	scan_profile(f);
    206  1.14     kamil 	(void) fclose(f);
    207  1.14     kamil     }
    208  1.14     kamil     option_source = "Command line";
    209   1.1       cgd }
    210   1.1       cgd 
    211  1.14     kamil static void
    212   1.7       wiz scan_profile(FILE *f)
    213   1.1       cgd {
    214  1.15    rillig     int		comment_index, i;
    215  1.14     kamil     char	*p;
    216  1.14     kamil     char        buf[BUFSIZ];
    217  1.14     kamil 
    218  1.22    rillig     for (;;) {
    219  1.14     kamil 	p = buf;
    220  1.15    rillig 	comment_index = 0;
    221  1.14     kamil 	while ((i = getc(f)) != EOF) {
    222  1.25    rillig 	    if (i == '*' && comment_index == 0 && p > buf && p[-1] == '/') {
    223  1.22    rillig 		comment_index = (int)(p - buf);
    224  1.14     kamil 		*p++ = i;
    225  1.25    rillig 	    } else if (i == '/' && comment_index != 0 && p > buf && p[-1] == '*') {
    226  1.15    rillig 		p = buf + comment_index - 1;
    227  1.15    rillig 		comment_index = 0;
    228  1.14     kamil 	    } else if (isspace((unsigned char)i)) {
    229  1.25    rillig 		if (p > buf && comment_index == 0)
    230  1.14     kamil 		    break;
    231  1.14     kamil 	    } else {
    232  1.14     kamil 		*p++ = i;
    233  1.14     kamil 	    }
    234   1.1       cgd 	}
    235  1.14     kamil 	if (p != buf) {
    236  1.14     kamil 	    *p++ = 0;
    237  1.14     kamil 	    if (opt.verbose)
    238  1.14     kamil 		printf("profile: %s\n", buf);
    239  1.14     kamil 	    set_option(buf);
    240  1.19    rillig 	} else if (i == EOF)
    241  1.14     kamil 	    return;
    242  1.14     kamil     }
    243   1.1       cgd }
    244   1.1       cgd 
    245  1.14     kamil static const char *
    246  1.30    rillig skip_over(const char *s, const char *prefix)
    247   1.1       cgd {
    248  1.30    rillig     while (*prefix != '\0') {
    249  1.30    rillig 	if (*prefix++ != *s++)
    250  1.16    rillig 	    return NULL;
    251  1.14     kamil     }
    252  1.30    rillig     return s;
    253   1.1       cgd }
    254  1.14     kamil 
    255   1.5     lukem void
    256  1.29    rillig set_option(const char *arg)
    257   1.1       cgd {
    258  1.17    rillig     const struct pro *p;
    259  1.14     kamil     const char	*param_start;
    260   1.1       cgd 
    261  1.14     kamil     arg++;			/* ignore leading "-" */
    262  1.28    rillig     for (p = pro; p->p_name[0] != '\0'; p++)
    263  1.30    rillig 	if (p->p_name[0] == arg[0])
    264  1.30    rillig 	    if ((param_start = skip_over(arg, p->p_name)) != NULL)
    265  1.30    rillig 		goto found;
    266  1.14     kamil     errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
    267  1.30    rillig 
    268   1.1       cgd found:
    269  1.14     kamil     switch (p->p_type) {
    270   1.1       cgd 
    271  1.14     kamil     case PRO_SPECIAL:
    272  1.14     kamil 	switch (p->p_special) {
    273   1.1       cgd 
    274  1.14     kamil 	case IGN:
    275  1.14     kamil 	    break;
    276   1.1       cgd 
    277  1.14     kamil 	case CLI:
    278  1.30    rillig 	    if (param_start[0] == '\0')
    279  1.14     kamil 		goto need_param;
    280  1.14     kamil 	    opt.case_indent = atof(param_start);
    281  1.14     kamil 	    break;
    282  1.14     kamil 
    283  1.14     kamil 	case STDIN:
    284  1.14     kamil 	    if (input == NULL)
    285  1.14     kamil 		input = stdin;
    286  1.14     kamil 	    if (output == NULL)
    287  1.14     kamil 		output = stdout;
    288  1.14     kamil 	    break;
    289  1.14     kamil 
    290  1.14     kamil 	case KEY:
    291  1.30    rillig 	    if (param_start[0] == '\0')
    292  1.14     kamil 		goto need_param;
    293  1.14     kamil 	    add_typename(param_start);
    294  1.14     kamil 	    break;
    295  1.14     kamil 
    296  1.14     kamil 	case KEY_FILE:
    297  1.30    rillig 	    if (param_start[0] == '\0')
    298  1.14     kamil 		goto need_param;
    299  1.14     kamil 	    add_typedefs_from_file(param_start);
    300  1.14     kamil 	    break;
    301  1.14     kamil 
    302  1.14     kamil 	case VERSION:
    303  1.14     kamil 	    printf("FreeBSD indent %s\n", INDENT_VERSION);
    304  1.14     kamil 	    exit(0);
    305  1.22    rillig 	    /*NOTREACHED*/
    306   1.1       cgd 
    307   1.5     lukem 	default:
    308  1.14     kamil 	    errx(1, "set_option: internal error: p_special %d", p->p_special);
    309  1.14     kamil 	}
    310  1.14     kamil 	break;
    311  1.14     kamil 
    312  1.14     kamil     case PRO_BOOL:
    313  1.31    rillig 	*(bool *)p->p_obj = p->p_special != 0;
    314  1.14     kamil 	break;
    315  1.14     kamil 
    316  1.14     kamil     case PRO_INT:
    317  1.14     kamil 	if (!isdigit((unsigned char)*param_start)) {
    318  1.14     kamil     need_param:
    319  1.14     kamil 	    errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
    320   1.1       cgd 	}
    321  1.25    rillig 	*(int *)p->p_obj = atoi(param_start);
    322  1.14     kamil 	break;
    323  1.14     kamil 
    324  1.14     kamil     default:
    325  1.14     kamil 	errx(1, "set_option: internal error: p_type %d", p->p_type);
    326  1.14     kamil     }
    327  1.14     kamil }
    328  1.14     kamil 
    329  1.14     kamil void
    330  1.30    rillig add_typedefs_from_file(const char *fname)
    331  1.14     kamil {
    332  1.14     kamil     FILE *file;
    333  1.14     kamil     char line[BUFSIZ];
    334  1.14     kamil 
    335  1.30    rillig     if ((file = fopen(fname, "r")) == NULL) {
    336  1.30    rillig 	fprintf(stderr, "indent: cannot open file %s\n", fname);
    337  1.14     kamil 	exit(1);
    338  1.14     kamil     }
    339  1.14     kamil     while ((fgets(line, BUFSIZ, file)) != NULL) {
    340  1.14     kamil 	/* Remove trailing whitespace */
    341  1.14     kamil 	line[strcspn(line, " \t\n\r")] = '\0';
    342  1.14     kamil 	add_typename(line);
    343  1.14     kamil     }
    344  1.14     kamil     fclose(file);
    345   1.1       cgd }
    346