amiga-request@ab20.larc.nasa.gov (Amiga Sources/Binaries Moderator) (03/14/91)
Submitted-by: eyal@echo.canberra.edu.au (Eyal Lebedinsky) Posting-number: Volume 91, Issue 052 Archive-name: news/vn-res-1.1-2/part06 #!/bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of archive 6 (of 6)." # Contents: regexp.c std.c # Wrapped by tadguy@ab20 on Wed Mar 13 19:10:12 1991 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f 'regexp.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'regexp.c'\" else echo shar: Extracting \"'regexp.c'\" \(28412 characters\) sed "s/^X//" >'regexp.c' <<'END_OF_FILE' X/* X * regcomp and regexec -- regsub and regerror are elsewhere X * X * Copyright (c) 1986 by University of Toronto. X * Written by Henry Spencer. Not derived from licensed software. X * X * Permission is granted to anyone to use this software for any X * purpose on any computer system, and to redistribute it freely, X * subject to the following restrictions: X * X * 1. The author is not responsible for the consequences of use of X * this software, no matter how awful, even if they arise X * from defects in it. X * X * 2. The origin of this software must not be misrepresented, either X * by explicit claim or by omission. X * X * 3. Altered versions must be plainly marked as such, and must not X * be misrepresented as being the original software. X * X * Beware that some of this code is subtly aware of the way operator X * precedence is structured in regular expressions. Serious changes in X * regular-expression syntax might require a total rethink. X * X * The third parameter to regexec was added by Martin C. Atkins. X * Andy Tanenbaum also made some changes. X */ X X#include <stdio.h> X#include "regexp.h" X X#ifdef amiga X#undef putchar X#define putchar ttputc Xextern char pr_buf[]; Xextern ttputc (); X#endif X X/* X * The first byte of the regexp internal "program" is actually this magic X * number; the start node begins in the second byte. X */ X#define MAGIC 0234 X X/* X * The "internal use only" fields in regexp.h are present to pass info from X * compile to execute that permits the execute phase to run lots faster on X * simple cases. They are: X * X * regstart char that must begin a match; '\0' if none obvious X * reganch is the match anchored (at beginning-of-line only)? X * regmust string (pointer into program) that match must include, or NULL X * regmlen length of regmust string X * X * Regstart and reganch permit very fast decisions on suitable starting points X * for a match, cutting down the work a lot. Regmust permits fast rejection X * of lines that cannot possibly match. The regmust tests are costly enough X * that regcomp() supplies a regmust only if the r.e. contains something X * potentially expensive (at present, the only such thing detected is * or + X * at the start of the r.e., which can involve a lot of backup). Regmlen is X * supplied because the test in regexec() needs it and regcomp() is computing X * it anyway. X */ X X/* X * Structure for regexp "program". This is essentially a linear encoding X * of a nondeterministic finite-state machine (aka syntax charts or X * "railroad normal form" in parsing technology). Each node is an opcode X * plus a "next" pointer, possibly plus an operand. "Next" pointers of X * all nodes except BRANCH implement concatenation; a "next" pointer with X * a BRANCH on both ends of it is connecting two alternatives. (Here we X * have one of the subtle syntax dependencies: an individual BRANCH (as X * opposed to a collection of them) is never concatenated with anything X * because of operator precedence.) The operand of some types of node is X * a literal string; for others, it is a node leading into a sub-FSM. In X * particular, the operand of a BRANCH node is the first node of the branch. X * (NB this is *not* a tree structure: the tail of the branch connects X * to the thing following the set of BRANCHes.) The opcodes are: X */ X X/* definition number opnd? meaning */ X#define END 0 /* no End of program. */ X#define BOL 1 /* no Match "" at beginning of line. */ X#define EOL 2 /* no Match "" at end of line. */ X#define ANY 3 /* no Match any one character. */ X#define ANYOF 4 /* str Match any character in this string. */ X#define ANYBUT 5 /* str Match any character not in this string. */ X#define BRANCH 6 /* node Match this alternative, or the next... */ X#define BACK 7 /* no Match "", "next" ptr points backward. */ X#define EXACTLY 8 /* str Match this string. */ X#define NOTHING 9 /* no Match empty string. */ X#define STAR 10 /* node Match this (simple) thing 0 or more times. */ X#define PLUS 11 /* node Match this (simple) thing 1 or more times. */ X#define OPEN 20 /* no Mark this point in input as start of #n. */ X /* OPEN+1 is number 1, etc. */ X#define CLOSE 30 /* no Analogous to OPEN. */ X X/* X * Opcode notes: X * X * BRANCH The set of branches constituting a single choice are hooked X * together with their "next" pointers, since precedence prevents X * anything being concatenated to any individual branch. The X * "next" pointer of the last BRANCH in a choice points to the X * thing following the whole choice. This is also where the X * final "next" pointer of each individual branch points; each X * branch starts with the operand node of a BRANCH node. X * X * BACK Normal "next" pointers all implicitly point forward; BACK X * exists to make loop structures possible. X * X * STAR,PLUS '?', and complex '*' and '+', are implemented as circular X * BRANCH structures using BACK. Simple cases (one character X * per match) are implemented with STAR and PLUS for speed X * and to minimize recursive plunges. X * X * OPEN,CLOSE ...are numbered at compile time. X */ X X/* X * A node is one char of opcode followed by two chars of "next" pointer. X * "Next" pointers are stored as two 8-bit pieces, high order first. The X * value is a positive offset from the opcode of the node containing it. X * An operand, if any, simply follows the node. (Note that much of the X * code generation knows about this implicit relationship.) X * X * Using two bytes for the "next" pointer is vast overkill for most things, X * but allows patterns to get big without disasters. X */ X#define OP(p) (*(p)) X#define NEXT(p) (((*((p)+1)&0377)<<8) + *((p)+2)&0377) X#define OPERAND(p) ((p) + 3) X X X X/* X * Utility definitions. X */ X#ifndef CHARBITS X#define UCHARAT(p) ((int)*(unsigned char *)(p)) X#else X#define UCHARAT(p) ((int)*(p)&CHARBITS) X#endif X X#define FAIL(m) { regerror(m); return(NULL); } X#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?') X#define META "^$.[()|?+*\\" X X/* X * Flags to be passed up and down. X */ X#define HASWIDTH 01 /* Known never to match null string. */ X#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */ X#define SPSTART 04 /* Starts with * or +. */ X#define WORST 0 /* Worst case. */ X X/* X * Global work variables for regcomp(). X */ Xstatic char *regparse; /* Input-scan pointer. */ Xstatic int regnpar; /* () count. */ Xstatic char regdummy; Xstatic char *regcode; /* Code-emit pointer; ®dummy = don't. */ Xstatic long regsize; /* Code size. */ X X/* X * Forward declarations for regcomp()'s friends. X */ X#ifndef STATIC X#define STATIC static X#endif XSTATIC char *reg(); XSTATIC char *regbranch(); XSTATIC char *regpiece(); XSTATIC char *regatom(); XSTATIC char *regnode(); XSTATIC char *regnext(); XSTATIC void regc(); XSTATIC void reginsert(); XSTATIC void regtail(); XSTATIC void regoptail(); XSTATIC int strcspn(); X X/* X - regcomp - compile a regular expression into internal code X * X * We can't allocate space until we know how big the compiled form will be, X * but we can't compile it (and thus know how big it is) until we've got a X * place to put the code. So we cheat: we compile it twice, once with code X * generation turned off and size counting turned on, and once "for real". X * This also means that we don't allocate space until we are sure that the X * thing really will compile successfully, and we never have to move the X * code and thus invalidate pointers into it. (Note that it has to be in X * one piece because free() must be able to free it all.) X * X * Beware that the optimization-preparation code in here knows about some X * of the structure of the compiled regexp. X */ Xregexp * Xregcomp(exp) Xchar *exp; X{ X register regexp *r; X register char *scan; X register char *longest; X register int len; X int flags; X extern char *malloc(); X X if (exp == NULL) X FAIL("NULL argument"); X X /* First pass: determine size, legality. */ X regparse = exp; X regnpar = 1; X regsize = 0L; X regcode = ®dummy; X regc(MAGIC); X if (reg(0, &flags) == NULL) X return(NULL); X X /* Small enough for pointer-storage convention? */ X if (regsize >= 32767L) /* Probably could be 65535L. */ X FAIL("regexp too big"); X X /* Allocate space. */ X r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize); X if (r == NULL) X FAIL("out of space"); X X /* Second pass: emit code. */ X regparse = exp; X regnpar = 1; X regcode = r->program; X regc(MAGIC); X if (reg(0, &flags) == NULL) X return(NULL); X X /* Dig out information for optimizations. */ X r->regstart = '\0'; /* Worst-case defaults. */ X r->reganch = 0; X r->regmust = NULL; X r->regmlen = 0; X scan = r->program+1; /* First BRANCH. */ X if (OP(regnext(scan)) == END) { /* Only one top-level choice. */ X scan = OPERAND(scan); X X /* Starting-point info. */ X if (OP(scan) == EXACTLY) X r->regstart = *OPERAND(scan); X else if (OP(scan) == BOL) X r->reganch++; X X /* X * If there's something expensive in the r.e., find the X * longest literal string that must appear and make it the X * regmust. Resolve ties in favor of later strings, since X * the regstart check works with the beginning of the r.e. X * and avoiding duplication strengthens checking. Not a X * strong reason, but sufficient in the absence of others. X */ X if (flags&SPSTART) { X longest = NULL; X len = 0; X for (; scan != NULL; scan = regnext(scan)) X if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) { X longest = OPERAND(scan); X len = strlen(OPERAND(scan)); X } X r->regmust = longest; X r->regmlen = len; X } X } X X return(r); X} X X/* X - reg - regular expression, i.e. main body or parenthesized thing X * X * Caller must absorb opening parenthesis. X * X * Combining parenthesis handling with the base level of regular expression X * is a trifle forced, but the need to tie the tails of the branches to what X * follows makes it hard to avoid. X */ Xstatic char * Xreg(paren, flagp) Xint paren; /* Parenthesized? */ Xint *flagp; X{ X register char *ret; X register char *br; X register char *ender; X register int parno; X int flags; X X *flagp = HASWIDTH; /* Tentatively. */ X X /* Make an OPEN node, if parenthesized. */ X if (paren) { X if (regnpar >= NSUBEXP) X FAIL("too many ()"); X parno = regnpar; X regnpar++; X ret = regnode(OPEN+parno); X } else X ret = NULL; X X /* Pick up the branches, linking them together. */ X br = regbranch(&flags); X if (br == NULL) X return(NULL); X if (ret != NULL) X regtail(ret, br); /* OPEN -> first. */ X else X ret = br; X if (!(flags&HASWIDTH)) X *flagp &= ~HASWIDTH; X *flagp |= flags&SPSTART; X while (*regparse == '|') { X regparse++; X br = regbranch(&flags); X if (br == NULL) X return(NULL); X regtail(ret, br); /* BRANCH -> BRANCH. */ X if (!(flags&HASWIDTH)) X *flagp &= ~HASWIDTH; X *flagp |= flags&SPSTART; X } X X /* Make a closing node, and hook it on the end. */ X ender = regnode((paren) ? CLOSE+parno : END); X regtail(ret, ender); X X /* Hook the tails of the branches to the closing node. */ X for (br = ret; br != NULL; br = regnext(br)) X regoptail(br, ender); X X /* Check for proper termination. */ X if (paren && *regparse++ != ')') { X FAIL("unmatched ()"); X } else if (!paren && *regparse != '\0') { X if (*regparse == ')') { X FAIL("unmatched ()"); X } else X FAIL("junk on end"); /* "Can't happen". */ X /* NOTREACHED */ X } X X return(ret); X} X X/* X - regbranch - one alternative of an | operator X * X * Implements the concatenation operator. X */ Xstatic char * Xregbranch(flagp) Xint *flagp; X{ X register char *ret; X register char *chain; X register char *latest; X int flags; X X *flagp = WORST; /* Tentatively. */ X X ret = regnode(BRANCH); X chain = NULL; X while (*regparse != '\0' && *regparse != '|' && *regparse != ')') { X latest = regpiece(&flags); X if (latest == NULL) X return(NULL); X *flagp |= flags&HASWIDTH; X if (chain == NULL) /* First piece. */ X *flagp |= flags&SPSTART; X else X regtail(chain, latest); X chain = latest; X } X if (chain == NULL) /* Loop ran zero times. */ X (void) regnode(NOTHING); X X return(ret); X} X X/* X - regpiece - something followed by possible [*+?] X * X * Note that the branching code sequences used for ? and the general cases X * of * and + are somewhat optimized: they use the same NOTHING node as X * both the endmarker for their branch list and the body of the last branch. X * It might seem that this node could be dispensed with entirely, but the X * endmarker role is not redundant. X */ Xstatic char * Xregpiece(flagp) Xint *flagp; X{ X register char *ret; X register char op; X register char *next; X int flags; X X ret = regatom(&flags); X if (ret == NULL) X return(NULL); X X op = *regparse; X if (!ISMULT(op)) { X *flagp = flags; X return(ret); X } X X if (!(flags&HASWIDTH) && op != '?') X FAIL("*+ operand could be empty"); X *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH); X X if (op == '*' && (flags&SIMPLE)) X reginsert(STAR, ret); X else if (op == '*') { X /* Emit x* as (x&|), where & means "self". */ X reginsert(BRANCH, ret); /* Either x */ X regoptail(ret, regnode(BACK)); /* and loop */ X regoptail(ret, ret); /* back */ X regtail(ret, regnode(BRANCH)); /* or */ X regtail(ret, regnode(NOTHING)); /* null. */ X } else if (op == '+' && (flags&SIMPLE)) X reginsert(PLUS, ret); X else if (op == '+') { X /* Emit x+ as x(&|), where & means "self". */ X next = regnode(BRANCH); /* Either */ X regtail(ret, next); X regtail(regnode(BACK), ret); /* loop back */ X regtail(next, regnode(BRANCH)); /* or */ X regtail(ret, regnode(NOTHING)); /* null. */ X } else if (op == '?') { X /* Emit x? as (x|) */ X reginsert(BRANCH, ret); /* Either x */ X regtail(ret, regnode(BRANCH)); /* or */ X next = regnode(NOTHING); /* null. */ X regtail(ret, next); X regoptail(ret, next); X } X regparse++; X if (ISMULT(*regparse)) X FAIL("nested *?+"); X X return(ret); X} X X/* X - regatom - the lowest level X * X * Optimization: gobbles an entire sequence of ordinary characters so that X * it can turn them into a single node, which is smaller to store and X * faster to run. Backslashed characters are exceptions, each becoming a X * separate node; the code is simpler that way and it's not worth fixing. X */ Xstatic char * Xregatom(flagp) Xint *flagp; X{ X register char *ret; X int flags; X X *flagp = WORST; /* Tentatively. */ X X switch (*regparse++) { X case '^': X ret = regnode(BOL); X break; X case '$': X ret = regnode(EOL); X break; X case '.': X ret = regnode(ANY); X *flagp |= HASWIDTH|SIMPLE; X break; X case '[': { X register int class; X register int classend; X X if (*regparse == '^') { /* Complement of range. */ X ret = regnode(ANYBUT); X regparse++; X } else X ret = regnode(ANYOF); X if (*regparse == ']' || *regparse == '-') X regc(*regparse++); X while (*regparse != '\0' && *regparse != ']') { X if (*regparse == '-') { X regparse++; X if (*regparse == ']' || *regparse == '\0') X regc('-'); X else { X class = UCHARAT(regparse-2)+1; X classend = UCHARAT(regparse); X if (class > classend+1) X FAIL("invalid [] range"); X for (; class <= classend; class++) X regc(class); X regparse++; X } X } else X regc(*regparse++); X } X regc('\0'); X if (*regparse != ']') X FAIL("unmatched []"); X regparse++; X *flagp |= HASWIDTH|SIMPLE; X } X break; X case '(': X ret = reg(1, &flags); X if (ret == NULL) X return(NULL); X *flagp |= flags&(HASWIDTH|SPSTART); X break; X case '\0': X case '|': X case ')': X FAIL("internal urp"); /* Supposed to be caught earlier. */ X break; X case '?': X case '+': X case '*': X FAIL("?+* follows nothing"); X break; X case '\\': X if (*regparse == '\0') X FAIL("trailing \\"); X ret = regnode(EXACTLY); X regc(*regparse++); X regc('\0'); X *flagp |= HASWIDTH|SIMPLE; X break; X default: { X register int len; X register char ender; X X regparse--; X len = strcspn(regparse, META); X if (len <= 0) X FAIL("internal disaster"); X ender = *(regparse+len); X if (len > 1 && ISMULT(ender)) X len--; /* Back off clear of ?+* operand. */ X *flagp |= HASWIDTH; X if (len == 1) X *flagp |= SIMPLE; X ret = regnode(EXACTLY); X while (len > 0) { X regc(*regparse++); X len--; X } X regc('\0'); X } X break; X } X X return(ret); X} X X/* X - regnode - emit a node X */ Xstatic char * /* Location. */ Xregnode(op) Xchar op; X{ X register char *ret; X register char *ptr; X X ret = regcode; X if (ret == ®dummy) { X regsize += 3; X return(ret); X } X X ptr = ret; X *ptr++ = op; X *ptr++ = '\0'; /* Null "next" pointer. */ X *ptr++ = '\0'; X regcode = ptr; X X return(ret); X} X X/* X - regc - emit (if appropriate) a byte of code X */ Xstatic void Xregc(b) Xchar b; X{ X if (regcode != ®dummy) X *regcode++ = b; X else X regsize++; X} X X/* X - reginsert - insert an operator in front of already-emitted operand X * X * Means relocating the operand. X */ Xstatic void Xreginsert(op, opnd) Xchar op; Xchar *opnd; X{ X register char *src; X register char *dst; X register char *place; X X if (regcode == ®dummy) { X regsize += 3; X return; X } X X src = regcode; X regcode += 3; X dst = regcode; X while (src > opnd) X *--dst = *--src; X X place = opnd; /* Op node, where operand used to be. */ X *place++ = op; X *place++ = '\0'; X *place++ = '\0'; X} X X/* X - regtail - set the next-pointer at the end of a node chain X */ Xstatic void Xregtail(p, val) Xchar *p; Xchar *val; X{ X register char *scan; X register char *temp; X register int offset; X X if (p == ®dummy) X return; X X /* Find last node. */ X scan = p; X for (;;) { X temp = regnext(scan); X if (temp == NULL) X break; X scan = temp; X } X X if (OP(scan) == BACK) X offset = scan - val; X else X offset = val - scan; X *(scan+1) = (offset>>8)&0377; X *(scan+2) = offset&0377; X} X X/* X - regoptail - regtail on operand of first argument; nop if operandless X */ Xstatic void Xregoptail(p, val) Xchar *p; Xchar *val; X{ X /* "Operandless" and "op != BRANCH" are synonymous in practice. */ X if (p == NULL || p == ®dummy || OP(p) != BRANCH) X return; X regtail(OPERAND(p), val); X} X X/* X * regexec and friends X */ X X/* X * Global work variables for regexec(). X */ Xstatic char *reginput; /* String-input pointer. */ Xstatic char *regbol; /* Beginning of input, for ^ check. */ Xstatic char **regstartp; /* Pointer to startp array. */ Xstatic char **regendp; /* Ditto for endp. */ X X/* X * Forwards. X */ XSTATIC int regtry(); XSTATIC int regmatch(); XSTATIC int regrepeat(); X X#ifdef DEBUG Xint regnarrate = 0; Xvoid regdump(); XSTATIC char *regprop(); X#endif X X/* X - regexec - match a regexp against a string X */ Xint Xregexec(prog, string, bolflag) Xregister regexp *prog; Xregister char *string; Xint bolflag; X{ X register char *s; X extern char *strchr(); X X /* Be paranoid... */ X if (prog == NULL || string == NULL) { X regerror("NULL parameter"); X return(0); X } X X /* Check validity of program. */ X if (UCHARAT(prog->program) != MAGIC) { X regerror("corrupted program"); X return(0); X } X X /* If there is a "must appear" string, look for it. */ X if (prog->regmust != NULL) { X s = string; X while ((s = strchr(s, prog->regmust[0])) != NULL) { X if (strncmp(s, prog->regmust, prog->regmlen) == 0) X break; /* Found it. */ X s++; X } X if (s == NULL) /* Not present. */ X return(0); X } X X /* Mark beginning of line for ^ . */ X if(bolflag) X regbol = string; X else X regbol = NULL; X X /* Simplest case: anchored match need be tried only once. */ X if (prog->reganch) X return(regtry(prog, string)); X X /* Messy cases: unanchored match. */ X s = string; X if (prog->regstart != '\0') X /* We know what char it must start with. */ X while ((s = strchr(s, prog->regstart)) != NULL) { X if (regtry(prog, s)) X return(1); X s++; X } X else X /* We don't -- general case. */ X do { X if (regtry(prog, s)) X return(1); X } while (*s++ != '\0'); X X /* Failure. */ X return(0); X} X X/* X - regtry - try match at specific point X */ Xstatic int /* 0 failure, 1 success */ Xregtry(prog, string) Xregexp *prog; Xchar *string; X{ X register int i; X register char **sp; X register char **ep; X X reginput = string; X regstartp = prog->startp; X regendp = prog->endp; X X sp = prog->startp; X ep = prog->endp; X for (i = NSUBEXP; i > 0; i--) { X *sp++ = NULL; X *ep++ = NULL; X } X if (regmatch(prog->program + 1)) { X prog->startp[0] = string; X prog->endp[0] = reginput; X return(1); X } else X return(0); X} X X/* X - regmatch - main matching routine X * X * Conceptually the strategy is simple: check to see whether the current X * node matches, call self recursively to see whether the rest matches, X * and then act accordingly. In practice we make some effort to avoid X * recursion, in particular by going through "ordinary" nodes (that don't X * need to know whether the rest of the match failed) by a loop instead of X * by recursion. X */ Xstatic int /* 0 failure, 1 success */ Xregmatch(prog) Xchar *prog; X{ X register char *scan; /* Current node. */ X char *next; /* Next node. */ X extern char *strchr(); X X scan = prog; X#ifdef DEBUG X if (scan != NULL && regnarrate) X fprintf(stderr, "%s(\n", regprop(scan)); X#endif X while (scan != NULL) { X#ifdef DEBUG X if (regnarrate) X fprintf(stderr, "%s...\n", regprop(scan)); X#endif X next = regnext(scan); X X switch (OP(scan)) { X case BOL: X if (reginput != regbol) X return(0); X break; X case EOL: X if (*reginput != '\0') X return(0); X break; X case ANY: X if (*reginput == '\0') X return(0); X reginput++; X break; X case EXACTLY: { X register int len; X register char *opnd; X X opnd = OPERAND(scan); X /* Inline the first character, for speed. */ X if (*opnd != *reginput) X return(0); X len = strlen(opnd); X if (len > 1 && strncmp(opnd, reginput, len) != 0) X return(0); X reginput += len; X } X break; X case ANYOF: X if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL) X return(0); X reginput++; X break; X case ANYBUT: X if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL) X return(0); X reginput++; X break; X case NOTHING: X break; X case BACK: X break; X case OPEN+1: X case OPEN+2: X case OPEN+3: X case OPEN+4: X case OPEN+5: X case OPEN+6: X case OPEN+7: X case OPEN+8: X case OPEN+9: { X register int no; X register char *save; X X no = OP(scan) - OPEN; X save = reginput; X X if (regmatch(next)) { X /* X * Don't set startp if some later X * invocation of the same parentheses X * already has. X */ X if (regstartp[no] == NULL) X regstartp[no] = save; X return(1); X } else X return(0); X } X break; X case CLOSE+1: X case CLOSE+2: X case CLOSE+3: X case CLOSE+4: X case CLOSE+5: X case CLOSE+6: X case CLOSE+7: X case CLOSE+8: X case CLOSE+9: { X register int no; X register char *save; X X no = OP(scan) - CLOSE; X save = reginput; X X if (regmatch(next)) { X /* X * Don't set endp if some later X * invocation of the same parentheses X * already has. X */ X if (regendp[no] == NULL) X regendp[no] = save; X return(1); X } else X return(0); X } X break; X case BRANCH: { X register char *save; X X if (OP(next) != BRANCH) /* No choice. */ X next = OPERAND(scan); /* Avoid recursion. */ X else { X do { X save = reginput; X if (regmatch(OPERAND(scan))) X return(1); X reginput = save; X scan = regnext(scan); X } while (scan != NULL && OP(scan) == BRANCH); X return(0); X /* NOTREACHED */ X } X } X break; X case STAR: X case PLUS: { X register char nextch; X register int no; X register char *save; X register int min; X X /* X * Lookahead to avoid useless match attempts X * when we know what character comes next. X */ X nextch = '\0'; X if (OP(next) == EXACTLY) X nextch = *OPERAND(next); X min = (OP(scan) == STAR) ? 0 : 1; X save = reginput; X no = regrepeat(OPERAND(scan)); X while (no >= min) { X /* If it could work, try it. */ X if (nextch == '\0' || *reginput == nextch) X if (regmatch(next)) X return(1); X /* Couldn't or didn't -- back up. */ X no--; X reginput = save + no; X } X return(0); X } X break; X case END: X return(1); /* Success! */ X break; X default: X regerror("memory corruption"); X return(0); X break; X } X X scan = next; X } X X /* X * We get here only if there's trouble -- normally "case END" is X * the terminating point. X */ X regerror("corrupted pointers"); X return(0); X} X X/* X - regrepeat - repeatedly match something simple, report how many X */ Xstatic int Xregrepeat(p) Xchar *p; X{ X register int count = 0; X register char *scan; X register char *opnd; X X scan = reginput; X opnd = OPERAND(p); X switch (OP(p)) { X case ANY: X count = strlen(scan); X scan += count; X break; X case EXACTLY: X while (*opnd == *scan) { X count++; X scan++; X } X break; X case ANYOF: X while (*scan != '\0' && strchr(opnd, *scan) != NULL) { X count++; X scan++; X } X break; X case ANYBUT: X while (*scan != '\0' && strchr(opnd, *scan) == NULL) { X count++; X scan++; X } X break; X default: /* Oh dear. Called inappropriately. */ X regerror("internal foulup"); X count = 0; /* Best compromise. */ X break; X } X reginput = scan; X X return(count); X} X X/* X - regnext - dig the "next" pointer out of a node X */ Xstatic char * Xregnext(p) Xregister char *p; X{ X register int offset; X X if (p == ®dummy) X return(NULL); X X offset = NEXT(p); X if (offset == 0) X return(NULL); X X if (OP(p) == BACK) X return(p-offset); X else X return(p+offset); X} X X#ifdef DEBUG X XSTATIC char *regprop(); X X/* X - regdump - dump a regexp onto stdout in vaguely comprehensible form X */ Xvoid Xregdump(r) Xregexp *r; X{ X register char *s; X register char op = EXACTLY; /* Arbitrary non-END op. */ X register char *next; X extern char *strchr(); X X X s = r->program + 1; X while (op != END) { /* While that wasn't END last time... */ X op = OP(s); X#ifdef amiga X sprintf(pr_buf,"%2d%s", s-r->program, regprop(s)); /* Where, what. */ X tputs(pr_buf,1,ttputc); X#else X printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */ X#endif X next = regnext(s); X if (next == NULL) { /* Next ptr. */ X#ifdef amiga X sprintf(pr_buf,"(0)"); X tputs(pr_buf,1,ttputc); X#else X printf("(0)"); X#endif X } else { X#ifdef amiga X sprintf(pr_buf,"(%d)", (s-r->program)+(next-s)); X tputs(pr_buf,1,ttputc); X#else X printf("(%d)", (s-r->program)+(next-s)); X#endif X } X s += 3; X if (op == ANYOF || op == ANYBUT || op == EXACTLY) { X /* Literal string, where present. */ X while (*s != '\0') { X putchar(*s); X s++; X } X s++; X } X putchar('\n'); X } X X /* Header fields of interest. */ X if (r->regstart != '\0') { X#ifdef amiga X sprintf(pr_buf,"start `%c' ", r->regstart); X tputs(pr_buf,1,ttputc); X#else X printf("start `%c' ", r->regstart); X#endif X } X if (r->reganch) { X#ifdef amiga X sprintf(pr_buf,"anchored "); X tputs(pr_buf,1,ttputc); X#else X printf("anchored "); X#endif X } X if (r->regmust != NULL) { X#ifdef amiga X sprintf(pr_buf,"must have \"%s\"", r->regmust); X tputs(pr_buf,1,ttputc); X#else X printf("must have \"%s\"", r->regmust); X#endif X } X#ifdef amiga X sprintf(pr_buf,"\n"); X tputs(pr_buf,1,ttputc); X#else X printf("\n"); X#endif X} X X/* X - regprop - printable representation of opcode X */ Xstatic char * Xregprop(op) Xchar *op; X{ X register char *p; X static char buf[50]; X X (void) strcpy(buf, ":"); X X switch (OP(op)) { X case BOL: X p = "BOL"; X break; X case EOL: X p = "EOL"; X break; X case ANY: X p = "ANY"; X break; X case ANYOF: X p = "ANYOF"; X break; X case ANYBUT: X p = "ANYBUT"; X break; X case BRANCH: X p = "BRANCH"; X break; X case EXACTLY: X p = "EXACTLY"; X break; X case NOTHING: X p = "NOTHING"; X break; X case BACK: X p = "BACK"; X break; X case END: X p = "END"; X break; X case OPEN+1: X case OPEN+2: X case OPEN+3: X case OPEN+4: X case OPEN+5: X case OPEN+6: X case OPEN+7: X case OPEN+8: X case OPEN+9: X sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN); X p = NULL; X break; X case CLOSE+1: X case CLOSE+2: X case CLOSE+3: X case CLOSE+4: X case CLOSE+5: X case CLOSE+6: X case CLOSE+7: X case CLOSE+8: X case CLOSE+9: X sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE); X p = NULL; X break; X case STAR: X p = "STAR"; X break; X case PLUS: X p = "PLUS"; X break; X default: X regerror("corrupted opcode"); X break; X } X if (p != NULL) X (void) strcat(buf, p); X return(buf); X} X#endif X X/* X * The following is provided for those people who do not have strcspn() in X * their C libraries. They should get off their butts and do something X * about it; at least one public-domain implementation of those (highly X * useful) string routines has been published on Usenet. X */ X/* X * strcspn - find length of initial segment of s1 consisting entirely X * of characters not from s2 X */ X Xstatic int Xstrcspn(s1, s2) Xchar *s1; Xchar *s2; X{ X register char *scan1; X register char *scan2; X register int count; X X count = 0; X for (scan1 = s1; *scan1 != '\0'; scan1++) { X for (scan2 = s2; *scan2 != '\0';) /* ++ moved down. */ X if (*scan1 == *scan2++) X return(count); X count++; X } X return(count); X} END_OF_FILE if test 28412 -ne `wc -c <'regexp.c'`; then echo shar: \"'regexp.c'\" unpacked with wrong size! fi # end of 'regexp.c' fi if test -f 'std.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'std.c'\" else echo shar: Extracting \"'std.c'\" \(25716 characters\) sed "s/^X//" >'std.c' <<'END_OF_FILE' X#include <stdio.h> X#include <pwd.h> X#include <ctype.h> X Xextern char *malloc(); X X#ifndef MINIX X#ifndef MSDOS X#ifndef amiga X/* Minix is missing it */ X#include <sys/param.h> X#endif X#endif X#endif X X#include "server.h" X#include "config_std.h" X#include "std.h" X X#ifdef MINIX X#define clearerr(p) (((p)->_flags) &= ~_ERR) X#endif X X#ifdef amiga Xextern char pr_buf[]; Xextern ttputc (); X#endif X X#ifndef MAXPATHLEN X#define MAXPATHLEN 240 X#endif X Xextern NODE *hashfind(); X#ifndef amiga Xextern FILE *fopen(); X#endif Xextern char *index(), *rindex(); Xextern char *str_tstore(), *str_tpool(), *str_store(); Xextern char *strtok(), *strpbrk(); Xextern char *regex(), *regcmp(); X X#ifdef MAILCHOOSE Xextern int (*Massage)(); X#endif X X/* X global flags signifying options set X*/ X#define GF_ALL 1 /* -x option - scan everything */ X#define GF_SPEC 2 /* -n option(s) - user specified groups */ X#define GF_OVER 4 /* command line specification - overide marks */ X Xchar *Vns_version = "res1.1"; X Xstatic char *Onews, *Newsrc; Xstatic int Ntopt, Nntopt, Nwopt, Nnwopt; X Xstatic char *Wopt[NUMFILTER]; /* regular expressions for -w options */ Xstatic char *Topt[NUMFILTER]; /* for -t options */ Xstatic char *Negwopt[NUMFILTER]; /* for negated -w options */ Xstatic char *Negtopt[NUMFILTER]; /* for negated -t options */ X Xstatic char *Options[OPTLINES]; Xstatic int Max_name, Optlines; Xstatic unsigned Gflags = 0; Xstatic char **Active; Xstatic int Actnum; Xstatic char *Mailer, *Poster; X Xstatic char *RT_head = RTHEAD; Xstatic char *P_head = PHEAD; Xstatic char *M_head = MHEAD; Xstatic char *R_head = RHEAD; Xstatic char *TO_head = TOHEAD; Xstatic char *F_head = FHEAD; Xstatic char *FT_head = FTHEAD; Xstatic char *T_head = THEAD; Xstatic char *DIS_head = DISHEAD; Xstatic char *L_head = LHEAD; Xstatic char *N_head = NHEAD; X Xstatic char *Fpfix = FPFIX; X Xstatic void specmark (), mail_cmd (); X Xstatic g_dir (), emptyline (), fill_active (), art_active (), chkgroup (), X arg_opt (), newsrc_opt (), do_opt (), specfilter (), X findall (), digname (); Xstatic char *nfgets (), *mail_trim (); X X/* X** environment setup. X*/ Xvns_envir() X{ X char dbuf[MAXPATHLEN], *rcname; X char *vn_env(); X struct passwd *ptr, *getpwuid(); X#ifdef amiga X long fh; /* on the amiga creat or open locks a file */ X#endif X#ifdef MAILCHOOSE X int mail_prompt(); X X Massage = mail_prompt; X#endif X X ptr = getpwuid (getuid()); X X rcname = vn_env("MAILER",DEF_MAIL); X#ifdef INLETTER X sprintf(dbuf,"cat %%s | %s",rcname); X#else X /* used as a format string TWICE (%%%% -> %% -> %) */ X sprintf(dbuf,"cat %%%%s | %s %%s",rcname); X#endif X Mailer = str_store(dbuf); X rcname = vn_env("VNPOSTER",DEF_POST); X sprintf(dbuf,"%s %%s",rcname); X Poster = str_store(dbuf); X rcname = vn_env("NEWSRC",DEF_NEWSRC); X if (*rcname != DIRSEP) X { X#ifdef amiga X sprintf (dbuf, "%s%s",ptr->pw_dir,rcname); X#else X sprintf (dbuf, "%s%c%s",ptr->pw_dir,DIRSEP,rcname); X#endif X Newsrc = str_store (dbuf); X } X else X Newsrc = str_store (rcname); X X /* above logic guarantees that Newsrc contains a '/' */ X strcpy(dbuf,Newsrc); X#ifdef amiga X strcpy(rindex(dbuf,':')+1,"vnXXXXXX"); X#else X strcpy(rindex(dbuf,DIRSEP)+1,"vnXXXXXX"); X#endif X mktemp(dbuf); X Onews = str_store (dbuf); X X if (access (Newsrc,0) != 0) X#ifdef amiga X { /* we must close a file we creat otherwise it is locked and X inaccesible */ X fh = creat (Newsrc,0666); X if (fh > 0) X close (fh); X } X#else X creat (Newsrc,0666); X#endif X} X X/* X change directory to group X*/ Xvns_gset(grp) Xchar *grp; X{ X char dbuf [RECLEN]; X g_dir (grp,dbuf); X if (chdir(dbuf) < 0) X printex("can't change to newsgroup directory"); X} X X/* X g_dir converts newsgroup name to directory string X*/ Xstatic Xg_dir(s,t) Xchar *s,*t; X{ X char *ptr; X#ifdef amiga X char *spooldir, *nested; X spooldir = vn_env("NEWSDIR",NULL); X if (spooldir == NULL) X sprintf (t,"%s%s",SPOOLDIR,s); X else X sprintf (t,"%s%s",spooldir,s); X#else X sprintf (t,"%s%c%s",SPOOLDIR,DIRSEP,s); X#endif X#ifdef amiga X/* if AmigaUUCP and VNNESTED not defined or is "N" do not replace .'s X in groupnames */ X nested = vn_env("VNNESTED",NULL); X if (nested != NULL && *nested != 'N') X#endif X for (ptr=t+strlen(SPOOLDIR)+1; X (ptr = index(ptr,'.')) != NULL; X *ptr = DIRSEP) X ; X} X X/* X** myfind is used for hashfind() calls which aren't supposed to fail. X*/ Xstatic NODE * Xmyfind(name) Xchar *name; X{ X NODE *n; X X n = hashfind(name); X if (n == NULL) X printex("Unexpected table lookup failure"); X return (n); X} X Xvns_news(argc,argv,lfirst,nun) Xint argc; Xchar **argv; Xint *lfirst, *nun; X{ X FILE *fp; X static char marks[] = X { X NEWS_ON, NEWS_OFF, '\0' X }; X int line, len, num; X char buf [RECLEN], trail, optpflag, submark, *fret, *ptr; X X ++argv; X --argc; X X /* fill table with active newsgroups */ X fill_active (); X X if (argc > 0) X { X Gflags |= GF_OVER; X arg_opt(argc,argv,lfirst,nun); X optpflag = 'y'; X } X else X optpflag = 'n'; X X if ((fp = fopen (Newsrc,"r")) == NULL) X printex ("can't open %s for reading",Newsrc); X X Optlines = 0; X X for (line = 1; (fret = fgets(buf,RECLEN-1,fp)) != NULL && emptyline(buf) == 1; ++line) X ; X if (fret != NULL && strncmp (buf,"options",7) == 0) X { X Options[0] = str_store(buf); X Optlines = 1; X trail = buf [strlen(buf)-2]; X for ( ; (fret = fgets(buf,RECLEN-1,fp)) != NULL; ++line) X { X if (trail != '\\' && buf[0] != ' ' && buf[0] != '\t') X break; X if (Optlines >= OPTLINES) X printex ("%s - too many option lines (%d allowed)",Newsrc,OPTLINES); X Options[Optlines] = str_store(buf); X ++Optlines; X if ((len = strlen(buf)) >= 2 && buf[len-2] != '\\') X trail = buf[len-2]; X else X trail = '\0'; X } X } X X /* do the options from the newsrc file if there weren't command line args */ X if (Optlines > 0 && optpflag == 'n') X newsrc_opt (lfirst,nun); X X for ( ; fret != NULL; ++line, fret = fgets(buf,RECLEN-1,fp)) X { X if (emptyline(buf) == 1) X continue; X if ((ptr = strpbrk(buf,marks)) == NULL) X { X fprintf (stderr,"\nwarning: line %d of %s (%s) - bad syntax\n", X line,Newsrc,buf); X continue; X } X submark = *ptr; X *ptr = '\0'; X ++ptr; X num = 0; X for (ptr = strtok(ptr," ,-\n"); ptr != NULL; ptr = strtok(NULL," ,-\n")) X { X len = atoi (ptr); X for ( ; *ptr >= '0' && *ptr <= '9'; ++ptr) X ; X if (*ptr != '\0' || len < num) X { X num = -1; X fprintf (stderr,"\nwarning: line %d of %s (%s) - bad syntax\n", X line,Newsrc,buf); X break; X } X num = len; X } X if (num < 0) X continue; X chkgroup (buf,submark,num,0); X } X fclose (fp); X X /* now take care of groups not specified in .newsrc */ X art_active(); X X /* free up the option string storage */ X for (num=0; num < Ntopt; ++num) X regfree (Topt[num]); X for (num=0; num < Nwopt; ++num) X regfree (Wopt[num]); X for (num=0; num < Nntopt; ++num) X regfree (Negtopt[num]); X for (num=0; num < Nnwopt; ++num) X regfree (Negwopt[num]); X Ntopt = Nwopt = Nntopt = Nnwopt = 0; X X /* free the active list */ X free ((char *) Active); X} X Xstatic Xemptyline(s) Xchar *s; X{ X while (isspace(*s)) X ++s; X if (*s == '\0') X return (1); X return (0); X} X X/* X fill hash table from active news group list X This is needed to be able to process options X before scanning user order. Constructs an array X of active newsgroup names for the rest of vns_nws(). X*/ Xstatic Xfill_active () X{ X FILE *f; X char *nread, act_rec[RECLEN], *actfile; X int num,lownum,rcount; X X Max_name = 0; X actfile = vn_env("ACTFILE", ACTFILE); X if ((f = fopen (actfile,"r")) == NULL) X printex ("couldn't open %s\n",actfile); X X /* X ** we do things this way so that we only examine active records X ** once, minimizing the window where changes could screw us up X ** at the cost of possibly alloc'ing a few extra bytes. We start X ** with a count of one to have a positive rcount for alloc. X */ X for(rcount=1; fgets(act_rec, RECLEN-1, f) != NULL; ++rcount) X ; X if ((Active = (char **) malloc(rcount*sizeof(char *))) == NULL) X printex("Memory allocation failure"); X rewind(f); X X Actnum = 0; X while (Actnum < rcount && fgets(act_rec, RECLEN-1, f) != NULL) X { X if (strtok (act_rec," \n") == NULL) X continue; X nread = strtok (NULL, " \n"); X if (nread != NULL) X num = atoi(nread); X else X num = 0; X nread = strtok (NULL, " \n"); X if (nread != NULL) X lownum = atoi(nread); X else X lownum = 0; X if (lownum > 0) X --lownum; X if (strlen(act_rec) > Max_name) X Max_name = strlen(act_rec); X /* enter newsgroup, point to permanent copy of name */ X hashenter (act_rec, num, lownum); X Active[Actnum] = (myfind(act_rec))->nd_name; X ++Actnum; X } X X fclose (f); X} X X/* X check active newsgroups not mentioned in NEWSRC file X (SFLG_SCAN not set) X*/ Xstatic Xart_active () X{ X int i; X NODE *ptr; X X for( i=0; i < Actnum; ++i) X { X ptr = myfind(Active[i]); X if ((ptr->state & SFLG_SCAN) == 0) X chkgroup (ptr->nd_name, NEWS_ON, 0, 1); X } X} X X/* X check group for new articles: X s - group X c - subscription indicator from NEWSRC X n - number read X new - new newsgroup flag X*/ Xstatic Xchkgroup (s,c,n,new) Xchar *s,c; Xint n; Xint new; X{ X NODE *ptr; X char sub; X int nrd; X int lowart; X int st; X X if ((ptr = hashfind(s)) != NULL && (ptr->state & SFLG_SCAN) == 0) X { X ptr->state |= SFLG_SCAN; X X#ifdef SYN_CHECK X /* if "read" more than exist, reset */ X if (n > ptr->highnum) X { X n = ptr->highnum - SYN_SETBACK; X fgprintf("%s: .newsrc out of synch, resetting\n",s); X } X#endif X lowart = ptr->lownum; X if (n < ptr->lownum) X n = ptr->lownum; X X nrd = n; X sub = c; X X /* X ** scan decision is rather complex, since GF_ALL setting X ** overides "n" value, GF_SPEC indicates SFLG_SPEC flag used. X ** if GF_OVER set, SFLG_SPEC overides subscription mark, else X ** SFLG_SPEC AND subscribed is neccesary. X */ X if ((Gflags & GF_SPEC) != 0) X { X if ((ptr->state & SFLG_SPEC) == 0) X c = NEWS_OFF; X else X { X if ((Gflags & GF_OVER) != 0) X c = NEWS_ON; X } X } X if ((Gflags & GF_ALL) != 0) X n = lowart; X fw_group(s, new, sub == NEWS_ON, nrd, c == NEWS_ON); X if (c == NEWS_ON && ptr->highnum > n) X { X st = outgroup (s,n,ptr->highnum); X if (st > nrd) X fw_chg(new, sub == NEWS_ON, st, c == NEWS_ON); X } X } X} X X/* X vns_write writes the .newsrc file X*/ Xvns_write(news,ncount) XNODE **news; Xint ncount; X{ X FILE *fp; X NODE *p; X char c; X int i,rc; X X if (link(Newsrc,Onews) < 0) X printex ("can't backup %s to %s before writing",Newsrc,Onews); X X if (unlink(Newsrc) < 0 || (fp = fopen(Newsrc,"w")) == NULL) X printex ("can't open %s for writing (backed up in %s)",Newsrc,Onews); X else X { X clearerr(fp); X for (i=0; (rc = ferror(fp)) == 0 && i < Optlines; ++i) X fprintf (fp,"%s",Options[i]); X for (i=0; rc == 0 && i < ncount; ++i) X { X p = news[i]; X if ((p->flags & FLG_SUB) == 0) X c = NEWS_OFF; X else X c = NEWS_ON; X#ifdef OLDRC X fprintf (fp,"%s%c %d\n",p->nd_name,c,p->rdnum); X#else X if (p->rdnum > 0) X fprintf(fp,"%s%c 1-%d\n",p->nd_name,c,p->rdnum); X else X fprintf(fp,"%s%c 0\n",p->nd_name,c); X#endif X rc = ferror(fp); X } X fclose (fp); X if (rc != 0) X printex ("write of %s failed, old copy stored in %s",Newsrc,Onews); X else X unlink (Onews); X } X} X X/* X arg_opt must be called prior to option scanning, since X it uses the options array. This is a bit of a kludge, X but it saves a bunch of work. NOTE - no command name argument X*/ Xstatic Xarg_opt (argc,argv,lfirst,nun) Xint argc; Xchar **argv; Xint *lfirst, *nun; X{ X if (argc > OPTLINES) X printex ("too many command line options (%d allowed)\n",OPTLINES); X for (Optlines=0; Optlines < argc; ++Optlines) X { X Options[Optlines] = *argv; X ++argv; X } X newsrc_opt(lfirst,nun); X} X X/* X option setting routine: X sets global flags: GF_ALL for -x option GF_SPEC for -n. X sets up filter array for article scanning X*/ Xstatic Xnewsrc_opt(lfirst,nun) Xint *lfirst, *nun; X{ X int i; X char curopt,tmp[RECLEN],*tok; X X *nun = *lfirst = 0; X Ntopt = Nwopt = Nnwopt = Nntopt = 0; X curopt = '\0'; X for (i=0; i < Optlines; ++i) X { X strcpy(tmp,Options[i]); X for (tok = strtok(tmp,",\\ \t\n"); tok != NULL; tok = strtok(NULL,",\\ \t\n")) X { X if (*tok != '-') X do_opt (curopt,tok); X else X { X for (++tok; index("nwt",*tok) == NULL; ++tok) X { X /* options with no strings */ X switch(*tok) X { X case 'S': X Gflags &= ~GF_OVER; X break; X case '%': X *lfirst = 1; X break; X case 'U': X *nun = 1; X break; X#ifdef OLDRC X case 'i': X /* Treat "-i" as synonym for "-x" */ X#endif X case 'x': X Gflags |= GF_ALL; X default: X break; X } X } X curopt = *tok; X if (*(++tok) != '\0') X do_opt (curopt,tok); X } X } X } X} X X/* do_opt is for options with strings attached */ Xstatic Xdo_opt (opt,str) Xchar opt, *str; X{ X switch (opt) X { X case 'n': X Gflags |= GF_SPEC; X specmark(str); X break; X case 'w': X specfilter (FIL_AUTHOR,str); X break; X case 't': X specfilter (FIL_TITLE,str); X break; X default: X#ifdef OLDRC X Gflags |= GF_SPEC; /* Assume anything else is newsgroup */ X specmark(str); X#endif X break; X } X} X Xstatic Xspecfilter (comp,str) Xchar comp,*str; X{ X int *count; X char **rex; X X /* X ** we may set rex one past end of array. we will error before X ** referencing it if that's the case, however. X */ X if (*str == '!') X { X if (comp == FIL_TITLE) X { X count = &Nntopt; X rex = Negtopt + *count; X } X else X { X count = &Nnwopt; X rex = Negwopt + *count; X } X ++str; X } X else X { X if (comp == FIL_TITLE) X { X count = &Ntopt; X rex = Topt + *count; X } X else X { X count = &Nwopt; X rex = Wopt + *count; X } X } X if (*count >= NUMFILTER) X printex ("too many %c options, %d allowed",comp,NUMFILTER); X if ((*rex = regcmp(str,(char *) 0)) == NULL) X printex ("%c option regular expression syntax: %s",comp,str); X ++(*count); X} X X/* X handle the newsgroup specification string. X ("all" convention - braack!!!) X*/ Xstatic Xvoid Xspecmark (s) Xchar *s; X{ X unsigned ormask,andmask; X int i,len; X char *ptr,*re,pattern[RECLEN]; X NODE *nptr; X X if (*s == '!') X { X ++s; X ormask = 0; X andmask = ~SFLG_SPEC; X if (*s == '\0') X return; X } X else X { X ormask = SFLG_SPEC; X andmask = 0xffff; X } X X /* convert "all" not bounded by alphanumerics to ".*". ".all" becomes ".*" */ X for (ptr = s; (len = findall(ptr)) >= 0; ptr += len+1) X { X if (len > 0 && isalnum (s[len-1])) X continue; X if (isalnum (s[len+3])) X continue; X if (len > 0 && s[len-1] == '.') X { X --len; X strcpy (s+len,s+len+1); X } X s[len] = '.'; X s[len+1] = '*'; X strcpy (s+len+2,s+len+3); X } X X /* now use regular expressions */ X sprintf (pattern,"^%s$",s); X if ((re = regcmp(pattern,(char *) 0)) == NULL) X printex ("n option regular expression syntax: %s",s); X for (i=0; i < Actnum; ++i) X { X nptr = myfind(Active[i]); X if (regex(re,nptr->nd_name) != NULL) X { X nptr->state |= ormask; X nptr->state &= andmask; X } X } X regfree (re); X} X Xstatic Xfindall (s) Xchar *s; X{ X int len; X for (len=0; *s != '\0'; ++s,++len) X { X if (*s == 'a' && strncmp(s,"all",3) == 0) X return (len); X } X return (-1); X} X Xstatic Xgrp_indic (s,ok) Xchar *s; Xint ok; X{ X if (ok) X fgprintf(" %s\n",s); X else X fgprintf(" %s - Can't access spool directory\n",s); X} X X/* X enter newsgroup articles. X all articles between low and hi are to be included. X X Returns the highest number less than an OPENED (not neccesarily X accepted) article to allow caller to revise "articles read" X number beyond non-existent articles. X*/ Xoutgroup (s,low,hi) Xchar *s; Xint low,hi; X{ X int i; X char subj[RECLEN], lines[RECLEN], auth[RECLEN], gd[RECLEN]; X int ret,op; X X if ((hi-low) > MAXARTRANGE) X low = hi - MAXARTRANGE; X X ret = low; X op = 1; X X g_dir(s,gd); X if (chdir(gd) < 0) X { X grp_indic(s,0); X return (ret); X } X grp_indic(s,1); X for (i=low+1; i <= hi; ++i) X { X if (digname(i,subj,lines,auth,&op) >= 0) X { X fw_art(i,subj,lines,auth); X } X else X { X if (op) X ret = i; X } X } X X return(ret); X} X X/* X** open article and interpret options, if any. The op parameter is set X** to ZERO if and only if an article is opened. Used above as a flag to X** indicate no articles opened yet. X*/ Xstatic digname (n, subj, lines, auth, op) Xint n; Xchar *subj, *lines, *auth; Xint *op; X{ X int i,j; X FILE *fp; X char t[RECLEN]; X X /* open article */ X sprintf (t,"%d", n); X if ((fp = fopen(t,"r")) == NULL) X return (-1); X *op = 0; X X /* get subject, from and lines by reading article */ X subj[0] = lines[0] = auth[0] = '?'; X subj[1] = lines[1] = auth[1] = '\0'; X for (i = 0; i < HDR_LINES && nfgets(t,RECLEN-1,fp) != NULL; ++i) X { X if (index(CHFIRST,t[0]) == NULL) X continue; X t[strlen(t) - 1] = '\0'; X if (strncmp(T_head,t,THDLEN) == 0) X { X for (j=0; j < Nntopt; ++j) X { X if (regex(Negtopt[j],t+THDLEN) != NULL) X { X fclose(fp); X return(-1); X } X } X if (Ntopt > 0) X { X for (j=0; j < Ntopt; ++j) X { X if (regex(Topt[j],t+THDLEN) != NULL) X break; X } X if (j >= Ntopt) X { X fclose(fp); X return(-1); X } X } X strcpy(subj,t+THDLEN); X continue; X } X if (strncmp(F_head,t,FHDLEN) == 0) X { X for (j=0; j < Nnwopt; ++j) X { X if (regex(Negwopt[j],t+FHDLEN) != NULL) X { X fclose(fp); X return(-1); X } X } X if (Nwopt > 0) X { X for (j=0; j < Nwopt; ++j) X { X if (regex(Wopt[j],t+FHDLEN) != NULL) X break; X } X if (j >= Nwopt) X { X fclose(fp); X return(-1); X } X } X strcpy(auth,t+FHDLEN); X continue; X } X if (strncmp(L_head,t,LHDLEN) == 0) X { X strcpy(lines,t+LHDLEN); X break; X } X } X X fclose (fp); X X /* reject empty or 1 line files */ X if (i < 2) X return (-1); X X return (0); X} X X/* X** special fgets for reading header lines, which unfolds continued lines X** and throws away trailing stuff on buffer overflow. X*/ Xstatic char * Xnfgets(buf, size, fp) Xchar *buf; Xint size; XFILE *fp; X{ X register int c; X X while (!feof(fp)) X { X if ((c = getc(fp)) == '\n') X { X if ((c = getc(fp)) == '\t' || c == ' ') X continue; X ungetc(c, fp); X *buf = '\n'; X ++buf; X *buf = '\0'; X ++buf; X return (buf); X } X X /* prevent "terminal bombs" */ X if (c < ' ' || c == '\177') X { X switch(c) X { X case '\r': X case '\010': X case '\07': X break; X case '\177': X c = '~'; X break; X case '\t': X c = ' '; X break; X default: X if (size > 1) X { X *buf = '^'; X ++buf; X --size; X } X c += 'A' - 1; X break; X } X } X X if (size > 0) X { X *buf = c; X ++buf; X --size; X } X if (c == '\r') X { X if ((c = getc(fp)) != '\n') X { X ungetc(c, fp); X continue; X } X if ((c = getc(fp)) != ' ' && c != '\t') X { X *buf = '\0'; X ++buf; X ungetc(c, fp); X return (buf); X } X --buf; X ++size; X continue; X } X } X X *buf = '\0'; X ++buf; X return (NULL); X} X Xstatic char *Mail[2], *Show[6], *Post[4]; Xstatic char *Priv[8]; Xstatic char *Pool = NULL; X XFILE * Xvns_aopen(art,hdr) Xint art; XARTHEADER *hdr; X{ X char buf[RECLEN]; X char *dist, *reply, *from, *ngrp, *flto, *path, *resubj; X FILE *fp; X int n; X X dist = resubj = path = reply = from = ngrp = flto = NULL; X X sprintf(buf,"%d",art); X if ((fp = fopen(buf,"r")) == NULL) X return(NULL); X X /* X ** we only really need a lot extra if MAILCHOOSE, but allocating X ** a temporary array of pointers isn't that much. Similarly, a X ** few assignments, and the "Priv" declaration are only needed X ** with some define settings. Not worth ifdef'ing. X */ X Pool = str_tpool(100); X X hdr->artid = "<some article>"; X hdr->from = "<somebody>"; X hdr->priv = Priv; X hdr->postcmd = Poster; X hdr->mail = Mail; X hdr->show = Show; X hdr->post = Post; X hdr->priv_num = hdr->show_num = hdr->post_num = hdr->mail_num = 0; X X /* for conditional is abnormal - expected exit is break */ X for (n=0; n < HDR_LINES && fgets(buf,RECLEN-1,fp) != NULL; ++n) X { X /* bail out at first non-header line */ X if (buf[0] == '\n') X break; X if (strncmp(buf,RT_head,RTHDLEN) == 0) X { X buf [strlen(buf)-1] = '\0'; X reply = str_tstore(Pool,buf+RTHDLEN); X continue; X } X if (strncmp(buf,P_head,PHDLEN) == 0) X { X buf [strlen(buf)-1] = '\0'; X path = str_tstore(Pool,buf+PHDLEN); X continue; X } X if (strncmp(buf,DIS_head,DISHDLEN) == 0) X { X buf [strlen(buf)-1] = '\0'; X dist = str_tstore(Pool,buf); X continue; X } X if (strncmp(buf,M_head,MHDLEN) == 0) X { X buf [strlen(buf)-1] = '\0'; X hdr->artid = str_tstore(Pool,buf+MHDLEN); X continue; X } X if (strncmp(buf,F_head,FHDLEN) == 0) X { X buf [strlen(buf)-1] = '\0'; X (hdr->show)[hdr->show_num] = str_tstore(Pool,buf); X from = hdr->from = (hdr->show)[hdr->show_num]+FHDLEN; X ++(hdr->show_num); X continue; X } X if (strncmp(buf,T_head,THDLEN) == 0) X { X buf [strlen(buf)-1] = '\0'; X (hdr->show)[hdr->show_num] = str_tstore(Pool,buf); X if (strncmp(buf+THDLEN,Fpfix,FPFLEN) != 0) X { X sprintf(buf,"%s%s%s",T_head,Fpfix, X ((hdr->show)[hdr->show_num])+THDLEN); X resubj = str_tstore(Pool,buf); X } X else X resubj = (hdr->show)[hdr->show_num]; X ++(hdr->show_num); X continue; X } X if (strncmp(buf,N_head,NHDLEN) == 0) X { X buf [strlen(buf)-1] = '\0'; X X /* if multiple newsgroups, include in "show" */ X if (index(buf,',') != NULL) X { X (hdr->show)[hdr->show_num] = str_tstore(Pool,buf); X ngrp = (hdr->show)[hdr->show_num] + NHDLEN; X ++(hdr->show_num); X } X else X ngrp = str_tstore(Pool,buf+NHDLEN); X continue; X } X if (strncmp(buf,FT_head,FTHDLEN) == 0) X { X buf [strlen(buf)-1] = '\0'; X (hdr->show)[hdr->show_num] = str_tstore(Pool,buf); X flto = (hdr->show)[hdr->show_num] + FTHDLEN; X ++(hdr->show_num); X continue; X } X if (strncmp(buf,L_head,LHDLEN) == 0) X { X buf [strlen(buf)-1] = '\0'; X hdr->lines = atoi(buf+LHDLEN); X (hdr->show)[hdr->show_num] = str_tstore(Pool,buf); X ++(hdr->show_num); X continue; X } X } X X hdr->hlines = n; X X#ifdef MAILCHOOSE X (hdr->priv)[hdr->priv_num] = resubj; X ++(hdr->priv_num); X if (reply != NULL) X { X (hdr->priv)[hdr->priv_num] = mail_trim(reply); X ++(hdr->priv_num); X } X if (from != NULL) X { X (hdr->priv)[hdr->priv_num] = mail_trim(from); X ++(hdr->priv_num); X } X if (path != NULL) X { X (hdr->priv)[hdr->priv_num] = mail_trim(path); X ++(hdr->priv_num); X } X#else X#ifdef MAILSMART X if (reply == NULL) X if (from != NULL) X reply = from; X else X { X if (path != NULL) X reply = path; X } X#else X if (path != NULL) X reply = path; X#endif X if (reply != NULL) X reply = mail_trim(reply); X mail_cmd(hdr,reply,resubj); X#endif /* MAILCHOOSE */ X X if (flto == NULL) X { X if ((flto = ngrp) == NULL) X flto = "group.unknown"; X } X ngrp = rindex(flto,'.'); X X if (strncmp("mod.",flto,4) == 0 || X (ngrp != NULL && strcmp(".announce",ngrp) == 0)) X { X sprintf(buf,"Cannot post a follow-up to \"%s\", reply with mail to moderator",flto); X hdr->post_err = str_tstore(Pool,buf); X return (fp); X } X X if (ngrp != NULL && strcmp(ngrp,".general") == 0) X { X *ngrp = '\0'; X sprintf(buf,"%s%s.followup",N_head,flto); X } X else X sprintf(buf,"%s%s",N_head,flto); X flto = str_tstore(Pool,buf); X X hdr->post_err = NULL; X X if (resubj != NULL) X { X (hdr->post)[hdr->post_num] = resubj; X ++(hdr->post_num); X } X X (hdr->post)[hdr->post_num] = flto; X ++(hdr->post_num); X X sprintf(buf,"%s%s",R_head,hdr->artid); X (hdr->post)[hdr->post_num] = str_tstore(Pool,buf); X ++(hdr->post_num); X X if (dist != NULL) X { X (hdr->post)[hdr->post_num] = dist; X ++(hdr->post_num); X } X X return (fp); X} X X#ifdef MAILCHOOSE X/* X** routine to prompt user for mail path approval X*/ Xstatic Xmail_prompt(hdr) XARTHEADER *hdr; X{ X int i; X char buf[RECLEN],*ptr; X X tty_set(SAVEMODE); X for (i=1; i < hdr->priv_num; ++i) X { X#ifdef amiga X sprintf(pr_buf,"%d - %s\n",i,(hdr->priv)[i]); X tputs(pr_buf,1,ttputc); X#else X printf("%d - %s\n",i,(hdr->priv)[i]); X#endif X } X#ifdef amiga X sprintf(pr_buf,"\nType number to choose one of the above, or input address: "); X tputs(pr_buf,1,ttputc); X#else X printf("\nType number to choose one of the above, or input address: "); X#endif X fgets(buf,RECLEN-1,stdin); X tty_set(RESTORE); X X ptr = strtok(buf," \t\n"); X if (ptr == NULL) X ptr = ""; X X i = strlen(ptr); X if (i == 1) X { X i = atoi(ptr); X if (i > 0 && i <= hdr->priv_num) X ptr = (hdr->priv)[i]; X i = 1; X } X X /* X ** If the user keeps cycling through here on the same article, X ** we will eventually run out of strings. We made Pool large X ** enough to make it unlikely (user will have to retry about 80 X ** times without switching articles). Hardly elegant, but should X ** be sufficient. X */ X if (i > 1 && hdr->priv_num < 8) X { X (hdr->priv)[hdr->priv_num] = str_tstore(Pool,ptr); X ++(hdr->priv_num); X } X mail_cmd(hdr,ptr,(hdr->priv)[0]); X} X#endif X X/* X** trim () off potential mail address, and make copy if needed. X** addr must be allocated string. X*/ Xstatic char * Xmail_trim(addr) Xchar *addr; X{ X char buf[RECLEN]; X char *ptr; X X if (index(addr,'(') == NULL) X return(addr); X X strcpy(buf,addr); X ptr = index(buf,'('); X for (--ptr; *ptr == ' ' || *ptr == '\t'; --ptr) X ; X ++ptr; X *ptr = '\0'; X return (str_tstore(Pool,buf)); X} X X/* X** format mail command. Subj must point to allocated string. X*/ Xstatic Xvoid Xmail_cmd(hdr,addr,subj) XARTHEADER *hdr; Xchar *addr, *subj; X{ X char buf[RECLEN]; X X if (addr == NULL || *addr == '\0') X { X hdr->mail_err = "No address"; X return; X } X X hdr->mail_err = NULL; X ; X X#ifdef INLETTER X hdr->mailcmd = Mailer; X sprintf(buf,"%s%s",TO_head,addr); X (hdr->mail)[0] = str_tstore(Pool,buf); X hdr->mail_num = 1; X#else X sprintf(buf,Mailer,addr); X hdr->mailcmd = str_tstore(Pool,buf); X hdr->mail_num = 0; X#endif X if (subj != NULL) X { X (hdr->mail)[hdr->mail_num] = subj; X ++(hdr->mail_num); X } X} X Xvns_aclose(fp) XFILE *fp; X{ X if (Pool != NULL) X str_tfree(Pool); X Pool = NULL; X fclose(fp); X} X X/* X** we don't use the count / name / mode arguments because this doesn't X** implement any fancy article massaging X*/ Xvoid Xvns_asave(art,fp) Xint art; XFILE *fp; X{ X char buf[RECLEN]; X FILE *fin; X X sprintf(buf,"%d",art); X if ((fin = fopen(buf,"r")) == NULL) X return; X X while (fgets(buf,RECLEN-1,fin) != NULL) X fputs(buf,fp); X fclose(fin); X} X Xvns_exit() X{ X} END_OF_FILE if test 25716 -ne `wc -c <'std.c'`; then echo shar: \"'std.c'\" unpacked with wrong size! fi # end of 'std.c' fi echo shar: End of archive 6 \(of 6\). cp /dev/null ark6isdone MISSING="" for I in 1 2 3 4 5 6 ; do if test ! -f ark${I}isdone ; then MISSING="${MISSING} ${I}" fi done if test "${MISSING}" = "" ; then echo You have unpacked all 6 archives. rm -f ark[1-9]isdone else echo You still need to unpack the following archives: echo " " ${MISSING} fi ## End of shell archive. exit 0 -- Mail submissions (sources or binaries) to <amiga@uunet.uu.net>. Mail comments to the moderator at <amiga-request@uunet.uu.net>. Post requests for sources, and general discussion to comp.sys.amiga.misc.