home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 479a.lha / barn_v2.01 / source / kill.c < prev    next >
Text File  |  1991-02-10  |  6KB  |  217 lines

  1. /*
  2.  *    File Name:        kill.c
  3.  *    Project:        BARN - Bah's Amiga ReadNews.
  4.  *    Purpose:        Defines functions related to kill list.
  5.  *    Functions:        GetKillList, PutKillList, DestroyKillList.
  6.  *    Author:            Jeff Van Epps
  7.  *    Created:        04 Sep 89
  8.  *    Last Modified:    22 Oct 90
  9.  *    Comments:
  10.  *    History:
  11.  *        04 Sep 89/JVE    Created.
  12.  *        30 Dec 89/JVE    Implemented GetKillList and PutKillList, which were
  13.  *                        only stubbed previously.
  14.  *        22 Oct 90/JVE    Added regular expression capability in kill files.
  15.  *                        A kill list is no longer just a list of HEADER_INFOs,
  16.  *                        it now has its own structure.
  17.  */
  18.  
  19. # include    <stdio.h>
  20. # include    <stdlib.h>
  21. # include    <string.h>
  22. # include    <fcntl.h>
  23. # include    <regexp.h>
  24. # include    "standard.h"
  25. # include    "article.h"
  26. # include    "kill.h"
  27.  
  28.  
  29. /****************************************************************************/
  30. /*    FUNCTION:    GetKillList                                                    */
  31. /*                                                                            */
  32. /*    PURPOSE:    Parse the kill file which applies to all newsgroups.        */
  33. /*                                                                            */
  34. /*    INPUT PARAMETERS:                                                        */
  35. /*        NAME        I/O        DESCRIPTION                                        */
  36. /*        ----        ---        -----------                                        */
  37. /*        filename     I        Name of kill file.                                */
  38. /*                                                                            */
  39. /*    RETURNS:                                                                */
  40. /*        (KILL_INFO *)    Pointer to start of kill list.                        */
  41. /*                                                                            */
  42. /*    COMMENTS:                                                                */
  43. /*                                                                            */
  44. /*    HISTORY:                                                                */
  45. /*        1.    30 Dec 89        Created.                                        */
  46. /*                                                                            */
  47. /****************************************************************************/
  48.  
  49. KILL_INFO    *GetKillList( filename )
  50.  
  51. char        *filename;
  52.  
  53. {
  54. KILL_INFO    *root = NULLP( KILL_INFO );
  55. KILL_INFO    **ptr;
  56. FILE        *fp;
  57.  
  58. if ( ( fp = fopen( filename, "r" ) ) != NULLP( FILE ) )
  59.     {
  60.     ptr = &root;
  61.     while ( GetNextKill( fp, &ptr ) ) ;
  62.     fclose( fp );
  63.     }
  64. return root;
  65. }
  66.  
  67.  
  68. /****************************************************************************/
  69. /*    FUNCTION:    PutKillList                                                    */
  70. /*                                                                            */
  71. /*    PURPOSE:    Write back a kill list to specified file.                    */
  72. /*                                                                            */
  73. /*    INPUT PARAMETERS:                                                        */
  74. /*        NAME        I/O        DESCRIPTION                                        */
  75. /*        ----        ---        -----------                                        */
  76. /*        filename     I        File in which to put kill list.                    */
  77. /*        root         I        Pointer to start of kill list.                    */
  78. /*                                                                            */
  79. /*    RETURNS: none                                                            */
  80. /*                                                                            */
  81. /*    COMMENTS:                                                                */
  82. /*                                                                            */
  83. /*    HISTORY:                                                                */
  84. /*        1.    30 Dec 89        Created.                                        */
  85. /*                                                                            */
  86. /****************************************************************************/
  87.  
  88. void PutKillList( filename, root )
  89.  
  90. char        *filename;
  91. KILL_INFO    *root;
  92.  
  93. {
  94. FILE        *fp;
  95.  
  96. remove( filename );
  97. if ( ( fp = fopen( filename, "a" ) ) != NULLP( FILE ) )
  98.     {
  99.     while ( root != NULLP( KILL_INFO ) )
  100.         {
  101.         fprintf( fp, "%s: %s\n", root->fieldname, root->fieldvalue );
  102.         root = root->next;
  103.         }
  104.     fclose( fp );
  105.     }
  106. }
  107.  
  108.  
  109. /****************************************************************************/
  110. /*    FUNCTION:    DestroyKillList                                                */
  111. /*                                                                            */
  112. /*    PURPOSE:    Release memory consumed by construction of kill list.        */
  113. /*                                                                            */
  114. /*    INPUT PARAMETERS:                                                        */
  115. /*        NAME        I/O        DESCRIPTION                                        */
  116. /*        ----        ---        -----------                                        */
  117. /*        root         I        Pointer to start of kill list.                    */
  118. /*                                                                            */
  119. /*    RETURNS: none                                                            */
  120. /*                                                                            */
  121. /*    COMMENTS:                                                                */
  122. /*                                                                            */
  123. /*    HISTORY:                                                                */
  124. /*        1.    04 Sep 89        Created.                                        */
  125. /*                                                                            */
  126. /****************************************************************************/
  127.  
  128. void DestroyKillList( root )
  129.  
  130. KILL_INFO        *root;
  131.  
  132. {
  133. KILL_INFO        *tmp;
  134.  
  135. for ( ; root != NULLP( KILL_INFO ); root = tmp )
  136.     {
  137.     tmp = root->next;
  138.     free( root -> fieldname );
  139.     free( root -> fieldvalue );
  140.     free( (char *) ( root -> pattern ) );
  141.     free( root );
  142.     }
  143. }
  144.  
  145. /****************************************************************************/
  146. /*    FUNCTION:    GetNextKill                                                    */
  147. /*                                                                            */
  148. /*    PURPOSE:    Parses the next kill entry from a kill file.                */
  149. /*                                                                            */
  150. /*    INPUT PARAMETERS:                                                        */
  151. /*        NAME        I/O        DESCRIPTION                                        */
  152. /*        ----        ---        -----------                                        */
  153. /*        fp             I        FILE pointer to file containing article.        */
  154. /*        hdr            I/O        Where to put pointer to newly allocated            */
  155. /*                            KILL_INFO structure if we allocate one.            */
  156. /*                                                                            */
  157. /*    RETURNS:                                                                */
  158. /*        TRUE    Successfully parsed another entry from file.                */
  159. /*        FALSE    Failed/no more entries.                                        */
  160. /*                                                                            */
  161. /*    COMMENTS:                                                                */
  162. /*        Adjusts hdr when done so next call will set correct pointer.        */
  163. /*                                                                            */
  164. /*    HISTORY:                                                                */
  165. /*        1.    22 Oct 90        Created from GetNextHeader.                        */
  166. /*                                                                            */
  167. /****************************************************************************/
  168.  
  169. GetNextKill( fp, hdr )
  170.  
  171. FILE            *fp;
  172. KILL_INFO        ***hdr;
  173.  
  174. {
  175. int                rc;                /* return code from function */
  176. char            buf[BUFSIZ];    /* holds input line from article */
  177. char            *firstspace, *firstcolon, *data;    /* for parsing */
  178.  
  179. if ( fgets( buf, BUFSIZ, fp ) == NULL )
  180.     rc = FALSE;
  181. else
  182.     {
  183.     firstspace = strchr( buf, ' ' );
  184.     firstcolon = strchr( buf, ':' );
  185.     if ( ( firstcolon == NULL ) || 
  186.       ( ( firstspace != NULL ) && ( firstspace < firstcolon ) ) )
  187.         rc = FALSE;
  188.     else
  189.         {
  190.         *firstcolon = NULL;        /* terminate fieldname string */
  191.         firstcolon++;            /* advance past colon/NULL to value string */
  192.         data = firstcolon + strspn( firstcolon, " \t" );    /* skip white */
  193.         data[strlen(data)-1] = NULL;    /* remove trailing newline */
  194.  
  195.         /*
  196.          *    Allocate and fill a new kill structure.
  197.          */
  198.  
  199.         **hdr = (KILL_INFO *) malloc( sizeof( KILL_INFO ) );
  200.         (**hdr)->fieldname = strdup( buf );
  201.         (**hdr)->fieldvalue = strdup( data );
  202.         (**hdr)->pattern = regcomp( data );
  203.         if ( (**hdr) -> pattern == NULL )
  204.             fprintf( stderr, "Bad kill pattern '%s'\n", data );
  205.         (**hdr)->next = NULLP( KILL_INFO );
  206.  
  207.         /*
  208.          *    Reset the hdr pointer so next call will continue chain correctly.
  209.          */
  210.  
  211.         *hdr = &((**hdr)->next);
  212.         rc = TRUE;
  213.         }
  214.     }
  215. return rc;
  216. }
  217.