lua bindings: add ctor/dtor.
authorPierre Habouzit <madcoder@debian.org>
Sun, 18 Mar 2007 16:36:35 +0000 (17:36 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sun, 18 Mar 2007 16:36:35 +0000 (17:36 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
lib-lua/luapkg2c.pl
lib-lua/madmutt.cpkg

index 72d84c7..9ab8789 100755 (executable)
@@ -83,7 +83,7 @@ sub parse_type($$) {
 
     while (stream_getline($src)) {
         last if (/^\s*\}\s*;\s*/);
 
     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])'$/) {
             $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') {
     }
     unless (defined $t{check}) {
         if ($t{kind} eq 'b') {
@@ -286,9 +291,12 @@ EOF
         next if $t{const};
 
         put_line($p, 0);
         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";
 };
     }
     print "};\n";
 };
@@ -300,7 +308,11 @@ sub dump_struct_short($) {
     foreach (@{$pkg->{props}}) {
         my $p = $pkg->{members}{$_};
         my %t = find_type($p, $p->{type});
     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 <<EOF;
     }
     put_line($pkg, 0);
     print <<EOF;
@@ -394,11 +406,18 @@ EOF
 
         put_line($p, 0);
         print  "      case LTK_$tok: \n";
 
         put_line($p, 0);
         print  "      case LTK_$tok: \n";
-        print "        $var = $check"               if ($t{type}->{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 <<EOF;
     }
 
 print <<EOF;
index 89ff6f2..270e198 100644 (file)
 
 #include "../mutt.h"
 
 
 #include "../mutt.h"
 
-static const char *madmutt_init_shell(void)
+static char *madmutt_init_shell(void)
 {
     struct passwd *pw = getpwuid(getuid());
 {
     struct passwd *pw = getpwuid(getuid());
-    return pw ? pw->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());
 {
     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());
 {
     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)
 }
 
 static const char *madmutt_pwd(void)
@@ -63,14 +63,18 @@ static const char *luaM_path_post(const char *val)
 };
 
 @type string_t = {
 };
 
 @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 *;
 };
 
 @type path_t = {
     .push  = luaM_path_post($$);
     .kind  = 's';
     .ctype = char *;
+    .dtor  = p_delete($$);
+    .ctor  = m_strdup($$);
 };
 
 @type quadopt_t = {
 };
 
 @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;
 
     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 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;
 
     quadopt_t quit     = M_YES;
     bool      beep     = 1;
@@ -111,7 +115,7 @@ static const char *luaM_path_post(const char *val)
 } MCore;
 
 @package MTransport {
 } 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;
 
     string_t dsn_notify = NULL;
     string_t dsn_return = NULL;