summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2020-08-18 14:38:10 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2020-08-18 14:38:10 (GMT)
commitd2eb03a3b9db683418cc0bd084915468e4968854 (patch) (side-by-side diff)
treeec40bc0c8e9f9f53acb9cb0b7d76ee197410c85c
parent37713b42a501892469234b90454731d8d8b7a3e6 (diff)
downloadmailutils-d2eb03a3b9db683418cc0bd084915468e4968854.tar.gz
mailutils-d2eb03a3b9db683418cc0bd084915468e4968854.tar.bz2
libmailutils/tests tools: use mu_cli_simple for option handling
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libmailutils/tests/cidr.c43
-rw-r--r--libmailutils/tests/debugspec.c54
-rw-r--r--libmailutils/tests/decode2047.c36
-rw-r--r--libmailutils/tests/encode2047.c43
-rw-r--r--libmailutils/tests/fsfolder.c60
-rw-r--r--libmailutils/tests/globtest.c55
-rw-r--r--libmailutils/tests/imapio.at2
-rw-r--r--libmailutils/tests/imapio.c56
-rw-r--r--libmailutils/tests/mailcap.c61
-rw-r--r--libmailutils/tests/mcf.c49
-rw-r--r--libmailutils/tests/modmesg.c134
-rw-r--r--libmailutils/tests/msgset.c187
-rw-r--r--libmailutils/tests/scantime.c39
-rw-r--r--libmailutils/tests/stream-getdelim.c6
-rw-r--r--libmailutils/tests/strerr.at22
-rw-r--r--libmailutils/tests/strftime.c62
-rw-r--r--libmailutils/tests/strin.c74
-rw-r--r--libmailutils/tests/strout.at22
-rw-r--r--libmailutils/tests/strout.c77
-rw-r--r--libmailutils/tests/t-streamshift.c336
-rw-r--r--libmailutils/tests/t0-stream.c2
-rw-r--r--libmailutils/tests/temp_stream.c50
-rw-r--r--libmailutils/tests/tempfile.c130
23 files changed, 858 insertions, 742 deletions
diff --git a/libmailutils/tests/cidr.c b/libmailutils/tests/cidr.c
index 20e18b0..febf92f 100644
--- a/libmailutils/tests/cidr.c
+++ b/libmailutils/tests/cidr.c
@@ -30,36 +30,40 @@ print_bytes (unsigned char *b, size_t l)
printf ("\n");
}
+static int ipv6_simplify;
+
+struct mu_option options[] = {
+ { "simplify", 's', NULL, MU_OPTION_DEFAULT,
+ "simplify IPv6 addresses", mu_c_bool, &ipv6_simplify },
+ MU_OPTION_END
+};
+
int
main (int argc, char **argv)
{
- int flags = 0;
-
mu_set_program_name (argv[0]);
- if (argc < 2)
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC, "cidr test tool",
+ MU_CLI_OPTION_PROG_ARGS, "CIDR [CIDR...]",
+ MU_CLI_OPTION_RETURN_ARGC, &argc,
+ MU_CLI_OPTION_RETURN_ARGV, &argv,
+ MU_CLI_OPTION_END);
+
+ if (argc == 0)
{
- mu_error ("usage: %s [-sS] CIDR [CIDR...]", argv[0]);
+ mu_error ("required argument missing; try %s --help for more info",
+ mu_program_name);
return 1;
}
- while (--argc)
+ while (argc--)
{
- char *arg = *++argv;
+ char *arg = *argv++;
struct mu_cidr cidr;
int rc;
char *str;
- if (strcmp (arg, "-s") == 0)
- {
- flags |= MU_CIDR_FMT_SIMPLIFY;
- continue;
- }
- else if (strcmp (arg, "-S") == 0)
- {
- flags &= ~MU_CIDR_FMT_SIMPLIFY;
- continue;
- }
-
rc = mu_cidr_from_string (&cidr, arg);
if (rc)
{
@@ -74,10 +78,11 @@ main (int argc, char **argv)
print_bytes (cidr.address, cidr.len);
printf ("netmask =");
print_bytes (cidr.netmask, cidr.len);
- rc = mu_cidr_format (&cidr, flags, &str);
+ rc = mu_cidr_format (&cidr, ipv6_simplify ? MU_CIDR_FMT_SIMPLIFY : 0,
+ &str);
if (rc)
{
- mu_error ("cannot covert to string: %s", mu_strerror (rc));
+ mu_error ("cannot convert to string: %s", mu_strerror (rc));
return 2;
}
diff --git a/libmailutils/tests/debugspec.c b/libmailutils/tests/debugspec.c
index 9c6feaa..f99b720 100644
--- a/libmailutils/tests/debugspec.c
+++ b/libmailutils/tests/debugspec.c
@@ -29,47 +29,35 @@ main (int argc, char **argv)
{
char *names = NULL;
int showunset = 0;
- char *arg;
+ struct mu_option options[] = {
+ { "names", 0, "NAME[;NAME...]", MU_OPTION_DEFAULT,
+ "show only selected categories", mu_c_string, &names },
+ { "showunset", 0, NULL, MU_OPTION_DEFAULT,
+ "show unset debug categories as well", mu_c_incr, &showunset },
+ MU_OPTION_END
+ };
mu_set_program_name (argv[0]);
mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
- if (argc == 1)
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_SINGLE_DASH,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC, "Mailutils debug specification test tool",
+ MU_CLI_OPTION_PROG_ARGS, "SPEC",
+ MU_CLI_OPTION_RETURN_ARGC, &argc,
+ MU_CLI_OPTION_RETURN_ARGV, &argv,
+ MU_CLI_OPTION_END);
+
+
+ if (argc != 1)
{
- mu_printf ("usage: %s spec\n", argv[0]);
+ mu_error ("exactly one argument expected; try %s -help for more info",
+ mu_program_name);
return 0;
}
- while (argc--)
- {
- arg = *++argv;
-
- if (strncmp (arg, "-names=", 7) == 0)
- names = arg + 7;
- else if (strcmp (arg, "-showunset") == 0)
- showunset = 1;
- else if (arg[0] == '-')
- {
- if (arg[1] == '-' && arg[2] == 0)
- {
- argc--;
- argv++;
- break;
- }
- mu_error ("unrecognised argument: %s", arg);
- return 1;
- }
- else
- break;
- }
-
- if (argc != 1)
- {
- mu_error ("usage: %s spec", mu_program_name);
- return 1;
- }
-
- mu_debug_parse_spec (arg);
+ mu_debug_parse_spec (argv[0]);
mu_debug_format_spec (mu_strout, names, showunset);
mu_printf ("\n");
diff --git a/libmailutils/tests/decode2047.c b/libmailutils/tests/decode2047.c
index 2e3c133..d2cf185 100644
--- a/libmailutils/tests/decode2047.c
+++ b/libmailutils/tests/decode2047.c
@@ -20,19 +20,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
-#include <string.h>
-
#include <mailutils/mailutils.h>
-#define ISPRINT(c) ((c)=='\t'||(c)==' '||(c)=='\n'||\
- ((unsigned char)(c)>' '&&(unsigned char)(c)<128))
-
void
print (char *p, int printable)
{
for (; *p; p++)
{
- if (printable && *p != '\n' && !ISPRINT (*p))
+ if (printable && *p != '\n' && !mu_isprint (*p))
printf ("\\%03o", *(unsigned char *) p);
else
putchar (*p);
@@ -43,23 +38,22 @@ int
main (int argc, char *argv[])
{
char buf[256];
- int c, printable = 0;
+ int printable = 0;
char *charset = "iso-8859-1";
-
- while ((c = getopt (argc, argv, "c:p")) != EOF)
- switch (c)
- {
- case 'c':
- charset = optarg;
- break;
- case 'p':
- printable = 1;
- break;
-
- default:
- exit (1);
- }
+ struct mu_option options[] = {
+ { "charset", 'c', "NAME", MU_OPTION_DEFAULT,
+ "define output character set", mu_c_string, &charset },
+ { "printable", 'p', NULL, MU_OPTION_DEFAULT,
+ "make sure the output is printable", mu_c_incr, &printable },
+ MU_OPTION_END
+ };
+ mu_set_program_name (argv[0]);
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC, "Test RFC 2047 decoding function",
+ MU_CLI_OPTION_END);
+
while (fgets (buf, sizeof (buf), stdin))
{
char *p = NULL;
diff --git a/libmailutils/tests/encode2047.c b/libmailutils/tests/encode2047.c
index 5ee89e1..1880efd 100644
--- a/libmailutils/tests/encode2047.c
+++ b/libmailutils/tests/encode2047.c
@@ -110,35 +110,24 @@ main (int argc, char *argv[])
char *charset = "iso-8859-1";
char *encoding = "quoted-printable";
int octal = 0;
-
- while ((rc = getopt (argc, argv, "c:e:hot")) != EOF)
- switch (rc)
- {
- case 'c':
- charset = optarg;
- break;
-
- case 'e':
- encoding = optarg;
- break;
-
- case 'o':
- octal = 1;
- break;
-
- case 't':
- octal = 0;
- break;
-
- case 'h':
- printf ("usage: %s [-c charset] [-e encoding] [-ot]\n", argv[0]);
- exit (0);
-
- default:
- exit (1);
- }
+ struct mu_option options[] = {
+ { "charset", 'c', "NAME", MU_OPTION_DEFAULT,
+ "define input character set", mu_c_string, &charset },
+ { "encoding", 'e', "NAME", MU_OPTION_DEFAULT,
+ "define input encoding", mu_c_string, &encoding },
+ { "octal", 'o', NULL, MU_OPTION_DEFAULT,
+ "decode octal escape notations on input", mu_c_bool, &octal },
+ MU_OPTION_END
+ };
+ mu_set_program_name (argv[0]);
mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC, "Test RFC 2047 encoding function",
+ MU_CLI_OPTION_EXTRA_INFO, "Defaults are: --charset=iso-8859-1 --encoding=quoted-printable",
+ MU_CLI_OPTION_END);
+
while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0)
{
char *p;
diff --git a/libmailutils/tests/fsfolder.c b/libmailutils/tests/fsfolder.c
index 7e0952f..9735057 100644
--- a/libmailutils/tests/fsfolder.c
+++ b/libmailutils/tests/fsfolder.c
@@ -33,6 +33,7 @@
#include <mailutils/sys/registrar.h>
#include <mailutils/assoc.h>
#include <mailutils/iterator.h>
+#include <mailutils/cli.h>
#include "tesh.h"
int sort_option;
@@ -227,16 +228,6 @@ static struct mu_tesh_command comtab[] = {
{ NULL }
};
-static void
-usage (void)
-{
- mu_printf (
- "usage: %s [-debug=SPEC] -name=URL [-sort] [-glob] OP ARG... [\\; OP ARG...]...]\n",
- mu_program_name);
- mu_printf ("OPerations and corresponding ARGuments are:\n");
- mu_tesh_help (comtab, NULL);
-}
-
static int
_always_is_scheme (mu_record_t record, mu_url_t url, int flags)
{
@@ -314,39 +305,42 @@ static struct _mu_record reg_record =
int
main (int argc, char **argv)
{
- int i;
int rc;
mu_folder_t folder;
char *fname = NULL;
int glob_option = 0;
-
+ struct mu_option options[] = {
+ { "name", 0, "NAME-OR-URL", MU_OPTION_DEFAULT,
+ "set file name (mandatory)", mu_c_string, &fname },
+ { "sort", 0, NULL, MU_OPTION_DEFAULT,
+ "sort folders", mu_c_incr, &sort_option },
+ { "glob", 0, NULL, MU_OPTION_DEFAULT,
+ "assume globbing patterns", mu_c_incr, &glob_option },
+ MU_OPTION_END
+ };
+ char *capa[] = { "debug", NULL };
+
+ mu_set_program_name (argv[0]);
mu_tesh_init (argv[0]);
mu_registrar_record (&any_record);
mu_registrar_record (&reg_record);
- if (argc == 1)
- {
- usage ();
- exit (0);
- }
-
- for (i = 1; i < argc; i++)
- {
- if (strncmp (argv[i], "-debug=", 7) == 0)
- mu_debug_parse_spec (argv[i] + 7);
- else if (strncmp (argv[i], "-name=", 6) == 0)
- fname = argv[i] + 6;
- else if (strcmp (argv[i], "-sort") == 0)
- sort_option = 1;
- else if (strcmp (argv[i], "-glob") == 0)
- glob_option = 1;
- else
- break;
- }
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_SINGLE_DASH,
+ MU_CLI_OPTION_IN_ORDER,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_CAPABILITIES, capa,
+ MU_CLI_OPTION_RETURN_ARGC, &argc,
+ MU_CLI_OPTION_RETURN_ARGV, &argv,
+ MU_CLI_OPTION_PROG_DOC, "Test file system folder traversal",
+ MU_CLI_OPTION_PROG_ARGS, "[OP ARG... [\\; OP ARG...]...]",
+ MU_CLI_OPTION_END);
+
if (!fname)
{
- mu_error ("name not specified");
+ mu_error ("-name not specified; try %s -help for more info",
+ mu_program_name);
exit (1);
}
@@ -376,7 +370,7 @@ main (int argc, char **argv)
if (glob_option)
mu_folder_set_match (folder, mu_folder_glob_match);
- mu_tesh_read_and_eval (argc - i, argv + i, comtab, folder);
+ mu_tesh_read_and_eval (argc, argv, comtab, folder);
mu_folder_close (folder);
mu_folder_destroy (&folder);
diff --git a/libmailutils/tests/globtest.c b/libmailutils/tests/globtest.c
index a955057..d44e4cd 100644
--- a/libmailutils/tests/globtest.c
+++ b/libmailutils/tests/globtest.c
@@ -26,37 +26,42 @@ main (int argc, char **argv)
int flags = 0;
int rc;
int i;
-
+ int sub_opt = 0, icase_opt = 0, collapse_opt = 0;
+ struct mu_option options[] = {
+ { "icase", 'i', NULL, MU_OPTION_DEFAULT,
+ "ignore case", mu_c_incr, &icase_opt },
+ { "sub", 's', NULL, MU_OPTION_DEFAULT,
+ "treat each wildcard as regexp parenthesized group",
+ mu_c_incr, &sub_opt },
+ { "collapse", 'c', NULL, MU_OPTION_DEFAULT,
+ "collapse contiguous runs of *", mu_c_incr, &collapse_opt },
+ MU_OPTION_END
+ };
+
mu_set_program_name (argv[0]);
- for (i = 1; i < argc; i++)
- {
- char *a = argv[i];
- if (strcmp (a, "-i") == 0)
- flags |= MU_GLOBF_ICASE;
- else if (strcmp (a, "-s") == 0)
- flags |= MU_GLOBF_SUB;
- else if (strcmp (a, "-c") == 0)
- flags |= MU_GLOBF_COLLAPSE;
- else if (strcmp (a, "--") == 0)
- {
- i++;
- break;
- }
- else if (*a != '-')
- break;
- else
- {
- mu_error ("unknown option %s", a);
- return 1;
- }
- }
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC, "convert globbing pattern to regexp",
+ MU_CLI_OPTION_PROG_ARGS, "PATTERN [STRING ...]",
+ MU_CLI_OPTION_RETURN_ARGC, &argc,
+ MU_CLI_OPTION_RETURN_ARGV, &argv,
+ MU_CLI_OPTION_END);
+
+ if (icase_opt)
+ flags |= MU_GLOBF_ICASE;
+ if (sub_opt)
+ flags |= MU_GLOBF_SUB;
+ if (collapse_opt)
+ flags |= MU_GLOBF_COLLAPSE;
- if (i == argc)
+ if (argc == 0)
{
- mu_printf ("usage: %s [-ics] PATTERN [WORD...]\n", mu_program_name);
+ mu_error ("pattern must be given; try %s --help for details",
+ mu_program_name);
return 1;
}
+ i = 0;
pattern = argv[i++];
if (i == argc)
diff --git a/libmailutils/tests/imapio.at b/libmailutils/tests/imapio.at
index 779d333..3a0ea90 100644
--- a/libmailutils/tests/imapio.at
+++ b/libmailutils/tests/imapio.at
@@ -93,7 +93,7 @@ fat man
3: 'fat man'
])
-m4_pushdef([IMAPIO_FLAGS],[-server])
+m4_pushdef([IMAPIO_FLAGS],[--server])
IMAPIO([literals (server)],[literal],
[A001 LOGIN {11}
FRED FOOBAR {7}
diff --git a/libmailutils/tests/imapio.c b/libmailutils/tests/imapio.c
index bcccc95..4746f4a 100644
--- a/libmailutils/tests/imapio.c
+++ b/libmailutils/tests/imapio.c
@@ -25,14 +25,7 @@
#include <mailutils/error.h>
#include <mailutils/stream.h>
#include <mailutils/stdstream.h>
-
-void
-usage ()
-{
- mu_stream_printf (mu_strout, "usage: %s [debug=SPEC] [-transcript] [-server]\n",
- mu_program_name);
- exit (0);
-}
+#include <mailutils/cli.h>
int
main (int argc, char **argv)
@@ -41,31 +34,32 @@ main (int argc, char **argv)
int transcript = 0;
mu_imapio_t io;
mu_stream_t str;
- int imapio_mode = MU_IMAPIO_CLIENT;
+ int imapio_mode;
+ int server_opt = 0;
+ struct mu_option options[] = {
+ { "transcript", 't', NULL, MU_OPTION_DEFAULT,
+ "enable transcript", mu_c_incr, &transcript },
+ { "server", 's', NULL, MU_OPTION_DEFAULT,
+ "server mode", mu_c_incr, &server_opt },
+ MU_OPTION_END
+ };
+ char *capa[] = { "debug", NULL };
+ mu_stream_t dstr;
+ int t = 1;
- mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
-
- for (i = 1; i < argc; i++)
- {
- char *opt = argv[i];
+ /* Create a separate diagnostic stream, independent from mu_strerr */
+ MU_ASSERT (mu_stdio_stream_create (&dstr, MU_STDERR_FD, 0));
+ mu_stream_ioctl (dstr, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &t);
- if (strncmp (opt, "debug=", 6) == 0)
- mu_debug_parse_spec (opt + 6);
- else if (strcmp (opt, "-transcript") == 0)
- transcript = 1;
- else if (strcmp (opt, "-server") == 0)
- imapio_mode = MU_IMAPIO_SERVER;
- else if (strcmp (opt, "-h") == 0)
- usage ();
- else
- {
- mu_error ("%s: unrecognized argument", opt);
- exit (1);
- }
- }
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_CAPABILITIES, capa,
+ MU_CLI_OPTION_PROG_DOC, "imap parser test tool",
+ MU_CLI_OPTION_END);
- MU_ASSERT (mu_iostream_create (&str, mu_strin, mu_strout));
+ imapio_mode = server_opt ? MU_IMAPIO_SERVER : MU_IMAPIO_CLIENT;
+ MU_ASSERT (mu_iostream_create (&str, mu_strin, mu_strout));
MU_ASSERT (mu_imapio_create (&io, str, imapio_mode));
@@ -82,10 +76,10 @@ main (int argc, char **argv)
if (wc == 0)
break;
- mu_stream_printf (mu_strerr, "%lu\n", (unsigned long) wc);
+ mu_stream_printf (dstr, "%lu\n", (unsigned long) wc);
for (i = 0; i < wc; i++)
{
- mu_stream_printf (mu_strerr, "%d: '%s'\n", i, wv[i]);
+ mu_stream_printf (dstr, "%d: '%s'\n", i, wv[i]);
}
}
diff --git a/libmailutils/tests/mailcap.c b/libmailutils/tests/mailcap.c
index 17e95ce..229b91a 100644
--- a/libmailutils/tests/mailcap.c
+++ b/libmailutils/tests/mailcap.c
@@ -31,6 +31,13 @@ static void list_single_entry (mu_mailcap_entry_t ent);
static int list_field (char const *name, char const *value, void *data);
static int list_entry (mu_mailcap_entry_t ent, void *closure);
+static void
+cli_locus (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ int *pflag = opt->opt_ptr;
+ *pflag |= MU_MAILCAP_FLAG_LOCUS;
+}
+
/* usage:
mailcap [-f FILE]
List entries
@@ -46,39 +53,29 @@ main (int argc, char **argv)
int flags = MU_MAILCAP_FLAG_DEFAULT;
mu_mailcap_t mailcap;
char *file = NULL;
- char *arg;
-
- mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
-
- while (--argc && (arg = *++argv)[0] == '-')
- {
- if (strcmp (arg, "--") == 0)
- {
- argc--;
- argv++;
- break;
- }
- else if (strncmp (arg, "-f", 2) == 0)
- {
- if (arg[2])
- file = arg + 2;
- else if (--argc)
- file = *++argv;
- else
- {
- mu_error ("-f requires arguments");
- return 1;
- }
- }
- else if (strcmp (arg, "-l") == 0)
- flags |= MU_MAILCAP_FLAG_LOCUS;
- else
- {
- mu_error ("unrecognized option: %s", arg);
- return 1;
- }
- }
+ struct mu_option options[] = {
+ { "file", 'f', "NAME", MU_OPTION_DEFAULT,
+ "mailcap file name", mu_c_string, &file },
+ { "locus", 'l', NULL, MU_OPTION_DEFAULT,
+ "record location of each entry in the source file",
+ mu_c_incr, &flags, cli_locus },
+ MU_OPTION_END
+ };
+
+ mu_set_program_name (argv[0]);
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC,
+ "mailcap test: without arguments lists all mailcap entries, "
+ "with single argument, prints the first entry matching TYPE, "
+ "with two arguments prints FIELD from the first entry "
+ "matching TYPE.",
+ MU_CLI_OPTION_PROG_ARGS, "[TYPE [FIELD]]",
+ MU_CLI_OPTION_RETURN_ARGC, &argc,
+ MU_CLI_OPTION_RETURN_ARGV, &argv,
+ MU_CLI_OPTION_END);
+
MU_ASSERT (mu_mailcap_create (&mailcap));
mu_mailcap_set_error (mailcap, &mu_mailcap_default_error_closure);
if (flags != MU_MAILCAP_FLAG_DEFAULT)
diff --git a/libmailutils/tests/mcf.c b/libmailutils/tests/mcf.c
index e2085f9..406dda3 100644
--- a/libmailutils/tests/mcf.c
+++ b/libmailutils/tests/mcf.c
@@ -39,6 +39,13 @@ selector (mu_mailcap_entry_t entry, void *data)
static void list_entry (mu_mailcap_entry_t ent, unsigned long n);
+static void
+cli_locus (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ int *pflag = opt->opt_ptr;
+ *pflag |= MU_MAILCAP_FLAG_LOCUS;
+}
+
int
main (int argc, char **argv)
{
@@ -49,31 +56,29 @@ main (int argc, char **argv)
mu_mailcap_entry_t entry;
size_t n;
int flags = MU_MAILCAP_FLAG_DEFAULT;
- char *arg;
-
- mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
-
- while (--argc && (arg = *++argv)[0] == '-')
- {
- if (strcmp (arg, "--") == 0)
- {
- argc--;
- argv++;
- break;
- }
- else if (strcmp (arg, "-l") == 0)
- flags |= MU_MAILCAP_FLAG_LOCUS;
- else
- {
- mu_error ("unrecognized option: %s", arg);
- usage (mu_strerr);
- return 1;
- }
- }
+ struct mu_option options[] = {
+ { "locus", 'l', NULL, MU_OPTION_DEFAULT,
+ "record location of each entry in the source file",
+ mu_c_incr, &flags, cli_locus },
+ MU_OPTION_END
+ };
+
+ mu_set_program_name (argv[0]);
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC,
+ "extracts entries matching TYPE/SUBTYPE from the "
+ "given mailcap FILEs",
+ MU_CLI_OPTION_PROG_ARGS, "TYPE/SUBTYPE FILE...",
+ MU_CLI_OPTION_RETURN_ARGC, &argc,
+ MU_CLI_OPTION_RETURN_ARGV, &argv,
+ MU_CLI_OPTION_END);
+
if (argc < 2)
{
- usage (mu_strerr);
+ mu_error ("required arguments missing; try %s --help for assistance",
+ mu_program_name);
return 1;
}
type = argv[0];
diff --git a/libmailutils/tests/modmesg.c b/libmailutils/tests/modmesg.c
index f6dd280..a7adc67 100644
--- a/libmailutils/tests/modmesg.c
+++ b/libmailutils/tests/modmesg.c
@@ -23,16 +23,84 @@ This is a test message.\n\
oo\n\
";
+static void
+cli_a (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ mu_header_t *hdr = opt->opt_ptr;
+ char *p;
+
+ p = strchr (arg, ':');
+ if (!p)
+ {
+ mu_parseopt_error (po, "%s: invalid append header format", arg);
+ exit (po->po_exit_error);
+ }
+
+ *p++ = 0;
+ while (*p && mu_isspace (*p))
+ p++;
+ MU_ASSERT (mu_header_set_value (*hdr, arg, p, 1));
+}
+
+static void
+cli_t (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ mu_stream_t *stream = opt->opt_ptr;
+ mu_wordsplit_t ws;
+
+ if (mu_wordsplit (arg, &ws,
+ MU_WRDSF_NOSPLIT | MU_WRDSF_DEFFLAGS))
+ {
+ mu_parseopt_error (po, "mu_wordsplit: %s", mu_wordsplit_strerror (&ws));
+ exit (po->po_exit_error);
+ }
+ else
+ MU_ASSERT (mu_stream_write (*stream, ws.ws_wordv[0],
+ strlen (ws.ws_wordv[0]), NULL));
+ mu_wordsplit_free (&ws);
+}
+
+static void
+cli_l (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ mu_stream_t *stream = opt->opt_ptr;
+ mu_off_t off;
+ int whence = MU_SEEK_SET;
+ char *p;
+
+ errno = 0;
+ off = strtol (arg, &p, 10);
+ if (errno || *p)
+ {
+ mu_parseopt_error (po, "%s: invalid offset", arg);
+ exit (po->po_exit_error);
+ }
+
+ if (off < 0)
+ whence = MU_SEEK_END;
+ MU_ASSERT (mu_stream_seek (*stream, off, whence, NULL));
+}
+
int
main (int argc, char **argv)
{
- int i;
- char *p;
mu_message_t msg;
mu_stream_t stream = NULL;
mu_header_t hdr;
mu_body_t body;
-
+
+ struct mu_option options[] = {
+ { "add-header", 'a', "HDR:VAL", MU_OPTION_DEFAULT,
+ "add header to the message", mu_c_string, &hdr, cli_a },
+ { "seek", 'l', "OFF", MU_OPTION_DEFAULT,
+ "seek to the given position in message stream", mu_c_string, &stream,
+ cli_l },
+ { "text", 't', "TEXT", MU_OPTION_DEFAULT,
+ "write given text to the message stream in current position",
+ mu_c_string, &stream, cli_t },
+ MU_OPTION_END
+ };
+
mu_set_program_name (argv[0]);
mu_static_memory_stream_create (&stream, text, strlen (text));
@@ -42,59 +110,13 @@ main (int argc, char **argv)
MU_ASSERT (mu_message_get_body (msg, &body));
MU_ASSERT (mu_body_get_streamref (body, &stream));
MU_ASSERT (mu_stream_seek (stream, 0, MU_SEEK_END, NULL));
-
- for (i = 1; i < argc; i++)
- {
- if (strcmp (argv[i], "-h") == 0)
- {
- mu_printf ("usage: %s [-a HDR:VAL] [-t TEXT]\n", mu_program_name);
- return 0;
- }
-
- if (strcmp (argv[i], "-a") == 0)
- {
- i++;
- assert (argv[i] != NULL);
- p = strchr (argv[i], ':');
- assert (p != NULL);
- *p++ = 0;
- while (*p && mu_isspace (*p))
- p++;
- MU_ASSERT (mu_header_set_value (hdr, argv[i], p, 1));
- }
- else if (strcmp (argv[i], "-l") == 0)
- {
- mu_off_t off;
- int whence = MU_SEEK_SET;
-
- i++;
- assert (argv[i] != NULL);
- off = strtol (argv[i], &p, 10);
- assert (*p == 0);
- if (off < 0)
- whence = MU_SEEK_END;
- MU_ASSERT (mu_stream_seek (stream, off, whence, NULL));
- }
- else if (strcmp (argv[i], "-t") == 0)
- {
- mu_wordsplit_t ws;
- i++;
- assert (argv[i] != NULL);
-
- if (mu_wordsplit (argv[i], &ws,
- MU_WRDSF_NOSPLIT | MU_WRDSF_DEFFLAGS))
- {
- mu_error ("mu_wordsplit: %s", mu_wordsplit_strerror (&ws));
- exit (1);
- }
- else
- MU_ASSERT (mu_stream_write (stream, ws.ws_wordv[0],
- strlen (ws.ws_wordv[0]), NULL));
- mu_wordsplit_free (&ws);
- }
- else
- mu_error ("ignoring unknown argument %s", argv[i]);
- }
+
+ //parse arguments
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC, "test message modifications",
+ MU_CLI_OPTION_END);
+
mu_stream_unref (stream);
MU_ASSERT (mu_message_get_streamref (msg, &stream));
diff --git a/libmailutils/tests/msgset.c b/libmailutils/tests/msgset.c
index ea4469f..6c519f8 100644
--- a/libmailutils/tests/msgset.c
+++ b/libmailutils/tests/msgset.c
@@ -21,7 +21,7 @@
mu_msgset_format_t format = mu_msgset_fmt_imap;
static void
-parse_msgrange (char *arg, struct mu_msgrange *range)
+parse_msgrange (char const *arg, struct mu_msgrange *range)
{
size_t msgnum;
char *p;
@@ -98,88 +98,125 @@ print_last (mu_msgset_t msgset)
printf ("%zu\n", n);
}
-int
-main (int argc, char **argv)
+static void
+cli_msgset (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
- int i;
- char *msgset_string = NULL;
- mu_msgset_t msgset;
- void (*print) (mu_msgset_t) = print_all;
-
- mu_set_program_name (argv[0]);
- for (i = 1; i < argc; i++)
+ mu_msgset_t *msgset = opt->opt_ptr;
+ if (*msgset)
{
- char *arg = argv[i];
+ mu_parseopt_error (po, "message set already defined");
+ exit (po->po_exit_error);
+ }
+ *msgset = parse_msgset (arg);
+}
- if (strcmp (arg, "-h") == 0 || strcmp (arg, "-help") == 0)
- {
- mu_printf ("usage: %s [-mh] [-msgset=SET] [-add=X[:Y]] [-del=X[:Y]] "
- "[-addset=SET] [-delset=SET] [-first] [-last] ...\n",
- mu_program_name);
- return 0;
- }
- else if (strncmp (arg, "-msgset=", 8) == 0)
- msgset_string = arg + 8;
- else if (strcmp (arg, "-mh") == 0)
- format = mu_msgset_fmt_mh;
- else
- break;
+static void
+cli_mh (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ format = mu_msgset_fmt_mh;
+}
+
+static mu_msgset_t
+get_msgset (struct mu_option *opt)
+{
+ mu_msgset_t *msgset = opt->opt_ptr;
+ if (!*msgset)
+ {
+ *msgset = parse_msgset (NULL);
}
+ return *msgset;
+}
+
+static void
+cli_add (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ struct mu_msgrange range;
+ parse_msgrange (arg, &range);
+ MU_ASSERT (mu_msgset_add_range (get_msgset (opt), range.msg_beg,
+ range.msg_end, MU_MSGSET_NUM));
+}
+
+static void
+cli_sub (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ struct mu_msgrange range;
+ parse_msgrange (arg, &range);
+ MU_ASSERT (mu_msgset_sub_range (get_msgset (opt), range.msg_beg,
+ range.msg_end, MU_MSGSET_NUM));
+}
+
+static void
+cli_addset (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ mu_msgset_t tset = parse_msgset (arg);
+ MU_ASSERT (mu_msgset_add (get_msgset (opt), tset));
+ mu_msgset_free (tset);
+}
+
+static void
+cli_subset (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ mu_msgset_t tset = parse_msgset (arg);
+ MU_ASSERT (mu_msgset_sub (get_msgset (opt), tset));
+ mu_msgset_free (tset);
+}
- msgset = parse_msgset (msgset_string);
+static void
+cli_first (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ void (**print) (mu_msgset_t) = opt->opt_ptr;
+ *print = print_first;
+}
+
+static void
+cli_last (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
+{
+ void (**print) (mu_msgset_t) = opt->opt_ptr;
+ *print = print_last;
+}
+
+int
+main (int argc, char **argv)
+{
+ mu_msgset_t msgset = NULL;
+ void (*print) (mu_msgset_t) = print_all;
- for (; i < argc; i++)
+ struct mu_option options[] = {
+ { "mh", 0, NULL, MU_OPTION_DEFAULT,
+ "use MH message set format for output", mu_c_incr, NULL, cli_mh },
+ { "msgset", 0, "SET", MU_OPTION_DEFAULT,
+ "define message set", mu_c_string, &msgset, cli_msgset },
+ { "add", 0, "X[:Y]", MU_OPTION_DEFAULT,
+ "add range to message set", mu_c_string, &msgset, cli_add },
+ { "sub", 0, "X[:Y]", MU_OPTION_DEFAULT,
+ "subtract range from message set", mu_c_string, &msgset, cli_sub },
+ { "addset", 0, "SET", MU_OPTION_DEFAULT,
+ "add message set to message set", mu_c_string, &msgset, cli_addset },
+ { "subset", 0, "SET", MU_OPTION_DEFAULT,
+ "subtract message set from message set", mu_c_string, &msgset,
+ cli_subset },
+ { "first", 0, NULL, MU_OPTION_DEFAULT,
+ "print only first element from the resulting set",
+ mu_c_string, &print, cli_first },
+ { "last", 0, NULL, MU_OPTION_DEFAULT,
+ "print only last element from the resulting set",
+ mu_c_string, &print, cli_last },
+ MU_OPTION_END
+ };
+
+ mu_set_program_name (argv[0]);
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_SINGLE_DASH,
+ MU_CLI_OPTION_PROG_DOC, "message set parser test utility",
+ MU_CLI_OPTION_END);
+
+ if (!msgset)
{
- char *arg = argv[i];
- struct mu_msgrange range;
-
- if (strncmp (arg, "-add=", 5) == 0)
- {
- parse_msgrange (arg + 5, &range);
- MU_ASSERT (mu_msgset_add_range (msgset, range.msg_beg,
- range.msg_end, MU_MSGSET_NUM));
- }
- else if (strncmp (arg, "-sub=", 5) == 0)
- {
- parse_msgrange (arg + 5, &range);
- MU_ASSERT (mu_msgset_sub_range (msgset, range.msg_beg,
- range.msg_end, MU_MSGSET_NUM));
- }
- else if (strncmp (arg, "-addset=", 8) == 0)
- {
- mu_msgset_t tset = parse_msgset (arg + 8);
- if (!msgset)
- msgset = tset;
- else
- {
- MU_ASSERT (mu_msgset_add (msgset, tset));
- mu_msgset_free (tset);
- }
- }
- else if (strncmp (arg, "-subset=", 8) == 0)
- {
- mu_msgset_t tset = parse_msgset (arg + 8);
- if (!msgset)
- {
- mu_error ("no initial message set");
- exit (1);
- }
- else
- {
- MU_ASSERT (mu_msgset_sub (msgset, tset));
- mu_msgset_free (tset);
- }
- }
- else if (strcmp (arg, "-first") == 0)
- print = print_first;
- else if (strcmp (arg, "-last") == 0)
- print = print_last;
- else
- {
- mu_error ("unknown option %s", arg);
- return 1;
- }
+ mu_error ("nothing to do; try %s -help for assistance", mu_program_name);
+ exit (1);
}
+
print (msgset);
mu_msgset_free (msgset);
diff --git a/libmailutils/tests/scantime.c b/libmailutils/tests/scantime.c
index 8319dae..a9a01af 100644
--- a/libmailutils/tests/scantime.c
+++ b/libmailutils/tests/scantime.c
@@ -27,43 +27,30 @@
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
#include <mailutils/stdstream.h>
-
-void
-usage ()
-{
- mu_stream_printf (mu_strout, "usage: %s [-format=FMT] [-tz=TZ]\n",
- mu_program_name);
- exit (0);
-}
+#include <mailutils/cli.h>
int
main (int argc, char **argv)
{
- int rc, i;
+ int rc;
char *format = "%d-%b-%Y%? %H:%M:%S %z";
char *buf = NULL;
size_t size = 0;
size_t n;
int line;
-
- mu_set_program_name (argv[0]);
-
- mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
- for (i = 1; i < argc; i++)
- {
- char *opt = argv[i];
+ struct mu_option options[] = {
+ { "format", 0, "FMT", MU_OPTION_DEFAULT,
+ "set scanning format", mu_c_string, &format },
+ MU_OPTION_END
+ };
- if (strncmp (opt, "-format=", 8) == 0)
- format = opt + 8;
- else if (strcmp (opt, "-h") == 0)
- usage ();
- else
- {
- mu_error ("%s: unrecognized argument", opt);
- exit (1);
- }
- }
+ mu_set_program_name (argv[0]);
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_SINGLE_DASH,
+ MU_CLI_OPTION_PROG_DOC, "mu_scan_datetime tester",
+ MU_CLI_OPTION_END);
line = 0;
while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0)
diff --git a/libmailutils/tests/stream-getdelim.c b/libmailutils/tests/stream-getdelim.c
index 075afa9..2a864bc 100644
--- a/libmailutils/tests/stream-getdelim.c
+++ b/libmailutils/tests/stream-getdelim.c
@@ -3,9 +3,9 @@ NAME
stream-getdelim - test the mu_stream_getdelim function.
DESCRIPTION
- This implements a simple memory-based stream implementation and
- tests mu_stream_getdelim on a predefined stream content with various
- combinations of buffering type and buffer size settings.
+ This implements a simple memory-based stream and tests the
+ mu_stream_getdelim function on a predefined stream content with
+ various combinations of buffering type and buffer size settings.
On success, returns 0. On error, prints diagnostics on stderr and
exits with a non-0 code or aborts.
diff --git a/libmailutils/tests/strerr.at b/libmailutils/tests/strerr.at
index 018be60..946ad2f 100644
--- a/libmailutils/tests/strerr.at
+++ b/libmailutils/tests/strerr.at
@@ -15,22 +15,18 @@
# along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
AT_SETUP([strerr])
-AT_KEYWORDS([stdstream])
+AT_KEYWORDS([stdstream strerr])
-AT_CHECK([
-strout -err now is the time for all good men
-],
+AT_DATA([experr],
+[The fact that an opinion has been widely held is no evidence that it is not
+utterly absurd; indeed, in view of the silliness of the majority of mankind,
+a widespread belief is more often likely to be foolish than sensible.
+])
+
+AT_CHECK([strout -err < experr],
[0],
[],
-[strout: now
-strout: is
-strout: the
-strout: time
-strout: for
-strout: all
-strout: good
-strout: men
-])
+[experr])
AT_CLEANUP
diff --git a/libmailutils/tests/strftime.c b/libmailutils/tests/strftime.c
index 6ac24f5..e4bc7e3 100644
--- a/libmailutils/tests/strftime.c
+++ b/libmailutils/tests/strftime.c
@@ -27,60 +27,50 @@
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
#include <mailutils/stdstream.h>
-
-void
-usage ()
-{
- mu_stream_printf (mu_strout, "usage: %s [-format=FMT] [-tz=TZ]\n",
- mu_program_name);
- exit (0);
-}
+#include <mailutils/cli.h>
int
main (int argc, char **argv)
{
- int rc, i;
+ int rc;
char *format = "%c";
char *buf = NULL;
size_t size = 0;
size_t n;
struct mu_timezone tz, *tzp = NULL;
-
- mu_set_program_name (argv[0]);
+ char *tzstr = NULL;
+
+ struct mu_option options[] = {
+ { "format", 0, "FMT", MU_OPTION_DEFAULT,
+ "set scanning format", mu_c_string, &format },
+ { "tz", 0, "UTCOFF", MU_OPTION_DEFAULT,
+ "set time zone", mu_c_string, &tzstr },
+ MU_OPTION_END
+ };
mu_set_program_name (argv[0]);
- mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_SINGLE_DASH,
+ MU_CLI_OPTION_PROG_DOC, "mu_c_streamftime tester",
+ MU_CLI_OPTION_END);
memset (&tz, 0, sizeof tz);
- for (i = 1; i < argc; i++)
+ if (tzstr)
{
- char *opt = argv[i];
-
- if (strncmp (opt, "-format=", 8) == 0)
- format = opt + 8;
- else if (strncmp (opt, "-tz=", 4) == 0)
+ int sign;
+ int n = atoi (tzstr);
+ if (n < 0)
{
- int sign;
- int n = atoi (opt + 4);
- if (n < 0)
- {
- sign = -1;
- n = - n;
- }
- else
- sign = 1;
- tz.utc_offset = sign * ((n / 100 * 60) + n % 100) * 60;
- tzp = &tz;
+ sign = -1;
+ n = - n;
}
- else if (strcmp (opt, "-h") == 0)
- usage ();
else
- {
- mu_error ("%s: unrecognized argument", opt);
- exit (1);
- }
+ sign = 1;
+ tz.utc_offset = sign * ((n / 100 * 60) + n % 100) * 60;
+ tzp = &tz;
}
-
+
while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0)
{
char *p;
diff --git a/libmailutils/tests/strin.c b/libmailutils/tests/strin.c
index 0995306..f9b4543 100644
--- a/libmailutils/tests/strin.c
+++ b/libmailutils/tests/strin.c
@@ -1,18 +1,38 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
- Copyright (C) 2011-2020 Free Software Foundation, Inc.
+/*
+NAME
+ strin - test whether mu_strin is functioning.
- 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.
+DESCRIPTION
+ This test program reads data from mu_strin and prints it on stdout
+ using stdio function call.
- 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.
+ The mu_strin object, as well as the other two standard streams, springs
+ into existence the first time it is used by any function of the stream
+ family. Therefore, care is taken not to call any mailutils I/O function
+ either directly or indirectly prior to the first read from mu_strin.
+
+OPTIONS
+ -noecho
+ If mu_strin is attached to a tty, this option disables echoing of
+ the data read.
+
+LICENSE
+ GNU Mailutils -- a suite of utilities for electronic mail
+ Copyright (C) 2011-2020 Free Software Foundation, Inc.
- You should have received a copy of the GNU General Public License
- along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
+ 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>
@@ -31,8 +51,8 @@ main (int argc, char **argv)
int i, rc;
int echo_state = 0;
size_t n;
- char buf[80];
-
+ int ex = 0;
+
for (i = 1; i < argc; i++)
{
char *arg = argv[i];
@@ -53,12 +73,28 @@ main (int argc, char **argv)
}
}
- while ((rc = mu_stream_read (mu_strin, buf, sizeof (buf), &n) == 0) &&
- n > 0)
- fwrite (buf, 1, n, stdout);
-
+ while (1)
+ {
+ char c;
+ if ((rc = mu_stream_read (mu_strin, &c, 1, &n)) != 0)
+ {
+ fprintf (stderr, "mu_stream_read: %s\n", mu_strerror (rc));
+ ex = 1;
+ break;
+ }
+ if (n == 0)
+ break;
+ if (n != 1)
+ {
+ fprintf (stderr, "read %zu bytes?\n", n);
+ ex = 1;
+ break;
+ }
+ fputc (c, stdout);
+ }
+
if (echo_state)
MU_ASSERT (mu_stream_ioctl (mu_strin, MU_IOCTL_ECHO, MU_IOCTL_OP_SET,
&echo_state));
- return 0;
+ return ex;
}
diff --git a/libmailutils/tests/strout.at b/libmailutils/tests/strout.at
index 0db7c95..77289d1 100644
--- a/libmailutils/tests/strout.at
+++ b/libmailutils/tests/strout.at
@@ -15,22 +15,18 @@
# along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
AT_SETUP([strout])
-AT_KEYWORDS([stdstream])
+AT_KEYWORDS([stdstream strout])
-AT_CHECK([
-strout now is the time for all good men
-],
-[0],
-[now
-is
-the
-time
-for
-all
-good
-men
+AT_DATA([expout],
+[The fact that an opinion has been widely held is no evidence that it is not
+utterly absurd; indeed, in view of the silliness of the majority of mankind,
+a widespread belief is more often likely to be foolish than sensible.
])
+AT_CHECK([strout < expout],
+[0],
+[expout])
+
AT_CLEANUP
diff --git a/libmailutils/tests/strout.c b/libmailutils/tests/strout.c
index 8a36385..5b1bed9 100644
--- a/libmailutils/tests/strout.c
+++ b/libmailutils/tests/strout.c
@@ -1,18 +1,42 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
- Copyright (C) 2011-2020 Free Software Foundation, Inc.
+/*
+NAME
+ strout - test whether mu_strout or mu_strerr is functioning.
- 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.
+DESCRIPTION
+ This test program reads data byte by byte from stdin using the stdio
+ input functions and sends them to mu_strout or mu_strerr using the
+ mu_stream_write function.
- 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.
+ Both mu_strout and mu_strerr spring into existence the first time they
+ are used by any function of the stream family. Therefore, care is
+ taken not to call any mailutils I/O function either directly or
+ indirectly prior to the first write to the stream being tested.
- You should have received a copy of the GNU General Public License
- along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
+OPTIONS
+ -err
+ Write to mu_strerr.
+
+ -reset
+ Call mu_stdstream_setup with the appropriate MU_STDSTREAM_RESET_
+ flag explicitly.
+
+LICENSE
+ GNU Mailutils -- a suite of utilities for electronic mail
+ Copyright (C) 2011-2020 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>
@@ -23,21 +47,14 @@
#include <mailutils/stream.h>
#include <mailutils/stdstream.h>
#include <mailutils/diag.h>
+#include <mailutils/errno.h>
int
main (int argc, char **argv)
{
mu_stream_t str = mu_strout;
int i;
-
- if (argc == 1)
- {
- fprintf (stderr, "usage: %s: word|option [word|option...]\n", argv[0]);
- fprintf (stderr, "options are: -out, -err, -reset\n");
- return 1;
- }
-
- mu_set_program_name (argv[0]);
+ char c;
for (i = 1; i < argc; i++)
{
@@ -67,8 +84,22 @@ main (int argc, char **argv)
return 1;
}
}
- else
- mu_stream_printf (str, "%s\n", arg);
+ }
+
+ while ((c = getchar ()) != EOF)
+ {
+ size_t n;
+ int rc = mu_stream_write (str, &c, 1, &n);
+ if (rc)
+ {
+ fprintf (stderr, "mu_stream_write: %s", mu_strerror (rc));
+ return 1;
+ }
+ if (n != 1)
+ {
+ fprintf (stderr, "wrote %zu bytes?\n", n);
+ return 1;
+ }
}
return 0;
diff --git a/libmailutils/tests/t-streamshift.c b/libmailutils/tests/t-streamshift.c
index 2fae0fa..5949163 100644
--- a/libmailutils/tests/t-streamshift.c
+++ b/libmailutils/tests/t-streamshift.c
@@ -14,22 +14,22 @@ DESCRIPTION
repeated pattern of 256 characters.
OPTIONS
- -a
+ -a, --alnum
Fill the pattern with alphanumeric characters only.
- -b SIZE
+ -b, --bufsize=SIZE
Set buffer size for the shift operation. This option has
effect only if the data are shifted in place, i.e. if
mailfromd is compiled with mailutils version 3.9.90 or
later.
- -d
+ -d, --dump
Dump the resulting stream on stdout.
- -i FILE
+ -i, --init-file=FILE
Initialize temporary stream from FILE.
- -s SIZE
+ -s, --init-size=SIZE
Set initial size for the temporary stream. Default is
four times the MU_STREAM_DEFBUFSIZ constant (32768).
@@ -63,203 +63,219 @@ LICENSE
#include <mailutils/mailutils.h>
static unsigned long
-strnum(char const *str)
+strnum (char const *str)
{
- char *p;
- unsigned long n;
- errno = 0;
- n = strtol(str, &p, 10);
- assert(*p == 0 && errno == 0);
- return n;
+ char *p;
+ unsigned long n;
+ errno = 0;
+ n = strtol (str, &p, 10);
+ assert (*p == 0 && errno == 0);
+ return n;
}
char pattern[256];
static void
-gen_pattern(int alnum)
+gen_pattern (int alnum)
{
- int i, c;
+ int i, c;
- for (i = c = 0; i < sizeof(pattern); i++, c++) {
- if (alnum) {
- while (!mu_isalnum(c))
- c = (c + 1) % sizeof(pattern);
- }
- pattern[i] = c;
+ for (i = c = 0; i < sizeof(pattern); i++, c++)
+ {
+ if (alnum)
+ {
+ while (!mu_isalnum (c))
+ c = (c + 1) % sizeof (pattern);
}
+ pattern[i] = c;
+ }
}
static void
-stream_fill(mu_stream_t str, size_t size)
+stream_fill (mu_stream_t str, size_t size)
{
- while (size) {
- size_t n = sizeof(pattern);
- if (n > size)
- n = size;
- MU_ASSERT(mu_stream_write(str, pattern, n, NULL));
- size -= n;
- }
+ while (size)
+ {
+ size_t n = sizeof (pattern);
+ if (n > size)
+ n = size;
+ MU_ASSERT (mu_stream_write (str, pattern, n, NULL));
+ size -= n;
+ }
}
static void
-stream_verify_part(mu_stream_t str, int patstart, mu_off_t off, mu_off_t size)
+stream_verify_part (mu_stream_t str, int patstart, mu_off_t off, mu_off_t size)
{
- char buffer[sizeof(pattern)];
- char pat[sizeof(pattern)];
- int i;
-
- for (i = 0; i < sizeof(pat); i++) {
- pat[i] = pattern[patstart];
- patstart = (patstart + 1) % sizeof(pattern);
- }
+ char buffer[sizeof(pattern)];
+ char pat[sizeof(pattern)];
+ int i;
+
+ for (i = 0; i < sizeof(pat); i++)
+ {
+ pat[i] = pattern[patstart];
+ patstart = (patstart + 1) % sizeof (pattern);
+ }
- MU_ASSERT(mu_stream_seek(str, off, MU_SEEK_SET, NULL));
- while (size) {
- size_t n = sizeof(buffer);
- if (n > size)
- n = size;
- MU_ASSERT(mu_stream_read(str, buffer, n, NULL));
- if (memcmp(buffer, pat, n)) {
- mu_off_t pos;
- MU_ASSERT(mu_stream_seek(str, 0, MU_SEEK_CUR, &pos));
- fprintf(stderr, "%lu: chunk differs\n", pos - n);
- exit(1);
- }
- size -= n;
+ MU_ASSERT (mu_stream_seek (str, off, MU_SEEK_SET, NULL));
+ while (size)
+ {
+ size_t n = sizeof (buffer);
+ if (n > size)
+ n = size;
+ MU_ASSERT (mu_stream_read (str, buffer, n, NULL));
+ if (memcmp (buffer, pat, n))
+ {
+ mu_off_t pos;
+ MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_CUR, &pos));
+ fprintf (stderr, "%lu: chunk differs\n", pos - n);
+ exit (1);
}
+ size -= n;
+ }
}
static void
-stream_verify(mu_stream_t str, mu_off_t isize, mu_off_t a, mu_off_t b)
+stream_verify (mu_stream_t str, mu_off_t isize, mu_off_t a, mu_off_t b)
{
- mu_off_t size;
+ mu_off_t size;
- MU_ASSERT(mu_stream_seek(str, 0, MU_SEEK_END, &size));
- if (size != isize + a - b) {
- fprintf(stderr,
- "actual and expected sizes differ: %lu != %lu\n",
- (unsigned long) size,
- (unsigned long) (isize + a - b));
- exit(1);
- }
+ MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_END, &size));
+ if (size != isize + a - b)
+ {
+ fprintf (stderr,
+ "actual and expected sizes differ: %lu != %lu\n",
+ (unsigned long) size,
+ (unsigned long) (isize + a - b));
+ exit (1);
+ }
- stream_verify_part(str, 0, 0, a < b ? a : b);
- stream_verify_part(str, b % sizeof(pattern), a, size);
+ stream_verify_part (str, 0, 0, a < b ? a : b);
+ stream_verify_part (str, b % sizeof (pattern), a, size);
}
static void
-file_verify_part(mu_stream_t str, mu_stream_t orig, mu_off_t str_off,
- mu_off_t orig_off, mu_off_t size)
+file_verify_part (mu_stream_t str, mu_stream_t orig, mu_off_t str_off,
+ mu_off_t orig_off, mu_off_t size)
{
- char str_buf[sizeof(pattern)];
- char orig_buf[sizeof(pattern)];
+ char str_buf[sizeof (pattern)];
+ char orig_buf[sizeof (pattern)];
- MU_ASSERT(mu_stream_seek(str, str_off, MU_SEEK_SET, NULL));
- MU_ASSERT(mu_stream_seek(orig, orig_off, MU_SEEK_SET, NULL));
- while (size) {
- size_t n = sizeof(str_buf);
- if (n > size)
- n = size;
- MU_ASSERT(mu_stream_read(str, str_buf, n, NULL));
- MU_ASSERT(mu_stream_read(orig, orig_buf, n, NULL));
- if (memcmp(str_buf, orig_buf, n)) {
- mu_off_t pos;
- MU_ASSERT(mu_stream_seek(str, 0, MU_SEEK_CUR, &pos));
- fprintf(stderr, "%lu: chunk differs\n", pos - n);
- exit(1);
- }
- size -= n;
+ MU_ASSERT (mu_stream_seek (str, str_off, MU_SEEK_SET, NULL));
+ MU_ASSERT (mu_stream_seek (orig, orig_off, MU_SEEK_SET, NULL));
+ while (size)
+ {
+ size_t n = sizeof (str_buf);
+ if (n > size)
+ n = size;
+ MU_ASSERT (mu_stream_read (str, str_buf, n, NULL));
+ MU_ASSERT (mu_stream_read (orig, orig_buf, n, NULL));
+ if (memcmp (str_buf, orig_buf, n))
+ {
+ mu_off_t pos;
+ MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_CUR, &pos));
+ fprintf (stderr, "%lu: chunk differs\n", pos - n);
+ exit (1);
}
+ size -= n;
+ }
}
static void
-file_verify(mu_stream_t str, mu_stream_t orig, mu_off_t isize,
- mu_off_t a, mu_off_t b)
+file_verify (mu_stream_t str, mu_stream_t orig, mu_off_t isize,
+ mu_off_t a, mu_off_t b)
{
- mu_off_t size;
+ mu_off_t size;
- MU_ASSERT(mu_stream_seek(str, 0, MU_SEEK_END, &size));
- if (size != isize + a - b) {
- fprintf(stderr,
- "actual and expected sizes differ: %lu != %lu\n",
- (unsigned long) size,
- (unsigned long) (isize + a - b));
- exit(1);
- }
- file_verify_part(str, orig, 0, 0, a < b ? a : b);
- file_verify_part(str, orig, a, b, size - a);
+ MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_END, &size));
+ if (size != isize + a - b)
+ {
+ fprintf (stderr,
+ "actual and expected sizes differ: %lu != %lu\n",
+ (unsigned long) size,
+ (unsigned long) (isize + a - b));
+ exit (1);
+ }
+ file_verify_part (str, orig, 0, 0, a < b ? a : b);
+ file_verify_part (str, orig, a, b, size - a);
}
int
-main(int argc, char **argv)
+main (int argc, char **argv)
{
- int ascii_mode = 0;
- size_t bs = 0;
- mu_off_t from_off;
- mu_off_t to_off;
- mu_off_t init_size = 0;
- char *init_file = NULL;
- int dump_opt = 0;
- mu_stream_t temp;
- mu_stream_t str;
- int c;
+ int ascii_mode = 0;
+ size_t bs = 0;
+ mu_off_t from_off;
+ mu_off_t to_off;
+ mu_off_t init_size = 0;
+ char *init_file = NULL;
+ int dump_opt = 0;
+ mu_stream_t temp;
+ mu_stream_t str;
- while ((c = getopt(argc, argv, "ab:di:s:")) != EOF) {
- switch (c) {
- case 'a':
- ascii_mode = 1;
- break;
- case 'b':
- bs = strnum(optarg);
- break;
- case 'd':
- dump_opt = 1;
- break;
- case 'i':
- init_file = optarg;
- break;
- case 's':
- init_size = strnum(optarg);
- break;
- default:
- exit(2);
- }
- }
+ struct mu_option options[] = {
+ { "alnum", 'a', NULL, MU_OPTION_DEFAULT,
+ "fill the pattern with alphanumeric characters only",
+ mu_c_incr, &ascii_mode },
+ { "bufsize", 'b', "N", MU_OPTION_DEFAULT,
+ "size of the buffer for shift operations", mu_c_size, &bs },
+ { "dump", 'd', NULL, MU_OPTION_DEFAULT,
+ "dump the resulting stream on the stdout at the end of the run",
+ mu_c_incr, &dump_opt },
+ { "init-file", 'i', "FILE", MU_OPTION_DEFAULT,
+ "initialize source stream from FILE",
+ mu_c_string, &init_file },
+ { "init-size", 's', "N", MU_OPTION_DEFAULT,
+ "initial size of the stream", mu_c_off, &init_size },
+ MU_OPTION_END
+ };
- if (argc != optind + 2) {
- fprintf(stderr,
- "usage: %s [-ad] [-b SIZE] [-i FILE] [-s SIZE] "
- "-f OFF -t OFF\n",
- argv[0]);
- exit(2);
- }
+ mu_set_program_name (argv[0]);
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC, "mu_stream_shift test",
+ MU_CLI_OPTION_PROG_ARGS, "FROM_OFF TO_OFF",
+ MU_CLI_OPTION_EX_USAGE, 2,
+ MU_CLI_OPTION_RETURN_ARGC, &argc,
+ MU_CLI_OPTION_RETURN_ARGV, &argv,
+ MU_CLI_OPTION_END);
- from_off = strnum(argv[optind]);
- to_off = strnum(argv[optind+1]);
-
- MU_ASSERT(mu_temp_file_stream_create(&temp, NULL, 0));
- if (init_file) {
- MU_ASSERT(mu_file_stream_create(&str, init_file,
- MU_STREAM_READ));
- MU_ASSERT(mu_stream_copy(temp, str, 0, NULL));
- if (init_size)
- MU_ASSERT(mu_stream_truncate(temp, init_size));
- } else {
- gen_pattern(ascii_mode);
- if (!init_size)
- init_size = 4 * MU_STREAM_DEFBUFSIZ;
- stream_fill(temp, init_size);
- }
- MU_ASSERT(mu_stream_seek(temp, 0, MU_SEEK_CUR, &init_size));
+ if (argc != 2)
+ {
+ mu_error ("expected exactly two arguments; try %s --help for assistance",
+ mu_program_name);
+ exit(2);
+ }
- MU_ASSERT(mu_stream_shift(temp, to_off, from_off, bs));
- if (init_file)
- file_verify(temp, str, init_size, to_off, from_off);
- else
- stream_verify(temp, init_size, to_off, from_off);
- if (dump_opt) {
- MU_ASSERT(mu_stream_seek(temp, 0, MU_SEEK_SET, NULL));
- MU_ASSERT(mu_stream_copy(mu_strout, temp, 0, NULL));
- }
- return 0;
+ from_off = strnum (argv[0]);
+ to_off = strnum (argv[1]);
+
+ MU_ASSERT (mu_temp_file_stream_create (&temp, NULL, 0));
+ if (init_file)
+ {
+ MU_ASSERT (mu_file_stream_create (&str, init_file, MU_STREAM_READ));
+ MU_ASSERT (mu_stream_copy (temp, str, 0, NULL));
+ if (init_size)
+ MU_ASSERT (mu_stream_truncate (temp, init_size));
+ }
+ else
+ {
+ gen_pattern (ascii_mode);
+ if (!init_size)
+ init_size = 4 * MU_STREAM_DEFBUFSIZ;
+ stream_fill (temp, init_size);
+ }
+ MU_ASSERT (mu_stream_seek (temp, 0, MU_SEEK_CUR, &init_size));
+
+ MU_ASSERT (mu_stream_shift (temp, to_off, from_off, bs));
+ if (init_file)
+ file_verify (temp, str, init_size, to_off, from_off);
+ else
+ stream_verify (temp, init_size, to_off, from_off);
+ if (dump_opt)
+ {
+ MU_ASSERT (mu_stream_seek (temp, 0, MU_SEEK_SET, NULL));
+ MU_ASSERT (mu_stream_copy (mu_strout, temp, 0, NULL));
+ }
+ return 0;
}
diff --git a/libmailutils/tests/t0-stream.c b/libmailutils/tests/t0-stream.c
index 8de869a..393ac1d 100644
--- a/libmailutils/tests/t0-stream.c
+++ b/libmailutils/tests/t0-stream.c
@@ -33,7 +33,7 @@
char mem[1024];
int
-main(int argc, char **argv)
+main (int argc, char **argv)
{
mu_stream_t str;
char buf[128];
diff --git a/libmailutils/tests/temp_stream.c b/libmailutils/tests/temp_stream.c
index e509e30..7564676 100644
--- a/libmailutils/tests/temp_stream.c
+++ b/libmailutils/tests/temp_stream.c
@@ -1,5 +1,53 @@
/*
- */
+NAME
+ temp_stream - test for the temp_stream implementation
+
+SYNOPSIS
+ temp_stream
+
+DESCRIPTION
+ A temporary streams works as a memory stream until its size reaches
+ a preconfigured threshold value. Then, it writes all data collected
+ so far to a temporary file and from then on operates as a file stream.
+
+ This test program creates a temporary stream with a threshold of MAXMEM
+ bytes. It writes exactly MAXMEM bytes to the stream, reads that data
+ back to ensure they are the same and verifies that the stream has not
+ yet switched to the temporary file mode.
+
+ Then, another byte is written, which should trigger conversion to
+ temporary file and the stream mode is tested again.
+
+ Finally, MAXMEM-1 more bytes are written, read back and compared.
+
+ The exit status is 0 if all the above passed as expected, and 1
+ otherwise. If any unhandled error occured, the program aborts.
+
+ To test the current stream mode, the MU_IOCTL_FD ioctl is used. If
+ the stream responds successfully to the MU_IOCTL_FD_GET_BORROW ioctl
+ operation, then it is a file stream.
+
+SEE ALSO
+ libmailutils/stream/test_stream.c - implementation of the temporary
+ stream.
+
+LICENSE
+ This file is part of GNU mailutils.
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ This program 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.
+
+ This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+*/
#include <mailutils/mailutils.h>
#include <mailutils/sys/stream.h>
#include <mailutils/sys/temp_stream.h>
diff --git a/libmailutils/tests/tempfile.c b/libmailutils/tests/tempfile.c
index 2ff4731..63faa99 100644
--- a/libmailutils/tests/tempfile.c
+++ b/libmailutils/tests/tempfile.c
@@ -25,75 +25,56 @@
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/stream.h>
+#include <mailutils/stdstream.h>
+#include <mailutils/cli.h>
char *progname;
-void
-usage (FILE *fp, int code)
-{
- fprintf (fp,
- "usage: %s [-tmpdir=DIR] [-suffix=SUF] [-dry-run | -unlink] { file | dir }\n",
- progname);
- exit (code);
-}
-
int
main (int argc, char **argv)
{
- struct mu_tempfile_hints hints, *phints;
+ struct mu_tempfile_hints hints;
int flags = 0;
int fd;
- int *pfd = &fd;
char *filename;
- char **pname = &filename;
char *infile = NULL;
+ int yes = 1;
int verify = 0;
int verbose = 0;
- int yes = 1;
-
- progname = argv[0];
+ int dry_run = 0;
+ int unlink_opt = 0;
+ int mkdir_opt = 0;
- if (argc == 1)
- usage (stdout, 0);
+ struct mu_option options[] = {
+ { "tmpdir", 'D', "DIRNAME", MU_OPTION_DEFAULT,
+ "set the temporary directory to use", mu_c_string, &hints.tmpdir },
+ { "suffix", 's', "STRING", MU_OPTION_DEFAULT,
+ "set file name suffix", mu_c_string, &hints.suffix },
+ { "dry-run", 'n', NULL, MU_OPTION_DEFAULT,
+ "dry run mode", mu_c_incr, &dry_run },
+ { "unlink", 'u', NULL, MU_OPTION_DEFAULT,
+ "unlink the file", mu_c_incr, &unlink_opt },
+ { "infile", 'f', "FILE", MU_OPTION_DEFAULT,
+ "copy the content of the FILE to the temporary file",
+ mu_c_string, &infile },
+ { "verify", 'V', NULL, MU_OPTION_DEFAULT,
+ "dump the stream?", mu_c_incr, &verify },
+ { "verbose", 'v', NULL, MU_OPTION_DEFAULT,
+ "verbose mode", mu_c_incr, &verbose },
+ { "dir", 'm', NULL, MU_OPTION_DEFAULT,
+ "create temporary directory, instead of file",
+ mu_c_incr, &mkdir_opt },
+ MU_OPTION_END
+ };
+
+ memset (&hints, 0, sizeof (hints));
+ mu_set_program_name (argv[0]);
+ mu_cli_simple (argc, argv,
+ MU_CLI_OPTION_OPTIONS, options,
+ MU_CLI_OPTION_PROG_DOC, "test temporary file creation",
+ MU_CLI_OPTION_END);
- while (--argc)
- {
- char *arg = *++argv;
-
- if (strncmp (arg, "-tmpdir=", 8) == 0)
- {
- hints.tmpdir = arg + 8;
- flags |= MU_TEMPFILE_TMPDIR;
- }
- else if (strncmp (arg, "-suffix=", 8) == 0)
- {
- hints.suffix = arg + 8;
- flags |= MU_TEMPFILE_SUFFIX;
- }
- else if (strcmp (arg, "-dry-run") == 0)
- pfd = NULL;
- else if (strcmp (arg, "-unlink") == 0)
- pname = NULL;
- else if (strncmp (arg, "-infile=", 8) == 0)
- infile = arg + 8;
- else if (strncmp (arg, "-verify", 7) == 0)
- verify = 1;
- else if (strncmp (arg, "-verbose", 8) == 0)
- verbose = 1;
- else
- break;
- }
-
- if (argv[0] == NULL)
- usage (stderr, 1);
- if (strcmp (argv[0], "file") == 0)
- /* nothing */;
- else if (strcmp (argv[0], "dir") == 0)
- flags |= MU_TEMPFILE_MKDIR;
- else
- usage (stderr, 1);
-
- if (pname == NULL && pfd == NULL)
+ if (dry_run && unlink_opt)
{
mu_error ("both -unlink and -dry-run given");
exit (1);
@@ -103,30 +84,37 @@ main (int argc, char **argv)
{
if (flags & MU_TEMPFILE_MKDIR)
{
- mu_error ("-infile is useless with dir");
+ mu_error ("--infile is useless with --mkdir");
exit (1);
}
- else if (pfd == NULL)
+ else if (dry_run)
{
- mu_error ("-infile is useless with -dry-run");
+ mu_error ("--infile is useless with --dry-run");
exit (1);
}
}
- if (verify && pfd == NULL)
+ if (verify && dry_run)
{
- mu_error ("-verify is useless with -dry-run");
+ mu_error ("--verify is useless with --dry-run");
exit (1);
}
-
- phints = flags ? &hints : NULL;
-
- MU_ASSERT (mu_tempfile (phints, flags, pfd, pname));
+
+ if (hints.tmpdir)
+ flags |= MU_TEMPFILE_TMPDIR;
+ if (hints.suffix)
+ flags |= MU_TEMPFILE_SUFFIX;
+ if (mkdir_opt)
+ flags |= MU_TEMPFILE_MKDIR;
+
+ MU_ASSERT (mu_tempfile (flags ? &hints : NULL, flags,
+ dry_run ? NULL : &fd,
+ unlink_opt ? NULL : &filename));
if (filename)
- printf ("created file name %s\n", filename);
+ mu_printf ("created file name %s\n", filename);
- if (!pfd)
+ if (dry_run)
return 0;
if (infile)
@@ -143,24 +131,22 @@ main (int argc, char **argv)
mu_stream_ioctl (out, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
MU_ASSERT (mu_stream_copy (out, in, 0, &size));
if (verbose)
- printf ("copied %lu bytes to the temporary\n", (unsigned long) size);
+ mu_printf ("copied %lu bytes to the temporary\n", (unsigned long) size);
mu_stream_unref (out);
mu_stream_unref (in);
}
if (verify)
{
- mu_stream_t in, out;
+ mu_stream_t in;
mu_off_t size;
- MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0));
MU_ASSERT (mu_fd_stream_create (&in, filename, fd,
MU_STREAM_READ|MU_STREAM_SEEK));
mu_stream_ioctl (in, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
- MU_ASSERT (mu_stream_copy (out, in, 0, &size));
+ MU_ASSERT (mu_stream_copy (mu_strout, in, 0, &size));
if (verbose)
- printf ("dumped %lu bytes\n", (unsigned long) size);
- mu_stream_unref (out);
+ mu_printf ("dumped %lu bytes\n", (unsigned long) size);
mu_stream_unref (in);
}

Return to:

Send suggestions and report system problems to the System administrator.