| 1 | =head1 NAME | ||
| 2 | |||
| 3 | DBIx::Perform::DigestPer - "Perform" screen file digester | ||
| 4 | |||
| 5 | Digests an Informixoid .per file and make a string suitable for | ||
| 6 | writing to a file or just eval'ing. | ||
| 7 | |||
| 8 | =head1 MODULE VERSION | ||
| 9 | |||
| 10 | 0.04 | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use DBIx::Perform::DigestPer; | ||
| 15 | $desc = digest(*INFILE_HANDLE); | ||
| 16 | # now do the right thing with $desc | ||
| 17 | |||
| 18 | shell> perl -MDBIx::Perform::DigestPer -e'digest_file("foo.per")' | ||
| 19 | # writes file foo.pps or named in 2nd argument. | ||
| 20 | # now read and do the right thing with foo.pps | ||
| 21 | |||
| 22 | =head1 REQUIREMENTS | ||
| 23 | |||
| 24 | Data::Dumper | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | Digests an Informix "Perform" screen descriptor file into a form usable by | ||
| 29 | the Perform emulator B<Perform.pm>. May be used inline or to write a file. | ||
| 30 | |||
| 31 | Among other things, it digests the screen layout into a series of Curses | ||
| 32 | widget specs, as either Label or TextField types. | ||
| 33 | |||
| 34 | The output string/file is evaluable Perl source code, which | ||
| 35 | sets four variables: | ||
| 36 | |||
| 37 | $db: name of database | ||
| 38 | |||
| 39 | $screen: screen descriptor, a hash including a Curses::Forms spec. | ||
| 40 | Form fields' widgets are named as labelled (e.g. 'f000'). | ||
| 41 | |||
| 42 | $tables: array of table names. | ||
| 43 | |||
| 44 | $attrs: hash of field names to [table column attributes] . | ||
| 45 | The 'attributes' string is unparsed. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | package DBIx::Perform::DigestPer; | ||
| 50 | use strict; | ||
| 51 | use base 'Exporter'; | ||
| 52 | |||
| 53 | use vars qw(@EXPORT_OK $VERSION %HEADING_WORDS); | ||
| 54 | |||
| 55 | BEGIN | ||
| 56 | { | ||
| 57 | @EXPORT_OK = qw(digest digest_file); | ||
| 58 | $VERSION = '0.04'; | ||
| 59 | |||
| 60 | %HEADING_WORDS = | ||
| 61 | map { ($_, 1) } qw(screen tables attributes instructions end); | ||
| 62 | } | ||
| 63 | |||
| 64 | use Data::Dumper; | ||
| 65 | |||
| 66 | =head2 digest | ||
| 67 | |||
| 68 | digest (IOHandle_Ref) | ||
| 69 | |||
| 70 | Digests an Informix .per file into a string that evaluates to a Perform | ||
| 71 | descriptor. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | our $TABLES; | ||
| 76 | |||
| 77 | sub digest # | ||
| 78 | { | ||
| 79 | shift if ($_[0] eq __PACKAGE__); | ||
| 80 | my $ioh = shift; | ||
| 81 | |||
| 82 | my $parser = new DBIx::Perform::DigestPer::Parser($ioh); | ||
| 83 | my $word; | ||
| 84 | my ($db, $tables, $atts, $instrs); | ||
| 85 | my $screens = []; | ||
| 86 | local $TABLES; # for attributes parser to check | ||
| 87 | while ($word = $parser->readword('true')) { | ||
| 88 | if ($word eq 'database') { | ||
| 89 | $db = read_database($parser); | ||
| 90 | } | ||
| 91 | elsif ($word eq 'screen') { | ||
| 92 | push (@$screens, read_screen($parser)); # might return many | ||
| 93 | } | ||
| 94 | elsif ($word eq 'tables') { | ||
| 95 | $TABLES = $tables = read_tables($parser); | ||
| 96 | } | ||
| 97 | elsif ($word eq 'attributes') { | ||
| 98 | $atts = read_attributes($parser); | ||
| 99 | } | ||
| 100 | elsif ($word eq 'instructions') { | ||
| 101 | $instrs = read_instructions($parser); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | return outputstring($db, $screens, $tables, $atts, $instrs); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub read_database | ||
| 110 | { | ||
| 111 | my $parser = shift; | ||
| 112 | |||
| 113 | return $parser->readword(); # just the name. | ||
| 114 | } | ||
| 115 | |||
| 116 | sub read_screen | ||
| 117 | { | ||
| 118 | my @screens; | ||
| 119 | my $parser = shift; | ||
| 120 | |||
| 121 | my $result = {}; | ||
| 122 | my $word; | ||
| 123 | while ($word = $parser->readword()) { | ||
| 124 | if ($word eq 'size') { | ||
| 125 | # read size... | ||
| 126 | my $height = 0 + $parser->readword(); | ||
| 127 | my $by = $parser->readword(); | ||
| 128 | my $width = 0 + $parser->readword(); | ||
| 129 | die "Expected 'by' but got '$by'" | ||
| 130 | if ($by ne 'by'); | ||
| 131 | $result->{'MINSIZE'} = [ $width, $height]; | ||
| 132 | } | ||
| 133 | elsif ($word eq '{' ) { | ||
| 134 | # read screen format | ||
| 135 | my $widgets = {}; | ||
| 136 | my $line; | ||
| 137 | my $lineno = 0; | ||
| 138 | my $labelno = '000'; | ||
| 139 | my @fields = (); | ||
| 140 | my $last_line_blank; | ||
| 141 | my $page_split; | ||
| 142 | while (defined($line = $parser->readline()) && $line !~ /\}/) { | ||
| 143 | if ($line =~ /^\s*$/){ | ||
| 144 | $page_split = 1 if $last_line_blank; | ||
| 145 | $last_line_blank = 1; | ||
| 146 | $lineno++; | ||
| 147 | next; | ||
| 148 | } | ||
| 149 | while ($line =~ /(\[\s*(\w+)\s*\]|([^\s\[]+\s?)+)/g) { | ||
| 150 | my $pre = $`; | ||
| 151 | my $post = $'; #' | ||
| 152 | my $match = $1; | ||
| 153 | my $id = $2; | ||
| 154 | if ($page_split) { | ||
| 155 | push (@screens, { WIDGETS => $widgets, | ||
| 156 | FIELDS => [ @fields ], | ||
| 157 | LINES => $lineno, | ||
| 158 | }); | ||
| 159 | $widgets = {}; | ||
| 160 | @fields = (); | ||
| 161 | $lineno = 0; | ||
| 162 | } | ||
| 163 | undef $page_split; | ||
| 164 | undef $last_line_blank; | ||
| 165 | my $x = length($pre); # + $pos | ||
| 166 | if ($id) { | ||
| 167 | # it\'s a field | ||
| 168 | my $cols = length($match) - 2; | ||
| 169 | if (0) { | ||
| 170 | # Leading bracket | ||
| 171 | $widgets->{"${id}_openbracket"} = { | ||
| 172 | TYPE => 'Label', COLUMNS => 1, | ||
| 173 | Y => $lineno, X => $x, VALUE => '[' } ; | ||
| 174 | } | ||
| 175 | # The field... | ||
| 176 | $x++; | ||
| 177 | # Note, the OnEnter/OnExit subs must be supplied | ||
| 178 | # by the Perform emulator. | ||
| 179 | $widgets->{$id} = { | ||
| 180 | TYPE => 'TextField', COLUMNS => $cols, | ||
| 181 | Y => $lineno, X => $x, BORDER => 0}; | ||
| 182 | $x += $cols; | ||
| 183 | if (0) { | ||
| 184 | # Trailing Bracket... | ||
| 185 | $widgets->{"${id}_closebracket"} = { | ||
| 186 | TYPE => 'Label', COLUMNS => 1, | ||
| 187 | Y => $lineno, X => $x, VALUE => ']' }; | ||
| 188 | } | ||
| 189 | push (@fields, $id); | ||
| 190 | } | ||
| 191 | else { | ||
| 192 | # it\'s a label | ||
| 193 | $match =~ s/\s$//; # ignore trailing whitespace | ||
| 194 | my $cols = length($match); | ||
| 195 | $widgets->{"label_$labelno"} = { | ||
| 196 | TYPE => 'Label', COLUMNS => $cols, | ||
| 197 | Y => $lineno, X => $x, VALUE => $match }; | ||
| 198 | $labelno++; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | $lineno++; | ||
| 202 | } | ||
| 203 | push (@screens, { WIDGETS => $widgets, | ||
| 204 | FIELDS => [ @fields ], | ||
| 205 | LINES => $lineno} ); | ||
| 206 | } | ||
| 207 | elsif (lc($word) eq 'end') { | ||
| 208 | return @screens; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | die "Unknown screen section directive '$word'"; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | return @screens; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub read_tables | ||
| 218 | { | ||
| 219 | my $parser = shift; | ||
| 220 | |||
| 221 | my $line; | ||
| 222 | my @tables; | ||
| 223 | while ($line = $parser->readline()) { | ||
| 224 | push (@tables, $line =~ /(\w+)/g); | ||
| 225 | } | ||
| 226 | return [ @tables ]; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub read_attributes | ||
| 230 | { | ||
| 231 | my $parser = shift; | ||
| 232 | |||
| 233 | my $line; | ||
| 234 | my %fields; | ||
| 235 | my $lines = ''; | ||
| 236 | while ($line = $parser->readline()) { | ||
| 237 | chomp $line; | ||
| 238 | $lines .= ' ' . $line; | ||
| 239 | next unless $line =~ /;/; | ||
| 240 | my ($name, $cols, $ignore, $ignore1, $ignore2, $attrs) = | ||
| 241 | $lines =~ /\s*(\w+)((\s*=\s*\*?\w+\.\w+(\s*\[\d+,\d+\])?)+)\s*(\,\s*(.*))?;/; | ||
| 242 | $attrs = '' | ||
| 243 | unless defined($attrs); | ||
| 244 | my $collist = []; | ||
| 245 | foreach my $colspec (split /\s*=\s*/, $cols) { | ||
| 246 | next unless $colspec; | ||
| 247 | my ($verify, $tbl, $col, $subfield) = | ||
| 248 | $colspec =~ /(\*)?(\w+)\.(\w+)(\s*\[\d+,\d+\])?/; | ||
| 249 | my $subf; | ||
| 250 | if ($subfield) { | ||
| 251 | my ($st, $en) = $subfield =~ /\[(\d+),(\d+)\]/; | ||
| 252 | $subf = [$st, $en]; | ||
| 253 | } | ||
| 254 | push @$collist, [$tbl, $col, $verify, $subf]; | ||
| 255 | } | ||
| 256 | my $attrhash = {}; | ||
| 257 | while($attrs =~ | ||
| 258 | /\s*(\w+)\s*((\w+)?\s*=\s*(\"[^\"]*\"|\((\s*\"[^\"]*\"\s*,?)*\)|(\w+)\s+JOINING\s+(\*?)(\w+)\.(\w+)|\w+))?,?/g | ||
| 259 | ) { | ||
| 260 | my $atname = uc $1; | ||
| 261 | if (uc($atname) eq 'LOOKUP') { | ||
| 262 | my $lfield = $3; | ||
| 263 | my $lcol = $6; | ||
| 264 | my $star = $7; | ||
| 265 | my $ltable = $8; | ||
| 266 | my $jcol = $9; | ||
| 267 | $$attrhash{$atname} = [$lfield, $lcol, $star, $ltable, $jcol]; | ||
| 268 | warn "LOOKUP table $ltable not in tables list" | ||
| 269 | unless grep {$_ eq $ltable} @$TABLES; | ||
| 270 | #warn "LOOKUP field $lfield not in fields list" | ||
| 271 | # unless 0; # FIX ME! Look up in screens! | ||
| 272 | } | ||
| 273 | else { | ||
| 274 | my $atval = $4; | ||
| 275 | $atval =~ s/^\"(.*)\"$/$1/; | ||
| 276 | $$attrhash{$atname} = defined($atval) ? $atval : 1; | ||
| 277 | if ($5) { # list entry | ||
| 278 | # digest list-valued attribute here. | ||
| 279 | if ($atval =~ /^\s*\((\s*\"[^\"]*\",?)*\s*\)\s*$/) { | ||
| 280 | my @vals = $atval =~ /\"([^\"]*)\"/g; | ||
| 281 | my $hash = @vals && +{ map { ($_, 1) } @vals }; | ||
| 282 | $$attrhash{"${atname}HASH"} = $hash | ||
| 283 | if $hash; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | } | ||
| 287 | } | ||
| 288 | # special notice for INCLUDE | ||
| 289 | warn "INCLUDE attribute (for field $name) must be a list of " . | ||
| 290 | "double-quoted strings." | ||
| 291 | if ($$attrhash{'INCLUDE'} && !$$attrhash{INCLUDEHASH}); | ||
| 292 | $fields{$name} = [$collist, $attrhash]; | ||
| 293 | $lines = ''; | ||
| 294 | } | ||
| 295 | return { %fields }; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub read_instructions | ||
| 299 | { | ||
| 300 | my $parser = shift; | ||
| 301 | |||
| 302 | my $line; | ||
| 303 | my $instrs = {}; | ||
| 304 | INSTRUCTION: | ||
| 305 | while ($line = $parser->readline()){ | ||
| 306 | next if $line =~ /^\s*$/; | ||
| 307 | last if $line =~ /^\s*end\s*$/i; | ||
| 308 | if ($line =~ /^\s*(\w+)\s+master\s+of\s+(\w+)/i) { | ||
| 309 | push (@{$$instrs{MASTERS}}, [$1, $2]); | ||
| 310 | } | ||
| 311 | elsif ($line =~ /^\s*(before|after)\s+((\w+)\s+)+of\s+(\w+)\s*$/){ | ||
| 312 | # control block | ||
| 313 | my $when = $1; | ||
| 314 | my $ops = $2; | ||
| 315 | my $col = $4; | ||
| 316 | my $action; | ||
| 317 | while ($action = $parser->readline()) { | ||
| 318 | last if $action =~ /^\s*$/; | ||
| 319 | last INSTRUCTION if $action =~ /^\s*end\s*$/i; | ||
| 320 | my @action; | ||
| 321 | if ((@action = $action =~ /\s*(let)\s+(\w+)\s*=\s*(\w+)\s*([-+*\/])\s*(\w+)/i) || | ||
| 322 | (@action = $action =~ /(nextfield)\s*=\s*(\w+)/i)) { | ||
| 323 | $action[0] = lc($action[0]); | ||
| 324 | my $actionref = [ @action ]; | ||
| 325 | while ($ops =~ /(\w+)/g) { | ||
| 326 | my $op = $1; | ||
| 327 | push (@{$$instrs{CONTROLS}{$col}{$op}{$when}}, | ||
| 328 | $actionref); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | else { | ||
| 332 | warn "Unrecognized control block action: $action"; | ||
| 333 | warn "(only let field = field + field and nextfield = field" | ||
| 334 | ." are supported at this time)"; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | } | ||
| 338 | else { | ||
| 339 | warn "Unrecognized instruction line:\n$line\n"; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | return $instrs; | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | |||
| 348 | sub outputstring | ||
| 349 | { | ||
| 350 | my $db = shift; | ||
| 351 | my $screens = shift; | ||
| 352 | my $tables = shift; | ||
| 353 | my $attrs = shift; | ||
| 354 | my $instrs = shift; | ||
| 355 | |||
| 356 | my $form = { db => $db, screens => $screens, | ||
| 357 | tables => $tables, attrs => $attrs, | ||
| 358 | instrs => $instrs }; | ||
| 359 | my @strs = Data::Dumper->Dump([$form], ['form']); | ||
| 360 | return join ($/, 'our $form;', @strs); | ||
| 361 | } | ||
| 362 | |||
| 363 | =head2 digest_file | ||
| 364 | |||
| 365 | digest_file input_filename [output_filename] | ||
| 366 | |||
| 367 | Reads the perform spec file, and writes a Perl Perform Spec file | ||
| 368 | with the same basename but extension .pps unless an output filename | ||
| 369 | is explicitly provided. Calls "digest" in this package to do the | ||
| 370 | work. | ||
| 371 | |||
| 372 | It\'s a little clumsy, but one can do a command-line "digestion" by: | ||
| 373 | perl -MDBIx::Perform::DigestPer -e'digest_file "foo.per"' . | ||
| 374 | Maybe a top-level Perl or shell script should be made for this purpose. | ||
| 375 | |||
| 376 | =cut | ||
| 377 | |||
| 378 | sub digest_file | ||
| 379 | { | ||
| 380 | my $infile = shift; | ||
| 381 | my $outfile = shift; | ||
| 382 | |||
| 383 | unless ($outfile ) { | ||
| 384 | $outfile = $infile; | ||
| 385 | $outfile .= ".pps" | ||
| 386 | unless $outfile =~ s/\..*$/.pps/; | ||
| 387 | } | ||
| 388 | |||
| 389 | open (IN, "< $infile") | ||
| 390 | or die "Couldn't open '$infile' for reading: $!"; | ||
| 391 | open (OUT, "> $outfile") | ||
| 392 | or die "Couldn't open '$outfile' for writing: $!"; | ||
| 393 | |||
| 394 | my ($str) = digest(*IN); | ||
| 395 | print OUT $str; | ||
| 396 | print OUT "\n1;\n"; # let it be require'd | ||
| 397 | close(OUT); | ||
| 398 | } | ||
| 399 | |||
| 400 | |||
| 401 | |||
| 402 | |||
| 403 | |||
| 404 | # Our little word muncher... | ||
| 405 | package DBIx::Perform::DigestPer::Parser; | ||
| 406 | |||
| 407 | sub new | ||
| 408 | { | ||
| 409 | my $class = shift; | ||
| 410 | my $ioh = shift; | ||
| 411 | |||
| 412 | my $self = bless {}, $class; | ||
| 413 | $self->{'ioh'} = $ioh; | ||
| 414 | $self->{'tail'} = ''; | ||
| 415 | return $self; | ||
| 416 | } | ||
| 417 | |||
| 418 | # maybe "read token" would be a better description for this... | ||
| 419 | sub readword | ||
| 420 | { | ||
| 421 | my $self = shift; | ||
| 422 | my $accept_header_word = shift; | ||
| 423 | |||
| 424 | my ($ioh, $tail) = @$self{'ioh','tail'}; | ||
| 425 | |||
| 426 | do { | ||
| 427 | my ($word) = $tail =~ /(\w+|[^\w\s]+)/; | ||
| 428 | if ($word) { | ||
| 429 | return undef | ||
| 430 | if $DBIx::Perform::DigestPer::HEADING_WORDS{lc($word)} && | ||
| 431 | lc($word) ne 'end' && | ||
| 432 | ! $accept_header_word; | ||
| 433 | $self->{'tail'} = $'; #' | ||
| 434 | return $word; | ||
| 435 | } | ||
| 436 | $tail = <$ioh>; | ||
| 437 | chomp $tail; | ||
| 438 | $self->{'tail'} = $tail; | ||
| 439 | } | ||
| 440 | while (defined($tail)); | ||
| 441 | return undef; | ||
| 442 | } | ||
| 443 | |||
| 444 | sub unread_word { | ||
| 445 | my $self = shift; | ||
| 446 | my $word = shift; | ||
| 447 | |||
| 448 | $self->{'tail'} = $word . $self->{'tail'}; | ||
| 449 | } | ||
| 450 | |||
| 451 | sub readline | ||
| 452 | { | ||
| 453 | my $self = shift; | ||
| 454 | my $accept_heading_word = shift; | ||
| 455 | |||
| 456 | my $tail = $self->{'tail'}; | ||
| 457 | return undef if $tail =~ /^\s*(\w+)\s*$/ && | ||
| 458 | $DBIx::Perform::DigestPer::HEADING_WORDS{lc($1)} && lc($1) ne 'end' && | ||
| 459 | !$accept_heading_word; | ||
| 460 | $self->{'tail'} = ''; | ||
| 461 | return $tail if ($tail =~ /\S/); | ||
| 462 | my $ioh = $self->{'ioh'}; | ||
| 463 | my $line = <$ioh>; | ||
| 464 | return undef unless defined($line); | ||
| 465 | chomp $line; | ||
| 466 | return (($self->{'tail'} = $line) && undef) | ||
| 467 | if $line =~ /^\s*(\w+)\s*$/ && | ||
| 468 | $DBIx::Perform::DigestPer::HEADING_WORDS{lc($1)} && lc($1) ne 'end' && | ||
| 469 | !$accept_heading_word; | ||
| 470 | return $line eq '' ? ' ' : $line; | ||
| 471 | } | ||
| 472 | |||
| 473 | |||
| 474 | |||
| 475 | 1; | ||
| 476 |