2 * Copyright (C) 1998-1999,2003 Ian Jackson
4 * This is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with userv-utils; if not, write to the Free Software
16 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 static void outerror(void) {
36 exit(debugmode ?
0 : -1);
39 void syserror(const char *m
) {
40 if (printf("Content-Type: text/plain\n"
42 "ucgi: system call error:\n"
44 m
,strerror(errno
))==EOF
|| fflush(stdout
)) outerror();
48 void error(const char *m
, int st
) {
49 if (printf("Content-Type: text/plain\n"
53 st
, m
)==EOF
|| fflush(stdout
)) outerror();
57 void *xmalloc(size_t sz
) {
61 if (!r
) syserror("malloc failed");
65 void *xrealloc(void *ptr
, size_t sz
) {
69 if (!r
) syserror("realloc failed");
73 void xsetenv(const char *en
, const char *ev
, int overwrite
) {
74 if (setenv(en
,ev
,overwrite
)) syserror("setenv");
77 const char **load_filters(unsigned flags
, const char *first
, ...) {
79 const char *name
, *p
, *q
, **v
;
83 char buf
[MAX_ENVVAR_NAME
];
85 D( if (debugmode
) printf(";; load_filters...\n"); )
87 for (name
= first
; name
; name
= va_arg(ap
, const char *)) {
88 fp
= fopen(name
, "r"); if (fp
) goto opened
;
90 printf(";; file `%s': %s\n", name
, strerror(errno
)); )
91 if (errno
!= ENOENT
) syserror("failed to open environment filters");
94 if (flags
& LOADF_MUST
) syserror("failed to open environment filters");
95 D( if (debugmode
) printf(";; using default filters\n"); )
100 D( if (debugmode
) printf(";; file `%s': OK\n", name
); )
102 n
= 0; sz
= 128; v
= xmalloc(sz
* sizeof(*v
));
104 if (!fgets(buf
, sizeof(buf
), fp
)) break;
106 if (buf
[l
- 1] == '\n') buf
[--l
]= 0;
107 if (l
+ 1 == sizeof(buf
))
108 error("line too long in environment filter file", 500);
110 while (isspace((unsigned char)*p
)) p
++;
111 while (q
> p
&& isspace((unsigned char)q
[-1])) q
--;
112 if (*p
== '#' || p
== q
) continue;
118 D( if (debugmode
) printf(";; filter: `%s'\n", pp
); )
121 v
= xrealloc(v
, sz
* sizeof(*v
));
124 if (ferror(fp
)) syserror("failed to read environment filters");
129 static int envvar_match(unsigned flags
, const char *en
,
130 const char *const *patv
,
131 const char *const *defaults
,
133 const char *const *patp
;
138 if (!patv
) { patv
= defaults
; defaults
= 0; }
139 for (patp
= patv
; (pat
= *patp
); patp
++) {
142 if (*pat
== '!' && (flags
& FILTF_WILDCARD
)) { acceptp
= 0; pat
++; }
143 else if (*pat
== '?') {
144 if (strcmp(pat
+ 1, "DEFAULTS") == 0) {
146 rc
= envvar_match(flags
, en
, defaults
, 0, ev
);
149 error("unknown pattern directive", 500);
157 printf(";; mismatch `%s' (prefix)\n", *patp
); )
160 D( if (debugmode
) printf(";; matched pattern `%s'\n", *patp
); )
162 } else if (*pat
== '*' && (flags
& FILTF_WILDCARD
)) {
166 printf(";; mismatch `%s' (discard: no `=')\n", *patp
); )
170 printf(";; wildcard match for `%s'\n", *patp
); )
173 if (*pat
++ != *q
++) {
174 D( if (debugmode
) printf(";; mismatch `%s'\n", *patp
); )
183 if (!acceptp
) return -1;
188 void filter_environment(unsigned flags
, const char *prefix_in
,
189 const char *const *patv
,
190 const char *const *defaults
,
191 void (*foundone
)(const char *fulln
,
192 const char *en
, const char *ev
,
197 char enbuf
[MAX_ENVVAR_NAME
];
198 size_t n
, pn
= strlen(prefix_in
);
200 D( if (debugmode
) printf(";; filter_environment...\n"); )
201 for (ep
= environ
; (en
= *ep
); ep
++) {
202 D( if (debugmode
) printf(";; consider env-var `%s'\n", en
); )
203 if (strncmp(en
, prefix_in
, pn
) != 0 || !en
[pn
]) {
204 D( if (debugmode
) printf(";; doesn't match prefix\n"); )
207 if (envvar_match(flags
, en
+ pn
, patv
, defaults
, &ev
) > 0) {
209 if (n
>= sizeof(enbuf
))
210 error("environment variable name too long", 500);
211 memcpy(enbuf
, en
, n
);
214 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
215 enbuf
, enbuf
+ pn
, ev
); )
216 foundone(enbuf
, enbuf
+ pn
, ev
, p
);