dhcpcd: Note update to 9.4.1
[dragonfly.git] / usr.bin / indent / args.c
blob2c933069955ac880701bae4cc9d98cd1985cd961
1 /*-
2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1985 Sun Microsystems, Inc.
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)args.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $
38 * Argument scanning and profile reading code. Default parameters are set
39 * here as well.
42 #include <ctype.h>
43 #include <err.h>
44 #include <limits.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include "indent_globs.h"
49 #include "indent.h"
51 #define INDENT_VERSION "2.0"
53 /* profile types */
54 #define PRO_SPECIAL 1 /* special case */
55 #define PRO_BOOL 2 /* boolean */
56 #define PRO_INT 3 /* integer */
58 /* profile specials for booleans */
59 #define ON 1 /* turn it on */
60 #define OFF 0 /* turn it off */
62 /* profile specials for specials */
63 #define IGN 1 /* ignore it */
64 #define CLI 2 /* case label indent (float) */
65 #define STDIN 3 /* use stdin */
66 #define KEY 4 /* type (keyword) */
68 static void scan_profile(FILE *);
70 #define KEY_FILE 5 /* only used for args */
71 #define VERSION 6 /* only used for args */
73 const char *option_source = "?";
75 void add_typedefs_from_file(const char *str);
78 * N.B.: because of the way the table here is scanned, options whose names are
79 * substrings of other options must occur later; that is, with -lp vs -l, -lp
80 * must be first. Also, while (most) booleans occur more than once, the last
81 * default value is the one actually assigned.
83 struct pro {
84 const char *p_name; /* name, e.g. -bl, -cli */
85 int p_type; /* type (int, bool, special) */
86 int p_default; /* the default value (if int) */
87 int p_special; /* depends on type */
88 int *p_obj; /* the associated variable */
89 } pro[] = {
91 {"T", PRO_SPECIAL, 0, KEY, 0},
92 {"U", PRO_SPECIAL, 0, KEY_FILE, 0},
93 {"-version", PRO_SPECIAL, 0, VERSION, 0},
94 {"P", PRO_SPECIAL, 0, IGN, 0},
95 {"bacc", PRO_BOOL, false, ON, &opt.blanklines_around_conditional_compilation},
96 {"badp", PRO_BOOL, false, ON, &opt.blanklines_after_declarations_at_proctop},
97 {"bad", PRO_BOOL, false, ON, &opt.blanklines_after_declarations},
98 {"bap", PRO_BOOL, false, ON, &opt.blanklines_after_procs},
99 {"bbb", PRO_BOOL, false, ON, &opt.blanklines_before_blockcomments},
100 {"bc", PRO_BOOL, true, OFF, &opt.leave_comma},
101 {"bl", PRO_BOOL, true, OFF, &opt.btype_2},
102 {"br", PRO_BOOL, true, ON, &opt.btype_2},
103 {"bs", PRO_BOOL, false, ON, &opt.Bill_Shannon},
104 {"cdb", PRO_BOOL, true, ON, &opt.comment_delimiter_on_blankline},
105 {"cd", PRO_INT, 0, 0, &opt.decl_com_ind},
106 {"ce", PRO_BOOL, true, ON, &opt.cuddle_else},
107 {"ci", PRO_INT, 0, 0, &opt.continuation_indent},
108 {"cli", PRO_SPECIAL, 0, CLI, 0},
109 {"cs", PRO_BOOL, false, ON, &opt.space_after_cast},
110 {"c", PRO_INT, 33, 0, &opt.com_ind},
111 {"di", PRO_INT, 16, 0, &opt.decl_indent},
112 {"dj", PRO_BOOL, false, ON, &opt.ljust_decl},
113 {"d", PRO_INT, 0, 0, &opt.unindent_displace},
114 {"eei", PRO_BOOL, false, ON, &opt.extra_expression_indent},
115 {"ei", PRO_BOOL, true, ON, &opt.else_if},
116 {"fbs", PRO_BOOL, true, ON, &opt.function_brace_split},
117 {"fc1", PRO_BOOL, true, ON, &opt.format_col1_comments},
118 {"fcb", PRO_BOOL, true, ON, &opt.format_block_comments},
119 {"ip", PRO_BOOL, true, ON, &opt.indent_parameters},
120 {"i", PRO_INT, 8, 0, &opt.ind_size},
121 {"lc", PRO_INT, 0, 0, &opt.block_comment_max_col},
122 {"ldi", PRO_INT, -1, 0, &opt.local_decl_indent},
123 {"lpl", PRO_BOOL, false, ON, &opt.lineup_to_parens_always},
124 {"lp", PRO_BOOL, true, ON, &opt.lineup_to_parens},
125 {"l", PRO_INT, 78, 0, &opt.max_col},
126 {"nbacc", PRO_BOOL, false, OFF, &opt.blanklines_around_conditional_compilation},
127 {"nbadp", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations_at_proctop},
128 {"nbad", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations},
129 {"nbap", PRO_BOOL, false, OFF, &opt.blanklines_after_procs},
130 {"nbbb", PRO_BOOL, false, OFF, &opt.blanklines_before_blockcomments},
131 {"nbc", PRO_BOOL, true, ON, &opt.leave_comma},
132 {"nbs", PRO_BOOL, false, OFF, &opt.Bill_Shannon},
133 {"ncdb", PRO_BOOL, true, OFF, &opt.comment_delimiter_on_blankline},
134 {"nce", PRO_BOOL, true, OFF, &opt.cuddle_else},
135 {"ncs", PRO_BOOL, false, OFF, &opt.space_after_cast},
136 {"ndj", PRO_BOOL, false, OFF, &opt.ljust_decl},
137 {"neei", PRO_BOOL, false, OFF, &opt.extra_expression_indent},
138 {"nei", PRO_BOOL, true, OFF, &opt.else_if},
139 {"nfbs", PRO_BOOL, true, OFF, &opt.function_brace_split},
140 {"nfc1", PRO_BOOL, true, OFF, &opt.format_col1_comments},
141 {"nfcb", PRO_BOOL, true, OFF, &opt.format_block_comments},
142 {"nip", PRO_BOOL, true, OFF, &opt.indent_parameters},
143 {"nlpl", PRO_BOOL, false, OFF, &opt.lineup_to_parens_always},
144 {"nlp", PRO_BOOL, true, OFF, &opt.lineup_to_parens},
145 {"npcs", PRO_BOOL, false, OFF, &opt.proc_calls_space},
146 {"npro", PRO_SPECIAL, 0, IGN, 0},
147 {"npsl", PRO_BOOL, true, OFF, &opt.procnames_start_line},
148 {"nsc", PRO_BOOL, true, OFF, &opt.star_comment_cont},
149 {"nsob", PRO_BOOL, false, OFF, &opt.swallow_optional_blanklines},
150 {"nut", PRO_BOOL, true, OFF, &opt.use_tabs},
151 {"nv", PRO_BOOL, false, OFF, &opt.verbose},
152 {"pcs", PRO_BOOL, false, ON, &opt.proc_calls_space},
153 {"psl", PRO_BOOL, true, ON, &opt.procnames_start_line},
154 {"sc", PRO_BOOL, true, ON, &opt.star_comment_cont},
155 {"sob", PRO_BOOL, false, ON, &opt.swallow_optional_blanklines},
156 {"st", PRO_SPECIAL, 0, STDIN, 0},
157 {"ta", PRO_BOOL, false, ON, &opt.auto_typedefs},
158 {"ts", PRO_INT, 8, 0, &opt.tabsize},
159 {"ut", PRO_BOOL, true, ON, &opt.use_tabs},
160 {"v", PRO_BOOL, false, ON, &opt.verbose},
161 /* whew! */
162 {0, 0, 0, 0, 0}
166 * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
167 * given in these files.
169 void
170 set_profile(const char *profile_name)
172 FILE *f;
173 char fname[PATH_MAX];
174 static char prof[] = ".indent.pro";
176 if (profile_name == NULL)
177 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
178 else
179 snprintf(fname, sizeof(fname), "%s", profile_name + 2);
180 if ((f = fopen(option_source = fname, "r")) != NULL) {
181 scan_profile(f);
182 (void) fclose(f);
184 if ((f = fopen(option_source = prof, "r")) != NULL) {
185 scan_profile(f);
186 (void) fclose(f);
188 option_source = "Command line";
191 static void
192 scan_profile(FILE *f)
194 int comment, i;
195 char *p;
196 char buf[BUFSIZ];
198 while (1) {
199 p = buf;
200 comment = 0;
201 while ((i = getc(f)) != EOF) {
202 if (i == '*' && !comment && p > buf && p[-1] == '/') {
203 comment = p - buf;
204 *p++ = i;
205 } else if (i == '/' && comment && p > buf && p[-1] == '*') {
206 p = buf + comment - 1;
207 comment = 0;
208 } else if (isspace((unsigned char)i)) {
209 if (p > buf && !comment)
210 break;
211 } else {
212 *p++ = i;
215 if (p != buf) {
216 *p++ = 0;
217 if (opt.verbose)
218 printf("profile: %s\n", buf);
219 set_option(buf);
221 else if (i == EOF)
222 return;
226 static const char *
227 eqin(const char *s1, const char *s2)
229 while (*s1) {
230 if (*s1++ != *s2++)
231 return (NULL);
233 return (s2);
237 * Set the defaults.
239 void
240 set_defaults(void)
242 struct pro *p;
245 * Because ps.case_indent is a float, we can't initialize it from the
246 * table:
248 opt.case_indent = 0.0; /* -cli0.0 */
249 for (p = pro; p->p_name; p++)
250 if (p->p_type != PRO_SPECIAL)
251 *p->p_obj = p->p_default;
254 void
255 set_option(char *arg)
257 struct pro *p;
258 const char *param_start;
260 arg++; /* ignore leading "-" */
261 for (p = pro; p->p_name; p++)
262 if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL)
263 goto found;
264 errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
265 found:
266 switch (p->p_type) {
268 case PRO_SPECIAL:
269 switch (p->p_special) {
271 case IGN:
272 break;
274 case CLI:
275 if (*param_start == 0)
276 goto need_param;
277 opt.case_indent = atof(param_start);
278 break;
280 case STDIN:
281 if (input == NULL)
282 input = stdin;
283 if (output == NULL)
284 output = stdout;
285 break;
287 case KEY:
288 if (*param_start == 0)
289 goto need_param;
290 add_typename(param_start);
291 break;
293 case KEY_FILE:
294 if (*param_start == 0)
295 goto need_param;
296 add_typedefs_from_file(param_start);
297 break;
299 case VERSION:
300 printf("FreeBSD indent %s\n", INDENT_VERSION);
301 exit(0);
303 default:
304 errx(1, "set_option: internal error: p_special %d", p->p_special);
306 break;
308 case PRO_BOOL:
309 if (p->p_special == OFF)
310 *p->p_obj = false;
311 else
312 *p->p_obj = true;
313 break;
315 case PRO_INT:
316 if (!isdigit((unsigned char)*param_start)) {
317 need_param:
318 errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
320 *p->p_obj = atoi(param_start);
321 break;
323 default:
324 errx(1, "set_option: internal error: p_type %d", p->p_type);
328 void
329 add_typedefs_from_file(const char *str)
331 FILE *file;
332 char line[BUFSIZ];
334 if ((file = fopen(str, "r")) == NULL) {
335 fprintf(stderr, "indent: cannot open file %s\n", str);
336 exit(1);
338 while ((fgets(line, BUFSIZ, file)) != NULL) {
339 /* Remove trailing whitespace */
340 line[strcspn(line, " \t\n\r")] = '\0';
341 add_typename(line);
343 fclose(file);