args.c revision 1.11 1 1.11 mrg /* $NetBSD: args.c,v 1.11 2014/09/04 04:06:07 mrg 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.11 mrg __RCSID("$NetBSD: args.c,v 1.11 2014/09/04 04:06:07 mrg 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.1 cgd #include <stdio.h>
82 1.1 cgd #include <stdlib.h>
83 1.1 cgd #include <string.h>
84 1.1 cgd #include "indent_globs.h"
85 1.1 cgd
86 1.1 cgd /* profile types */
87 1.1 cgd #define PRO_SPECIAL 1 /* special case */
88 1.1 cgd #define PRO_BOOL 2 /* boolean */
89 1.1 cgd #define PRO_INT 3 /* integer */
90 1.1 cgd #define PRO_FONT 4 /* troff font */
91 1.1 cgd
92 1.1 cgd /* profile specials for booleans */
93 1.1 cgd #define ON 1 /* turn it on */
94 1.1 cgd #define OFF 0 /* turn it off */
95 1.1 cgd
96 1.1 cgd /* profile specials for specials */
97 1.1 cgd #define IGN 1 /* ignore it */
98 1.1 cgd #define CLI 2 /* case label indent (float) */
99 1.1 cgd #define STDIN 3 /* use stdin */
100 1.1 cgd #define KEY 4 /* type (keyword) */
101 1.1 cgd
102 1.10 lukem const char *option_source = "?";
103 1.1 cgd
104 1.1 cgd /*
105 1.1 cgd * N.B.: because of the way the table here is scanned, options whose names are
106 1.1 cgd * substrings of other options must occur later; that is, with -lp vs -l, -lp
107 1.1 cgd * must be first. Also, while (most) booleans occur more than once, the last
108 1.1 cgd * default value is the one actually assigned.
109 1.1 cgd */
110 1.1 cgd struct pro {
111 1.10 lukem const char *p_name; /* name, eg -bl, -cli */
112 1.5 lukem int p_type; /* type (int, bool, special) */
113 1.5 lukem int p_default; /* the default value (if int) */
114 1.5 lukem int p_special; /* depends on type */
115 1.5 lukem int *p_obj; /* the associated variable */
116 1.5 lukem } pro[] = {
117 1.5 lukem {
118 1.5 lukem "T", PRO_SPECIAL, 0, KEY, 0
119 1.5 lukem },
120 1.5 lukem {
121 1.5 lukem "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation
122 1.5 lukem },
123 1.5 lukem {
124 1.5 lukem "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop
125 1.5 lukem },
126 1.5 lukem {
127 1.5 lukem "bad", PRO_BOOL, false, ON, &blanklines_after_declarations
128 1.5 lukem },
129 1.5 lukem {
130 1.5 lukem "bap", PRO_BOOL, false, ON, &blanklines_after_procs
131 1.5 lukem },
132 1.5 lukem {
133 1.5 lukem "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments
134 1.5 lukem },
135 1.5 lukem {
136 1.5 lukem "bc", PRO_BOOL, true, OFF, &ps.leave_comma
137 1.5 lukem },
138 1.5 lukem {
139 1.5 lukem "bl", PRO_BOOL, true, OFF, &btype_2
140 1.5 lukem },
141 1.5 lukem {
142 1.5 lukem "br", PRO_BOOL, true, ON, &btype_2
143 1.5 lukem },
144 1.5 lukem {
145 1.5 lukem "bs", PRO_BOOL, false, ON, &Bill_Shannon
146 1.5 lukem },
147 1.5 lukem {
148 1.5 lukem "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline
149 1.5 lukem },
150 1.5 lukem {
151 1.5 lukem "cd", PRO_INT, 0, 0, &ps.decl_com_ind
152 1.5 lukem },
153 1.5 lukem {
154 1.5 lukem "ce", PRO_BOOL, true, ON, &cuddle_else
155 1.5 lukem },
156 1.5 lukem {
157 1.5 lukem "ci", PRO_INT, 0, 0, &continuation_indent
158 1.5 lukem },
159 1.5 lukem {
160 1.5 lukem "cli", PRO_SPECIAL, 0, CLI, 0
161 1.5 lukem },
162 1.5 lukem {
163 1.5 lukem "c", PRO_INT, 33, 0, &ps.com_ind
164 1.5 lukem },
165 1.5 lukem {
166 1.5 lukem "di", PRO_INT, 16, 0, &ps.decl_indent
167 1.5 lukem },
168 1.5 lukem {
169 1.5 lukem "dj", PRO_BOOL, false, ON, &ps.ljust_decl
170 1.5 lukem },
171 1.5 lukem {
172 1.5 lukem "d", PRO_INT, 0, 0, &ps.unindent_displace
173 1.5 lukem },
174 1.5 lukem {
175 1.5 lukem "eei", PRO_BOOL, false, ON, &extra_expression_indent
176 1.5 lukem },
177 1.5 lukem {
178 1.5 lukem "ei", PRO_BOOL, true, ON, &ps.else_if
179 1.5 lukem },
180 1.5 lukem {
181 1.5 lukem "fbc", PRO_FONT, 0, 0, (int *) &blkcomf
182 1.5 lukem },
183 1.5 lukem {
184 1.5 lukem "fbx", PRO_FONT, 0, 0, (int *) &boxcomf
185 1.5 lukem },
186 1.5 lukem {
187 1.5 lukem "fb", PRO_FONT, 0, 0, (int *) &bodyf
188 1.5 lukem },
189 1.5 lukem {
190 1.5 lukem "fc1", PRO_BOOL, true, ON, &format_col1_comments
191 1.5 lukem },
192 1.5 lukem {
193 1.5 lukem "fc", PRO_FONT, 0, 0, (int *) &scomf
194 1.5 lukem },
195 1.5 lukem {
196 1.5 lukem "fk", PRO_FONT, 0, 0, (int *) &keywordf
197 1.5 lukem },
198 1.5 lukem {
199 1.5 lukem "fs", PRO_FONT, 0, 0, (int *) &stringf
200 1.5 lukem },
201 1.5 lukem {
202 1.5 lukem "ip", PRO_BOOL, true, ON, &ps.indent_parameters
203 1.5 lukem },
204 1.5 lukem {
205 1.5 lukem "i", PRO_INT, 8, 0, &ps.ind_size
206 1.5 lukem },
207 1.5 lukem {
208 1.5 lukem "lc", PRO_INT, 0, 0, &block_comment_max_col
209 1.5 lukem },
210 1.5 lukem {
211 1.5 lukem "lp", PRO_BOOL, true, ON, &lineup_to_parens
212 1.5 lukem },
213 1.5 lukem {
214 1.5 lukem "l", PRO_INT, 78, 0, &max_col
215 1.5 lukem },
216 1.5 lukem {
217 1.5 lukem "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation
218 1.5 lukem },
219 1.5 lukem {
220 1.5 lukem "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop
221 1.5 lukem },
222 1.5 lukem {
223 1.5 lukem "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations
224 1.5 lukem },
225 1.5 lukem {
226 1.5 lukem "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs
227 1.5 lukem },
228 1.5 lukem {
229 1.5 lukem "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments
230 1.5 lukem },
231 1.5 lukem {
232 1.5 lukem "nbc", PRO_BOOL, true, ON, &ps.leave_comma
233 1.5 lukem },
234 1.5 lukem {
235 1.5 lukem "nbs", PRO_BOOL, false, OFF, &Bill_Shannon
236 1.5 lukem },
237 1.5 lukem {
238 1.5 lukem "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline
239 1.5 lukem },
240 1.5 lukem {
241 1.5 lukem "nce", PRO_BOOL, true, OFF, &cuddle_else
242 1.5 lukem },
243 1.5 lukem {
244 1.5 lukem "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl
245 1.5 lukem },
246 1.5 lukem {
247 1.5 lukem "neei", PRO_BOOL, false, OFF, &extra_expression_indent
248 1.5 lukem },
249 1.5 lukem {
250 1.5 lukem "nei", PRO_BOOL, true, OFF, &ps.else_if
251 1.5 lukem },
252 1.5 lukem {
253 1.5 lukem "nfc1", PRO_BOOL, true, OFF, &format_col1_comments
254 1.5 lukem },
255 1.5 lukem {
256 1.5 lukem "nip", PRO_BOOL, true, OFF, &ps.indent_parameters
257 1.5 lukem },
258 1.5 lukem {
259 1.5 lukem "nlp", PRO_BOOL, true, OFF, &lineup_to_parens
260 1.5 lukem },
261 1.5 lukem {
262 1.5 lukem "npcs", PRO_BOOL, false, OFF, &proc_calls_space
263 1.5 lukem },
264 1.5 lukem {
265 1.5 lukem "npro", PRO_SPECIAL, 0, IGN, 0
266 1.5 lukem },
267 1.5 lukem {
268 1.5 lukem "npsl", PRO_BOOL, true, OFF, &procnames_start_line
269 1.5 lukem },
270 1.5 lukem {
271 1.5 lukem "nps", PRO_BOOL, false, OFF, &pointer_as_binop
272 1.5 lukem },
273 1.5 lukem {
274 1.5 lukem "nsc", PRO_BOOL, true, OFF, &star_comment_cont
275 1.5 lukem },
276 1.5 lukem {
277 1.11 mrg "nut", PRO_BOOL, true, OFF, &use_tabs
278 1.11 mrg },
279 1.11 mrg {
280 1.5 lukem "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines
281 1.5 lukem },
282 1.5 lukem {
283 1.5 lukem "nv", PRO_BOOL, false, OFF, &verbose
284 1.5 lukem },
285 1.5 lukem {
286 1.5 lukem "pcs", PRO_BOOL, false, ON, &proc_calls_space
287 1.5 lukem },
288 1.5 lukem {
289 1.5 lukem "psl", PRO_BOOL, true, ON, &procnames_start_line
290 1.5 lukem },
291 1.5 lukem {
292 1.5 lukem "ps", PRO_BOOL, false, ON, &pointer_as_binop
293 1.5 lukem },
294 1.5 lukem {
295 1.5 lukem "sc", PRO_BOOL, true, ON, &star_comment_cont
296 1.5 lukem },
297 1.5 lukem {
298 1.5 lukem "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines
299 1.5 lukem },
300 1.5 lukem {
301 1.5 lukem "st", PRO_SPECIAL, 0, STDIN, 0
302 1.5 lukem },
303 1.5 lukem {
304 1.5 lukem "troff", PRO_BOOL, false, ON, &troff
305 1.5 lukem },
306 1.5 lukem {
307 1.11 mrg "ut", PRO_BOOL, true, ON, &use_tabs
308 1.11 mrg },
309 1.11 mrg {
310 1.5 lukem "v", PRO_BOOL, false, ON, &verbose
311 1.5 lukem },
312 1.5 lukem /* whew! */
313 1.5 lukem {
314 1.5 lukem 0, 0, 0, 0, 0
315 1.5 lukem }
316 1.1 cgd };
317 1.1 cgd /*
318 1.1 cgd * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
319 1.1 cgd * given in these files.
320 1.1 cgd */
321 1.5 lukem void
322 1.7 wiz set_profile(void)
323 1.1 cgd {
324 1.5 lukem FILE *f;
325 1.5 lukem char fname[BUFSIZ];
326 1.5 lukem static char prof[] = ".indent.pro";
327 1.5 lukem
328 1.8 itojun snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
329 1.5 lukem if ((f = fopen(option_source = fname, "r")) != NULL) {
330 1.5 lukem scan_profile(f);
331 1.5 lukem (void) fclose(f);
332 1.5 lukem }
333 1.5 lukem if ((f = fopen(option_source = prof, "r")) != NULL) {
334 1.5 lukem scan_profile(f);
335 1.5 lukem (void) fclose(f);
336 1.5 lukem }
337 1.5 lukem option_source = "Command line";
338 1.1 cgd }
339 1.1 cgd
340 1.5 lukem void
341 1.7 wiz scan_profile(FILE *f)
342 1.1 cgd {
343 1.5 lukem int i;
344 1.5 lukem char *p;
345 1.5 lukem char buf[BUFSIZ];
346 1.5 lukem
347 1.5 lukem while (1) {
348 1.5 lukem for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
349 1.5 lukem if (p != buf) {
350 1.5 lukem *p++ = 0;
351 1.5 lukem if (verbose)
352 1.5 lukem printf("profile: %s\n", buf);
353 1.5 lukem set_option(buf);
354 1.5 lukem } else
355 1.5 lukem if (i == EOF)
356 1.5 lukem return;
357 1.1 cgd }
358 1.1 cgd }
359 1.1 cgd
360 1.10 lukem const char *param_start;
361 1.1 cgd
362 1.5 lukem int
363 1.10 lukem eqin(const char *s1, const char *s2)
364 1.1 cgd {
365 1.5 lukem while (*s1) {
366 1.5 lukem if (*s1++ != *s2++)
367 1.5 lukem return (false);
368 1.5 lukem }
369 1.5 lukem param_start = s2;
370 1.5 lukem return (true);
371 1.1 cgd }
372 1.1 cgd /*
373 1.1 cgd * Set the defaults.
374 1.1 cgd */
375 1.5 lukem void
376 1.7 wiz set_defaults(void)
377 1.1 cgd {
378 1.5 lukem struct pro *p;
379 1.1 cgd
380 1.5 lukem /*
381 1.5 lukem * Because ps.case_indent is a float, we can't initialize it from the
382 1.5 lukem * table:
383 1.5 lukem */
384 1.5 lukem ps.case_indent = 0.0; /* -cli0.0 */
385 1.5 lukem for (p = pro; p->p_name; p++)
386 1.5 lukem if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
387 1.5 lukem *p->p_obj = p->p_default;
388 1.1 cgd }
389 1.1 cgd
390 1.5 lukem void
391 1.7 wiz set_option(char *arg)
392 1.1 cgd {
393 1.5 lukem struct pro *p;
394 1.1 cgd
395 1.5 lukem arg++; /* ignore leading "-" */
396 1.5 lukem for (p = pro; p->p_name; p++)
397 1.5 lukem if (*p->p_name == *arg && eqin(p->p_name, arg))
398 1.5 lukem goto found;
399 1.5 lukem fprintf(stderr, "indent: %s: unknown parameter \"%s\"\n", option_source, arg - 1);
400 1.5 lukem exit(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.5 lukem fprintf(stderr, "\
436 1.1 cgd indent: set_option: internal error: p_special %d\n", p->p_special);
437 1.5 lukem exit(1);
438 1.5 lukem }
439 1.5 lukem break;
440 1.5 lukem
441 1.5 lukem case PRO_BOOL:
442 1.5 lukem if (p->p_special == OFF)
443 1.5 lukem *p->p_obj = false;
444 1.5 lukem else
445 1.5 lukem *p->p_obj = true;
446 1.5 lukem break;
447 1.5 lukem
448 1.5 lukem case PRO_INT:
449 1.6 christos if (!isdigit((unsigned char)*param_start)) {
450 1.5 lukem need_param:
451 1.5 lukem fprintf(stderr, "indent: %s: ``%s'' requires a parameter\n",
452 1.5 lukem option_source, arg - 1);
453 1.5 lukem exit(1);
454 1.5 lukem }
455 1.5 lukem *p->p_obj = atoi(param_start);
456 1.5 lukem break;
457 1.5 lukem
458 1.5 lukem case PRO_FONT:
459 1.5 lukem parsefont((struct fstate *) p->p_obj, param_start);
460 1.5 lukem break;
461 1.1 cgd
462 1.5 lukem default:
463 1.5 lukem fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
464 1.5 lukem p->p_type);
465 1.5 lukem exit(1);
466 1.1 cgd }
467 1.1 cgd }
468