home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / ngsrch.c < prev    next >
C/C++ Source or Header  |  1993-12-04  |  4KB  |  166 lines

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