home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / rn_4_3_blars.lzh / artsrch.c < prev    next >
C/C++ Source or Header  |  1990-08-22  |  7KB  |  315 lines

  1. /* $Header: artsrch.c,v 4.3.2.4 89/11/27 01:30:00 sob Locked $
  2.  *
  3.  * $Log:    artsrch.c,v $
  4.  * Revision 4.3.2.4  89/11/27  01:30:00  sob
  5.  * Altered NNTP code per ideas suggested by Bela Lubkin
  6.  * <filbo@gorn.santa-cruz.ca.us>
  7.  * 
  8.  * Revision 4.3.2.3  89/11/26  22:54:37  sob
  9.  * Added new patches to make rrn faster.
  10.  * 
  11.  * Revision 4.3.2.2  89/11/26  22:20:57  sob
  12.  * Added better NNTP support.
  13.  * 
  14.  * Revision 4.3.2.1  89/11/26  22:13:10  sob
  15.  * Added support for NNTP
  16.  * 
  17.  * Revision 4.3  85/05/01  11:35:47  lwall
  18.  * Baseline for release with 4.3bsd.
  19.  * 
  20.  */
  21.  
  22. #include "EXTERN.h"
  23. #include "common.h"
  24. #include "search.h"
  25. #include "term.h"
  26. #include "util.h"
  27. #include "intrp.h"
  28. #include "bits.h"
  29. #include "kfile.h"
  30. #include "head.h"
  31. #include "final.h"
  32. #include "cheat.h"
  33. #ifdef SERVER
  34. #include "server.h"
  35. #endif
  36. #include "ng.h"
  37. #include "artio.h"
  38. #include "INTERN.h"
  39. #include "artsrch.h"
  40.  
  41. void
  42. artsrch_init()
  43. {
  44. #ifdef ARTSEARCH
  45. #ifdef ZEROGLOB
  46.     init_compex(&sub_compex);
  47.     init_compex(&art_compex);
  48. #endif
  49. #endif
  50. }
  51.  
  52. /* search for an article containing some pattern */
  53.  
  54. #ifdef ARTSEARCH
  55. int
  56. art_search(patbuf,patbufsiz,get_cmd)
  57. char *patbuf;                /* if patbuf != buf, get_cmd must */
  58. int patbufsiz;
  59. int get_cmd;                /*   be set to FALSE!!! */
  60. {
  61.     char *pattern;            /* unparsed pattern */
  62.     register char cmdchr = *patbuf;    /* what kind of search? */
  63.     register char *s;
  64.     bool backward = cmdchr == '?' || cmdchr == Ctl('p');
  65.                     /* direction of search */
  66.     COMPEX *compex;            /* which compiled expression */
  67.     char *cmdlst = Nullch;        /* list of commands to do */
  68.     int normal_return = SRCH_NOTFOUND;    /* assume no commands */
  69.     bool saltaway = FALSE;        /* store in KILL file? */
  70.     char howmuch;            /* search just the subjects */
  71.     bool doread;            /* search read articles? */
  72.     bool foldcase = TRUE;        /* fold upper and lower case? */
  73.  
  74.     int_count = 0;
  75.     if (cmdchr == '/' || cmdchr == '?') {    /* normal search? */
  76.     if (get_cmd && buf == patbuf)
  77.         if (!finish_command(FALSE))    /* get rest of command */
  78.         return SRCH_ABORT;
  79.     compex = &art_compex;
  80.     if (patbuf[1]) {
  81.         howmuch = 0;
  82.         doread = FALSE;
  83.     }
  84.     else {
  85.         howmuch = art_howmuch;
  86.         doread = art_doread;
  87.     }
  88.     s = cpytill(buf,patbuf+1,cmdchr);/* ok to cpy buf+1 to buf */
  89.     pattern = buf;
  90.     if (*pattern) {
  91.         if (*lastpat)
  92.         free(lastpat);
  93.         lastpat = savestr(pattern);
  94.     }
  95.     if (*s) {            /* modifiers or commands? */
  96.         for (s++; *s && index("Kharc",*s); s++) {
  97.         if (*s == 'h')        /* scan header */
  98.             howmuch = 1;
  99.         else if (*s == 'a')    /* scan article */
  100.             howmuch = 2;
  101.         else if (*s == 'r')    /* scan read articles */
  102.             doread = TRUE;
  103.         else if (*s == 'K')    /* put into KILL file */
  104.             saltaway = TRUE;
  105.         else if (*s == 'c')    /* make search case sensitive */
  106.             foldcase = FALSE;
  107.         }
  108.     }
  109.     while (isspace(*s) || *s == ':')
  110.         s++;
  111.     if (*s) {
  112.         if (*s == 'm' || *s == 'M')
  113.         doread = TRUE;
  114.         if (*s == 'k')        /* grandfather clause */
  115.         *s = 'j';
  116.         cmdlst = savestr(s);
  117.         normal_return = SRCH_DONE;
  118.     }
  119.     art_howmuch = howmuch;
  120.     art_doread = doread;
  121.     if (srchahead)
  122.         srchahead = -1;
  123.     }
  124.     else {
  125.     register char *h;
  126.  
  127.     howmuch = 0;            /* just search subjects */
  128.     doread = (cmdchr == Ctl('p'));
  129.     if (cmdchr == Ctl('n'))
  130.         normal_return = SRCH_SUBJDONE;
  131.     compex = &sub_compex;
  132.     pattern = patbuf+1;
  133.     strcpy(pattern,": *");
  134.     h = pattern + strlen(pattern);
  135.     interp(h,patbufsiz - (h-patbuf),"%s");    /* fetch current subject */
  136.     if (cmdchr == 'K') {
  137.         saltaway = TRUE;
  138.         cmdchr = 'k';
  139.     }
  140.     if (cmdchr == 'k') {
  141.         normal_return = SRCH_DONE;
  142.         cmdlst = savestr("j");
  143.         mark_as_read(art);        /* this article has this subject */
  144.         if (!*h) {
  145. #ifdef VERBOSE
  146.         IF(verbose)
  147.             fputs("\nCannot delete null subject.\n",stdout) FLUSH;
  148.         ELSE
  149. #endif
  150. #ifdef TERSE
  151.             fputs("\nNull subject.\n",stdout) FLUSH;
  152. #endif
  153.         return SRCH_ABORT;
  154.         }
  155. #ifdef VERBOSE
  156.         else if (verbose)
  157.         printf("\nMarking subject \"%s\" as read.\n",h) FLUSH;
  158. #endif
  159.     }
  160.     else if (!srchahead)
  161.         srchahead = -1;
  162.     h[24] = '\0';        /* compensate for notesfiles */
  163.     while (*h) {
  164.         if (index("/\\[.^*$'\"",*h) != Nullch)
  165.         *h++ = '.';
  166.         else
  167.         h++;
  168.     }
  169. #ifdef DEBUGGING
  170.     if (debug) {
  171.         printf("\npattern = %s\n",pattern) FLUSH;
  172.     }
  173. #endif
  174.     }
  175.     if ((s = compile(compex,pattern,TRUE,foldcase)) != Nullch) {
  176.                     /* compile regular expression */
  177.     printf("\n%s\n",s) FLUSH;
  178.     return SRCH_ABORT;
  179.     }
  180. #ifdef KILLFILES
  181.     if (saltaway) {
  182.     char saltbuf[LBUFLEN];
  183.  
  184.     s = saltbuf;
  185.     sprintf(s,"/%s/",pattern);
  186.     s += strlen(s);
  187.     if (doread)
  188.         *s++ = 'r';
  189.     if (howmuch==1)
  190.         *s++ = 'h';
  191.     else if (howmuch==2)
  192.         *s++ = 'a';
  193.     *s++ = ':';
  194.     if (!cmdlst)
  195.         cmdlst = savestr("j");
  196.     safecpy(s,cmdlst,LBUFLEN-(s-saltbuf));
  197.     kf_append(saltbuf);
  198.     }
  199. #endif
  200.     if (cmdlst && index(cmdlst,'='))
  201.     normal_return = SRCH_ERROR;    /* listing subjects is an error? */
  202.     if (get_cmd) {
  203.     fputs("\nSearching...\n",stdout) FLUSH;
  204.                     /* give them something to read */
  205.     }
  206.     if (backward) {
  207.     if (cmdlst && art < lastart)
  208.         art++;            /* include current article */
  209.     if (doread)
  210.         check_first(absfirst);
  211.     }
  212.     else {
  213.     if (art > lastart)
  214.         art = (doread ? absfirst : firstart) - 1;
  215.     else if (cmdlst && art > absfirst)
  216.         art--;            /* include current article */
  217.     check_first(art);
  218.     }
  219.     if (srchahead > 0) {
  220.     if (!backward)
  221.         art = srchahead - 1;
  222.     srchahead = -1;
  223.     }
  224.     assert(!cmdlst || *cmdlst);
  225.     for (;;) {
  226.     if (int_count) {
  227.         int_count = 0;
  228.         if (cmdlst)
  229.         free(cmdlst);
  230.         return SRCH_INTR;
  231.     }
  232.     if (backward ?
  233.         (--art < absfirst || (!doread && art < firstart)) :
  234.         (++art > lastart)
  235.       ) {            /* out of articles? */
  236.         if (cmdlst)
  237.         free(cmdlst);
  238.         return normal_return;
  239.     }
  240.     /*NOSTRICT*/
  241.     if (doread || !was_read(art)) {
  242.         if (wanted(compex,art,howmuch)) {
  243.                     /* does the shoe fit? */
  244.         if (cmdlst) {
  245.             if (perform(cmdlst,TRUE)) {
  246.             if (cmdlst)
  247.                 free(cmdlst);
  248.             return SRCH_INTR;
  249.             }
  250.         }
  251.         else {
  252.             if (cmdlst)
  253.             free(cmdlst);
  254.             return SRCH_FOUND;
  255.         }
  256.         }
  257.         else if (!cmdlst && ! (art%50)) {
  258.         printf("...%ld",(long)art);
  259.         fflush(stdout);
  260.         }
  261.     }
  262.     }
  263. }
  264.  
  265. /* determine if article fits pattern */
  266. /* returns TRUE if it exists and fits pattern, FALSE otherwise */
  267.  
  268. bool
  269. wanted(compex, artnum, scope)
  270. COMPEX *compex;
  271. ART_NUM artnum;
  272. char scope;
  273. {
  274.     if (!scope) {
  275.     char subj_buf[266];
  276.     
  277.     strcpy(subj_buf, "Subject: ");
  278.     strncpy(subj_buf+9,fetchsubj(artnum,FALSE,FALSE),256);
  279. #ifdef DEBUGGING
  280.     if (debug & DEB_SEARCH_AHEAD)
  281.         printf("%s\n",subj_buf) FLUSH;
  282. #endif
  283.     return execute(compex,subj_buf) != Nullch;
  284.     }
  285. #ifdef CACHESUBJ
  286.     else
  287.     fetchsubj(artnum,FALSE,FALSE);/* might as well get subject handy */
  288. #endif
  289.     
  290. #ifdef SERVER
  291.     if (scope == 1){
  292.     if (nntpopen(artnum,HEAD) == Nullfp) /* we only need the header */
  293.         return FALSE;
  294.     }
  295.     else
  296. #endif
  297.     if (artopen(artnum) == Nullfp)    /* ensure that article is open */
  298.  
  299.     return FALSE;            /* if not, return NO MATCH */
  300.     scope--;
  301.     while (fgets(buf,LBUFLEN,artfp) != Nullch) {
  302.                     /* for each line of article */
  303.     if (!scope && index(buf,':') == Nullch && *buf != ' ' && *buf != '\t')
  304.                     /* if headers only and out of header */
  305.         return FALSE;        /* say no go */
  306.     if (execute(compex,buf) != Nullch) {
  307.                     /* does pattern matcher match? */
  308.         return TRUE;        /* say Eureka */
  309.     }
  310.     }
  311.     return FALSE;            /* out of article, so no match */
  312. }
  313. #endif
  314.  
  315.