Introduced modular filters
[privoxy.git] / loaders.c
index 74fab48..7036158 100644 (file)
--- a/loaders.c
+++ b/loaders.c
@@ -1,4 +1,4 @@
-const char loaders_rcs[] = "$Id: loaders.c,v 1.35 2002/01/17 21:03:08 jongfoster Exp $";
+const char loaders_rcs[] = "$Id: loaders.c,v 1.40 2002/03/08 17:46:04 jongfoster Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loaders.c,v $
@@ -35,6 +35,28 @@ const char loaders_rcs[] = "$Id: loaders.c,v 1.35 2002/01/17 21:03:08 jongfoster
  *
  * Revisions   :
  *    $Log: loaders.c,v $
+ *    Revision 1.40  2002/03/08 17:46:04  jongfoster
+ *    Fixing int/size_t warnings
+ *
+ *    Revision 1.39  2002/03/07 03:46:17  oes
+ *    Fixed compiler warnings
+ *
+ *    Revision 1.38  2002/03/06 22:54:35  jongfoster
+ *    Automated function-comment nitpicking.
+ *
+ *    Revision 1.37  2002/03/03 15:07:49  oes
+ *    Re-enabled automatic config reloading
+ *
+ *    Revision 1.36  2002/01/22 23:46:18  jongfoster
+ *    Moving edit_read_line() and simple_read_line() to loaders.c, and
+ *    extending them to support reading MS-DOS, Mac and UNIX style files
+ *    on all platforms.
+ *
+ *    Modifying read_config_line() (without changing it's prototype) to
+ *    be a trivial wrapper for edit_read_line().  This means that we have
+ *    one function to read a line and handle comments, which is common
+ *    between the initialization code and the edit interface.
+ *
  *    Revision 1.35  2002/01/17 21:03:08  jongfoster
  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
  *
@@ -422,8 +444,7 @@ int check_file_changed(const struct file_list * current,
        && (current->lastmodified == statbuf->st_mtime)
        && (0 == strcmp(current->filename, filename)))
    {
-       /* force reload of configfile and all the logs */
-       if ( !MustReload ) return 0;
+      return 0;
    }
 
    fs = (struct file_list *)zalloc(sizeof(struct file_list));
@@ -477,8 +498,8 @@ int check_file_changed(const struct file_list * current,
  *********************************************************************/
 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
 {
-   int len = 0;
-   int buflen = BUFFER_SIZE;
+   size_t len = 0;
+   size_t buflen = BUFFER_SIZE;
    char * buf;
    char * p;
    int ch;
@@ -872,13 +893,13 @@ jb_err edit_read_line(FILE *fp,
  *          1  :  buf = Buffer to use.
  *          2  :  buflen = Size of buffer in bytes.
  *          3  :  fp = File to read from
- *         4  :  linenum = linenumber in file
+ *          4  :  linenum = linenumber in file
  *
  * Returns     :  NULL on EOF or error
  *                Otherwise, returns buf.
  *
  *********************************************************************/
-char *read_config_line(char *buf, int buflen, FILE *fp, unsigned long *linenum)
+char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
 {
    jb_err err;
    char *buf2 = NULL;
@@ -894,7 +915,7 @@ char *read_config_line(char *buf, int buflen, FILE *fp, unsigned long *linenum)
    else
    {
       assert(buf2);
-      assert(strlen(buf2) + 1U < (unsigned)buflen);
+      assert(strlen(buf2) + 1U < buflen);
       strncpy(buf, buf2, buflen - 1);
       free(buf2);
       buf[buflen - 1] = '\0';
@@ -1073,7 +1094,8 @@ load_trustfile_error:
  *
  * Function    :  unload_re_filterfile
  *
- * Description :  Unload the re_filter list.
+ * Description :  Unload the re_filter list by freeing all chained
+ *                re_filterfile specs and their data.
  *
  * Parameters  :
  *          1  :  f = the data structure associated with the filterfile.
@@ -1083,27 +1105,31 @@ load_trustfile_error:
  *********************************************************************/
 static void unload_re_filterfile(void *f)
 {
-   struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
+   struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
 
-   if (b == NULL)
+   while (b != NULL)
    {
-      return;
-   }
+      a = b->next;
 
-   destroy_list(b->patterns);
-   pcrs_free_joblist(b->joblist);
-   freez(b);
+      destroy_list(b->patterns);
+      pcrs_free_joblist(b->joblist);
+      freez(b);
+
+      b = a;
+   }
 
    return;
 }
 
+
 /*********************************************************************
  *
  * Function    :  load_re_filterfile
  *
- * Description :  Load the re_filterfile. Each non-comment, non-empty
- *                line is instantly added to the joblist, which is
- *                a chained list of pcrs_job structs.
+ * Description :  Load the re_filterfile. 
+ *                Generate a chained list of re_filterfile_spec's from
+ *                the "FILTER: " blocks, compiling all their substitutions
+ *                into chained lists of pcrs_job structs.
  *
  * Parameters  :
  *          1  :  csp = Current client state (buffers, headers, etc...)
@@ -1115,7 +1141,7 @@ int load_re_filterfile(struct client_state *csp)
 {
    FILE *fp;
 
-   struct re_filterfile_spec *bl;
+   struct re_filterfile_spec *bl, *new_bl;
    struct file_list *fs;
 
    char  buf[BUFFER_SIZE];
@@ -1123,9 +1149,11 @@ int load_re_filterfile(struct client_state *csp)
    unsigned long linenum = 0;
    pcrs_job *dummy;
 
+   /*
+    * No need to reload if unchanged
+    */
    if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
    {
-      /* No need to load */
       if (csp)
       {
          csp->rlist = current_re_filterfile;
@@ -1137,46 +1165,87 @@ int load_re_filterfile(struct client_state *csp)
       goto load_re_filterfile_error;
    }
 
+   /*
+    * Allocate the first re_filterfile_spec struct
+    */
    fs->f = bl = (struct re_filterfile_spec  *)zalloc(sizeof(*bl));
    if (bl == NULL)
    {
       goto load_re_filterfile_error;
    }
 
-   /* Open the file or fail */
+   /*
+    * Initialize the name in case there are
+    * expressions before the first block header
+    */
+   bl->filtername = "default";
+
+   /* 
+    * Open the file or fail
+    */
    if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
    {
       goto load_re_filterfile_error;
    }
 
-   /* Read line by line */
+   /* 
+    * Read line by line
+    */
    while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
    {
-      enlist( bl->patterns, buf );
+      /*
+       * If this is the head of a new filter block, make it a
+       * re_filterfile spec of its own and chain it to the list:
+       */
+      if (strncmp(buf, "FILTER:", 7) == 0)
+      {
+         new_bl = (struct re_filterfile_spec  *)zalloc(sizeof(*bl));
+         if (new_bl == NULL)
+         {
+            goto load_re_filterfile_error;
+         }
+         else
+         {
+            new_bl->filtername = strdup(chomp(buf + 7));
+            bl->next = new_bl;
+            bl = new_bl;
+         }
+         continue;
+      }
+
+      /* 
+       * Else, save the expression, make it a pcrs_job
+       * and chain it into the current filter's joblist 
+       */
+      enlist(bl->patterns, buf);
 
-      /* We have a meaningful line -> make it a job */
       if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
       {
          log_error(LOG_LEVEL_RE_FILTER,
-               "Adding re_filter job %s failed with error %d.", buf, error);
+               "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->filtername, error);
          continue;
       }
       else
       {
          dummy->next = bl->joblist;
          bl->joblist = dummy;
-         log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
+         log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->filtername);
       }
    }
 
    fclose(fp);
 
-   /* the old one is now obsolete */
+   /* 
+    * Schedule the now-obsolete old data for unloading
+    */
    if ( NULL != current_re_filterfile )
    {
       current_re_filterfile->unloader = unload_re_filterfile;
    }
 
+   /*
+    * Chain this file into the global list of loaded files
+    */
    fs->next    = files->next;
    files->next = fs;
    current_re_filterfile = fs;