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