Annotation of src/usr.bin/indent/args.c, Revision 1.5
1.5 ! millert 1: /* $OpenBSD: args.c,v 1.4 1997/07/25 22:00:44 mickey Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*
4: * Copyright (c) 1985 Sun Microsystems, Inc.
5: * Copyright (c) 1980 The Regents of the University of California.
6: * Copyright (c) 1976 Board of Trustees of the University of Illinois.
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: #ifndef lint
39: /*static char sccsid[] = "from: @(#)args.c 5.10 (Berkeley) 2/26/91";*/
1.5 ! millert 40: static char rcsid[] = "$OpenBSD: args.c,v 1.4 1997/07/25 22:00:44 mickey Exp $";
1.1 deraadt 41: #endif /* not lint */
42:
43: /*
44: * Argument scanning and profile reading code. Default parameters are set
45: * here as well.
46: */
47:
48: #include <stdio.h>
49: #include <ctype.h>
50: #include <stdlib.h>
51: #include <string.h>
1.3 millert 52: #include <errno.h>
1.1 deraadt 53: #include "indent_globs.h"
1.4 mickey 54: #include <err.h>
1.1 deraadt 55:
56: /* profile types */
57: #define PRO_SPECIAL 1 /* special case */
58: #define PRO_BOOL 2 /* boolean */
59: #define PRO_INT 3 /* integer */
60: #define PRO_FONT 4 /* troff font */
61:
62: /* profile specials for booleans */
63: #define ON 1 /* turn it on */
64: #define OFF 0 /* turn it off */
65:
66: /* profile specials for specials */
67: #define IGN 1 /* ignore it */
68: #define CLI 2 /* case label indent (float) */
69: #define STDIN 3 /* use stdin */
70: #define KEY 4 /* type (keyword) */
71:
72: char *option_source = "?";
73:
74: /*
75: * N.B.: because of the way the table here is scanned, options whose names are
76: * substrings of other options must occur later; that is, with -lp vs -l, -lp
77: * must be first. Also, while (most) booleans occur more than once, the last
78: * default value is the one actually assigned.
79: */
80: struct pro {
81: char *p_name; /* name, eg -bl, -cli */
82: int p_type; /* type (int, bool, special) */
83: int p_default; /* the default value (if int) */
84: int p_special; /* depends on type */
85: int *p_obj; /* the associated variable */
86: } pro[] = {
87:
1.4 mickey 88: { "T", PRO_SPECIAL, 0, KEY, 0 },
89: {"bacc", PRO_BOOL, false, ON,
90: &blanklines_around_conditional_compilation },
91: {"badp", PRO_BOOL, false, ON,
92: &blanklines_after_declarations_at_proctop },
93: {"bad", PRO_BOOL, false, ON, &blanklines_after_declarations },
94: {"bap", PRO_BOOL, false, ON, &blanklines_after_procs },
95: {"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments },
96: {"bc", PRO_BOOL, true, OFF, &ps.leave_comma },
97: {"bl", PRO_BOOL, true, OFF, &btype_2 },
98: {"br", PRO_BOOL, true, ON, &btype_2 },
99: {"bs", PRO_BOOL, false, ON, &Bill_Shannon },
100: {"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline },
101: {"cd", PRO_INT, 0, 0, &ps.decl_com_ind },
102: {"ce", PRO_BOOL, true, ON, &cuddle_else },
103: {"ci", PRO_INT, 0, 0, &continuation_indent },
104: {"cli", PRO_SPECIAL, 0, CLI, 0 },
105: {"c", PRO_INT, 33, 0, &ps.com_ind },
106: {"di", PRO_INT, 16, 0, &ps.decl_indent },
107: {"dj", PRO_BOOL, false, ON, &ps.ljust_decl },
108: {"d", PRO_INT, 0, 0, &ps.unindent_displace },
109: {"eei", PRO_BOOL, false, ON, &extra_expression_indent },
110: {"ei", PRO_BOOL, true, ON, &ps.else_if },
111: {"fbc", PRO_FONT, 0, 0, (int *) &blkcomf },
112: {"fbx", PRO_FONT, 0, 0, (int *) &boxcomf },
113: {"fb", PRO_FONT, 0, 0, (int *) &bodyf },
114: {"fc1", PRO_BOOL, true, ON, &format_col1_comments },
115: {"fc", PRO_FONT, 0, 0, (int *) &scomf },
116: {"fk", PRO_FONT, 0, 0, (int *) &keywordf },
117: {"fs", PRO_FONT, 0, 0, (int *) &stringf },
118: {"ip", PRO_BOOL, true, ON, &ps.indent_parameters },
119: {"i", PRO_INT, 8, 0, &ps.ind_size },
120: {"lc", PRO_INT, 0, 0, &block_comment_max_col },
121: {"lp", PRO_BOOL, true, ON, &lineup_to_parens },
122: {"l", PRO_INT, 78, 0, &max_col },
123: {"nbacc", PRO_BOOL, false, OFF,
124: &blanklines_around_conditional_compilation },
125: {"nbadp", PRO_BOOL, false, OFF,
126: &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: {"nip", PRO_BOOL, true, OFF, &ps.indent_parameters },
139: {"nlp", PRO_BOOL, true, OFF, &lineup_to_parens },
140: {"npcs", PRO_BOOL, false, OFF, &proc_calls_space },
141: {"npro", PRO_SPECIAL, 0, IGN, 0 },
142: {"npsl", PRO_BOOL, true, OFF, &procnames_start_line },
143: {"nps", PRO_BOOL, false, OFF, &pointer_as_binop },
144: {"nsc", PRO_BOOL, true, OFF, &star_comment_cont },
145: {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines },
146: {"nv", PRO_BOOL, false, OFF, &verbose },
147: {"pcs", PRO_BOOL, false, ON, &proc_calls_space },
148: {"psl", PRO_BOOL, true, ON, &procnames_start_line },
149: {"ps", PRO_BOOL, false, ON, &pointer_as_binop },
150: {"sc", PRO_BOOL, true, ON, &star_comment_cont },
151: {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines },
152: {"st", PRO_SPECIAL, 0, STDIN, 0 },
153: {"troff", PRO_BOOL, false, ON, &troff },
154: {"v", PRO_BOOL, false, ON, &verbose },
155: /* whew! */
156: { 0, 0, 0, 0, 0 }
1.1 deraadt 157: };
158:
1.4 mickey 159: void scan_profile();
160: void set_option();
161:
1.1 deraadt 162: /*
163: * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
164: * given in these files.
165: */
1.4 mickey 166: void
1.1 deraadt 167: set_profile()
168: {
169: register FILE *f;
170: char fname[BUFSIZ];
1.5 ! millert 171: char *home;
1.1 deraadt 172: static char prof[] = ".indent.pro";
173:
1.5 ! millert 174: home = getenv("HOME");
! 175: if (home != NULL && *home != '\0') {
! 176: if (strlen(home) + sizeof(prof) > sizeof(fname)) {
! 177: warnx("%s/%s: %s", home, prof, strerror(ENAMETOOLONG));
! 178: return;
! 179: }
! 180: sprintf(fname, "%s/%s", home, prof);
! 181: if ((f = fopen(option_source = fname, "r")) != NULL) {
! 182: scan_profile(f);
! 183: (void) fclose(f);
! 184: }
1.1 deraadt 185: }
186: if ((f = fopen(option_source = prof, "r")) != NULL) {
187: scan_profile(f);
188: (void) fclose(f);
189: }
190: option_source = "Command line";
191: }
192:
1.4 mickey 193: void
1.1 deraadt 194: scan_profile(f)
195: register FILE *f;
196: {
197: register int i;
198: register char *p;
199: char buf[BUFSIZ];
200:
201: while (1) {
202: for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
203: if (p != buf) {
204: *p++ = 0;
205: if (verbose)
206: printf("profile: %s\n", buf);
207: set_option(buf);
208: }
209: else if (i == EOF)
210: return;
211: }
212: }
213:
214: char *param_start;
215:
1.4 mickey 216: int
1.1 deraadt 217: eqin(s1, s2)
218: register char *s1;
219: register char *s2;
220: {
221: while (*s1) {
222: if (*s1++ != *s2++)
223: return (false);
224: }
225: param_start = s2;
226: return (true);
227: }
228:
229: /*
230: * Set the defaults.
231: */
1.4 mickey 232: void
1.1 deraadt 233: set_defaults()
234: {
235: register struct pro *p;
236:
237: /*
238: * Because ps.case_indent is a float, we can't initialize it from the
239: * table:
240: */
241: ps.case_indent = 0.0; /* -cli0.0 */
242: for (p = pro; p->p_name; p++)
243: if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
244: *p->p_obj = p->p_default;
245: }
246:
1.4 mickey 247: void
1.1 deraadt 248: set_option(arg)
249: register char *arg;
250: {
251: register struct pro *p;
252: extern double atof();
253:
254: arg++; /* ignore leading "-" */
255: for (p = pro; p->p_name; p++)
256: if (*p->p_name == *arg && eqin(p->p_name, arg))
257: goto found;
258: fprintf(stderr, "indent: %s: unknown parameter \"%s\"\n", option_source, arg - 1);
259: exit(1);
260: found:
261: switch (p->p_type) {
262:
263: case PRO_SPECIAL:
264: switch (p->p_special) {
265:
266: case IGN:
267: break;
268:
269: case CLI:
270: if (*param_start == 0)
271: goto need_param;
272: ps.case_indent = atof(param_start);
273: break;
274:
275: case STDIN:
276: if (input == 0)
277: input = stdin;
278: if (output == 0)
279: output = stdout;
280: break;
281:
282: case KEY:
283: if (*param_start == 0)
284: goto need_param;
285: {
286: register char *str = (char *) malloc(strlen(param_start) + 1);
287: strcpy(str, param_start);
288: addkey(str, 4);
289: }
290: break;
291:
292: default:
293: fprintf(stderr, "\
294: indent: set_option: internal error: p_special %d\n", p->p_special);
295: exit(1);
296: }
297: break;
298:
299: case PRO_BOOL:
300: if (p->p_special == OFF)
301: *p->p_obj = false;
302: else
303: *p->p_obj = true;
304: break;
305:
306: case PRO_INT:
307: if (!isdigit(*param_start)) {
308: need_param:
309: fprintf(stderr, "indent: %s: ``%s'' requires a parameter\n",
310: option_source, arg - 1);
311: exit(1);
312: }
313: *p->p_obj = atoi(param_start);
314: break;
315:
316: case PRO_FONT:
317: parsefont((struct fstate *) p->p_obj, param_start);
318: break;
319:
320: default:
321: fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
322: p->p_type);
323: exit(1);
324: }
325: }