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