Home | History | Annotate | Line # | Download | only in indent
args.c revision 1.55
      1 /*	$NetBSD: args.c,v 1.55 2021/10/13 23:33:52 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.55 2021/10/13 23:33:52 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 <limits.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 
     60 #include "indent.h"
     61 
     62 #define INDENT_VERSION	"2.0"
     63 
     64 #if __STDC_VERSION__ >= 201112L
     65 #define assert_type(expr, type) _Generic((expr), type : (expr))
     66 #else
     67 #define assert_type(expr, type) (expr)
     68 #endif
     69 
     70 #define bool_option(name, value, var) \
     71 	{name, true, value, false, assert_type(&(opt.var), bool *)}
     72 #define bool_options(name, var) \
     73 	{name, true, false, true, assert_type(&(opt.var), bool *)}
     74 #define int_option(name, var) \
     75 	{name, false, false, false, assert_type(&(opt.var), int *)}
     76 
     77 /*
     78  * N.B.: an option whose name is a prefix of another option must come earlier;
     79  * for example, "l" must come before "lp".
     80  *
     81  * See set_special_option for special options.
     82  */
     83 static const struct pro {
     84     const char p_name[5];	/* e.g. "bl", "cli" */
     85     bool p_is_bool;
     86     bool p_bool_value;
     87     bool p_may_negate;
     88     void *p_var;		/* the associated variable */
     89 }   pro[] = {
     90     bool_options("bacc", blanklines_around_conditional_compilation),
     91     bool_options("bad", blanklines_after_decl),
     92     bool_options("badp", blanklines_after_decl_at_top),
     93     bool_options("bap", blanklines_after_procs),
     94     bool_options("bbb", blanklines_before_block_comments),
     95     bool_options("bc", break_after_comma),
     96     bool_option("bl", false, brace_same_line),
     97     bool_option("br", true, brace_same_line),
     98     bool_options("bs", blank_after_sizeof),
     99     int_option("c", comment_column),
    100     int_option("cd", decl_comment_column),
    101     bool_options("cdb", comment_delimiter_on_blankline),
    102     bool_options("ce", cuddle_else),
    103     int_option("ci", continuation_indent),
    104     /* "cli" is special */
    105     bool_options("cs", space_after_cast),
    106     int_option("d", unindent_displace),
    107     int_option("di", decl_indent),
    108     bool_options("dj", ljust_decl),
    109     bool_options("eei", extra_expr_indent),
    110     bool_options("ei", else_if),
    111     bool_options("fbs", function_brace_split),
    112     bool_options("fc1", format_col1_comments),
    113     bool_options("fcb", format_block_comments),
    114     int_option("i", indent_size),
    115     bool_options("ip", indent_parameters),
    116     int_option("l", max_line_length),
    117     int_option("lc", block_comment_max_line_length),
    118     int_option("ldi", local_decl_indent),
    119     bool_options("lp", lineup_to_parens),
    120     bool_options("lpl", lineup_to_parens_always),
    121     /* "npro" is special */
    122     /* "P" is special */
    123     bool_options("pcs", proc_calls_space),
    124     bool_options("psl", procnames_start_line),
    125     bool_options("sc", star_comment_cont),
    126     bool_options("sob", swallow_optional_blanklines),
    127     /* "st" is special */
    128     bool_option("ta", true, auto_typedefs),
    129     /* "T" is special */
    130     int_option("ts", tabsize),
    131     /* "U" is special */
    132     bool_options("ut", use_tabs),
    133     bool_options("v", verbose),
    134 };
    135 
    136 static void
    137 load_profile(const char *fname, bool must_exist)
    138 {
    139     FILE *f;
    140 
    141     if ((f = fopen(fname, "r")) == NULL) {
    142 	if (must_exist)
    143 	    err(EXIT_FAILURE, "profile %s", fname);
    144 	return;
    145     }
    146 
    147     for (;;) {
    148 	char buf[BUFSIZ];
    149 	size_t n = 0;
    150 	int ch, comment_ch = -1;
    151 
    152 	while ((ch = getc(f)) != EOF) {
    153 	    if (ch == '*' && comment_ch < 0 && n > 0 && buf[n - 1] == '/') {
    154 		n--;
    155 		comment_ch = ch;
    156 	    } else if (comment_ch >= 0) {
    157 		comment_ch = ch == '/' && comment_ch == '*' ? -1 : ch;
    158 	    } else if (isspace((unsigned char)ch)) {
    159 		break;
    160 	    } else if (n >= nitems(buf) - 5) {
    161 		diag(1, "buffer overflow in %s, starting with '%.10s'",
    162 		     fname, buf);
    163 		exit(1);
    164 	    } else
    165 		buf[n++] = (char)ch;
    166 	}
    167 
    168 	if (n > 0) {
    169 	    buf[n] = '\0';
    170 	    if (opt.verbose)
    171 		printf("profile: %s\n", buf);
    172 	    set_option(buf, fname);
    173 	} else if (ch == EOF)
    174 	    break;
    175     }
    176     (void)fclose(f);
    177 }
    178 
    179 void
    180 load_profiles(const char *profile_name)
    181 {
    182     char fname[PATH_MAX];
    183 
    184     if (profile_name != NULL)
    185 	load_profile(profile_name, true);
    186     else {
    187 	snprintf(fname, sizeof(fname), "%s/.indent.pro", getenv("HOME"));
    188 	load_profile(fname, false);
    189     }
    190     load_profile(".indent.pro", false);
    191 }
    192 
    193 static const char *
    194 skip_over(const char *s, bool may_negate, const char *prefix)
    195 {
    196     if (may_negate && s[0] == 'n')
    197 	s++;
    198     while (*prefix != '\0') {
    199 	if (*prefix++ != *s++)
    200 	    return NULL;
    201     }
    202     return s;
    203 }
    204 
    205 static void
    206 add_typedefs_from_file(const char *fname)
    207 {
    208     FILE *file;
    209     char line[BUFSIZ];
    210 
    211     if ((file = fopen(fname, "r")) == NULL) {
    212 	fprintf(stderr, "indent: cannot open file %s\n", fname);
    213 	exit(1);
    214     }
    215     while ((fgets(line, BUFSIZ, file)) != NULL) {
    216 	/* Remove trailing whitespace */
    217 	line[strcspn(line, " \t\n\r")] = '\0';
    218 	add_typename(line);
    219     }
    220     (void)fclose(file);
    221 }
    222 
    223 static bool
    224 set_special_option(const char *arg, const char *option_source)
    225 {
    226     const char *arg_end;
    227 
    228     if (strncmp(arg, "-version", 8) == 0) {
    229 	printf("FreeBSD indent %s\n", INDENT_VERSION);
    230 	exit(0);
    231     }
    232 
    233     if (arg[0] == 'P' || strncmp(arg, "npro", 4) == 0)
    234 	return true;
    235 
    236     if (strncmp(arg, "cli", 3) == 0) {
    237 	arg_end = arg + 3;
    238 	if (arg_end[0] == '\0')
    239 	    goto need_param;
    240 	opt.case_indent = (float)atof(arg_end);
    241 	return true;
    242     }
    243 
    244     if (strncmp(arg, "st", 2) == 0) {
    245 	if (input == NULL)
    246 	    input = stdin;
    247 	if (output == NULL)
    248 	    output = stdout;
    249 	return true;
    250     }
    251 
    252     if (arg[0] == 'T') {
    253 	arg_end = arg + 1;
    254 	if (arg_end[0] == '\0')
    255 	    goto need_param;
    256 	add_typename(arg_end);
    257 	return true;
    258     }
    259 
    260     if (arg[0] == 'U') {
    261 	arg_end = arg + 1;
    262 	if (arg_end[0] == '\0')
    263 	    goto need_param;
    264 	add_typedefs_from_file(arg_end);
    265 	return true;
    266     }
    267 
    268     return false;
    269 
    270 need_param:
    271     errx(1, "%s: ``%.*s'' requires a parameter",
    272 	option_source, (int)(arg_end - arg), arg);
    273     /* NOTREACHED */
    274 }
    275 
    276 void
    277 set_option(const char *arg, const char *option_source)
    278 {
    279     const struct pro *p;
    280     const char *param_start;
    281 
    282     arg++;			/* skip leading '-' */
    283     if (set_special_option(arg, option_source))
    284 	return;
    285 
    286     for (p = pro + nitems(pro); p-- != pro;) {
    287 	param_start = skip_over(arg, p->p_may_negate, p->p_name);
    288 	if (param_start != NULL)
    289 	    goto found;
    290     }
    291     errx(1, "%s: unknown option \"-%s\"", option_source, arg);
    292 
    293 found:
    294     if (p->p_is_bool) {
    295 	if (param_start[0] != '\0')
    296 	    errx(1, "%s: unknown option \"-%s\"", option_source, arg);
    297 	*(bool *)p->p_var = p->p_may_negate ? arg[0] != 'n' : p->p_bool_value;
    298     } else {
    299 	if (!isdigit((unsigned char)*param_start))
    300 	    errx(1, "%s: option \"-%s\" requires an integer parameter",
    301 		option_source, p->p_name);
    302 	*(int *)p->p_var = atoi(param_start);
    303     }
    304 }
    305