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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
|
/*
* cook - file construction tool
* Copyright (C) 1997, 1998, 1999, 2000, 2001 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to manipulate opcode contexts
*/
#include <ac/errno.h>
#include <ac/stddef.h>
#include <sys/wait.h>
#include <desist.h>
#include <error_intl.h>
#include <id.h>
#include <id/global.h>
#include <id/variable.h>
#include <match/stack.h>
#include <mem.h>
#include <opcode.h>
#include <opcode/context.h>
#include <opcode/list.h>
#include <opcode/thread-id.h>
#include <option.h>
#include <os/wait.h>
#include <str_list.h>
#include <symtab.h>
#include <trace.h>
/*
* NAME
* opcode_context_new
*
* SYNOPSIS
* opcode_context_ty *opcode_context_new(opcode_list_ty *);
*
* DESCRIPTION
* The opcode_context_new function is used to create a new instance
* of an opcode context in dynamic memory, for the purpose of
* executing the given opcode stream.
*
* CAVEAT
* Use opcode_context_delete when you are done with it.
*
* The opcode list is expected to endure until executing
* terminates. The reference count in the opcode list is not
* exploited.
*/
opcode_context_ty *
opcode_context_new(olp, mp)
opcode_list_ty *olp;
const match_ty *mp;
{
opcode_context_ty *ocp;
trace(("opcode_context_new(olp = %08lX)\n{\n"/*}*/, (long)olp));
ocp = mem_alloc(sizeof(opcode_context_ty));
ocp->call_stack_length = 0;
ocp->call_stack_maximum = 0;
ocp->call_stack = 0;
ocp->value_stack_length = 0;
ocp->value_stack_maximum = 0;
ocp->value_stack = 0;
ocp->thread_id = opcode_thread_id_borrow();
ocp->msp = 0;
ocp->pid = 0;
ocp->exit_status = 0;
ocp->rup = 0;
ocp->wlp = 0;
ocp->need_age = 0;
ocp->flags = 0;
ocp->mp = 0;
ocp->thread_stp = 0;
ocp->host_binding = 0;
ocp->gp = 0;
opcode_context_match_push(ocp, mp);
if (olp)
opcode_context_call(ocp, olp);
trace((/*{*/"}\n"));
return ocp;
}
/*
* NAME
* opcode_context_delete
*
* SYNOPSIS
* void opcode_context_delete(opcode_context_ty *);
*
* DESCRIPTION
* The opcode_context_delete function is used to release the
* resources held by an opcode context.
*/
void
opcode_context_delete(ocp)
opcode_context_ty *ocp;
{
trace(("opcode_context_delete(ocp = %08lX)\n{\n"/*}*/, (long)ocp));
if (ocp->call_stack)
mem_free(ocp->call_stack);
ocp->call_stack_length = 0;
ocp->call_stack_maximum = 0;
ocp->call_stack = 0;
while (ocp->value_stack_length > 0)
{
string_list_ty *slp;
slp = opcode_context_string_list_pop(ocp);
string_list_delete(slp);
}
if (ocp->value_stack)
mem_free(ocp->value_stack);
ocp->value_stack_length = 0;
ocp->value_stack_maximum = 0;
ocp->value_stack = 0;
if (ocp->rup)
mem_free(ocp->rup);
if (ocp->thread_stp)
symtab_free(ocp->thread_stp);
if (ocp->msp)
match_stack_delete(ocp->msp);
trace(("mark\n"));
if (ocp->flags)
mem_free(ocp->flags);
/* do not free the match pointer, it does not belong to us */
if (ocp->host_binding)
str_free(ocp->host_binding);
opcode_thread_id_return(ocp->thread_id);
mem_free(ocp);
trace((/*{*/"}\n"));
}
/*
* NAME
* opcode_context_execute_inner
*
* SYNOPSIS
* opcode_status_ty opcode_context_execute_inner(opcode_context_ty *ocp,
* opcode_status_ty (*func)(const opcode_ty *,
* opcode_context_ty *));
*
* DESCRIPTION
* The opcode_context_execute_inner function is used to drive the
* execution of an opcode context until it terminates.
*
* RETURNS
* opcode_status_ty to indicate the result
*
* CAVEAT
* Some termination states are restartable.
*/
static opcode_status_ty opcode_context_execute_inner _((opcode_context_ty *ocp,
opcode_status_ty (*)(const opcode_ty *, opcode_context_ty *)));
static opcode_status_ty
opcode_context_execute_inner(ocp, func)
opcode_context_ty *ocp;
opcode_status_ty (*func)_((const opcode_ty *, opcode_context_ty *));
{
opcode_status_ty status;
/*
* keep executing until the last call exits
*/
trace(("opcode_context_execute(ocp = %08lX)\n{\n"/*}*/, (long)ocp));
status = opcode_status_success;
while (ocp->call_stack_length > 0)
{
opcode_frame_ty *frame;
const opcode_ty *op;
/*
* stop if we have been asked to
*/
if (desist_requested())
{
status = opcode_status_interrupted;
break;
}
/*
* If we have run off the end of the opcodes, we have
* reached an implicit return.
*/
frame = &ocp->call_stack[ocp->call_stack_length - 1];
if (frame->pc >= frame->olp->length)
{
trace(("rtn\n"));
ocp->call_stack_length--;
if (frame->stp)
symtab_free(frame->stp);
frame->stp = 0;
frame->olp = 0;
frame->pc = 0;
continue;
}
/*
* run the opcode
*/
trace(("pc = %ld;\n", (long)frame->pc));
op = frame->olp->list[frame->pc++];
status = func(op, ocp);
if (status != opcode_status_success)
{
/* back-up so can re-start */
frame->pc--;
break;
}
}
trace(("return %s;\n", opcode_status_name(status)));
trace((/*{*/"}\n"));
return status;
}
/*
* NAME
* opcode_context_execute
*
* SYNOPSIS
* opcode_status_ty opcode_context_execute(opcode_context_ty *);
*
* DESCRIPTION
* The opcode_context_execute function is used to execute an opcode
* context until it terminates.
*
* RETURNS
* opcode_status_ty to indicate the result
*
* CAVEAT
* Some termination states are restartable.
*/
opcode_status_ty
opcode_context_execute(ocp)
opcode_context_ty *ocp;
{
return opcode_context_execute_inner(ocp, opcode_execute);
}
/*
* NAME
* opcode_context_execute_nowait
*
* SYNOPSIS
* opcode_status_ty opcode_context_execute_nowait(opcode_context_ty *);
*
* DESCRIPTION
* The opcode_context_execute_nowait function is used to execute an
* opcode context until it terminates. You will never see the
* opcode_status_wait return code.
*
* RETURNS
* opcode_status_ty to indicate the result
*
* CAVEAT
* Some termination states are restartable.
*/
opcode_status_ty
opcode_context_execute_nowait(ocp)
opcode_context_ty *ocp;
{
opcode_status_ty status;
trace(("opcode_context_execute_nowait(ocp = %08lX)\n{\n"/*}*/,
(long)ocp));
for (;;)
{
status = opcode_context_execute(ocp);
switch (status)
{
case opcode_status_wait:
for (;;)
{
int pid;
pid = os_waitpid(ocp->pid, &ocp->exit_status);
if (pid < 0)
{
sub_context_ty *scp;
if (errno == EINTR)
continue;
scp = sub_context_new();
sub_errno_set(scp);
fatal_intl(scp, i18n("wait(): $errno"));
/* NOTREACHED */
}
assert(pid == ocp->pid);
break;
}
continue;
case opcode_status_success:
case opcode_status_error:
case opcode_status_interrupted:
break;
}
break;
}
trace(("return %s;\n", opcode_status_name(status)));
trace((/*{*/"}\n"));
return status;
}
/*
* NAME
* opcode_context_script
*
* SYNOPSIS
* opcode_status_ty opcode_context_script(opcode_context_ty *);
*
* DESCRIPTION
* The opcode_context_script function is used to script an opcode
* context until it terminates.
*
* RETURNS
* opcode_status_ty to indicate the result
*/
opcode_status_ty
opcode_context_script(ocp)
opcode_context_ty *ocp;
{
return opcode_context_execute_inner(ocp, opcode_script);
}
/*
* NAME
* opcode_context_call
*
* SYNOPSIS
* void opcode_context_call(opcode_context_ty *, opcode_list_ty *);
*
* DESCRIPTION
* The opcode_context_call function is used to perform a function
* call. The call stack and value stack are separate.
*
* CAVEAT
* To be used only internally to the interpratation by individial
* opcodes.
*/
void
opcode_context_call(ocp, olp)
opcode_context_ty *ocp;
opcode_list_ty *olp;
{
opcode_frame_ty *frame;
trace(("opcode_context_call(ocp = %08lX, olp = %08lX)\n{\n"/*}*/,
(long)ocp, (long)olp));
if (ocp->call_stack_length >= ocp->call_stack_maximum)
{
size_t nbytes;
ocp->call_stack_maximum = ocp->call_stack_maximum * 2 + 4;
nbytes = ocp->call_stack_maximum * sizeof(ocp->call_stack[0]);
ocp->call_stack = mem_change_size(ocp->call_stack, nbytes);
}
frame = &ocp->call_stack[ocp->call_stack_length++];
frame->olp = olp;
frame->pc = 0;
frame->stp = 0;
trace((/*{*/"}\n"));
}
/*
* NAME
* opcode_context_string_list_push
*
* SYNOPSIS
* void opcode_context_string_list_push(opcode_context_ty *);
*
* DESCRIPTION
* The opcode_context_string_list_push function is used to push a
* fresh string list onto the value stack. This is the normal
* mechanism for accumulating arguments. The call stack and value
* stack are separate.
*
* CAVEAT
* To be used only internally to the interpratation by individial
* opcodes.
*/
void
opcode_context_string_list_push(ocp)
opcode_context_ty *ocp;
{
trace(("opcode_context_string_list_push(ocp = %08lX)\n{\n"/*}*/,
(long)ocp));
assert(ocp);
if (ocp->value_stack_length >= ocp->value_stack_maximum)
{
size_t nbytes;
ocp->value_stack_maximum = ocp->value_stack_maximum * 2 + 4;
nbytes = ocp->value_stack_maximum * sizeof(ocp->value_stack[0]);
ocp->value_stack = mem_change_size(ocp->value_stack, nbytes);
}
ocp->value_stack[ocp->value_stack_length++] = string_list_new();
trace((/*{*/"}\n"));
}
/*
* NAME
* opcode_context_string_push
*
* SYNOPSIS
* void opcode_context_string_push(opcode_context_ty *, string_ty *);
*
* DESCRIPTION
* The opcode_context_string_push function is used to append a
* string to the string list on the top of the value stack. This
* is the normal mechanism for accumulating argument lists.
*
* CAVEAT
* To be used only internally to the interpratation by individial
* opcodes.
*/
void
opcode_context_string_push(ocp, s)
opcode_context_ty *ocp;
string_ty *s;
{
string_list_ty *slp;
trace(("opcode_context_string_push(ocp = %08lX)\n{\n"/*}*/, (long)ocp));
assert(ocp);
assert(ocp->value_stack_length > 0);
slp = ocp->value_stack[ocp->value_stack_length - 1];
string_list_append(slp, s);
trace((/*{*/"}\n"));
}
void
opcode_context_string_push_list(ocp, i)
opcode_context_ty *ocp;
const string_list_ty *i;
{
string_list_ty *slp;
trace(("opcode_context_string_push(ocp = %08lX)\n{\n"/*}*/, (long)ocp));
assert(ocp);
assert(ocp->value_stack_length > 0);
slp = ocp->value_stack[ocp->value_stack_length - 1];
string_list_append_list(slp, i);
trace((/*{*/"}\n"));
}
/*
* NAME
* opcode_context_string_list_pop
*
* SYNOPSIS
* string_list_ty *opcode_context_string_list_pop(opcode_context_ty *);
*
* DESCRIPTION
* The opcode_context_string_list_pop function is used to obtain
* the top-most string list from the value stack (it is removed
* from the stack). This is the normal mechanism for obtaining
* argument lists. Use string_list_delete when you are done with it.
*
* CAVEAT
* To be used only internally to the interpratation by individial
* opcodes.
*/
string_list_ty *
opcode_context_string_list_pop(ocp)
opcode_context_ty *ocp;
{
string_list_ty *slp;
trace(("opcode_context_string_list_pop(ocp = %08lX)\n{\n"/*}*/,
(long)ocp));
assert(ocp);
assert(ocp->value_stack_length > 0);
ocp->value_stack_length--;
slp = ocp->value_stack[ocp->value_stack_length];
trace(("return %08lX;\n", (long)slp));
trace((/*{*/"}\n"));
return slp;
}
string_list_ty *
opcode_context_string_list_peek(ocp)
const opcode_context_ty *ocp;
{
string_list_ty *slp;
trace(("opcode_context_string_list_peek(ocp = %08lX)\n{\n"/*}*/,
(long)ocp));
assert(ocp);
assert(ocp->value_stack_length > 0);
slp = ocp->value_stack[ocp->value_stack_length - 1];
trace(("return %08lX;\n", (long)slp));
trace((/*{*/"}\n"));
return slp;
}
/*
* NAME
* opcode_context_goto
*
* SYNOPSIS
* void opcode_context_goto(opcode_context_ty *, size_t);
*
* DESCRIPTION
* The opcode_context_goto function is used to move the execution
* location pointer. This uis the normal ``jump'' mechanism.
*
* CAVEAT
* To be used only internally to the interpratation by individial
* opcodes.
*/
void
opcode_context_goto(ocp, pc)
opcode_context_ty *ocp;
size_t pc;
{
opcode_frame_ty *frame;
trace(("opcode_context_goto(ocp = %08lX, pc = %ld)\n{\n"/*}*/,
(long)ocp, (long)pc));
assert(ocp);
assert(ocp->call_stack_length > 0);
frame = &ocp->call_stack[ocp->call_stack_length - 1];
assert(pc <= frame->olp->length);
frame->pc = pc;
trace((/*{*/"}\n"));
}
int
opcode_context_getpid(ocp)
opcode_context_ty *ocp;
{
return ocp->pid;
}
void
opcode_context_waited(ocp, es)
opcode_context_ty *ocp;
int es;
{
ocp->exit_status = es;
}
void
opcode_context_suspend(ocp)
opcode_context_ty *ocp;
{
/*
* save and clear flag state
*/
trace(("opcode_context_suspend(ocp = %08lX)\n{\n"/*}*/, (long)ocp));
ocp->flags = option_flag_state_get();
option_undo_level(OPTION_LEVEL_EXECUTE);
option_undo_level(OPTION_LEVEL_RECIPE);
trace((/*{*/"}\n"));
}
void
opcode_context_resume(ocp)
opcode_context_ty *ocp;
{
/*
* restore flag state
*/
trace(("opcode_context_resume(ocp = %08lX)\n{\n"/*}*/, (long)ocp));
assert(ocp->flags);
if (ocp->flags)
option_flag_state_set(ocp->flags);
ocp->flags = 0;
trace((/*{*/"}\n"));
}
void
opcode_context_host_binding_set(ocp, host_binding)
opcode_context_ty *ocp;
string_ty *host_binding;
{
assert(ocp);
assert(host_binding);
assert(!ocp->host_binding);
ocp->host_binding = str_copy(host_binding);
}
const match_ty *
opcode_context_match_top(ocp)
const opcode_context_ty *ocp;
{
if (!ocp->msp)
return 0;
return match_stack_top(ocp->msp);
}
const match_ty *
opcode_context_match_pop(ocp)
opcode_context_ty *ocp;
{
assert(ocp->msp);
return match_stack_pop(ocp->msp);
}
void
opcode_context_match_push(ocp, mp)
opcode_context_ty *ocp;
const match_ty *mp;
{
if (!ocp->msp)
ocp->msp = match_stack_new();
match_stack_push(ocp->msp, mp);
}
void
opcode_context_id_assign(ocp, name, value, local)
opcode_context_ty *ocp;
string_ty *name;
id_ty *value;
int local;
{
opcode_frame_ty *frame;
assert(ocp);
if (ocp->call_stack_length < 1)
frame = 0;
else
frame = &ocp->call_stack[ocp->call_stack_length - 1];
if (local > 0 && frame)
{
if (!frame->stp)
{
frame->stp = symtab_alloc(5);
frame->stp->reap = id_global_reap;
}
symtab_assign(frame->stp, name, value);
}
else if (local < 0)
{
if (!ocp->thread_stp)
{
ocp->thread_stp = symtab_alloc(5);
ocp->thread_stp->reap = id_global_reap;
}
symtab_assign(ocp->thread_stp, name, value);
}
else if (frame && frame->stp && symtab_query(frame->stp, name))
symtab_assign(frame->stp, name, value);
else if (ocp->thread_stp && symtab_query(ocp->thread_stp, name))
symtab_assign(ocp->thread_stp, name, value);
else
symtab_assign(id_global_stp(), name, value);
}
id_ty *
opcode_context_id_search(ocp, name)
const opcode_context_ty *ocp;
string_ty *name;
{
id_ty *result;
assert(ocp);
if (ocp->call_stack_length >= 1)
{
opcode_frame_ty *frame;
frame = &ocp->call_stack[ocp->call_stack_length - 1];
if (frame->stp)
{
result = symtab_query(frame->stp, name);
if (result)
return result;
}
}
if (ocp->thread_stp)
{
result = symtab_query(ocp->thread_stp, name);
if (result)
return result;
}
return symtab_query(id_global_stp(), name);
}
id_ty *
opcode_context_id_search_fuzzy(ocp, name, guess)
const opcode_context_ty *ocp;
string_ty *name;
string_ty **guess;
{
symtab_ty *argv[3];
size_t argc;
*guess = 0;
assert(ocp);
assert(ocp->call_stack_length > 0);
argc = 0;
if (ocp->call_stack_length >= 1)
{
opcode_frame_ty *frame;
frame = &ocp->call_stack[ocp->call_stack_length - 1];
if (frame->stp)
argv[argc++] = frame->stp;
}
if (ocp->thread_stp)
argv[argc++] = ocp->thread_stp;
argv[argc++] = id_global_stp();
return symtab_query_fuzzyN(argv, argc, name, guess);
}
/*
* NAME
* opcode_context_run
*
* SYNOPSIS
* string_list_ty *opcode_context_run(opcode_list_ty *);
*
* DESCRIPTION
* The opcode_context_run function is used to execute an opcode list
* with the expectation of extracting a string list result. Used
* to evaluate the need1 and need2 ingredients.
*
* RETURNS
* string_list_ty *; or NULL pointer on error
*
* CAVEAT
* Use string_list_delete when you are done with it.
*/
string_list_ty *
opcode_context_run(ocp, olp)
opcode_context_ty *ocp;
opcode_list_ty *olp;
{
opcode_status_ty status;
assert(ocp->call_stack_length == 0);
if (!olp)
return string_list_new();
opcode_context_call(ocp, olp);
status = opcode_context_execute(ocp);
if (status != opcode_status_success)
return 0;
return opcode_context_string_list_pop(ocp);
}
/*
* NAME
* opcode_context_run_bool
*
* SYNOPSIS
* string_list_ty *opcode_context_run_bool(opcode_list_ty *);
*
* DESCRIPTION
* The opcode_context_run_bool function is used to execute an opcode list
* with the expectation of extracting a boolean result. Used
* to evaluate recipe predicates.
*
* RETURNS
* int; 0 if false, 1 if true, -1 on error
*/
int
opcode_context_run_bool(ocp, olp)
opcode_context_ty *ocp;
opcode_list_ty *olp;
{
string_list_ty *slp;
int result;
if (!olp)
return 1;
slp = opcode_context_run(ocp, olp);
if (!slp)
return -1;
result = string_list_bool(slp);
string_list_delete(slp);
return result;
}
|