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