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