summaryrefslogtreecommitdiff
path: root/libmailutils/base
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2010-12-04 12:19:50 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2010-12-04 12:52:34 +0200
commit76df98da17f576b71e6cbc83bbf527b685be1178 (patch)
tree3af08ebb7dcff4a82d468b03491f722e6e7090c0 /libmailutils/base
parent30bf4d542198abc23b897dd81fc280354903e49f (diff)
downloadmailutils-76df98da17f576b71e6cbc83bbf527b685be1178.tar.gz
mailutils-76df98da17f576b71e6cbc83bbf527b685be1178.tar.bz2
Introduce stdstream module; move deprecated functions to libmu_compat.
* include/mailutils/log.h: New file. * include/mailutils/stdstream.h: New file. * include/mailutils/Makefile.am (pkginclude_HEADERS): Add stdstream.h and log.h. * include/mailutils/argcv.h: Include mailutils/types.h * include/mailutils/mailutils.h: Include stdstream.h Remove vartab.h * include/mailutils/types.hin (MU_DEPRECATED): New define. * include/mailutils/vartab.h: Mark this header and all prototypes it defines as deprecated. * libmailutils/stdstream/Makefile.am: New file. * libmailutils/stdstream/basestr.c: New file. * libmailutils/stdstream/strerr.c: New file. * libmu_compat/Makefile.am: New file. * libmu_compat/tests/.gitignore: New file. * libmu_compat/tests/Makefile.am: New file. * libmu_compat/tests/atlocal.in: New file. * libmu_compat/tests/testsuite.at: New file. * libmailutils/base/Makefile.am (noinst_LTLIBRARIES): Remove argcv.c and vartab.c. * libmailutils/Makefile.am (SUBDIRS): Add stdstream. (libmailutils_la_LIBADD): Add stdstream/libstdstream.la. * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Remove argcv (TESTSUITE_AT): Remove argcv.at * libmailutils/tests/testsuite.at: Do not include argcv.at. * libmailutils/base/argcv.c: Move to libmu_compat/argcv.c * libmailutils/tests/argcv.at: Move to libmu_compat/tests/argcv.at * libmailutils/tests/argcv.c: Move to libmu_compat/tests/argcv.c * libmailutils/base/vartab.c: Move to libmu_compat/vartab.c * Makefile.am (SUBDIRS): Add libmu_compat. * configure.ac: Add libmu_compat/tests, libmu_compat/tests/Makefile, libmu_compat/tests/atlocal, libmu_compat/Makefile and libmailutils/stdstream/Makefile to config file list. * mu/ldflags.c (lib_descr): Add mu_compat. * mu/logger.c (logger_parse_opt): -t does not imply --syslog. (mutool_logger): Use inline-comment filter to insert "tag: " prefix. Flush and destroy both streams before returning. * po/POTFILES.in: Add libmailutils/stream/logstream.c, libmailutils/stdstream/baseio.c, libmailutils/stdstream/strerr.c and mu/logger.c.
Diffstat (limited to 'libmailutils/base')
-rw-r--r--libmailutils/base/Makefile.am2
-rw-r--r--libmailutils/base/argcv.c440
-rw-r--r--libmailutils/base/vartab.c346
3 files changed, 0 insertions, 788 deletions
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am
index ae9d2a972..b50781c20 100644
--- a/libmailutils/base/Makefile.am
+++ b/libmailutils/base/Makefile.am
@@ -20,7 +20,6 @@ noinst_LTLIBRARIES = libbase.la
libbase_la_SOURCES = \
alloc.c\
amd.c\
- argcv.c\
argcvfree.c\
argcvjoin.c\
argcvrem.c\
@@ -65,7 +64,6 @@ libbase_la_SOURCES = \
ticket.c\
tilde.c\
usremail.c\
- vartab.c\
version.c\
wicket.c
diff --git a/libmailutils/base/argcv.c b/libmailutils/base/argcv.c
deleted file mode 100644
index 142de6b6a..000000000
--- a/libmailutils/base/argcv.c
+++ /dev/null
@@ -1,440 +0,0 @@
-/* argcv.c - simple functions for parsing input based on whitespace
- Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2010 Free
- Software Foundation, Inc.
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 3 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General
- Public License along with this library. If not, see
- <http://www.gnu.org/licenses/>. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <ctype.h>
-#include <errno.h>
-#define MU_ARCGV_DEPRECATED
-#include <mailutils/argcv.h>
-
-/* Keep mailutils namespace clean */
-#define argcv_get mu_argcv_get
-#define argcv_get_n mu_argcv_get_n
-#define argcv_get_np mu_argcv_get_np
-#define argcv_unquote_char mu_argcv_unquote_char
-#define argcv_quote_char mu_argcv_quote_char
-#define argcv_quoted_length mu_argcv_quoted_length
-#define argcv_unquote_copy mu_argcv_unquote_copy
-#define argcv_quote_copy mu_argcv_quote_copy
-
-/*
- * takes a string and splits it into several strings, breaking at ' '
- * command is the string to split
- * the number of strings is placed into argc
- * the split strings are put into argv
- * returns 0 on success, nonzero on failure
- */
-
-#define isws(c) ((c)==' '||(c)=='\t'||(c)=='\n')
-#define isdelim(c,delim) (strchr(delim,(c))!=NULL)
-
-struct argcv_info
-{
- int len;
- const char *command;
- const char *delim;
- const char *comment;
- int flags;
-
- int start;
- int end;
- int save;
- int finish_pos;
-};
-
-static void
-init_argcv_info (struct argcv_info *ap, int flags,
- int len, const char *command, const char *delim,
- const char *comment)
-{
- memset (ap, 0, sizeof *ap);
- ap->len = len;
- ap->command = command;
- ap->delim = delim;
- ap->comment = comment;
- ap->flags = flags;
-}
-
-static int
-argcv_scan (struct argcv_info *ap)
-{
- int i = 0;
- int len = ap->len;
- const char *command = ap->command;
- const char *delim = ap->delim;
- const char *comment = ap->comment;
-
- for (;;)
- {
- i = ap->save;
-
- if (i >= len)
- return i + 1;
-
- /* Skip initial whitespace */
- while (i < len && isws (command[i]))
- i++;
- ap->start = i;
-
- if (!isdelim (command[i], delim))
- {
- while (i < len)
- {
- if (command[i] == '\\')
- {
- if (++i == len)
- break;
- i++;
- continue;
- }
-
- if (command[i] == '\'' || command[i] == '"')
- {
- int j;
- for (j = i + 1; j < len && command[j] != command[i]; j++)
- if (command[j] == '\\')
- j++;
- if (j < len)
- i = j + 1;
- else
- i++;
- }
- else if (isws (command[i]) || isdelim (command[i], delim))
- break;
- else
- i++; /* skip the escaped character */
- }
- i--;
- }
- else if (!(ap->flags & MU_ARGCV_RETURN_DELIMS))
- {
- while (i < len && isdelim (command[i], delim))
- i++;
- ap->save = i;
- continue;
- }
-
-
- ap->end = i;
- ap->save = ap->finish_pos = i + 1;
-
- /* If we have a token, and it starts with a comment character, skip
- to the newline and restart the token search. */
- if (ap->save <= len)
- {
- if (strchr (comment, command[ap->start]) != NULL)
- {
- ap->finish_pos = ap->start;
- i = ap->save;
- while (i < len && command[i] != '\n')
- i++;
-
- ap->save = i;
- continue;
- }
- }
- break;
- }
- return ap->save;
-}
-
-static char quote_transtab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v";
-
-int
-argcv_unquote_char (int c)
-{
- char *p;
-
- for (p = quote_transtab; *p; p += 2)
- {
- if (*p == c)
- return p[1];
- }
- return c;
-}
-
-int
-argcv_quote_char (int c)
-{
- char *p;
-
- for (p = quote_transtab + sizeof(quote_transtab) - 2;
- p > quote_transtab; p -= 2)
- {
- if (*p == c)
- return p[-1];
- }
- return -1;
-}
-
-#define to_num(c) \
- (isdigit(c) ? c - '0' : (isxdigit(c) ? toupper(c) - 'A' + 10 : 255 ))
-
-static int
-xtonum (int *pval, const char *src, int base, int cnt)
-{
- int i, val;
-
- for (i = 0, val = 0; i < cnt; i++, src++)
- {
- int n = *(unsigned char*)src;
- if (n > 127 || (n = to_num(n)) >= base)
- break;
- val = val*base + n;
- }
- *pval = val;
- return i;
-}
-
-size_t
-argcv_quoted_length (const char *str, int *quote)
-{
- size_t len = 0;
-
- *quote = 0;
- for (; *str; str++)
- {
- if (*str == ' ')
- {
- len++;
- *quote = 1;
- }
- else if (*str == '"')
- {
- len += 2;
- *quote = 1;
- }
- else if (*str != '\t' && *str != '\\' && isprint (*str))
- len++;
- else if (argcv_quote_char (*str) != -1)
- len += 2;
- else
- len += 4;
- }
- return len;
-}
-
-void
-argcv_unquote_copy (char *dst, const char *src, size_t n)
-{
- int i = 0;
- int c;
- int expect_delim = 0;
-
- while (i < n)
- {
- switch (src[i])
- {
- case '\'':
- case '"':
- if (!expect_delim)
- {
- const char *p;
-
- for (p = src+i+1; *p && *p != src[i]; p++)
- if (*p == '\\')
- p++;
- if (*p)
- expect_delim = src[i++];
- else
- *dst++ = src[i++];
- }
- else if (expect_delim == src[i])
- ++i;
- else
- *dst++ = src[i++];
- break;
-
- case '\\':
- ++i;
- if (src[i] == 'x' || src[i] == 'X')
- {
- if (n - i < 2)
- {
- *dst++ = '\\';
- *dst++ = src[i++];
- }
- else
- {
- int off = xtonum(&c, src + i + 1, 16, 2);
- if (off == 0)
- {
- *dst++ = '\\';
- *dst++ = src[i++];
- }
- else
- {
- *dst++ = c;
- i += off + 1;
- }
- }
- }
- else if ((unsigned char)src[i] < 128 && isdigit (src[i]))
- {
- if (n - i < 1)
- {
- *dst++ = '\\';
- *dst++ = src[i++];
- }
- else
- {
- int off = xtonum (&c, src+i, 8, 3);
- if (off == 0)
- {
- *dst++ = '\\';
- *dst++ = src[i++];
- }
- else
- {
- *dst++ = c;
- i += off;
- }
- }
- }
- else
- *dst++ = argcv_unquote_char (src[i++]);
- break;
-
- default:
- *dst++ = src[i++];
- }
- }
- *dst = 0;
-}
-
-void
-argcv_quote_copy (char *dst, const char *src)
-{
- for (; *src; src++)
- {
- if (*src == '"')
- {
- *dst++ = '\\';
- *dst++ = *src;
- }
- else if (*src != '\t' && *src != '\\' && isprint(*src))
- *dst++ = *src;
- else
- {
- int c = argcv_quote_char (*src);
- *dst++ = '\\';
- if (c != -1)
- *dst++ = c;
- else
- {
- char tmp[4];
- snprintf (tmp, sizeof tmp, "%03o", *(unsigned char*)src);
- memcpy (dst, tmp, 3);
- dst += 3;
- }
- }
- }
-}
-
-int
-argcv_get_np (const char *command, int len,
- const char *delim, const char *cmnt,
- int flags,
- int *pargc, char ***pargv, char **endp)
-{
- int i = 0;
- struct argcv_info info;
- int argc;
- char **argv;
-
- if (!delim)
- delim = "";
- if (!cmnt)
- cmnt = "";
-
- init_argcv_info (&info, flags, len, command, delim, cmnt);
-
- /* Count number of arguments */
- argc = 0;
- while (argcv_scan (&info) <= len)
- argc++;
-
- argv = calloc ((argc + 1), sizeof (char *));
- if (argv == NULL)
- return ENOMEM;
-
- i = 0;
- info.save = 0;
- for (i = 0; i < argc; i++)
- {
- int n;
- int unquote;
-
- argcv_scan (&info);
-
- if ((command[info.start] == '"' || command[info.end] == '\'')
- && command[info.end] == command[info.start])
- {
- if (info.start < info.end)
- {
- info.start++;
- info.end--;
- }
- unquote = 0;
- }
- else
- unquote = 1;
-
- n = info.end - info.start + 1;
- argv[i] = calloc (n + 1, sizeof (char));
- if (argv[i] == NULL)
- {
- mu_argcv_free (i, argv);
- return ENOMEM;
- }
- if (unquote)
- argcv_unquote_copy (argv[i], &command[info.start], n);
- else
- memcpy (argv[i], &command[info.start], n);
- argv[i][n] = 0;
- }
- argv[i] = NULL;
-
- *pargc = argc;
- *pargv = argv;
- if (endp)
- *endp = (char*) (command + info.finish_pos);
- return 0;
-}
-
-int
-argcv_get_n (const char *command, int len, const char *delim, const char *cmnt,
- int *pargc, char ***pargv)
-{
- return argcv_get_np (command, len, delim, cmnt, MU_ARGCV_RETURN_DELIMS,
- pargc, pargv, NULL);
-}
-
-int
-argcv_get (const char *command, const char *delim, const char *cmnt,
- int *argc, char ***argv)
-{
- return argcv_get_n (command, strlen (command), delim, cmnt, argc, argv);
-}
-
-
-
-
diff --git a/libmailutils/base/vartab.c b/libmailutils/base/vartab.c
deleted file mode 100644
index 566a5077b..000000000
--- a/libmailutils/base/vartab.c
+++ /dev/null
@@ -1,346 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
- Copyright (C) 2007, 2010 Free Software Foundation, Inc.
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 3 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General
- Public License along with this library; If not, see
- <http://www.gnu.org/licenses/>. */
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/assoc.h>
-#include <mailutils/stream.h>
-#include <mailutils/errno.h>
-#include <mailutils/iterator.h>
-#include <mailutils/vartab.h>
-
-struct _mu_vartab
-{
- mu_assoc_t assoc;
- mu_stream_t stream;
- char *buf;
- size_t bufsize;
-};
-
-#define MU_VARDEFN_STATIC 0x1
-#define MU_VARDEFN_FUNC 0x2
-
-struct vardefn
-{
- int flags;
- char *value;
- mu_var_expansion_fp fun;
- mu_var_free_fp free;
- void *data;
-};
-
-int
-mu_vartab_create (mu_vartab_t *pvar)
-{
- int rc;
- struct _mu_vartab *p = calloc (1, sizeof *p);
- if (!p)
- return ENOMEM;
- rc = mu_assoc_create (&p->assoc, sizeof (struct vardefn), 0);
- if (rc)
- {
- free (p);
- return rc;
- }
- *pvar = p;
- return 0;
-}
-
-static void
-vardefn_free (struct vardefn *vd)
-{
- if (vd->value)
- {
- if (vd->free)
- vd->free (vd->data, vd->value);
- else if (!(vd->flags & MU_VARDEFN_STATIC))
- free (vd->value);
- }
- memset (vd, 0, sizeof vd);
-}
-
-int
-mu_vartab_destroy (mu_vartab_t *pvar)
-{
- int rc;
- mu_vartab_t var = *pvar;
- mu_iterator_t itr;
-
- if (!var)
- return EINVAL;
- rc = mu_assoc_get_iterator (var->assoc, &itr);
- if (rc)
- return rc;
- for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
- mu_iterator_next (itr))
- {
- struct vardefn *vd;
- mu_iterator_current (itr, (void**)&vd);
- vardefn_free (vd);
- }
- mu_iterator_destroy (&itr);
-
- mu_assoc_destroy (&var->assoc);
- mu_stream_destroy (&var->stream);
- free (var->buf);
- free (var);
- *pvar = NULL;
- return 0;
-}
-
-int
-mu_vartab_define (mu_vartab_t var, const char *name, const char *value,
- int isstatic)
-{
- int rc;
- struct vardefn *vd;
-
- if (!var)
- return EINVAL;
- rc = mu_assoc_ref_install (var->assoc, name, (void **) &vd);
- if (rc == MU_ERR_EXISTS)
- vardefn_free (vd);
- else if (rc != 0)
- return rc;
-
- if (isstatic)
- {
- vd->flags = MU_VARDEFN_STATIC;
- vd->value = (char*) value;
- }
- else
- {
- vd->flags = 0;
- vd->value = strdup (value);
- if (!vd->value)
- return ENOMEM;
- }
- return 0;
-}
-
-int
-mu_vartab_define_exp (mu_vartab_t var, const char *name,
- mu_var_expansion_fp fun, mu_var_free_fp free,
- void *data)
-{
- int rc;
- struct vardefn *vd;
-
- if (!var)
- return EINVAL;
- rc = mu_assoc_ref_install (var->assoc, name, (void **) &vd);
- if (rc == MU_ERR_EXISTS)
- vardefn_free (vd);
- else if (rc != 0)
- return rc;
-
- vd->flags = MU_VARDEFN_FUNC;
- vd->fun = fun;
- vd->free = free;
- vd->data = data;
- return 0;
-}
-
-int
-mu_vartab_count (mu_vartab_t vt, size_t *pcount)
-{
- if (!vt)
- return EINVAL;
- return mu_assoc_count (vt->assoc, pcount);
-}
-
-static int
-vardefn_expand (const char *name, struct vardefn *vd, const char **pvalue)
-{
- if (!vd->value)
- {
- if (vd->fun)
- {
- int rc = vd->fun (name, vd->data, &vd->value);
- if (rc)
- return rc;
- }
- else
- return EINVAL;
- }
- *pvalue = vd->value;
- return 0;
-}
-
-int
-mu_vartab_getvar (mu_vartab_t vt, const char *name, const char **pvalue)
-{
- struct vardefn *vdefn;
-
- if (!vt)
- return EINVAL;
- vdefn = mu_assoc_ref (vt->assoc, name);
- if (!vdefn)
- return MU_ERR_NOENT;
- return vardefn_expand (name, vdefn, pvalue);
-}
-
-static char *
-copy_name (mu_vartab_t vt, const char *name, size_t len)
-{
- if (len + 1 > vt->bufsize)
- {
- char *p = realloc (vt->buf, len + 1);
- if (!p)
- return NULL;
- vt->buf = p;
- vt->bufsize = len + 1;
- }
- memcpy (vt->buf, name, len);
- vt->buf[len] = 0;
- return vt->buf;
-}
-
-int
-mu_vartab_expand (mu_vartab_t vt, const char *str, char **pres)
-{
- int rc;
- mu_off_t size;
- const char *p;
-
- if (!vt)
- return EINVAL;
- if (!vt->stream)
- {
- rc = mu_memory_stream_create (&vt->stream, 0);
- if (rc)
- return rc;
- }
- else
- mu_stream_truncate (vt->stream, 0);
- mu_stream_seek (vt->stream, 0, MU_SEEK_SET, NULL);
-
- for (p = str; *p; )
- {
- if (*p == '$')
- {
- switch (*++p)
- {
- case '$':
- mu_stream_write (vt->stream, str, p - str, NULL);
- str = p + 1;
- p = str + 1;
- break;
-
- case '{':
- {
- const char *e = strchr (p + 1, '}');
- if (e)
- {
- const char *pvalue;
- size_t len = e - p - 1;
- char *name = copy_name (vt, p + 1, len);
- rc = mu_vartab_getvar (vt, name, &pvalue);
- if (rc == 0)
- {
- mu_stream_write (vt->stream, str, p - str - 1, NULL);
- mu_stream_write (vt->stream, pvalue, strlen (pvalue),
- NULL);
- str = e + 1;
- p = str + 1;
- }
- else if (rc == MU_ERR_NOENT)
- p = e + 1;
- else
- return rc;
- }
- else
- p++;
- }
- break;
-
- default:
- {
- char *name = copy_name (vt, p, 1);
- const char *pvalue;
- rc = mu_vartab_getvar (vt, name, &pvalue);
- if (rc == 0)
- {
- mu_stream_write (vt->stream, str, p - str - 1, NULL);
- mu_stream_write (vt->stream, pvalue, strlen (pvalue),
- NULL);
- str = p + 1;
- p = str + 1;
- }
- else if (rc == MU_ERR_NOENT)
- p++;
- else
- return rc;
- }
- break;
- }
- }
- else if (*p == '%')
- {
- /* allow `%' as prefix for single-character entities, for
- compatibility with v. prior to 1.2.91 */
- if (*++p == '%')
- {
- mu_stream_write (vt->stream, str, p - str, NULL);
- str = p + 1;
- p = str + 1;
- }
- else
- {
- char *name = copy_name (vt, p, 1);
- const char *pvalue;
- rc = mu_vartab_getvar (vt, name, &pvalue);
- if (rc == 0)
- {
- mu_stream_write (vt->stream, str, p - str - 1, NULL);
- mu_stream_write (vt->stream, pvalue, strlen (pvalue),
- NULL);
- str = p + 1;
- p = str + 1;
- }
- else if (rc == MU_ERR_NOENT)
- p++;
- else
- return rc;
- }
- }
- else
- p++;
- }
-
- if (p > str)
- mu_stream_write (vt->stream, str, p - str, NULL);
-
- mu_stream_size (vt->stream, &size);
- *pres = malloc (size + 1);
- if (!*pres)
- return ENOMEM;
- mu_stream_seek (vt->stream, 0, MU_SEEK_SET, NULL);
- mu_stream_read (vt->stream, *pres, size, NULL);
- (*pres)[size] = 0;
- return 0;
-}
-
-
-
-
-
-
-

Return to:

Send suggestions and report system problems to the System administrator.