remove paraslash relict in comment
[adu.git] / string.c
index a0666d35bde5d038bb0df48c954618f1041af16a..946faabdfd09ce688059c5d8ce15e140af282a29 100644 (file)
--- a/string.c
+++ b/string.c
@@ -4,7 +4,7 @@
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file string.c Memory allocation and string handling functions. */
+/** \file string.c \brief Memory allocation and string handling functions. */
 
 #include "adu.h"
 #include "string.h"
@@ -12,7 +12,7 @@
 #include "error.h"
 
 /**
- * Paraslash's version of realloc().
+ * Adu's version of realloc().
  *
  * \param p Pointer to the memory block, may be \p NULL.
  * \param size The desired new size.
@@ -41,7 +41,7 @@ __must_check __malloc void *adu_realloc(void *p, size_t size)
 }
 
 /**
- * Paraslash's version of malloc().
+ * Adu's version of malloc().
  *
  * \param size The desired new size.
  *
@@ -66,7 +66,7 @@ __must_check __malloc void *adu_malloc(size_t size)
 }
 
 /**
- * Paraslash's version of calloc().
+ * Adu's version of calloc().
  *
  * \param size The desired new size.
  *
@@ -86,7 +86,7 @@ __must_check __malloc void *adu_calloc(size_t size)
 }
 
 /**
- * Paraslash's version of strdup().
+ * Adu's version of strdup().
  *
  * \param s The string to be duplicated.
  *
@@ -129,6 +129,34 @@ __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
        return msg;
 }
 
+/**
+ * adu's version of strcat().
+ *
+ * \param a String to be appended to.
+ * \param b String to append.
+ *
+ * Append \p b to \p a.
+ *
+ * \return If \a a is \p NULL, return a pointer to a copy of \a b, i.e.
+ * adu_strcat(NULL, b) is equivalent to adu_strdup(b). If \a b is \p NULL,
+ * return \a a without making a copy of \a a.  Otherwise, construct the
+ * concatenation \a c, free \a a (but not \a b) and return \a c.
+ *
+ * \sa strcat(3).
+ */
+__must_check __malloc char *adu_strcat(char *a, const char *b)
+{
+       char *tmp;
+
+       if (!a)
+               return adu_strdup(b);
+       if (!b)
+               return a;
+       tmp = make_message("%s%s", a, b);
+       free(a);
+       return tmp;
+}
+
 /** \cond LLONG_MAX and LLONG_LIN might not be defined. */
 #ifndef LLONG_MAX
 #define LLONG_MAX (1 << (sizeof(long) - 1))
@@ -142,7 +170,7 @@ __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
  * Convert a string to a 64-bit signed integer value.
  *
  * \param str The string to be converted.
- * \param value Result pointer.
+ * \param result Result pointer.
  *
  * \return Standard.
  *
@@ -166,3 +194,182 @@ __must_check int atoi64(const char *str, int64_t *result)
        *result = tmp;
        return 1;
 }
+
+/**
+ * Split a string and return pointers to its parts.
+ *
+ * \param args The string to be split.
+ * \param argv_ptr Pointer to the list of substrings.
+ * \param delim Delimiter.
+ *
+ * This function modifies \a args by replacing each occurance of \a delim by
+ * zero. A \p NULL terminated array of pointers to char* is allocated dynamically
+ * and these pointers are initialized to point to the broken-up substrings
+ * within \a args. A pointer to this array is returned via \a argv_ptr.
+ *
+ * \return The number of substrings found in \a args.
+ */
+__must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
+{
+       char *p = args;
+       char **argv;
+       size_t n = 0, i, j;
+
+       p = args + strspn(args, delim);
+       for (;;) {
+               i = strcspn(p, delim);
+               if (!i)
+                       break;
+               p += i;
+               n++;
+               p += strspn(p, delim);
+       }
+       *argv_ptr = adu_malloc((n + 1) * sizeof(char *));
+       argv = *argv_ptr;
+       i = 0;
+       p = args + strspn(args, delim);
+       while (p) {
+               argv[i] = p;
+               j = strcspn(p, delim);
+               if (!j)
+                       break;
+               p += strcspn(p, delim);
+               if (*p) {
+                       *p = '\0';
+                       p++;
+                       p += strspn(p, delim);
+               }
+               i++;
+       }
+       argv[n] = NULL;
+       return n;
+}
+
+
+static int get_next_word(const char *line, char **word)
+{
+       enum line_state_flags {LSF_HAVE_WORD = 1, LSF_BACKSLASH = 2,
+               LSF_QUOTE = 4};
+       const char *in;
+       char *out;
+       int ret, state = 0;
+
+       out = adu_malloc(strlen(line) + 1);
+       *out = '\0';
+       *word = out;
+       for (in = line; *in; in++) {
+               switch (*in) {
+               case '\\':
+                       if (state & LSF_BACKSLASH) /* \\ */
+                               break;
+                       state |= LSF_BACKSLASH;
+                       state |= LSF_HAVE_WORD;
+                       continue;
+               case 'n':
+               case 't':
+                       if (state & LSF_BACKSLASH) { /* \n or \t */
+                               *out++ = (*in == 'n')? '\n' : '\t';
+                               state &= ~LSF_BACKSLASH;
+                               continue;
+                       }
+                       break;
+               case '"':
+                       if (state & LSF_BACKSLASH) /* \" */
+                               break;
+                       if (state & LSF_QUOTE) {
+                               state &= ~LSF_QUOTE;
+                               continue;
+                       }
+                       state |= LSF_HAVE_WORD;
+                       state |= LSF_QUOTE;
+                       continue;
+               case ' ':
+               case '\t':
+               case '\n':
+                       if (state & LSF_BACKSLASH)
+                               break;
+                       if (state & LSF_QUOTE)
+                               break;
+                       if (state & LSF_HAVE_WORD)
+                               goto success;
+                       /* ignore space at the beginning */
+                       continue;
+               }
+               /* copy char */
+               state |= LSF_HAVE_WORD;
+               *out++ = *in;
+               state &= ~LSF_BACKSLASH;
+       }
+       ret = 0;
+       if (!(state & LSF_HAVE_WORD))
+               goto out;
+       ret = -ERRNO_TO_ERROR(EINVAL);
+       if (state & LSF_BACKSLASH) {
+               ERROR_LOG("trailing backslash\n");
+               goto out;
+       }
+       if (state & LSF_QUOTE) {
+               ERROR_LOG("unmatched quote character\n");
+               goto out;
+       }
+success:
+       *out = '\0';
+       return in - line;
+out:
+       free(*word);
+       *word = NULL;
+       return ret;
+}
+
+/**
+ * Free an array of words created by create_argv().
+ *
+ * \param argv A pointer previously obtained by \ref create_argv().
+ */
+void free_argv(char **argv)
+{
+       int i;
+
+       for (i = 0; argv[i]; i++)
+               free(argv[i]);
+       free(argv);
+}
+
+/**
+ * Split a line into words which are separated by whitespace.
+ *
+ * In contrast to gengetopt's string parser, double quotes, backslash-escaped
+ * characters and special characters like \p \\n are honored. The result
+ * contains pointers to copies of the words contained in \a line and has to be
+ * freed by using \ref free_argv().
+ *
+ * \param line The line to be split.
+ * \param result The array of words is returned here.
+ *
+ * \return Number of words in \a line, negative on errors.
+ */
+int create_argv(const char *line, char ***result)
+{
+       char *word, **argv = adu_malloc(2 * sizeof(char *));
+       const char *p;
+       int ret, num_words;
+
+       argv[0] = adu_strdup(line);
+       for (p = line, num_words = 1; ; p += ret, num_words++) {
+               ret = get_next_word(p, &word);
+               if (ret < 0)
+                       goto err;
+               if (!ret)
+                       break;
+               argv = adu_realloc(argv, (num_words + 2) * sizeof(char*));
+               argv[num_words] = word;
+       }
+       argv[num_words] = NULL;
+       *result = argv;
+       return num_words;
+err:
+       while (num_words > 0)
+               free(argv[--num_words]);
+       free(argv);
+       return ret;
+}