curl tool: move so called 'multi_files' stuff into tool_mfiles.[ch]
authorYang Tse <yangsita@gmail.com>
Fri, 16 Sep 2011 17:46:01 +0000 (19:46 +0200)
committerYang Tse <yangsita@gmail.com>
Fri, 16 Sep 2011 17:46:01 +0000 (19:46 +0200)
Additionally some code reorganization and direct OOM handling fixes,
just another step towards fixing curl tool issues uncovered 2011-09-15

src/Makefile.inc
src/Makefile.vc6
src/main.c
src/tool_mfiles.c [new file with mode: 0644]
src/tool_mfiles.h [new file with mode: 0644]
src/vc6curlsrc.dsp

index f13b171..f534075 100644 (file)
@@ -16,12 +16,12 @@ CURLX_ONES = $(top_srcdir)/lib/strtoofft.c \
 
 CURL_CFILES = main.c hugehelp.c urlglob.c writeout.c writeenv.c \
        getpass.c homedir.c curlutil.c os-specific.c xattr.c \
-       tool_convert.c
+       tool_convert.c tool_mfiles.c
 
 CURL_HFILES = hugehelp.h setup.h config-win32.h config-mac.h \
        config-riscos.h urlglob.h version.h os-specific.h \
        writeout.h writeenv.h getpass.h homedir.h curlutil.h \
-       xattr.h tool_convert.h
+       xattr.h tool_convert.h tool_mfiles.h
 
 curl_SOURCES = $(CURL_CFILES) $(CURLX_ONES) $(CURL_HFILES)
 
index 14e9399..950d076 100644 (file)
@@ -143,6 +143,7 @@ RELEASE_OBJS= \
        rawstrr.obj \\r
        strtoofftr.obj \\r
        tool_convertr.obj \\r
+       tool_mfilesr.obj \\r
        urlglobr.obj \\r
        writeoutr.obj \\r
        xattrr.obj \\r
@@ -159,6 +160,7 @@ DEBUG_OBJS= \
        rawstrd.obj \\r
        strtoofftd.obj \\r
        tool_convertd.obj \\r
+       tool_mfilesd.obj \\r
        urlglobd.obj \\r
        writeoutd.obj \\r
        xattrd.obj \\r
@@ -306,6 +308,8 @@ strtoofftr.obj: ../lib/strtoofft.c
        $(CCR) $(CFLAGS) /Fo"$@" ../lib/strtoofft.c\r
 tool_convertr.obj: tool_convert.c\r
        $(CCR) $(CFLAGS) /Fo"$@" tool_convert.c\r
+tool_mfilesr.obj: tool_mfiles.c\r
+       $(CCR) $(CFLAGS) /Fo"$@" tool_mfiles.c\r
 xattrr.obj: xattr.c\r
        $(CCR) $(CFLAGS) /Fo"$@" xattr.c\r
 mainr.obj: main.c\r
@@ -336,6 +340,8 @@ strtoofftd.obj: ../lib/strtoofft.c
        $(CCD) $(CFLAGS) /Fo"$@" ../lib/strtoofft.c\r
 tool_convertd.obj: tool_convert.c\r
        $(CCD) $(CFLAGS) /Fo"$@" tool_convert.c\r
+tool_mfilesd.obj: tool_mfiles.c\r
+       $(CCD) $(CFLAGS) /Fo"$@" tool_mfiles.c\r
 xattrd.obj: xattr.c\r
        $(CCD) $(CFLAGS) /Fo"$@" xattr.c\r
 maind.obj: main.c\r
index 52b700f..532ccd3 100644 (file)
 #include "version.h"
 #include "xattr.h"
 #include "tool_convert.h"
+#include "tool_mfiles.h"
 #ifdef USE_MANUAL
 #  include "hugehelp.h"
 #endif
@@ -918,92 +919,6 @@ static struct getout *new_getout(struct Configurable *config)
   return node;
 }
 
-/* Structure for storing the information needed to build a multiple files
- * section
- */
-struct multi_files {
-  struct curl_forms   form;
-  struct multi_files *next;
-};
-
-/* Add a new list entry possibly with a type_name
- */
-static struct multi_files *
-AddMultiFiles(const char *file_name,
-              const char *type_name,
-              const char *show_filename,
-              struct multi_files **multi_start,
-              struct multi_files **multi_current)
-{
-  struct multi_files *multi;
-  struct multi_files *multi_type = NULL;
-  struct multi_files *multi_name = NULL;
-  multi = malloc(sizeof(struct multi_files));
-  if(multi) {
-    memset(multi, 0, sizeof(struct multi_files));
-    multi->form.option = CURLFORM_FILE;
-    multi->form.value = file_name;
-  }
-  else
-    return NULL;
-
-  if(!*multi_start)
-    *multi_start = multi;
-
-  if(type_name) {
-    multi_type = malloc(sizeof(struct multi_files));
-    if(multi_type) {
-      memset(multi_type, 0, sizeof(struct multi_files));
-      multi_type->form.option = CURLFORM_CONTENTTYPE;
-      multi_type->form.value = type_name;
-      multi->next = multi_type;
-
-      multi = multi_type;
-    }
-    else {
-      Curl_safefree(multi);
-      return NULL;
-    }
-  }
-  if(show_filename) {
-    multi_name = malloc(sizeof(struct multi_files));
-    if(multi_name) {
-      memset(multi_name, 0, sizeof(struct multi_files));
-      multi_name->form.option = CURLFORM_FILENAME;
-      multi_name->form.value = show_filename;
-      multi->next = multi_name;
-
-      multi = multi_name;
-    }
-    else {
-      Curl_safefree(multi);
-      return NULL;
-    }
-  }
-
-  if(*multi_current)
-    (*multi_current)->next = multi;
-
-  *multi_current = multi;
-
-  return *multi_current;
-}
-
-/* Free the items of the list.
- */
-static void FreeMultiInfo(struct multi_files **multi_start)
-{
-  struct multi_files *next;
-  struct multi_files *item = *multi_start;
-
-  while(item) {
-    next = item->next;
-    Curl_safefree(item);
-    item = next;
-  }
-  *multi_start = NULL;
-}
-
 /* Print list of OpenSSL engines supported.
  */
 static void list_engines(const struct curl_slist *engines)
@@ -1087,11 +1002,13 @@ static int formparse(struct Configurable *config,
     contp = contents;
 
     if('@' == contp[0] && !literal_value) {
-      struct multi_files *multi_start = NULL, *multi_current = NULL;
+
       /* we use the @-letter to indicate file name(s) */
-      contp++;
 
-      multi_start = multi_current=NULL;
+      struct multi_files *multi_start = NULL;
+      struct multi_files *multi_current = NULL;
+
+      contp++;
 
       do {
         /* since this was a file, it may have a content-type specifier
@@ -1136,7 +1053,7 @@ static int formparse(struct Configurable *config,
                              major, minor)) {
                 warnf(config, "Illegally formatted content-type field!\n");
                 Curl_safefree(contents);
-                FreeMultiInfo(&multi_start);
+                FreeMultiInfo(&multi_start, &multi_current);
                 return 2; /* illegal content-type syntax! */
               }
 
@@ -1198,7 +1115,6 @@ static int formparse(struct Configurable *config,
                           &multi_current)) {
           warnf(config, "Error building form post!\n");
           Curl_safefree(contents);
-          FreeMultiInfo(&multi_start);
           return 3;
         }
         contp = sep; /* move the contents pointer to after the separator */
@@ -1218,7 +1134,7 @@ static int formparse(struct Configurable *config,
         if(!forms) {
           fprintf(config->errors, "Error building form post!\n");
           Curl_safefree(contents);
-          FreeMultiInfo(&multi_start);
+          FreeMultiInfo(&multi_start, &multi_current);
           return 4;
         }
         for(i = 0, ptr = multi_start; i < count; ++i, ptr = ptr->next) {
@@ -1226,7 +1142,7 @@ static int formparse(struct Configurable *config,
           forms[i].value = ptr->form.value;
         }
         forms[count].option = CURLFORM_END;
-        FreeMultiInfo(&multi_start);
+        FreeMultiInfo(&multi_start, &multi_current);
         if(curl_formadd(httppost, last_post,
                         CURLFORM_COPYNAME, name,
                         CURLFORM_ARRAY, forms, CURLFORM_END) != 0) {
diff --git a/src/tool_mfiles.c b/src/tool_mfiles.c
new file mode 100644 (file)
index 0000000..b87b5b5
--- /dev/null
@@ -0,0 +1,128 @@
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include "setup.h"
+
+#include <curl/curl.h>
+
+#include "tool_mfiles.h"
+
+#include "memdebug.h" /* keep this as LAST include */
+
+static void AppendNode(struct multi_files **first,
+                       struct multi_files **last,
+                       struct multi_files  *new)
+{
+  DEBUGASSERT(((*first) && (*last)) || ((!*first) && (!*last)));
+
+  if(*last)
+    (*last)->next = new;
+  else
+    *first = new;
+  *last = new;
+}
+
+/*
+ * AddMultiFiles: Add a new list node possibly followed with a type_name.
+ *
+ * multi_first argument is the address of a pointer to the first element
+ * of the multi_files linked list. A NULL pointer indicates empty list.
+ *
+ * multi_last argument is the address of a pointer to the last element
+ * of the multi_files linked list. A NULL pointer indicates empty list.
+ *
+ * Pointers stored in multi_first and multi_last are modified while
+ * function is executed. An out of memory condition free's the whole
+ * list and returns with pointers stored in multi_first and multi_last
+ * set to NULL and a NULL function result.
+ *
+ * Function returns same pointer as stored at multi_last.
+ */
+
+struct multi_files *AddMultiFiles(const char *file_name,
+                                  const char *type_name,
+                                  const char *show_filename,
+                                  struct multi_files **multi_first,
+                                  struct multi_files **multi_last)
+{
+  struct multi_files *multi;
+  struct multi_files *multi_type;
+  struct multi_files *multi_name;
+
+  multi = calloc(1, sizeof(struct multi_files));
+  if(multi) {
+    multi->form.option = CURLFORM_FILE;
+    multi->form.value = file_name;
+    AppendNode(multi_first, multi_last, multi);
+  }
+  else {
+    FreeMultiInfo(multi_first, multi_last);
+    return NULL;
+  }
+
+  if(type_name) {
+    multi_type = calloc(1, sizeof(struct multi_files));
+    if(multi_type) {
+      multi_type->form.option = CURLFORM_CONTENTTYPE;
+      multi_type->form.value = type_name;
+      AppendNode(multi_first, multi_last, multi_type);
+    }
+    else {
+      FreeMultiInfo(multi_first, multi_last);
+      return NULL;
+    }
+  }
+
+  if(show_filename) {
+    multi_name = calloc(1, sizeof(struct multi_files));
+    if(multi_name) {
+      multi_name->form.option = CURLFORM_FILENAME;
+      multi_name->form.value = show_filename;
+      AppendNode(multi_first, multi_last, multi_name);
+    }
+    else {
+      FreeMultiInfo(multi_first, multi_last);
+      return NULL;
+    }
+  }
+
+  return *multi_last;
+}
+
+/*
+ * FreeMultiInfo: Free the items of the list.
+ */
+
+void FreeMultiInfo(struct multi_files **multi_first,
+                   struct multi_files **multi_last)
+{
+  struct multi_files *next;
+  struct multi_files *item = *multi_first;
+
+  while(item) {
+    next = item->next;
+    Curl_safefree(item);
+    item = next;
+  }
+  *multi_first = NULL;
+  if(multi_last)
+    *multi_last = NULL;
+}
diff --git a/src/tool_mfiles.h b/src/tool_mfiles.h
new file mode 100644 (file)
index 0000000..157fc53
--- /dev/null
@@ -0,0 +1,45 @@
+#ifndef HEADER_CURL_TOOL_MFILES_H
+#define HEADER_CURL_TOOL_MFILES_H
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include "setup.h"
+
+/*
+ * Structure for storing the information needed to build
+ * a multiple files section.
+ */
+
+struct multi_files {
+  struct curl_forms   form;
+  struct multi_files *next;
+};
+
+struct multi_files *AddMultiFiles(const char *file_name,
+                                  const char *type_name,
+                                  const char *show_filename,
+                                  struct multi_files **multi_first,
+                                  struct multi_files **multi_last);
+
+void FreeMultiInfo(struct multi_files **multi_first,
+                   struct multi_files **multi_last);
+
+#endif /* HEADER_CURL_TOOL_MFILES_H */
index eff6b77..486d9e9 100644 (file)
@@ -179,6 +179,10 @@ SOURCE=.\tool_convert.c
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\tool_mfiles.c\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\urlglob.c\r
 # End Source File\r
 # Begin Source File\r
@@ -243,6 +247,10 @@ SOURCE=.\tool_convert.h
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\tool_mfiles.h\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\urlglob.h\r
 # End Source File\r
 # Begin Source File\r