Commit 43a6e030 43a6e0300e65f797320beafb117202000499c0e8 by Alain Magloire

* mailbox2/debug.c: Remove mu_debug_printv() and only keep

	mu_debug_print().
	* mailbox2/sdebug.c (mu_debug_stream_create): New function
	replace mu_debug_stdio_create().
	* mailbox2/fdstream.c (_fds_desroy): Do not close the stream
	when distroying, it should have been done explicitely with
	stream_close ();
	* mailbox2/fstream.c (_fs_desroy): Do not close the stream
	when distroying, it should have been done explicitely with
	stream_close ();
	* mailbox2/mapstream.c (_map_desroy): Do not close the stream
	when distroying, it should have been done explicitely with
	stream_close ();
	* mailbox2/memstream.c (_memory_desroy): Do not close the stream
	when distroying, it should have been done explicitely with
	stream_close ();
	* mailbox2/tcpstream.c (_tcp_desroy): Do not close the stream
	when distroying, it should have been done explicitely with
	stream_close ();
1 parent ff32732d
......@@ -63,20 +63,9 @@ mu_debug_get_level (mu_debug_t debug, unsigned int *plevel)
}
int
mu_debug_print (mu_debug_t debug, unsigned int level, const char *fmt, ...)
mu_debug_print (mu_debug_t debug, unsigned int level, const char *msg)
{
va_list ap;
va_start (ap, fmt);
mu_debug_printv (debug, level, fmt, ap);
va_end (ap);
return 0;
}
int
mu_debug_printv (mu_debug_t debug, unsigned int level, const char *fmt,
va_list ap)
{
if (debug == NULL || debug->vtable == NULL || debug->vtable->printv == NULL)
if (debug == NULL || debug->vtable == NULL || debug->vtable->print == NULL)
return MU_ERROR_NOT_SUPPORTED;
return debug->vtable->printv (debug, level, fmt, ap);
return debug->vtable->print (debug, level, msg);
}
......
......@@ -52,8 +52,6 @@ _fds_destroy (stream_t *pstream)
struct _fds *fds = (struct _fds *)*pstream;
if (mu_refcount_dec (fds->refcount) == 0)
{
if (fds->fd != -1)
close (fds->fd);
mu_refcount_destroy (&fds->refcount);
free (fds);
}
......
......@@ -47,8 +47,6 @@ _fs_destroy (stream_t *pstream)
struct _fs *fs = (struct _fs *)*pstream;
if (mu_refcount_dec (fs->refcount) == 0)
{
if (fs->file)
fclose (fs->file);
mu_refcount_destroy (&fs->refcount);
free (fs);
}
......@@ -347,7 +345,7 @@ _fs_open (stream_t stream, const char *filename, int port, int flags)
|| filebuf.st_nlink != 1
|| !S_ISREG(fdbuf.st_mode))
{
mu_error ("%s must be a plain file with one link\n", filename);
/*mu_error ("%s must be a plain file with one link\n", filename); */
close (fd);
return EINVAL;
}
......
......@@ -19,8 +19,7 @@
#define _MAILUTILS_DEBUG_H
#include <sys/types.h>
#include <stdarg.h>
#include <stdio.h>
#include <mailutils/stream.h>
#ifndef __P
#ifdef __STDC__
......@@ -42,14 +41,11 @@ typedef struct _mu_debug* mu_debug_t;
extern int mu_debug_ref __P ((mu_debug_t));
extern void mu_debug_destroy __P ((mu_debug_t *));
extern int mu_debug_set_level __P ((mu_debug_t, unsigned int level));
extern int mu_debug_get_level __P ((mu_debug_t, unsigned int *plevel));
extern int mu_debug_print __P ((mu_debug_t debug, unsigned int level,
const char *format, ...));
extern int mu_debug_printv __P ((mu_debug_t debug, unsigned int level,
const char *format, va_list argp));
extern int mu_debug_stdio_create __P ((mu_debug_t *, FILE *));
extern int mu_debug_set_level __P ((mu_debug_t, unsigned int));
extern int mu_debug_get_level __P ((mu_debug_t, unsigned int *));
extern int mu_debug_print __P ((mu_debug_t, unsigned int, const char *));
extern int mu_debug_stream_create __P ((mu_debug_t *, stream_t, int));
#ifdef __cplusplus
}
......
......@@ -43,7 +43,7 @@ struct _mu_debug_vtable
int (*get_level) __P ((mu_debug_t, unsigned int *));
int (*set_level) __P ((mu_debug_t, unsigned int));
int (*printv) __P ((mu_debug_t, unsigned int, const char *, va_list));
int (*print) __P ((mu_debug_t, unsigned int, const char *));
};
struct _mu_debug
......
......@@ -42,7 +42,8 @@ struct _sdebug
struct _mu_debug base;
mu_refcount_t refcount;
int level;
FILE *fp;
stream_t stream;
int close_on_destroy;
};
#ifdef __cplusplus
......
......@@ -53,13 +53,6 @@ _map_destroy (stream_t *pstream)
if (mu_refcount_dec (ms->refcount) == 0)
{
if (ms->ptr != MAP_FAILED)
{
if (ms->ptr)
munmap (ms->ptr, ms->size);
}
if (ms->fd != -1)
close (ms->fd);
mu_refcount_destroy (&ms->refcount);
free (ms);
}
......
......@@ -39,8 +39,6 @@ _memory_destroy (stream_t *pstream)
struct _memory_stream *mem = (struct _memory_stream *)*pstream;
if (mu_refcount_dec (mem->refcount) == 0)
{
if (mem && mem->ptr != NULL)
free (mem->ptr);
mu_refcount_destroy (&mem->refcount);
free (mem);
}
......
......@@ -25,6 +25,7 @@
# include <strings.h>
#endif
#include <stdio.h>
#include <mailutils/sys/pop3.h>
int
......@@ -36,7 +37,10 @@ pop3_get_debug (pop3_t pop3, mu_debug_t *pdebug)
if (pop3->debug == NULL)
{
int status = mu_debug_stdio_create (&pop3->debug, stderr);
stream_t stream;
int status = stream_stdio_create (&stream, stderr);
if (status == 0)
status = mu_debug_stream_create (&pop3->debug, stream, 0);
if (status != 0)
return status;
}
......
......@@ -48,7 +48,6 @@ int com_stat (char *);
int com_top (char *);
int com_uidl (char *);
int com_user (char *);
int print_response (void);
void initialize_readline (void);
char *stripwhite (char *);
......@@ -152,7 +151,7 @@ main (int argc, char **argv)
exit (0);
}
/* Execute a command line. */
/* Parse and execute a command line. */
int
execute_line (char *line)
{
......@@ -302,28 +301,11 @@ command_generator (const char *text, int state)
}
int
print_response ()
{
#if 0
char response[1024];
if (pop3)
{
pop3_response (pop3, response, sizeof response, NULL);
fprintf (stderr, "%s\n", response);
}
else
fprintf (stderr, "Not connected, try `connect' first\n");
#endif
return 0;
}
int
com_user (char *arg)
{
if (!valid_argument ("user", arg))
return 1;
pop3_user (pop3, arg);
return print_response ();
return pop3_user (pop3, arg);
}
int
com_apop (char *arg)
......@@ -336,8 +318,7 @@ com_apop (char *arg)
digest = strtok (NULL, " ");
if (!valid_argument ("apop", user) || !valid_argument ("apop", digest))
return 1;
pop3_apop (pop3, user, digest);
return print_response ();
return pop3_apop (pop3, user, digest);
}
int
......@@ -346,7 +327,6 @@ com_capa (char *arg)
iterator_t iterator;
int status = pop3_capa (pop3, &iterator);
(void)arg;
print_response ();
if (status == 0)
{
for (iterator_first (iterator);
......@@ -370,7 +350,6 @@ com_uidl (char *arg)
{
iterator_t uidl_iterator;
int status = pop3_uidl_all (pop3, &uidl_iterator);
print_response ();
if (status == 0)
{
for (iterator_first (uidl_iterator);
......@@ -392,8 +371,6 @@ com_uidl (char *arg)
unsigned int msgno = strtoul (arg, NULL, 10);
if (pop3_uidl (pop3, msgno, &uidl) == 0)
printf ("Msg: %d UIDL: %s\n", msgno, (uidl) ? uidl : "");
else
print_response ();
}
return 0;
}
......@@ -405,7 +382,6 @@ com_list (char *arg)
{
iterator_t list_iterator;
int status = pop3_list_all (pop3, &list_iterator);
print_response ();
if (status == 0)
{
for (iterator_first (list_iterator);
......@@ -426,8 +402,6 @@ com_list (char *arg)
unsigned int msgno = strtoul (arg, NULL, 10);
if (pop3_list (pop3, msgno, &size) == 0)
printf ("Msg: %d Size: %d\n", msgno, size);
else
print_response ();
}
return 0;
}
......@@ -436,8 +410,7 @@ int
com_noop (char *arg)
{
(void)arg;
pop3_noop (pop3);
return print_response ();
return pop3_noop (pop3);
}
static void
......@@ -475,8 +448,7 @@ com_pass (arg)
pass [strlen (pass) - 1] = '\0'; /* nuke the trailing line. */
arg = pass;
}
pop3_pass (pop3, arg);
return print_response ();
return pop3_pass (pop3, arg);
}
int
......@@ -487,7 +459,6 @@ com_stat (char *arg)
(void)arg;
count = size = 0;
pop3_stat (pop3, &count, &size);
print_response ();
fprintf (stdout, "Mesgs: %d Size %d\n", count, size);
return 0;
}
......@@ -500,8 +471,7 @@ com_dele (arg)
if (!valid_argument ("dele", arg))
return 1;
msgno = strtoul (arg, NULL, 10);
pop3_dele (pop3, msgno);
return print_response ();
return pop3_dele (pop3, msgno);
}
/* Print out help for ARG, or for all of the commands if ARG is
......@@ -548,8 +518,7 @@ int
com_rset (char *arg)
{
(void)arg;
pop3_rset (pop3);
return print_response ();
return pop3_rset (pop3);
}
int
......@@ -575,7 +544,6 @@ com_top (char *arg)
msgno = strtoul (arg, NULL, 10);
status = pop3_top (pop3, msgno, lines, &stream);
print_response ();
if (status == 0)
{
......@@ -600,7 +568,6 @@ com_retr (char *arg)
msgno = strtoul (arg, NULL, 10);
status = pop3_retr (pop3, msgno, &stream);
print_response ();
if (status == 0)
{
......@@ -634,7 +601,6 @@ com_connect (char *arg)
pop3_get_debug (pop3, &debug);
mu_debug_set_level (debug, MU_DEBUG_PROT);
pop3_connect (pop3, host, port);
print_response ();
}
else
fprintf (stderr, "Failed to create pop3: %s\n", strerror (status));
......@@ -662,12 +628,10 @@ com_quit (char *arg)
{
if (pop3_quit (pop3) == 0)
{
print_response ();
pop3_disconnect (pop3);
}
else
{
print_response ();
fprintf (stdout, "Try 'exit' to leave %s\n", progname);
}
}
......
......@@ -42,6 +42,12 @@ _sdebug_destroy (mu_debug_t *pdebug)
struct _sdebug *sdebug = (struct _sdebug *)*pdebug;
if (mu_refcount_dec (sdebug->refcount) == 0)
{
if (sdebug->stream)
{
if (sdebug->close_on_destroy)
stream_close (sdebug->stream);
stream_destroy (&sdebug->stream);
}
mu_refcount_destroy (&sdebug->refcount);
free (sdebug);
}
......@@ -67,18 +73,17 @@ _sdebug_get_level (mu_debug_t debug, size_t *plevel)
}
static int
_sdebug_printv (mu_debug_t debug, size_t level, const char *fmt, va_list ap)
_sdebug_print (mu_debug_t debug, size_t level, const char *mesg)
{
struct _sdebug *sdebug = (struct _sdebug *)debug;
if (fmt == NULL)
if (mesg == NULL)
return MU_ERROR_INVALID_PARAMETER;
if (!(sdebug->level & level))
return 0;
vfprintf (sdebug->fp, fmt, ap);
return 0;
return stream_write (sdebug->stream, mesg, strlen (mesg), NULL);
}
struct _mu_debug_vtable _sdebug_vtable =
......@@ -88,26 +93,32 @@ struct _mu_debug_vtable _sdebug_vtable =
_sdebug_get_level,
_sdebug_set_level,
_sdebug_printv
_sdebug_print
};
int
mu_debug_stdio_create (mu_debug_t *pdebug, FILE *fp)
mu_debug_stream_create (mu_debug_t *pdebug, stream_t stream,
int close_on_destroy)
{
struct _sdebug *sdebug;
if (pdebug == NULL || fp == NULL)
if (pdebug == NULL || stream == NULL)
return MU_ERROR_INVALID_PARAMETER;
sdebug = calloc (sizeof (*sdebug), 1);
if (sdebug == NULL)
return MU_ERROR_NO_MEMORY;
mu_refcount_create (&sdebug->refcount);
if (sdebug->refcount == NULL)
{
free (sdebug);
return MU_ERROR_NO_MEMORY;
}
sdebug->level = 0;
sdebug->fp = fp;
sdebug->stream = stream;
sdebug->close_on_destroy = close_on_destroy;
sdebug->base.vtable = &_sdebug_vtable;
*pdebug = &sdebug->base;
return 0;
......
......@@ -64,8 +64,6 @@ _tcp_destroy (stream_t *pstream)
{
if (tcp->host)
free (tcp->host);
if (tcp->fd != -1)
close (tcp->fd);
mu_refcount_destroy (&tcp->refcount);
free (tcp);
}
......@@ -78,6 +76,9 @@ _tcp_close0 (stream_t stream)
if (tcp->fd != -1)
close (tcp->fd);
tcp->fd = -1;
if (tcp->host)
free (tcp->host);
tcp->host = NULL;
tcp->state = TCP_STATE_INIT;
return 0;
}
......@@ -108,6 +109,8 @@ _tcp_open0 (stream_t stream, const char *host, int port, int flags)
if (tcp->state == TCP_STATE_INIT)
{
tcp->port = port;
if (tcp->host)
free (tcp->host);
tcp->host = strdup (host);
if (tcp->host == NULL)
return MU_ERROR_NO_MEMORY;
......