home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume23 / trn / part12 / kfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-22  |  7.2 KB  |  362 lines

  1. /* $Header: kfile.c,v 4.3.3.2 91/01/16 02:46:28 davison Trn $
  2.  *
  3.  * $Log:    kfile.c,v $
  4.  * Revision 4.3.3.2  91/01/16  02:46:28  davison
  5.  * Integrated rn patches 48-54.
  6.  * 
  7.  * Revision 4.3.3.1  90/06/20  22:38:06  davison
  8.  * Initial Trn Release
  9.  * 
  10.  * Revision 4.3.2.2  90/11/22  16:09:54  sob
  11.  * Added changes to accomodate picky C preprocessors
  12.  * 
  13.  * Revision 4.3.2.1  90/03/22  23:04:41  sob
  14.  * Fixes provided by Wayne Davison <drivax!davison>
  15.  * 
  16.  * Revision 4.3.1.3  85/05/29  09:11:52  lwall
  17.  * Suppressed some killing messages on -t.
  18.  * 
  19.  * Revision 4.3.1.2  85/05/10  14:21:29  lwall
  20.  * Prevented THRU from setting art < absfirst.
  21.  * 
  22.  * Revision 4.3.1.1  85/05/10  11:34:33  lwall
  23.  * Branch for patches.
  24.  * 
  25.  * Revision 4.3  85/05/01  11:41:53  lwall
  26.  * Baseline for release with 4.3bsd.
  27.  * 
  28.  */
  29.  
  30. #include "EXTERN.h"
  31. #include "common.h"
  32. #include "term.h"
  33. #include "util.h"
  34. #include "artsrch.h"
  35. #include "ng.h"
  36. #include "bits.h"
  37. #include "intrp.h"
  38. #include "ngstuff.h"
  39. #include "rcstuff.h"
  40. #include "rn.h"
  41. #ifdef USETHREADS
  42. #include "rthreads.h"
  43. #endif
  44. #include "INTERN.h"
  45. #include "kfile.h"
  46.  
  47. static bool exitcmds = FALSE;
  48.  
  49. void
  50. kfile_init()
  51. {
  52.     ;
  53. }
  54.  
  55. #ifndef KILLFILES
  56. int
  57. edit_kfile()
  58. {
  59.     notincl("^K");
  60.     return -1;
  61. }
  62.  
  63. #else /* KILLFILES */
  64.  
  65. char killglobal[] = KILLGLOBAL;
  66. char killlocal[] = KILLLOCAL;
  67.  
  68. void
  69. mention(str)
  70. char *str;
  71. {
  72. #ifdef VERBOSE
  73.     IF(verbose) {
  74. #ifdef NOFIREWORKS
  75.     no_sofire();
  76. #endif
  77.     standout();
  78.     fputs(str,stdout);
  79.     un_standout();
  80.     putchar('\n');
  81.     }
  82.     ELSE
  83. #endif
  84. #ifdef TERSE
  85.     putchar('.');
  86. #endif
  87.     fflush(stdout);
  88. }
  89.  
  90. bool kill_mentioned;
  91.  
  92. int
  93. do_kfile(kfp,entering)
  94. FILE *kfp;
  95. int entering;
  96. {
  97. #ifdef USETHREADS
  98.     int i;
  99.     ART_NUM kill_thread;
  100. #endif
  101.  
  102.     art = lastart+1;
  103.     fseek(kfp,0L,0);            /* rewind file */
  104.     while (fgets(buf,LBUFLEN,kfp) != Nullch) {
  105.     buf[strlen(buf)-1] = '\0';
  106.     if (strnEQ(buf,"THRU",4)) {
  107.         ART_NUM tmpart;
  108.  
  109.         tmpart = atol(buf+4)+1;
  110.         if (tmpart < absfirst)
  111.         tmpart = absfirst;
  112.         check_first(tmpart);
  113.         firstart = tmpart;
  114.         continue;
  115.     }
  116.     if (*buf == 'X') {        /* exit command? */
  117.         if (entering) {
  118.         exitcmds = TRUE;
  119.         continue;
  120.         }
  121.         strcpy(buf,buf+1);
  122.     }
  123.     else {
  124.         if (!entering)
  125.         continue;
  126.     }
  127.     if (*buf == '&') {
  128.         mention(buf);
  129.         switcheroo();
  130.     }
  131.     else if (*buf == '/' && firstart <= lastart) {
  132.         mention(buf);
  133.         kill_mentioned = TRUE;
  134.         switch (art_search(buf, (sizeof buf), FALSE)) {
  135.         case SRCH_ABORT:
  136.         continue;
  137.         case SRCH_INTR:
  138. #ifdef VERBOSE
  139.         IF(verbose)
  140.             printf("\n(Interrupted at article %ld)\n",(long)art)
  141.               FLUSH;
  142.         ELSE
  143. #endif
  144. #ifdef TERSE
  145.             printf("\n(Intr at %ld)\n",(long)art) FLUSH;
  146. #endif
  147.         return -1;
  148.         case SRCH_DONE:
  149.         break;
  150.         case SRCH_SUBJDONE:
  151.         fputs("\tsubject not found (???)\n",stdout) FLUSH;
  152.         break;
  153.         case SRCH_NOTFOUND:
  154.         fputs("\tnot found\n",stdout) FLUSH;
  155.         break;
  156.         case SRCH_FOUND:
  157.         fputs("\tfound\n",stdout) FLUSH;
  158.         }
  159.     }
  160. #ifdef USETHREADS
  161.     else if (*buf == 'T' && firstart <= lastart && p_roots) {
  162.         /* kill a thread by its root id number */
  163.         kill_thread = atol(buf+1);
  164.         for (i = 0; i < total.root; i++) {
  165.         if (p_roots[i].root_num == kill_thread) {
  166.             if (count_one_root(i) != 0) {
  167.             mention(buf);
  168.             kill_mentioned = TRUE;
  169.             printf("%ldx%d ",(long)kill_thread,
  170.                      root_article_cnts[i]);
  171.             p_art = p_articles + p_roots[i].articles;
  172.             art = p_art->num;
  173.             follow_thread('k');
  174.             }
  175.             break;
  176.         }
  177.         }
  178.     }
  179. #endif
  180.     }
  181.  
  182.     return 0;
  183. }
  184.  
  185. void
  186. kill_unwanted(starting,message,entering)
  187. ART_NUM starting;
  188. char *message;
  189. int entering;
  190. {
  191.     bool intr = FALSE;            /* did we get an interrupt? */
  192.     ART_NUM oldfirst;
  193.     bool anytokill = (toread[ng] > 0);
  194.  
  195.     if (localkfp || globkfp) {
  196.     if (!entering && !exitcmds)
  197.         return;
  198.     exitcmds = FALSE;
  199.     oldfirst = firstart;
  200.     firstart = starting;
  201.     clear();
  202. #ifdef VERBOSE
  203. # ifdef TERSE
  204.     if (message && (verbose || entering))
  205. # else
  206.     if (message)
  207. # endif
  208. #else
  209.     if (message && entering)
  210. #endif
  211.         fputs(message,stdout) FLUSH;
  212.  
  213.     kill_mentioned = FALSE;
  214.     if (localkfp)
  215.         intr = do_kfile(localkfp,entering);
  216.     if (globkfp && !intr)
  217.         intr = do_kfile(globkfp,entering);
  218.     if (entering && localkfp && !intr)
  219.         setthru(lastart);
  220.     putchar('\n') FLUSH;
  221.     if (entering && kill_mentioned)
  222. #ifdef VERBOSE
  223.         IF(verbose)
  224.         get_anything();
  225.         ELSE
  226. #endif
  227. #ifdef TERSE
  228.         pad(just_a_sec);
  229. #endif
  230.     if (anytokill)            /* if there was anything to kill */
  231.         forcelast = FALSE;        /* allow for having killed it all */
  232.     firstart = oldfirst;
  233.     }
  234. }
  235.  
  236. void
  237. setthru(thru)
  238. ART_NUM thru;
  239. {
  240.     FILE *newkfp;
  241.     bool no_kills = 0;
  242. #ifdef USETHREADS
  243.     int i;
  244.     ART_NUM kill_thread;
  245. #endif
  246.  
  247.     fseek(localkfp,0L,0);        /* rewind current file */
  248.     if (fgets(buf,LBUFLEN,localkfp) != Nullch
  249.      && (strnNE(buf,"THRU",4) || fgets(buf,LBUFLEN,localkfp) != Nullch))
  250.     fseek(localkfp,0L,0);
  251.     else
  252.     no_kills = 1;
  253.     strcpy(buf,filexp(getval("KILLLOCAL",killlocal)));
  254.     UNLINK(buf);            /* to prevent file reuse */
  255.     if (no_kills)
  256.     open_kfile(KF_LOCAL);        /* close file and reset open flag */
  257.     else if (newkfp = fopen(buf,"w")) {
  258.     fprintf(newkfp,"THRU %ld\n",(long)thru);
  259.     while (fgets(buf,LBUFLEN,localkfp) != Nullch) {
  260.         if (strnEQ(buf,"THRU",4))
  261.         continue;
  262. #ifdef USETHREADS
  263.         /* Leave out any outdated thread kills */
  264.         if (*buf == 'T' && p_roots) {
  265.         kill_thread = atol(buf+1);
  266.         for (i = 0; i < total.root; i++) {
  267.             if (p_roots[i].root_num == kill_thread) {
  268.             break;
  269.             }
  270.         }
  271.         if (i == total.root)
  272.             continue;
  273.         }
  274. #endif
  275.         fputs(buf,newkfp);
  276.     }
  277.     fclose(newkfp);
  278.     open_kfile(KF_LOCAL);        /* and reopen local file */
  279.     }
  280.     else
  281.     printf(cantcreate,buf) FLUSH;
  282. }
  283.  
  284. /* edit KILL file for newsgroup */
  285.  
  286. int
  287. edit_kfile()
  288. {
  289.     int r = -1;
  290.  
  291.     if (in_ng)
  292.     strcpy(buf,filexp(getval("KILLLOCAL",killlocal)));
  293.     else
  294.     strcpy(buf,filexp(getval("KILLGLOBAL",killglobal)));
  295.     if ((r = makedir(buf,MD_FILE)) >= 0) {
  296.     sprintf(cmd_buf,"%s %s",
  297.         filexp(getval("VISUAL",getval("EDITOR",defeditor))),buf);
  298.     printf("\nEditing %s KILL file:\n%s\n",
  299.         (in_ng?"local":"global"),cmd_buf) FLUSH;
  300.     resetty();            /* make sure tty is friendly */
  301.     r = doshell(sh,cmd_buf);/* invoke the shell */
  302.     noecho();            /* and make terminal */
  303.     crmode();            /*   unfriendly again */
  304.     open_kfile(in_ng);
  305.     }
  306.     else
  307.     printf("Can't make %s\n",buf) FLUSH;
  308.     return r;
  309. }
  310.  
  311. void
  312. open_kfile(local)
  313. int local;
  314. {
  315.     char *kname = filexp(local ?
  316.     getval("KILLLOCAL",killlocal) :
  317.     getval("KILLGLOBAL",killglobal)
  318.     );
  319.     
  320.     stat(kname,&filestat);
  321.     if (!filestat.st_size)        /* nothing in the file? */
  322.     UNLINK(kname);            /* delete the file */
  323.     if (local) {
  324.     if (localkfp)
  325.         fclose(localkfp);
  326.     localkfp = fopen(kname,"r");
  327.     }
  328.     else {
  329.     if (globkfp)
  330.         fclose(globkfp);
  331.     globkfp = fopen(kname,"r");
  332.     }
  333. }
  334.  
  335. void
  336. kf_append(cmd)
  337. char *cmd;
  338. {
  339.     strcpy(cmd_buf,filexp(getval("KILLLOCAL",killlocal)));
  340.     if (makedir(cmd_buf,MD_FILE) >= 0) {
  341. #ifdef VERBOSE
  342.     IF(verbose)
  343.         printf("\nDepositing command in %s...",cmd_buf);
  344.     ELSE
  345. #endif
  346. #ifdef TERSE
  347.         printf("\n--> %s...",cmd_buf);
  348. #endif
  349.     fflush(stdout);
  350.     sleep(2);
  351.     if ((tmpfp = fopen(cmd_buf,"a")) != Nullfp) {
  352.         fseek(tmpfp,0L,2);        /* get to EOF for sure */
  353.         fprintf(tmpfp,"%s\n",cmd);
  354.         fclose(tmpfp);
  355.         fputs("done\n",stdout) FLUSH;
  356.     }
  357.     else
  358.         printf(cantopen,cmd_buf) FLUSH;
  359.     }
  360. }
  361. #endif /* KILLFILES */
  362.