From bb89a1ca75e867edbe281dc848720a95650c24ce Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 18 Mar 2007 17:36:35 +0100 Subject: [PATCH] lua bindings: add ctor/dtor. Signed-off-by: Pierre Habouzit --- lib-lua/luapkg2c.pl | 43 +++++++++++++++++++++++++++++++------------ lib-lua/madmutt.cpkg | 28 ++++++++++++++++------------ 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/lib-lua/luapkg2c.pl b/lib-lua/luapkg2c.pl index 72d84c7..9ab8789 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|check)\s*=\s*(.*?)\s*;\s*$/) { + if (/^\s*\.(push|kind|ctype|dtor|ctor|check)\s*=\s*(.*?)\s*;\s*$/) { $t{$1} = $2; if ($1 eq "kind") { if ($2 =~ /^'([bis])'$/) { @@ -97,8 +97,13 @@ sub parse_type($$) { } } - for my $i ('kind', 'ctype') { - fatal($src, "incomplete type $type: missing .$i") unless defined $t{$i}; + for ('kind', 'ctype') { + fatal($src, "incomplete type $type: missing .$_") unless defined $t{$_}; + } + if ($t{kind} eq 's') { + for ('dtor', 'ctor') { + fatal($src, "incomplete type $type: missing .$_") unless defined $t{$_}; + } } unless (defined $t{check}) { if ($t{kind} eq 'b') { @@ -286,9 +291,12 @@ EOF next if $t{const}; put_line($p, 0); - print " $var = $p->{init};\n" if ($t{type}->{kind} eq 'b'); - print " $var = $p->{init};\n" if ($t{type}->{kind} eq 'i'); - print " m_strreplace(&$var, $p->{init});\n" if ($t{type}->{kind} eq 's'); + if ($t{type}->{dtor}) { + my $dtor = $t{type}->{dtor}; + $dtor =~ s/\$\$/\&$var/; + print " $dtor;\n"; + } + print " $var = $p->{init};\n" } print "};\n"; }; @@ -300,7 +308,11 @@ sub dump_struct_short($) { foreach (@{$pkg->{props}}) { my $p = $pkg->{members}{$_}; my %t = find_type($p, $p->{type}); - printf " $t{type}->{ctypefmt};\n", $p->{name}; + if ($t{const}) { + printf " const $t{type}->{ctypefmt};\n", $p->{name}; + } else { + printf " $t{type}->{ctypefmt};\n", $p->{name}; + } } put_line($pkg, 0); print <{kind} eq 'b'); - print " $var = $check" if ($t{type}->{kind} eq 'i'); - print " m_strreplace(&$var, $check)" if ($t{type}->{kind} eq 's'); - - print ";\n return 1;\n"; + if ($t{type}->{dtor}) { + my $dtor = $t{type}->{dtor}; + $dtor =~ s/\$\$/\&$var/; + print " $dtor;\n"; + } + if ($t{type}->{ctor}) { + my $ctor = $t{type}->{ctor}; + $ctor =~ s/\$\$/$check/; + $check = $ctor; + } + print " $var = $check;\n"; + print " return 1;\n"; } print <pw_shell : (getenv("SHELL") ?: "/bin/sh"); + return m_strdup(pw ? pw->pw_shell : (getenv("SHELL") ?: "/bin/sh")); } -static const char *madmutt_init_username(void) +static char *madmutt_init_username(void) { struct passwd *pw = getpwuid(getuid()); - return pw ? pw->pw_name : (getenv("USER") ?: "john_doe"); + return m_strdup(pw ? pw->pw_name : (getenv("USER") ?: "john_doe")); } -static const char *madmutt_init_homedir(void) +static char *madmutt_init_homedir(void) { struct passwd *pw = getpwuid(getuid()); - return pw ? pw->pw_dir : (getenv("HOME") ?: "/"); + return m_strdup(pw ? pw->pw_dir : (getenv("HOME") ?: "/")); } static const char *madmutt_pwd(void) @@ -63,14 +63,18 @@ static const char *luaM_path_post(const char *val) }; @type string_t = { - .kind = 's'; - .ctype = const char *; + .kind = 's'; + .ctype = char *; + .dtor = p_delete($$); + .ctor = m_strdup($$); }; @type path_t = { .push = luaM_path_post($$); .kind = 's'; .ctype = char *; + .dtor = p_delete($$); + .ctor = m_strdup($$); }; @type quadopt_t = { @@ -96,12 +100,12 @@ static const char *luaM_path_post(const char *val) const string_t docdir = PKGDOCDIR; const string_t hcache_backend = HCACHE_BACKEND; - path_t dotlock = BINDIR "/mutt_dotlock"; - path_t editor = getenv("VISUAL") ?: getenv("EDITOR") ?: "vi"; + 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 = getenv("TMPDIR") ?: "/tmp"; + path_t tmpdir = m_strdup(getenv("TMPDIR") ?: "/tmp"); quadopt_t quit = M_YES; bool beep = 1; @@ -111,7 +115,7 @@ static const char *luaM_path_post(const char *val) } MCore; @package MTransport { - path_t sendmail = SENDMAIL " -eom -oi"; + path_t sendmail = m_strdup(SENDMAIL " -eom -oi"); string_t dsn_notify = NULL; string_t dsn_return = NULL; -- 2.20.1