1 /* 2 * Copyright (c) 1985 Sun Microsystems, Inc. 3 * Copyright (c) 1980, 1993 4 * The Regents of the University of California. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)args.c 8.1 (Berkeley) 6/6/93"; 39 #endif 40 #endif /* not lint */ 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 /* 45 * Argument scanning and profile reading code. Default parameters are set 46 * here as well. 47 */ 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include "indent_globs.h" 55 #include "indent.h" 56 57 /* profile types */ 58 #define PRO_SPECIAL 1 /* special case */ 59 #define PRO_BOOL 2 /* boolean */ 60 #define PRO_INT 3 /* integer */ 61 #define PRO_FONT 4 /* troff font */ 62 63 /* profile specials for booleans */ 64 #define ON 1 /* turn it on */ 65 #define OFF 0 /* turn it off */ 66 67 /* profile specials for specials */ 68 #define IGN 1 /* ignore it */ 69 #define CLI 2 /* case label indent (float) */ 70 #define STDIN 3 /* use stdin */ 71 #define KEY 4 /* type (keyword) */ 72 73 static void scan_profile(FILE *); 74 75 const char *option_source = "?"; 76 77 /* 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. 82 */ 83 struct pro { 84 const char *p_name; /* name, eg -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[] = { 90 91 {"T", PRO_SPECIAL, 0, KEY, 0}, 92 {"bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation}, 93 {"badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop}, 94 {"bad", PRO_BOOL, false, ON, &blanklines_after_declarations}, 95 {"bap", PRO_BOOL, false, ON, &blanklines_after_procs}, 96 {"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments}, 97 {"bc", PRO_BOOL, true, OFF, &ps.leave_comma}, 98 {"bl", PRO_BOOL, true, OFF, &btype_2}, 99 {"br", PRO_BOOL, true, ON, &btype_2}, 100 {"bs", PRO_BOOL, false, ON, &Bill_Shannon}, 101 {"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline}, 102 {"cd", PRO_INT, 0, 0, &ps.decl_com_ind}, 103 {"ce", PRO_BOOL, true, ON, &cuddle_else}, 104 {"ci", PRO_INT, 0, 0, &continuation_indent}, 105 {"cli", PRO_SPECIAL, 0, CLI, 0}, 106 {"c", PRO_INT, 33, 0, &ps.com_ind}, 107 {"di", PRO_INT, 16, 0, &ps.decl_indent}, 108 {"dj", PRO_BOOL, false, ON, &ps.ljust_decl}, 109 {"d", PRO_INT, 0, 0, &ps.unindent_displace}, 110 {"eei", PRO_BOOL, false, ON, &extra_expression_indent}, 111 {"ei", PRO_BOOL, true, ON, &ps.else_if}, 112 {"fbc", PRO_FONT, 0, 0, (int *) &blkcomf}, 113 {"fbx", PRO_FONT, 0, 0, (int *) &boxcomf}, 114 {"fb", PRO_FONT, 0, 0, (int *) &bodyf}, 115 {"fc1", PRO_BOOL, true, ON, &format_col1_comments}, 116 {"fcb", PRO_BOOL, true, ON, &format_block_comments}, 117 {"fc", PRO_FONT, 0, 0, (int *) &scomf}, 118 {"fk", PRO_FONT, 0, 0, (int *) &keywordf}, 119 {"fs", PRO_FONT, 0, 0, (int *) &stringf}, 120 {"ip", PRO_BOOL, true, ON, &ps.indent_parameters}, 121 {"i", PRO_INT, 8, 0, &ps.ind_size}, 122 {"lc", PRO_INT, 0, 0, &block_comment_max_col}, 123 {"lp", PRO_BOOL, true, ON, &lineup_to_parens}, 124 {"l", PRO_INT, 78, 0, &max_col}, 125 {"nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation}, 126 {"nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop}, 127 {"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations}, 128 {"nbap", PRO_BOOL, false, OFF, &blanklines_after_procs}, 129 {"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments}, 130 {"nbc", PRO_BOOL, true, ON, &ps.leave_comma}, 131 {"nbs", PRO_BOOL, false, OFF, &Bill_Shannon}, 132 {"ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline}, 133 {"nce", PRO_BOOL, true, OFF, &cuddle_else}, 134 {"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl}, 135 {"neei", PRO_BOOL, false, OFF, &extra_expression_indent}, 136 {"nei", PRO_BOOL, true, OFF, &ps.else_if}, 137 {"nfc1", PRO_BOOL, true, OFF, &format_col1_comments}, 138 {"nfcb", PRO_BOOL, true, OFF, &format_block_comments}, 139 {"nip", PRO_BOOL, true, OFF, &ps.indent_parameters}, 140 {"nlp", PRO_BOOL, true, OFF, &lineup_to_parens}, 141 {"npcs", PRO_BOOL, false, OFF, &proc_calls_space}, 142 {"npro", PRO_SPECIAL, 0, IGN, 0}, 143 {"npsl", PRO_BOOL, true, OFF, &procnames_start_line}, 144 {"nps", PRO_BOOL, false, OFF, &pointer_as_binop}, 145 {"nsc", PRO_BOOL, true, OFF, &star_comment_cont}, 146 {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines}, 147 {"nv", PRO_BOOL, false, OFF, &verbose}, 148 {"pcs", PRO_BOOL, false, ON, &proc_calls_space}, 149 {"psl", PRO_BOOL, true, ON, &procnames_start_line}, 150 {"ps", PRO_BOOL, false, ON, &pointer_as_binop}, 151 {"sc", PRO_BOOL, true, ON, &star_comment_cont}, 152 {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines}, 153 {"st", PRO_SPECIAL, 0, STDIN, 0}, 154 {"troff", PRO_BOOL, false, ON, &troff}, 155 {"v", PRO_BOOL, false, ON, &verbose}, 156 /* whew! */ 157 {0, 0, 0, 0, 0} 158 }; 159 160 /* 161 * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments 162 * given in these files. 163 */ 164 void 165 set_profile(void) 166 { 167 FILE *f; 168 char fname[BUFSIZ]; 169 static char prof[] = ".indent.pro"; 170 171 sprintf(fname, "%s/%s", getenv("HOME"), prof); 172 if ((f = fopen(option_source = fname, "r")) != NULL) { 173 scan_profile(f); 174 (void) fclose(f); 175 } 176 if ((f = fopen(option_source = prof, "r")) != NULL) { 177 scan_profile(f); 178 (void) fclose(f); 179 } 180 option_source = "Command line"; 181 } 182 183 static void 184 scan_profile(FILE *f) 185 { 186 int i; 187 char *p; 188 char buf[BUFSIZ]; 189 190 while (1) { 191 for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p); 192 if (p != buf) { 193 *p++ = 0; 194 if (verbose) 195 printf("profile: %s\n", buf); 196 set_option(buf); 197 } 198 else if (i == EOF) 199 return; 200 } 201 } 202 203 const char *param_start; 204 205 static int 206 eqin(const char *s1, const char *s2) 207 { 208 while (*s1) { 209 if (*s1++ != *s2++) 210 return (false); 211 } 212 param_start = s2; 213 return (true); 214 } 215 216 /* 217 * Set the defaults. 218 */ 219 void 220 set_defaults(void) 221 { 222 struct pro *p; 223 224 /* 225 * Because ps.case_indent is a float, we can't initialize it from the 226 * table: 227 */ 228 ps.case_indent = 0.0; /* -cli0.0 */ 229 for (p = pro; p->p_name; p++) 230 if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT) 231 *p->p_obj = p->p_default; 232 } 233 234 void 235 set_option(char *arg) 236 { 237 struct pro *p; 238 239 arg++; /* ignore leading "-" */ 240 for (p = pro; p->p_name; p++) 241 if (*p->p_name == *arg && eqin(p->p_name, arg)) 242 goto found; 243 errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1); 244 found: 245 switch (p->p_type) { 246 247 case PRO_SPECIAL: 248 switch (p->p_special) { 249 250 case IGN: 251 break; 252 253 case CLI: 254 if (*param_start == 0) 255 goto need_param; 256 ps.case_indent = atof(param_start); 257 break; 258 259 case STDIN: 260 if (input == 0) 261 input = stdin; 262 if (output == 0) 263 output = stdout; 264 break; 265 266 case KEY: 267 if (*param_start == 0) 268 goto need_param; 269 { 270 char *str = (char *) malloc(strlen(param_start) + 1); 271 strcpy(str, param_start); 272 addkey(str, 4); 273 } 274 break; 275 276 default: 277 errx(1, "set_option: internal error: p_special %d", p->p_special); 278 } 279 break; 280 281 case PRO_BOOL: 282 if (p->p_special == OFF) 283 *p->p_obj = false; 284 else 285 *p->p_obj = true; 286 break; 287 288 case PRO_INT: 289 if (!isdigit(*param_start)) { 290 need_param: 291 errx(1, "%s: ``%s'' requires a parameter", option_source, arg - 1); 292 } 293 *p->p_obj = atoi(param_start); 294 break; 295 296 case PRO_FONT: 297 parsefont((struct fstate *) p->p_obj, param_start); 298 break; 299 300 default: 301 errx(1, "set_option: internal error: p_type %d", p->p_type); 302 } 303 } 304