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.
33 static void outerror(void) {
35 exit(debugmode ? 0 : -1);
38 void syserror(const char *m) {
39 if (printf("Content-Type: text/plain\n\n"
40 "ucgi: system call error:\n"
42 m,strerror(errno))==EOF || fflush(stdout)) outerror();
46 void error(const char *m) {
47 if (printf("Content-Type: text/plain\n\n"
50 m)==EOF || fflush(stdout)) outerror();
54 void *xmalloc(size_t sz) {
58 if (!r) syserror("malloc failed");
62 void *xrealloc(void *ptr, size_t sz) {
66 if (!r) syserror("realloc failed");
70 void xsetenv(const char *en, const char *ev, int overwrite) {
71 if (setenv(en,ev,overwrite)) syserror("setenv");
74 const char **load_filters(unsigned flags, const char *first, ...)
77 const char *name, *p, *q, **v;
81 char buf[MAX_ENVVAR_NAME];
83 D( if (debugmode) printf(";; load_filters...\n"); )
85 for (name= first; name; name= va_arg(ap, const char *)) {
86 fp= fopen(name, "r"); if (fp) goto opened;
88 printf(";; file `%s': %s\n", name, strerror(errno)); )
89 if (errno != ENOENT) syserror("failed to open environment filters");
92 if (flags & LOADF_MUST) syserror("failed to open environment filters");
93 D( if (debugmode) printf(";; using default filters\n"); )
98 D( if (debugmode) printf(";; file `%s': OK\n", name); )
100 n= 0; sz= 128; v= xmalloc(sz * sizeof(*v));
102 if (!fgets(buf, sizeof(buf), fp)) break;
104 if (buf[l - 1] == '\n') buf[--l]= 0;
105 if (l + 1 == sizeof(buf))
106 error("line too long in environment filter file");
108 while (isspace((unsigned char)*p)) p++;
109 while (q > p && isspace((unsigned char)q[-1])) q--;
110 if (*p == '#' || p == q) continue;
116 D( if (debugmode) printf(";; filter: `%s'\n", pp); )
119 v= xrealloc(v, sz * sizeof(*v));
122 if (ferror(fp)) syserror("failed to read environment filters");
127 static int envvar_match(unsigned flags, const char *en,
128 const char *const *patv,
131 const char *const *patp;
135 for (patp= patv; (pat= *patp); patp++) {
138 if (*pat == '!' && (flags & FILTF_WILDCARD)) { acceptp= 0; pat++; }
144 printf(";; mismatch `%s' (prefix)\n", *patp); )
147 D( if (debugmode) printf(";; matched pattern `%s'\n", *patp); )
149 } else if (*pat == '*' && (flags & FILTF_WILDCARD)) {
153 printf(";; mismatch `%s' (discard: no `=')\n", *patp); )
157 printf(";; wildcard match for `%s'\n", *patp); )
160 if (*pat++ != *q++) {
161 D( if (debugmode) printf(";; mismatch `%s'\n", *patp); )
170 if (!acceptp) return -1;
175 void filter_environment(unsigned flags, const char *prefix_in,
176 const char *const *patv,
177 void (*foundone)(const char *fulln,
178 const char *en, const char *ev,
184 char enbuf[MAX_ENVVAR_NAME];
185 size_t n, pn = strlen(prefix_in);
187 D( if (debugmode) printf(";; filter_environment...\n"); )
188 for (ep= environ; (en= *ep); ep++) {
189 D( if (debugmode) printf(";; consider env-var `%s'\n", en); )
190 if (strncmp(en, prefix_in, pn) != 0 || !en[pn]) {
191 D( if (debugmode) printf(";; doesn't match prefix\n"); )
194 if (envvar_match(flags, en + pn, patv, &ev) > 0) {
196 if (n >= sizeof(enbuf))
197 error("environment variable name too long");
198 memcpy(enbuf, en, n);
201 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
202 enbuf, enbuf + pn, ev); )
203 foundone(enbuf, enbuf + pn, ev, p);