Adding Gabor Liptak, who reported a bug with the CygWin build.
[privoxy.git] / actions.c
index 3b01d36..13a6241 100644 (file)
--- a/actions.c
+++ b/actions.c
@@ -1,4 +1,4 @@
-const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 jongfoster Exp $";
+const char actions_rcs[] = "$Id: actions.c,v 1.18 2001/11/07 00:06:06 steudten Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/actions.c,v $
@@ -10,10 +10,10 @@ const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 jongfoster
  *                IJBSWA team.  http://ijbswa.sourceforge.net
  *
  *                Based on the Internet Junkbuster originally written
- *                by and Copyright (C) 1997 Anonymous Coders and 
+ *                by and Copyright (C) 1997 Anonymous Coders and
  *                Junkbusters Corporation.  http://www.junkbusters.com
  *
- *                This program is free software; you can redistribute it 
+ *                This program is free software; you can redistribute it
  *                and/or modify it under the terms of the GNU General
  *                Public License as published by the Free Software
  *                Foundation; either version 2 of the License, or (at
@@ -33,6 +33,19 @@ const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 jongfoster
  *
  * Revisions   :
  *    $Log: actions.c,v $
+ *    Revision 1.18  2001/11/07 00:06:06  steudten
+ *    Add line number in error output for lineparsing for
+ *    actionsfile.
+ *
+ *    Revision 1.17  2001/10/25 03:40:47  david__schmidt
+ *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
+ *    threads to call select() simultaneously.  So, it's time to do a real, live,
+ *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
+ *    (native). Both versions will work, but using __OS2__ offers multi-threading.
+ *
+ *    Revision 1.16  2001/10/23 21:30:30  jongfoster
+ *    Adding error-checking to selected functions.
+ *
  *    Revision 1.15  2001/10/14 21:58:22  jongfoster
  *    Adding support for the CGI-based editor:
  *    - Exported get_actions()
@@ -103,6 +116,7 @@ const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 jongfoster
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <stdlib.h>
 
 #include "project.h"
 #include "jcc.h"
@@ -135,7 +149,7 @@ const char actions_h_rcs[] = ACTIONS_H_VERSION;
 #define AV_REM_MULTI  4 /* -multiopt{string} -multiopt{*}       */
 
 /*
- * We need a structure to hold the name, flag changes, 
+ * We need a structure to hold the name, flag changes,
  * type, and string index.
  */
 struct action_name
@@ -194,7 +208,7 @@ static const struct action_name action_names[] =
  * Returns     :  JB_ERR_OK or JB_ERR_MEMORY
  *
  *********************************************************************/
-jb_err merge_actions (struct action_spec *dest, 
+jb_err merge_actions (struct action_spec *dest,
                       const struct action_spec *src)
 {
    int i;
@@ -242,7 +256,7 @@ jb_err merge_actions (struct action_spec *dest,
          /* No "remove all"s to worry about. */
          list_remove_list(dest->multi_add[i], src->multi_remove[i]);
          err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
-         err = err || list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
+         if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
       }
 
       if (err)
@@ -271,11 +285,11 @@ jb_err merge_actions (struct action_spec *dest,
  * Returns     :  N/A
  *
  *********************************************************************/
-jb_err copy_action (struct action_spec *dest, 
+jb_err copy_action (struct action_spec *dest,
                     const struct action_spec *src)
 {
    int i;
-   jb_err err;
+   jb_err err = JB_ERR_OK;
 
    memset(dest, '\0', sizeof(*dest));
 
@@ -299,13 +313,18 @@ jb_err copy_action (struct action_spec *dest,
    for (i = 0; i < ACTION_MULTI_COUNT; i++)
    {
       dest->multi_remove_all[i] = src->multi_remove_all[i];
-      err =        list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
-      err = err || list_duplicate(dest->multi_add[i],    src->multi_add[i]);
+      err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
+      if (err)
+      {
+         return err;
+      }
+      err = list_duplicate(dest->multi_add[i],    src->multi_add[i]);
       if (err)
       {
          return err;
       }
    }
+   return err;
 }
 
 
@@ -362,7 +381,7 @@ void free_action (struct action_spec *src)
  *                       we found an action.
  *          2  :  name = [out] Start of action name, null
  *                       terminated.  NULL on EOL
- *          3  :  value = [out] Start of action value, null 
+ *          3  :  value = [out] Start of action value, null
  *                        terminated.  NULL if none or EOL.
  *
  * Returns     :  JB_ERR_OK => Ok
@@ -399,7 +418,7 @@ jb_err get_action_token(char **line, char **name, char **value)
    *name = str;
 
    /* parse option */
-   while (((ch = *str) != '\0') && 
+   while (((ch = *str) != '\0') &&
           (ch != ' ') && (ch != '\t') && (ch != '{'))
    {
       if (ch == '}')
@@ -488,7 +507,7 @@ jb_err get_actions(char *line,
       if (option)
       {
          /* handle option in 'option' */
-      
+
          /* Check for standard action name */
          const struct action_name * action = action_names;
 
@@ -598,7 +617,7 @@ jb_err get_actions(char *line,
          {
             /* try user aliases. */
             const struct action_alias * alias = alias_list;
-            
+
             while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
             {
                alias = alias->next;
@@ -964,14 +983,15 @@ void init_action (struct action_spec *dest)
  *          1  :  dest = Current actions, to modify.
  *          2  :  src = Action to add.
  *
- * Returns     :  N/A
+ * Returns  0  :  no error
+ *        !=0  :  error
  *
  *********************************************************************/
-jb_err merge_current_action (struct current_action_spec *dest, 
+jb_err merge_current_action (struct current_action_spec *dest,
                              const struct action_spec *src)
 {
    int i;
-   jb_err err;
+   jb_err err = JB_ERR_OK;
 
    dest->flags  &= src->mask;
    dest->flags  |= src->add;
@@ -1012,6 +1032,7 @@ jb_err merge_current_action (struct current_action_spec *dest,
          }
       }
    }
+   return err;
 }
 
 
@@ -1093,7 +1114,7 @@ void free_alias_list(struct action_alias *alias_list)
    {
       struct action_alias * next = alias_list->next;
       alias_list->next = NULL;
-      freez((char *)alias_list->name);
+      freez(alias_list->name);
       free_action(alias_list->action);
       free(alias_list);
       alias_list = next;
@@ -1139,6 +1160,7 @@ int load_actions_file(struct client_state *csp)
    struct action_spec * cur_action = NULL;
    int cur_action_used = 0;
    struct action_alias * alias_list = NULL;
+   unsigned long linenum = 0;
 
    if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
    {
@@ -1171,7 +1193,7 @@ int load_actions_file(struct client_state *csp)
       return 1; /* never get here */
    }
 
-   while (read_config_line(buf, sizeof(buf), fp) != NULL)
+   while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
    {
       if (*buf == '{')
       {
@@ -1186,9 +1208,9 @@ int load_actions_file(struct client_state *csp)
             {
                /* too short */
                fclose(fp);
-               log_error(LOG_LEVEL_FATAL, 
-                  "can't load actions file '%s': invalid line: %s",
-                  csp->config->actions_file, buf);
+               log_error(LOG_LEVEL_FATAL,
+                  "can't load actions file '%s': invalid line (%lu): %s", 
+                  csp->config->actions_file, linenum, buf);
                return 1; /* never get here */
             }
 
@@ -1200,9 +1222,9 @@ int load_actions_file(struct client_state *csp)
             {
                /* too short */
                fclose(fp);
-               log_error(LOG_LEVEL_FATAL, 
-                  "can't load actions file '%s': invalid line: {{ }}",
-                  csp->config->actions_file);
+               log_error(LOG_LEVEL_FATAL,
+                  "can't load actions file '%s': invalid line (%lu): {{ }}",
+                  csp->config->actions_file, linenum);
                return 1; /* never get here */
             }
 
@@ -1232,9 +1254,9 @@ int load_actions_file(struct client_state *csp)
                    * appear once.
                    */
                   fclose(fp);
-                  log_error(LOG_LEVEL_FATAL, 
-                     "can't load actions file '%s': {{settings}} must only appear once, and it must be before anything else.",
-                     csp->config->actions_file);
+                  log_error(LOG_LEVEL_FATAL,
+                     "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
+                     csp->config->actions_file, linenum);
                }
                mode = MODE_SETTINGS;
             }
@@ -1246,9 +1268,9 @@ int load_actions_file(struct client_state *csp)
                   /* {{description}} is a singleton and only {{settings}} may proceed it
                    */
                   fclose(fp);
-                  log_error(LOG_LEVEL_FATAL, 
-                     "can't load actions file '%s': {{description}} must only appear once, and only a {{settings}} block may be above it.",
-                     csp->config->actions_file);
+                  log_error(LOG_LEVEL_FATAL,
+                     "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
+                     csp->config->actions_file, linenum);
                }
                mode = MODE_DESCRIPTION;
             }
@@ -1268,9 +1290,9 @@ int load_actions_file(struct client_state *csp)
                    * completely rewriting the file becomes non-trivial)
                    */
                   fclose(fp);
-                  log_error(LOG_LEVEL_FATAL, 
-                     "can't load actions file '%s': {{alias}} must only appear once, and it must be before all actions.",
-                     csp->config->actions_file, start);
+                  log_error(LOG_LEVEL_FATAL,
+                     "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
+                     csp->config->actions_file, linenum);
                }
                mode = MODE_ALIAS;
             }
@@ -1278,9 +1300,9 @@ int load_actions_file(struct client_state *csp)
             {
                /* invalid {{something}} block */
                fclose(fp);
-               log_error(LOG_LEVEL_FATAL, 
-                  "can't load actions file '%s': invalid line: {{%s}}",
-                  csp->config->actions_file, start);
+               log_error(LOG_LEVEL_FATAL,
+                  "can't load actions file '%s': invalid line (%lu): {{%s}}",
+                  csp->config->actions_file, linenum, start);
                return 1; /* never get here */
             }
          }
@@ -1309,7 +1331,7 @@ int load_actions_file(struct client_state *csp)
             if (cur_action == NULL)
             {
                fclose(fp);
-               log_error(LOG_LEVEL_FATAL, 
+               log_error(LOG_LEVEL_FATAL,
                   "can't load actions file '%s': out of memory",
                   csp->config->actions_file);
                return 1; /* never get here */
@@ -1325,9 +1347,9 @@ int load_actions_file(struct client_state *csp)
             {
                /* No closing } */
                fclose(fp);
-               log_error(LOG_LEVEL_FATAL, 
-                  "can't load actions file '%s': invalid line: %s",
-                  csp->config->actions_file, buf);
+               log_error(LOG_LEVEL_FATAL,
+                  "can't load actions file '%s': invalid line (%lu): %s",
+                  csp->config->actions_file, linenum, buf);
                return 1; /* never get here */
             }
             *end = '\0';
@@ -1339,9 +1361,9 @@ int load_actions_file(struct client_state *csp)
             {
                /* error */
                fclose(fp);
-               log_error(LOG_LEVEL_FATAL, 
-                  "can't load actions file '%s': invalid line: %s",
-                  csp->config->actions_file, buf);
+               log_error(LOG_LEVEL_FATAL,
+                  "can't load actions file '%s': invalid line (%lu): %s",
+                  csp->config->actions_file, linenum, buf);
                return 1; /* never get here */
             }
          }
@@ -1374,9 +1396,9 @@ int load_actions_file(struct client_state *csp)
 
          if ((start == NULL) || (start == buf))
          {
-            log_error(LOG_LEVEL_FATAL, 
-               "can't load actions file '%s': invalid alias line: %s",
-               csp->config->actions_file, buf);
+            log_error(LOG_LEVEL_FATAL,
+               "can't load actions file '%s': invalid alias line (%lu): %s",
+               csp->config->actions_file, linenum, buf);
             return 1; /* never get here */
          }
 
@@ -1409,9 +1431,9 @@ int load_actions_file(struct client_state *csp)
          }
          if (*start == '\0')
          {
-            log_error(LOG_LEVEL_FATAL, 
-               "can't load actions file '%s': invalid alias line: %s",
-               csp->config->actions_file, buf);
+            log_error(LOG_LEVEL_FATAL,
+               "can't load actions file '%s': invalid alias line (%lu): %s",
+               csp->config->actions_file, linenum, buf);
             return 1; /* never get here */
          }
 
@@ -1423,12 +1445,12 @@ int load_actions_file(struct client_state *csp)
          {
             /* error */
             fclose(fp);
-            log_error(LOG_LEVEL_FATAL, 
-               "can't load actions file '%s': invalid alias line: %s = %s",
-               csp->config->actions_file, buf, start);
+            log_error(LOG_LEVEL_FATAL,
+               "can't load actions file '%s': invalid alias line (%lu): %s = %s",
+               csp->config->actions_file, linenum, buf, start);
             return 1; /* never get here */
          }
-         
+
          /* add to list */
          new_alias->next = alias_list;
          alias_list = new_alias;
@@ -1454,9 +1476,9 @@ int load_actions_file(struct client_state *csp)
          if (create_url_spec(perm->url, buf))
          {
             fclose(fp);
-            log_error(LOG_LEVEL_FATAL, 
-               "can't load actions file '%s': cannot create URL pattern from: %s",
-               csp->config->actions_file, buf);
+            log_error(LOG_LEVEL_FATAL,
+               "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
+               csp->config->actions_file, linenum, buf);
             return 1; /* never get here */
          }
 
@@ -1468,16 +1490,16 @@ int load_actions_file(struct client_state *csp)
       {
          /* oops - please have a {} line as 1st line in file. */
          fclose(fp);
-         log_error(LOG_LEVEL_FATAL, 
-            "can't load actions file '%s': first line is invalid: %s",
-            csp->config->actions_file, buf);
+         log_error(LOG_LEVEL_FATAL,
+            "can't load actions file '%s': first needed line (%lu) is invalid: %s",
+            csp->config->actions_file, linenum, buf);
          return 1; /* never get here */
       }
       else
       {
          /* How did we get here? This is impossible! */
          fclose(fp);
-         log_error(LOG_LEVEL_FATAL, 
+         log_error(LOG_LEVEL_FATAL,
             "can't load actions file '%s': INTERNAL ERROR - mode = %d",
             csp->config->actions_file, mode);
          return 1; /* never get here */
@@ -1485,7 +1507,7 @@ int load_actions_file(struct client_state *csp)
    }
 
    fclose(fp);
-   
+
    free_action(cur_action);
 
    free_alias_list(alias_list);