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