Perform/DigestPer.pm untested

filter: Statementstmt 0.0% 0 / 221 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 395 CC 41 SCAR 41.4
CC
41
Cov
0%
read_screen
132.0
read_attributes
90.0
read_instructions
72.0
CRAP 62.7
1 =head1 NAME
2  
3 DBIx::Informix::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.0.1
11  
12 =head1 SYNOPSIS
13  
14 use DBIx::Informix::Perform::DigestPer;
15 $desc = digest(*INFILE_HANDLE);
16 # now do the right thing with $desc
17  
18 shell> perl -MDBIx::Informix::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::Informix::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.0.2';
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  
76 sub digest #
77 {
78 shift if ($_[0] eq __PACKAGE__);
79 my $ioh = shift;
80  
81 my $parser = new DBIx::Informix::Perform::DigestPer::Parser($ioh);
82 my $word;
83 my ($db, $tables, $atts, $instrs);
84 my $screens = [];
85 while ($word = $parser->readword('true')) {
86 if ($word eq 'database') {
87 $db = read_database($parser);
88 }
89 elsif ($word eq 'screen') {
90 push (@$screens, read_screen($parser)); # might return many
91 }
92 elsif ($word eq 'tables') {
93 $tables = read_tables($parser);
94 }
95 elsif ($word eq 'attributes') {
96 $atts = read_attributes($parser);
97 }
98 elsif ($word eq 'instructions') {
99 $instrs = read_instructions($parser);
100 }
101 }
102  
103 return outputstring($db, $screens, $tables, $atts, $instrs);
104 }
105  
106  
107 sub read_database
108 {
109 my $parser = shift;
110  
111 return $parser->readword(); # just the name.
112 }
113  
114 sub read_screen
115 {
116 my @screens;
117 my $parser = shift;
118  
119 my $result = {};
120 my $word;
121 while ($word = $parser->readword()) {
122 if ($word eq 'size') {
123 # read size...
124 my $height = 0 + $parser->readword();
125 my $by = $parser->readword();
126 my $width = 0 + $parser->readword();
127 die "Expected 'by' but got '$by'"
128 if ($by ne 'by');
129 $result->{'MINSIZE'} = [ $width, $height];
130 }
131 elsif ($word eq '{' ) {
132 # read screen format
133 my $widgets = {};
134 my $line;
135 my $lineno = 0;
136 my $labelno = '000';
137 my @fields = ();
138 my $last_line_blank;
139 my $page_split;
140 while (defined($line = $parser->readline()) && $line !~ /\}/) {
141 if ($line =~ /^\s*$/){
142 $page_split = 1 if $last_line_blank;
143 $last_line_blank = 1;
144 $lineno++;
145 next;
146 }
147 while ($line =~ /(\[\s*(\w+)\s*\]|([^\s\[]+\s?)+)/g) {
148 my $pre = $`;
149 my $post = $';
150 my $match = $1;
151 my $id = $2;
152 if ($page_split) {
153 push (@screens, { WIDGETS => $widgets,
154 FIELDS => [ @fields ],
155 LINES => $lineno,
156 });
157 $widgets = {};
158 @fields = ();
159 $lineno = 0;
160 }
161 undef $page_split;
162 undef $last_line_blank;
163 my $x = length($pre); # + $pos
164 if ($id) {
165 # it's a field
166 my $cols = length($match) - 2;
167 if (0) {
168 # Leading bracket
169 $widgets->{"${id}_openbracket"} = {
170 TYPE => 'Label', COLUMNS => 1,
171 Y => $lineno, X => $x, VALUE => '[' } ;
172 }
173 # The field...
174 $x++;
175 # Note, the OnEnter/OnExit subs must be supplied
176 # by the Perform emulator.
177 $widgets->{$id} = {
178 TYPE => 'TextField', COLUMNS => $cols,
179 Y => $lineno, X => $x, BORDER => 0};
180 $x += $cols;
181 if (0) {
182 # Trailing Bracket...
183 $widgets->{"${id}_closebracket"} = {
184 TYPE => 'Label', COLUMNS => 1,
185 Y => $lineno, X => $x, VALUE => ']' };
186 }
187 push (@fields, $id);
188 }
189 else {
190 # it's a label
191 $match =~ s/\s$//; # ignore trailing whitespace
192 my $cols = length($match);
193 $widgets->{"label_$labelno"} = {
194 TYPE => 'Label', COLUMNS => $cols,
195 Y => $lineno, X => $x, VALUE => $match };
196 $labelno++;
197 }
198 }
199 $lineno++;
200 }
201 push (@screens, { WIDGETS => $widgets,
202 FIELDS => [ @fields ],
203 LINES => $lineno} );
204 }
205 elsif (lc($word) eq 'end') {
206 return @screens;
207 }
208 else {
209 die "Unknown screen section directive '$word'";
210 }
211 }
212 return @screens;
213 }
214  
215 sub read_tables
216 {
217 my $parser = shift;
218
219 my $line;
220 my @tables;
221 while ($line = $parser->readline()) {
222 push (@tables, $line =~ /(\w+)/g);
223 }
224 return [ @tables ];
225 }
226  
227 sub read_attributes
228 {
229 my $parser = shift;
230  
231 my $line;
232 my %fields;
233 my $lines = '';
234 while ($line = $parser->readline()) {
235 chomp $line;
236 $lines .= ' ' . $line;
237 next unless $line =~ /;/;
238 my ($name, $cols, $ignore, $ignore1, $attrs) =
239 $lines =~ /\s*(\w+)((\s*=\s*\*?\w+\.\w+)+)\s*(\,\s*(.*))?;/;
240 $attrs = ''
241 unless defined($attrs);
242 my $collist = [];
243 foreach my $colspec (split /\s*=\s*/, $cols) {
244 next unless $colspec;
245 my ($verify, $tbl, $col) = $colspec =~ /(\*)?(\w+)\.(\w+)/;
246 push @$collist, [$tbl, $col, $verify];
247 }
248 my $attrhash = {};
249 while($attrs =~
250 /\s*(\w+)\s*(=\s*(\w+|\"[^\"]*\"|\((\s*\"[^\"]*\"\s*,?)*\)))?,?/g
251 ) {
252 my $atname = uc $1;
253 my $atval = $3;
254 $atval =~ s/^\"(.*)\"$/$1/;
255 $$attrhash{$atname} = defined($atval) ? $atval : 1;
256 if ($4) { # list entry
257 # digest list-valued attribute here.
258 if ($atval =~ /^\s*\((\s*\"[^\"]*\",?)*\s*\)\s*$/) {
259 my @vals = $atval =~ /\"([^\"]*)\"/g;
260 my $hash = @vals && +{ map { ($_, 1) } @vals };
261 $$attrhash{"${atname}HASH"} = $hash
262 if $hash;
263 }
264 }
265 }
266 # special notice for INCLUDE
267 warn "INCLUDE attribute (for field $name) must be a list of " .
268 "double-quoted strings."
269 if ($$attrhash{'INCLUDE'} && !$$attrhash{INCLUDEHASH});
270 $fields{$name} = [$collist, $attrhash];
271 $lines = '';
272 }
273 return { %fields };
274 }
275  
276 sub read_instructions
277 {
278 my $parser = shift;
279  
280 my $line;
281 my $instrs = {};
282 INSTRUCTION:
283 while ($line = $parser->readline()){
284 next if $line =~ /^\s*$/;
285 last if $line =~ /^\s*end\s*$/i;
286 if ($line =~ /^\s*(\w+)\s+master\s+of\s+(\w+)/i) {
287 push (@{$$instrs{MASTERS}}, [$1, $2]);
288 }
289 elsif ($line =~ /^\s*(before|after)\s+((\w+)\s+)+of\s+(\w+)\s*$/){
290 # control block
291 my $when = $1;
292 my $ops = $2;
293 my $col = $4;
294 my $action;
295 while ($action = $parser->readline()) {
296 last if $action =~ /^\s*$/;
297 last INSTRUCTION if $action =~ /^\s*end\s*$/i;
298 my @action;
299 if ((@action = $action =~ /(let)\s+(\w+)\s*=\s*(\w+)\s*(\+)\s*(\w+)/i) ||
300 (@action = $action =~ /(nextfield)\s*=\s*(\w+)/i)) {
301 $action[0] = lc($action[0]);
302 my $actionref = [ @action ];
303 while ($ops =~ /(\w+)/g) {
304 my $op = $1;
305 push (@{$$instrs{CONTROLS}{$col}{$op}{$when}},
306 $actionref);
307 }
308 }
309 else {
310 warn "Unrecognized control block action: $action";
311 warn "(only let field = field + field and nextfield = field"
312 ." are supported at this time)";
313 }
314 }
315 }
316 else {
317 warn "Unrecognized instruction line:\n$line\n";
318 }
319 }
320 return $instrs;
321 }
322
323
324
325  
326 sub outputstring
327 {
328 my $db = shift;
329 my $screens = shift;
330 my $tables = shift;
331 my $attrs = shift;
332 my $instrs = shift;
333  
334 my $form = { db => $db, screens => $screens,
335 tables => $tables, attrs => $attrs,
336 instrs => $instrs };
337 my @strs = Data::Dumper->Dump([$form], ['form']);
338 return join ($/, 'our $form;', @strs);
339 }
340  
341 =head2 digest_file
342  
343 digest_file input_filename [output_filename]
344  
345 Reads the perform spec file, and writes a Perl Perform Spec file
346 with the same basename but extension .pps unless an output filename
347 is explicitly provided. Calls "digest" in this package to do the
348 work.
349  
350 It's a little clumsy, but one can do a command-line "digestion" by:
351 perl -MDBIx::Informix::Perform::DigestPer -e'digest_file "foo.per"' .
352 Maybe a top-level Perl or shell script should be made for this purpose.
353  
354 =cut
355  
356 sub digest_file
357 {
358 my $infile = shift;
359 my $outfile = shift;
360  
361 unless ($outfile ) {
362 $outfile = $infile;
363 $outfile .= ".pps"
364 unless $outfile =~ s/\..*$/.pps/;
365 }
366  
367 open (IN, "< $infile")
368 or die "Couldn't open '$infile' for reading: $!";
369 open (OUT, "> $outfile")
370 or die "Couldn't open '$outfile' for writing: $!";
371  
372 my ($str) = digest(*IN);
373 print OUT $str;
374 print OUT "\n1;\n"; # let it be require'd
375 close(OUT);
376 }
377  
378
379  
380  
381  
382 # Our little word muncher...
383 package DBIx::Informix::Perform::DigestPer::Parser;
384  
385 sub new
386 {
387 my $class = shift;
388 my $ioh = shift;
389  
390 my $self = bless {}, $class;
391 $self->{'ioh'} = $ioh;
392 $self->{'tail'} = '';
393 return $self;
394 }
395  
396 # maybe "read token" would be a better description for this...
397 sub readword
398 {
399 my $self = shift;
400 my $accept_header_word = shift;
401  
402 my ($ioh, $tail) = @$self{'ioh','tail'};
403  
404 do {
405 my ($word) = $tail =~ /(\w+|[^\w\s]+)/;
406 if ($word) {
407 return undef
408 if $DBIx::Informix::Perform::DigestPer::HEADING_WORDS{lc($word)} &&
409 lc($word) ne 'end' &&
410 ! $accept_header_word;
411 $self->{'tail'} = $';
412 return $word;
413 }
414 $tail = <$ioh>;
415 chomp $tail;
416 $self->{'tail'} = $tail;
417 }
418 while (defined($tail));
419 return undef;
420 }
421  
422 sub unread_word {
423 my $self = shift;
424 my $word = shift;
425  
426 $self->{'tail'} = $word . $self->{'tail'};
427 }
428  
429 sub readline
430 {
431 my $self = shift;
432 my $accept_heading_word = shift;
433  
434 my $tail = $self->{'tail'};
435 return undef if $tail =~ /^\s*(\w+)\s*$/ &&
436 $DBIx::Informix::Perform::DigestPer::HEADING_WORDS{lc($1)} && lc($1) ne 'end' &&
437 !$accept_heading_word;
438 $self->{'tail'} = '';
439 return $tail if ($tail =~ /\S/);
440 my $ioh = $self->{'ioh'};
441 my $line = <$ioh>;
442 return undef unless defined($line);
443 chomp $line;
444 return (($self->{'tail'} = $line) && undef)
445 if $line =~ /^\s*(\w+)\s*$/ &&
446 $DBIx::Informix::Perform::DigestPer::HEADING_WORDS{lc($1)} && lc($1) ne 'end' &&
447 !$accept_heading_word;
448 return $line eq '' ? ' ' : $line;
449 }
450  
451  
452  
453 1;
454