1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/*
* This file is part of the sn package.
* Distribution of sn is covered by the GNU GPL. See file COPYING.
* Copyright 1998-2000 Harold Tay.
* Copyright 2000- Patrik Rdman.
*/
/*
* Run a program on each article in an article stream.
* We could use a pipe instead of tmp file, but user might want to
* seek on it. Anyway profiling indicates the pipe method not to be
* significantly faster.
*/
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "config.h"
#include "unfold.h"
#include <opt.h>
#include <readln.h>
#include <out.h>
#include <format.h>
static const char rcsid[] = "$Id$";
static void nomem(void) { fail(2, "no memory"); }
int debug;
struct readln input = { 0, };
static int tmpfd;
#define SEQUENCE "SEQUENCE=000000"
#define MAXSEQ 999999
static char seqbuf[sizeof (SEQUENCE)];
static char bytebuf[40];
static char hlbuf[50];
static char blbuf[50];
static int head_lines;
static int body_lines;
static struct export {
char *s;
int len;
char *name;
} *exports = 0;
static int nrexports;
static char **env = 0;
static int envstart, envused, envend;
static int article_bytes;
void clear_env (void)
{
int i;
for (i = envstart; i < envend; i++)
if (env[i])
{
free(env[i]);
env[i] = 0;
}
for (i = 0; i < nrexports; i++)
exports[i].s = 0; /* just freed */
env[envused = envstart] = 0;
}
void put_env (int index, char *value)
{
struct export *ep;
int len;
char *p;
char *q;
ep = exports + index;
ep->s = malloc(len = ep->len + 4 + 1 + strlen(value) + 1);
if (!ep->s)
nomem();
strcpy(ep->s, "FLD_");
for (p = ep->s + 4, q = ep->name; *q; q++, p++)
if (*q >= 'a' && *q <= 'z')
*p = *q - ('a' - 'A');
else if ('-' == *q)
*p = '_';
*p++ = '=';
strcpy(p, value);
env[envused++] = ep->s;
}
extern char **environ; /* the real thing */
void setup_env (char **headers)
{
int i, nr;
int seq;
for (nr = 0; environ[nr]; nr++) ;
seq = 0;
for (i = 0; environ[i];)
if (!seq && 0 == strncmp(environ[i], "SEQUENCE=", 9))
{
seq = atoi(environ[i] + 9);
environ[i] = environ[--nr];
environ[nr] = 0;
}
else if (0 == strncmp(environ[i], "FLD_", 4)
|| 0 == strncmp(environ[i], "BYTES=", 6)
|| 0 == strncmp(environ[i], "HEAD_LINES=", 11)
|| 0 == strncmp(environ[i], "BODY_LINES=", 11))
{
environ[i] = environ[--nr];
environ[nr] = 0;
}
else
i++;
if (!(env = malloc((i + 6) * sizeof (char *))))
nomem();
for (nr = 0; nr < i; nr++)
env[i] = environ[i];
env[nr++] = strcpy(seqbuf, SEQUENCE);
if (seq > 0 && seq < MAXSEQ)
{
char *p;
seq++;
for (p = seqbuf + sizeof (SEQUENCE) - 2; seq > 0; p--, seq /= 10)
*p += seq % 10;
}
env[nr++] = bytebuf;
env[nr++] = hlbuf;
env[nr++] = blbuf;
env[nr] = 0;
envused = envstart = nr;
envend = nr + nrexports;
if (!headers)
return;
exports = malloc(nrexports * sizeof (struct export));
if (!exports)
nomem();
for (i = 0; i < nrexports; i++)
{
exports[i].s = 0;
exports[i].len = strlen(exports[i].name = headers[i]);
}
}
static void writetmp (char *buf, int len)
{
if (-1 == write(tmpfd, buf, len))
fail(2, "can't write to tmp file:%m");
article_bytes += len;
}
int write_file (char *line, int len)
{
int i;
writetmp(line, len);
writetmp("\n", 1);
head_lines++;
for (i = 0; i < nrexports; i++)
{
int n;
n = exports[i].len;
if (!exports[i].s && len >= n && ':' == line[n])
if (0 == strncasecmp(line, exports[i].name, n))
{
char *p;
for (p = line + n + 1; ' ' == *p; p++) ;
put_env(i, p);
break;
}
}
return (0);
}
static void shortread(void) { fail(3, "unexpected EOF"); }
static void badread(void) { fail(2, "can't read article:%m"); }
static void badfmt(void) { fail(3, "bad rnews line"); }
int read_rnews (void)
{
char *line;
int len, c, bytes;
len = readln(&input, &line, '\n');
if (0 == len)
return (0);
if (-1 == len) badread();
line[--len] = '\0';
if (len > 0)
if ('\r' == line[len - 1])
line[--len] = '\0';
if ('#' != *line++) badfmt();
if ('!' != *line++) badfmt();
while (' ' == *line)
line++;
if (strncmp(line, "rnews ", 6)) badfmt();
for (line += 6; ' ' == *line; line++) ;
for (bytes = 0; (c = *line); line++)
if (c < '0' || c > '9') badfmt();
else
{
bytes *= 10;
bytes += c - '0';
}
len = unfold(&input, write_file);
if (0 == len) shortread();
if (len < 0) badread();
bytes -= len;
writetmp("\n", 1);
do
{
if (-1 == (len = readln(&input, &line, '\n'))) badread();
if (0 == len) shortread();
if ((bytes -= len) < 0) shortread();
if (--len > 0)
if ('\r' == line[len - 1])
--len;
if (len)
writetmp(line, len);
writetmp("\n", 1);
body_lines++;
}
while (bytes > 0);
return (1);
}
int read_wire (void)
{
char *line;
int len;
switch (unfold(&input, write_file))
{
case 0:
return (0);
case -1:
badread();
case -2:
badfmt();
}
writetmp("\n", 1);
while ((len = readln(&input, &line, '\n')) > 0)
{
if (--len > 0)
if ('\r' == line[len - 1])
--len;
if ('.' == *line)
{
if (0 == --len)
return (1);
line++;
}
if (len > 0)
writetmp(line, len);
writetmp("\n", 1);
body_lines++;
}
if (0 == len)
shortread();
badread();
/* Not Reached */
return (0);
}
void usage (void) { fail(1, "Usage: %s [-r] [header ... -] prog ...", progname); }
static void incseq (void)
{
int i;
for (i = sizeof (SEQUENCE) - 2; i > sizeof ("SEQUENCE=") - 2; i--)
{
seqbuf[i]++;
if (seqbuf[i] <= '9')
break;
seqbuf[i] = '0';
}
}
int main (int argc, char **argv)
{
char buf[80];
char **xv;
char **hv;
char *p;
int i, pid, s;
int (*reader) (void);
progname = ((p = strrchr(argv[0], '/')) ? p + 1 : argv[0]);
reader = read_wire;
while ((i = opt_get(argc, argv, "")) > -1)
switch (i)
{
case 'r': reader = read_rnews; break;
case 'd': debug++; break;
case 'V': version(); _exit(0);
default: usage();
}
if (opt_ind >= argc)
usage();
hv = argv + opt_ind;
for (i = opt_ind; i < argc; i++)
if ('-' == *argv[i] && !argv[i][1])
break;
if (i < argc)
{
argv[i] = 0;
xv = argv + i + 1;
}
else
{
xv = hv;
hv = 0;
}
if (!*xv)
usage();
if (readln_ready(0, 0, &input)) nomem();
formats(buf, sizeof (buf) - 1, "/tmp/.%s.%d", progname, getpid());
tmpfd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0644);
if (-1 == tmpfd)
fail(2, "open(%s):%m", buf);
unlink(buf);
if (hv)
nrexports = i - opt_ind;
else
nrexports = 0;
setup_env(hv);
for (;; incseq())
{
article_bytes = head_lines = body_lines = 0;
if (!reader())
break;
formats(hlbuf, sizeof (hlbuf) - 1, "HEAD_LINES=%d", head_lines);
formats(blbuf, sizeof (blbuf) - 1, "BODY_LINES=%d", body_lines);
formats(bytebuf, sizeof (bytebuf) - 1, "BYTES=%d", article_bytes);
pid = fork();
if (-1 == pid)
fail(2, "fork:%m");
if (0 == pid)
{
lseek(tmpfd, 0, SEEK_SET);
dup2(tmpfd, 0);
environ = env;
execvp(*xv, xv);
fail(1, "exec(%s):%m", *xv);
}
while (-1 == waitpid(pid, &s, 0) && EINTR == errno) ;
if (WIFEXITED(s))
{
if ((s = WEXITSTATUS(s)))
fail(s, "%s exited with %d", *xv, s);
}
else if (WIFSIGNALED(s))
{
s = WTERMSIG(s);
fail(128 + s, "%s caught signal %d", *xv, s);
}
else
fail(128, "%s unknown wait status %d", *xv, s);
lseek(tmpfd, 0, SEEK_SET);
ftruncate(tmpfd, 0);
if (hv)
clear_env();
}
_exit(0);
}
|