lib: split out utils to lib/utils
This commit is contained in:
69
lib/utils/Makefile.am
Normal file
69
lib/utils/Makefile.am
Normal file
@ -0,0 +1,69 @@
|
||||
## Copyright (C) 2019 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
##
|
||||
## 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 of the License, 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, write to the Free Software Foundation,
|
||||
## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
include $(top_srcdir)/gtest.mk
|
||||
|
||||
AM_CFLAGS= \
|
||||
$(WARN_CFLAGS) \
|
||||
$(GLIB_CFLAGS) \
|
||||
$(ASAN_CFLAGS) \
|
||||
-DMU_TESTMAILDIR=\"${abs_top_srcdir}/lib/tests/testdir\" \
|
||||
-DMU_TESTMAILDIR2=\"${abs_top_srcdir}/lib/tests/testdir2\" \
|
||||
-Wno-format-nonliteral \
|
||||
-Wno-switch-enum \
|
||||
-Wno-deprecated-declarations \
|
||||
-Wno-inline
|
||||
|
||||
AM_CXXFLAGS= \
|
||||
$(WARN_CXXFLAGS) \
|
||||
$(GLIB_CFLAGS) \
|
||||
$(ASAN_CXXFLAGS)
|
||||
|
||||
noinst_LTLIBRARIES= \
|
||||
libmu-utils.la
|
||||
|
||||
libmu_utils_la_SOURCES= \
|
||||
mu-date.c \
|
||||
mu-date.h \
|
||||
mu-log.c \
|
||||
mu-log.h \
|
||||
mu-str.c \
|
||||
mu-str.h \
|
||||
mu-util.c \
|
||||
mu-util.h
|
||||
|
||||
libmu_utils_la_LIBADD= \
|
||||
$(GLIB_LIBS)
|
||||
|
||||
libmu_utils_la_LDFLAGS= \
|
||||
$(ASAN_LDFLAGS)
|
||||
|
||||
noinst_PROGRAMS= \
|
||||
$(TEST_PROGS)
|
||||
|
||||
TEST_PROGS+= \
|
||||
test-mu-util
|
||||
test_mu_util_SOURCES= \
|
||||
test-mu-util.c
|
||||
test_mu_util_LDADD= \
|
||||
libmu-utils.la
|
||||
|
||||
TEST_PROGS+= \
|
||||
test-mu-str
|
||||
test_mu_str_SOURCES= \
|
||||
test-mu-str.c
|
||||
test_mu_str_LDADD= \
|
||||
libmu-utils.la
|
||||
87
lib/utils/mu-date.c
Normal file
87
lib/utils/mu-date.c
Normal file
@ -0,0 +1,87 @@
|
||||
/*
|
||||
** Copyright (C) 2012 <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "mu-util.h"
|
||||
#include "mu-date.h"
|
||||
#include "mu-str.h"
|
||||
|
||||
const char*
|
||||
mu_date_str_s (const char* frm, time_t t)
|
||||
{
|
||||
struct tm *tmbuf;
|
||||
static char buf[128];
|
||||
static int is_utf8 = -1;
|
||||
size_t len;
|
||||
|
||||
if (G_UNLIKELY(is_utf8 == -1))
|
||||
is_utf8 = mu_util_locale_is_utf8 () ? 1 : 0;
|
||||
|
||||
g_return_val_if_fail (frm, NULL);
|
||||
|
||||
tmbuf = localtime(&t);
|
||||
len = strftime (buf, sizeof(buf) - 1, frm, tmbuf);
|
||||
if (len == 0)
|
||||
return ""; /* not necessarily an error... */
|
||||
|
||||
if (!is_utf8) {
|
||||
/* charset is _not_ utf8, so we need to convert it, so
|
||||
* the date could contain locale-specific characters*/
|
||||
gchar *conv;
|
||||
GError *err;
|
||||
err = NULL;
|
||||
conv = g_locale_to_utf8 (buf, -1, NULL, NULL, &err);
|
||||
if (err) {
|
||||
g_warning ("conversion failed: %s", err->message);
|
||||
g_error_free (err);
|
||||
strcpy (buf, "<error>");
|
||||
} else {
|
||||
strncpy (buf, conv, sizeof(buf)-1);
|
||||
buf[sizeof(buf)-1] = '\0';
|
||||
}
|
||||
|
||||
g_free (conv);
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
char*
|
||||
mu_date_str (const char *frm, time_t t)
|
||||
{
|
||||
return g_strdup (mu_date_str_s(frm, t));
|
||||
}
|
||||
|
||||
|
||||
const char*
|
||||
mu_date_display_s (time_t t)
|
||||
{
|
||||
time_t now;
|
||||
static const time_t SECS_IN_DAY = 24 * 60 * 60;
|
||||
|
||||
now = time (NULL);
|
||||
|
||||
if (ABS(now - t) > SECS_IN_DAY)
|
||||
return mu_date_str_s ("%x", t);
|
||||
else
|
||||
return mu_date_str_s ("%X", t);
|
||||
}
|
||||
67
lib/utils/mu-date.h
Normal file
67
lib/utils/mu-date.h
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
** Copyright (C) 2012-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#ifndef __MU_DATE_H__
|
||||
#define __MU_DATE_H__
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
|
||||
/**
|
||||
* @addtogroup MuDate
|
||||
* Date-related functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* get a string for a given time_t
|
||||
*
|
||||
* mu_date_str_s returns a ptr to a static buffer,
|
||||
* while mu_date_str returns dynamically allocated
|
||||
* memory that must be freed after use.
|
||||
*
|
||||
* @param frm the format of the string (in strftime(3) format)
|
||||
* @param t the time as time_t
|
||||
*
|
||||
* @return a string representation of the time; see above for what to
|
||||
* do with it. Length is max. 128 bytes, inc. the ending \0. if the
|
||||
* format is too long, the value will be truncated. in practice this
|
||||
* should not happen.
|
||||
*/
|
||||
const char* mu_date_str_s (const char* frm, time_t t) G_GNUC_CONST;
|
||||
char* mu_date_str (const char* frm, time_t t) G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* get a display string for a given time_t; if the given is less than
|
||||
* 24h from the current time, we display the time, otherwise the date,
|
||||
* using the preferred date/time for the current locale
|
||||
*
|
||||
* mu_str_display_date_s returns a ptr to a static buffer,
|
||||
*
|
||||
* @param t the time as time_t
|
||||
*
|
||||
* @return a string representation of the time/date
|
||||
*/
|
||||
const char* mu_date_display_s (time_t t);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /*__MU_DATE_H__*/
|
||||
319
lib/utils/mu-log.c
Normal file
319
lib/utils/mu-log.c
Normal file
@ -0,0 +1,319 @@
|
||||
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
||||
|
||||
/*
|
||||
** Copyright (C) 2008-2016 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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 of the License, 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif /*HAVE_CONFIG_H*/
|
||||
|
||||
#include "mu-log.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mu-util.h"
|
||||
|
||||
#define MU_MAX_LOG_FILE_SIZE 1000 * 1000 /* 1 MB (SI units) */
|
||||
#define MU_LOG_FILE "mu.log"
|
||||
|
||||
struct _MuLog {
|
||||
int _fd; /* log file descriptor */
|
||||
|
||||
MuLogOptions _opts;
|
||||
|
||||
gboolean _color_stdout; /* whether to use color */
|
||||
gboolean _color_stderr;
|
||||
|
||||
GLogFunc _old_log_func;
|
||||
};
|
||||
typedef struct _MuLog MuLog;
|
||||
|
||||
/* we use globals, because logging is a global operation as it
|
||||
* globally modifies the behaviour of g_warning and friends
|
||||
*/
|
||||
static MuLog* MU_LOG = NULL;
|
||||
static void log_write (const char* domain, GLogLevelFlags level,
|
||||
const gchar *msg);
|
||||
|
||||
static void
|
||||
try_close (int fd)
|
||||
{
|
||||
if (fd < 0)
|
||||
return;
|
||||
|
||||
if (close (fd) < 0)
|
||||
g_printerr ("%s: close() of fd %d failed: %s\n",
|
||||
__func__, fd, strerror(errno));
|
||||
}
|
||||
|
||||
static void
|
||||
silence (void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
gboolean
|
||||
mu_log_init_silence (void)
|
||||
{
|
||||
g_return_val_if_fail (!MU_LOG, FALSE);
|
||||
|
||||
MU_LOG = g_new0 (MuLog, 1);
|
||||
MU_LOG->_fd = -1;
|
||||
|
||||
mu_log_options_set (MU_LOG_OPTIONS_NONE);
|
||||
|
||||
MU_LOG->_old_log_func =
|
||||
g_log_set_default_handler ((GLogFunc)silence, NULL);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
log_handler (const gchar* log_domain, GLogLevelFlags log_level,
|
||||
const gchar* msg)
|
||||
{
|
||||
if ((log_level & G_LOG_LEVEL_DEBUG) &&
|
||||
!(MU_LOG->_opts & MU_LOG_OPTIONS_DEBUG))
|
||||
return;
|
||||
|
||||
log_write (log_domain ? log_domain : "mu", log_level, msg);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
mu_log_options_set (MuLogOptions opts)
|
||||
{
|
||||
g_return_if_fail (MU_LOG);
|
||||
|
||||
MU_LOG->_opts = opts;
|
||||
|
||||
/* when color is, only enable it when output is to a tty */
|
||||
if (MU_LOG->_opts & MU_LOG_OPTIONS_COLOR) {
|
||||
MU_LOG->_color_stdout = isatty(fileno(stdout));
|
||||
MU_LOG->_color_stderr = isatty(fileno(stderr));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
MuLogOptions
|
||||
mu_log_options_get (void)
|
||||
{
|
||||
g_return_val_if_fail (MU_LOG, MU_LOG_OPTIONS_NONE);
|
||||
|
||||
return MU_LOG->_opts;
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
move_log_file (const char *logfile)
|
||||
{
|
||||
gchar *logfile_old;
|
||||
int rv;
|
||||
|
||||
logfile_old = g_strdup_printf ("%s.old", logfile);
|
||||
rv = rename (logfile, logfile_old);
|
||||
g_free (logfile_old);
|
||||
|
||||
if (rv != 0) {
|
||||
g_warning ("failed to move %s to %s.old: %s",
|
||||
logfile, logfile, strerror(rv));
|
||||
return FALSE;
|
||||
} else
|
||||
return TRUE;
|
||||
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
log_file_backup_maybe (const char *logfile)
|
||||
{
|
||||
struct stat statbuf;
|
||||
|
||||
if (stat (logfile, &statbuf) != 0) {
|
||||
if (errno == ENOENT)
|
||||
return TRUE; /* it did not exist yet, no problem */
|
||||
else {
|
||||
g_warning ("failed to stat(2) %s: %s",
|
||||
logfile, strerror(errno));
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/* log file is still below the max size? */
|
||||
if (statbuf.st_size <= MU_MAX_LOG_FILE_SIZE)
|
||||
return TRUE;
|
||||
|
||||
/* log file is too big!; we move it to <logfile>.old, overwriting */
|
||||
return move_log_file (logfile);
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
mu_log_init (const char* logfile, MuLogOptions opts)
|
||||
{
|
||||
int fd;
|
||||
|
||||
/* only init once... */
|
||||
g_return_val_if_fail (!MU_LOG, FALSE);
|
||||
g_return_val_if_fail (logfile, FALSE);
|
||||
|
||||
if (opts & MU_LOG_OPTIONS_BACKUP)
|
||||
if (!log_file_backup_maybe(logfile)) {
|
||||
g_warning ("failed to backup log file");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
fd = open (logfile, O_WRONLY|O_CREAT|O_APPEND, 00600);
|
||||
if (fd < 0) {
|
||||
g_warning ("%s: open() of '%s' failed: %s", __func__,
|
||||
logfile, strerror(errno));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
MU_LOG = g_new0 (MuLog, 1);
|
||||
MU_LOG->_fd = fd;
|
||||
|
||||
mu_log_options_set (opts);
|
||||
|
||||
MU_LOG->_old_log_func =
|
||||
g_log_set_default_handler ((GLogFunc)log_handler, NULL);
|
||||
|
||||
MU_WRITE_LOG ("logging started");
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
mu_log_uninit (void)
|
||||
{
|
||||
if (!MU_LOG)
|
||||
return;
|
||||
|
||||
MU_WRITE_LOG ("logging stopped");
|
||||
|
||||
try_close (MU_LOG->_fd);
|
||||
g_free (MU_LOG);
|
||||
|
||||
MU_LOG = NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char*
|
||||
levelstr (GLogLevelFlags level)
|
||||
{
|
||||
switch (level) {
|
||||
case G_LOG_LEVEL_WARNING: return " [WARN] ";
|
||||
case G_LOG_LEVEL_ERROR : return " [ERR ] ";
|
||||
case G_LOG_LEVEL_DEBUG: return " [DBG ] ";
|
||||
case G_LOG_LEVEL_CRITICAL: return " [CRIT] ";
|
||||
case G_LOG_LEVEL_MESSAGE: return " [MSG ] ";
|
||||
case G_LOG_LEVEL_INFO : return " [INFO] ";
|
||||
default: return " [LOG ] ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define color_stdout_maybe(C) \
|
||||
do{if (MU_LOG->_color_stdout) fputs ((C),stdout);} while (0)
|
||||
#define color_stderr_maybe(C) \
|
||||
do{if (MU_LOG->_color_stderr) fputs ((C),stderr);} while (0)
|
||||
|
||||
|
||||
|
||||
static void
|
||||
log_write_fd (GLogLevelFlags level, const gchar *msg)
|
||||
{
|
||||
time_t now;
|
||||
char timebuf [22];
|
||||
const char *mylevel;
|
||||
|
||||
/* get the time/date string */
|
||||
now = time(NULL);
|
||||
strftime (timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S",
|
||||
localtime(&now));
|
||||
|
||||
if (write (MU_LOG->_fd, timebuf, strlen (timebuf)) < 0)
|
||||
goto err;
|
||||
|
||||
mylevel = levelstr (level);
|
||||
if (write (MU_LOG->_fd, mylevel, strlen (mylevel)) < 0)
|
||||
goto err;
|
||||
|
||||
if (write (MU_LOG->_fd, msg, strlen (msg)) < 0)
|
||||
goto err;
|
||||
|
||||
if (write (MU_LOG->_fd, "\n", strlen ("\n")) < 0)
|
||||
goto err;
|
||||
|
||||
return; /* all went well */
|
||||
|
||||
err:
|
||||
fprintf (stderr, "%s: failed to write to log: %s\n",
|
||||
__func__, strerror(errno));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
log_write_stdout_stderr (GLogLevelFlags level, const gchar *msg)
|
||||
{
|
||||
const char *mu;
|
||||
|
||||
mu = MU_LOG->_opts & MU_LOG_OPTIONS_NEWLINE ?
|
||||
"\nmu: " : "mu: ";
|
||||
|
||||
if (!(MU_LOG->_opts & MU_LOG_OPTIONS_QUIET) &&
|
||||
(level & G_LOG_LEVEL_MESSAGE)) {
|
||||
color_stdout_maybe (MU_COLOR_GREEN);
|
||||
fputs (mu, stdout);
|
||||
fputs (msg, stdout);
|
||||
fputs ("\n", stdout);
|
||||
color_stdout_maybe (MU_COLOR_DEFAULT);
|
||||
}
|
||||
|
||||
/* for errors, log them to stderr as well */
|
||||
if (level & G_LOG_LEVEL_ERROR ||
|
||||
level & G_LOG_LEVEL_CRITICAL ||
|
||||
level & G_LOG_LEVEL_WARNING) {
|
||||
color_stderr_maybe (MU_COLOR_RED);
|
||||
fputs (mu, stderr);
|
||||
fputs (msg, stderr);
|
||||
fputs ("\n", stderr);
|
||||
color_stderr_maybe (MU_COLOR_DEFAULT);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
log_write (const char* domain, GLogLevelFlags level, const gchar *msg)
|
||||
{
|
||||
g_return_if_fail (MU_LOG);
|
||||
|
||||
log_write_fd (level, msg);
|
||||
log_write_stdout_stderr (level, msg);
|
||||
}
|
||||
99
lib/utils/mu-log.h
Normal file
99
lib/utils/mu-log.h
Normal file
@ -0,0 +1,99 @@
|
||||
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset:
|
||||
8 -*-*/
|
||||
/*
|
||||
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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 of the License, 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#ifndef __MU_LOG_H__
|
||||
#define __MU_LOG_H__
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
/* mu log is the global logging system */
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
enum _MuLogOptions {
|
||||
MU_LOG_OPTIONS_NONE = 0,
|
||||
|
||||
/* when size of log file > MU_MAX_LOG_FILE_SIZE, move the log
|
||||
* file to <log file>.old and start a new one. The .old file will
|
||||
* overwrite existing files of that name */
|
||||
MU_LOG_OPTIONS_BACKUP = 1 << 1,
|
||||
|
||||
/* quiet: don't log non-errors to stdout/stderr */
|
||||
MU_LOG_OPTIONS_QUIET = 1 << 2,
|
||||
|
||||
/* should lines be preceded by \n? useful when errors come
|
||||
* during indexing */
|
||||
MU_LOG_OPTIONS_NEWLINE = 1 << 3,
|
||||
|
||||
/* color in output (iff output is to a tty) */
|
||||
MU_LOG_OPTIONS_COLOR = 1 << 4,
|
||||
|
||||
/* log everything to stderr */
|
||||
MU_LOG_OPTIONS_STDERR = 1 << 5,
|
||||
|
||||
/* debug: debug include debug-level information */
|
||||
MU_LOG_OPTIONS_DEBUG = 1 << 6
|
||||
};
|
||||
typedef enum _MuLogOptions MuLogOptions;
|
||||
|
||||
|
||||
/**
|
||||
* write logging information to a log file
|
||||
*
|
||||
* @param full path to the log file (does not have to exist yet, but
|
||||
* it's directory must)
|
||||
* @param opts logging options
|
||||
*
|
||||
* @return TRUE if initialization succeeds, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_log_init (const char *logfile, MuLogOptions opts)
|
||||
G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* be silent except for runtime errors, which will be written to
|
||||
* stderr.
|
||||
*
|
||||
* @return TRUE if initialization succeeds, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_log_init_silence (void) G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* uninitialize the logging system, and free all resources
|
||||
*/
|
||||
void mu_log_uninit (void);
|
||||
|
||||
/**
|
||||
* set logging options, a logical-OR'd value of MuLogOptions
|
||||
*
|
||||
* @param opts the options (logically OR'd)
|
||||
*/
|
||||
void mu_log_options_set (MuLogOptions opts);
|
||||
|
||||
/**
|
||||
* get logging options, a logical-OR'd value of MuLogOptions
|
||||
*
|
||||
* @param opts the options (logically OR'd)
|
||||
*/
|
||||
MuLogOptions mu_log_options_get (void);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /*__MU_LOG_H__*/
|
||||
639
lib/utils/mu-str.c
Normal file
639
lib/utils/mu-str.c
Normal file
@ -0,0 +1,639 @@
|
||||
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
||||
/*
|
||||
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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 of the License, 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif /*HAVE_CONFIG_H*/
|
||||
|
||||
|
||||
#include <glib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mu-util.h" /* PATH_MAX */
|
||||
#include "mu-str.h"
|
||||
#include "mu-msg-fields.h"
|
||||
|
||||
|
||||
const char*
|
||||
mu_str_size_s (size_t s)
|
||||
{
|
||||
static char buf[32];
|
||||
char *tmp;
|
||||
|
||||
tmp = g_format_size_for_display ((goffset)s);
|
||||
strncpy (buf, tmp, sizeof(buf));
|
||||
buf[sizeof(buf) -1] = '\0'; /* just in case */
|
||||
g_free (tmp);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
char*
|
||||
mu_str_size (size_t s)
|
||||
{
|
||||
return g_strdup (mu_str_size_s(s));
|
||||
}
|
||||
|
||||
const char*
|
||||
mu_str_flags_s (MuFlags flags)
|
||||
{
|
||||
return mu_flags_to_str_s (flags, MU_FLAG_TYPE_ANY);
|
||||
}
|
||||
|
||||
char*
|
||||
mu_str_flags (MuFlags flags)
|
||||
{
|
||||
return g_strdup (mu_str_flags_s(flags));
|
||||
}
|
||||
|
||||
char*
|
||||
mu_str_summarize (const char* str, size_t max_lines)
|
||||
{
|
||||
char *summary;
|
||||
size_t nl_seen;
|
||||
unsigned i,j;
|
||||
gboolean last_was_blank;
|
||||
|
||||
g_return_val_if_fail (str, NULL);
|
||||
g_return_val_if_fail (max_lines > 0, NULL);
|
||||
|
||||
/* len for summary <= original len */
|
||||
summary = g_new (gchar, strlen(str) + 1);
|
||||
|
||||
/* copy the string up to max_lines lines, replace CR/LF/tab with
|
||||
* single space */
|
||||
for (i = j = 0, nl_seen = 0, last_was_blank = TRUE;
|
||||
nl_seen < max_lines && str[i] != '\0'; ++i) {
|
||||
|
||||
if (str[i] == '\n' || str[i] == '\r' ||
|
||||
str[i] == '\t' || str[i] == ' ' ) {
|
||||
|
||||
if (str[i] == '\n')
|
||||
++nl_seen;
|
||||
|
||||
/* no double-blanks or blank at end of str */
|
||||
if (!last_was_blank && str[i+1] != '\0')
|
||||
summary[j++] = ' ';
|
||||
|
||||
last_was_blank = TRUE;
|
||||
} else {
|
||||
|
||||
summary[j++] = str[i];
|
||||
last_was_blank = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
summary[j] = '\0';
|
||||
return summary;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
cleanup_contact (char *contact)
|
||||
{
|
||||
char *c, *c2;
|
||||
|
||||
/* replace "'<> with space */
|
||||
for (c2 = contact; *c2; ++c2)
|
||||
if (*c2 == '"' || *c2 == '\'' || *c2 == '<' || *c2 == '>')
|
||||
*c2 = ' ';
|
||||
|
||||
/* remove everything between '()' if it's after the 5th pos;
|
||||
* good to cleanup corporate contact address spam... */
|
||||
c = g_strstr_len (contact, -1, "(");
|
||||
if (c && c - contact > 5)
|
||||
*c = '\0';
|
||||
|
||||
g_strstrip (contact);
|
||||
}
|
||||
|
||||
|
||||
/* this is still somewhat simplistic... */
|
||||
const char*
|
||||
mu_str_display_contact_s (const char *str)
|
||||
{
|
||||
static gchar contact[255];
|
||||
gchar *c, *c2;
|
||||
|
||||
str = str ? str : "";
|
||||
g_strlcpy (contact, str, sizeof(contact));
|
||||
|
||||
/* we check for '<', so we can strip out the address stuff in
|
||||
* e.g. 'Hello World <hello@world.xx>, but only if there is
|
||||
* something alphanumeric before the <
|
||||
*/
|
||||
c = g_strstr_len (contact, -1, "<");
|
||||
if (c != NULL) {
|
||||
for (c2 = contact; c2 < c && !(isalnum(*c2)); ++c2);
|
||||
if (c2 != c) /* apparently, there was something,
|
||||
* so we can remove the <... part*/
|
||||
*c = '\0';
|
||||
}
|
||||
|
||||
cleanup_contact (contact);
|
||||
|
||||
return contact;
|
||||
}
|
||||
|
||||
char*
|
||||
mu_str_display_contact (const char *str)
|
||||
{
|
||||
g_return_val_if_fail (str, NULL);
|
||||
|
||||
return g_strdup (mu_str_display_contact_s (str));
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
mu_str_replace (const char *str, const char *substr, const char *repl)
|
||||
{
|
||||
GString *gstr;
|
||||
const char *cur;
|
||||
|
||||
g_return_val_if_fail (str, NULL);
|
||||
g_return_val_if_fail (substr, NULL);
|
||||
g_return_val_if_fail (repl, NULL);
|
||||
|
||||
gstr = g_string_sized_new (2 * strlen (str));
|
||||
|
||||
for (cur = str; *cur; ++cur) {
|
||||
if (g_str_has_prefix (cur, substr)) {
|
||||
g_string_append (gstr, repl);
|
||||
cur += strlen (substr) - 1;
|
||||
} else
|
||||
g_string_append_c (gstr, *cur);
|
||||
}
|
||||
|
||||
return g_string_free (gstr, FALSE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
char*
|
||||
mu_str_from_list (const GSList *lst, char sepa)
|
||||
{
|
||||
const GSList *cur;
|
||||
char *str;
|
||||
|
||||
g_return_val_if_fail (sepa, NULL);
|
||||
|
||||
for (cur = lst, str = NULL; cur; cur = g_slist_next(cur)) {
|
||||
|
||||
char *tmp;
|
||||
/* two extra dummy '\0' so -Wstack-protector won't complain */
|
||||
char sep[4] = { '\0', '\0', '\0', '\0' };
|
||||
sep[0] = cur->next ? sepa : '\0';
|
||||
|
||||
tmp = g_strdup_printf ("%s%s%s",
|
||||
str ? str : "",
|
||||
(gchar*)cur->data,
|
||||
sep);
|
||||
g_free (str);
|
||||
str = tmp;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
GSList*
|
||||
mu_str_to_list (const char *str, char sepa, gboolean strip)
|
||||
{
|
||||
GSList *lst;
|
||||
gchar **strs, **cur;
|
||||
/* two extra dummy '\0' so -Wstack-protector won't complain */
|
||||
char sep[4] = { '\0', '\0', '\0', '\0' };
|
||||
|
||||
g_return_val_if_fail (sepa, NULL);
|
||||
|
||||
if (!str)
|
||||
return NULL;
|
||||
|
||||
sep[0] = sepa;
|
||||
strs = g_strsplit (str, sep, -1);
|
||||
|
||||
for (cur = strs, lst = NULL; cur && *cur; ++cur) {
|
||||
char *elm;
|
||||
elm = g_strdup(*cur);
|
||||
if (strip)
|
||||
elm = g_strstrip (elm);
|
||||
|
||||
lst = g_slist_prepend (lst, elm);
|
||||
}
|
||||
|
||||
lst = g_slist_reverse (lst);
|
||||
g_strfreev (strs);
|
||||
|
||||
return lst;
|
||||
}
|
||||
|
||||
GSList*
|
||||
mu_str_esc_to_list (const char *strings)
|
||||
{
|
||||
GSList *lst;
|
||||
GString *part;
|
||||
unsigned u;
|
||||
gboolean quoted, escaped;
|
||||
|
||||
g_return_val_if_fail (strings, NULL);
|
||||
|
||||
part = g_string_new (NULL);
|
||||
|
||||
for (u = 0, lst = NULL, quoted = FALSE, escaped = FALSE;
|
||||
u != strlen (strings); ++u) {
|
||||
|
||||
char kar;
|
||||
kar = strings[u];
|
||||
|
||||
if (kar == '\\') {
|
||||
if (escaped)
|
||||
g_string_append_c (part, '\\');
|
||||
escaped = !escaped;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (quoted && kar != '"') {
|
||||
g_string_append_c (part, kar);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (kar) {
|
||||
case '"':
|
||||
if (!escaped)
|
||||
quoted = !quoted;
|
||||
else
|
||||
g_string_append_c (part, kar);
|
||||
continue;
|
||||
case ' ':
|
||||
if (part->len > 0) {
|
||||
lst = g_slist_prepend
|
||||
(lst, g_string_free (part, FALSE));
|
||||
part = g_string_new (NULL);
|
||||
}
|
||||
continue;
|
||||
default:
|
||||
g_string_append_c (part, kar);
|
||||
}
|
||||
}
|
||||
|
||||
if (part->len)
|
||||
lst = g_slist_prepend (lst, g_string_free (part, FALSE));
|
||||
|
||||
return g_slist_reverse (lst);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
mu_str_free_list (GSList *lst)
|
||||
{
|
||||
g_slist_foreach (lst, (GFunc)g_free, NULL);
|
||||
g_slist_free (lst);
|
||||
}
|
||||
|
||||
|
||||
/* this function is critical for sorting performance; therefore, no
|
||||
* regexps, but just some good old c pointer magic */
|
||||
const gchar*
|
||||
mu_str_subject_normalize (const gchar* str)
|
||||
{
|
||||
const char* cur;
|
||||
|
||||
g_return_val_if_fail (str, NULL);
|
||||
|
||||
cur = str;
|
||||
while (isspace(*cur)) ++cur; /* skip space */
|
||||
|
||||
/* starts with Re:? */
|
||||
if (tolower(cur[0]) == 'r' && tolower(cur[1]) == 'e')
|
||||
cur += 2;
|
||||
/* starts with Fwd:? */
|
||||
else if (tolower(cur[0]) == 'f' && tolower(cur[1]) == 'w' &&
|
||||
tolower(cur[2]) == 'd')
|
||||
cur += 3;
|
||||
else /* nope, different string */
|
||||
return str;
|
||||
|
||||
/* we're now past either 'Re' or 'Fwd'. Maybe there's a [<num>] now?
|
||||
* ie., the Re[3]: foo case */
|
||||
if (cur[0] == '[') { /* handle the Re[3]: case */
|
||||
if (isdigit(cur[1])) {
|
||||
do { ++cur; } while (isdigit(*cur));
|
||||
if ( cur[0] != ']') {
|
||||
return str; /* nope: no ending ']' */
|
||||
} else /* skip ']' and space */
|
||||
do { ++cur; } while (isspace(*cur));
|
||||
} else /* nope: no number after '[' */
|
||||
return str;
|
||||
}
|
||||
|
||||
/* now, cur points past either 're' or 'fwd', possibly with
|
||||
* [<num>]; check if it's really a prefix -- after re or fwd
|
||||
* there should either a ':' and possibly some space */
|
||||
if (cur[0] == ':') {
|
||||
do { ++cur; } while (isspace(*cur));
|
||||
/* note: there may still be another prefix, such as
|
||||
* Re[2]: Fwd: foo */
|
||||
return mu_str_subject_normalize (cur);
|
||||
} else
|
||||
return str; /* nope, it was not a prefix */
|
||||
}
|
||||
|
||||
|
||||
/* note: this function is *not* re-entrant, it returns a static buffer */
|
||||
const char*
|
||||
mu_str_fullpath_s (const char* path, const char* name)
|
||||
{
|
||||
static char buf[PATH_MAX + 1];
|
||||
|
||||
g_return_val_if_fail (path, NULL);
|
||||
|
||||
snprintf (buf, sizeof(buf), "%s%c%s", path, G_DIR_SEPARATOR,
|
||||
name ? name : "");
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
mu_str_escape_c_literal (const gchar* str, gboolean in_quotes)
|
||||
{
|
||||
const char* cur;
|
||||
GString *tmp;
|
||||
|
||||
g_return_val_if_fail (str, NULL);
|
||||
|
||||
tmp = g_string_sized_new (2 * strlen(str));
|
||||
|
||||
if (in_quotes)
|
||||
g_string_append_c (tmp, '"');
|
||||
|
||||
for (cur = str; *cur; ++cur)
|
||||
switch (*cur) {
|
||||
case '\\': tmp = g_string_append (tmp, "\\\\"); break;
|
||||
case '"': tmp = g_string_append (tmp, "\\\""); break;
|
||||
default: tmp = g_string_append_c (tmp, *cur);
|
||||
}
|
||||
|
||||
if (in_quotes)
|
||||
g_string_append_c (tmp, '"');
|
||||
|
||||
return g_string_free (tmp, FALSE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* turn \0-terminated buf into ascii (which is a utf8 subset); convert
|
||||
* any non-ascii into '.'
|
||||
*/
|
||||
char*
|
||||
mu_str_asciify_in_place (char *buf)
|
||||
{
|
||||
char *c;
|
||||
|
||||
g_return_val_if_fail (buf, NULL);
|
||||
|
||||
for (c = buf; c && *c; ++c) {
|
||||
if ((!isprint(*c) && !isspace (*c)) || !isascii(*c))
|
||||
*c = '.';
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
char*
|
||||
mu_str_utf8ify (const char *buf)
|
||||
{
|
||||
char *utf8;
|
||||
|
||||
g_return_val_if_fail (buf, NULL);
|
||||
|
||||
utf8 = g_strdup (buf);
|
||||
|
||||
if (!g_utf8_validate (buf, -1, NULL))
|
||||
mu_str_asciify_in_place (utf8);
|
||||
|
||||
return utf8;
|
||||
}
|
||||
|
||||
|
||||
|
||||
gchar*
|
||||
mu_str_convert_to_utf8 (const char* buffer, const char *charset)
|
||||
{
|
||||
GError *err;
|
||||
gchar * utf8;
|
||||
|
||||
g_return_val_if_fail (buffer, NULL);
|
||||
g_return_val_if_fail (charset, NULL );
|
||||
|
||||
err = NULL;
|
||||
utf8 = g_convert_with_fallback (buffer, -1, "UTF-8",
|
||||
charset, NULL,
|
||||
NULL, NULL, &err);
|
||||
if (!utf8) /* maybe the charset lied; try 8859-15 */
|
||||
utf8 = g_convert_with_fallback (buffer, -1, "UTF-8",
|
||||
"ISO8859-15", NULL,
|
||||
NULL, NULL, &err);
|
||||
/* final attempt, maybe it was utf-8 already */
|
||||
if (!utf8 && g_utf8_validate (buffer, -1, NULL))
|
||||
utf8 = g_strdup (buffer);
|
||||
|
||||
if (!utf8) {
|
||||
g_warning ("%s: conversion failed from %s: %s",
|
||||
__func__, charset, err ? err->message : "");
|
||||
}
|
||||
|
||||
g_clear_error (&err);
|
||||
|
||||
return utf8;
|
||||
}
|
||||
|
||||
|
||||
gchar*
|
||||
mu_str_quoted_from_strv (const gchar **params)
|
||||
{
|
||||
GString *str;
|
||||
int i;
|
||||
|
||||
g_return_val_if_fail (params, NULL);
|
||||
|
||||
if (!params[0])
|
||||
return g_strdup ("");
|
||||
|
||||
str = g_string_sized_new (64); /* just a guess */
|
||||
|
||||
for (i = 0; params[i]; ++i) {
|
||||
|
||||
if (i > 0)
|
||||
g_string_append_c (str, ' ');
|
||||
|
||||
g_string_append_c (str, '"');
|
||||
g_string_append (str, params[i]);
|
||||
g_string_append_c (str, '"');
|
||||
}
|
||||
|
||||
return g_string_free (str, FALSE);
|
||||
}
|
||||
|
||||
|
||||
static char*
|
||||
read_key (const char *str, const char **val, GError **err)
|
||||
{
|
||||
const char *cur;
|
||||
GString *gstr;
|
||||
|
||||
cur = str;
|
||||
|
||||
gstr = g_string_sized_new (strlen(cur));
|
||||
while (*cur && *cur != ':') {
|
||||
g_string_append_c (gstr, *cur);
|
||||
++cur;
|
||||
}
|
||||
|
||||
if (*cur != ':' || gstr->len == 0) {
|
||||
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR,
|
||||
"expected: '<alphanum>+:' (%s)",
|
||||
str);
|
||||
g_string_free (gstr, TRUE);
|
||||
*val = NULL;
|
||||
return NULL;
|
||||
} else {
|
||||
*val = cur + 1;
|
||||
return g_string_free (gstr, FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static char*
|
||||
read_val (const char *str, const char **endval, GError **err)
|
||||
{
|
||||
const char *cur;
|
||||
gboolean quoted;
|
||||
GString *gstr;
|
||||
|
||||
gstr = g_string_sized_new (strlen(str));
|
||||
|
||||
for (quoted = FALSE, cur = str; *cur; ++cur) {
|
||||
|
||||
if (*cur == '\\') {
|
||||
if (cur[1] != '"' && cur[1] != '\\') {
|
||||
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR,
|
||||
"invalid escaping");
|
||||
goto errexit;
|
||||
} else {
|
||||
++cur;
|
||||
g_string_append_c (gstr, *cur);
|
||||
continue;
|
||||
}
|
||||
} else if (*cur == '"') {
|
||||
quoted = !quoted;
|
||||
continue;
|
||||
} else if (isblank(*cur) && !quoted)
|
||||
break;
|
||||
else
|
||||
g_string_append_c (gstr, *cur);
|
||||
}
|
||||
|
||||
if (quoted) {
|
||||
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR,
|
||||
"error in quoting");
|
||||
goto errexit;
|
||||
}
|
||||
|
||||
*endval = cur;
|
||||
return g_string_free (gstr, FALSE);
|
||||
|
||||
errexit:
|
||||
g_string_free (gstr, TRUE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
GHashTable*
|
||||
mu_str_parse_arglist (const char *args, GError **err)
|
||||
{
|
||||
GHashTable *hash;
|
||||
const char *cur;
|
||||
|
||||
g_return_val_if_fail (args, NULL);
|
||||
|
||||
hash = g_hash_table_new_full (
|
||||
g_str_hash,
|
||||
g_str_equal,
|
||||
(GDestroyNotify)g_free,
|
||||
(GDestroyNotify)g_free);
|
||||
|
||||
cur = args;
|
||||
while ((isblank(*cur)))
|
||||
++cur;
|
||||
|
||||
do {
|
||||
char *key, *val;
|
||||
const char *valstart, *valend;
|
||||
|
||||
key = read_key (cur, &valstart, err);
|
||||
if (!key)
|
||||
goto errexit;
|
||||
|
||||
val = read_val (valstart, &valend, err);
|
||||
if (!val)
|
||||
goto errexit;
|
||||
|
||||
/* g_print ("%s->%s\n", key, val); */
|
||||
g_hash_table_insert (hash, key, val);
|
||||
|
||||
cur = valend;
|
||||
while ((isblank(*cur)))
|
||||
++cur;
|
||||
} while (*cur);
|
||||
|
||||
return hash;
|
||||
|
||||
errexit:
|
||||
g_hash_table_destroy (hash);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
mu_str_remove_ctrl_in_place (char *str)
|
||||
{
|
||||
char *orig, *cur;
|
||||
|
||||
g_return_val_if_fail (str, NULL);
|
||||
|
||||
orig = str;
|
||||
|
||||
for (cur = orig; *cur; ++cur) {
|
||||
if (isspace(*cur)) {
|
||||
/* squash special white space into a simple space */
|
||||
*orig++ = ' ';
|
||||
} else if (iscntrl(*cur)) {
|
||||
/* eat it */
|
||||
} else
|
||||
*orig++ = *cur;
|
||||
}
|
||||
|
||||
*orig = '\0'; /* ensure the updated string has a NULL */
|
||||
|
||||
return str;
|
||||
}
|
||||
277
lib/utils/mu-str.h
Normal file
277
lib/utils/mu-str.h
Normal file
@ -0,0 +1,277 @@
|
||||
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
||||
|
||||
/*
|
||||
** Copyright (C) 2008-2017 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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 of the License, 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#ifndef __MU_STR_H__
|
||||
#define __MU_STR_H__
|
||||
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <mu-msg.h>
|
||||
#include <mu-flags.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
/**
|
||||
* @addtogroup MuStr
|
||||
* Various string utilities
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* create a 'display contact' from an email header To/Cc/Bcc/From-type address
|
||||
* ie., turn
|
||||
* "Foo Bar" <foo@bar.com>
|
||||
* into
|
||||
* Foo Bar
|
||||
* Note that this is based on some simple heuristics. Max length is 255 bytes.
|
||||
*
|
||||
* mu_str_display_contact_s returns a statically allocated
|
||||
* buffer (ie, non-reentrant), while mu_str_display_contact
|
||||
* returns a newly allocated string that you must free with g_free
|
||||
* when done with it.
|
||||
*
|
||||
* @param str a 'contact str' (ie., what is in the To/Cc/Bcc/From
|
||||
* fields), or NULL
|
||||
*
|
||||
* @return a newly allocated string with a display contact
|
||||
*/
|
||||
const char* mu_str_display_contact_s (const char *str) G_GNUC_CONST;
|
||||
char *mu_str_display_contact (const char *str) G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
|
||||
/**
|
||||
* get a display size for a given size_t; uses M for sizes >
|
||||
* 1000*1000, k for smaller sizes. Note: this function use the
|
||||
* 10-based SI units, _not_ the powers-of-2 based ones.
|
||||
*
|
||||
* mu_str_size_s returns a ptr to a static buffer,
|
||||
* while mu_str_size returns dynamically allocated
|
||||
* memory that must be freed after use.
|
||||
*
|
||||
* @param t the size as an size_t
|
||||
*
|
||||
* @return a string representation of the size; see above
|
||||
* for what to do with it
|
||||
*/
|
||||
const char* mu_str_size_s (size_t s) G_GNUC_CONST;
|
||||
char* mu_str_size (size_t s) G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
|
||||
/**
|
||||
* Replace all occurrences of substr in str with repl
|
||||
*
|
||||
* @param str a string
|
||||
* @param substr some string to replace
|
||||
* @param repl a replacement string
|
||||
*
|
||||
* @return a newly allocated string with the substr replaced by repl; free with g_free
|
||||
*/
|
||||
char *mu_str_replace (const char *str, const char *substr, const char *repl);
|
||||
|
||||
|
||||
/**
|
||||
* get a display string for a given set of flags, OR'ed in
|
||||
* @param flags; one character per flag:
|
||||
* D=draft,F=flagged,N=new,P=passed,R=replied,S=seen,T=trashed
|
||||
* a=has-attachment,s=signed, x=encrypted
|
||||
*
|
||||
* mu_str_file_flags_s returns a ptr to a static buffer,
|
||||
* while mu_str_file_flags returns dynamically allocated
|
||||
* memory that must be freed after use.
|
||||
*
|
||||
* @param flags file flags
|
||||
*
|
||||
* @return a string representation of the flags; see above
|
||||
* for what to do with it
|
||||
*/
|
||||
const char* mu_str_flags_s (MuFlags flags) G_GNUC_CONST;
|
||||
char* mu_str_flags (MuFlags flags)
|
||||
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* get a 'summary' of the string, ie. the first /n/ lines of the
|
||||
* strings, with all newlines removed, replaced by single spaces
|
||||
*
|
||||
* @param str the source string
|
||||
* @param max_lines the maximum number of lines to include in the summary
|
||||
*
|
||||
* @return a newly allocated string with the summary. use g_free to free it.
|
||||
*/
|
||||
char* mu_str_summarize (const char* str, size_t max_lines)
|
||||
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* create a full path from a path + a filename. function is _not_
|
||||
* reentrant.
|
||||
*
|
||||
* @param path a path (!= NULL)
|
||||
* @param name a name (may be NULL)
|
||||
*
|
||||
* @return the path as a statically allocated buffer. don't free.
|
||||
*/
|
||||
const char* mu_str_fullpath_s (const char* path, const char* name);
|
||||
|
||||
/**
|
||||
* escape a string like a string literal in C; ie. replace \ with \\,
|
||||
* and " with \"
|
||||
*
|
||||
* @param str a non-NULL str
|
||||
* @param in_quotes whether the result should be enclosed in ""
|
||||
*
|
||||
* @return the escaped string, newly allocated (free with g_free)
|
||||
*/
|
||||
char* mu_str_escape_c_literal (const gchar* str, gboolean in_quotes)
|
||||
G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* turn a string into plain ascii by replacing each non-ascii
|
||||
* character with a dot ('.'). Replacement is done in-place.
|
||||
*
|
||||
* @param buf a buffer to asciify
|
||||
*
|
||||
* @return the buf ptr (as to allow for function composition)
|
||||
*/
|
||||
char* mu_str_asciify_in_place (char *buf);
|
||||
|
||||
/**
|
||||
* turn string in buf into valid utf8. If this string is not valid
|
||||
* utf8 already, the function massages the offending characters.
|
||||
*
|
||||
* @param buf a buffer to utf8ify
|
||||
*
|
||||
* @return a newly allocated utf8 string
|
||||
*/
|
||||
char* mu_str_utf8ify (const char *buf);
|
||||
|
||||
/**
|
||||
* convert a string in a certain charset into utf8
|
||||
*
|
||||
* @param buffer a buffer to convert
|
||||
* @param charset source character set.
|
||||
*
|
||||
* @return a UTF8 string (which you need to g_free when done with it),
|
||||
* or NULL in case of error
|
||||
*/
|
||||
gchar* mu_str_convert_to_utf8 (const char* buffer, const char *charset);
|
||||
|
||||
|
||||
/**
|
||||
* macro to check whether the string is empty, ie. if it's NULL or
|
||||
* it's length is 0
|
||||
*
|
||||
* @param S a string
|
||||
*
|
||||
* @return TRUE if the string is empty, FALSE otherwise
|
||||
*/
|
||||
#define mu_str_is_empty(S) ((!(S)||!(*S))?TRUE:FALSE)
|
||||
|
||||
/**
|
||||
* convert a GSList of strings to a #sepa-separated list
|
||||
*
|
||||
* @param lst a GSList
|
||||
* @param the separator character
|
||||
*
|
||||
* @return a newly allocated string
|
||||
*/
|
||||
char* mu_str_from_list (const GSList *lst, char sepa);
|
||||
|
||||
/**
|
||||
* convert a #sepa-separated list of strings in to a GSList
|
||||
*
|
||||
* @param str a #sepa-separated list of strings
|
||||
* @param the separator character
|
||||
* @param remove leading/trailing whitespace from the string
|
||||
*
|
||||
* @return a newly allocated GSList (free with mu_str_free_list)
|
||||
*/
|
||||
GSList* mu_str_to_list (const char *str, char sepa, gboolean strip);
|
||||
|
||||
/**
|
||||
* convert a string (with possible escaping) to a list. list items are
|
||||
* separated by one or more spaces. list items can be quoted (using
|
||||
* '"').
|
||||
*
|
||||
* @param str a string
|
||||
*
|
||||
* @return a list of elements or NULL in case of error, free with
|
||||
* mu_str_free_list
|
||||
*/
|
||||
GSList* mu_str_esc_to_list (const char *str);
|
||||
|
||||
/**
|
||||
* Parse a list of <key>:<value> arguments, where <value> supports
|
||||
* quoting and escaping.
|
||||
*
|
||||
* @param args a list of arguments
|
||||
* @param err receives error information
|
||||
*
|
||||
* @return a hash table with key->value, or NULL in case of
|
||||
* error. Free with g_hash_table_destroy.
|
||||
*/
|
||||
GHashTable* mu_str_parse_arglist (const char *args, GError **err)
|
||||
G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* free a GSList consisting of allocated strings
|
||||
*
|
||||
* @param lst a GSList
|
||||
*/
|
||||
void mu_str_free_list (GSList *lst);
|
||||
|
||||
/**
|
||||
* strip the subject of Re:, Fwd: etc.
|
||||
*
|
||||
* @param str a subject string
|
||||
*
|
||||
* @return a new string -- this is pointing somewhere inside the @str;
|
||||
* no copy is made, don't free
|
||||
*/
|
||||
const gchar* mu_str_subject_normalize (const gchar* str);
|
||||
|
||||
|
||||
/**
|
||||
* take a list of strings, and return the concatenation of their
|
||||
* quoted forms
|
||||
*
|
||||
* @param params NULL-terminated array of strings
|
||||
*
|
||||
* @return the quoted concatenation of the strings
|
||||
*/
|
||||
gchar* mu_str_quoted_from_strv (const gchar **params);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Remove control characters from a string
|
||||
*
|
||||
* @param str a string
|
||||
*
|
||||
* @return the str with control characters removed
|
||||
*/
|
||||
char* mu_str_remove_ctrl_in_place (char *str);
|
||||
|
||||
|
||||
/** @} */
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /*__MU_STR_H__*/
|
||||
495
lib/utils/mu-util.c
Normal file
495
lib/utils/mu-util.c
Normal file
@ -0,0 +1,495 @@
|
||||
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
||||
/*
|
||||
**
|
||||
** Copyright (C) 2008-2016 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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 of the License, 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif /*HAVE_CONFIG_H*/
|
||||
|
||||
#include "mu-util.h"
|
||||
#define _XOPEN_SOURCE 500
|
||||
|
||||
#ifdef HAVE_WORDEXP_H
|
||||
#include <wordexp.h> /* for shell-style globbing */
|
||||
#endif /*HAVE_WORDEXP_H*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <locale.h> /* for setlocale() */
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <glib-object.h>
|
||||
#include <glib/gstdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <langinfo.h>
|
||||
|
||||
|
||||
static char*
|
||||
do_wordexp (const char *path)
|
||||
{
|
||||
#ifdef HAVE_WORDEXP_H
|
||||
wordexp_t wexp;
|
||||
char *dir;
|
||||
|
||||
if (!path) {
|
||||
/* g_debug ("%s: path is empty", __func__); */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (wordexp (path, &wexp, 0) != 0) {
|
||||
/* g_debug ("%s: expansion failed for %s", __func__, path); */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* we just pick the first one */
|
||||
dir = g_strdup (wexp.we_wordv[0]);
|
||||
|
||||
/* strangely, below seems to lead to a crash on MacOS (BSD);
|
||||
so we have to allow for a tiny leak here on that
|
||||
platform... maybe instead of __APPLE__ it should be
|
||||
__BSD__?
|
||||
|
||||
Hmmm., cannot reproduce that crash anymore, so commenting
|
||||
it out for now...
|
||||
*/
|
||||
/* #ifndef __APPLE__ */
|
||||
wordfree (&wexp);
|
||||
/* #endif /\*__APPLE__*\/ */
|
||||
return dir;
|
||||
|
||||
# else /*!HAVE_WORDEXP_H*/
|
||||
/* E.g. OpenBSD does not have wordexp.h, so we ignore it */
|
||||
return path ? g_strdup (path) : NULL;
|
||||
#endif /*HAVE_WORDEXP_H*/
|
||||
}
|
||||
|
||||
|
||||
/* note, the g_debugs are commented out because this function may be
|
||||
* called before the log handler is installed. */
|
||||
char*
|
||||
mu_util_dir_expand (const char *path)
|
||||
{
|
||||
char *dir;
|
||||
char resolved[PATH_MAX + 1];
|
||||
|
||||
g_return_val_if_fail (path, NULL);
|
||||
|
||||
dir = do_wordexp (path);
|
||||
if (!dir)
|
||||
return NULL; /* error */
|
||||
|
||||
/* don't try realpath if the dir does not exist */
|
||||
if (access (dir, F_OK) != 0)
|
||||
return dir;
|
||||
|
||||
/* now resolve any symlinks, .. etc. */
|
||||
if (realpath (dir, resolved) == NULL) {
|
||||
/* g_debug ("%s: could not get realpath for '%s': %s", */
|
||||
/* __func__, dir, strerror(errno)); */
|
||||
g_free (dir);
|
||||
return NULL;
|
||||
} else
|
||||
g_free (dir);
|
||||
|
||||
return g_strdup (resolved);
|
||||
}
|
||||
|
||||
GQuark
|
||||
mu_util_error_quark (void)
|
||||
{
|
||||
static GQuark error_domain = 0;
|
||||
|
||||
if (G_UNLIKELY(error_domain == 0))
|
||||
error_domain = g_quark_from_static_string
|
||||
("mu-error-quark");
|
||||
|
||||
return error_domain;
|
||||
}
|
||||
|
||||
|
||||
const char*
|
||||
mu_util_cache_dir (void)
|
||||
{
|
||||
static char cachedir [PATH_MAX];
|
||||
|
||||
snprintf (cachedir, sizeof(cachedir), "%s%cmu-%u",
|
||||
g_get_tmp_dir(), G_DIR_SEPARATOR,
|
||||
getuid());
|
||||
|
||||
return cachedir;
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
mu_util_check_dir (const gchar* path, gboolean readable, gboolean writeable)
|
||||
{
|
||||
int mode;
|
||||
struct stat statbuf;
|
||||
|
||||
if (!path)
|
||||
return FALSE;
|
||||
|
||||
mode = F_OK | (readable ? R_OK : 0) | (writeable ? W_OK : 0);
|
||||
|
||||
if (access (path, mode) != 0) {
|
||||
/* g_debug ("Cannot access %s: %s", path, strerror (errno)); */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (stat (path, &statbuf) != 0) {
|
||||
/* g_debug ("Cannot stat %s: %s", path, strerror (errno)); */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return S_ISDIR(statbuf.st_mode) ? TRUE: FALSE;
|
||||
}
|
||||
|
||||
|
||||
gchar*
|
||||
mu_util_guess_maildir (void)
|
||||
{
|
||||
const gchar *mdir1, *home;
|
||||
|
||||
/* first, try MAILDIR */
|
||||
mdir1 = g_getenv ("MAILDIR");
|
||||
|
||||
if (mdir1 && mu_util_check_dir (mdir1, TRUE, FALSE))
|
||||
return g_strdup (mdir1);
|
||||
|
||||
/* then, try <home>/Maildir */
|
||||
home = g_get_home_dir();
|
||||
if (home) {
|
||||
char *mdir2;
|
||||
mdir2 = g_strdup_printf ("%s%cMaildir",
|
||||
home, G_DIR_SEPARATOR);
|
||||
if (mu_util_check_dir (mdir2, TRUE, FALSE))
|
||||
return mdir2;
|
||||
g_free (mdir2);
|
||||
}
|
||||
|
||||
/* nope; nothing found */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gboolean
|
||||
mu_util_create_dir_maybe (const gchar *path, mode_t mode, gboolean nowarn)
|
||||
{
|
||||
struct stat statbuf;
|
||||
|
||||
g_return_val_if_fail (path, FALSE);
|
||||
|
||||
/* if it exists, it must be a readable dir */
|
||||
if (stat (path, &statbuf) == 0) {
|
||||
if ((!S_ISDIR(statbuf.st_mode)) ||
|
||||
(access (path, W_OK|R_OK) != 0)) {
|
||||
if (!nowarn)
|
||||
g_warning ("not a read-writable"
|
||||
"directory: %s", path);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
if (g_mkdir_with_parents (path, mode) != 0) {
|
||||
if (!nowarn)
|
||||
g_warning ("failed to create %s: %s",
|
||||
path, strerror(errno));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
mu_util_create_writeable_fd (const char* path, mode_t mode,
|
||||
gboolean overwrite)
|
||||
{
|
||||
errno = 0; /* clear! */
|
||||
g_return_val_if_fail (path, -1);
|
||||
|
||||
if (overwrite)
|
||||
return open (path, O_WRONLY|O_CREAT|O_TRUNC, mode);
|
||||
else
|
||||
return open (path, O_WRONLY|O_CREAT|O_EXCL, mode);
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
mu_util_is_local_file (const char* path)
|
||||
{
|
||||
/* if it starts with file:// it's a local file (for the
|
||||
* purposes of this function -- if it's on a remote FS it's
|
||||
* still considered local) */
|
||||
if (g_ascii_strncasecmp ("file://", path, strlen("file://")) == 0)
|
||||
return TRUE;
|
||||
|
||||
if (access (path, R_OK) == 0)
|
||||
return TRUE;
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
mu_util_supports (MuFeature feature)
|
||||
{
|
||||
|
||||
/* check for Guile support */
|
||||
#ifndef BUILD_GUILE
|
||||
if (feature & MU_FEATURE_GUILE)
|
||||
return FALSE;
|
||||
#endif /*BUILD_GUILE*/
|
||||
|
||||
/* check for Gnuplot */
|
||||
if (feature & MU_FEATURE_GNUPLOT)
|
||||
if (!mu_util_program_in_path ("gnuplot"))
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
mu_util_program_in_path (const char *prog)
|
||||
{
|
||||
gchar *path;
|
||||
|
||||
g_return_val_if_fail (prog, FALSE);
|
||||
|
||||
path = g_find_program_in_path (prog);
|
||||
g_free (path);
|
||||
|
||||
return (path != NULL) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Set the child to a group leader to avoid being killed when the
|
||||
* parent group is killed.
|
||||
*/
|
||||
static void
|
||||
maybe_setsid (G_GNUC_UNUSED gpointer user_data)
|
||||
{
|
||||
#if HAVE_SETSID
|
||||
setsid();
|
||||
#endif /*HAVE_SETSID*/
|
||||
}
|
||||
|
||||
gboolean
|
||||
mu_util_play (const char *path, gboolean allow_local, gboolean allow_remote,
|
||||
GError **err)
|
||||
{
|
||||
gboolean rv;
|
||||
const gchar *argv[3];
|
||||
const char *prog;
|
||||
|
||||
g_return_val_if_fail (path, FALSE);
|
||||
g_return_val_if_fail (mu_util_is_local_file (path) || allow_remote,
|
||||
FALSE);
|
||||
g_return_val_if_fail (!mu_util_is_local_file (path) || allow_local,
|
||||
FALSE);
|
||||
|
||||
prog = g_getenv ("MU_PLAY_PROGRAM");
|
||||
if (!prog) {
|
||||
#ifdef __APPLE__
|
||||
prog = "open";
|
||||
#else
|
||||
prog = "xdg-open";
|
||||
#endif /*!__APPLE__*/
|
||||
}
|
||||
|
||||
if (!mu_util_program_in_path (prog)) {
|
||||
mu_util_g_set_error (err, MU_ERROR_FILE_CANNOT_EXECUTE,
|
||||
"cannot find '%s' in path", prog);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
argv[0] = prog;
|
||||
argv[1] = path;
|
||||
argv[2] = NULL;
|
||||
|
||||
err = NULL;
|
||||
rv = g_spawn_async (NULL, (gchar**)&argv, NULL,
|
||||
G_SPAWN_SEARCH_PATH, maybe_setsid,
|
||||
NULL, NULL, err);
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
unsigned char
|
||||
mu_util_get_dtype_with_lstat (const char *path)
|
||||
{
|
||||
struct stat statbuf;
|
||||
|
||||
g_return_val_if_fail (path, DT_UNKNOWN);
|
||||
|
||||
if (lstat (path, &statbuf) != 0) {
|
||||
g_warning ("stat failed on %s: %s", path, strerror(errno));
|
||||
return DT_UNKNOWN;
|
||||
}
|
||||
|
||||
/* we only care about dirs, regular files and links */
|
||||
if (S_ISREG (statbuf.st_mode))
|
||||
return DT_REG;
|
||||
else if (S_ISDIR (statbuf.st_mode))
|
||||
return DT_DIR;
|
||||
else if (S_ISLNK (statbuf.st_mode))
|
||||
return DT_LNK;
|
||||
|
||||
return DT_UNKNOWN;
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
mu_util_locale_is_utf8 (void)
|
||||
{
|
||||
const gchar *dummy;
|
||||
static int is_utf8 = -1;
|
||||
|
||||
if (G_UNLIKELY(is_utf8 == -1))
|
||||
is_utf8 = g_get_charset(&dummy) ? 1 : 0;
|
||||
|
||||
return is_utf8 ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
mu_util_fputs_encoded (const char *str, FILE *stream)
|
||||
{
|
||||
int rv;
|
||||
char *conv;
|
||||
|
||||
g_return_val_if_fail (stream, FALSE);
|
||||
|
||||
/* g_get_charset return TRUE when the locale is UTF8 */
|
||||
if (mu_util_locale_is_utf8())
|
||||
return fputs (str, stream) == EOF ? FALSE : TRUE;
|
||||
|
||||
/* charset is _not_ utf8, so we need to convert it */
|
||||
conv = NULL;
|
||||
if (g_utf8_validate (str, -1, NULL))
|
||||
conv = g_locale_from_utf8 (str, -1, NULL, NULL, NULL);
|
||||
|
||||
/* conversion failed; this happens because is some cases GMime may gives
|
||||
* us non-UTF-8 strings from e.g. wrongly encoded message-subjects; if
|
||||
* so, we escape the string */
|
||||
conv = conv ? conv : g_strescape (str, "\n\t");
|
||||
rv = conv ? fputs (conv, stream) : EOF;
|
||||
g_free (conv);
|
||||
|
||||
return (rv == EOF) ? FALSE : TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
gboolean
|
||||
mu_util_g_set_error (GError **err, MuError errcode, const char *frm, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *msg;
|
||||
|
||||
/* don't bother with NULL errors, or errors already set */
|
||||
if (!err || *err)
|
||||
return FALSE;
|
||||
|
||||
msg = NULL;
|
||||
va_start (ap, frm);
|
||||
g_vasprintf (&msg, frm, ap);
|
||||
va_end (ap);
|
||||
|
||||
g_set_error (err, MU_ERROR_DOMAIN, errcode, "%s", msg);
|
||||
|
||||
g_free (msg);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static gboolean
|
||||
print_args (FILE *stream, const char *frm, va_list args)
|
||||
{
|
||||
gchar *str;
|
||||
gboolean rv;
|
||||
|
||||
str = g_strdup_vprintf (frm, args);
|
||||
|
||||
rv = mu_util_fputs_encoded (str, stream);
|
||||
|
||||
g_free (str);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
mu_util_print_encoded (const char *frm, ...)
|
||||
{
|
||||
va_list args;
|
||||
gboolean rv;
|
||||
|
||||
g_return_val_if_fail (frm, FALSE);
|
||||
|
||||
va_start (args, frm);
|
||||
rv = print_args (stdout, frm, args);
|
||||
va_end (args);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
gboolean
|
||||
mu_util_printerr_encoded (const char *frm, ...)
|
||||
{
|
||||
va_list args;
|
||||
gboolean rv;
|
||||
|
||||
g_return_val_if_fail (frm, FALSE);
|
||||
|
||||
va_start (args, frm);
|
||||
rv = print_args (stderr, frm, args);
|
||||
va_end (args);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
mu_util_read_password (const char *prompt)
|
||||
{
|
||||
char *pass;
|
||||
|
||||
g_return_val_if_fail (prompt, NULL);
|
||||
|
||||
/* note: getpass is obsolete; replace with something better */
|
||||
|
||||
pass = getpass (prompt); /* returns static mem, don't free */
|
||||
if (!pass) {
|
||||
if (errno)
|
||||
g_warning ("error: %s", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return g_strdup (pass);
|
||||
}
|
||||
520
lib/utils/mu-util.h
Normal file
520
lib/utils/mu-util.h
Normal file
@ -0,0 +1,520 @@
|
||||
/*
|
||||
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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 of the License, 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#ifndef __MU_UTIL_H__
|
||||
#define __MU_UTIL_H__
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdio.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h> /* for mode_t */
|
||||
|
||||
/* hopefully, this should get us a sane PATH_MAX */
|
||||
#include <limits.h>
|
||||
/* not all systems provide PATH_MAX in limits.h */
|
||||
#ifndef PATH_MAX
|
||||
#include <sys/param.h>
|
||||
#ifndef PATH_MAX
|
||||
#define PATH_MAX MAXPATHLEN
|
||||
#endif /*!PATH_MAX*/
|
||||
#endif /*PATH_MAX*/
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
/**
|
||||
* get the expanded path; ie. perform shell expansion on the path. the
|
||||
* path does not have to exist
|
||||
*
|
||||
* @param path path to expand
|
||||
*
|
||||
* @return the expanded path as a newly allocated string, or NULL in
|
||||
* case of error
|
||||
*/
|
||||
char* mu_util_dir_expand (const char* path)
|
||||
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* guess the maildir; first try $MAILDIR; if it is unset or
|
||||
* non-existant, try ~/Maildir if both fail, return NULL
|
||||
*
|
||||
* @return full path of the guessed Maildir, or NULL; must be freed (gfree)
|
||||
*/
|
||||
char* mu_util_guess_maildir (void)
|
||||
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* if path exists, check that's a read/writeable dir; otherwise try to
|
||||
* create it (with perms 0700)
|
||||
*
|
||||
* @param path path to the dir
|
||||
* @param mode to set for the dir (as per chmod(1))
|
||||
* @param nowarn, if TRUE, don't write warnings (if any) to stderr
|
||||
*
|
||||
* @return TRUE if a read/writeable directory `path' exists after
|
||||
* leaving this function, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_util_create_dir_maybe (const gchar *path, mode_t mode,
|
||||
gboolean nowarn) G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* check whether path is a directory, and optionally, if it's readable
|
||||
* and/or writeable
|
||||
*
|
||||
* @param path dir path
|
||||
* @param readable check for readability
|
||||
* @param writeable check for writability
|
||||
*
|
||||
* @return TRUE if dir exist and has the specified properties
|
||||
*/
|
||||
gboolean mu_util_check_dir (const gchar* path, gboolean readable,
|
||||
gboolean writeable)
|
||||
G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
|
||||
/**
|
||||
* get our the cache directory, typically, /tmp/mu-<userid>/
|
||||
*
|
||||
* @return the cache directory; don't free
|
||||
*/
|
||||
const char* mu_util_cache_dir (void) G_GNUC_CONST;
|
||||
|
||||
/**
|
||||
* create a writeable file and return its file descriptor (which
|
||||
* you'll need to close(2) when done with it.)
|
||||
*
|
||||
* @param path the full path of the file to create
|
||||
* @param the mode to open (ie. 0644 or 0600 etc., see chmod(3)
|
||||
* @param overwrite should we allow for overwriting existing files?
|
||||
*
|
||||
* @return a file descriptor, or -1 in case of error. If it's a file
|
||||
* system error, 'errno' may contain more info. use 'close()' when done
|
||||
* with the file descriptor
|
||||
*/
|
||||
int mu_util_create_writeable_fd (const char* path, mode_t mode,
|
||||
gboolean overwrite)
|
||||
G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
|
||||
/**
|
||||
* check if file is local, ie. on the local file system. this means
|
||||
* that it's either having a file URI, *or* that it's an existing file
|
||||
*
|
||||
* @param path a path
|
||||
*
|
||||
* @return TRUE if the file is local, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_util_is_local_file (const char* path);
|
||||
|
||||
|
||||
/**
|
||||
* is the current locale utf-8 compatible?
|
||||
*
|
||||
* @return TRUE if it's utf8 compatible, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_util_locale_is_utf8 (void) G_GNUC_CONST;
|
||||
|
||||
/**
|
||||
* write a string (assumed to be in utf8-format) to a stream,
|
||||
* converted to the current locale
|
||||
*
|
||||
* @param str a string
|
||||
* @param stream a stream
|
||||
*
|
||||
* @return TRUE if printing worked, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_util_fputs_encoded (const char *str, FILE *stream);
|
||||
|
||||
/**
|
||||
* print a formatted string (assumed to be in utf8-format) to stdout,
|
||||
* converted to the current locale
|
||||
*
|
||||
* @param a standard printf() format string, followed by a parameter list
|
||||
*
|
||||
* @return TRUE if printing worked, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_util_print_encoded (const char *frm, ...) G_GNUC_PRINTF(1,2);
|
||||
|
||||
/**
|
||||
* print a formatted string (assumed to be in utf8-format) to stderr,
|
||||
* converted to the current locale
|
||||
*
|
||||
* @param a standard printf() format string, followed by a parameter list
|
||||
*
|
||||
* @return TRUE if printing worked, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_util_printerr_encoded (const char *frm, ...) G_GNUC_PRINTF(1,2);
|
||||
|
||||
|
||||
/**
|
||||
* read a password from stdin (without echoing), and return it.
|
||||
*
|
||||
* @param prompt the prompt text before the password
|
||||
*
|
||||
* @return the password (free with g_free), or NULL
|
||||
*/
|
||||
char* mu_util_read_password (const char *prompt)
|
||||
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
* Try to 'play' (ie., open with it's associated program) a file. On
|
||||
* MacOS, the the program 'open' is used for this; on other platforms
|
||||
* 'xdg-open' to do the actual opening. In addition you can set it to another program
|
||||
* by setting the MU_PLAY_PROGRAM environment variable
|
||||
*
|
||||
* @param path full path of the file to open
|
||||
* @param allow_local allow local files (ie. with file:// prefix or fs paths)
|
||||
* @param allow_remote allow URIs (ie., http, mailto)
|
||||
* @param err receives error information, if any
|
||||
*
|
||||
* @return TRUE if it succeeded, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_util_play (const char *path, gboolean allow_local,
|
||||
gboolean allow_remote, GError **err);
|
||||
|
||||
/**
|
||||
* Check whether program prog exists in PATH
|
||||
*
|
||||
* @param prog a program (executable)
|
||||
*
|
||||
* @return TRUE if it exists and is executable, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_util_program_in_path (const char *prog);
|
||||
|
||||
|
||||
|
||||
enum _MuFeature {
|
||||
MU_FEATURE_GUILE = 1 << 0, /* do we support Guile 2.0? */
|
||||
MU_FEATURE_GNUPLOT = 1 << 1, /* do we have gnuplot installed? */
|
||||
MU_FEATURE_CRYPTO = 1 << 2 /* do we support crypto (Gmime >= 2.6) */
|
||||
};
|
||||
typedef enum _MuFeature MuFeature;
|
||||
|
||||
|
||||
/**
|
||||
* Check whether mu supports some particular feature
|
||||
*
|
||||
* @param feature a feature (multiple features can be logical-or'd together)
|
||||
*
|
||||
* @return TRUE if the feature is supported, FALSE otherwise
|
||||
*/
|
||||
gboolean mu_util_supports (MuFeature feature);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get an error-query for mu, to be used in `g_set_error'. Recent
|
||||
* version of Glib warn when using 0 for the error-domain in
|
||||
* g_set_error.
|
||||
*
|
||||
*
|
||||
* @return an error quark for mu
|
||||
*/
|
||||
GQuark mu_util_error_quark (void) G_GNUC_CONST;
|
||||
#define MU_ERROR_DOMAIN (mu_util_error_quark())
|
||||
|
||||
|
||||
/*
|
||||
* for OSs with out support for direntry->d_type, like Solaris
|
||||
*/
|
||||
#ifndef DT_UNKNOWN
|
||||
enum {
|
||||
DT_UNKNOWN = 0,
|
||||
#define DT_UNKNOWN DT_UNKNOWN
|
||||
DT_FIFO = 1,
|
||||
#define DT_FIFO DT_FIFO
|
||||
DT_CHR = 2,
|
||||
#define DT_CHR DT_CHR
|
||||
DT_DIR = 4,
|
||||
#define DT_DIR DT_DIR
|
||||
DT_BLK = 6,
|
||||
#define DT_BLK DT_BLK
|
||||
DT_REG = 8,
|
||||
#define DT_REG DT_REG
|
||||
DT_LNK = 10,
|
||||
#define DT_LNK DT_LNK
|
||||
DT_SOCK = 12,
|
||||
#define DT_SOCK DT_SOCK
|
||||
DT_WHT = 14
|
||||
#define DT_WHT DT_WHT
|
||||
};
|
||||
#endif /*DT_UNKNOWN*/
|
||||
|
||||
|
||||
/**
|
||||
* get the d_type (as in direntry->d_type) for the file at path, using
|
||||
* lstat(3)
|
||||
*
|
||||
* @param path full path
|
||||
*
|
||||
* @return DT_REG, DT_DIR, DT_LNK, or DT_UNKNOWN (other values are not
|
||||
* supported currently )
|
||||
*/
|
||||
unsigned char mu_util_get_dtype_with_lstat (const char *path);
|
||||
|
||||
|
||||
/**
|
||||
* we need this when using Xapian::Document* from C
|
||||
*
|
||||
*/
|
||||
typedef gpointer XapianDocument;
|
||||
|
||||
/**
|
||||
* we need this when using Xapian::Enquire* from C
|
||||
*
|
||||
*/
|
||||
typedef gpointer XapianEnquire;
|
||||
|
||||
|
||||
/* print a warning for a GError, and free it */
|
||||
#define MU_HANDLE_G_ERROR(GE) \
|
||||
do { \
|
||||
if (!(GE)) \
|
||||
g_warning ("%s:%u: an error occurred in %s", \
|
||||
__FILE__, __LINE__, __func__); \
|
||||
else { \
|
||||
g_warning ("error %u: %s", (GE)->code, (GE)->message); \
|
||||
g_error_free ((GE)); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* don't repeat these catch blocks everywhere...
|
||||
*
|
||||
*/
|
||||
|
||||
#define MU_STORE_CATCH_BLOCK_RETURN(GE,R) \
|
||||
catch (const MuStoreError& merr) { \
|
||||
mu_util_g_set_error ((GE), \
|
||||
merr.mu_error(), "%s", \
|
||||
merr.what().c_str()); \
|
||||
return (R); \
|
||||
} \
|
||||
|
||||
|
||||
#define MU_XAPIAN_CATCH_BLOCK \
|
||||
catch (const Xapian::Error &xerr) { \
|
||||
g_critical ("%s: xapian error '%s'", \
|
||||
__func__, xerr.get_msg().c_str()); \
|
||||
} catch (const std::runtime_error& re) { \
|
||||
g_critical ("%s: error: %s", __func__, re.what()); \
|
||||
} catch (...) { \
|
||||
g_critical ("%s: caught exception", __func__); \
|
||||
}
|
||||
|
||||
#define MU_XAPIAN_CATCH_BLOCK_G_ERROR(GE,E) \
|
||||
catch (const Xapian::DatabaseLockError &xerr) { \
|
||||
mu_util_g_set_error ((GE), \
|
||||
MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK, \
|
||||
"%s: xapian error '%s'", \
|
||||
__func__, xerr.get_msg().c_str()); \
|
||||
} catch (const Xapian::DatabaseError &xerr) { \
|
||||
mu_util_g_set_error ((GE),MU_ERROR_XAPIAN, \
|
||||
"%s: xapian error '%s'", \
|
||||
__func__, xerr.get_msg().c_str()); \
|
||||
} catch (const Xapian::Error &xerr) { \
|
||||
mu_util_g_set_error ((GE),(E), \
|
||||
"%s: xapian error '%s'", \
|
||||
__func__, xerr.get_msg().c_str()); \
|
||||
} catch (const std::runtime_error& ex) { \
|
||||
mu_util_g_set_error ((GE),(MU_ERROR_INTERNAL), \
|
||||
"%s: error: %s", __func__, ex.what()); \
|
||||
\
|
||||
} catch (...) { \
|
||||
mu_util_g_set_error ((GE),(MU_ERROR_INTERNAL), \
|
||||
"%s: caught exception", __func__); \
|
||||
}
|
||||
|
||||
|
||||
#define MU_XAPIAN_CATCH_BLOCK_RETURN(R) \
|
||||
catch (const Xapian::Error &xerr) { \
|
||||
g_critical ("%s: xapian error '%s'", \
|
||||
__func__, xerr.get_msg().c_str()); \
|
||||
return (R); \
|
||||
} catch (const std::runtime_error& ex) { \
|
||||
g_critical("%s: error: %s", __func__, ex.what()); \
|
||||
return (R); \
|
||||
} catch (...) { \
|
||||
g_critical ("%s: caught exception", __func__); \
|
||||
return (R); \
|
||||
}
|
||||
|
||||
#define MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(GE,E,R) \
|
||||
catch (const Xapian::Error &xerr) { \
|
||||
mu_util_g_set_error ((GE),(E), \
|
||||
"%s: xapian error '%s'", \
|
||||
__func__, xerr.get_msg().c_str()); \
|
||||
return (R); \
|
||||
} catch (const std::runtime_error& ex) { \
|
||||
mu_util_g_set_error ((GE),(MU_ERROR_INTERNAL), \
|
||||
"%s: error: %s", __func__, ex.what()); \
|
||||
return (R); \
|
||||
} catch (...) { \
|
||||
if ((GE)&&!(*(GE))) \
|
||||
mu_util_g_set_error ((GE), \
|
||||
(MU_ERROR_INTERNAL), \
|
||||
"%s: caught exception", __func__); \
|
||||
return (R); \
|
||||
}
|
||||
|
||||
/**
|
||||
* log something in the log file; note, we use G_LOG_LEVEL_INFO
|
||||
* for such messages
|
||||
*/
|
||||
#define MU_WRITE_LOG(...) \
|
||||
G_STMT_START { \
|
||||
g_log (G_LOG_DOMAIN, \
|
||||
G_LOG_LEVEL_INFO, \
|
||||
__VA_ARGS__); \
|
||||
} G_STMT_END
|
||||
|
||||
|
||||
|
||||
#define MU_G_ERROR_CODE(GE) ((GE)&&(*(GE))?(*(GE))->code:MU_ERROR)
|
||||
|
||||
|
||||
enum _MuError {
|
||||
/* no error at all! */
|
||||
MU_OK = 0,
|
||||
|
||||
/* generic error */
|
||||
MU_ERROR = 1,
|
||||
MU_ERROR_IN_PARAMETERS = 2,
|
||||
MU_ERROR_INTERNAL = 3,
|
||||
MU_ERROR_NO_MATCHES = 4,
|
||||
|
||||
/* not really an error; for callbacks */
|
||||
MU_IGNORE = 5,
|
||||
|
||||
MU_ERROR_SCRIPT_NOT_FOUND = 8,
|
||||
|
||||
/* general xapian related error */
|
||||
MU_ERROR_XAPIAN = 11,
|
||||
|
||||
/* (parsing) error in the query */
|
||||
MU_ERROR_XAPIAN_QUERY = 13,
|
||||
|
||||
/* missing data for a document */
|
||||
MU_ERROR_XAPIAN_MISSING_DATA = 17,
|
||||
/* can't get write lock */
|
||||
MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK = 19,
|
||||
/* could not write */
|
||||
MU_ERROR_XAPIAN_STORE_FAILED = 21,
|
||||
/* could not remove */
|
||||
MU_ERROR_XAPIAN_REMOVE_FAILED = 22,
|
||||
/* database was modified; reload */
|
||||
MU_ERROR_XAPIAN_MODIFIED = 23,
|
||||
/* database was modified; reload */
|
||||
MU_ERROR_XAPIAN_NEEDS_REINDEX = 24,
|
||||
|
||||
|
||||
/* GMime related errors */
|
||||
|
||||
/* gmime parsing related error */
|
||||
MU_ERROR_GMIME = 30,
|
||||
|
||||
/* contacts related errors */
|
||||
MU_ERROR_CONTACTS = 50,
|
||||
MU_ERROR_CONTACTS_CANNOT_RETRIEVE = 51,
|
||||
|
||||
/* crypto related errors */
|
||||
MU_ERROR_CRYPTO = 60,
|
||||
|
||||
|
||||
/* File errors */
|
||||
/* generic file-related error */
|
||||
MU_ERROR_FILE = 70,
|
||||
MU_ERROR_FILE_INVALID_NAME = 71,
|
||||
MU_ERROR_FILE_CANNOT_LINK = 72,
|
||||
MU_ERROR_FILE_CANNOT_OPEN = 73,
|
||||
MU_ERROR_FILE_CANNOT_READ = 74,
|
||||
MU_ERROR_FILE_CANNOT_EXECUTE = 75,
|
||||
MU_ERROR_FILE_CANNOT_CREATE = 76,
|
||||
MU_ERROR_FILE_CANNOT_MKDIR = 77,
|
||||
MU_ERROR_FILE_STAT_FAILED = 78,
|
||||
MU_ERROR_FILE_READDIR_FAILED = 79,
|
||||
MU_ERROR_FILE_INVALID_SOURCE = 80,
|
||||
MU_ERROR_FILE_TARGET_EQUALS_SOURCE = 81,
|
||||
MU_ERROR_FILE_CANNOT_WRITE = 82,
|
||||
MU_ERROR_FILE_CANNOT_UNLINK = 83,
|
||||
|
||||
/* not really an error, used in callbacks */
|
||||
MU_STOP = 99
|
||||
};
|
||||
typedef enum _MuError MuError;
|
||||
|
||||
|
||||
/**
|
||||
* set an error if it's not already set, and return FALSE
|
||||
*
|
||||
* @param err errptr, or NULL
|
||||
* @param errcode error code
|
||||
* @param frm printf-style format, followed by parameters
|
||||
*
|
||||
* @return FALSE
|
||||
*/
|
||||
gboolean mu_util_g_set_error (GError **err, MuError errcode, const char *frm, ...)
|
||||
G_GNUC_PRINTF(3,4);
|
||||
|
||||
/**
|
||||
* calculate a 64-bit hash for the given string, based on a combination of the
|
||||
* DJB and BKDR hash functions.
|
||||
*
|
||||
* @param a string
|
||||
*
|
||||
* @return the hash
|
||||
*/
|
||||
static inline guint64
|
||||
mu_util_get_hash (const char* str)
|
||||
{
|
||||
guint32 djbhash;
|
||||
guint32 bkdrhash;
|
||||
guint32 bkdrseed;
|
||||
guint64 hash;
|
||||
|
||||
djbhash = 5381;
|
||||
bkdrhash = 0;
|
||||
bkdrseed = 1313;
|
||||
|
||||
for(unsigned u = 0U; str[u]; ++u) {
|
||||
djbhash = ((djbhash << 5) + djbhash) + str[u];
|
||||
bkdrhash = bkdrhash * bkdrseed + str[u];
|
||||
}
|
||||
|
||||
hash = djbhash;
|
||||
return (hash<<32) | bkdrhash;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define MU_COLOR_RED "\x1b[31m"
|
||||
#define MU_COLOR_GREEN "\x1b[32m"
|
||||
#define MU_COLOR_YELLOW "\x1b[33m"
|
||||
#define MU_COLOR_BLUE "\x1b[34m"
|
||||
#define MU_COLOR_MAGENTA "\x1b[35m"
|
||||
#define MU_COLOR_CYAN "\x1b[36m"
|
||||
#define MU_COLOR_DEFAULT "\x1b[0m"
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /*__MU_UTIL_H__*/
|
||||
374
lib/utils/test-mu-str.c
Normal file
374
lib/utils/test-mu-str.c
Normal file
@ -0,0 +1,374 @@
|
||||
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
||||
|
||||
/*
|
||||
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif /*HAVE_CONFIG_H*/
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <locale.h>
|
||||
|
||||
#include "test-mu-common.h"
|
||||
#include "mu-str.h"
|
||||
#include "mu-msg-prio.h"
|
||||
|
||||
|
||||
|
||||
|
||||
static void
|
||||
test_mu_str_size_01 (void)
|
||||
{
|
||||
struct lconv *lc;
|
||||
char *tmp2;
|
||||
|
||||
lc = localeconv();
|
||||
|
||||
g_assert_cmpstr (mu_str_size_s (0), ==, "0 bytes");
|
||||
|
||||
tmp2 = g_strdup_printf ("97%s7 KB", lc->decimal_point);
|
||||
g_assert_cmpstr (mu_str_size_s (100000), ==, tmp2);
|
||||
g_free (tmp2);
|
||||
|
||||
tmp2 = g_strdup_printf ("1%s0 MB", lc->decimal_point);
|
||||
g_assert_cmpstr (mu_str_size_s (1100*1000), ==, tmp2);
|
||||
g_free (tmp2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
test_mu_str_size_02 (void)
|
||||
{
|
||||
struct lconv *lc;
|
||||
char *tmp1, *tmp2;
|
||||
|
||||
lc = localeconv();
|
||||
|
||||
tmp2 = g_strdup_printf ("1%s0 MB", lc->decimal_point);
|
||||
tmp1 = mu_str_size (999999);
|
||||
g_assert_cmpstr (tmp1, !=, tmp2);
|
||||
|
||||
g_free (tmp1);
|
||||
g_free (tmp2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
test_mu_str_prio_01 (void)
|
||||
{
|
||||
g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_LOW), ==, "low");
|
||||
g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_NORMAL), ==, "normal");
|
||||
g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_HIGH), ==, "high");
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
ignore_error (const char* log_domain, GLogLevelFlags log_level,
|
||||
const gchar* msg, gpointer user_data)
|
||||
{
|
||||
return FALSE; /* don't abort */
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_str_prio_02 (void)
|
||||
{
|
||||
/* this must fail */
|
||||
g_test_log_set_fatal_handler ((GTestLogFatalFunc)ignore_error, NULL);
|
||||
g_assert_cmpstr (mu_msg_prio_name(666), ==, NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static void
|
||||
test_parse_arglist (void)
|
||||
{
|
||||
const char *args;
|
||||
GHashTable *hash;
|
||||
GError *err;
|
||||
|
||||
args = "cmd:find query:maildir:\"/sent items\" maxnum:500";
|
||||
|
||||
err = NULL;
|
||||
hash = mu_str_parse_arglist (args, &err);
|
||||
g_assert_no_error (err);
|
||||
g_assert (hash);
|
||||
|
||||
g_assert_cmpstr (g_hash_table_lookup (hash, "cmd"), ==,
|
||||
"find");
|
||||
/* note, mu4e now passes queries as base64 */
|
||||
g_assert_cmpstr (g_hash_table_lookup (hash, "query"), ==,
|
||||
"maildir:/sent items");
|
||||
g_assert_cmpstr (g_hash_table_lookup (hash, "maxnum"), ==,
|
||||
"500");
|
||||
|
||||
g_hash_table_destroy (hash);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_str_esc_to_list (void)
|
||||
{
|
||||
int i;
|
||||
struct {
|
||||
const char* str;
|
||||
const char* strs[3];
|
||||
} strings [] = {
|
||||
{ "maildir:foo",
|
||||
{"maildir:foo", NULL, NULL}},
|
||||
{ "maildir:sent items",
|
||||
{"maildir:sent", "items", NULL}},
|
||||
{ "\"maildir:sent items\"",
|
||||
{"maildir:sent items", NULL, NULL}},
|
||||
};
|
||||
|
||||
for (i = 0; i != G_N_ELEMENTS(strings); ++i) {
|
||||
GSList *lst, *cur;
|
||||
unsigned u;
|
||||
lst = mu_str_esc_to_list (strings[i].str);
|
||||
for (cur = lst, u = 0; cur; cur = g_slist_next(cur), ++u)
|
||||
g_assert_cmpstr ((const char*)cur->data,==,
|
||||
strings[i].strs[u]);
|
||||
mu_str_free_list (lst);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_str_display_contact (void)
|
||||
{
|
||||
int i;
|
||||
struct {
|
||||
const char* word;
|
||||
const char* disp;
|
||||
} words [] = {
|
||||
{ "\"Foo Bar\" <aap@noot.mies>", "Foo Bar"},
|
||||
{ "Foo Bar <aap@noot.mies>", "Foo Bar" },
|
||||
{ "<aap@noot.mies>", "aap@noot.mies" },
|
||||
{ "foo@bar.nl", "foo@bar.nl" }
|
||||
};
|
||||
|
||||
for (i = 0; i != G_N_ELEMENTS(words); ++i)
|
||||
g_assert_cmpstr (mu_str_display_contact_s (words[i].word), ==,
|
||||
words[i].disp);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
assert_cmplst (GSList *lst, const char *items[])
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!lst)
|
||||
g_assert (!items);
|
||||
|
||||
for (i = 0; lst; lst = g_slist_next(lst), ++i)
|
||||
g_assert_cmpstr ((char*)lst->data,==,items[i]);
|
||||
|
||||
g_assert (items[i] == NULL);
|
||||
}
|
||||
|
||||
|
||||
static GSList*
|
||||
create_list (const char *items[])
|
||||
{
|
||||
GSList *lst;
|
||||
|
||||
lst = NULL;
|
||||
while (items && *items) {
|
||||
lst = g_slist_prepend (lst, g_strdup(*items));
|
||||
++items;
|
||||
}
|
||||
|
||||
return g_slist_reverse (lst);
|
||||
|
||||
}
|
||||
|
||||
static void
|
||||
test_mu_str_from_list (void)
|
||||
{
|
||||
{
|
||||
const char *strs[] = {"aap", "noot", "mies", NULL};
|
||||
GSList *lst = create_list (strs);
|
||||
gchar *str = mu_str_from_list (lst, ',');
|
||||
g_assert_cmpstr ("aap,noot,mies", ==, str);
|
||||
mu_str_free_list (lst);
|
||||
g_free (str);
|
||||
}
|
||||
|
||||
{
|
||||
const char *strs[] = {"aap", "no,ot", "mies", NULL};
|
||||
GSList *lst = create_list (strs);
|
||||
gchar *str = mu_str_from_list (lst, ',');
|
||||
g_assert_cmpstr ("aap,no,ot,mies", ==, str);
|
||||
mu_str_free_list (lst);
|
||||
g_free (str);
|
||||
}
|
||||
|
||||
{
|
||||
const char *strs[] = {NULL};
|
||||
GSList *lst = create_list (strs);
|
||||
gchar *str = mu_str_from_list (lst,'@');
|
||||
g_assert_cmpstr (NULL, ==, str);
|
||||
mu_str_free_list (lst);
|
||||
g_free (str);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_str_to_list (void)
|
||||
{
|
||||
{
|
||||
const char *items[]= {"foo", "bar ", "cuux", NULL};
|
||||
GSList *lst = mu_str_to_list ("foo@bar @cuux",'@', FALSE);
|
||||
assert_cmplst (lst, items);
|
||||
mu_str_free_list (lst);
|
||||
}
|
||||
|
||||
{
|
||||
GSList *lst = mu_str_to_list (NULL,'x',FALSE);
|
||||
g_assert (lst == NULL);
|
||||
mu_str_free_list (lst);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_mu_str_to_list_strip (void)
|
||||
{
|
||||
const char *items[]= {"foo", "bar", "cuux", NULL};
|
||||
GSList *lst = mu_str_to_list ("foo@bar @cuux",'@', TRUE);
|
||||
assert_cmplst (lst, items);
|
||||
mu_str_free_list (lst);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_str_replace (void)
|
||||
{
|
||||
unsigned u;
|
||||
struct {
|
||||
const char* str;
|
||||
const char* sub;
|
||||
const char *repl;
|
||||
const char *exp;
|
||||
} strings [] = {
|
||||
{ "hello", "ll", "xx", "hexxo" },
|
||||
{ "hello", "hello", "hi", "hi" },
|
||||
{ "hello", "foo", "bar", "hello" }
|
||||
};
|
||||
|
||||
for (u = 0; u != G_N_ELEMENTS(strings); ++u) {
|
||||
char *res;
|
||||
res = mu_str_replace (strings[u].str,
|
||||
strings[u].sub,
|
||||
strings[u].repl);
|
||||
g_assert_cmpstr (res,==,strings[u].exp);
|
||||
g_free (res);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_str_remove_ctrl_in_place (void)
|
||||
{
|
||||
unsigned u;
|
||||
struct {
|
||||
char *str;
|
||||
const char *exp;
|
||||
} strings [] = {
|
||||
{ g_strdup(""), ""},
|
||||
{ g_strdup("hello, world!"), "hello, world!" },
|
||||
{ g_strdup("hello,\tworld!"), "hello, world!" },
|
||||
{ g_strdup("hello,\n\nworld!"), "hello, world!", },
|
||||
{ g_strdup("hello,\x1f\x1e\x1ew\nor\nld!"), "hello,w or ld!" },
|
||||
{ g_strdup("\x1ehello, world!\x1f"), "hello, world!" }
|
||||
};
|
||||
|
||||
for (u = 0; u != G_N_ELEMENTS(strings); ++u) {
|
||||
char *res;
|
||||
res = mu_str_remove_ctrl_in_place (strings[u].str);
|
||||
g_assert_cmpstr (res,==,strings[u].exp);
|
||||
g_free (strings[u].str);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
setlocale (LC_ALL, "");
|
||||
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
/* mu_str_size */
|
||||
g_test_add_func ("/mu-str/mu-str-size-01",
|
||||
test_mu_str_size_01);
|
||||
g_test_add_func ("/mu-str/mu-str-size-02",
|
||||
test_mu_str_size_02);
|
||||
|
||||
/* mu_str_prio */
|
||||
g_test_add_func ("/mu-str/mu-str-prio-01",
|
||||
test_mu_str_prio_01);
|
||||
g_test_add_func ("/mu-str/mu-str-prio-02",
|
||||
test_mu_str_prio_02);
|
||||
|
||||
|
||||
g_test_add_func ("/mu-str/mu-str-display_contact",
|
||||
test_mu_str_display_contact);
|
||||
|
||||
g_test_add_func ("/mu-str/mu-str-from-list",
|
||||
test_mu_str_from_list);
|
||||
g_test_add_func ("/mu-str/mu-str-to-list",
|
||||
test_mu_str_to_list);
|
||||
g_test_add_func ("/mu-str/mu-str-to-list-strip",
|
||||
test_mu_str_to_list_strip);
|
||||
|
||||
g_test_add_func ("/mu-str/mu-str-parse-arglist",
|
||||
test_parse_arglist);
|
||||
|
||||
g_test_add_func ("/mu-str/mu-str-replace",
|
||||
test_mu_str_replace);
|
||||
|
||||
g_test_add_func ("/mu-str/mu-str-esc-to-list",
|
||||
test_mu_str_esc_to_list);
|
||||
|
||||
g_test_add_func ("/mu-str/mu_str_remove_ctrl_in_place",
|
||||
test_mu_str_remove_ctrl_in_place);
|
||||
|
||||
/* FIXME: add tests for mu_str_flags; but note the
|
||||
* function simply calls mu_msg_field_str */
|
||||
|
||||
g_log_set_handler (NULL,
|
||||
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL|
|
||||
G_LOG_FLAG_RECURSION,
|
||||
(GLogFunc)black_hole, NULL);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
255
lib/utils/test-mu-util.c
Normal file
255
lib/utils/test-mu-util.c
Normal file
@ -0,0 +1,255 @@
|
||||
/*
|
||||
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** 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, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif /*HAVE_CONFIG_H*/
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "test-mu-common.h"
|
||||
#include "lib/mu-util.h"
|
||||
|
||||
static void
|
||||
test_mu_util_dir_expand_00 (void)
|
||||
{
|
||||
#ifdef HAVE_WORDEXP_H
|
||||
gchar *got, *expected;
|
||||
|
||||
got = mu_util_dir_expand ("~/IProbablyDoNotExist");
|
||||
expected = g_strdup_printf ("%s%cIProbablyDoNotExist",
|
||||
getenv("HOME"), G_DIR_SEPARATOR);
|
||||
|
||||
g_assert_cmpstr (got,==,expected);
|
||||
|
||||
g_free (got);
|
||||
g_free (expected);
|
||||
#endif /*HAVE_WORDEXP_H*/
|
||||
}
|
||||
|
||||
static void
|
||||
test_mu_util_dir_expand_01 (void)
|
||||
{
|
||||
/* XXXX: the testcase does not work when using some dir
|
||||
* setups; (see issue #585), although the code should still
|
||||
* work. Turn of the test for now */
|
||||
return;
|
||||
|
||||
|
||||
#ifdef HAVE_WORDEXP_H
|
||||
{
|
||||
gchar *got, *expected;
|
||||
|
||||
got = mu_util_dir_expand ("~/Desktop");
|
||||
expected = g_strdup_printf ("%s%cDesktop",
|
||||
getenv("HOME"), G_DIR_SEPARATOR);
|
||||
|
||||
g_assert_cmpstr (got,==,expected);
|
||||
|
||||
g_free (got);
|
||||
g_free (expected);
|
||||
}
|
||||
#endif /*HAVE_WORDEXP_H*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
test_mu_util_guess_maildir_01 (void)
|
||||
{
|
||||
char *got;
|
||||
const char *expected;
|
||||
|
||||
/* skip the test if there's no /tmp */
|
||||
if (access ("/tmp", F_OK))
|
||||
return;
|
||||
|
||||
g_setenv ("MAILDIR", "/tmp", TRUE);
|
||||
|
||||
got = mu_util_guess_maildir ();
|
||||
expected = "/tmp";
|
||||
|
||||
g_assert_cmpstr (got,==,expected);
|
||||
g_free (got);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_util_guess_maildir_02 (void)
|
||||
{
|
||||
char *got, *mdir;
|
||||
|
||||
g_unsetenv ("MAILDIR");
|
||||
|
||||
mdir = g_strdup_printf ("%s%cMaildir",
|
||||
getenv("HOME"), G_DIR_SEPARATOR);
|
||||
got = mu_util_guess_maildir ();
|
||||
|
||||
if (access (mdir, F_OK) == 0)
|
||||
g_assert_cmpstr (got, ==, mdir);
|
||||
else
|
||||
g_assert_cmpstr (got, == , NULL);
|
||||
|
||||
g_free (got);
|
||||
g_free (mdir);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_util_check_dir_01 (void)
|
||||
{
|
||||
if (g_access ("/usr/bin", F_OK) == 0) {
|
||||
g_assert_cmpuint (
|
||||
mu_util_check_dir ("/usr/bin", TRUE, FALSE) == TRUE,
|
||||
==,
|
||||
g_access ("/usr/bin", R_OK) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_util_check_dir_02 (void)
|
||||
{
|
||||
if (g_access ("/tmp", F_OK) == 0) {
|
||||
g_assert_cmpuint (
|
||||
mu_util_check_dir ("/tmp", FALSE, TRUE) == TRUE,
|
||||
==,
|
||||
g_access ("/tmp", W_OK) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_util_check_dir_03 (void)
|
||||
{
|
||||
if (g_access (".", F_OK) == 0) {
|
||||
g_assert_cmpuint (
|
||||
mu_util_check_dir (".", TRUE, TRUE) == TRUE,
|
||||
==,
|
||||
g_access (".", W_OK | R_OK) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_util_check_dir_04 (void)
|
||||
{
|
||||
/* not a dir, so it must be false */
|
||||
g_assert_cmpuint (
|
||||
mu_util_check_dir ("test-util.c", TRUE, TRUE),
|
||||
==,
|
||||
FALSE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_mu_util_get_dtype_with_lstat (void)
|
||||
{
|
||||
g_assert_cmpuint (
|
||||
mu_util_get_dtype_with_lstat (MU_TESTMAILDIR), ==, DT_DIR);
|
||||
g_assert_cmpuint (
|
||||
mu_util_get_dtype_with_lstat (MU_TESTMAILDIR2), ==, DT_DIR);
|
||||
g_assert_cmpuint (
|
||||
mu_util_get_dtype_with_lstat (MU_TESTMAILDIR2 "/Foo/cur/mail5"),
|
||||
==, DT_REG);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_util_supports (void)
|
||||
{
|
||||
gboolean has_guile;
|
||||
gchar *path;
|
||||
|
||||
has_guile = FALSE;
|
||||
#ifdef BUILD_GUILE
|
||||
has_guile = TRUE;
|
||||
#endif /*BUILD_GUILE*/
|
||||
|
||||
g_assert_cmpuint (mu_util_supports (MU_FEATURE_GUILE), == ,has_guile);
|
||||
g_assert_cmpuint (mu_util_supports (MU_FEATURE_CRYPTO), == ,TRUE);
|
||||
|
||||
path = g_find_program_in_path ("gnuplot");
|
||||
g_free (path);
|
||||
|
||||
g_assert_cmpuint (mu_util_supports (MU_FEATURE_GNUPLOT),==,
|
||||
path ? TRUE : FALSE);
|
||||
|
||||
g_assert_cmpuint (
|
||||
mu_util_supports (MU_FEATURE_GNUPLOT|MU_FEATURE_GUILE|
|
||||
MU_FEATURE_CRYPTO),
|
||||
==,
|
||||
has_guile && path ? TRUE : FALSE);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_mu_util_program_in_path (void)
|
||||
{
|
||||
g_assert_cmpuint (mu_util_program_in_path("ls"),==,TRUE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
/* mu_util_dir_expand */
|
||||
g_test_add_func ("/mu-util/mu-util-dir-expand-00",
|
||||
test_mu_util_dir_expand_00);
|
||||
g_test_add_func ("/mu-util/mu-util-dir-expand-01",
|
||||
test_mu_util_dir_expand_01);
|
||||
|
||||
/* mu_util_guess_maildir */
|
||||
g_test_add_func ("/mu-util/mu-util-guess-maildir-01",
|
||||
test_mu_util_guess_maildir_01);
|
||||
g_test_add_func ("/mu-util/mu-util-guess-maildir-02",
|
||||
test_mu_util_guess_maildir_02);
|
||||
|
||||
/* mu_util_check_dir */
|
||||
g_test_add_func ("/mu-util/mu-util-check-dir-01",
|
||||
test_mu_util_check_dir_01);
|
||||
g_test_add_func ("/mu-util/mu-util-check-dir-02",
|
||||
test_mu_util_check_dir_02);
|
||||
g_test_add_func ("/mu-util/mu-util-check-dir-03",
|
||||
test_mu_util_check_dir_03);
|
||||
g_test_add_func ("/mu-util/mu-util-check-dir-04",
|
||||
test_mu_util_check_dir_04);
|
||||
|
||||
g_test_add_func ("/mu-util/mu-util-get-dtype-with-lstat",
|
||||
test_mu_util_get_dtype_with_lstat);
|
||||
|
||||
g_test_add_func ("/mu-util/mu-util-supports", test_mu_util_supports);
|
||||
g_test_add_func ("/mu-util/mu-util-program-in-path",
|
||||
test_mu_util_program_in_path);
|
||||
|
||||
|
||||
g_log_set_handler (NULL,
|
||||
G_LOG_LEVEL_DEBUG|
|
||||
G_LOG_LEVEL_MESSAGE|
|
||||
G_LOG_LEVEL_INFO, (GLogFunc)black_hole, NULL);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
Reference in New Issue
Block a user