1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
#!/usr/bin/perl -w
use strict;
# Author: Andreas Marienborg <andreas.marienborg@gmail.com>
# Donated as is to xapian project. Use and modify as see fit
BEGIN {
my $dir = shift || '.';
if ($dir eq '--help') {
print "Syntax: $0 [<path to srcdir> [<xapian version>]\n\n";
print "This script needs to know the source directory to find\n";
print "exception_data.pm - by default it looks in '.'.\n";
exit 0;
}
push(@INC, $dir);
$::version = shift || '';
if ($::version =~ /^(\d+)\.(\d+)\,(\d+)/) {
$::version = $1 * 1000000 + $2 * 1000 + $3;
} else {
# Assume 1.4.x if not specified.
$::version = 1004000;
}
}
# We load the exception data from xapian-core.
use exception_data qw(@baseclasses @classes %subclasses);
# Generate typemaps for Xapian::Error and its subclasses.
my $tm = "typemap-errorclasses";
open(TM, ">", "$tm.tmp")
or die "cannot write '$tm.tmp': $!\n";
foreach (@baseclasses, @classes) {
my ($classname, $parent, $desc) = split /\t/;
print TM "$classname\tO_OBJECT\n";
print TM "$classname *\tO_OBJECT\n";
}
close TM;
rename "$tm.tmp", $tm
or die "Failed to rename '$tm.tmp' to '$tm': $!\n";
my $fnm = "Xapian/Error.pm";
open F, '>', "$fnm.tmp" or die $!;
print F <<'END';
package Search::Xapian::Error;
=head1 NAME
Search::Xapian::Error - Base class for all exceptions in Search::Xapian
=head1 DESCRIPTION
This is an abstract class in C++, i.e. it cannot be instantiated directly.
In Perl there is no such concept, but you should not need to create instances
of this class yourself.
=head1 METHODS
All exception objects have the following methods
=head2 get_msg
Returns a string with a descriptive error message, useful for outputting
=head2 get_type
The type of this error (e.g. "DocNotFoundError").
=head2 get_context
Optional context information, returned as a string
=head2 get_error_string
Returns any error string from errno or similar associated with this error
=cut
use 5.006;
use strict;
use warnings;
require DynaLoader;
END
foreach my $subclass (@{$subclasses{'Error'}}) {
print F "use Search::Xapian::$subclass;\n";
}
print F <<'END';
our @ISA = qw(DynaLoader);
# Preloaded methods go here.
# In a new thread, copy objects of this class to unblessed, undef values.
sub CLONE_SKIP { 1 }
sub new {
my $class = shift;
my ($self);
bless $self, $class;
return $self;
}
1;
END
close F or die $!;
rename "$fnm.tmp", "$fnm" or die $!;
foreach ('Error', @baseclasses, @classes) {
my ($classname, $parent, $full_description) = split /\t/;
# XS/CLASSNAME.xs
$fnm = "XS/$classname.xs";
open F, '>', "$fnm.tmp" or die $!;
print F <<"END";
MODULE = Search::Xapian\t PACKAGE = Search::Xapian::$classname
PROTOTYPES: ENABLE
string
${classname}::get_type()
string
${classname}::get_msg()
string
${classname}::get_context()
const char *
${classname}::get_error_string()
void
${classname}::DESTROY()
END
if (exists $subclasses{$classname}) {
print F "\n";
foreach my $subclass (@{$subclasses{$classname}}) {
print F "INCLUDE: XS/$subclass.xs\n";
}
}
close F or die $!;
rename "$fnm.tmp", "$fnm" or die $!;
next if $classname eq 'Error';
$full_description =~ s!^[/ ]\*[*/]?!!mg;
$full_description =~ s!\*\/$!!mg; # ! to unconfuse vim
my ($heading, $desc) = split('\n\n', $full_description, 2);
$desc ||= '';
# Xapian/CLASSNAME.pm
$fnm = "Xapian/$classname.pm";
open F, '>', "$fnm.tmp" or die $!;
print F <<"END";
package Search::Xapian::$classname;
=head1 NAME
Search::Xapian::$classname - $heading
=head1 DESCRIPTION
$desc
=cut
use 5.006;
use strict;
use warnings;
require DynaLoader;
# For compatibility with XS Search::Xapian < 1.2.3 which still threw strings
# in some cases.
use overload '""' => sub { "Exception: ".\$_[0]->get_msg };
END
if (exists $subclasses{$classname}) {
foreach my $subclass (@{$subclasses{$classname}}) {
print F "use Search::Xapian::$subclass;\n";
}
print F "\n";
}
print F <<"END";
our \@ISA = qw(DynaLoader Search::Xapian::$parent);
1;
END
close F or die $!;
rename "$fnm.tmp", "$fnm" or die $!;
}
# write new handle_exception.cc
$fnm = "handle_exception.cc";
open F, '>', "$fnm.tmp" or die $!;
print F <<'END';
#include <xapian.h>
extern "C" {
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
}
/* handle_exception function
*
* called in catch blocks to croak or rethrow in perl land
*/
void handle_exception(void) {
try {
throw;
END
foreach (reverse @classes) {
my ($classname, $parent, $full_description) = split /\t/;
print F <<"END";
} catch (const Xapian::$classname & error) {
SV * errsv = get_sv("\@", TRUE);
sv_setref_pv(errsv, "Search::Xapian::$classname", (void *) new Xapian::$classname(error));
croak(Nullch);
END
}
print F <<'END';
} catch (const std::exception & error) {
croak( "std::exception: %s", error.what());
} catch (...) {
croak("something terrible happened");
}
}
END
close F or die $!;
rename "$fnm.tmp", "$fnm" or die $!;
|