1 /* $NetBSD: args.c,v 1.14 2019/04/04 15:22:13 kamil 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 #ifndef lint 42 static char sccsid[] = "@(#)args.c 8.1 (Berkeley) 6/6/93"; 43 #endif /* not lint */ 44 #endif 45 46 #include <sys/cdefs.h> 47 #ifndef lint 48 #if defined(__NetBSD__) 49 __RCSID("$NetBSD: args.c,v 1.14 2019/04/04 15:22:13 kamil Exp $"); 50 #elif defined(__FreeBSD__) 51 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $"); 52 #endif 53 #endif 54 55 /* 56 * Argument scanning and profile reading code. Default parameters are set 57 * here as well. 58 */ 59 60 #include <ctype.h> 61 #include <err.h> 62 #include <limits.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include "indent_globs.h" 67 #include "indent.h" 68 69 #define INDENT_VERSION "2.0" 70 71 /* profile types */ 72 #define PRO_SPECIAL 1 /* special case */ 73 #define PRO_BOOL 2 /* boolean */ 74 #define PRO_INT 3 /* integer */ 75 76 /* profile specials for booleans */ 77 #define ON 1 /* turn it on */ 78 #define OFF 0 /* turn it off */ 79 80 /* profile specials for specials */ 81 #define IGN 1 /* ignore it */ 82 #define CLI 2 /* case label indent (float) */ 83 #define STDIN 3 /* use stdin */ 84 #define KEY 4 /* type (keyword) */ 85 86 static void scan_profile(FILE *); 87 88 #define KEY_FILE 5 /* only used for args */ 89 #define VERSION 6 /* only used for args */ 90 91 const char *option_source = "?"; 92 93 void add_typedefs_from_file(const char *str); 94 95 /* 96 * N.B.: because of the way the table here is scanned, options whose names are 97 * substrings of other options must occur later; that is, with -lp vs -l, -lp 98 * must be first. Also, while (most) booleans occur more than once, the last 99 * default value is the one actually assigned. 100 */ 101 struct pro { 102 const char *p_name; /* name, e.g. -bl, -cli */ 103 int p_type; /* type (int, bool, special) */ 104 int p_default; /* the default value (if int) */ 105 int p_special; /* depends on type */ 106 int *p_obj; /* the associated variable */ 107 } pro[] = { 108 109 {"T", PRO_SPECIAL, 0, KEY, 0}, 110 {"U", PRO_SPECIAL, 0, KEY_FILE, 0}, 111 {"-version", PRO_SPECIAL, 0, VERSION, 0}, 112 {"P", PRO_SPECIAL, 0, IGN, 0}, 113 {"bacc", PRO_BOOL, false, ON, &opt.blanklines_around_conditional_compilation}, 114 {"badp", PRO_BOOL, false, ON, &opt.blanklines_after_declarations_at_proctop}, 115 {"bad", PRO_BOOL, false, ON, &opt.blanklines_after_declarations}, 116 {"bap", PRO_BOOL, false, ON, &opt.blanklines_after_procs}, 117 {"bbb", PRO_BOOL, false, ON, &opt.blanklines_before_blockcomments}, 118 {"bc", PRO_BOOL, true, OFF, &opt.leave_comma}, 119 {"bl", PRO_BOOL, true, OFF, &opt.btype_2}, 120 {"br", PRO_BOOL, true, ON, &opt.btype_2}, 121 {"bs", PRO_BOOL, false, ON, &opt.Bill_Shannon}, 122 {"cdb", PRO_BOOL, true, ON, &opt.comment_delimiter_on_blankline}, 123 {"cd", PRO_INT, 0, 0, &opt.decl_com_ind}, 124 {"ce", PRO_BOOL, true, ON, &opt.cuddle_else}, 125 {"ci", PRO_INT, 0, 0, &opt.continuation_indent}, 126 {"cli", PRO_SPECIAL, 0, CLI, 0}, 127 {"cs", PRO_BOOL, false, ON, &opt.space_after_cast}, 128 {"c", PRO_INT, 33, 0, &opt.com_ind}, 129 {"di", PRO_INT, 16, 0, &opt.decl_indent}, 130 {"dj", PRO_BOOL, false, ON, &opt.ljust_decl}, 131 {"d", PRO_INT, 0, 0, &opt.unindent_displace}, 132 {"eei", PRO_BOOL, false, ON, &opt.extra_expression_indent}, 133 {"ei", PRO_BOOL, true, ON, &opt.else_if}, 134 {"fbs", PRO_BOOL, true, ON, &opt.function_brace_split}, 135 {"fc1", PRO_BOOL, true, ON, &opt.format_col1_comments}, 136 {"fcb", PRO_BOOL, true, ON, &opt.format_block_comments}, 137 {"ip", PRO_BOOL, true, ON, &opt.indent_parameters}, 138 {"i", PRO_INT, 8, 0, &opt.ind_size}, 139 {"lc", PRO_INT, 0, 0, &opt.block_comment_max_col}, 140 {"ldi", PRO_INT, -1, 0, &opt.local_decl_indent}, 141 {"lpl", PRO_BOOL, false, ON, &opt.lineup_to_parens_always}, 142 {"lp", PRO_BOOL, true, ON, &opt.lineup_to_parens}, 143 {"l", PRO_INT, 78, 0, &opt.max_col}, 144 {"nbacc", PRO_BOOL, false, OFF, &opt.blanklines_around_conditional_compilation}, 145 {"nbadp", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations_at_proctop}, 146 {"nbad", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations}, 147 {"nbap", PRO_BOOL, false, OFF, &opt.blanklines_after_procs}, 148 {"nbbb", PRO_BOOL, false, OFF, &opt.blanklines_before_blockcomments}, 149 {"nbc", PRO_BOOL, true, ON, &opt.leave_comma}, 150 {"nbs", PRO_BOOL, false, OFF, &opt.Bill_Shannon}, 151 {"ncdb", PRO_BOOL, true, OFF, &opt.comment_delimiter_on_blankline}, 152 {"nce", PRO_BOOL, true, OFF, &opt.cuddle_else}, 153 {"ncs", PRO_BOOL, false, OFF, &opt.space_after_cast}, 154 {"ndj", PRO_BOOL, false, OFF, &opt.ljust_decl}, 155 {"neei", PRO_BOOL, false, OFF, &opt.extra_expression_indent}, 156 {"nei", PRO_BOOL, true, OFF, &opt.else_if}, 157 {"nfbs", PRO_BOOL, true, OFF, &opt.function_brace_split}, 158 {"nfc1", PRO_BOOL, true, OFF, &opt.format_col1_comments}, 159 {"nfcb", PRO_BOOL, true, OFF, &opt.format_block_comments}, 160 {"nip", PRO_BOOL, true, OFF, &opt.indent_parameters}, 161 {"nlpl", PRO_BOOL, false, OFF, &opt.lineup_to_parens_always}, 162 {"nlp", PRO_BOOL, true, OFF, &opt.lineup_to_parens}, 163 {"npcs", PRO_BOOL, false, OFF, &opt.proc_calls_space}, 164 {"npro", PRO_SPECIAL, 0, IGN, 0}, 165 {"npsl", PRO_BOOL, true, OFF, &opt.procnames_start_line}, 166 {"nsc", PRO_BOOL, true, OFF, &opt.star_comment_cont}, 167 {"nsob", PRO_BOOL, false, OFF, &opt.swallow_optional_blanklines}, 168 {"nut", PRO_BOOL, true, OFF, &opt.use_tabs}, 169 {"nv", PRO_BOOL, false, OFF, &opt.verbose}, 170 {"pcs", PRO_BOOL, false, ON, &opt.proc_calls_space}, 171 {"psl", PRO_BOOL, true, ON, &opt.procnames_start_line}, 172 {"sc", PRO_BOOL, true, ON, &opt.star_comment_cont}, 173 {"sob", PRO_BOOL, false, ON, &opt.swallow_optional_blanklines}, 174 {"st", PRO_SPECIAL, 0, STDIN, 0}, 175 {"ta", PRO_BOOL, false, ON, &opt.auto_typedefs}, 176 {"ts", PRO_INT, 8, 0, &opt.tabsize}, 177 {"ut", PRO_BOOL, true, ON, &opt.use_tabs}, 178 {"v", PRO_BOOL, false, ON, &opt.verbose}, 179 /* whew! */ 180 {0, 0, 0, 0, 0} 181 }; 182 183 /* 184 * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments 185 * given in these files. 186 */ 187 void 188 set_profile(const char *profile_name) 189 { 190 FILE *f; 191 char fname[PATH_MAX]; 192 static char prof[] = ".indent.pro"; 193 194 if (profile_name == NULL) 195 snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof); 196 else 197 snprintf(fname, sizeof(fname), "%s", profile_name + 2); 198 if ((f = fopen(option_source = fname, "r")) != NULL) { 199 scan_profile(f); 200 (void) fclose(f); 201 } 202 if ((f = fopen(option_source = prof, "r")) != NULL) { 203 scan_profile(f); 204 (void) fclose(f); 205 } 206 option_source = "Command line"; 207 } 208 209 static void 210 scan_profile(FILE *f) 211 { 212 int comment, i; 213 char *p; 214 char buf[BUFSIZ]; 215 216 while (1) { 217 p = buf; 218 comment = 0; 219 while ((i = getc(f)) != EOF) { 220 if (i == '*' && !comment && p > buf && p[-1] == '/') { 221 comment = p - buf; 222 *p++ = i; 223 } else if (i == '/' && comment && p > buf && p[-1] == '*') { 224 p = buf + comment - 1; 225 comment = 0; 226 } else if (isspace((unsigned char)i)) { 227 if (p > buf && !comment) 228 break; 229 } else { 230 *p++ = i; 231 } 232 } 233 if (p != buf) { 234 *p++ = 0; 235 if (opt.verbose) 236 printf("profile: %s\n", buf); 237 set_option(buf); 238 } 239 else if (i == EOF) 240 return; 241 } 242 } 243 244 static const char * 245 eqin(const char *s1, const char *s2) 246 { 247 while (*s1) { 248 if (*s1++ != *s2++) 249 return (NULL); 250 } 251 return (s2); 252 } 253 254 /* 255 * Set the defaults. 256 */ 257 void 258 set_defaults(void) 259 { 260 struct pro *p; 261 262 /* 263 * Because ps.case_indent is a float, we can't initialize it from the 264 * table: 265 */ 266 opt.case_indent = 0.0; /* -cli0.0 */ 267 for (p = pro; p->p_name; p++) 268 if (p->p_type != PRO_SPECIAL) 269 *p->p_obj = p->p_default; 270 } 271 272 void 273 set_option(char *arg) 274 { 275 struct pro *p; 276 const char *param_start; 277 278 arg++; /* ignore leading "-" */ 279 for (p = pro; p->p_name; p++) 280 if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL) 281 goto found; 282 errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1); 283 found: 284 switch (p->p_type) { 285 286 case PRO_SPECIAL: 287 switch (p->p_special) { 288 289 case IGN: 290 break; 291 292 case CLI: 293 if (*param_start == 0) 294 goto need_param; 295 opt.case_indent = atof(param_start); 296 break; 297 298 case STDIN: 299 if (input == NULL) 300 input = stdin; 301 if (output == NULL) 302 output = stdout; 303 break; 304 305 case KEY: 306 if (*param_start == 0) 307 goto need_param; 308 add_typename(param_start); 309 break; 310 311 case KEY_FILE: 312 if (*param_start == 0) 313 goto need_param; 314 add_typedefs_from_file(param_start); 315 break; 316 317 case VERSION: 318 printf("FreeBSD indent %s\n", INDENT_VERSION); 319 exit(0); 320 321 default: 322 errx(1, "set_option: internal error: p_special %d", p->p_special); 323 } 324 break; 325 326 case PRO_BOOL: 327 if (p->p_special == OFF) 328 *p->p_obj = false; 329 else 330 *p->p_obj = true; 331 break; 332 333 case PRO_INT: 334 if (!isdigit((unsigned char)*param_start)) { 335 need_param: 336 errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name); 337 } 338 *p->p_obj = atoi(param_start); 339 break; 340 341 default: 342 errx(1, "set_option: internal error: p_type %d", p->p_type); 343 } 344 } 345 346 void 347 add_typedefs_from_file(const char *str) 348 { 349 FILE *file; 350 char line[BUFSIZ]; 351 352 if ((file = fopen(str, "r")) == NULL) { 353 fprintf(stderr, "indent: cannot open file %s\n", str); 354 exit(1); 355 } 356 while ((fgets(line, BUFSIZ, file)) != NULL) { 357 /* Remove trailing whitespace */ 358 line[strcspn(line, " \t\n\r")] = '\0'; 359 add_typename(line); 360 } 361 fclose(file); 362 } 363