3 # xdump example program.
4 # Copyright (C) 2006-2017 Markus Armbruster
6 # Empire is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Connect to server, xdump everything into tables, then print the
22 # A table is an array of records.
23 # A record is a hash mapping field names to values.
25 # This is just an example! Error handling is anemic. Tables and
26 # records just beg to be objects, but aren't.
33 # Map table name to meta table reference
34 my %meta_by_name = ();
35 # Map table UID to meta table reference
37 # Map table name to table reference
38 my %table_by_name = ();
39 # Map table UID to table reference
40 my @table_by_uid = ();
44 # The meta-type symbol table
47 my $empire = new Empire(-host => '127.0.0.1',
52 my $dumper = new Dumpvalue;
56 my $mmd = send_cmd('xdump meta meta');
58 $table_by_name{meta} = $meta_by_name{meta} = \@meta_meta;
60 parse_xdump($mmd, undef, \&store_meta_meta_record);
61 # parse for real (requires field names)
62 parse_xdump($mmd, undef);
68 $meta_by_name{table} = [];
69 parse_xdump(send_cmd('xdump meta table'), undef);
70 # table's first field is record UID, and its table field is the table uid:
71 my $tuid = $meta_by_name{table}->[0]->{table};
72 $meta_by_uid[$tuid] = $meta_by_name{table};
75 $table_by_uid[$tuid] = $table_by_name{table} = [];
76 parse_xdump(send_cmd('xdump table *'), $tuid);
79 # complete work for xdump meta meta
81 # find the meta table UID
82 my ($meta) = grep { defined $_ && $_->{name} eq 'meta' }
83 @{$table_by_name{table}};
84 my $tuid = $meta->{uid};
85 # enter it into @*_by_uid
86 $meta_by_uid[$tuid] = $meta_by_name{meta};
87 $table_by_uid[$tuid] = $table_by_name{meta};
92 # lookup record with name => 'type' in meta meta:
93 my ($mmt) = grep {$_->{name} eq 'type'} @meta_meta;
94 # its table field is UID of table meta-type:
95 my $tuid = $mmt->{table};
96 # lookup table meta-type's name:
97 my $name = $table_by_name{table}->[$tuid]->{name};
99 # xdump meta meta-type
100 $meta_by_uid[$tuid] = $meta_by_name{$name} = [];
101 parse_xdump(send_cmd("xdump meta $tuid"), $tuid);
105 $table_by_uid[$tuid] = $table_by_name{$name} = \@meta_type;
106 parse_xdump(send_cmd("xdump $tuid *"), $tuid);
110 for my $tab (@{$table_by_name{table}}) {
112 next unless exists $tab->{name};
113 # skip tables already dumped:
114 next if defined $meta_by_name{$tab->{name}};
117 $meta_by_uid[$tab->{uid}] = $meta_by_name{$tab->{name}} = [];
118 parse_xdump(send_cmd("xdump meta $tab->{uid}"), $tab->{uid});
121 $table_by_uid[$tab->{uid}] = $table_by_name{$tab->{name}} = [];
122 parse_xdump(send_cmd("xdump $tab->{uid} *"), $tab->{uid});
125 $dumper->dumpValue(\@table_by_uid);
130 $empire->empcmd($cmd);
131 my ($status, $data) = $empire->empreadline($cmd);
132 die unless ($status eq $Empire::C_PROMPT);
137 my ($data, $tuid, $store_record) = @_;
138 $store_record = \&store_record unless defined $store_record;
141 ($_, $data) = split(/\n/, $data, 2);
142 /^XDUMP (meta )?(\S+) ([0-9])+$/
144 my $meta = defined $1;
150 while ($data !~ /^\//) {
151 ($_, $data) = split(/\n/, $data, 2);
152 my @fld = split(/ /);
153 &$store_record($tuid, $meta, $name, $n, \@fld, $timestamp);
158 $data =~ /^\/([0-9]+)$/
165 my ($tuid, $meta, $name, $idx, $fld, $timestamp) = @_;
166 my ($meta_table, $table);
168 # find table to update and its meta table
170 $meta_table = \@meta_meta;
171 $table = $meta_by_name{$name};
173 $meta_table = $meta_by_name{$name};
174 $table = $table_by_name{$name};
176 if has_record_uid($tuid);
180 my $record = $table->[$idx] || {};
182 for (my $i = 0; $i <= $#{$meta_table}; $i++) {
183 my $n = is_array($meta_table->[$i]);
187 my $fldname = $meta_table->[$i]->{name};
188 die unless defined $fld->[$fldidx];
189 $record->{$fldname} = eval_fld($fld->[$fldidx++]);
192 $table->[$idx] = $record;
195 sub store_meta_meta_record {
196 my ($tuid, $meta, $name, $idx, $fld, $timestamp) = @_;
197 die unless $name eq 'meta';
198 my $val = eval_fld($fld->[0]);
199 die if $idx == 0 && $val ne 'name';
200 $meta_meta[$idx] = {name => $val, len => 0};
205 if ($fld =~ /\A[-+.0-9]/) {
206 # sloppy, doesn't flag malformed numbers
208 } elsif ($fld =~ /\A\"(.*)\"/) {
209 # sloppy, doesn't junk after string
211 $fld =~ s/\\([0-7][0-7][0-7])/ chr(oct($1)) /eg;
218 # a table has record UIDs if the first field's table is the table's UID
220 die unless defined $tuid;
221 die unless defined $meta_by_uid[$tuid];
222 die unless defined $meta_by_uid[$tuid]->[0];
223 die unless defined $meta_by_uid[$tuid]->[0]->{table};
224 return $meta_by_uid[$tuid]->[0]->{table} == $tuid;
228 my ($symtab, $value) = @_;
229 my ($sym) = grep { $_->{value} == $value } @{$symtab};
235 my $len = $meta->{len};
236 # non-zero $len means array, except for format 'c'
237 if ($len == 0 || sym_by_value(\@meta_type, $meta->{type}) eq 'c') {