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