- Prevent line wrap beween "View/Edit" link buttons on status page
[privoxy.git] / cgiedit.c
index 7d5adbd..834a86e 100644 (file)
--- a/cgiedit.c
+++ b/cgiedit.c
@@ -1,4 +1,4 @@
-const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41 2002/05/21 19:09:45 oes Exp $";
+const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41.2.4 2003/03/11 11:53:59 oes Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/Attic/cgiedit.c,v $
@@ -42,6 +42,18 @@ const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41 2002/05/21 19:09:45 oes Exp $"
  *
  * Revisions   :
  *    $Log: cgiedit.c,v $
+ *    Revision 1.41.2.4  2003/03/11 11:53:59  oes
+ *    Cosmetic: Renamed cryptic variable
+ *
+ *    Revision 1.41.2.3  2002/11/12 15:01:41  oes
+ *    Fix: Don't free uninitialized struct editable_file
+ *
+ *    Revision 1.41.2.2  2002/08/05 20:02:59  oes
+ *    Bugfix: "Insert new section at top" did not work properly if first non-comment line in file was of type FILE_LINE_ACTION
+ *
+ *    Revision 1.41.2.1  2002/08/02 12:43:14  oes
+ *    Fixed bug #588514: first_time now set on a per-string basis in actions_from_radio; javascriptify now called on copies
+ *
  *    Revision 1.41  2002/05/21 19:09:45  oes
  *     - Made Add/Edit/Remove URL Submit and Cancel
  *       buttons jump back to relevant section in eal
@@ -286,7 +298,7 @@ const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.41 2002/05/21 19:09:45 oes Exp $"
 #include "errlog.h"
 #include "loaders.h"
 #include "loadcfg.h"
-/* loadcfg.h is for g_bToggleIJB only */
+/* loadcfg.h is for global_toggle_state only */
 #include "urlmatch.h"
 
 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
@@ -2446,20 +2458,9 @@ jb_err cgi_edit_actions(struct client_state *csp,
    }
 
    /* FIXME: Incomplete */
-   rsp->status = strdup("302 Local Redirect from Privoxy");
-   if (rsp->status == NULL)
-   {
-      return JB_ERR_MEMORY;
-   }
-   if (enlist_unique_header(rsp->headers, "Location",
-      CGI_PREFIX "edit-actions-list?f=default"))
-   {
-      free(rsp->status);
-      rsp->status = NULL;
-      return JB_ERR_MEMORY;
-   }
 
-   return JB_ERR_OK;
+   return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
+
 }
 
 
@@ -2515,7 +2516,6 @@ jb_err cgi_edit_actions_list(struct client_state *csp,
 
    if (NULL == (exports = default_exports(csp, NULL)))
    {
-      edit_free_file(file);
       return JB_ERR_MEMORY;
    }
 
@@ -2877,12 +2877,13 @@ jb_err cgi_edit_actions_list(struct client_state *csp,
 
       /* Could also do section-specific exports here, but it wouldn't be as fast */
 
+      snprintf(buf, 150, "%d", line_number);
+      if (!err) err = map(section_exports, "s-next", 1, buf, 1);
+
       if ( (cur_line != NULL)
         && (cur_line->type == FILE_LINE_ACTION))
       {
          /* Not last section */
-         snprintf(buf, 150, "%d", line_number);
-         if (!err) err = map(section_exports, "s-next", 1, buf, 1);
          if (!err) err = map_block_keep(section_exports, "s-next-exists");
       }
       else
@@ -3185,7 +3186,7 @@ jb_err cgi_edit_actions_submit(struct client_state *csp,
    struct editable_file * file;
    struct file_line * cur_line;
    unsigned line_number;
-   char * target;
+   char target[1024];
    jb_err err;
    int index;
    const char * action_set_name;
@@ -3373,29 +3374,12 @@ jb_err cgi_edit_actions_submit(struct client_state *csp,
       return err;
    }
 
-   target = strdup(CGI_PREFIX "edit-actions-list?f=");
-   string_append(&target, file->identifier);
-   string_join(&target, section_target(sectionid));
-
+   snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
+            (long) time(NULL), file->identifier, sectionid);
 
    edit_free_file(file);
 
-   if (target == NULL)
-   {
-      /* Out of memory */
-      return JB_ERR_MEMORY;
-   }
-
-   rsp->status = strdup("302 Local Redirect from Privoxy");
-   if (rsp->status == NULL)
-   {
-      free(target);
-      return JB_ERR_MEMORY;
-   }
-   err = enlist_unique_header(rsp->headers, "Location", target);
-   free(target);
-
-   return err;
+   return cgi_redirect(rsp, target);
 }
 
 
@@ -3434,9 +3418,13 @@ jb_err cgi_edit_actions_url(struct client_state *csp,
    struct file_line * cur_line;
    unsigned line_number;
    unsigned section_start_line_number = 0;
-   char * target;
+   char target[1024];
    jb_err err;
 
+   assert(csp);
+   assert(rsp);
+   assert(parameters);
+
    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
    {
       return cgi_error_disabled(csp, rsp);
@@ -3507,28 +3495,12 @@ jb_err cgi_edit_actions_url(struct client_state *csp,
       return err;
    }
 
-   target = strdup(CGI_PREFIX "edit-actions-list?f=");
-   string_append(&target, file->identifier);
-   string_join(&target, section_target(section_start_line_number));
+   snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
+            (long) time(NULL), file->identifier, section_start_line_number);
 
    edit_free_file(file);
 
-   if (target == NULL)
-   {
-      /* Out of memory */
-      return JB_ERR_MEMORY;
-   }
-
-   rsp->status = strdup("302 Local Redirect from Privoxy");
-   if (rsp->status == NULL)
-   {
-      free(target);
-      return JB_ERR_MEMORY;
-   }
-   err = enlist_unique_header(rsp->headers, "Location", target);
-   free(target);
-
-   return err;
+   return cgi_redirect(rsp, target);
 }
 
 
@@ -3566,7 +3538,7 @@ jb_err cgi_edit_actions_add_url(struct client_state *csp,
    struct editable_file * file;
    struct file_line * cur_line;
    unsigned line_number;
-   char * target;
+   char target[1024];
    jb_err err;
 
    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
@@ -3652,28 +3624,12 @@ jb_err cgi_edit_actions_add_url(struct client_state *csp,
       return err;
    }
 
-   target = strdup(CGI_PREFIX "edit-actions-list?f=");
-   string_append(&target, file->identifier);
-   string_join(&target, section_target(sectionid));
+   snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
+            (long) time(NULL), file->identifier, sectionid);
 
    edit_free_file(file);
 
-   if (target == NULL)
-   {
-      /* Out of memory */
-      return JB_ERR_MEMORY;
-   }
-
-   rsp->status = strdup("302 Local Redirect from Privoxy");
-   if (rsp->status == NULL)
-   {
-      free(target);
-      return JB_ERR_MEMORY;
-   }
-   err = enlist_unique_header(rsp->headers, "Location", target);
-   free(target);
-
-   return err;
+   return cgi_redirect(rsp, target);
 }
 
 
@@ -3710,7 +3666,7 @@ jb_err cgi_edit_actions_remove_url(struct client_state *csp,
    struct file_line * prev_line;
    unsigned line_number;
    unsigned section_start_line_number = 0;
-   char * target;
+   char target[1024];
    jb_err err;
 
    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
@@ -3779,28 +3735,12 @@ jb_err cgi_edit_actions_remove_url(struct client_state *csp,
       return err;
    }
 
-   target = strdup(CGI_PREFIX "edit-actions-list?f=");
-   string_append(&target, file->identifier);
-   string_join(&target, section_target(section_start_line_number));
+   snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s#l%d",
+            (long) time(NULL), file->identifier, section_start_line_number);
 
    edit_free_file(file);
 
-   if (target == NULL)
-   {
-      /* Out of memory */
-      return JB_ERR_MEMORY;
-   }
-
-   rsp->status = strdup("302 Local Redirect from Privoxy");
-   if (rsp->status == NULL)
-   {
-      free(target);
-      return JB_ERR_MEMORY;
-   }
-   err = enlist_unique_header(rsp->headers, "Location", target);
-   free(target);
-
-   return err;
+   return cgi_redirect(rsp, target);
 }
 
 
@@ -3837,7 +3777,7 @@ jb_err cgi_edit_actions_section_remove(struct client_state *csp,
    struct file_line * cur_line;
    struct file_line * prev_line;
    unsigned line_number;
-   char * target;
+   char target[1024];
    jb_err err;
 
    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
@@ -3917,27 +3857,12 @@ jb_err cgi_edit_actions_section_remove(struct client_state *csp,
       return err;
    }
 
-   target = strdup(CGI_PREFIX "edit-actions-list?f=");
-   string_append(&target, file->identifier);
+   snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
+            (long) time(NULL), file->identifier);
 
    edit_free_file(file);
 
-   if (target == NULL)
-   {
-      /* Out of memory */
-      return JB_ERR_MEMORY;
-   }
-
-   rsp->status = strdup("302 Local Redirect from Privoxy");
-   if (rsp->status == NULL)
-   {
-      free(target);
-      return JB_ERR_MEMORY;
-   }
-   err = enlist_unique_header(rsp->headers, "Location", target);
-   free(target);
-
-   return err;
+   return cgi_redirect(rsp, target);
 }
 
 
@@ -3975,7 +3900,7 @@ jb_err cgi_edit_actions_section_add(struct client_state *csp,
    struct editable_file * file;
    struct file_line * cur_line;
    unsigned line_number;
-   char * target;
+   char target[1024];
    jb_err err;
 
    if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
@@ -3999,10 +3924,10 @@ jb_err cgi_edit_actions_section_add(struct client_state *csp,
    line_number = 1;
    cur_line = file->lines;
 
-   if (sectionid < 1U)
+   if (sectionid <= 1U)
    {
       /* Add to start of file */
-      if (cur_line != NULL)
+      if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
       {
          /* There's something in the file, find the line before the first
           * action.
@@ -4014,6 +3939,11 @@ jb_err cgi_edit_actions_section_add(struct client_state *csp,
             line_number++;
          }
       }
+      else
+      {
+         /* File starts with action line, so insert at top */
+         cur_line = NULL;
+      }
    }
    else
    {
@@ -4096,27 +4026,12 @@ jb_err cgi_edit_actions_section_add(struct client_state *csp,
       return err;
    }
 
-   target = strdup(CGI_PREFIX "edit-actions-list?f=");
-   string_append(&target, file->identifier);
+   snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
+            (long) time(NULL), file->identifier);
 
    edit_free_file(file);
 
-   if (target == NULL)
-   {
-      /* Out of memory */
-      return JB_ERR_MEMORY;
-   }
-
-   rsp->status = strdup("302 Local Redirect from Privoxy");
-   if (rsp->status == NULL)
-   {
-      free(target);
-      return JB_ERR_MEMORY;
-   }
-   err = enlist_unique_header(rsp->headers, "Location", target);
-   free(target);
-
-   return err;
+   return cgi_redirect(rsp, target);
 }
 
 
@@ -4300,27 +4215,12 @@ jb_err cgi_edit_actions_section_swap(struct client_state *csp,
       }
    } /* END if (section1 != section2) */
 
-   target = strdup(CGI_PREFIX "edit-actions-list?f=");
-   string_append(&target, file->identifier);
+   snprintf(target, 1024, CGI_PREFIX "edit-actions-list?foo=%lu&f=%s",
+            (long) time(NULL), file->identifier);
 
    edit_free_file(file);
 
-   if (target == NULL)
-   {
-      /* Out of memory */
-      return JB_ERR_MEMORY;
-   }
-
-   rsp->status = strdup("302 Local Redirect from Privoxy");
-   if (rsp->status == NULL)
-   {
-      free(target);
-      return JB_ERR_MEMORY;
-   }
-   err = enlist_unique_header(rsp->headers, "Location", target);
-   free(target);
-
-   return err;
+   return cgi_redirect(rsp, target);
 }
 
 
@@ -4367,17 +4267,17 @@ jb_err cgi_toggle(struct client_state *csp,
    if (mode == 'E')
    {
       /* Enable */
-      g_bToggleIJB = 1;
+      global_toggle_state = 1;
    }
    else if (mode == 'D')
    {
       /* Disable */
-      g_bToggleIJB = 0;
+      global_toggle_state = 0;
    }
    else if (mode == 'T')
    {
       /* Toggle */
-      g_bToggleIJB = !g_bToggleIJB;
+      global_toggle_state = !global_toggle_state;
    }
 
    if (NULL == (exports = default_exports(csp, "toggle")))