From:	MERC::"uunet!WKUVX1.BITNET!FILESERV-Mgr" 12-JUN-1992 15:44:23.75
To:	galaxy::gleeve
CC:	
Subj:	FGREP.03_OF_06

-+-+-+-+-+-+-+-+ START OF PART 3 -+-+-+-+-+-+-+-+
XArthur David Olson contributed code that finds fixed strings for
Xthe aforementioned BMG search for a large class of regexps.
X.sk
XRichard Stallman wrote the backtracking regexp matcher that is
Xused for \digit backreferences, as well as the getopt that
Xis provided for 4.2BSD sites.  The backtracking matcher was
Xoriginally written for GNU Emacs.
X.sk
XD. A. Gwyn wrote the C alloca emulation that is provided so
XSystem V machines can run this program.  (Alloca is used only
Xby RMS' backtracking matcher, and then only rarely, so there
Xis no loss if your machine doesn't have a "real" alloca.)
X.sk
XScott Anderson and Henry Spencer designed the regression tests
Xused in the "regress" script.
X.sk
XPaul Placeway wrote the original version of this manual page.
X.sk
XHunter Goatley ported grep 1.6 and fgrep 1.1 to VMS and produced the on-line
Xhelp for VMS.
X.sk;.indent -4
X2 Examples
X.br;
X.literal
X1.  $ grep int *.c
X.end literal
X.sk
XIn this example, all files with the extension .C will be searched for the
Xstring "int".
X.sk
X.literal
X2.  $ grep -i "`5E..int" *.c
X.end literal
X.sk
XIn this example, all files with the extension .C are searched for lines that
Xbegin with any two characters followed by "int".  The "-i" option tells grep
Xto ignore case.
X.sk
X.literal
X3.  $ grep -ih "`5E..int" *.c
X.end literal
X.sk
XIn this example, the "h" option is specified to prevent grep from displaying
Xthe names of matching files.
X.sk
X.literal
X4.  $ create tmp.tmp
X    search-string1
X    search-string2
X    `5EZ
X    $ fgrep -f tmp.tmp *.c
X.end literal
X.sk
XIn this example, a file containing more than one search string is created
Xand used as input to fgrep.
$ CALL UNPACK [.FGREP011]GREP.RNH;1 290734874
$ create 'f'
X/* kwset.c - search for any of a set of keywords.
X   Copyright 1989 Free Software Foundation
X`09`09  Written August 1989 by Mike Haertel.
X
X   This program is free software; you can redistribute it and/or modify
X   it under the terms of the GNU General Public License as published by
X   the Free Software Foundation; either version 1, or (at your option)
X   any later version.
X
X   This program is distributed in the hope that it will be useful,
X   but WITHOUT ANY WARRANTY; without even the implied warranty of
X   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X   GNU General Public License for more details.
X
X   You should have received a copy of the GNU General Public License
X   along with this program; if not, write to the Free Software
X   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X
X   The author may be reached (Email) at the address mike@ai.mit.edu,
X   or (US mail) as Mike Haertel c/o Free Software Foundation. */
X
X#include "std.h"
X
X/* The algorithm implemented by these routines bears a startling resemblence
X   to one discovered by Beate Commentz-Walter, although it is not identical.
X   See "A String Matching Algorithm Fast on the Average," Technical Report,
X   IBM-Germany, Scientific Center Heidelberg, Tiergartenstrasse 15, D-6900
X   Heidelberg, Germany.  See also Aho, A.V., and M. Corasick, "Efficient
X   String Matching:  An Aid to Bibliographic Search," CACM June 1975,
X   Vol. 18, No. 6, which describes the failure function used below. */
X
X#include "kwset.h"
X#include "obstack.h"
X
X#define NCHAR (UCHAR_MAX + 1)
X#define obstack_chunk_alloc malloc
X#define obstack_chunk_free free
X
X/* Balanced tree of edges and labels leaving a given trie node. */
Xstruct tree
X`7B
X  struct tree *llink;`09`09/* Left link; MUST be first field. */
X  struct tree *rlink;`09`09/* Right link (to larger labels). */
X  struct trie *trie;`09`09/* Trie node pointed to by this edge. */
X  unsigned char label;`09`09/* Label on this edge. */
X  char balance;`09`09`09/* Difference in depths of subtrees. */
X`7D;
X
X/* Node of a trie representing a set of reversed keywords. */
Xstruct trie
X`7B
X  unsigned int accepting;`09/* Word index of accepted word, or zero. */
X  struct tree *links;`09`09/* Tree of edges leaving this node. */
X  struct trie *parent;`09`09/* Parent of this node. */
X  struct trie *next;`09`09/* List of all trie nodes in level order. */
X  struct trie *fail;`09`09/* Aho-Corasick failure function. */
X  int depth;`09`09`09/* Depth of this node from the root. */
X  int shift;`09`09`09/* Shift function for search failures. */
X  int maxshift;`09`09`09/* Max shift of self and descendents. */
X`7D;
X
X/* Structure returned opaquely to the caller, containing everything. */
Xstruct kwset
X`7B
X  struct obstack obstack;`09/* Obstack for node allocation. */
X  int words;`09`09`09/* Number of words in the trie. */
X  struct trie *trie;`09`09/* The trie itself. */
X  int mind;`09`09`09/* Minimum depth of an accepting node. */
X  int maxd;`09`09`09/* Maximum depth of any node. */
X  int delta`5BNCHAR`5D;`09`09/* Delta table for rapid search. */
X  struct trie *next`5BNCHAR`5D;`09/* Table of children of the root. */
X  const char *trans;`09`09/* Character translation table. */
X`7D;
X
X/* Allocate and initialize a keyword set object, returning an opaque
X   pointer to it.  Return NULL if memory is not available. */
Xkwset_t
XDEFUN(kwsalloc, (trans), const char *trans)
X`7B
X  struct kwset *kwset;
X
X  kwset = (struct kwset *) malloc(sizeof (struct kwset));
X  if (!kwset)
X    return NULL;
X
X  obstack_init(&kwset->obstack);
X  kwset->words = 0;
X  kwset->trie
X    = (struct trie *) obstack_alloc(&kwset->obstack, sizeof (struct trie));
X  if (!kwset->trie)
X    `7B
X      kwsfree((kwset_t) kwset);
X      return NULL;
X    `7D
X  kwset->trie->accepting = 0;
X  kwset->trie->links = NULL;
X  kwset->trie->parent = NULL;
X  kwset->trie->next = NULL;
X  kwset->trie->fail = NULL;
X  kwset->trie->depth = 0;
X  kwset->trie->shift = 0;
X  kwset->mind = INT_MAX;
X  kwset->maxd = -1;
X  kwset->trans = trans;
X
X  return (kwset_t) kwset;
X`7D
X
X/* Add the given string to the contents of the keyword set.  Return NULL
X   for success, an error message otherwise. */
Xconst char *
XDEFUN(kwsincr, (kws, text, len),
X      kwset_t kws AND const char *text AND size_t len)
X`7B
X  struct kwset *kwset;
X  register struct trie *trie;
X  register unsigned char label;
X  register struct tree *link;
X  register int depth;
X  struct tree *links`5B12`5D;
X  enum `7B L, R `7D dirs`5B12`5D;
X  struct tree *t, *r, *l, *rl, *lr;
X
X  kwset = (struct kwset *) kws;
X  trie = kwset->trie;
X  text += len;
X
X  /* Descend the trie (built of reversed keywords) character-by-character,
X     installing new nodes when necessary. */
X  while (len--)
X    `7B
X      label = kwset->trans ? kwset->trans`5B(unsigned char) *--text`5D : *--
Vtext;
X
X      /* Descend the tree of outgoing links for this trie node,
X`09 looking for the current character and keeping track
X`09 of the path followed. */
X      link = trie->links;
X      links`5B0`5D = (struct tree *) &trie->links;
X      dirs`5B0`5D = L;
X      depth = 1;
X
X      while (link && label != link->label)
X`09`7B
X`09  links`5Bdepth`5D = link;
X`09  if (label < link->label)
X`09    dirs`5Bdepth++`5D = L, link = link->llink;
X`09  else
X`09    dirs`5Bdepth++`5D = R, link = link->rlink;
X`09`7D
X
X      /* The current character doesn't have an outgoing link at
X`09 this trie node, so build a new trie node and install
X`09 a link in the current trie node's tree. */
X      if (!link)
X`09`7B
X`09  link = (struct tree *) obstack_alloc(&kwset->obstack,
X`09`09`09`09`09       sizeof (struct tree));
X`09  if (!link)
X`09    return "memory exhausted";
X`09  link->llink = NULL;
X`09  link->rlink = NULL;
X`09  link->trie = (struct trie *) obstack_alloc(&kwset->obstack,
X`09`09`09`09`09`09     sizeof (struct trie));
X`09  if (!link->trie)
X`09    return "memory exhausted";
X`09  link->trie->accepting = 0;
X`09  link->trie->links = NULL;
X`09  link->trie->parent = trie;
X`09  link->trie->next = NULL;
X`09  link->trie->fail = NULL;
X`09  link->trie->depth = trie->depth + 1;
X`09  link->trie->shift = 0;
X`09  link->label = label;
X`09  link->balance = 0;
X
X`09  /* Install the new tree node in its parent. */
X`09  if (dirs`5B--depth`5D == L)
X`09    links`5Bdepth`5D->llink = link;
X`09  else
X`09    links`5Bdepth`5D->rlink = link;
X
X`09  /* Back up the tree fixing the balance flags. */
X`09  while (depth && !links`5Bdepth`5D->balance)
X`09    `7B
X`09      if (dirs`5Bdepth`5D == L)
X`09`09--links`5Bdepth`5D->balance;
X`09      else
X`09`09++links`5Bdepth`5D->balance;
X`09      --depth;
X`09    `7D
X
X`09  /* Rebalance the tree by pointer rotations if necessary. */
X`09  if (depth && (dirs`5Bdepth`5D == L && --links`5Bdepth`5D->balance
X`09`09`09`7C`7C dirs`5Bdepth`5D == R && ++links`5Bdepth`5D->balance))
X`09    `7B
X`09      switch (links`5Bdepth`5D->balance)
X`09`09`7B
X`09`09case (char) -2:
X`09`09  switch (dirs`5Bdepth + 1`5D)
X`09`09    `7B
X`09`09    case L:
X`09`09      r = links`5Bdepth`5D, t = r->llink, rl = t->rlink;
X`09`09      t->rlink = r, r->llink = rl;
X`09`09      t->balance = r->balance = 0;
X`09`09      break;
X`09`09    case R:
X`09`09      r = links`5Bdepth`5D, l = r->llink, t = l->rlink;
X`09`09      rl = t->rlink, lr = t->llink;
X`09`09      t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl;
X`09`09      l->balance = t->balance != 1 ? 0 : -1;
X`09`09      r->balance = t->balance != (char) -1 ? 0 : 1;
X`09`09      t->balance = 0;
X`09`09      break;
X`09`09    `7D
X`09`09  break;
X`09`09case 2:
X`09`09  switch (dirs`5Bdepth + 1`5D)
X`09`09    `7B
X`09`09    case R:
X`09`09      l = links`5Bdepth`5D, t = l->rlink, lr = t->llink;
X`09`09      t->llink = l, l->rlink = lr;
X`09`09      t->balance = l->balance = 0;
X`09`09      break;
X`09`09    case L:
X`09`09      l = links`5Bdepth`5D, r = l->rlink, t = r->llink;
X`09`09      lr = t->llink, rl = t->rlink;
X`09`09      t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl;
X`09`09      l->balance = t->balance != 1 ? 0 : -1;
X`09`09      r->balance = t->balance != (char) -1 ? 0 : 1;
X`09`09      t->balance = 0;
X`09`09      break;
X`09`09    `7D
X`09`09  break;
X`09`09`7D
X
X`09      if (dirs`5Bdepth - 1`5D == L)
X`09`09links`5Bdepth - 1`5D->llink = t;
X`09      else
X`09`09links`5Bdepth - 1`5D->rlink = t;
X`09    `7D
X`09`7D
X
X      trie = link->trie;
X    `7D
X
X  /* Mark the node we finally reached as accepting, encoding the
X     index number of this word in the keyword set so far. */
X  if (!trie->accepting)
X    trie->accepting = 1 + 2 * kwset->words;
X  ++kwset->words;
X
X  /* Keep track of the longest and shortest string of the keyword set. */
X  if (trie->depth < kwset->mind)
X    kwset->mind = trie->depth;
X  if (trie->depth > kwset->maxd)
X    kwset->maxd = trie->depth;
X
X  return NULL;
X`7D
X
X/* Enqueue the trie nodes referenced from the given tree in the
X   given queue. */
Xstatic void
XDEFUN(enqueue, (tree, last), struct tree *tree AND struct trie **last)
X`7B
X  if (!tree)
X    return;
X  enqueue(tree->llink, last);
X  enqueue(tree->rlink, last);
X  (*last) = (*last)->next = tree->trie;
X`7D
X
X/* Compute the Aho-Corasick failure function for the trie nodes referenced
X   from the given tree, given the failure function for their parent as
X   well as a last resort failure node. */
Xstatic void
XDEFUN(treefails, (tree, fail, recourse),
X      register struct tree *tree
X      AND struct trie *fail AND struct trie *recourse)
X`7B
X  register struct tree *link;
X
X  if (!tree)
X    return;
X
X  treefails(tree->llink, fail, recourse);
X  treefails(tree->rlink, fail, recourse);
X
X  /* Find, in the chain of fails going back to the root, the first
X     node that has a descendent on the current label. */
X  while (fail)
X    `7B
X      link = fail->links;
X      while (link && tree->label != link->label)
X`09if (tree->label < link->label)
X`09  link = link->llink;
X`09else
X`09  link = link->rlink;
X      if (link)
X`09`7B
X`09  tree->trie->fail = link->trie;
X`09  return;
X`09`7D
X      fail = fail->fail;
X    `7D
X
X  tree->trie->fail = recourse;
X`7D
X
X/* Set delta entries for the links of the given tree such that
X   the preexisting delta value is larger than the current depth. */
Xstatic void
XDEFUN(treedelta, (tree, depth, delta),
X      register struct tree *tree AND register int depth AND int delta`5B`5D)
X`7B
X  if (!tree)
X    return;
X  treedelta(tree->llink, depth, delta);
X  treedelta(tree->rlink, depth, delta);
X  if (depth < delta`5Btree->label`5D)
X    delta`5Btree->label`5D = depth;
X`7D
X
X/* Return true if A has every label in B. */
Xstatic int
XDEFUN(hasevery, (a, b), register struct tree *a AND register struct tree *b)
X`7B
X  if (!b)
X    return 1;
X  if (!hasevery(a, b->llink))
X    return 0;
X  if (!hasevery(a, b->rlink))
X    return 0;
X  while (a && b->label != a->label)
X    if (b->label < a->label)
X      a = a->llink;
X    else
X      a = a->rlink;
X  return !!a;
X`7D
X
X/* Compute a vector, indexed by character code, of the trie nodes
X   referenced from the given tree. */
Xstatic void
XDEFUN(treenext, (tree, next), struct tree *tree AND struct trie *next`5B`5D)
X`7B
X  if (!tree)
X    return;
X  treenext(tree->llink, next);
X  treenext(tree->rlink, next);
X  next`5Btree->label`5D = tree->trie;
X`7D
X
X/* Compute the shift for each trie node, as well as the delta
X   table and next cache for the given keyword set. */
Xconst char *
XDEFUN(kwsprep, (kws), kwset_t kws)
X`7B
X  register struct kwset *kwset;
X  register int i;
X  register struct trie *curr, *fail;
X  register const char *trans;
X  int delta`5BNCHAR`5D;
X  struct trie *last, *next`5BNCHAR`5D;
X
X  kwset = (struct kwset *) kws;
X
X  /* Initial values for the delta table; will be changed later.  The
X     delta entry for a given character is the smallest depth of any
X     node at which an outgoing edge is labeled by that character. */
X  for (i = 0; i < NCHAR; ++i)
X    delta`5Bi`5D = kwset->mind;
X
X  /* Traverse the nodes of the trie in level order, simultaneously
X     computing the delta table, failure function, and shift function. */
X  for (curr = last = kwset->trie; curr; curr = curr->next)
X    `7B
X      /* Enqueue the immediate descendents in the level order queue. */
X      enqueue(curr->links, &last);
X
X      curr->shift = kwset->mind;
X      curr->maxshift = kwset->mind;
X
X      /* Update the delta table for the descendents of this node. */
X      treedelta(curr->links, curr->depth, delta);
X
X      /* Compute the failure function for the decendents of this node. */
X      treefails(curr->links, curr->fail, kwset->trie);
X
X      /* Update the shifts at each node in the current node's chain
X`09 of fails back to the root. */
X      for (fail = curr->fail; fail; fail = fail->fail)
X`09`7B
X`09  /* If the current node has some outgoing edge that the fail
X`09     doesn't, then the shift at the fail should be no larger
X`09     than the difference of their depths. */
X`09  if (!hasevery(fail->links, curr->links))
X`09    if (curr->depth - fail->depth < fail->shift)
X`09      fail->shift = curr->depth - fail->depth;
X
X`09  /* If the current node is accepting then the shift at the
X`09     fail and its descendents should be no larger than the
X`09     difference of their depths. */
X`09  if (curr->accepting && fail->maxshift > curr->depth - fail->depth)
X`09    fail->maxshift = curr->depth - fail->depth;
X`09`7D
X    `7D
X
X  /* Traverse the trie in level order again, fixing up all nodes whose
X     shift exceeds their inherited maxshift. */
X  for (curr = kwset->trie->next; curr; curr = curr->next)
X    `7B
X      if (curr->maxshift > curr->parent->maxshift)
X`09curr->maxshift = curr->parent->maxshift;
X      if (curr->shift > curr->maxshift)
X`09curr->shift = curr->maxshift;
X    `7D
X
X  /* Create a vector, indexed by character code, of the outgoing links
X     from the root node. */
X  for (i = 0; i < NCHAR; ++i)
X    next`5Bi`5D = NULL;
X  treenext(kwset->trie->links, next);
X
X  /* Fix things up for any translation table. */
X  if (trans = kwset->trans)
X    for (i = 0; i < NCHAR; ++i)
X      `7B
X`09kwset->delta`5Bi`5D = delta`5B(unsigned char) trans`5Bi`5D`5D;
X`09kwset->next`5Bi`5D = next`5B(unsigned char) trans`5Bi`5D`5D;
X      `7D
X  else
X    for (i = 0; i < NCHAR; ++i)
X      `7B
X`09kwset->delta`5Bi`5D = delta`5Bi`5D;
X`09kwset->next`5Bi`5D = next`5Bi`5D;
X      `7D
X
X  return NULL;
X`7D
X
X/* Search through the given text for a match of any member of the
X   given keyword set.  Return a pointer to the first character of
X   the matching substring, or NULL if no match is found.  If FOUNDLEN
X   is non-NULL store in the referenced location the length of the
X   matching substring.  Similarly, if FOUNDIDX is non-NULL, store
X   in the referenced location the index number of the particular
X   keyword matched. */
Xchar *
XDEFUN(kwsexec, (kws, text, len, kwsmatch),
X      kwset_t kws AND char *text AND size_t len AND struct kwsmatch *kwsmatc
Vh)
X`7B
X  struct kwset *kwset;
X  struct trie **next, *trie, *accept;
X  char *beg, *lim, *mch, *lmch;
X  register unsigned char c;
X  register int *delta, d;
X  register char *end, *qlim;
X  register struct tree *tree;
X  register const char *trans;
X
X  /* Initialize register copies and look for easy ways out. */
X  kwset = (struct kwset *) kws;
X  if (len < kwset->mind)
X    return NULL;
X  next = kwset->next;
X  delta = kwset->delta;
X  trans = kwset->trans;
X  lim = text + len;
X  end = text;
X  if (d = kwset->mind)
X    mch = NULL;
X  else
X    `7B
X      mch = text, accept = kwset->trie;
X      goto match;
X    `7D
X
X  if (len >= 4 * kwset->mind)
X    qlim = lim - 4 * kwset->mind;
X  else
X    qlim = NULL;
X
X  while (lim - end >= d)
X    `7B
X      if (qlim && end <= qlim)
X`09`7B
X`09  end += d - 1;
X`09  while ((d = delta`5Bc = *end`5D) && end < qlim)
X`09    `7B
X`09      end += d;
X`09      end += delta`5B(unsigned char) *end`5D;
X`09      end += delta`5B(unsigned char) *end`5D;
X`09    `7D
X`09  ++end;
X`09`7D
X      else
X`09d = delta`5Bc = (end += d)`5B-1`5D`5D;
X      if (d)
X`09continue;
X      beg = end - 1;
X      trie = next`5Bc`5D;
X      if (trie->accepting)
X`09`7B
X`09  mch = beg;
X`09  accept = trie;
X`09`7D
X      d = trie->shift;
X      while (beg > text)
X`09`7B
X`09  c = trans ? trans`5B(unsigned char) *--beg`5D : *--beg;
X`09  tree = trie->links;
X`09  while (tree && c != tree->label)
X`09    if (c < tree->label)
X`09      tree = tree->llink;
X`09    else
X`09      tree = tree->rlink;
X`09  if (tree)
X`09    `7B
X`09      trie = tree->trie;
X`09      if (trie->accepting)
X`09`09`7B
X`09`09  mch = beg;
X`09`09  accept = trie;
X`09`09`7D
X`09    `7D
X`09  else
X`09    break;
X`09  d = trie->shift;
X`09`7D
X      if (mch)
X`09goto match;
X    `7D
X  return NULL;
X
X match:
X  /* Given a known match, find the longest possible match anchored
X     at or before its starting point.  This is nearly a verbatim
X     copy of the preceding main search loops. */
X  if (lim - mch > kwset->maxd)
X    lim = mch + kwset->maxd;
X  lmch = NULL;
X  d = 1;
X  while (lim - end >= d)
X    `7B
X      if (d = delta`5Bc = (end += d)`5B-1`5D`5D)
X`09continue;
X      beg = end - 1;
X      if (!(trie = next`5Bc`5D))
X`09`7B
X`09  d = 1;
X`09  continue;
X`09`7D
X      if (trie->accepting && beg <= mch)
X`09`7B
X`09  lmch = beg;
X`09  accept = trie;
X`09`7D
X      d = trie->shift;
X      while (beg > text)
X`09`7B
X`09  c = trans ? trans`5B(unsigned char) *--beg`5D : *--beg;
X`09  tree = trie->links;
X`09  while (tree && c != tree->label)
X`09    if (c < tree->label)
X`09      tree = tree->llink;
X`09    else
X`09      tree = tree->rlink;
X`09  if (tree)
X`09    `7B
X`09      trie = tree->trie;
X`09      if (trie->accepting && beg <= mch)
X`09`09`7B
X`09`09  lmch = beg;
X`09`09  accept = trie;
X`09`09`7D
X`09    `7D
X`09  else
X`09    break;
X`09  d = trie->shift;
X`09`7D
X      if (lmch)
X`09`7B
X`09  mch = lmch;
X`09  goto match;
X`09`7D
X      if (!d)
X`09d = 1;
X    `7D
X
X  if (kwsmatch)
X    `7B
X      kwsmatch->index = accept->accepting / 2;
X      kwsmatch->beg`5B0`5D = mch;
X      kwsmatch->size`5B0`5D = accept->depth;
X    `7D
X  return mch;
X`7D
X `20
X/* Free the components of the given keyword set. */
Xvoid
XDEFUN(kwsfree, (kws), kwset_t kws)
X`7B
X  struct kwset *kwset;
X
X  kwset = (struct kwset *) kws;
X  obstack_free(&kwset->obstack, (PTR) NULL);
X  free((PTR) kws);
X`7D
$ CALL UNPACK [.FGREP011]KWSET.C;1 587784185
$ create 'f'
X/* kwset.h - header declaring the keyword set library.
X   Copyright 1989 Free Software Foundation
X`09`09  Written August 1989 by Mike Haertel.
X
X   This program is free software; you can redistribute it and/or modify
X   it under the terms of the GNU General Public License as published by
X   the Free Software Foundation; either version 1, or (at your option)
X   any later version.
X
X   This program is distributed in the hope that it will be useful,
X   but WITHOUT ANY WARRANTY; without even the implied warranty of
X   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X   GNU General Public License for more details.
X
X   You should have received a copy of the GNU General Public License
X   along with this program; if not, write to the Free Software
X   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X
X   The author may be reached (Email) at the address mike@ai.mit.edu,
X   or (US mail) as Mike Haertel c/o Free Software Foundation. */
X
Xstruct kwsmatch
X`7B
X  int index;`09`09`09/* Index number of matching keyword. */
X  char *beg`5B1`5D;`09`09`09/* Begin pointer for each submatch. */
X  size_t size`5B1`5D;`09`09/* Length of each submatch. */
X`7D;
X
X#if __STDC__
X
Xtypedef void *kwset_t;
X
X/* Return an opaque pointer to a newly allocated keyword set, or NULL
X   if enough memory cannot be obtained.  The argument if non-NULL
X   specifies a table of character translations to be applied to all
X   pattern and search text. */
Xextern kwset_t kwsalloc(const char *);
X
X/* Incrementally extend the keyword set to include the given string.
X   Return NULL for success, or an error message.  Remember an index
X   number for each keyword included in the set. */
Xextern const char *kwsincr(kwset_t, const char *, size_t);
X
X/* When the keyword set has been completely built, prepare it for
X   use.  Return NULL for success, or an error message. */
Xextern const char *kwsprep(kwset_t);
X
X/* Search through the given buffer for a member of the keyword set.
X   Return a pointer to the leftmost longest match found, or NULL if
X   no match is found.  If foundlen is non-NULL, store the length of
X   the matching substring in the integer it points to.  Similarly,
X   if foundindex is non-NULL, store the index of the particular
X   keyword found therein. */
Xextern char *kwsexec(kwset_t, char *, size_t, struct kwsmatch *);
X
X/* Deallocate the given keyword set and all its associated storage. */
Xextern void kwsfree(kwset_t);
X
X#else
X
Xtypedef char *kwset_t;
X
Xextern kwset_t kwsalloc();
Xextern char *kwsincr();
Xextern char *kwsprep();
Xextern char *kwsexec();
Xextern void kwsfree();
X
X#endif
$ CALL UNPACK [.FGREP011]KWSET.H;1 808066417
$ create 'f'
XDEFS=-DX_strerror
XCFLAGS=-O $(DEFS)
XLIBS=
XFOBJ=fgrep.o kwset.o obstack.o std.o
X
Xfgrep: $(FOBJ)
X`09$(CC) $(CFLAGS) -o fgrep $(FOBJ) $(LIBS)
X
Xclean: FRC
X`09rm -f fgrep core $(FOBJ)
X
XFRC:
X
Xfgrep.o: unix.h
Xkwset.o fgrep.o: kwset.h obstack.h std.h
Xobstack.o: obstack.h
Xstd.o: std.h unix.h
$ CALL UNPACK [.FGREP011]MAKEFILE.;1 795896332
$ create 'f'
X/* obstack.c - subroutines used implicitly by object stack macros
X   Copyright (C) 1988 Free Software Foundation, Inc.
X
XThis program is free software; you can redistribute it and/or modify it
Xunder the terms of the GNU General Public License as published by the
XFree Software Foundation; either version 1, or (at your option) any
Xlater version.
X
XThis program is distributed in the hope that it will be useful,
Xbut WITHOUT ANY WARRANTY; without even the implied warranty of
XMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
XGNU General Public License for more details.
X
XYou should have received a copy of the GNU General Public License
Xalong with this program; if not, write to the Free Software
XFoundation, 675 Mass Ave, Cambridge, MA 02139, USA.
X
X
XIn other words, you are welcome to use, share and improve this program.
XYou are forbidden to forbid anyone else to use, share and improve
Xwhat you give them.   Help stamp out software-hoarding!  */
X
X
X#include "obstack.h"
X
X#ifdef __STDC__
X#define POINTER void *
X#else
X#define POINTER char *
X#endif
X
X/* Determine default alignment.  */
Xstruct fooalign `7Bchar x; double d;`7D;
X#define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
X/* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMEN
VT.
X   But in fact it might be less smart and round addresses to as much as
X   DEFAULT_ROUNDING.  So we prepare for it to do that.  */
Xunion fooround `7Blong x; double d;`7D;
X#define DEFAULT_ROUNDING (sizeof (union fooround))
X
X/* When we copy a long block of data, this is the unit to do it with.
X   On some machines, copying successive ints does not work;
X   in such a case, redefine COPYING_UNIT to `60long' (if that works)
X   or `60char' as a last resort.  */
X#ifndef COPYING_UNIT
X#define COPYING_UNIT int
X#endif
X
X/* The non-GNU-C macros copy the obstack into this global variable
X   to avoid multiple evaluation.  */
X
Xstruct obstack *_obstack;
X`0C
X/* Initialize an obstack H for use.  Specify chunk size SIZE (0 means defaul
Vt).
X   Objects start on multiples of ALIGNMENT (0 means use default).
X   CHUNKFUN is the function to use to allocate chunks,
X   and FREEFUN the function to free them.  */
X
Xvoid
X_obstack_begin (h, size, alignment, chunkfun, freefun)
X     struct obstack *h;
X     int size;
X     int alignment;
X     POINTER (*chunkfun) ();
X     void (*freefun) ();
X`7B
X  register struct _obstack_chunk* chunk; /* points to new chunk */
X
X  if (alignment == 0)
X    alignment = DEFAULT_ALIGNMENT;
X  if (size == 0)
X    /* Default size is what GNU malloc can fit in a 4096-byte block.
X       Pick a number small enough that when rounded up to DEFAULT_ROUNDING
X       it is still smaller than 4096 - 4.  */
X    `7B
X      int extra = 4;
X      if (extra < DEFAULT_ROUNDING)
X`09extra = DEFAULT_ROUNDING;
X      size = 4096 - extra;
X    `7D
X
X  h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
X  h->freefun = freefun;
X  h->chunk_size = size;
X  h->alignment_mask = alignment - 1;
X
X  chunk`09= h->chunk = (*h->chunkfun) (h->chunk_size);
X  h->next_free = h->object_base = chunk->contents;
X  h->chunk_limit = chunk->limit
X   = (char *) chunk + h->chunk_size;
X  chunk->prev = 0;
X`7D
X
X/* Allocate a new current chunk for the obstack *H
X   on the assumption that LENGTH bytes need to be added
X   to the current object, or a new object of length LENGTH allocated.
X   Copies any partial object from the end of the old chunk
X   to the beginning of the new one.  */
X
Xvoid
X_obstack_newchunk (h, length)
X     struct obstack *h;
X     int length;
X`7B
X  register struct _obstack_chunk*`09old_chunk = h->chunk;
X  register struct _obstack_chunk*`09new_chunk;
X  register long`09new_size;
X  register int obj_size = h->next_free - h->object_base;
X  register int i;
X  int already;
X
X  /* Compute size for new chunk.  */
X  new_size = (obj_size + length) + (obj_size >> 3) + 100;
X  if (new_size < h->chunk_size)
X    new_size = h->chunk_size;
X
X  /* Allocate and initialize the new chunk.  */
X  new_chunk = h->chunk = (*h->chunkfun) (new_size);
X  new_chunk->prev = old_chunk;
X  new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
X
X  /* Move the existing object to the new chunk.
X     Word at a time is fast and is safe if the object
X     is sufficiently aligned.  */
X  if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
X    `7B
X      for (i = obj_size / sizeof (COPYING_UNIT) - 1;
X`09   i >= 0; i--)
X`09((COPYING_UNIT *)new_chunk->contents)`5Bi`5D
X`09  = ((COPYING_UNIT *)h->object_base)`5Bi`5D;
X      /* We used to copy the odd few remaining bytes as one extra COPYING_UN
VIT,
X`09 but that can cross a page boundary on a machine
X`09 which does not do strict alignment for COPYING_UNITS.  */
X      already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
X    `7D
X  else
X    already = 0;
X  /* Copy remaining bytes one by one.  */
X  for (i = already; i < obj_size; i++)
X    new_chunk->contents`5Bi`5D = h->object_base`5Bi`5D;
X
X  h->object_base = new_chunk->contents;
X  h->next_free = h->object_base + obj_size;
X`7D
X
X/* Return nonzero if object OBJ has been allocated from obstack H.
+-+-+-+-+-+-+-+-  END  OF PART 3 +-+-+-+-+-+-+-+-
