| 1 | # instructions | ||
| 2 | # These functions do the instructions part of a Perform script | ||
| 3 | # Instructions are grouped into blocks: | ||
| 4 | # before, after, on beginning, on ending | ||
| 5 | # Each block may contain actions: | ||
| 6 | # nextfield, abort, comments, let, if-then-else, call | ||
| 7 | # | ||
| 8 | # Brenton Chapin | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | |||
| 12 | our $VERSION = '0.694'; | ||
| 13 | |||
| 14 | our $GlobalUi = $DBIx::Perform::GlobalUi; | ||
| 15 | our $DB = $DBIx::Perform::DB; | ||
| 16 | |||
| 17 | # trigger on column, table.column, and maybe table of the given field object | ||
| 18 | sub trigger_ctrl_blk_fld | ||
| 19 | { | ||
| 20 | my $when = shift; # one of "before" or "after" | ||
| 21 | my $event = shift; | ||
| 22 | my $fo = shift; | ||
| 23 | my ( $field_tag, $table, $column_name) = $fo->get_names; | ||
| 24 | |||
| 25 | my $app = $GlobalUi->{app_object}; | ||
| 26 | if ($::TRACE) { | ||
| 27 | my $tbl = ''; | ||
| 28 | $tbl = " $table" if $app->{fresh}; | ||
| 29 | warn "TRACE: entering trigger_ctrl_blk_fld $when $table.$column_name$tbl\n"; | ||
| 30 | } | ||
| 31 | |||
| 32 | my @ofs = ($column_name, "$table.$column_name"); | ||
| 33 | push @ofs, $table if $app->{'fresh'}; | ||
| 34 | $app->{'fresh'} = 0; | ||
| 35 | |||
| 36 | return trigger_ctrl_blk($when, $event, @ofs); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub trigger_ctrl_blk | ||
| 40 | { | ||
| 41 | my $when = lc(shift); # one of "before" or "after" | ||
| 42 | my $event = shift; | ||
| 43 | my @ofs = @_; | ||
| 44 | |||
| 45 | #warn "TRACE: entering trigger for '$when $event'\n" | ||
| 46 | # . join (", ", @ofs) . "\n" if $::TRACE; | ||
| 47 | |||
| 48 | my $app = $GlobalUi->{app_object}; | ||
| 49 | my $instrs = $app->getField('instrs'); | ||
| 50 | my $controls = $instrs->{'CONTROLS'}; | ||
| 51 | |||
| 52 | #my @actions = map {$controls->{$_}{$event}{$when}} @cols; | ||
| 53 | my @actions; | ||
| 54 | |||
| 55 | foreach (@ofs) { | ||
| 56 | if ($controls->{$_}) { # if ctrl block exists | ||
| 57 | if ($controls->{$_}{$event}) { # if the col has an event defined | ||
| 58 | my $act = $controls->{$_}{$event}{$when}; # get actions | ||
| 59 | push @actions,$act if $act; # if defined, add it to the array | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | return "\c0" unless @actions; | ||
| 65 | |||
| 66 | warn "$when $event of " . join(', ', @ofs) . "\n" if $::TRACE_DATA; | ||
| 67 | |||
| 68 | #warn "TRACE: leaving trigger\n" if $::TRACE; | ||
| 69 | |||
| 70 | return act(@actions); | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub goto_nextfield { | ||
| 75 | my $field_tag = shift; | ||
| 76 | |||
| 77 | my $app = $GlobalUi->{app_object}; | ||
| 78 | my $form = $GlobalUi->get_current_form; | ||
| 79 | my $old_tag = $GlobalUi->{'focus'}; | ||
| 80 | if ($old_tag ne $field_tag) { | ||
| 81 | my $subform = $form->getSubform('DBForm'); | ||
| 82 | my $widget = $subform->getWidget($old_tag); | ||
| 83 | $widget->{CONF}->{'EXIT'} = 1; | ||
| 84 | my $scrns = get_screen_from_tag($field_tag); | ||
| 85 | my $scrn = $$scrns[0]; | ||
| 86 | goto_screen("Run$scrn"); | ||
| 87 | $form = $app->getForm("Run$scrn"); | ||
| 88 | $subform = $form->getSubform('DBForm'); | ||
| 89 | $subform->setField('FOCUSED', $field_tag); | ||
| 90 | } | ||
| 91 | $GlobalUi->{'newfocus'} = $field_tag | ||
| 92 | unless $GlobalUi->{'newfocus'}; | ||
| 93 | } | ||
| 94 | |||
| 95 | # The actions are stored in an array. | ||
| 96 | # Intent is to handle nested if-then-else actions | ||
| 97 | # by calling this function recursively | ||
| 98 | sub act | ||
| 99 | { | ||
| 100 | my $actions = shift; | ||
| 101 | |||
| 102 | my $app = $GlobalUi->{app_object}; | ||
| 103 | |||
| 104 | foreach my $act (@$actions) { | ||
| 105 | my ($ac, $field_tag, $perfexpr, $perfexpr2) = @$act; | ||
| 106 | if ($::TRACE) { | ||
| 107 | my $ft_print = $field_tag; | ||
| 108 | $ft_print = Data::Dumper->Dump([$field_tag], ['tag']) if ref $field_tag; | ||
| 109 | my $pe_print = $perfexpr; | ||
| 110 | $pe_print = Data::Dumper->Dump([$perfexpr], ['expr']) if ref $perfexpr; | ||
| 111 | warn "action :$ac:$ft_print:$pe_print:\n"; | ||
| 112 | } | ||
| 113 | if ($ac eq 'nextfield'){ | ||
| 114 | return "\c[" if (lc($field_tag) eq 'exitnow'); | ||
| 115 | goto_nextfield($field_tag); | ||
| 116 | } | ||
| 117 | elsif ($ac eq 'abort') { | ||
| 118 | return "\cC"; | ||
| 119 | } | ||
| 120 | elsif ($ac eq 'comments') { | ||
| 121 | #ANSI Escape sequence to reverse text sometimes doesn't work | ||
| 122 | # $perfexpr = "\e[7m". $perfexpr . "\e[0m" | ||
| 123 | # if ($field_tag =~ /reverse/i); | ||
| 124 | my $rr = $field_tag !~ /reverse/i; | ||
| 125 | $GlobalUi->display_error ( $perfexpr, $rr ); | ||
| 126 | beep if ($field_tag =~ /bell/i); | ||
| 127 | } | ||
| 128 | elsif ($ac eq 'let') { | ||
| 129 | my $val = do_the_math($perfexpr); | ||
| 130 | my $fo = DBIx::Perform::get_field_object_from_tag($field_tag); | ||
| 131 | $fo->set_value($val); | ||
| 132 | my ($rval, $rc) = $fo->format_value_for_display($val); | ||
| 133 | $GlobalUi->set_screen_value($field_tag, $rval); | ||
| 134 | DBIx::Perform::trigger_lookup($field_tag); | ||
| 135 | } | ||
| 136 | elsif ($ac eq 'if') { | ||
| 137 | my $val = do_the_math($field_tag); | ||
| 138 | if ($val) { | ||
| 139 | return act($perfexpr); | ||
| 140 | } else { | ||
| 141 | return act($perfexpr2); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | elsif ($ac eq 'call') { | ||
| 145 | my $parmlist = do_the_math($perfexpr); | ||
| 146 | my @parms = split /,/, $parmlist; | ||
| 147 | # push @parms, "''" unless defined @parms; | ||
| 148 | warn "call to " . $field_tag . ", parameters :" | ||
| 149 | . $parmlist .":\n" if $::TRACE; | ||
| 150 | call_extern_C_func($field_tag, 0, \@parms); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | return "\c0"; | ||
| 154 | } | ||
| 155 | |||
| 156 | #takes a template of an expression (in $math) and fills in the | ||
| 157 | #"variables" (field-tags) given in @$fts with their current values. | ||
| 158 | #Also fills in the string literals and regex literals from @$strs. | ||
| 159 | # | ||
| 160 | #FIX Need way to handle both strings and numbers. | ||
| 161 | # This will probably break if the Perform script has | ||
| 162 | # "if (a = b)" and a and b are fields that turn out to contain | ||
| 163 | # strings rather than numbers. | ||
| 164 | sub do_the_math | ||
| 165 | { | ||
| 166 | my $parms = shift; | ||
| 167 | my ($math, $strs, $fts) = @$parms; | ||
| 168 | #warn "expr :$math:\n"; | ||
| 169 | #warn "strings: " . join (' ', @$strs) . "\n"; | ||
| 170 | #warn "fields: " . join (' ', @$fts) . "\n"; | ||
| 171 | my ($si, $vi) = (0, 0); | ||
| 172 | my $result; | ||
| 173 | # my $e = '$result = '; | ||
| 174 | my @e; | ||
| 175 | my $i; | ||
| 176 | my $nest = 0; | ||
| 177 | my $fflag = 0; | ||
| 178 | my (@funcs, @fnest, @fparms); | ||
| 179 | push @fnest, -1; | ||
| 180 | |||
| 181 | my @m = split //, $math; | ||
| 182 | for ($i=0; $i < length $math; $i++) { | ||
| 183 | my $c = $m[$i]; | ||
| 184 | if ($c eq '"') { | ||
| 185 | $e[$#fnest] .= @$strs[$si++]; | ||
| 186 | } | ||
| 187 | elsif ($c eq 'v') { | ||
| 188 | # my $v = get_value_from_tag(@$fts[$vi++]); | ||
| 189 | my $v = $GlobalUi->get_screen_value(@$fts[$vi++]); | ||
| 190 | #warn "field :@$fts[$vi-1]: = :$v:\n"; | ||
| 191 | $v =~ s/'/\\'/g; | ||
| 192 | $v = "'" . $v . "'" unless DBIx::Perform::DigestPer::is_number($v); | ||
| 193 | $e[$#fnest] .= $v; | ||
| 194 | } | ||
| 195 | elsif ($c eq 'c') { | ||
| 196 | push @funcs, @$fts[$vi++]; | ||
| 197 | $fflag = 1; | ||
| 198 | } | ||
| 199 | elsif ($c eq '(') { | ||
| 200 | if ($fflag) { | ||
| 201 | push @fnest, $nest; | ||
| 202 | } else { | ||
| 203 | $e[$#fnest] .= $c; | ||
| 204 | } | ||
| 205 | $fflag = 0; | ||
| 206 | $nest++; | ||
| 207 | } | ||
| 208 | elsif ($c eq ')') { | ||
| 209 | $nest--; | ||
| 210 | if ($nest == $fnest[$#fnest]) { | ||
| 211 | $e[$#fnest] = '$result = [' . $e[$#fnest] . ']'; | ||
| 212 | eval $e[$#fnest]; | ||
| 213 | pop @fnest; | ||
| 214 | my $fname = pop @funcs; | ||
| 215 | my $v = call_extern_C_func($fname, 1, $result); | ||
| 216 | warn "return val from C function = :$v:\n" if $::TRACE; | ||
| 217 | $v =~ s/'/\\'/g; | ||
| 218 | $v = "'" . $v . "'" | ||
| 219 | unless DBIx::Perform::DigestPer::is_number($v); | ||
| 220 | $e[$#fnest] .= $v; | ||
| 221 | } else { | ||
| 222 | $e[$#fnest] .= $c; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | $e[$#fnest] .= $c; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | warn "evaluating :$e[0]:\n" if $::TRACE; | ||
| 230 | $e[0] = "''" unless $e[0] =~ /\S/; | ||
| 231 | $e[0] = '$result = ' . $e[0]; | ||
| 232 | eval $e[0]; | ||
| 233 | $result = '' unless defined $result; | ||
| 234 | warn "result = :$result:\n" if $::TRACE; | ||
| 235 | return $result; | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | #removes all string literals to an array, and remove all comments. | ||
| 240 | # leaves " in place of each string | ||
| 241 | sub pull_strings | ||
| 242 | { | ||
| 243 | my $strung = shift; | ||
| 244 | my @strs; | ||
| 245 | my $unstrung = $strung; | ||
| 246 | my $strb; | ||
| 247 | |||
| 248 | #remove \" by removing all \. | ||
| 249 | $unstrung =~ s/\\./xx/g; | ||
| 250 | |||
| 251 | my $e = length $strung; | ||
| 252 | # $i is index into the string, $q = 1 (q for "quote") if inside | ||
| 253 | # a string literal, and $r = 1 (r for "remark") if inside a comment | ||
| 254 | my ($i, $j, $q, $r) = (0,0,0,0); | ||
| 255 | my @ca = split //, $unstrung; | ||
| 256 | my @cb; | ||
| 257 | my $c; | ||
| 258 | for ($i = 0; $i < $e; $i++) { | ||
| 259 | $c = $ca[$i]; | ||
| 260 | $r = 1 if ($c eq '{' && !$q); | ||
| 261 | if ($c eq '"' && !$r) { | ||
| 262 | #entering or leaving a string literal (and not inside a comment) | ||
| 263 | $q ^= 1; | ||
| 264 | if ($q) { | ||
| 265 | $strb = $i+1; | ||
| 266 | $cb[$j++] = '"'; | ||
| 267 | } else { | ||
| 268 | $ca[$i] = 'x'; | ||
| 269 | push @strs, substr($strung, $strb, $i-$strb); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | else { | ||
| 273 | if ($q || $r) { | ||
| 274 | $ca[$i] = 'x'; | ||
| 275 | } else { | ||
| 276 | $cb[$j++] = $c; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | $r = 0 if ($c eq '}' && $r); | ||
| 280 | } | ||
| 281 | if ($q) { | ||
| 282 | warn "ERROR: no ending \" for string literal\n"; | ||
| 283 | } | ||
| 284 | $unstrung = join '', @ca; | ||
| 285 | return ($unstrung, @strs); | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | our $extern_started = 0; | ||
| 290 | our ($sock, $asock); | ||
| 291 | use IO::Socket::UNIX; | ||
| 292 | sub call_extern_C_func | ||
| 293 | { | ||
| 294 | my $funcname = shift; | ||
| 295 | my $is_retval = shift; | ||
| 296 | my $parms = shift; | ||
| 297 | my $extern_exe; | ||
| 298 | |||
| 299 | if (! $extern_started) { | ||
| 300 | warn "call_extern_C_func: parms = :" . join ("\n", @$parms) .":\n" if $::TRACE; | ||
| 301 | |||
| 302 | if (!defined $DBIx::Perform::extern_name) { | ||
| 303 | warn "Error: This Perform script uses external C functions\n" . | ||
| 304 | "and no C program was specified.\n"; | ||
| 305 | return 0; | ||
| 306 | } | ||
| 307 | $extern_exe = "$DBIx::Perform::extern_name"; | ||
| 308 | unless (-e $extern_exe) { | ||
| 309 | warn "can't find external program '$extern_exe'\n"; | ||
| 310 | return 0; | ||
| 311 | } | ||
| 312 | unless (-x $extern_exe) { | ||
| 313 | warn "external program '$extern_exe' is not executable\n"; | ||
| 314 | return 0; | ||
| 315 | } | ||
| 316 | |||
| 317 | warn "call_extern_C_func: found $extern_exe\n" if $::TRACE; | ||
| 318 | |||
| 319 | socket($sock, AF_UNIX, SOCK_STREAM, PF_UNSPEC); | ||
| 320 | my $socknm = extern_socket_name(); | ||
| 321 | my $saddr = sockaddr_un($socknm); | ||
| 322 | unlink $socknm; | ||
| 323 | bind $sock, $saddr or die "can't bind to $socknm\n"; | ||
| 324 | # 2 is just a guess. Number of connections probably never > 1 | ||
| 325 | listen $sock, 2 or die "can't listen to $socknm\n"; | ||
| 326 | |||
| 327 | my @cline = ($extern_exe, $socknm); | ||
| 328 | if (!fork) { | ||
| 329 | # my $child_db = $DB->clone(); | ||
| 330 | $DB->{InactiveDestroy} = 1; | ||
| 331 | # $DB->disconnect; | ||
| 332 | undef $DB; | ||
| 333 | exec(@cline); | ||
| 334 | die "ERROR: exec failed! err = $!\n"; | ||
| 335 | } | ||
| 336 | |||
| 337 | accept($asock, $sock); | ||
| 338 | warn "socket connected, using $socknm\n" if $::TRACE; | ||
| 339 | $extern_started = 1; | ||
| 340 | |||
| 341 | } | ||
| 342 | if (! $extern_started) { | ||
| 343 | warn "cannot access external program $extern_exe\n"; | ||
| 344 | return 0; | ||
| 345 | } | ||
| 346 | |||
| 347 | #send out all field-tags and current values | ||
| 348 | my @data_to = extern_data_out($funcname, $is_retval, $parms); | ||
| 349 | my $i; | ||
| 350 | for ($i = 0; $i < @data_to; $i++) { | ||
| 351 | warn "P: sending $data_to[$i]\n" if $::TRACE_DATA; | ||
| 352 | send ($asock, $data_to[$i] . "\n", 0); | ||
| 353 | } | ||
| 354 | send ($asock, ";\n", 0); | ||
| 355 | warn "sent data to C program, waiting for results\n" if $::TRACE; | ||
| 356 | my $more = 1; | ||
| 357 | my $din = ''; | ||
| 358 | my $lin = ''; | ||
| 359 | my $rcvd; | ||
| 360 | my @data_from; | ||
| 361 | do { | ||
| 362 | warn "P: calling recv()\n" if $::TRACE_DATA; | ||
| 363 | recv ($asock, $rcvd, 4096, 0); | ||
| 364 | $din .= $rcvd; | ||
| 365 | warn "P: recv buffer :$rcvd:\ndin = :$din:\n" if $::TRACE_DATA; | ||
| 366 | while ($din =~ /\A([^\n]*)\n((.|\n)*)\z/) { | ||
| 367 | $lin = $1; | ||
| 368 | $din = $2; | ||
| 369 | warn "P: received 1:$lin:2:$din:3\n" if $::TRACE_DATA; | ||
| 370 | if ($lin =~ /^;/) { | ||
| 371 | $more = 0; | ||
| 372 | } else { | ||
| 373 | push @data_from, $lin; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | } while ($more); | ||
| 377 | warn "exiting call_extern_C_func\n" if $::TRACE; | ||
| 378 | return extern_data_in(@data_from); | ||
| 379 | } | ||
| 380 | |||
| 381 | sub extern_socket_name { | ||
| 382 | return "/tmp/perl_perform.$ENV{USER}.$$"; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub extern_exit { | ||
| 386 | warn "exiting external program\n" if $::TRACE; | ||
| 387 | if ($extern_started) { | ||
| 388 | send($asock, ".\n", 0); | ||
| 389 | close($asock); | ||
| 390 | my $socknm = extern_socket_name(); | ||
| 391 | unlink $socknm; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | #assemble data for external program | ||
| 396 | #trying to keep the format simple, so everything depends on the first | ||
| 397 | # char of each line: | ||
| 398 | # [A-Za-z] field-tag value | ||
| 399 | # & name of function to call | ||
| 400 | # @ name of database | ||
| 401 | # < parameter for function (can be 0, 1, more than 1) | ||
| 402 | # > return value | ||
| 403 | # This means, can't have any CR/LF characters in the values or parameters | ||
| 404 | # (could use \ to escape those) | ||
| 405 | sub extern_data_out | ||
| 406 | { | ||
| 407 | my $funcname = shift; | ||
| 408 | my $is_retval = shift; | ||
| 409 | my $parms = shift; | ||
| 410 | my @data_o; | ||
| 411 | my $fl = $GlobalUi->get_field_list; | ||
| 412 | my $app = $GlobalUi->{app_object}; | ||
| 413 | |||
| 414 | push @data_o, "\@$ENV{DB_NAME}"; | ||
| 415 | push @data_o, "\&$funcname"; | ||
| 416 | push @data_o, ">" if $is_retval; | ||
| 417 | my $i; | ||
| 418 | for ($i = 0; $i < @$parms; $i++) { | ||
| 419 | push @data_o, "<@$parms[$i]"; | ||
| 420 | } | ||
| 421 | |||
| 422 | $fl->reset; | ||
| 423 | while ( my $fo = $fl->iterate_list ) | ||
| 424 | { | ||
| 425 | my $ft = $fo->get_field_tag; | ||
| 426 | my $scrns = get_screen_from_tag($ft); | ||
| 427 | my $scrn = $$scrns[0]; | ||
| 428 | my $form = $app->getForm("Run$scrn"); | ||
| 429 | my $subform = $form->getSubform('DBForm'); | ||
| 430 | my $val = $subform->getWidget($ft)->getField('VALUE'); | ||
| 431 | push @data_o, "$ft $val"; | ||
| 432 | } | ||
| 433 | return @data_o; | ||
| 434 | } | ||
| 435 | |||
| 436 | sub extern_data_in | ||
| 437 | { | ||
| 438 | my @data_i = shift; | ||
| 439 | my $rv; | ||
| 440 | my $i; | ||
| 441 | for ($i = $#data_i; $i >= 0; $i--) { | ||
| 442 | warn "extern_data_in: :$data_i[$i]:\n" if $::TRACE; | ||
| 443 | if ($data_i[$i] =~ /^>(.*?)$/) { | ||
| 444 | $rv = $1; | ||
| 445 | } | ||
| 446 | elsif ($data_i[$i] =~ /^$/) { | ||
| 447 | next; | ||
| 448 | } | ||
| 449 | elsif ($data_i[$i] =~ /^;$/) { | ||
| 450 | last; | ||
| 451 | } | ||
| 452 | elsif ($data_i[$i] =~ /^[^A-Za-z]?$/) { | ||
| 453 | #shouldn't happen | ||
| 454 | die "Error: Reception from C function is wrong.\n"; | ||
| 455 | } | ||
| 456 | else { | ||
| 457 | $data_i[$i] =~ /^([\S]+)\s(.*?)$/; | ||
| 458 | my $field_tag = $1; | ||
| 459 | my $val = $2; | ||
| 460 | warn ":$field_tag: = :$val:\n" if $::TRACE_DATA; | ||
| 461 | if ($field_tag eq 'nextfield') { | ||
| 462 | goto_nextfield($val); | ||
| 463 | } else { | ||
| 464 | my $fo = DBIx::Perform::get_field_object_from_tag($field_tag); | ||
| 465 | if (defined $fo) { | ||
| 466 | $fo->set_value($val); | ||
| 467 | my ($pos, $rc); | ||
| 468 | ($val, $rc) = $fo->format_value_for_display($val); | ||
| 469 | $GlobalUi->set_screen_value($field_tag, $val); | ||
| 470 | } | ||
| 471 | } | ||
| 472 | } | ||
| 473 | } | ||
| 474 | $GlobalUi->redraw_subform; | ||
| 475 | return $rv; | ||
| 476 | } | ||
| 477 | |||
| 478 | 1; |