home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XT.ZIP / RN / NGSRCH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-21  |  3.5 KB  |  169 lines

  1. /* $Header: E:\SRC\UUPC\RN\RCS/NGSRCH.C 1.1 1992/11/21 06:14:58 ahd Exp $
  2.  *
  3.  * $Log: NGSRCH.C $
  4.  * Revision 1.1  1992/11/21  06:14:58  ahd
  5.  * Initial
  6.  *
  7.  *
  8.  *    Rev 1.0   18 Nov 1990  0:22:22
  9.  * Initial revision.
  10.  * Revision 4.3  85/05/01  11:44:51  lwall
  11.  * Baseline for release with 4.3bsd.
  12.  *
  13.  */
  14.  
  15. #include "EXTERN.h"
  16. #include "common.h"
  17. #include "rcstuff.h"
  18. #include "final.h"
  19. #include "search.h"
  20. #include "rn.h"
  21. #include "util.h"
  22. #include "term.h"
  23. #include "rcln.h"
  24. #include "INTERN.h"
  25. #include "ngsrch.h"
  26.  
  27. #ifdef NGSORONLY
  28. COMPEX ngcompex;
  29.  
  30. #endif
  31.  
  32. void
  33.   ngsrch_init()
  34. {
  35.  
  36. #ifdef ZEROGLOB
  37.    init_compex(&ngcompex);
  38. #endif                       /* ZEROGLOB */
  39.  
  40.    ;
  41. }
  42.  
  43. #ifdef NGSEARCH
  44. int
  45.   ng_search(patbuf, get_cmd)
  46.    char *patbuf;             /* if patbuf != buf,
  47.                               * get_cmd must */
  48.    int get_cmd;              /* be set to FALSE!!! */
  49. {
  50.    char *pattern;            /* unparsed pattern */
  51.    register char cmdchr = *patbuf;      /* what kind of search? */
  52.    register char *s;
  53.    bool backward = cmdchr == '?';       /* direction of search */
  54.  
  55.    int_count = 0;
  56.    if (get_cmd && buf == patbuf)
  57.       if (!finish_command(FALSE))       /* get rest of command */
  58.          return NGS_ABORT;
  59.    for (pattern = patbuf + 1; *pattern == ' '; pattern++);
  60.    if (*pattern)
  61.    {
  62.       ng_doread = FALSE;
  63.    }
  64.    s = rindex(pattern, cmdchr);
  65.    if (s != Nullch && *(s - 1) != '\\')
  66.    {
  67.       *s++ = '\0';
  68.       if (index(s, 'r') != Nullch)
  69.          ng_doread = TRUE;
  70.    }
  71.    if ((s = ng_comp(&ngcompex, pattern, TRUE, TRUE)) != Nullch)
  72.    {
  73.       /* compile regular expression */
  74.       printf("\n%s\n", s) FLUSH;
  75.       return NGS_ABORT;
  76.    }
  77.    fputs("\nSearching...", stdout) FLUSH;       /* give them something
  78.                                                  * to read */
  79.    fflush(stdout);
  80.    for (;;)
  81.    {
  82.       if (int_count)
  83.       {
  84.          int_count = 0;
  85.          return NGS_INTR;
  86.       }
  87.       if (backward)
  88.       {
  89.          if (ng > 0)
  90.             --ng;
  91.          else
  92.             ng = nextrcline;
  93.       }
  94.       else
  95.       {
  96.          if (ng >= nextrcline)
  97.             ng = 0;
  98.          else
  99.             ++ng;
  100.       }
  101.       if (ng == current_ng)
  102.          return NGS_NOTFOUND;
  103.       if (ng == nextrcline || toread[ng] < TR_NONE || !ng_wanted())
  104.          continue;
  105.       if (toread[ng] == TR_NONE)
  106.          set_toread(ng);
  107.  
  108.       if (toread[ng] > TR_NONE)
  109.          return NGS_FOUND;
  110.       else if (toread[ng] == TR_NONE)
  111.          if (ng_doread)
  112.             return NGS_FOUND;
  113.          else
  114.             printf("\n[0 unread in %s--skipping]", rcline[ng]) FLUSH;
  115.    }
  116. }
  117.  
  118. bool
  119. ng_wanted()
  120. {
  121.    return execute(&ngcompex, rcline[ng]) != Nullch;
  122. }
  123.  
  124. #endif
  125.  
  126. #ifdef NGSORONLY
  127. char *
  128.   ng_comp(compex, pattern, RE, fold)
  129.    COMPEX *compex;
  130.    char *pattern;
  131.    bool RE;
  132.    bool fold;
  133. {
  134.    char ng_pattern[128];
  135.    register char *s = pattern, *d = ng_pattern;
  136.  
  137.    if (!*s)
  138.       return Nullch;         /* reuse old pattern */
  139.    for (; *s; s++)
  140.    {
  141.       if (*s == '.')
  142.       {
  143.          *d++ = '\\';
  144.          *d++ = *s;
  145.       }
  146.       else if (*s == '?')
  147.       {
  148.          *d++ = '.';
  149.       }
  150.       else if (*s == '*')
  151.       {
  152.          *d++ = '.';
  153.          *d++ = *s;
  154.       }
  155.       else if (strnEQ(s, "all", 3))
  156.       {
  157.          *d++ = '.';
  158.          *d++ = '*';
  159.          s += 2;
  160.       }
  161.       else
  162.          *d++ = *s;
  163.    }
  164.    *d = '\0';
  165.    return compile(compex, ng_pattern, RE, fold);
  166. }
  167.  
  168. #endif
  169.