From 6729a8ab646a00a661858b7ac7ee21f770bff3d3 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 11 Mar 2007 14:49:00 +0100 Subject: [PATCH] bye bye DT_SYS variables. Signed-off-by: Pierre Habouzit --- init.c | 61 ++--------------- init.h | 34 ---------- lib-lib/str.h | 22 ++++++ lib-lua/Makefile.am | 6 +- lib-lua/lib-lua_priv.h | 33 +++++++++ lib-lua/madmutt.c | 148 +++++++++++++++++++++++++++++++++++++++++ lib-lua/runtime.c | 64 +----------------- 7 files changed, 213 insertions(+), 155 deletions(-) create mode 100644 lib-lua/lib-lua_priv.h create mode 100644 lib-lua/madmutt.c diff --git a/init.c b/init.c index 6cfbf6a..0d8b52b 100644 --- a/init.c +++ b/init.c @@ -112,7 +112,6 @@ static void sort_to_string (char* dst, ssize_t dstlen, struct option_t* option) static void rx_to_string (char* dst, ssize_t dstlen, struct option_t* option); static void magic_to_string (char* dst, ssize_t dstlen, struct option_t* option); static void addr_to_string (char* dst, ssize_t dstlen, struct option_t* option); -static void sys_to_string (char* dst, ssize_t dstlen, struct option_t* option); /* protos for config type handles: convert to value from string */ static int bool_from_string (struct option_t* dst, const char* val, @@ -153,7 +152,6 @@ static struct { * but must define it as DT_ is used for indexing */ { DT_SYN, NULL, NULL }, { DT_ADDR, addr_to_string, addr_from_string }, - { DT_SYS, sys_to_string, NULL }, }; static void bool_to_string (char* dst, ssize_t dstlen, @@ -229,49 +227,6 @@ static void str_to_string (char* dst, ssize_t dstlen, NONULL (*((char**) option->data))); } -static void sys_to_string (char* dst, ssize_t dstlen, - struct option_t* option) { - char *val = NULL, *t = NULL; - int clean = 0; - - /* get some $madmutt_ values dynamically */ - if (m_strcmp("madmutt_pwd", option->option) == 0) { - val = p_new(char, _POSIX_PATH_MAX); - val = getcwd (val, _POSIX_PATH_MAX-1); - clean = 1; - } else if (m_strcmp("madmutt_folder_path", option->option) == 0 && - CurrentFolder && *CurrentFolder) { - val = CurrentFolder; - } else if (m_strcmp("madmutt_folder_name", option->option) == 0 && - CurrentFolder && *CurrentFolder) { - - ssize_t Maildirlength = m_strlen(Maildir); - - /* - * if name starts with $folder, just strip it to keep hierarchy - * $folder=imap://host, path=imap://host/inbox/b -> inbox/b - */ - if (Maildirlength > 0 && m_strncmp(CurrentFolder, Maildir, - Maildirlength) == 0 && - m_strlen(CurrentFolder) > Maildirlength) { - val = CurrentFolder + Maildirlength; - if (Maildir[Maildirlength]!='/') - val += 1; - /* if not $folder, just use everything after last / */ - } else if ((t = strrchr (CurrentFolder, '/')) != NULL) - val = t+1; - /* default: use as-is */ - else - val = (char *) CurrentFolder; - - } else - val = (char *) option->init; - - snprintf (dst, dstlen, "%s=\"%s\"", option->option, NONULL (val)); - if (clean) - p_delete(&val); -} - static int path_from_string (struct option_t* dst, const char* val, char* errbuf __attribute__ ((unused)), ssize_t errlen __attribute__ ((unused))) { char path[_POSIX_PATH_MAX]; @@ -1665,14 +1620,13 @@ static int parse_set (BUFFER * tmp, BUFFER * s, unsigned long data, DTYPE (option->type) == DT_MAGIC || DTYPE (option->type) == DT_NUM || DTYPE (option->type) == DT_SORT || - DTYPE (option->type) == DT_RX || - DTYPE (option->type) == DT_SYS) { - + DTYPE (option->type) == DT_RX) + { /* XXX maybe we need to get unset into handlers? */ if (DTYPE (option->type) == DT_STR || DTYPE (option->type) == DT_PATH || - DTYPE (option->type) == DT_ADDR || - DTYPE (option->type) == DT_SYS) { + DTYPE (option->type) == DT_ADDR) + { if (unset) { CHECK_PAGER; if (!FuncTable[DTYPE (option->type)].opt_fromstr) { @@ -2261,12 +2215,7 @@ void mutt_init (int skip_sys_rc, string_list_t * commands) ConfigOptions = hash_create (sizeof(MuttVars) * 2); for (i = 0; MuttVars[i].option; i++) { - if (DTYPE (MuttVars[i].type) != DT_SYS) - hash_insert (ConfigOptions, MuttVars[i].option, &MuttVars[i], 0); - else - hash_insert (ConfigOptions, MuttVars[i].option, - add_option (MuttVars[i].option, MuttVars[i].init, - DT_SYS, 0), 0); + hash_insert (ConfigOptions, MuttVars[i].option, &MuttVars[i], 0); } /* diff --git a/init.h b/init.h index 46a2cd1..0de8ae3 100644 --- a/init.h +++ b/init.h @@ -32,7 +32,6 @@ #define DT_MAGIC 8 /* mailbox type */ #define DT_SYN 9 /* synonym for another variable */ #define DT_ADDR 10 /* e-mail address */ -#define DT_SYS 11 /* pre-defined via $madmutt_ */ #define DTYPE(x) ((x) & DT_MASK) @@ -1366,39 +1365,6 @@ struct option_t MuttVars[] = { ** This specifies the folder into which read mail in your ``$$spoolfile'' ** folder will be appended. */ - {"madmutt_folder_path", DT_SYS, R_NONE, 0, "" }, - /* - ** .pp - ** \fIThis is a read-only system property and, at runtime, - ** specifies the full path or URI of the folder currently - ** open (if any).\fP - */ - {"madmutt_folder_name", DT_SYS, R_NONE, 0, "" }, - /* - ** .pp - ** \fIThis is a read-only system property and, at runtime, - ** specifies the actual name of the folder as far as it could - ** be detected.\fP - ** .pp - ** For detection, $$$folder is first taken into account - ** and simply stripped to form the result when a match is found. For - ** example, with $$$folder being \fTimap://host\fP and the folder is - ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just - ** \fTINBOX/foo\fP.) - ** .pp - ** Second, if the initial portion of a name is not $$$folder, - ** the result will be everything after the last ``/''. - ** .pp - ** Third and last, the result will be just the name if neither - ** $$$folder nor a ``/'' were found in the name. - */ - {"madmutt_pwd", DT_SYS, R_NONE, 0, "" }, - /* - ** .pp - ** \fIThis is a read-only system property and, at runtime, - ** specifies the current working directory of the madmutt - ** binary.\fP - */ {"operating_system", DT_STR, R_NONE, UL &OperatingSystem, "" }, /* ** .pp diff --git a/lib-lib/str.h b/lib-lib/str.h index 0136569..86fd758 100644 --- a/lib-lib/str.h +++ b/lib-lib/str.h @@ -209,6 +209,28 @@ ssize_t m_strwidth(const char *s); /* comparisons */ /****************************************************************************/ +/** \brief Tells whether s begins with p. + * + * \param[in] s the input string + * \param[in] p the prefix + * \param[out] pp position in s + * + * \return 1 if a match is found, 0 otherwise. + */ +static inline int m_strstart(const char *s, const char *p, const char **pp) +{ + if (!s) + return 0; + + while (*p) { + if (*s++ != *p++) + return 0; + } + if (pp) + *pp = s; + return 1; +} + /** \brief \c NULL resistant strcmp. * \param[in] a the first string. * \param[in] b the second string. diff --git a/lib-lua/Makefile.am b/lib-lua/Makefile.am index 53f6997..02c29a4 100644 --- a/lib-lua/Makefile.am +++ b/lib-lua/Makefile.am @@ -1,9 +1,9 @@ noinst_LIBRARIES = liblua.a -liblua_a_SOURCES = lib-lua.h \ - runtime.c +liblua_a_SOURCES = lib-lua.h lib-lua_priv.h \ + runtime.c madmutt.c -noinst_HEADERS = lib-lua.h +noinst_HEADERS = lib-lua.h lib-lua_priv.h DEFS=-DPKGDATADIR=\"$(pkgdatadir)\" -DSYSCONFDIR=\"$(sysconfdir)\" \ -DBINDIR=\"$(bindir)\" -DMUTTLOCALEDIR=\"$(datadir)/locale\" \ diff --git a/lib-lua/lib-lua_priv.h b/lib-lua/lib-lua_priv.h new file mode 100644 index 0000000..1dbb7f4 --- /dev/null +++ b/lib-lua/lib-lua_priv.h @@ -0,0 +1,33 @@ +/* + * 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 2 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. + * + * Copyright © 2007 Pierre Habouzit + */ + +#ifndef MUTT_LIB_LUA_LIB_LUA_PRIV_H +#define MUTT_LIB_LUA_LIB_LUA_PRIV_H + +#include "lib-lua.h" + +#include +#include +#include + +#define LUA_MADMUTT "madmutt" + +int luaopen_madmutt(lua_State *L); + +#endif diff --git a/lib-lua/madmutt.c b/lib-lua/madmutt.c new file mode 100644 index 0000000..351ecb5 --- /dev/null +++ b/lib-lua/madmutt.c @@ -0,0 +1,148 @@ +/* + * 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 2 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. + * + * Copyright © 2007 Pierre Habouzit + */ + +#include +#include "lib-lua_priv.h" + +#include "../mutt.h" + +static int madmutt_pwd(lua_State *L) +{ + char path[_POSIX_PATH_MAX]; + getcwd(path, sizeof(path)); + lua_pushstring(L, path); + return 1; +} + +static int madmutt_folder_path(lua_State *L) +{ + lua_pushstring(L, CurrentFolder ?: ""); + return 1; +} + +static int madmutt_folder_name(lua_State *L) +{ + const char *p; + + if (!m_strisempty(Maildir) + && m_strstart(CurrentFolder, Maildir, &p) && *p) { + while (*p == '/') + p++; + lua_pushstring(L, p); + } else { + p = strchr(CurrentFolder ?: "", '/'); + lua_pushstring(L, p ? p + 1 : (CurrentFolder ?: "")); + } + return 1; +} + +static const struct luaL_Reg madmutt_module_funcs[] = { + {"pwd", madmutt_pwd}, + /* + ** .pp + ** \fIThis is a read-only system property and, at runtime, + ** specifies the current working directory of the madmutt + ** binary.\fP + */ + {"folder_path", madmutt_folder_path}, + /* + ** .pp + ** \fIThis is a read-only system property and, at runtime, + ** specifies the full path or URI of the folder currently + ** open (if any).\fP + */ + {"folder_name", madmutt_folder_name}, + /* + ** .pp + ** \fIThis is a read-only system property and, at runtime, + ** specifies the actual name of the folder as far as it could + ** be detected.\fP + ** .pp + ** For detection, $$$folder is first taken into account + ** and simply stripped to form the result when a match is found. For + ** example, with $$$folder being \fTimap://host\fP and the folder is + ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just + ** \fTINBOX/foo\fP.) + ** .pp + ** Second, if the initial portion of a name is not $$$folder, + ** the result will be everything after the last ``/''. + ** .pp + ** Third and last, the result will be just the name if neither + ** $$$folder nor a ``/'' were found in the name. + */ + {NULL, NULL} +}; + +static const struct { + const char *key; + const char *value; +} madmutt_module_vars[] = { + { "version", VERSION }, + /* + ** .pp + ** \fIThis is a read-only system property and specifies madmutt's + ** version string.\fP + */ + { "sysconfdir", SYSCONFDIR }, + /* + ** .pp + ** \fIThis is a read-only system property and specifies madmutt's + ** subversion revision string.\fP + */ + { "bindir", BINDIR }, + /* + ** .pp + ** \fIThis is a read-only system property and specifies the + ** directory containing the madmutt binary.\fP + */ + { "docdir", PKGDOCDIR }, + /* + ** .pp + ** \fIThis is a read-only system property and specifies the + ** directory containing the madmutt documentation.\fP + */ +#ifdef USE_HCACHE +#if defined(HAVE_QDBM) + {"hcache_backend", "qdbm" }, +#elif defined(HAVE_GDBM) + {"hcache_backend", "gdbm" }, +#elif defined(HAVE_DB4) + {"hcache_backend", "db4" }, +#else + {"hcache_backend", "unknown" }, +#endif + /* + ** .pp + ** \fIThis is a read-only system property and specifies the + ** header chaching's database backend.\fP + */ +#endif +}; + +int luaopen_madmutt(lua_State *L) +{ + int i; + luaL_register(L, LUA_MADMUTT, madmutt_module_funcs); + + for (i = 0; i < countof(madmutt_module_vars); i++) { + lua_pushstring(L, madmutt_module_vars[i].value); + lua_setfield(L, -2, madmutt_module_vars[i].key); + } + return 1; +} diff --git a/lib-lua/runtime.c b/lib-lua/runtime.c index becd6de..60a6835 100644 --- a/lib-lua/runtime.c +++ b/lib-lua/runtime.c @@ -18,12 +18,7 @@ */ #include - -#include -#include -#include - -#include "lib-lua.h" +#include "lib-lua_priv.h" static lua_State *L; @@ -35,56 +30,7 @@ static const luaL_Reg lualibs[] = { {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, -}; - -static const struct luaL_Reg madmutt_module_funcs[] = { - {NULL, NULL} -}; - -static const struct { - const char *key; - const char *value; -} madmutt_module_vars[] = { - { "version", VERSION }, - /* - ** .pp - ** \fIThis is a read-only system property and specifies madmutt's - ** version string.\fP - */ - { "sysconfdir", SYSCONFDIR }, - /* - ** .pp - ** \fIThis is a read-only system property and specifies madmutt's - ** subversion revision string.\fP - */ - { "bindir", BINDIR }, - /* - ** .pp - ** \fIThis is a read-only system property and specifies the - ** directory containing the madmutt binary.\fP - */ - { "docdir", PKGDOCDIR }, - /* - ** .pp - ** \fIThis is a read-only system property and specifies the - ** directory containing the madmutt documentation.\fP - */ -#ifdef USE_HCACHE -#if defined(HAVE_QDBM) - {"hcache_backend", "qdbm" }, -#elif defined(HAVE_GDBM) - {"hcache_backend", "gdbm" }, -#elif defined(HAVE_DB4) - {"hcache_backend", "db4" }, -#else - {"hcache_backend", "unknown" }, -#endif - /* - ** .pp - ** \fIThis is a read-only system property and specifies the - ** header chaching's database backend.\fP - */ -#endif + {LUA_MADMUTT, luaopen_madmutt}, }; void mlua_initialize(void) @@ -97,12 +43,6 @@ void mlua_initialize(void) lua_pushstring(L, lualibs[i].name); lua_call(L, 1, 0); } - luaL_register(L, "madmutt", madmutt_module_funcs); - - for (i = 0; i < countof(madmutt_module_vars); i++) { - lua_pushstring(L, madmutt_module_vars[i].value); - lua_setfield(L, -2, madmutt_module_vars[i].key); - } } void mlua_shutdown(void) -- 2.20.1