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