home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trn / part02 / ngsrch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  3.3 KB  |  163 lines

  1. /* $Id: ngsrch.c,v 4.4 1991/09/09 20:23:31 sob Exp sob $
  2.  *
  3.  * $Log: ngsrch.c,v $
  4.  * Revision 4.4  1991/09/09  20:23:31  sob
  5.  * release 4.4
  6.  *
  7.  *
  8.  * 
  9.  */
  10. /* This software is Copyright 1991 by Stan Barber. 
  11.  *
  12.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  13.  * use this software as long as: there is no monetary profit gained
  14.  * specifically from the use or reproduction of this software, it is not
  15.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  16.  * included prominently in any copy made. 
  17.  *
  18.  * The author make no claims as to the fitness or correctness of this software
  19.  * for any use whatsoever, and it is provided as is. Any use of this software
  20.  * is at the user's own risk. 
  21.  */
  22.  
  23. #include "EXTERN.h"
  24. #include "common.h"
  25. #include "rcstuff.h"
  26. #include "final.h"
  27. #include "search.h"
  28. #include "rn.h"
  29. #include "util.h"
  30. #include "term.h"
  31. #include "rcln.h"
  32. #include "INTERN.h"
  33. #include "ngsrch.h"
  34.  
  35. #ifdef NGSORONLY
  36.     COMPEX ngcompex;
  37. #endif
  38.  
  39. void
  40. ngsrch_init()
  41. {
  42. #ifdef ZEROGLOB
  43.     init_compex(&ngcompex);
  44. #endif    /* ZEROGLOB */
  45.     ;
  46. }
  47.  
  48. #ifdef NGSEARCH
  49. int
  50. ng_search(patbuf,get_cmd)
  51. char *patbuf;                /* if patbuf != buf, get_cmd must */
  52. int get_cmd;                /*   be set to FALSE!!! */
  53. {
  54.     char *pattern;            /* unparsed pattern */
  55.     register char cmdchr = *patbuf;    /* what kind of search? */
  56.     register char *s;
  57.     bool backward = cmdchr == '?';    /* direction of search */
  58.  
  59.     int_count = 0;
  60.     if (get_cmd && buf == patbuf)
  61.     if (!finish_command(FALSE))        /* get rest of command */
  62.         return NGS_ABORT;
  63.     for (pattern = patbuf+1; *pattern == ' '; pattern++) ;
  64.     if (*pattern) {
  65.     ng_doread = FALSE;
  66.     }
  67.     s = rindex(pattern,cmdchr);
  68.     if (s != Nullch && *(s-1) != '\\') {
  69.     *s++ = '\0';
  70.     if (index(s,'r') != Nullch)
  71.         ng_doread = TRUE;
  72.     }
  73.     if ((s = ng_comp(&ngcompex,pattern,TRUE,TRUE)) != Nullch) {
  74.                     /* compile regular expression */
  75.     printf("\n%s\n",s) FLUSH;
  76.     return NGS_ERROR;
  77.     }
  78.     fputs("\nSearching...",stdout) FLUSH;    /* give them something to read */
  79.     fflush(stdout);
  80.     for (;;) {
  81.     if (int_count) {
  82.         int_count = 0;
  83.         return NGS_INTR;
  84.     }
  85.     if (backward) {
  86.         if (ng > 0)
  87.         --ng;
  88.         else
  89.         ng = nextrcline;
  90.     }
  91.     else {
  92.         if (ng >= nextrcline)
  93.         ng = 0;
  94.         else
  95.         ++ng;
  96.     }
  97.     if (ng == current_ng)
  98.         return NGS_NOTFOUND;
  99.     if (ng == nextrcline || toread[ng] < TR_NONE || !ng_wanted())
  100.         continue;
  101.     if (toread[ng] == TR_NONE)
  102.         set_toread(ng);
  103.     
  104.     if (toread[ng] > TR_NONE)
  105.         return NGS_FOUND;
  106.     else if (toread[ng] == TR_NONE)
  107.         if (ng_doread)
  108.         return NGS_FOUND;
  109.         else
  110.         printf("\n[0 unread in %s--skipping]",rcline[ng]) FLUSH;
  111.     }
  112. }
  113.  
  114. bool
  115. ng_wanted()
  116. {
  117.     return execute(&ngcompex,rcline[ng]) != Nullch;
  118. }
  119. #endif
  120.  
  121. #ifdef NGSORONLY
  122. char *
  123. ng_comp(compex,pattern,RE,fold)
  124. COMPEX *compex;
  125. char *pattern;
  126. bool_int RE;
  127. bool_int fold;
  128. {
  129.     char ng_pattern[128];
  130.     register char *s = pattern, *d = ng_pattern;
  131.  
  132.     if (!*s) {
  133.     if(compex->expbuf)
  134.         return Nullch;            /* reuse old pattern */
  135.     else
  136.         return "No previous search pattern";
  137.     }
  138.     for (; *s; s++) {
  139.     if (*s == '.') {
  140.         *d++ = '\\';
  141.         *d++ = *s;
  142.     }
  143.     else if (*s == '?') {
  144.         *d++ = '.';
  145.     }
  146.     else if (*s == '*') {
  147.         *d++ = '.';
  148.         *d++ = *s;
  149.     }
  150.     else if (strnEQ(s,"all",3)) {
  151.         *d++ = '.';
  152.         *d++ = '*';
  153.         s += 2;
  154.     }
  155.     else
  156.         *d++ = *s;
  157.     }
  158.     *d = '\0';
  159.     return compile(compex,ng_pattern,RE,fold);
  160. }
  161. #endif
  162.  
  163.