xref: /freebsd/usr.bin/indent/args.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
170a3049eSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1985 Sun Microsystems, Inc.
59b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
69b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
79b50d902SRodney W. Grimes  * All rights reserved.
89b50d902SRodney W. Grimes  *
99b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
109b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
119b50d902SRodney W. Grimes  * are met:
129b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
149b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
159b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
169b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
179b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
189b50d902SRodney W. Grimes  *    must display the following acknowledgement:
199b50d902SRodney W. Grimes  *	This product includes software developed by the University of
209b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
219b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
229b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
239b50d902SRodney W. Grimes  *    without specific prior written permission.
249b50d902SRodney W. Grimes  *
259b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
269b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
279b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
289b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
299b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
309b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
319b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
329b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
339b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
349b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
359b50d902SRodney W. Grimes  * SUCH DAMAGE.
369b50d902SRodney W. Grimes  */
379b50d902SRodney W. Grimes 
38e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
399b50d902SRodney W. Grimes /*
409b50d902SRodney W. Grimes  * Argument scanning and profile reading code.  Default parameters are set
419b50d902SRodney W. Grimes  * here as well.
429b50d902SRodney W. Grimes  */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <ctype.h>
45958d7c9fSPhilippe Charnier #include <err.h>
46c4c326cfSDavid Schultz #include <limits.h>
47958d7c9fSPhilippe Charnier #include <stdio.h>
489b50d902SRodney W. Grimes #include <stdlib.h>
499b50d902SRodney W. Grimes #include <string.h>
509b50d902SRodney W. Grimes #include "indent_globs.h"
517916863dSJens Schweikhardt #include "indent.h"
529b50d902SRodney W. Grimes 
53d0f86f66SPiotr Pawel Stefaniak #define INDENT_VERSION	"2.0"
54d0f86f66SPiotr Pawel Stefaniak 
559b50d902SRodney W. Grimes /* profile types */
569b50d902SRodney W. Grimes #define	PRO_SPECIAL	1	/* special case */
579b50d902SRodney W. Grimes #define	PRO_BOOL	2	/* boolean */
589b50d902SRodney W. Grimes #define	PRO_INT		3	/* integer */
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes /* profile specials for booleans */
619b50d902SRodney W. Grimes #define	ON		1	/* turn it on */
629b50d902SRodney W. Grimes #define	OFF		0	/* turn it off */
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes /* profile specials for specials */
659b50d902SRodney W. Grimes #define	IGN		1	/* ignore it */
669b50d902SRodney W. Grimes #define	CLI		2	/* case label indent (float) */
679b50d902SRodney W. Grimes #define	STDIN		3	/* use stdin */
689b50d902SRodney W. Grimes #define	KEY		4	/* type (keyword) */
699b50d902SRodney W. Grimes 
707916863dSJens Schweikhardt static void scan_profile(FILE *);
717916863dSJens Schweikhardt 
72771aff0aSPedro F. Giffuni #define	KEY_FILE		5	/* only used for args */
73d0f86f66SPiotr Pawel Stefaniak #define VERSION			6	/* only used for args */
74771aff0aSPedro F. Giffuni 
758c7e7698SDavid Malone const char *option_source = "?";
769b50d902SRodney W. Grimes 
77771aff0aSPedro F. Giffuni void add_typedefs_from_file(const char *str);
78771aff0aSPedro F. Giffuni 
799b50d902SRodney W. Grimes /*
809b50d902SRodney W. Grimes  * N.B.: because of the way the table here is scanned, options whose names are
819b50d902SRodney W. Grimes  * substrings of other options must occur later; that is, with -lp vs -l, -lp
829b50d902SRodney W. Grimes  * must be first.  Also, while (most) booleans occur more than once, the last
839b50d902SRodney W. Grimes  * default value is the one actually assigned.
849b50d902SRodney W. Grimes  */
859b50d902SRodney W. Grimes struct pro {
86d0054952SPhilippe Charnier     const char *p_name;		/* name, e.g. -bl, -cli */
879b50d902SRodney W. Grimes     int         p_type;		/* type (int, bool, special) */
889b50d902SRodney W. Grimes     int         p_default;	/* the default value (if int) */
899b50d902SRodney W. Grimes     int         p_special;	/* depends on type */
909b50d902SRodney W. Grimes     int        *p_obj;		/* the associated variable */
919b50d902SRodney W. Grimes }           pro[] = {
929b50d902SRodney W. Grimes 
937916863dSJens Schweikhardt     {"T", PRO_SPECIAL, 0, KEY, 0},
94771aff0aSPedro F. Giffuni     {"U", PRO_SPECIAL, 0, KEY_FILE, 0},
95d0f86f66SPiotr Pawel Stefaniak     {"-version", PRO_SPECIAL, 0, VERSION, 0},
96183668daSPiotr Pawel Stefaniak     {"P", PRO_SPECIAL, 0, IGN, 0},
977e53aaedSPiotr Pawel Stefaniak     {"bacc", PRO_BOOL, false, ON, &opt.blanklines_around_conditional_compilation},
987e53aaedSPiotr Pawel Stefaniak     {"badp", PRO_BOOL, false, ON, &opt.blanklines_after_declarations_at_proctop},
997e53aaedSPiotr Pawel Stefaniak     {"bad", PRO_BOOL, false, ON, &opt.blanklines_after_declarations},
1007e53aaedSPiotr Pawel Stefaniak     {"bap", PRO_BOOL, false, ON, &opt.blanklines_after_procs},
1017e53aaedSPiotr Pawel Stefaniak     {"bbb", PRO_BOOL, false, ON, &opt.blanklines_before_blockcomments},
1027e53aaedSPiotr Pawel Stefaniak     {"bc", PRO_BOOL, true, OFF, &opt.leave_comma},
1037e53aaedSPiotr Pawel Stefaniak     {"bl", PRO_BOOL, true, OFF, &opt.btype_2},
1047e53aaedSPiotr Pawel Stefaniak     {"br", PRO_BOOL, true, ON, &opt.btype_2},
1057e53aaedSPiotr Pawel Stefaniak     {"bs", PRO_BOOL, false, ON, &opt.Bill_Shannon},
1067e53aaedSPiotr Pawel Stefaniak     {"cdb", PRO_BOOL, true, ON, &opt.comment_delimiter_on_blankline},
1077e53aaedSPiotr Pawel Stefaniak     {"cd", PRO_INT, 0, 0, &opt.decl_com_ind},
1087e53aaedSPiotr Pawel Stefaniak     {"ce", PRO_BOOL, true, ON, &opt.cuddle_else},
1097e53aaedSPiotr Pawel Stefaniak     {"ci", PRO_INT, 0, 0, &opt.continuation_indent},
1107916863dSJens Schweikhardt     {"cli", PRO_SPECIAL, 0, CLI, 0},
111bb92a28bSPiotr Pawel Stefaniak     {"cs", PRO_BOOL, false, ON, &opt.space_after_cast},
1127e53aaedSPiotr Pawel Stefaniak     {"c", PRO_INT, 33, 0, &opt.com_ind},
1137e53aaedSPiotr Pawel Stefaniak     {"di", PRO_INT, 16, 0, &opt.decl_indent},
1147e53aaedSPiotr Pawel Stefaniak     {"dj", PRO_BOOL, false, ON, &opt.ljust_decl},
1157e53aaedSPiotr Pawel Stefaniak     {"d", PRO_INT, 0, 0, &opt.unindent_displace},
1167e53aaedSPiotr Pawel Stefaniak     {"eei", PRO_BOOL, false, ON, &opt.extra_expression_indent},
1177e53aaedSPiotr Pawel Stefaniak     {"ei", PRO_BOOL, true, ON, &opt.else_if},
1187e53aaedSPiotr Pawel Stefaniak     {"fbs", PRO_BOOL, true, ON, &opt.function_brace_split},
1197e53aaedSPiotr Pawel Stefaniak     {"fc1", PRO_BOOL, true, ON, &opt.format_col1_comments},
1207e53aaedSPiotr Pawel Stefaniak     {"fcb", PRO_BOOL, true, ON, &opt.format_block_comments},
1217e53aaedSPiotr Pawel Stefaniak     {"ip", PRO_BOOL, true, ON, &opt.indent_parameters},
1227e53aaedSPiotr Pawel Stefaniak     {"i", PRO_INT, 8, 0, &opt.ind_size},
1237e53aaedSPiotr Pawel Stefaniak     {"lc", PRO_INT, 0, 0, &opt.block_comment_max_col},
1247e53aaedSPiotr Pawel Stefaniak     {"ldi", PRO_INT, -1, 0, &opt.local_decl_indent},
1257e53aaedSPiotr Pawel Stefaniak     {"lpl", PRO_BOOL, false, ON, &opt.lineup_to_parens_always},
1267e53aaedSPiotr Pawel Stefaniak     {"lp", PRO_BOOL, true, ON, &opt.lineup_to_parens},
1277e53aaedSPiotr Pawel Stefaniak     {"l", PRO_INT, 78, 0, &opt.max_col},
1287e53aaedSPiotr Pawel Stefaniak     {"nbacc", PRO_BOOL, false, OFF, &opt.blanklines_around_conditional_compilation},
1297e53aaedSPiotr Pawel Stefaniak     {"nbadp", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations_at_proctop},
1307e53aaedSPiotr Pawel Stefaniak     {"nbad", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations},
1317e53aaedSPiotr Pawel Stefaniak     {"nbap", PRO_BOOL, false, OFF, &opt.blanklines_after_procs},
1327e53aaedSPiotr Pawel Stefaniak     {"nbbb", PRO_BOOL, false, OFF, &opt.blanklines_before_blockcomments},
1337e53aaedSPiotr Pawel Stefaniak     {"nbc", PRO_BOOL, true, ON, &opt.leave_comma},
1347e53aaedSPiotr Pawel Stefaniak     {"nbs", PRO_BOOL, false, OFF, &opt.Bill_Shannon},
1357e53aaedSPiotr Pawel Stefaniak     {"ncdb", PRO_BOOL, true, OFF, &opt.comment_delimiter_on_blankline},
1367e53aaedSPiotr Pawel Stefaniak     {"nce", PRO_BOOL, true, OFF, &opt.cuddle_else},
137bb92a28bSPiotr Pawel Stefaniak     {"ncs", PRO_BOOL, false, OFF, &opt.space_after_cast},
1387e53aaedSPiotr Pawel Stefaniak     {"ndj", PRO_BOOL, false, OFF, &opt.ljust_decl},
1397e53aaedSPiotr Pawel Stefaniak     {"neei", PRO_BOOL, false, OFF, &opt.extra_expression_indent},
1407e53aaedSPiotr Pawel Stefaniak     {"nei", PRO_BOOL, true, OFF, &opt.else_if},
1417e53aaedSPiotr Pawel Stefaniak     {"nfbs", PRO_BOOL, true, OFF, &opt.function_brace_split},
1427e53aaedSPiotr Pawel Stefaniak     {"nfc1", PRO_BOOL, true, OFF, &opt.format_col1_comments},
1437e53aaedSPiotr Pawel Stefaniak     {"nfcb", PRO_BOOL, true, OFF, &opt.format_block_comments},
1447e53aaedSPiotr Pawel Stefaniak     {"nip", PRO_BOOL, true, OFF, &opt.indent_parameters},
1457e53aaedSPiotr Pawel Stefaniak     {"nlpl", PRO_BOOL, false, OFF, &opt.lineup_to_parens_always},
1467e53aaedSPiotr Pawel Stefaniak     {"nlp", PRO_BOOL, true, OFF, &opt.lineup_to_parens},
1477e53aaedSPiotr Pawel Stefaniak     {"npcs", PRO_BOOL, false, OFF, &opt.proc_calls_space},
1487916863dSJens Schweikhardt     {"npro", PRO_SPECIAL, 0, IGN, 0},
1497e53aaedSPiotr Pawel Stefaniak     {"npsl", PRO_BOOL, true, OFF, &opt.procnames_start_line},
150*b5b9eaa9SDag-Erling Smørgrav     {"nps", PRO_BOOL, false, OFF, &opt.pointer_as_binop},
1517e53aaedSPiotr Pawel Stefaniak     {"nsc", PRO_BOOL, true, OFF, &opt.star_comment_cont},
1527e53aaedSPiotr Pawel Stefaniak     {"nsob", PRO_BOOL, false, OFF, &opt.swallow_optional_blanklines},
1537e53aaedSPiotr Pawel Stefaniak     {"nut", PRO_BOOL, true, OFF, &opt.use_tabs},
1547e53aaedSPiotr Pawel Stefaniak     {"nv", PRO_BOOL, false, OFF, &opt.verbose},
1557e53aaedSPiotr Pawel Stefaniak     {"pcs", PRO_BOOL, false, ON, &opt.proc_calls_space},
1567e53aaedSPiotr Pawel Stefaniak     {"psl", PRO_BOOL, true, ON, &opt.procnames_start_line},
157*b5b9eaa9SDag-Erling Smørgrav     {"ps", PRO_BOOL, false, ON, &opt.pointer_as_binop},
1587e53aaedSPiotr Pawel Stefaniak     {"sc", PRO_BOOL, true, ON, &opt.star_comment_cont},
1597e53aaedSPiotr Pawel Stefaniak     {"sob", PRO_BOOL, false, ON, &opt.swallow_optional_blanklines},
1607916863dSJens Schweikhardt     {"st", PRO_SPECIAL, 0, STDIN, 0},
1617e53aaedSPiotr Pawel Stefaniak     {"ta", PRO_BOOL, false, ON, &opt.auto_typedefs},
1627e53aaedSPiotr Pawel Stefaniak     {"ts", PRO_INT, 8, 0, &opt.tabsize},
1637e53aaedSPiotr Pawel Stefaniak     {"ut", PRO_BOOL, true, ON, &opt.use_tabs},
1647e53aaedSPiotr Pawel Stefaniak     {"v", PRO_BOOL, false, ON, &opt.verbose},
1659b50d902SRodney W. Grimes     /* whew! */
1667916863dSJens Schweikhardt     {0, 0, 0, 0, 0}
1679b50d902SRodney W. Grimes };
1689b50d902SRodney W. Grimes 
1699b50d902SRodney W. Grimes /*
1709b50d902SRodney W. Grimes  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
1719b50d902SRodney W. Grimes  * given in these files.
1729b50d902SRodney W. Grimes  */
1737916863dSJens Schweikhardt void
set_profile(const char * profile_name)17486adac04SPiotr Pawel Stefaniak set_profile(const char *profile_name)
1759b50d902SRodney W. Grimes {
17690af6a72SJuli Mallett     FILE *f;
177c4c326cfSDavid Schultz     char fname[PATH_MAX];
1789b50d902SRodney W. Grimes     static char prof[] = ".indent.pro";
1799b50d902SRodney W. Grimes 
18086adac04SPiotr Pawel Stefaniak     if (profile_name == NULL)
181c4c326cfSDavid Schultz 	snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
18286adac04SPiotr Pawel Stefaniak     else
18386adac04SPiotr Pawel Stefaniak 	snprintf(fname, sizeof(fname), "%s", profile_name + 2);
1849b50d902SRodney W. Grimes     if ((f = fopen(option_source = fname, "r")) != NULL) {
1859b50d902SRodney W. Grimes 	scan_profile(f);
1869b50d902SRodney W. Grimes 	(void) fclose(f);
1879b50d902SRodney W. Grimes     }
1889b50d902SRodney W. Grimes     if ((f = fopen(option_source = prof, "r")) != NULL) {
1899b50d902SRodney W. Grimes 	scan_profile(f);
1909b50d902SRodney W. Grimes 	(void) fclose(f);
1919b50d902SRodney W. Grimes     }
1929b50d902SRodney W. Grimes     option_source = "Command line";
1939b50d902SRodney W. Grimes }
1949b50d902SRodney W. Grimes 
1957916863dSJens Schweikhardt static void
scan_profile(FILE * f)19690af6a72SJuli Mallett scan_profile(FILE *f)
1979b50d902SRodney W. Grimes {
198efc4d24dSDag-Erling Smørgrav     int		comment, i;
19990af6a72SJuli Mallett     char	*p;
2009b50d902SRodney W. Grimes     char        buf[BUFSIZ];
2019b50d902SRodney W. Grimes 
2029b50d902SRodney W. Grimes     while (1) {
203efc4d24dSDag-Erling Smørgrav 	p = buf;
204efc4d24dSDag-Erling Smørgrav 	comment = 0;
205efc4d24dSDag-Erling Smørgrav 	while ((i = getc(f)) != EOF) {
206efc4d24dSDag-Erling Smørgrav 	    if (i == '*' && !comment && p > buf && p[-1] == '/') {
207efc4d24dSDag-Erling Smørgrav 		comment = p - buf;
208efc4d24dSDag-Erling Smørgrav 		*p++ = i;
209efc4d24dSDag-Erling Smørgrav 	    } else if (i == '/' && comment && p > buf && p[-1] == '*') {
210efc4d24dSDag-Erling Smørgrav 		p = buf + comment - 1;
211efc4d24dSDag-Erling Smørgrav 		comment = 0;
212e1baf57eSPiotr Pawel Stefaniak 	    } else if (isspace((unsigned char)i)) {
213efc4d24dSDag-Erling Smørgrav 		if (p > buf && !comment)
214efc4d24dSDag-Erling Smørgrav 		    break;
215efc4d24dSDag-Erling Smørgrav 	    } else {
216efc4d24dSDag-Erling Smørgrav 		*p++ = i;
217efc4d24dSDag-Erling Smørgrav 	    }
218efc4d24dSDag-Erling Smørgrav 	}
2199b50d902SRodney W. Grimes 	if (p != buf) {
2209b50d902SRodney W. Grimes 	    *p++ = 0;
2217e53aaedSPiotr Pawel Stefaniak 	    if (opt.verbose)
2229b50d902SRodney W. Grimes 		printf("profile: %s\n", buf);
2239b50d902SRodney W. Grimes 	    set_option(buf);
2249b50d902SRodney W. Grimes 	}
2259b50d902SRodney W. Grimes 	else if (i == EOF)
2269b50d902SRodney W. Grimes 	    return;
2279b50d902SRodney W. Grimes     }
2289b50d902SRodney W. Grimes }
2299b50d902SRodney W. Grimes 
230e725fe4bSPedro F. Giffuni static const char *
eqin(const char * s1,const char * s2)2318c7e7698SDavid Malone eqin(const char *s1, const char *s2)
2329b50d902SRodney W. Grimes {
2339b50d902SRodney W. Grimes     while (*s1) {
2349b50d902SRodney W. Grimes 	if (*s1++ != *s2++)
235e725fe4bSPedro F. Giffuni 	    return (NULL);
2369b50d902SRodney W. Grimes     }
237e725fe4bSPedro F. Giffuni     return (s2);
2389b50d902SRodney W. Grimes }
2399b50d902SRodney W. Grimes 
2409b50d902SRodney W. Grimes /*
2419b50d902SRodney W. Grimes  * Set the defaults.
2429b50d902SRodney W. Grimes  */
2437916863dSJens Schweikhardt void
set_defaults(void)2447916863dSJens Schweikhardt set_defaults(void)
2459b50d902SRodney W. Grimes {
24690af6a72SJuli Mallett     struct pro *p;
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes     /*
2499b50d902SRodney W. Grimes      * Because ps.case_indent is a float, we can't initialize it from the
2509b50d902SRodney W. Grimes      * table:
2519b50d902SRodney W. Grimes      */
25289463812SPiotr Pawel Stefaniak     opt.case_indent = 0.0;	/* -cli0.0 */
2539b50d902SRodney W. Grimes     for (p = pro; p->p_name; p++)
2541479f36dSPiotr Pawel Stefaniak 	if (p->p_type != PRO_SPECIAL)
2559b50d902SRodney W. Grimes 	    *p->p_obj = p->p_default;
2569b50d902SRodney W. Grimes }
2579b50d902SRodney W. Grimes 
2587916863dSJens Schweikhardt void
set_option(char * arg)2597916863dSJens Schweikhardt set_option(char *arg)
2609b50d902SRodney W. Grimes {
26190af6a72SJuli Mallett     struct	pro *p;
262e725fe4bSPedro F. Giffuni     const char	*param_start;
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes     arg++;			/* ignore leading "-" */
2659b50d902SRodney W. Grimes     for (p = pro; p->p_name; p++)
266e725fe4bSPedro F. Giffuni 	if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL)
2679b50d902SRodney W. Grimes 	    goto found;
268958d7c9fSPhilippe Charnier     errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
2699b50d902SRodney W. Grimes found:
2709b50d902SRodney W. Grimes     switch (p->p_type) {
2719b50d902SRodney W. Grimes 
2729b50d902SRodney W. Grimes     case PRO_SPECIAL:
2739b50d902SRodney W. Grimes 	switch (p->p_special) {
2749b50d902SRodney W. Grimes 
2759b50d902SRodney W. Grimes 	case IGN:
2769b50d902SRodney W. Grimes 	    break;
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes 	case CLI:
2799b50d902SRodney W. Grimes 	    if (*param_start == 0)
2809b50d902SRodney W. Grimes 		goto need_param;
28189463812SPiotr Pawel Stefaniak 	    opt.case_indent = atof(param_start);
2829b50d902SRodney W. Grimes 	    break;
2839b50d902SRodney W. Grimes 
2849b50d902SRodney W. Grimes 	case STDIN:
285c917a54bSPedro F. Giffuni 	    if (input == NULL)
2869b50d902SRodney W. Grimes 		input = stdin;
287c917a54bSPedro F. Giffuni 	    if (output == NULL)
2889b50d902SRodney W. Grimes 		output = stdout;
2899b50d902SRodney W. Grimes 	    break;
2909b50d902SRodney W. Grimes 
2919b50d902SRodney W. Grimes 	case KEY:
2929b50d902SRodney W. Grimes 	    if (*param_start == 0)
2939b50d902SRodney W. Grimes 		goto need_param;
294f3c23ec3SPedro F. Giffuni 	    add_typename(param_start);
2959b50d902SRodney W. Grimes 	    break;
2969b50d902SRodney W. Grimes 
297771aff0aSPedro F. Giffuni 	case KEY_FILE:
298771aff0aSPedro F. Giffuni 	    if (*param_start == 0)
299771aff0aSPedro F. Giffuni 		goto need_param;
300771aff0aSPedro F. Giffuni 	    add_typedefs_from_file(param_start);
301771aff0aSPedro F. Giffuni 	    break;
302771aff0aSPedro F. Giffuni 
303d0f86f66SPiotr Pawel Stefaniak 	case VERSION:
304d0f86f66SPiotr Pawel Stefaniak 	    printf("FreeBSD indent %s\n", INDENT_VERSION);
305d0f86f66SPiotr Pawel Stefaniak 	    exit(0);
306d0f86f66SPiotr Pawel Stefaniak 
3079b50d902SRodney W. Grimes 	default:
308958d7c9fSPhilippe Charnier 	    errx(1, "set_option: internal error: p_special %d", p->p_special);
3099b50d902SRodney W. Grimes 	}
3109b50d902SRodney W. Grimes 	break;
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes     case PRO_BOOL:
3139b50d902SRodney W. Grimes 	if (p->p_special == OFF)
3149b50d902SRodney W. Grimes 	    *p->p_obj = false;
3159b50d902SRodney W. Grimes 	else
3169b50d902SRodney W. Grimes 	    *p->p_obj = true;
3179b50d902SRodney W. Grimes 	break;
3189b50d902SRodney W. Grimes 
3199b50d902SRodney W. Grimes     case PRO_INT:
320e1baf57eSPiotr Pawel Stefaniak 	if (!isdigit((unsigned char)*param_start)) {
3219b50d902SRodney W. Grimes     need_param:
3220de58b3fSPiotr Pawel Stefaniak 	    errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
3239b50d902SRodney W. Grimes 	}
3249b50d902SRodney W. Grimes 	*p->p_obj = atoi(param_start);
3259b50d902SRodney W. Grimes 	break;
3269b50d902SRodney W. Grimes 
3279b50d902SRodney W. Grimes     default:
328958d7c9fSPhilippe Charnier 	errx(1, "set_option: internal error: p_type %d", p->p_type);
3299b50d902SRodney W. Grimes     }
3309b50d902SRodney W. Grimes }
331771aff0aSPedro F. Giffuni 
332771aff0aSPedro F. Giffuni void
add_typedefs_from_file(const char * str)333771aff0aSPedro F. Giffuni add_typedefs_from_file(const char *str)
334771aff0aSPedro F. Giffuni {
335771aff0aSPedro F. Giffuni     FILE *file;
336771aff0aSPedro F. Giffuni     char line[BUFSIZ];
337771aff0aSPedro F. Giffuni 
338771aff0aSPedro F. Giffuni     if ((file = fopen(str, "r")) == NULL) {
339771aff0aSPedro F. Giffuni 	fprintf(stderr, "indent: cannot open file %s\n", str);
340771aff0aSPedro F. Giffuni 	exit(1);
341771aff0aSPedro F. Giffuni     }
342771aff0aSPedro F. Giffuni     while ((fgets(line, BUFSIZ, file)) != NULL) {
343771aff0aSPedro F. Giffuni 	/* Remove trailing whitespace */
344a6bcfda4SPedro F. Giffuni 	line[strcspn(line, " \t\n\r")] = '\0';
345f3c23ec3SPedro F. Giffuni 	add_typename(line);
346771aff0aSPedro F. Giffuni     }
347771aff0aSPedro F. Giffuni     fclose(file);
348771aff0aSPedro F. Giffuni }
349