Home | History | Annotate | Line # | Download | only in indent
args.c revision 1.58
      1 /*	$NetBSD: args.c,v 1.58 2021/10/24 11:19:25 rillig Exp $	*/
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-4-Clause
      5  *
      6  * Copyright (c) 1985 Sun Microsystems, Inc.
      7  * Copyright (c) 1980, 1993
      8  *	The Regents of the University of California.  All rights reserved.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #if 0
     41 static char sccsid[] = "@(#)args.c	8.1 (Berkeley) 6/6/93";
     42 #endif
     43 
     44 #include <sys/cdefs.h>
     45 #if defined(__NetBSD__)
     46 __RCSID("$NetBSD: args.c,v 1.58 2021/10/24 11:19:25 rillig Exp $");
     47 #elif defined(__FreeBSD__)
     48 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
     49 #endif
     50 
     51 /* Read options from profile files and from the command line. */
     52 
     53 #include <ctype.h>
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <limits.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 
     61 #include "indent.h"
     62 
     63 #define INDENT_VERSION	"2.0"
     64 
     65 #if __STDC_VERSION__ >= 201112L
     66 #define assert_type(expr, type) _Generic((expr), type : (expr))
     67 #else
     68 #define assert_type(expr, type) (expr)
     69 #endif
     70 
     71 #define bool_option(name, value, var) \
     72 	{name, true, value, false, 0, 0, assert_type(&(opt.var), bool *)}
     73 #define bool_options(name, var) \
     74 	{name, true, false, true, 0, 0, assert_type(&(opt.var), bool *)}
     75 #define int_option(name, var, min, max) \
     76 	{name, false, false, false, min, max, assert_type(&(opt.var), int *)}
     77 
     78 /*
     79  * N.B.: an option whose name is a prefix of another option must come earlier;
     80  * for example, "l" must come before "lp".
     81  *
     82  * See set_special_option for special options.
     83  */
     84 static const struct pro {
     85     const char p_name[5];	/* e.g. "bl", "cli" */
     86     bool p_is_bool;
     87     bool p_bool_value;
     88     bool p_may_negate;
     89     short i_min;
     90     short i_max;
     91     void *p_var;		/* the associated variable */
     92 }   pro[] = {
     93     bool_options("bacc", blanklines_around_conditional_compilation),
     94     bool_options("bad", blanklines_after_decl),
     95     bool_options("badp", blanklines_after_decl_at_top),
     96     bool_options("bap", blanklines_after_procs),
     97     bool_options("bbb", blanklines_before_block_comments),
     98     bool_options("bc", break_after_comma),
     99     bool_option("bl", false, brace_same_line),
    100     bool_option("br", true, brace_same_line),
    101     bool_options("bs", blank_after_sizeof),
    102     int_option("c", comment_column, 1, 999),
    103     int_option("cd", decl_comment_column, 1, 999),
    104     bool_options("cdb", comment_delimiter_on_blankline),
    105     bool_options("ce", cuddle_else),
    106     int_option("ci", continuation_indent, 0, 999),
    107     /* "cli" is special */
    108     bool_options("cs", space_after_cast),
    109     int_option("d", unindent_displace, -999, 999),
    110     int_option("di", decl_indent, 0, 999),
    111     bool_options("dj", ljust_decl),
    112     bool_options("eei", extra_expr_indent),
    113     bool_options("ei", else_if),
    114     bool_options("fbs", function_brace_split),
    115     bool_options("fc1", format_col1_comments),
    116     bool_options("fcb", format_block_comments),
    117     int_option("i", indent_size, 1, 80),
    118     bool_options("ip", indent_parameters),
    119     int_option("l", max_line_length, 1, 999),
    120     int_option("lc", block_comment_max_line_length, 1, 999),
    121     int_option("ldi", local_decl_indent, 0, 999),
    122     bool_options("lp", lineup_to_parens),
    123     bool_options("lpl", lineup_to_parens_always),
    124     /* "npro" is special */
    125     /* "P" is special */
    126     bool_options("pcs", proc_calls_space),
    127     bool_options("psl", procnames_start_line),
    128     bool_options("sc", star_comment_cont),
    129     bool_options("sob", swallow_optional_blanklines),
    130     /* "st" is special */
    131     bool_option("ta", true, auto_typedefs),
    132     /* "T" is special */
    133     int_option("ts", tabsize, 1, 80),
    134     /* "U" is special */
    135     bool_options("ut", use_tabs),
    136     bool_options("v", verbose),
    137 };
    138 
    139 static void
    140 load_profile(const char *fname, bool must_exist)
    141 {
    142     FILE *f;
    143 
    144     if ((f = fopen(fname, "r")) == NULL) {
    145 	if (must_exist)
    146 	    err(EXIT_FAILURE, "profile %s", fname);
    147 	return;
    148     }
    149 
    150     for (;;) {
    151 	char buf[BUFSIZ];
    152 	size_t n = 0;
    153 	int ch, comment_ch = -1;
    154 
    155 	while ((ch = getc(f)) != EOF) {
    156 	    if (ch == '*' && comment_ch < 0 && n > 0 && buf[n - 1] == '/') {
    157 		n--;
    158 		comment_ch = ch;
    159 	    } else if (comment_ch >= 0) {
    160 		comment_ch = ch == '/' && comment_ch == '*' ? -1 : ch;
    161 	    } else if (isspace((unsigned char)ch)) {
    162 		break;
    163 	    } else if (n >= array_length(buf) - 5) {
    164 		diag(1, "buffer overflow in %s, starting with '%.10s'",
    165 		     fname, buf);
    166 		exit(1);
    167 	    } else
    168 		buf[n++] = (char)ch;
    169 	}
    170 
    171 	if (n > 0) {
    172 	    buf[n] = '\0';
    173 	    if (opt.verbose)
    174 		printf("profile: %s\n", buf);
    175 	    set_option(buf, fname);
    176 	} else if (ch == EOF)
    177 	    break;
    178     }
    179     (void)fclose(f);
    180 }
    181 
    182 void
    183 load_profiles(const char *profile_name)
    184 {
    185     char fname[PATH_MAX];
    186 
    187     if (profile_name != NULL)
    188 	load_profile(profile_name, true);
    189     else {
    190 	snprintf(fname, sizeof(fname), "%s/.indent.pro", getenv("HOME"));
    191 	load_profile(fname, false);
    192     }
    193     load_profile(".indent.pro", false);
    194 }
    195 
    196 static const char *
    197 skip_over(const char *s, bool may_negate, const char *prefix)
    198 {
    199     if (may_negate && s[0] == 'n')
    200 	s++;
    201     while (*prefix != '\0') {
    202 	if (*prefix++ != *s++)
    203 	    return NULL;
    204     }
    205     return s;
    206 }
    207 
    208 static void
    209 add_typedefs_from_file(const char *fname)
    210 {
    211     FILE *file;
    212     char line[BUFSIZ];
    213 
    214     if ((file = fopen(fname, "r")) == NULL) {
    215 	fprintf(stderr, "indent: cannot open file %s\n", fname);
    216 	exit(1);
    217     }
    218     while ((fgets(line, BUFSIZ, file)) != NULL) {
    219 	/* Remove trailing whitespace */
    220 	line[strcspn(line, " \t\n\r")] = '\0';
    221 	add_typename(line);
    222     }
    223     (void)fclose(file);
    224 }
    225 
    226 static bool
    227 set_special_option(const char *arg, const char *option_source)
    228 {
    229     const char *arg_end;
    230 
    231     if (strncmp(arg, "-version", 8) == 0) {
    232 	printf("FreeBSD indent %s\n", INDENT_VERSION);
    233 	exit(0);
    234     }
    235 
    236     if (arg[0] == 'P' || strncmp(arg, "npro", 4) == 0)
    237 	return true;
    238 
    239     if (strncmp(arg, "cli", 3) == 0) {
    240 	arg_end = arg + 3;
    241 	if (arg_end[0] == '\0')
    242 	    goto need_param;
    243 	opt.case_indent = (float)atof(arg_end);
    244 	return true;
    245     }
    246 
    247     if (strncmp(arg, "st", 2) == 0) {
    248 	if (input == NULL)
    249 	    input = stdin;
    250 	if (output == NULL)
    251 	    output = stdout;
    252 	return true;
    253     }
    254 
    255     if (arg[0] == 'T') {
    256 	arg_end = arg + 1;
    257 	if (arg_end[0] == '\0')
    258 	    goto need_param;
    259 	add_typename(arg_end);
    260 	return true;
    261     }
    262 
    263     if (arg[0] == 'U') {
    264 	arg_end = arg + 1;
    265 	if (arg_end[0] == '\0')
    266 	    goto need_param;
    267 	add_typedefs_from_file(arg_end);
    268 	return true;
    269     }
    270 
    271     return false;
    272 
    273 need_param:
    274     errx(1, "%s: ``%.*s'' requires a parameter",
    275 	option_source, (int)(arg_end - arg), arg);
    276     /* NOTREACHED */
    277 }
    278 
    279 void
    280 set_option(const char *arg, const char *option_source)
    281 {
    282     const struct pro *p;
    283     const char *param_start;
    284 
    285     arg++;			/* skip leading '-' */
    286     if (set_special_option(arg, option_source))
    287 	return;
    288 
    289     for (p = pro + array_length(pro); p-- != pro;) {
    290 	param_start = skip_over(arg, p->p_may_negate, p->p_name);
    291 	if (param_start != NULL)
    292 	    goto found;
    293     }
    294     errx(1, "%s: unknown option \"-%s\"", option_source, arg);
    295 
    296 found:
    297     if (p->p_is_bool) {
    298 	if (param_start[0] != '\0')
    299 	    errx(1, "%s: unknown option \"-%s\"", option_source, arg);
    300 	*(bool *)p->p_var = p->p_may_negate ? arg[0] != 'n' : p->p_bool_value;
    301     } else {
    302 	if (!isdigit((unsigned char)*param_start))
    303 	    errx(1, "%s: option \"-%s\" requires an integer parameter",
    304 		option_source, p->p_name);
    305 	errno = 0;
    306 	char *end;
    307 	long num = strtol(param_start, &end, 10);
    308 	if (!(errno == 0 && *end == '\0' &&
    309 		p->i_min <= num && num <= p->i_max))
    310 	    errx(1, "%s: invalid argument \"%s\" for option \"-%s\"",
    311 		 option_source, param_start, p->p_name);
    312 	*(int *)p->p_var = (int)num;
    313     }
    314 }
    315