Tidying up memory allocation. New function init_action().
[privoxy.git] / actions.c
index 9388449..909cd68 100644 (file)
--- a/actions.c
+++ b/actions.c
@@ -1,4 +1,4 @@
-const char actions_rcs[] = "$Id: actions.c,v 1.1 2001/05/31 21:16:46 jongfoster Exp $";
+const char actions_rcs[] = "$Id: actions.c,v 1.10 2001/09/10 10:14:34 oes Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/actions.c,v $
@@ -33,6 +33,38 @@ const char actions_rcs[] = "$Id: actions.c,v 1.1 2001/05/31 21:16:46 jongfoster
  *
  * Revisions   :
  *    $Log: actions.c,v $
+ *    Revision 1.10  2001/09/10 10:14:34  oes
+ *    Removing unused variable
+ *
+ *    Revision 1.9  2001/07/30 22:08:36  jongfoster
+ *    Tidying up #defines:
+ *    - All feature #defines are now of the form FEATURE_xxx
+ *    - Permanently turned off WIN_GUI_EDIT
+ *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
+ *
+ *    Revision 1.8  2001/06/29 13:19:52  oes
+ *    Removed logentry from cancelled commit
+ *
+ *    Revision 1.7  2001/06/09 10:55:28  jongfoster
+ *    Changing BUFSIZ ==> BUFFER_SIZE
+ *
+ *    Revision 1.6  2001/06/07 23:04:34  jongfoster
+ *    Made get_actions() static.
+ *
+ *    Revision 1.5  2001/06/03 19:11:48  oes
+ *    adapted to new enlist_unique arg format
+ *
+ *    Revision 1.4  2001/06/01 20:03:42  jongfoster
+ *    Better memory management - current_action->strings[] now
+ *    contains copies of the strings, not the original.
+ *
+ *    Revision 1.3  2001/06/01 18:49:17  jongfoster
+ *    Replaced "list_share" with "list" - the tiny memory gain was not
+ *    worth the extra complexity.
+ *
+ *    Revision 1.2  2001/05/31 21:40:00  jongfoster
+ *    Removing some commented out, obsolete blocks of code.
+ *
  *    Revision 1.1  2001/05/31 21:16:46  jongfoster
  *    Moved functions to process the action list into this new file.
  *
@@ -55,6 +87,8 @@ const char actions_rcs[] = "$Id: actions.c,v 1.1 2001/05/31 21:16:46 jongfoster
 
 const char actions_h_rcs[] = ACTIONS_H_VERSION;
 
+
+/* Turn off everything except forwarding */
 /* This structure is used to hold user-defined aliases */
 struct action_alias
 {
@@ -66,9 +100,9 @@ struct action_alias
 
 /*
  * Must declare this in this file for the above structure.
- * FIXME: Make this static or put structure in header.
  */
-extern int get_actions (char *line, struct action_alias * alias_list,
+static int get_actions (char *line, 
+                        struct action_alias * alias_list,
                         struct action_spec *cur_action);
 
 /*
@@ -202,6 +236,8 @@ void merge_actions (struct action_spec *dest,
  *
  * Description :  Copy an action_specs.
  *                Similar to "cur_action = new_action".
+ *                Note that dest better not contain valid data
+ *                - it's overwritten, not freed.
  *
  * Parameters  :
  *          1  :  dest = Destination of copy.
@@ -215,6 +251,8 @@ void copy_action (struct action_spec *dest,
 {
    int i;
 
+   memset(dest, '\0', sizeof(*dest));
+
    dest->mask = src->mask;
    dest->add  = src->add;
 
@@ -237,7 +275,9 @@ void copy_action (struct action_spec *dest,
  *
  * Function    :  free_action
  *
- * Description :  Free an action_specs.
+ * Description :  Destroy an action_spec.  Frees memory used by it,
+ *                except for the memory used by the struct action_spec
+ *                itself.
  *
  * Parameters  :
  *          1  :  src = Source to free.
@@ -387,10 +427,11 @@ int get_action_token(char **line, char **name, char **value)
  *                nonzero => Error (line was trashed anyway)
  *
  *********************************************************************/
-int get_actions(char *line, struct action_alias * alias_list,
-                struct action_spec *cur_action)
+static int get_actions(char *line,
+                       struct action_alias * alias_list,
+                       struct action_spec *cur_action)
 {
-   memset(cur_action, '\0', sizeof(*cur_action));
+   init_action(cur_action);
    cur_action->mask = ACTION_MASK_ALL;
 
    while (line)
@@ -459,7 +500,7 @@ int get_actions(char *line, struct action_alias * alias_list,
                   }
 
                   list_remove_item(remove, value);
-                  enlist_unique(add, value);
+                  enlist_unique(add, value, 0);
                   break;
                }
             case AV_REM_MULTI:
@@ -488,7 +529,7 @@ int get_actions(char *line, struct action_alias * alias_list,
                      if ( !cur_action->multi_remove_all[action->index] )
                      {
                         /* there isn't a catch-all in the remove list already */
-                        enlist_unique(remove, value);
+                        enlist_unique(remove, value, 0);
                      }
                      list_remove_item(add, value);
                   }
@@ -627,7 +668,7 @@ char * current_action_to_text(struct current_action_spec *action)
 {
    unsigned flags  = action->flags;
    char * result = strdup("");
-   struct list_share * lst;
+   struct list * lst;
 
 #define DEFINE_ACTION_BOOL(__name, __bit)   \
    if (flags & __bit)                       \
@@ -700,6 +741,24 @@ void init_current_action (struct current_action_spec *dest)
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  init_action
+ *
+ * Description :  Zero out an action.
+ *
+ * Parameters  :
+ *          1  :  dest = An uninitialized action_spec.
+ *
+ * Returns     :  N/A
+ *
+ *********************************************************************/
+void init_action (struct action_spec *dest)
+{
+   memset(dest, '\0', sizeof(*dest));
+}
+
+
 /*********************************************************************
  *
  * Function    :  merge_current_action
@@ -710,7 +769,7 @@ void init_current_action (struct current_action_spec *dest)
  *                is that this one doesn't allocate memory for
  *                strings (so "src" better be in memory for at least
  *                as long as "dest" is, and you'd better free
- *                "dest" using "current_free_action").
+ *                "dest" using "free_current_action").
  *                Also, there is no  mask or remove lists in dest.
  *                (If we're applying it to a URL, we don't need them)
  *
@@ -734,7 +793,8 @@ void merge_current_action (struct current_action_spec *dest,
       char * str = src->string[i];
       if (str)
       {
-         dest->string[i] = str;
+         freez(dest->string[i]);
+         dest->string[i] = strdup(str);
       }
    }
 
@@ -743,13 +803,13 @@ void merge_current_action (struct current_action_spec *dest,
       if (src->multi_remove_all[i])
       {
          /* Remove everything from dest */
-         destroy_list_share(dest->multi[i]);
-         list_duplicate_share(dest->multi[i], src->multi_add[i]);
+         destroy_list(dest->multi[i]);
+         list_duplicate(dest->multi[i], src->multi_add[i]);
       }
       else
       {
-         list_remove_list_share(dest->multi[i], src->multi_remove[i]);
-         list_append_list_unique_share(dest->multi[i], src->multi_add[i]);
+         list_remove_list(dest->multi[i], src->multi_remove[i]);
+         list_append_list_unique(dest->multi[i], src->multi_add[i]);
       }
    }
 }
@@ -759,7 +819,8 @@ void merge_current_action (struct current_action_spec *dest,
  *
  * Function    :  free_current_action
  *
- * Description :  Free a current_action_spec.
+ * Description :  Free memory used by a current_action_spec.
+ *                Does not free the current_action_spec itself.
  *
  * Parameters  :
  *          1  :  src = Source to free.
@@ -771,9 +832,14 @@ void free_current_action (struct current_action_spec *src)
 {
    int i;
 
+   for (i = 0; i < ACTION_STRING_COUNT; i++)
+   {
+      freez(src->string[i]);
+   }
+
    for (i = 0; i < ACTION_MULTI_COUNT; i++)
    {
-      destroy_list_share(src->multi[i]);
+      destroy_list(src->multi[i]);
    }
 
    memset(src, '\0', sizeof(*src));
@@ -801,6 +867,7 @@ void unload_actions_file(void *file_data)
    {
       next = cur->next;
       free_url(cur->url);
+      free_action(cur->action);
       freez(cur);
       cur = next;
    }
@@ -829,7 +896,7 @@ int load_actions_file(struct client_state *csp)
 
    struct url_actions *last_perm;
    struct url_actions *perm;
-   char  buf[BUFSIZ];
+   char  buf[BUFFER_SIZE];
    struct file_list *fs;
 #define MODE_START_OF_FILE 1
 #define MODE_ACTIONS       2
@@ -838,7 +905,7 @@ int load_actions_file(struct client_state *csp)
    struct action_spec cur_action[1];
    struct action_alias * alias_list = NULL;
 
-   memset(cur_action, '\0', sizeof(*cur_action));
+   init_action(cur_action);
 
    if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
    {
@@ -926,7 +993,7 @@ int load_actions_file(struct client_state *csp)
          {
             /* It's an actions block */
 
-            char  actions_buf[BUFSIZ];
+            char  actions_buf[BUFFER_SIZE];
             char * end;
 
             /* set mode */
@@ -978,9 +1045,8 @@ int load_actions_file(struct client_state *csp)
       else if (mode == MODE_ALIAS)
       {
          /* define an alias */
-         char  actions_buf[BUFSIZ];
+         char  actions_buf[BUFFER_SIZE];
          struct action_alias * new_alias;
-         int more = 1;
 
          char * start = strchr(buf, '=');
          char * end = start;
@@ -1110,13 +1176,6 @@ int load_actions_file(struct client_state *csp)
       alias_list = next;
    }
 
-#ifndef SPLIT_PROXY_ARGS
-   if (!suppress_blocklists)
-   {
-      fs->proxy_args = strsav(fs->proxy_args, "</pre>");
-   }
-#endif /* ndef SPLIT_PROXY_ARGS */
-
    /* the old one is now obsolete */
    if (current_actions_file)
    {