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