diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-10-28 13:21:25 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-10-28 13:21:25 +0300 |
commit | 02334735e86f1cfda6a2e07cfea9283e4fc88011 (patch) | |
tree | 913b14a24eb171561141741b7b3c4f93105bcaa9 /libmailutils | |
parent | e9a329bfe70d903fb0a78007adaff6b565ddfc6b (diff) | |
download | mailutils-02334735e86f1cfda6a2e07cfea9283e4fc88011.tar.gz mailutils-02334735e86f1cfda6a2e07cfea9283e4fc88011.tar.bz2 |
New interface for composing URLs from parts.
* include/mailutils/url.h (mu_url_sget_name)
(mu_url_aget_name,mu_url_get_name)
(mu_url_invalidate,mu_url_create_null)
(mu_url_set_user,mu_url_set_path,mu_url_set_scheme)
(mu_url_set_host,mu_url_set_port,mu_url_set_service)
(mu_url_set_auth,mu_url_set_secret,mu_url_add_param)
(mu_url_clear_param,mu_url_add_query,mu_url_clear_query): New
prototypes.
* libmailutils/tests/url-comp.at: New test case.
* libmailutils/tests/url-comp.c: New test program.
* libmailutils/tests/.gitignore: Update.
* libmailutils/tests/Makefile.am: Add new files.
* libmailutils/tests/testsuite.at: Add new tests.
* libmailutils/url/Makefile.am: Add new files.
* libmailutils/url/add-param.c: New file.
* libmailutils/url/add-query.c: New file.
* libmailutils/url/clr-param.c: New file.
* libmailutils/url/clr-query.c: New file.
* libmailutils/url/null.c: New file.
* libmailutils/url/set-auth.c: New file.
* libmailutils/url/set-host.c: New file.
* libmailutils/url/set-path.c: New file.
* libmailutils/url/set-port.c: New file.
* libmailutils/url/set-scheme.c: New file.
* libmailutils/url/set-secret.c: New file.
* libmailutils/url/set-service.c: New file.
* libmailutils/url/set-user.c: New file.
* libmailutils/url/urlinv.c: New file.
* libmailutils/url/create.c (mu_url_create_hint): Return EINVAL if
purl is NULL.
* libmailutils/url/scheme.c (mu_url_set_scheme): Remove. See set-scheme.c
* libmailutils/url/urlstr.c (mu_url_sget_name)
(mu_url_aget_name,mu_url_get_name): New functions.
(mu_url_to_string): Rewrite using the above.
* libmailutils/base/Makefile.am: Add fscompose.c
* libmailutils/base/fscompose.c: New file.
Diffstat (limited to 'libmailutils')
26 files changed, 1278 insertions, 17 deletions
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am index 96ea2f8da..5f41b0c7a 100644 --- a/libmailutils/base/Makefile.am +++ b/libmailutils/base/Makefile.am @@ -31,6 +31,7 @@ libbase_la_SOURCES = \ fgetpwent.c\ filename.c\ freeitem.c\ + fscompose.c\ getcwd.c\ getmaxfd.c\ getpass.c\ diff --git a/libmailutils/base/fscompose.c b/libmailutils/base/fscompose.c new file mode 100644 index 000000000..7af13acdf --- /dev/null +++ b/libmailutils/base/fscompose.c @@ -0,0 +1,63 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2011 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 <string.h> +#include <mailutils/types.h> +#include <mailutils/util.h> +#include <mailutils/nls.h> +#include <mailutils/error.h> +#include <mailutils/errno.h> + +int +mu_file_safety_compose (int *res, const char *name, int defval) +{ + int negate = 0; + int val; + + if (*name == '-') + { + negate = 1; + name++; + } + else if (*name == '+') + name++; + + if (strcmp (name, "none") == 0) + { + *res = negate ? MU_FILE_SAFETY_ALL : MU_FILE_SAFETY_NONE; + return 0; + } + else if (strcmp (name, "all") == 0) + { + *res = negate ? MU_FILE_SAFETY_NONE : MU_FILE_SAFETY_ALL; + return 0; + } + else if (strcmp (name, "default") == 0) + val = defval; + else if (mu_file_safety_name_to_code (name, &val)) + return MU_ERR_NOENT; + + if (negate) + *res &= ~val; + else + *res |= val; + + return 0; +} diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore index 625bdc5d9..0860f078e 100644 --- a/libmailutils/tests/.gitignore +++ b/libmailutils/tests/.gitignore @@ -15,6 +15,7 @@ fsaf listop mailcap prop +url-comp url-parse wicket wsp diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am index 3b7540f19..66d83ca0e 100644 --- a/libmailutils/tests/Makefile.am +++ b/libmailutils/tests/Makefile.am @@ -51,6 +51,7 @@ noinst_PROGRAMS = \ mailcap\ prop\ tempfile\ + url-comp\ url-parse\ wicket\ wsp @@ -80,6 +81,7 @@ TESTSUITE_AT = \ prop.at\ testsuite.at\ url.at\ + url-comp.at\ wicket.at\ wordsplit.at diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at index 91462b38d..e29846ba1 100644 --- a/libmailutils/tests/testsuite.at +++ b/libmailutils/tests/testsuite.at @@ -58,6 +58,7 @@ m4_include([list.at]) m4_include([address.at]) m4_include([wordsplit.at]) m4_include([url.at]) +m4_include([url-comp.at]) m4_include([mailcap.at]) m4_include([wicket.at]) m4_include([prop.at]) diff --git a/libmailutils/tests/url-comp.at b/libmailutils/tests/url-comp.at new file mode 100644 index 000000000..e776e4bfe --- /dev/null +++ b/libmailutils/tests/url-comp.at @@ -0,0 +1,72 @@ +# This file is part of GNU Mailutils. -*- Autotest -*- +# Copyright (C) 2011 Free Software Foundation, Inc. +# +# GNU Mailutils is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 3, or (at +# your option) any later version. +# +# GNU Mailutils 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 +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. + +AT_BANNER(URL Composition) + +dnl ------------------------------------------------------------ + 1 2 3 4 +dnl URLCOMP(KW, ARGS, [STDOUT = `'], [STDERR = `']) +dnl + +m4_pushdef([URLCOMP],[ +AT_SETUP([$2 => $3]) +AT_KEYWORDS([url-comp url-comp-$1]) +AT_CHECK([url-comp $2], +[m4_if([$4],,0,1)], +[m4_if([$3],,,[$3 +])], +m4_if([$4],,,[url-comp: $4 +])) +AT_CLEANUP +]) + +URLCOMP(0, [], [], [URL missing required parts]) +URLCOMP(1, [path=foo], [foo]) +URLCOMP(2, [scheme=file], [file://]) +URLCOMP(3, [scheme=file path=foo], [file://foo]) +URLCOMP(4, [scheme=smtp host=localhost port=587], [smtp://localhost:587]) +URLCOMP(5, + [scheme=smtp host=localhost port=587 param=strip-domain param=domain=example.com], + [smtp://localhost:587;strip-domain;domain=example.com]) +URLCOMP(6, [scheme=imap user=gray host=localhost path=INBOX], + [imap://gray@localhost/INBOX]) +URLCOMP(7, + [scheme=imap user=gray pass=test host=localhost path=INBOX], + [imap://gray:***@localhost/INBOX]) +URLCOMP(8, + [scheme=pop user=gray pass=test auth=+APOP host=localhost path=INBOX], + [pop://gray:***;AUTH=+APOP@localhost/INBOX]) +URLCOMP(9, + [scheme=prog path=/bin/mailman query=request query=list], + [prog:///bin/mailman?request&list]) +URLCOMP(10, + [scheme=prog path=/bin/mailman query=request query=list param=test], + [prog:///bin/mailman;test?request&list]) +URLCOMP(11, + [url=imap://gray@localhost/INBOX path=], + [imap://gray@localhost]) +URLCOMP(12, + [url=imap://gray@localhost/INBOX path= user=], + [imap://localhost]) +URLCOMP(13, + [url='prog:///bin/mailman;test?request&list' query= param=], + [prog:///bin/mailman]) +URLCOMP(14, + [url=smtp://gray@localhost/test host=], + [], + [URL missing required parts]) + +m4_popdef([URLCOMP])
\ No newline at end of file diff --git a/libmailutils/tests/url-comp.c b/libmailutils/tests/url-comp.c new file mode 100644 index 000000000..e1344ba77 --- /dev/null +++ b/libmailutils/tests/url-comp.c @@ -0,0 +1,147 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2011 Free Software Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GNU Mailutils 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <mailutils/error.h> +#include <mailutils/errno.h> +#include <mailutils/url.h> +#include <mailutils/secret.h> +#include <mailutils/kwd.h> + +static const char * +strval (const char *str) +{ + return str[0] ? str : NULL; +} + +void +usage (FILE *str, int code) +{ + fprintf (str, "usage: %s [url=URL] [OPTIONS]\n", mu_program_name); + exit (code); +} + +int +main (int argc, char **argv) +{ + int i = 1, rc; + mu_url_t url; + const char *arg; + + mu_set_program_name (argv[0]); + + if (argc > 1) + { + if (strcmp (argv[1], "help") == 0 || + strcmp (argv[1], "--help") == 0 || + strcmp (argv[1], "-h") == 0) + usage (stdout, 0); + + if (strncmp (argv[1], "url=", 4) == 0) + { + MU_ASSERT (mu_url_create (&url, argv[1] + 4)); + i = 2; + } + } + + if (!url) + { + MU_ASSERT (mu_url_create_null (&url)); + i = 1; + } + + for (; i < argc; i++) + { + if (strncmp (argv[i], "scheme=", 7) == 0) + { + MU_ASSERT (mu_url_set_scheme (url, strval (argv[i] + 7))); + } + else if (strncmp (argv[i], "user=", 5) == 0) + { + MU_ASSERT (mu_url_set_user (url, strval (argv[i] + 5))); + } + else if (strncmp (argv[i], "path=", 5) == 0) + { + MU_ASSERT (mu_url_set_path (url, strval (argv[i] + 5))); + } + else if (strncmp (argv[i], "host=", 5) == 0) + { + MU_ASSERT (mu_url_set_host (url, strval (argv[i] + 5))); + } + else if (strncmp (argv[i], "port=", 5) == 0) + { + MU_ASSERT (mu_url_set_port (url, atoi (argv[i] + 5))); + } + else if (strncmp (argv[i], "service=", 8) == 0) + { + MU_ASSERT (mu_url_set_service (url, strval (argv[i] + 8))); + } + else if (strncmp (argv[i], "auth=", 5) == 0) + { + MU_ASSERT (mu_url_set_auth (url, strval (argv[i] + 5))); + } + else if (strncmp (argv[i], "pass=", 5) == 0) + { + mu_secret_t secret; + + arg = strval (argv[i] + 5); + if (arg) + { + MU_ASSERT (mu_secret_create (&secret, arg, strlen (arg))); + } + else + secret = NULL; + MU_ASSERT (mu_url_set_secret (url, secret)); + } + else if (strncmp (argv[i], "param=", 6) == 0) + { + arg = strval (argv[i] + 6); + if (arg) + MU_ASSERT (mu_url_add_param (url, 1, (const char **)&arg)); + else + MU_ASSERT (mu_url_clear_param (url)); + } + else if (strncmp (argv[i], "query=", 6) == 0) + { + arg = strval (argv[i] + 6); + if (arg) + MU_ASSERT (mu_url_add_query (url, 1, (const char **)&arg)); + else + MU_ASSERT (mu_url_clear_query (url)); + } + else + { + mu_error ("unrecognized argument: %s", argv[i]); + return 1; + } + } + + rc = mu_url_sget_name (url, &arg); + if (rc) + { + mu_error ("%s", mu_strerror (rc)); + return 1; + } + + + printf ("%s\n", arg); + return 0; +} diff --git a/libmailutils/url/Makefile.am b/libmailutils/url/Makefile.am index 6f4a9c0cd..937f82379 100644 --- a/libmailutils/url/Makefile.am +++ b/libmailutils/url/Makefile.am @@ -19,6 +19,10 @@ noinst_LTLIBRARIES = liburl.la liburl_la_SOURCES = \ accessor.h\ + add-param.c\ + clr-param.c\ + add-query.c\ + clr-query.c\ copy.c\ create.c\ decode.c\ @@ -36,9 +40,19 @@ liburl_la_SOURCES = \ get-secret.c\ get-user.c\ match.c\ + null.c\ port.c\ scheme.c\ + set-auth.c\ + set-host.c\ + set-path.c\ + set-port.c\ + set-scheme.c\ + set-secret.c\ + set-service.c\ + set-user.c\ uplevel.c\ + urlinv.c\ urlstr.c INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils diff --git a/libmailutils/url/add-param.c b/libmailutils/url/add-param.c new file mode 100644 index 000000000..0bd0f4301 --- /dev/null +++ b/libmailutils/url/add-param.c @@ -0,0 +1,74 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 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 <errno.h> +#include <stdlib.h> +#include <string.h> +#ifdef HAVE_STRINGS_H +# include <strings.h> +#endif + +#include <mailutils/types.h> +#include <mailutils/errno.h> +#include <mailutils/sys/url.h> +#include <mailutils/url.h> + +int +mu_url_add_param (mu_url_t url, size_t pc, const char **pv) +{ + char **fv; + int i, j; + + if (!url) + return EINVAL; + if (!pc || !pv) + return 0; + + fv = realloc (url->fvpairs, + sizeof (url->fvpairs[0]) * (url->fvcount + pc + 1)); + if (!fv) + return ENOMEM; + url->fvpairs = fv; + for (i = url->fvcount, j = 0; j < pc; i++, j++) + { + fv[i] = strdup (pv[j]); + if (!fv[i]) + { + /* Restore the status quo */ + for (; j; j--) + free (fv[--i]); + if (url->fvcount) + fv[url->fvcount] = NULL; + else + { + free (url->fvpairs); + url->fvpairs = NULL; + url->fvcount = 0; + } + return ENOMEM; + } + } + fv[i] = NULL; + url->fvcount = i; + url->flags |= MU_URL_PARAM; + mu_url_invalidate (url); + return 0; +} diff --git a/libmailutils/url/add-query.c b/libmailutils/url/add-query.c new file mode 100644 index 000000000..8d810bb5e --- /dev/null +++ b/libmailutils/url/add-query.c @@ -0,0 +1,74 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 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 <errno.h> +#include <stdlib.h> +#include <string.h> +#ifdef HAVE_STRINGS_H +# include <strings.h> +#endif + +#include <mailutils/types.h> +#include <mailutils/errno.h> +#include <mailutils/sys/url.h> +#include <mailutils/url.h> + +int +mu_url_add_query (mu_url_t url, size_t pc, const char **pv) +{ + char **fv; + int i, j; + + if (!url) + return EINVAL; + if (!pc || !pv) + return 0; + + fv = realloc (url->qargv, + sizeof (url->qargv[0]) * (url->qargc + pc + 1)); + if (!fv) + return ENOMEM; + url->qargv = fv; + for (i = url->qargc, j = 0; j < pc; i++, j++) + { + fv[i] = strdup (pv[j]); + if (!fv[i]) + { + /* Restore the status quo */ + for (; j; j--) + free (fv[--i]); + if (url->qargc) + fv[url->qargc] = NULL; + else + { + free (url->qargv); + url->qargv = NULL; + url->qargc = 0; + } + return ENOMEM; + } + } + fv[i] = NULL; + url->qargc = i; + url->flags |= MU_URL_QUERY; + mu_url_invalidate (url); + return 0; +} diff --git a/libmailutils/url/clr-param.c b/libmailutils/url/clr-param.c new file mode 100644 index 000000000..0feb80f6d --- /dev/null +++ b/libmailutils/url/clr-param.c @@ -0,0 +1,50 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 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 <errno.h> +#include <stdlib.h> +#include <string.h> +#ifdef HAVE_STRINGS_H +# include <strings.h> +#endif + +#include <mailutils/types.h> +#include <mailutils/errno.h> +#include <mailutils/sys/url.h> +#include <mailutils/url.h> + +int +mu_url_clear_param (mu_url_t url) +{ + int i; + + if (!url) + return EINVAL; + + for (i = 0; i < url->fvcount; i++) + free (url->fvpairs[i]); + free (url->fvpairs); + url->fvpairs = NULL; + url->fvcount = 0; + url->flags &= ~MU_URL_PARAM; + mu_url_invalidate (url); + return 0; +} diff --git a/libmailutils/url/clr-query.c b/libmailutils/url/clr-query.c new file mode 100644 index 000000000..186cb25ff --- /dev/null +++ b/libmailutils/url/clr-query.c @@ -0,0 +1,50 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 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 <errno.h> +#include <stdlib.h> +#include <string.h> +#ifdef HAVE_STRINGS_H +# include <strings.h> +#endif + +#include <mailutils/types.h> +#include <mailutils/errno.h> +#include <mailutils/sys/url.h> +#include <mailutils/url.h> + +int +mu_url_clear_query (mu_url_t url) +{ + int i; + + if (!url) + return EINVAL; + + for (i = 0; i < url->qargc; i++) + free (url->qargv[i]); + free (url->qargv); + url->qargv = NULL; + url->qargc = 0; + url->flags &= ~MU_URL_QUERY; + mu_url_invalidate (url); + return 0; +} diff --git a/libmailutils/url/create.c b/libmailutils/url/create.c index 130b7a2bc..d6b01be2c 100644 --- a/libmailutils/url/create.c +++ b/libmailutils/url/create.c @@ -532,7 +532,11 @@ mu_url_create_hint (mu_url_t *purl, const char *str, int flags, { int rc; struct mu_url_ctx ctx; - mu_url_t url = calloc (1, sizeof (*url)); + mu_url_t url; + + if (!purl) + return EINVAL; + url = calloc (1, sizeof (*url)); if (url == NULL) return ENOMEM; url->name = strdup (str); diff --git a/libmailutils/url/flag.c b/libmailutils/url/flag.c index 586c8817b..4387948fb 100644 --- a/libmailutils/url/flag.c +++ b/libmailutils/url/flag.c @@ -27,7 +27,7 @@ mu_url_get_flags (mu_url_t url, int *pf) { if (!url || !pf) return EINVAL; - *pf = url->flags; + *pf = url->flags; return 0; } diff --git a/libmailutils/url/null.c b/libmailutils/url/null.c new file mode 100644 index 000000000..471f19492 --- /dev/null +++ b/libmailutils/url/null.c @@ -0,0 +1,38 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2010, 2011 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 <errno.h> +#include <mailutils/sys/url.h> +#include <mailutils/url.h> + +int +mu_url_create_null (mu_url_t *purl) +{ + mu_url_t url; + + if (!purl) + return EINVAL; + url = calloc (1, sizeof (*url)); + if (url == NULL) + return ENOMEM; + *purl = url; + return 0; +} diff --git a/libmailutils/url/scheme.c b/libmailutils/url/scheme.c index fa9fbf179..ec42f70e0 100644 --- a/libmailutils/url/scheme.c +++ b/libmailutils/url/scheme.c @@ -31,19 +31,6 @@ #include <mailutils/sys/url.h> int -mu_url_set_scheme (mu_url_t url, const char *scheme) -{ - char *p; - if (!url || !scheme) - return EINVAL; - p = realloc (url->scheme, strlen (scheme) + 1); - if (!p) - return ENOMEM; - strcpy (url->scheme, scheme); - return 0; -} - -int mu_url_is_scheme (mu_url_t url, const char *scheme) { if (url && scheme && url->scheme diff --git a/libmailutils/url/set-auth.c b/libmailutils/url/set-auth.c new file mode 100644 index 000000000..b4e9e610d --- /dev/null +++ b/libmailutils/url/set-auth.c @@ -0,0 +1,58 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 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 <errno.h> +#include <stdlib.h> +#include <string.h> +#ifdef HAVE_STRINGS_H +# include <strings.h> +#endif + +#include <mailutils/types.h> +#include <mailutils/errno.h> +#include <mailutils/sys/url.h> +#include <mailutils/url.h> + +int +mu_url_set_auth (mu_url_t url, const char *auth) +{ + char *copy; + + if (!url) + return EINVAL; + if (auth) + { + copy = strdup (auth); + if (!copy) + return ENOMEM; + url->flags |= MU_URL_AUTH; + } + else + { + url->flags &= ~MU_URL_AUTH; + copy = NULL; + } + free (url->auth); + url->auth = copy; + url->_get_auth = NULL; + mu_url_invalidate (url); + return 0; +} diff --git a/libmailutils/url/set-host.c b/libmailutils/url/set-host.c new file mode 100644 index 000000000..6cb21f61d --- /dev/null +++ b/libmailutils/url/set-host.c @@ -0,0 +1,73 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 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 <errno.h> +#include <stdlib.h> +#include <string.h> +#ifdef HAVE_STRINGS_H +# include <strings.h> +#endif + +#include <mailutils/types.h> +#include <mailutils/errno.h> +#include <mailutils/sys/url.h> +#include <mailutils/url.h> + +int +mu_url_set_host (mu_url_t url, const char *host) +{ + char *copy; + + if (!url) + return EINVAL; + if (host) + { + size_t len; + int flag = MU_URL_HOST; + + len = strlen (host); + if (len == 0) + return EINVAL; + if (host[0] == '[' && host[len-1] == ']') + { + flag |= MU_URL_IPV6; + host++; + len -= 2; + } + + copy = malloc (len + 1); + if (!copy) + return ENOMEM; + memcpy (copy, host, len); + copy[len] = 0; + |