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