Merge branch 'vendor/GCC44'
[dragonfly.git] / usr.bin / indent / args.c
1 /*
2  * Copyright (c) 1985 Sun Microsystems, Inc.
3  * Copyright (c) 1980, 1993
4  *      The Regents of the University of California.  All rights reserved.
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  * @(#)args.c   8.1 (Berkeley) 6/6/93
36  * $FreeBSD: src/usr.bin/indent/args.c,v 1.15 2004/09/19 20:34:30 das Exp $
37  * $DragonFly: src/usr.bin/indent/args.c,v 1.3 2005/04/10 20:55:38 drhodus Exp $
38  */
39
40 /*
41  * Argument scanning and profile reading code.  Default parameters are set
42  * here as well.
43  */
44
45 #include <ctype.h>
46 #include <err.h>
47 #include <limits.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include "indent_globs.h"
52 #include "indent.h"
53
54 /* profile types */
55 #define PRO_SPECIAL     1       /* special case */
56 #define PRO_BOOL        2       /* boolean */
57 #define PRO_INT         3       /* integer */
58 #define PRO_FONT        4       /* troff font */
59
60 /* profile specials for booleans */
61 #define ON              1       /* turn it on */
62 #define OFF             0       /* turn it off */
63
64 /* profile specials for specials */
65 #define IGN             1       /* ignore it */
66 #define CLI             2       /* case label indent (float) */
67 #define STDIN           3       /* use stdin */
68 #define KEY             4       /* type (keyword) */
69
70 static void scan_profile(FILE *);
71
72 const 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     const char *p_name;         /* name, e.g. -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
88     {"T", PRO_SPECIAL, 0, KEY, 0},
89     {"bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation},
90     {"badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop},
91     {"bad", PRO_BOOL, false, ON, &blanklines_after_declarations},
92     {"bap", PRO_BOOL, false, ON, &blanklines_after_procs},
93     {"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments},
94     {"bc", PRO_BOOL, true, OFF, &ps.leave_comma},
95     {"bl", PRO_BOOL, true, OFF, &btype_2},
96     {"br", PRO_BOOL, true, ON, &btype_2},
97     {"bs", PRO_BOOL, false, ON, &Bill_Shannon},
98     {"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline},
99     {"cd", PRO_INT, 0, 0, &ps.decl_com_ind},
100     {"ce", PRO_BOOL, true, ON, &cuddle_else},
101     {"ci", PRO_INT, 0, 0, &continuation_indent},
102     {"cli", PRO_SPECIAL, 0, CLI, 0},
103     {"c", PRO_INT, 33, 0, &ps.com_ind},
104     {"di", PRO_INT, 16, 0, &ps.decl_indent},
105     {"dj", PRO_BOOL, false, ON, &ps.ljust_decl},
106     {"d", PRO_INT, 0, 0, &ps.unindent_displace},
107     {"eei", PRO_BOOL, false, ON, &extra_expression_indent},
108     {"ei", PRO_BOOL, true, ON, &ps.else_if},
109     {"fbc", PRO_FONT, 0, 0, (int *) &blkcomf},
110     {"fbs", PRO_BOOL, true, ON, &function_brace_split},
111     {"fbx", PRO_FONT, 0, 0, (int *) &boxcomf},
112     {"fb", PRO_FONT, 0, 0, (int *) &bodyf},
113     {"fc1", PRO_BOOL, true, ON, &format_col1_comments},
114     {"fcb", PRO_BOOL, true, ON, &format_block_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     {"ldi", PRO_INT, -1, 0, &ps.local_decl_indent},
122     {"lp", PRO_BOOL, true, ON, &lineup_to_parens},
123     {"l", PRO_INT, 78, 0, &max_col},
124     {"nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation},
125     {"nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop},
126     {"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations},
127     {"nbap", PRO_BOOL, false, OFF, &blanklines_after_procs},
128     {"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments},
129     {"nbc", PRO_BOOL, true, ON, &ps.leave_comma},
130     {"nbs", PRO_BOOL, false, OFF, &Bill_Shannon},
131     {"ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline},
132     {"nce", PRO_BOOL, true, OFF, &cuddle_else},
133     {"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl},
134     {"neei", PRO_BOOL, false, OFF, &extra_expression_indent},
135     {"nei", PRO_BOOL, true, OFF, &ps.else_if},
136     {"nfbs", PRO_BOOL, true, OFF, &function_brace_split},
137     {"nfc1", PRO_BOOL, true, OFF, &format_col1_comments},
138     {"nfcb", PRO_BOOL, true, OFF, &format_block_comments},
139     {"nip", PRO_BOOL, true, OFF, &ps.indent_parameters},
140     {"nlp", PRO_BOOL, true, OFF, &lineup_to_parens},
141     {"npcs", PRO_BOOL, false, OFF, &proc_calls_space},
142     {"npro", PRO_SPECIAL, 0, IGN, 0},
143     {"npsl", PRO_BOOL, true, OFF, &procnames_start_line},
144     {"nps", PRO_BOOL, false, OFF, &pointer_as_binop},
145     {"nsc", PRO_BOOL, true, OFF, &star_comment_cont},
146     {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines},
147     {"nut", PRO_BOOL, true, OFF, &use_tabs},
148     {"nv", PRO_BOOL, false, OFF, &verbose},
149     {"pcs", PRO_BOOL, false, ON, &proc_calls_space},
150     {"psl", PRO_BOOL, true, ON, &procnames_start_line},
151     {"ps", PRO_BOOL, false, ON, &pointer_as_binop},
152     {"sc", PRO_BOOL, true, ON, &star_comment_cont},
153     {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines},
154     {"st", PRO_SPECIAL, 0, STDIN, 0},
155     {"troff", PRO_BOOL, false, ON, &troff},
156     {"ut", PRO_BOOL, true, ON, &use_tabs},
157     {"v", PRO_BOOL, false, ON, &verbose},
158     /* whew! */
159     {0, 0, 0, 0, 0}
160 };
161
162 /*
163  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
164  * given in these files.
165  */
166 void
167 set_profile(void)
168 {
169     FILE *f;
170     char fname[PATH_MAX];
171     static char prof[] = ".indent.pro";
172
173     snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
174     if ((f = fopen(option_source = fname, "r")) != NULL) {
175         scan_profile(f);
176         (void) fclose(f);
177     }
178     if ((f = fopen(option_source = prof, "r")) != NULL) {
179         scan_profile(f);
180         (void) fclose(f);
181     }
182     option_source = "Command line";
183 }
184
185 static void
186 scan_profile(FILE *f)
187 {
188     int         comment, i;
189     char *p;
190     char        buf[BUFSIZ];
191
192     while (1) {
193         p = buf;
194         comment = 0;
195         while ((i = getc(f)) != EOF) {
196             if (i == '*' && !comment && p > buf && p[-1] == '/') {
197                 comment = p - buf;
198                 *p++ = i;
199             } else if (i == '/' && comment && p > buf && p[-1] == '*') {
200                 p = buf + comment - 1;
201                 comment = 0;
202             } else if (isspace(i)) {
203                 if (p > buf && !comment)
204                     break;
205             } else {
206                 *p++ = i;
207             }
208         }
209         if (p != buf) {
210             *p++ = 0;
211             if (verbose)
212                 printf("profile: %s\n", buf);
213             set_option(buf);
214         }
215         else if (i == EOF)
216             return;
217     }
218 }
219
220 const char      *param_start;
221
222 static int
223 eqin(const char *s1, const char *s2)
224 {
225     while (*s1) {
226         if (*s1++ != *s2++)
227             return (false);
228     }
229     param_start = s2;
230     return (true);
231 }
232
233 /*
234  * Set the defaults.
235  */
236 void
237 set_defaults(void)
238 {
239     struct pro *p;
240
241     /*
242      * Because ps.case_indent is a float, we can't initialize it from the
243      * table:
244      */
245     ps.case_indent = 0.0;       /* -cli0.0 */
246     for (p = pro; p->p_name; p++)
247         if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
248             *p->p_obj = p->p_default;
249 }
250
251 void
252 set_option(char *arg)
253 {
254     struct pro *p;
255
256     arg++;                      /* ignore leading "-" */
257     for (p = pro; p->p_name; p++)
258         if (*p->p_name == *arg && eqin(p->p_name, arg))
259             goto found;
260     errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
261 found:
262     switch (p->p_type) {
263
264     case PRO_SPECIAL:
265         switch (p->p_special) {
266
267         case IGN:
268             break;
269
270         case CLI:
271             if (*param_start == 0)
272                 goto need_param;
273             ps.case_indent = atof(param_start);
274             break;
275
276         case STDIN:
277             if (input == 0)
278                 input = stdin;
279             if (output == 0)
280                 output = stdout;
281             break;
282
283         case KEY:
284             if (*param_start == 0)
285                 goto need_param;
286             {
287                 char *str = strdup(param_start);
288                 if (str == NULL)
289                         err(1, NULL);
290                 addkey(str, 4);
291             }
292             break;
293
294         default:
295             errx(1, "set_option: internal error: p_special %d", p->p_special);
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             errx(1, "%s: ``%s'' requires a parameter", option_source, arg - 1);
310         }
311         *p->p_obj = atoi(param_start);
312         break;
313
314     case PRO_FONT:
315         parsefont((struct fstate *) p->p_obj, param_start);
316         break;
317
318     default:
319         errx(1, "set_option: internal error: p_type %d", p->p_type);
320     }
321 }