db8c75d65858932034adae2f5d6ccb0d1f55b8cd
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 void filter_environment(unsigned flags
, const char *prefix_in
,
128 const char *const *patv
,
129 void (*foundone
)(const char *fulln
,
130 const char *en
, const char *ev
,
135 const char *const *patp
;
136 const char *en
, *ev
, *pat
, *q
;
137 char enbuf
[MAX_ENVVAR_NAME
];
138 size_t n
, pn
= strlen(prefix_in
);
141 D( if (debugmode
) printf(";; filter_environment...\n"); )
142 for (ep
= environ
; (en
= *ep
); ep
++) {
143 D( if (debugmode
) printf(";; consider env-var `%s'\n", en
); )
144 if (strncmp(en
, prefix_in
, pn
) != 0 || !en
[pn
]) {
145 D( if (debugmode
) printf(";; doesn't match prefix\n"); )
148 for (patp
= patv
; (pat
= *patp
); patp
++) {
151 if (*pat
== '!' && (flags
& FILTF_WILDCARD
)) {
158 printf(";; mismatch `%s' (prefix)\n", *patp
); )
161 D( if (debugmode
) printf(";; matched `%s'\n", *patp
); )
164 } else if (*pat
== '*' && (flags
& FILTF_WILDCARD
)) {
168 printf(";; mismatch `%s' (discard: no `=')\n", *patp
); )
172 printf(";; wildcard match for `%s'\n", *patp
); )
176 if (*pat
++ != *q
++) {
177 D( if (debugmode
) printf(";; mismatch `%s'\n", *patp
); )
183 if (n
>= sizeof(enbuf
))
184 error("environment variable name too long");
185 memcpy(enbuf
, en
, n
);
188 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
189 enbuf
, enbuf
+ pn
, ev
); )
190 foundone(enbuf
, enbuf
+ pn
, ev
, p
);
191 } D( else if (debugmode
)
192 printf(";; matched negated pattern\n"); )