From 05e61a9707a6da5c0bca77aa7c3c019a918494de Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Mon, 19 Mar 2007 00:06:53 +0100 Subject: [PATCH] make lua packages interfaces be *.li. make our lua packages support regexps, needed some bindings enhancements. fixes fixes fixes Signed-off-by: Pierre Habouzit --- .gitignore | 1 + Makefile.am | 6 ++++-- alias.cpkg | 12 +++++------- alias.h | 3 --- init.c | 7 ++----- init.h | 24 ------------------------ lib-lua/.gitignore | 3 ++- lib-lua/Makefile.am | 6 +++--- lib-lua/lib-lua.h | 5 ++++- lib-lua/lua-token.sh | 4 +++- lib-lua/luapkg2c.pl | 15 ++++++--------- lib-lua/madmutt.cpkg | 32 +++++++++++++++++--------------- lib-lua/runtime.c | 32 +++++++++++++++++++++++++++++++- lib-sys/unix.c | 6 +++--- lib-sys/unix.h | 2 +- 15 files changed, 82 insertions(+), 76 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e0984dc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +alias.c diff --git a/Makefile.am b/Makefile.am index f09fc6c..65c1bee 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,7 +4,9 @@ AUTOMAKE_OPTIONS = foreign EXTRA_PROGRAMS = madmutt_dotlock pgpringng pgpewrapng makedoc -.cpkg.c: ; ./lib-lua/luapkg2c.pl -c $< > $@ +.cpkg.c: ; ./lib-lua/luapkg2c.pl -c $< > $@ +.cpkg.li: ; ./lib-lua/luapkg2c.pl -h $< > $@ + if BUILD_NNTP NNTP_SUBDIR = nntp @@ -39,7 +41,7 @@ madmutt_LDADD = @MUTT_LIB_OBJECTS@ @LIBOBJS@ \ $(top_builddir)/lib-lib/liblib.a \ $(MUTTLIBS) $(INTLLIBS) $(LIBICONV) -madmutt_DEPENDENCIES = $(top_builddir)/lib-lua/madmutt.h \ +madmutt_DEPENDENCIES = $(top_builddir)/lib-lua/madmutt.li \ $(top_builddir)/lib-lua/lua-token.h \ @MUTT_LIB_OBJECTS@ @LIBOBJS@ \ $(top_builddir)/lib-lib/liblib.a \ diff --git a/alias.cpkg b/alias.cpkg index 424adda..a87f5f0 100644 --- a/alias.cpkg +++ b/alias.cpkg @@ -50,11 +50,10 @@ }; @type path_t = { - .push = luaM_path_post($$); .kind = 's'; .ctype = char *; .dtor = p_delete($$); - .ctor = m_strdup($$); + .ctor = luaM_pathnew($$); }; @type quadopt_t = { @@ -64,12 +63,11 @@ }; static @package MAlias { - path_t alias_format = m_strdup("%4n %2f %t %-10a %r"); + string_t alias_format = m_strdup("%4n %2f %t %-10a %r"); + path_t alias_file = m_strdup("~/.madmutt/aliases"); } MAlias; -char *AliasFile; alias_t *Aliases; -rx_t GecosMask; #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x @@ -311,7 +309,7 @@ void mutt_create_alias(ENVELOPE *cur, address_t *iadr) alias_list_push(&Aliases, new); - m_strcpy(buf, sizeof(buf), NONULL(AliasFile)); + m_strcpy(buf, sizeof(buf), NONULL(MAlias.alias_file)); if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) { return; } @@ -374,7 +372,7 @@ static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn) if (pw) { char namebuf[STRING]; - mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx); + mutt_gecos_name(namebuf, sizeof(namebuf), pw, MCore.gecos_mask.rx); m_strreplace(&pop->personal, namebuf); } } diff --git a/alias.h b/alias.h index 74eab42..29231bf 100644 --- a/alias.h +++ b/alias.h @@ -43,10 +43,7 @@ typedef struct alias_t { short num; } alias_t; -extern char *AliasFmt; -extern char *AliasFile; extern alias_t *Aliases; -extern rx_t GecosMask; DO_INIT(alias_t, alias); static inline void alias_wipe(alias_t *a) { diff --git a/init.c b/init.c index 96fcf0b..1e9eaad 100644 --- a/init.c +++ b/init.c @@ -2209,12 +2209,12 @@ void mutt_init (int skip_sys_rc, string_list_t * commands) snprintf (AttachmentMarker, sizeof(AttachmentMarker), "\033]9;%ld\a", (long) time (NULL)); + luaM_initialize(); /* Get some information about the user */ if ((pw = getpwuid (getuid ()))) { char rnbuf[STRING]; - mutt_gecos_name(rnbuf, sizeof(rnbuf), pw, GecosMask.rx); + mutt_gecos_name(rnbuf, sizeof(rnbuf), pw, MCore.gecos_mask); Realname = m_strdup(rnbuf); - endpwent (); } /* And about the host... */ @@ -2299,7 +2299,6 @@ void mutt_init (int skip_sys_rc, string_list_t * commands) From = rfc822_parse_adrlist (NULL, p); charset_initialize(); - luaM_initialize(); /* Set standard defaults */ hash_map (ConfigOptions, mutt_set_default, 0); @@ -2330,8 +2329,6 @@ void mutt_init (int skip_sys_rc, string_list_t * commands) mutt_expand_path (buffer, sizeof(buffer)); Muttrc = m_strdup(buffer); } - p_delete(&AliasFile); - AliasFile = m_strdup(NONULL (Muttrc)); /* Process the global rc file if it exists and the user hasn't explicity requested not to via "-n". */ diff --git a/init.h b/init.h index abc629b..136876f 100644 --- a/init.h +++ b/init.h @@ -138,15 +138,6 @@ struct option_t MuttVars[] = { ** check only happens after the \fIfirst\fP edit of the file). When set ** to \fIno\fP, composition will never be aborted. */ - {"alias_file", DT_PATH, R_NONE, UL &AliasFile, "~/.madmuttrc"}, - /* - ** .pp - ** The default file in which to save aliases created by the - ** ``$create-alias'' function. - ** .pp - ** \fBNote:\fP Madmutt will not automatically source this file; you must - ** explicitly use the ``$source'' command for it to be executed. - */ {"allow_8bit", DT_BOOL, R_NONE, OPTALLOW8BIT, "yes" }, /* ** .pp @@ -750,21 +741,6 @@ struct option_t MuttVars[] = { ** .pp ** Defaults to the contents of the environment variable \fT$$$EMAIL\fP. */ - {"gecos_mask", DT_RX, R_NONE, UL &GecosMask, "^[^,]*"}, - /* - ** .pp - ** A regular expression used by Madmutt to parse the GECOS field of a password - ** entry when expanding the alias. By default the regular expression is set - ** to ``\fT^[^,]*\fP'' which will return the string up to the first ``\fT,\fP'' encountered. - ** If the GECOS field contains a string like "lastname, firstname" then you - ** should do: \fTset gecos_mask=".*"\fP. - ** .pp - ** This can be useful if you see the following behavior: you address a e-mail - ** to user ID stevef whose full name is Steve Franklin. If Madmutt expands - ** stevef to ``Franklin'' stevef@foo.bar then you should set the gecos_mask to - ** a regular expression that will match the whole name so Madmutt will expand - ** ``Franklin'' to ``Franklin, Steve''. - */ #ifdef USE_NNTP {"nntp_group_index_format", DT_STR, R_BOTH, UL &GroupFormat, "%4C %M%N %5s %-45.45f %d"}, /* diff --git a/lib-lua/.gitignore b/lib-lua/.gitignore index 2fc4ed7..5165eec 100644 --- a/lib-lua/.gitignore +++ b/lib-lua/.gitignore @@ -1,2 +1,3 @@ lua-token.[hc] -madmutt.[hc] +madmutt.c +madmutt.li diff --git a/lib-lua/Makefile.am b/lib-lua/Makefile.am index 8f149f5..a12888b 100644 --- a/lib-lua/Makefile.am +++ b/lib-lua/Makefile.am @@ -1,12 +1,12 @@ BUILT_SOURCES = lua-token.h lua-token.c DISTCLEANFILES = $(BUILT_SOURCES) -.cpkg.c: ; ./luapkg2c.pl -c $< > $@ -.cpkg.h: ; ./luapkg2c.pl -h $< > $@ +.cpkg.c: ; ./luapkg2c.pl -c $< > $@ +.cpkg.li: ; ./luapkg2c.pl -h $< > $@ noinst_LIBRARIES = liblua.a -liblua_a_DEPENDENCIES = madmutt.h +liblua_a_DEPENDENCIES = madmutt.li liblua_a_SOURCES = madmutt.cpkg \ lib-lua.h \ runtime.c \ diff --git a/lib-lua/lib-lua.h b/lib-lua/lib-lua.h index 5afb91f..862b6b5 100644 --- a/lib-lua/lib-lua.h +++ b/lib-lua/lib-lua.h @@ -47,7 +47,10 @@ int luaM_wrap(void (*errfun)(const char *fmt, ...) int status); quadopt_t luaM_checkquadopt(lua_State *L, int narg); +const char *luaM_checkrx(lua_State *Ls, int narg); +char *luaM_pathnew(const char *val); +rx_t *luaM_rxnew(const char* val); -#include "madmutt.h" +#include "madmutt.li" #endif diff --git a/lib-lua/lua-token.sh b/lib-lua/lua-token.sh index a3ba223..8c5540c 100644 --- a/lib-lua/lua-token.sh +++ b/lib-lua/lua-token.sh @@ -110,6 +110,8 @@ chmod -w $1 exit 0 ############ Put tokens here ############ +## alias_file +## alias_format ## beep ## beep_new ## bindir @@ -118,6 +120,7 @@ exit 0 ## dsn_notify ## dsn_return ## editor +## gecos_mask ## hcache_backend ## homedir ## quit @@ -127,4 +130,3 @@ exit 0 ## tmpdir ## username ## version -## alias_format diff --git a/lib-lua/luapkg2c.pl b/lib-lua/luapkg2c.pl index b7d1ba3..f1bab32 100755 --- a/lib-lua/luapkg2c.pl +++ b/lib-lua/luapkg2c.pl @@ -83,7 +83,7 @@ sub parse_type($$) { while (stream_getline($src)) { last if (/^\s*\}\s*;\s*/); - if (/^\s*\.(push|kind|ctype|dtor|ctor|check)\s*=\s*(.*?)\s*;\s*$/) { + if (/^\s*\.(kind|ctype|dtor|ctor|push|check)\s*=\s*(.*?)\s*;\s*$/) { $t{$1} = $2; if ($1 eq "kind") { if ($2 =~ /^'([bis])'$/) { @@ -220,9 +220,7 @@ sub dump_fun($$) { } } - if (defined $t{type}->{push}) { - $call =~ s/\$\$/$t{type}->{push}/; - } + die "UNIMPLEMENTED" if defined $t{type}->{push}; if ($t{type}->{kind} eq 'b') { put_line($f, 0); @@ -411,7 +409,10 @@ EOF foreach (@{$pkg->{props}}) { my $p = $pkg->{members}{$_}; my %t = find_type($p, $p->{type}); - my $call = $pkg->{cname}.".".$p->{name}; + my $call = $t{type}->{push} || '$$'; + + $call =~ s/\$L/L/; + $call =~ s/\$\$/$pkg->{cname}.$p->{name}/; my $tok = $p->{name}; $tok =~ tr/a-z/A-Z/; @@ -428,10 +429,6 @@ EOF $call = "lua_pushstring(L, $call)"; } - if (defined $t{type}->{push}) { - $call =~ s/\$\$/$t{type}->{push}/; - } - put_line($p, 0); print " case LTK_$tok:\n"; printf " $call;\n"; diff --git a/lib-lua/madmutt.cpkg b/lib-lua/madmutt.cpkg index 270e198..5e48a45 100644 --- a/lib-lua/madmutt.cpkg +++ b/lib-lua/madmutt.cpkg @@ -50,13 +50,6 @@ static const char *madmutt_pwd(void) return path; } -static const char *luaM_path_post(const char *val) -{ - char path[PATH_MAX]; - _mutt_expand_path(path, sizeof(path), val, 0); - return path; -} - @type bool = { .kind = 'b'; .ctype = unsigned : 1; @@ -70,11 +63,10 @@ static const char *luaM_path_post(const char *val) }; @type path_t = { - .push = luaM_path_post($$); .kind = 's'; .ctype = char *; .dtor = p_delete($$); - .ctor = m_strdup($$); + .ctor = luaM_pathnew($$); }; @type quadopt_t = { @@ -83,6 +75,15 @@ static const char *luaM_path_post(const char *val) .ctype = unsigned : 2; }; +@type rx_t = { + .kind = 's'; + .ctype = rx_t *; + .check = luaM_checkrx($L, $$); + .push = ($$)->pattern; + .ctor = luaM_rxnew($$); + .dtor = rx_delete($$); +}; + #if defined(HAVE_QDBM) # define HCACHE_BACKEND "qdbm" #elif defined(HAVE_GDBM) @@ -100,12 +101,13 @@ static const char *luaM_path_post(const char *val) const string_t docdir = PKGDOCDIR; const string_t hcache_backend = HCACHE_BACKEND; - path_t dotlock = m_strdup(BINDIR "/mutt_dotlock"); - path_t editor = m_strdup(getenv("VISUAL") ?: getenv("EDITOR") ?: "vi"); - path_t shell = madmutt_init_shell(); - path_t username = madmutt_init_username(); - path_t homedir = madmutt_init_homedir(); - path_t tmpdir = m_strdup(getenv("TMPDIR") ?: "/tmp"); + path_t dotlock = m_strdup(BINDIR "/mutt_dotlock"); + path_t editor = m_strdup(getenv("VISUAL") ?: getenv("EDITOR") ?: "vi"); + path_t shell = madmutt_init_shell(); + path_t username = madmutt_init_username(); + path_t homedir = madmutt_init_homedir(); + path_t tmpdir = m_strdup(getenv("TMPDIR") ?: "/tmp"); + rx_t gecos_mask = luaM_rxnew("^[^,]*"); quadopt_t quit = M_YES; bool beep = 1; diff --git a/lib-lua/runtime.c b/lib-lua/runtime.c index f570b55..ac724af 100644 --- a/lib-lua/runtime.c +++ b/lib-lua/runtime.c @@ -20,7 +20,8 @@ #include #include -#include +#include "../alias.h" +#include "../mutt.h" static lua_State *L; @@ -83,3 +84,32 @@ quadopt_t luaM_checkquadopt(lua_State *Ls, int narg) return i; } +const char *luaM_checkrx(lua_State *Ls, int narg) +{ + const char *s = luaL_checkstring(Ls, narg); + char buf[STRING]; + + if (rx_validate(s, buf, ssizeof(buf))) { + s = lua_pushfstring(Ls, "invalid regexp: `%s'", buf); + luaL_argerror(Ls, narg, s); + return NULL; + } + + return s; +} + +char *luaM_pathnew(const char *val) +{ + char path[PATH_MAX]; + _mutt_expand_path(path, sizeof(path), val, 0); + return m_strdup(path); +} + +rx_t *luaM_rxnew(const char *val) +{ + if (m_strisempty(val)) + val = "."; + + return rx_compile(val, mutt_which_case(val)); +} + diff --git a/lib-sys/unix.c b/lib-sys/unix.c index 38389b2..9054435 100644 --- a/lib-sys/unix.c +++ b/lib-sys/unix.c @@ -20,7 +20,7 @@ * otherwise assume that the GECOS field is a comma-separated list. * Replace "&" by a capitalized version of the user's login name. */ -ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, regex_t *rx) +ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, rx_t *rx) { const char *p, *end; ssize_t len; @@ -31,10 +31,10 @@ ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, regex_t *rx) if (!pw->pw_gecos) return 0; - if (rx) { + if (rx->rx) { regmatch_t pat_match[1]; - if (regexec(rx, pw->pw_gecos, 1, pat_match, 0)) { + if (regexec(rx->rx, pw->pw_gecos, 1, pat_match, 0)) { return 0; } diff --git a/lib-sys/unix.h b/lib-sys/unix.h index bfc16c0..eda9d8b 100644 --- a/lib-sys/unix.h +++ b/lib-sys/unix.h @@ -23,7 +23,7 @@ #include #include -ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, regex_t *rx); +ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, rx_t *rx); int getdnsdomainname(char *, ssize_t); /* flags for _mutt_system() */ -- 2.20.1