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