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 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
|
/*
* cook - file construction tool
* Copyright (C) 1997-1999, 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 command opcodes
*
* See also: cook/os.c::execute()
*/
#include <ac/errno.h>
#include <ac/fcntl.h>
#include <ac/stdio.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/time.h>
#include <ac/unistd.h>
#include <sys/types.h>
#include <sys/stat.h> /* for chmod */
#ifdef HAVE_WAIT3
#include <sys/resource.h>
#endif
#include <error.h>
#include <error_intl.h>
#include <expr/position.h>
#include <flag.h>
#include <id.h>
#include <id/variable.h>
#include <mem.h>
#include <opcode/context.h>
#include <opcode/command.h>
#include <opcode/private.h>
#include <option.h>
#include <os_interface.h>
#include <star.h>
#include <str_list.h>
#include <tempfilename.h>
#include <trace.h>
typedef struct opcode_command_ty opcode_command_ty;
struct opcode_command_ty
{
opcode_ty inherited;
int input;
expr_position_ty pos;
};
static void destructor _((opcode_ty *));
static void
destructor(that)
opcode_ty *that;
{
opcode_command_ty *this;
this = (opcode_command_ty *)that;
expr_position_destructor(&this->pos);
}
/*
* NAME
* echo_command
*
* SYNOPSIS
* void echo_command(void);
*
* DESCRIPTION
* The echo_command function is used to determine whether the
* command will probably produce output. Used to see of a
* star_eoln needs to be issued.
*
* RETURNS
* int; 1 if prints, 0 if not
*/
static int echo_command _((string_list_ty *));
static int
echo_command(wlp)
string_list_ty *wlp;
{
static string_ty *echo;
string_ty *tmp;
char *cp;
if (!echo)
echo = str_from_c("echo");
if (wlp->nstrings < 1 || !str_equal(wlp->string[0], echo))
return 0;
tmp = wl2str(wlp, 0, wlp->nstrings - 1, (char *)0);
for (cp = tmp->str_text; *cp; ++cp)
{
if (strchr("^|>", *cp))
{
str_free(tmp);
return 0;
}
}
str_free(tmp);
return 1;
}
static string_list_ty *id_var_search _((opcode_context_ty *, string_ty *));
static string_list_ty *
id_var_search(ocp, key)
opcode_context_ty *ocp;
string_ty *key;
{
id_ty *idp;
string_list_ty *result;
idp = opcode_context_id_search(ocp, key);
if (!idp)
return 0;
result = id_variable_query2(idp);
if (!result || !result->nstrings)
return 0;
return result;
}
static void chmod_and_check _((const char *, int));
static void
chmod_and_check(fn, mode)
const char *fn;
int mode;
{
if (chmod(fn, mode))
{
sub_context_ty *scp;
scp = sub_context_new();
sub_errno_set(scp);
sub_var_set(scp, "File_Name", "%s", fn);
sub_var_set(scp, "Number", "0%o", mode);
fatal_intl(scp, i18n("chmod $filename $mode: $errno"));
/* NOTREACHED */
}
}
/*
* NAME
* spawn - execute a command
*
* SYNOPSIS
* opcode_status_ty spawn(string_list_ty *args, string_ty *input);
*
* DESCRIPTION
* The spawn function is used to launch the given command.
* The command is not waited for.
*
* RETURNS
* opcode_status_ty...
* opcode_status_error if something went wrong
* opcode_status_wait if something all went well
*/
static opcode_status_ty spawn _((string_list_ty *, string_ty *, int *,
string_ty *, opcode_context_ty *));
static opcode_status_ty
spawn(args, input, pid_p, host_binding, ocp)
string_list_ty *args;
string_ty *input;
int *pid_p;
string_ty *host_binding;
opcode_context_ty *ocp;
{
int j;
int fd;
string_ty *iname;
int pid;
opcode_status_ty status;
static char **argv;
static size_t argvlen;
string_list_ty cmd;
sub_context_ty *scp;
char *shell;
trace(("execute()\n{\n"/*}*/));
assert(args);
/*
* build the input file, if required
*/
status = opcode_status_error;
fd = -1;
iname = 0;
if (input)
{
/*
* He has given a string to be used as input to the command,
* so write it out to a file, and then redirect the input.
*/
iname = temporary_filename();
fd = open(iname->str_text, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
fatal_intl_open(iname->str_text);
if (unlink(iname->str_text))
{
error_intl_unlink(iname->str_text);
goto done;
}
if
(
write(fd, input->str_text, input->str_length) < 0
||
lseek(fd, 0L, SEEK_SET)
)
{
error_intl_write(iname->str_text);
goto done;
}
}
/*
* See if there is a host_binding in effect.
*/
if (host_binding)
{
static string_ty *key;
string_list_ty *slp;
string_ty *s;
string_ty *rcmd;
string_ty *rcmdq;
string_ty *result_fn;
FILE *result_fp;
string_ty *cwd;
string_ty *script_fn;
FILE *script_fp;
/*
* Work out the name of the remote shell command.
*/
if (!key)
key = str_from_c("parallel_rsh");
slp = id_var_search(ocp, key);
if (slp)
string_list_copy_constructor(&cmd, slp);
else
{
static string_ty *rsh;
if (!rsh)
rsh = str_from_c(CONF_REMOTE_SHELL);
string_list_constructor(&cmd);
string_list_append(&cmd, rsh);
}
/*
* Add the name of the host.
*/
string_list_append(&cmd, host_binding);
/*
* The remote end will need to change directory to where
* we are now, since it is *highly* unlikely that we are
* in our home directory.
*/
cwd = os_curdir(); /* do not str_free this */
/*
* We are going to create a small shell script contining
* the command to be executed. This deals with various
* weird and obscure $SHELL settings, and it also deals
* with command lines so long that rsh barfs.
*
* Yes, there is a potential race condition here.
* Between writing the script and executing it, we
* could be tricked into executing something else.
* Especially if there is a symlink lying in wait.
*/
script_fn = dot_temporary_filename();
script_fp = fopen_and_check(script_fn->str_text, "w");
/*
* what shell are we using
*/
shell = getenv("SHELL");
if (!shell || !*shell)
shell = CONF_SHELL;
fprintf(script_fp, "#!%s\n", shell);
/*
* Now write the actual command to the script file.
*/
rcmd = wl2str(args, 0, args->nstrings, (char *)0);
fprintf(script_fp, "%s\n", rcmd->str_text);
str_free(rcmd);
fflush_and_check(script_fp, script_fn->str_text);
fclose_and_check(script_fp, script_fn->str_text);
chmod_and_check(script_fn->str_text, 0755);
/*
* This is the result file. It is where the remote
* command will stash its exit status. We seed it with
* failure in case the rsh fails altogether.
*
* And, yes, there is a race conditon and we could be
* lied to if they are quick.
*/
result_fn = dot_temporary_filename();
result_fp = fopen_and_check(result_fn->str_text, "w");
fprintf(result_fp, "42\n");
fflush_and_check(result_fp, result_fn->str_text);
fclose_and_check(result_fp, result_fn->str_text);
/*
* Because rsh(1) always returns an exit status of zero,
* we have to make other arrangements to obtain the exit
* status.
*
* We actually need to be a more devious, so that we can
* get the exit status back. Write it to a temporary
* file server side, then read and remove it client side.
*
* This is the server side command. It gets quoted
* again to protect it from the client side shell.
*
* We use sh explicitly, because the shell at the other
* end could be csh, tcsh, or something even stranger,
* and we need to guarantee the command will execute
* exactly the way we need.
*/
rcmd =
str_format
(
"sh -c 'cd %S && sh %s %S/%S; echo $? > %S/%S'",
cwd,
(option_test(OPTION_ERROK) ? "-c" : "-ce"),
cwd,
script_fn,
cwd,
result_fn
);
rcmdq = str_quote_shell(rcmd);
str_free(rcmd);
string_list_append(&cmd, rcmdq);
str_free(rcmdq);
/*
* This is the rest of the server side command.
*
* On some systems this gives ``Text file busy'' errors,
* as if the script file has yet to be released by
* the kernel. Since we *aren't* executing it when the
* RM command runs, I have no idea how to avoid this
* (other than ignoring it with -f). Maybe this is some
* kind of NFS latency?
*/
s =
str_format
(
"&& exit `cat %S;rm -f %S %S`",
result_fn,
result_fn,
script_fn
);
string_list_append(&cmd, s);
str_free(s);
str_free(result_fn);
str_free(script_fn);
/*
* Because the command has a semicolon and back quotes,
* we need to hand it to a shell (as alluded to above).
* Assemble into a single string.
*/
rcmd = wl2str(&cmd, 0, cmd.nstrings, (char *)0);
string_list_destructor(&cmd);
/*
* Build the final command to be executed.
* It even cleans up after itself.
*/
s = str_from_c("sh");
string_list_append(&cmd, s);
str_free(s);
s = str_from_c("-c");
string_list_append(&cmd, s);
str_free(s);
string_list_append(&cmd, rcmd);
str_free(rcmd);
}
else
{
int magic_characters;
/*
* see if there are any magic shell cheracters
*/
magic_characters = 0;
for (j = 0; j < args->nstrings; ++j)
{
char *s;
for (s = args->string[j]->str_text; *s; ++s)
{
if (strchr("#|=^();&<>*?[]:$`'\"\\\n", *s))
{
magic_characters = 1;
break;
}
}
}
/*
* build the command
*/
if (magic_characters)
{
string_ty *str;
/*
* what shell are we using
*/
shell = getenv("SHELL");
if (!shell || !*shell)
shell = CONF_SHELL;
string_list_constructor(&cmd);
str = str_from_c(shell);
string_list_append(&cmd, str);
str_free(str);
if (option_test(OPTION_ERROK))
str = str_from_c("-c");
else
str = str_from_c("-ce");
string_list_append(&cmd, str);
str_free(str);
str = wl2str(args, 0, args->nstrings - 1, (char *)0);
string_list_append(&cmd, str);
str_free(str);
}
else
string_list_copy_constructor(&cmd, args);
}
/*
* build the argv array
*/
if (!argv)
{
argvlen = cmd.nstrings + 1;
argv = mem_alloc(argvlen * sizeof(char *));
}
else if (argvlen < cmd.nstrings + 1)
{
argvlen = cmd.nstrings + 1;
argv = mem_change_size(argv, argvlen * sizeof(char *));
}
if (cmd.nstrings == 0)
{
string_list_destructor(&cmd);
status = opcode_status_success;
goto done;
}
for (j = 0; j < cmd.nstrings; ++j)
argv[j] = cmd.string[j]->str_text;
argv[cmd.nstrings] = 0;
/*
* spawn the child process
*/
switch (pid = fork())
{
case -1:
scp = sub_context_new();
sub_errno_set(scp);
error_intl(scp, i18n("fork(): $errno"));
sub_context_delete(scp);
break;
case 0:
/*
* child
*/
if (fd >= 0)
{
if (close(0) && errno != EBADF)
{
string_ty *fn0;
int err;
err = errno;
scp = sub_context_new();
fn0 = subst_intl(scp, "standard input");
/* re-use substitution context */
sub_errno_setx(scp, err);
sub_var_set(scp, "File_Name", "%S", fn0);
fatal_intl
(
scp,
i18n("close $filename: $errno")
);
/* NOTREACHED */
sub_context_delete(scp);
str_free(fn0);
}
if (dup(fd) < 0)
{
scp = sub_context_new();
sub_errno_set(scp);
fatal_intl(scp, i18n("dup(): $errno"));
/* NOTREACHED */
sub_context_delete(scp);
}
close(fd);
}
if (argv[0][0] == '/')
execv(argv[0], argv);
else
execvp(argv[0], argv);
scp = sub_context_new();
sub_errno_set(scp);
sub_var_set(scp, "File_Name", "%s", argv[0]);
fatal_intl(scp, i18n("exec $filename: $errno"));
/* NOTREACHED */
sub_context_delete(scp);
default:
/*
* parent
*/
if (fd >= 0)
{
close(fd);
fd = -1;
}
string_list_destructor(&cmd);
status = opcode_status_wait;
trace(("pid = %d;\n", pid));
*pid_p = pid;
break;
}
done:
if (fd >= 0)
close(fd);
if (iname)
str_free(iname);
trace(("return %s;\n", opcode_status_name(status)));
trace((/*{*/"}\n"));
return status;
}
/*
* NAME
* meter_ptime - print timing info
*
* SYNOPSIS
* void meter_ptime(double t, char *s);
*
* DESCRIPTION
* The meter_ptime function is used to print e representation of the
* time, in seconds, given in the `t' argument. The `s' argument is a
* title string.
*/
#ifdef HAVE_WAIT3
static void meter_ptime _((double, char *));
static void
meter_ptime(t, s)
double t;
char *s;
{
long hour;
long min;
long sec;
long frac;
frac = t * 1000 + 0.5;
sec = frac / 1000;
frac %= 1000;
min = sec / 60;
sec %= 60;
hour = min / 60;
min %= 60;
fprintf(stderr, "%2ld:%02ld:%02ld.%03ld %s\n", hour, min, sec, frac, s);
}
/*
* NAME
* meter_print - end a metering interval
*
* SYNOPSIS
* void meter_print(struct rusage *);
*
* DESCRIPTION
* The meter_end function is used to end a metering interval and print
* the metered information.
*/
static void meter_print _((struct rusage *));
static void
meter_print(rup)
struct rusage *rup;
{
double usr;
double sys;
usr = rup->ru_utime.tv_sec + rup->ru_utime.tv_usec * 1.0e-6;
meter_ptime(usr, "usr");
sys = rup->ru_stime.tv_sec + rup->ru_stime.tv_usec * 1.0e-6;
meter_ptime(sys, "sys");
}
#endif /* HAVE_WAIT3 */
/*
* NAME
* execute
*
* SYNOPSIS
* opcode_status_ty execute(opcode_ty *, opcode_context_ty *);
*
* DESCRIPTION
* The execute function is used to execute the given opcode within
* the given interpretation context.
*
* RETURNS
* opcode_status_ty to indicate the result of the execution
*/
static opcode_status_ty execute _((const opcode_ty *, opcode_context_ty *));
static opcode_status_ty
execute(op, ocp)
const opcode_ty *op;
opcode_context_ty *ocp;
{
const opcode_command_ty *this;
opcode_status_ty status;
string_list_ty *wlp;
string_list_ty *flags_words;
string_ty *isp;
char *cmd;
flag_ty *flags;
/*
* pop the arguments off the value stack
*/
trace(("opcode_command::execute()\n{\n"/*}*/));
this = (const opcode_command_ty *)op;
status = opcode_status_success;
isp = 0;
flags_words = 0;
flags = 0;
wlp = 0;
if (ocp->pid)
{
ocp->pid = 0;
wlp = ocp->wlp;
ocp->wlp = 0;
goto resume;
}
if (this->input)
{
string_list_ty *islp;
islp = opcode_context_string_list_pop(ocp);
isp = wl2str(islp, 0, islp->nstrings - 1, (char *)0);
string_list_delete(islp);
}
flags_words = opcode_context_string_list_pop(ocp);
wlp = opcode_context_string_list_pop(ocp);
/*
* take care of the options
*/
flags = flag_recognize(flags_words, &this->pos);
if (!flags)
{
status = opcode_status_error;
goto done;
}
string_list_delete(flags_words);
flags_words = 0;
flag_set_options(flags, OPTION_LEVEL_EXECUTE);
flag_delete(flags);
flags = 0;
/*
* echo the command if required
*/
if (!option_test(OPTION_SILENT))
{
string_ty *cp;
/*
* If the command has not been silenced,
* form it into a string and echo it.
*/
cp = wl2str(wlp, 0, wlp->nstrings - 1, (char *)0);
error_raw("%s", cp->str_text);
str_free(cp);
}
/*
* execute the command if required
*/
if (option_test(OPTION_ACTION))
{
/*
* emit suitable progress stars
*/
if (option_test(OPTION_SILENT))
{
if (echo_command(wlp))
star_eoln();
else
star_bang();
}
else
star_sync();
/*
* invalidate the stat cache
*/
if (option_test(OPTION_INVALIDATE_STAT_CACHE))
{
size_t j;
for (j = 0; j < wlp->nstrings; ++j)
if (os_clear_stat(wlp->string[j]))
status = opcode_status_error;
}
/*
* prepare for metering
*/
#ifdef HAVE_WAIT3
if (option_test(OPTION_METER) && !ocp->rup)
ocp->rup = mem_alloc(sizeof(struct rusage));
#endif
/*
* run the command
*/
status = spawn(wlp, isp, &ocp->pid, ocp->host_binding, ocp);
if (status == opcode_status_wait)
{
trace(("...wait\n"));
ocp->wlp = wlp;
wlp = 0;
goto done;
}
if (status == opcode_status_error)
goto done;
resume:
#ifdef HAVE_WAIT3
if (option_test(OPTION_METER))
{
assert(ocp->rup);
meter_print(ocp->rup);
}
#endif
/*
* Echo the exit status of the command, if it was not
* successful. The error flag will also be examined (it
* changes the text of the message, too) to see if we
* should terminate successfully or with and error.
*/
cmd = (wlp->nstrings ? wlp->string[0]->str_text : "");
if
(
exit_status
(
cmd,
ocp->exit_status,
option_test(OPTION_ERROK)
)
)
{
status = opcode_status_error;
}
}
/*
* rescind the flag settings
*/
option_undo_level(OPTION_LEVEL_EXECUTE);
/*
* release the arguments and exit
*/
done:
if (flags_words)
string_list_delete(flags_words);
if (flags)
flag_delete(flags);
if (wlp)
string_list_delete(wlp);
if (isp)
str_free(isp);
trace(("return %s;\n", opcode_status_name(status)));
trace((/*{*/"}\n"));
return status;
}
/*
* NAME
* script
*
* SYNOPSIS
* opcode_status_ty script(opcode_ty *, opcode_context_ty *);
*
* DESCRIPTION
* The script function is used to script the given opcode within
* the given interpretation context.
*
* RETURNS
* opcode_status_ty to indicate the result
*/
static opcode_status_ty script _((const opcode_ty *, opcode_context_ty *));
static opcode_status_ty
script(op, ocp)
const opcode_ty *op;
opcode_context_ty *ocp;
{
const opcode_command_ty *this;
opcode_status_ty status;
string_list_ty *wlp;
string_ty *isp;
string_list_ty *flags_words;
flag_ty *flags;
/*
* pop the arguments off the value stack
*/
trace(("opcode_command::script()\n{\n"/*}*/));
this = (const opcode_command_ty *)op;
status = opcode_status_success;
if (this->input)
{
string_list_ty *islp;
islp = opcode_context_string_list_pop(ocp);
isp = wl2str(islp, 0, islp->nstrings - 1, (char *)0);
string_list_delete(islp);
}
else
isp = 0;
flags_words = opcode_context_string_list_pop(ocp);
wlp = opcode_context_string_list_pop(ocp);
/*
* set the flags
*/
flags = flag_recognize(flags_words, &this->pos);
string_list_delete(flags_words);
if (!flags)
{
status = opcode_status_error;
goto done;
}
flag_set_options(flags, OPTION_LEVEL_EXECUTE);
flag_delete(flags);
/*
* If the command is empty (e.g. [print ...]; or [write ...];)
* do nothing at all.
*/
if (wlp->nstrings == 0)
goto done;
/*
* echo the command if required
*/
if (!option_test(OPTION_SILENT))
{
string_ty *s1;
string_ty *s2;
s1 = wl2str(wlp, 0, wlp->nstrings, " ");
s2 = str_quote_shell(s1);
str_free(s1);
printf("echo %s\n", s2->str_text);
str_free(s2);
}
/*
* script the command if required
*/
if (option_test(OPTION_ACTION))
{
size_t j;
printf("(");
for (j = 0; j < wlp->nstrings; ++j)
printf(" %s", wlp->string[j]->str_text);
printf(" )");
if (isp)
{
char fubar[50];
time_t t;
int nl;
time(&t);
if (t < 0)
t = 0;
sprintf
(
fubar,
"fubar%ldfubar%ldfubar",
(long)t,
(long)t
);
nl =
(
isp->str_length
&&
isp->str_text[isp->str_length - 1] != '\n'
);
printf
(
" << '%s'\n%s%s%s",
fubar,
isp->str_text,
(nl ? "\n" : ""),
fubar
);
}
printf("\n");
if (!option_test(OPTION_ERROK))
printf("test $? -eq 0 || exit 1\n");
}
/*
* rescind the flag settings
*/
option_undo_level(OPTION_LEVEL_EXECUTE);
/*
* release the arguments and exit
*/
done:
string_list_delete(wlp);
if (isp)
str_free(isp);
trace(("return %s;\n", opcode_status_name(status)));
trace((/*{*/"}\n"));
return status;
}
/*
* NAME
* disassemble
*
* SYNOPSIS
* void disassemble(opcode_ty *);
*
* DESCRIPTION
* The disassemble function is used to disassemble the copdode and
* its arguments onto the standard output. Don't worry about the
* location or a trailing newline.
*/
static void disassemble _((const opcode_ty *));
static void
disassemble(op)
const opcode_ty *op;
{
const opcode_command_ty *this;
trace(("opcode_command::disassemble()\n{\n"/*}*/));
this = (const opcode_command_ty *)op;
printf
(
"%d\t# %s:%d",
this->input,
this->pos.pos_name->str_text,
this->pos.pos_line
);
trace((/*{*/"}\n"));
}
/*
* NAME
* method
*
* DESCRIPTION
* The method variable describes this class.
*
* CAVEAT
* This symbol is not exported from this file.
*/
static opcode_method_ty method =
{
"command",
sizeof(opcode_command_ty),
destructor,
execute,
script,
disassemble,
};
/*
* NAME
* opcode_command_new
*
* SYNOPSIS
* opcode_ty *opcode_command_new(void);
*
* DESCRIPTION
* The opcode_command_new function is used to allocate a new instance
* of a command opcode.
*
* RETURNS
* opcode_ty *; use opcode_delete when you are finished with it.
*/
opcode_ty *
opcode_command_new(input, pp)
int input;
const expr_position_ty *pp;
{
opcode_ty *op;
opcode_command_ty *this;
trace(("opcode_command_new()\n{\n"/*}*/));
op = opcode_new(&method);
this = (opcode_command_ty *)op;
this->input = input;
expr_position_copy_constructor(&this->pos, pp);
trace(("return %08lX;\n", (long)op));
trace((/*{*/"}\n"));
return op;
}
|