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