Commit 4322d512 authored by Yang Tse's avatar Yang Tse
Browse files

curl tool: move so called 'multi_files' stuff into tool_mfiles.[ch]

Additionally some code reorganization and direct OOM handling fixes,
just another step towards fixing curl tool issues uncovered 2011-09-15
parent e4819ae1
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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)
+6 −0
Original line number Diff line number Diff line
@@ -143,6 +143,7 @@ RELEASE_OBJS= \
	rawstrr.obj \
	strtoofftr.obj \
	tool_convertr.obj \
	tool_mfilesr.obj \
	urlglobr.obj \
	writeoutr.obj \
	xattrr.obj \
@@ -159,6 +160,7 @@ DEBUG_OBJS= \
	rawstrd.obj \
	strtoofftd.obj \
	tool_convertd.obj \
	tool_mfilesd.obj \
	urlglobd.obj \
	writeoutd.obj \
	xattrd.obj \
@@ -306,6 +308,8 @@ strtoofftr.obj: ../lib/strtoofft.c
	$(CCR) $(CFLAGS) /Fo"$@" ../lib/strtoofft.c
tool_convertr.obj: tool_convert.c
	$(CCR) $(CFLAGS) /Fo"$@" tool_convert.c
tool_mfilesr.obj: tool_mfiles.c
	$(CCR) $(CFLAGS) /Fo"$@" tool_mfiles.c
xattrr.obj: xattr.c
	$(CCR) $(CFLAGS) /Fo"$@" xattr.c
mainr.obj: main.c
@@ -336,6 +340,8 @@ strtoofftd.obj: ../lib/strtoofft.c
	$(CCD) $(CFLAGS) /Fo"$@" ../lib/strtoofft.c
tool_convertd.obj: tool_convert.c
	$(CCD) $(CFLAGS) /Fo"$@" tool_convert.c
tool_mfilesd.obj: tool_mfiles.c
	$(CCD) $(CFLAGS) /Fo"$@" tool_mfiles.c
xattrd.obj: xattr.c
	$(CCD) $(CFLAGS) /Fo"$@" xattr.c
maind.obj: main.c
+9 −93
Original line number Diff line number Diff line
@@ -112,6 +112,7 @@
#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) {

src/tool_mfiles.c

0 → 100644
+128 −0
Original line number Diff line number Diff line
/***************************************************************************
 *                                  _   _ ____  _
 *  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;
}

src/tool_mfiles.h

0 → 100644
+45 −0
Original line number Diff line number Diff line
#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 */
Loading