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\n"
41 "ucgi: system call error:\n"
43 m,strerror(errno))==EOF || fflush(stdout)) outerror();
47 void error(const char *m) {
48 if (printf("Content-Type: text/plain\n\n"
51 m)==EOF || fflush(stdout)) outerror();
55 void *xmalloc(size_t sz) {
59 if (!r) syserror("malloc failed");
63 void *xrealloc(void *ptr, size_t sz) {
67 if (!r) syserror("realloc failed");
71 void xsetenv(const char *en, const char *ev, int overwrite) {
72 if (setenv(en,ev,overwrite)) syserror("setenv");
75 const char **load_filters(unsigned flags, const char *first, ...)
78 const char *name, *p, *q, **v;
82 char buf[MAX_ENVVAR_NAME];
84 D( if (debugmode) printf(";; load_filters...\n"); )
86 for (name= first; name; name= va_arg(ap, const char *)) {
87 fp= fopen(name, "r"); if (fp) goto opened;
89 printf(";; file `%s': %s\n", name, strerror(errno)); )
90 if (errno != ENOENT) syserror("failed to open environment filters");
93 if (flags & LOADF_MUST) syserror("failed to open environment filters");
94 D( if (debugmode) printf(";; using default filters\n"); )
99 D( if (debugmode) printf(";; file `%s': OK\n", name); )
101 n= 0; sz= 128; v= xmalloc(sz * sizeof(*v));
103 if (!fgets(buf, sizeof(buf), fp)) break;
105 if (buf[l - 1] == '\n') buf[--l]= 0;
106 if (l + 1 == sizeof(buf))
107 error("line too long in environment filter file");
109 while (isspace((unsigned char)*p)) p++;
110 while (q > p && isspace((unsigned char)q[-1])) q--;
111 if (*p == '#' || p == q) continue;
117 D( if (debugmode) printf(";; filter: `%s'\n", pp); )
120 v= xrealloc(v, sz * sizeof(*v));
123 if (ferror(fp)) syserror("failed to read environment filters");
128 static int envvar_match(unsigned flags, const char *en,
129 const char *const *patv,
130 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");
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,
198 char enbuf[MAX_ENVVAR_NAME];
199 size_t n, pn = strlen(prefix_in);
201 D( if (debugmode) printf(";; filter_environment...\n"); )
202 for (ep= environ; (en= *ep); ep++) {
203 D( if (debugmode) printf(";; consider env-var `%s'\n", en); )
204 if (strncmp(en, prefix_in, pn) != 0 || !en[pn]) {
205 D( if (debugmode) printf(";; doesn't match prefix\n"); )
208 if (envvar_match(flags, en + pn, patv, defaults, &ev) > 0) {
210 if (n >= sizeof(enbuf))
211 error("environment variable name too long");
212 memcpy(enbuf, en, n);
215 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
216 enbuf, enbuf + pn, ev); )
217 foundone(enbuf, enbuf + pn, ev, p);