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
|
/* Yash: yet another shell */
/* option.c: option settings */
/* (C) 2007-2025 magicant */
/* 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, see <http://www.gnu.org/licenses/>. */
#include "common.h"
#include "option.h"
#include <assert.h>
#if HAVE_GETTEXT
# include <libintl.h>
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
#include "builtin.h"
#include "exec.h"
#include "job.h"
#include "plist.h"
#include "redir.h"
#include "sig.h"
#include "strbuf.h"
#include "util.h"
#include "variable.h"
/********** Option Definitions **********/
/* The value of special parameter $0. */
const wchar_t *command_name;
/* If set, the shell behaves strictly as defined in POSIX.
* Corresponds to the --posixly-correct option. */
bool posixly_correct = false;
/* If set, this shell is a login shell.
* Corresponds to the --login option. */
bool is_login_shell = false;
/* If set, this shell is interactive.
* Corresponds to the -i/--interactive option. */
/* This value does not change when entering a subshell, but subshells do not run
* interactively. See the `is_interactive_now' macro. */
bool is_interactive = false;
/* `shopt_cmdline' is set if the shell is executing a command in the command-
* line argument to the shell. `shopt_stdin' is set if the shell is reading
* commands to execute from the standard input.
* Correspond to the -c and -s options respectively. */
bool shopt_cmdline = false, shopt_stdin = false;
/* If set, the shell performs job control.
* Corresponds to the -m/--monitor option. */
/* This value does not change when entering a subshell, but subshells do not
* perform job control. See the `doing_job_control_now' macro. */
bool do_job_control = false;
/* If set, the shell immediately notifies on change of job status.
* Corresponds to the -b/--notify option. */
bool shopt_notify = false;
#if YASH_ENABLE_LINEEDIT
/* If set, the shell immediately notifies on change of job status during line-
* editing. Ignored if `shopt_notify' is set. Corresponds to the --notifyle
* option. */
bool shopt_notifyle = false;
#endif
/* If set, a background job is set to the current job when:
* - invoked as an asynchronous command
* - resumed in the background by the "bg" command
* - stopped
* respectively.
* Correspond to the --curasync, --curbg, --curstop options. */
bool shopt_curasync = true, shopt_curbg = true, shopt_curstop = true;
/* If set, any variable is exported when assigned.
* Corresponds to the -a/--allexport option. */
bool shopt_allexport = false;
/* If set, when a function is defined, all the commands in the function
* are hashed. Corresponds to the -h/--hashondef option. */
bool shopt_hashondef = false;
/* If set, the 'for' loop iteration variable will be made local. */
bool shopt_forlocal = true;
/* If set, when a command returns a non-zero status, the shell exits.
* Corresponds to the -e/--errexit option. */
bool shopt_errexit = false;
/* Like `shopt_errexit', but instead of exiting the shell, the "return"
* built-in is executed on a non-zero status. */
bool shopt_errreturn = false;
/* If set, the last non-zero exit status in a pipeline becomes the exit status
* of the whole pipeline. Otherwise, the last command in the pipeline always
* defines the exit status of the whole pipeline. Corresponds to the --pipefail
* option. */
bool shopt_pipefail = false;
/* If set, undefined variables are expanded to an empty string.
* Corresponds to the +u/--unset option. */
bool shopt_unset = true;
/* If set, commands are actually executed.
* Corresponds to the +n/--exec option. */
bool shopt_exec = true;
/* If set, the shell doesn't exit when EOF is entered (from a terminal).
* Corresponds to the --ignoreeof option. */
bool shopt_ignoreeof = false;
/* If set, the shell echos the input to the shell.
* Corresponds to the -v/--verbose option. */
bool shopt_verbose = false;
/* If set, the shell prints the trace of command execution and assignment.
* Corresponds to the -x/--xtrace option. */
bool shopt_xtrace = false;
/* If set, the "xtrace" option is not ignored while executing auxiliary
* commands. */
bool shopt_traceall = true;
#if YASH_ENABLE_HISTORY
/* If set, lines that start with a space are not saved in the history.
* Corresponds to the --histspace option. */
bool shopt_histspace = false;
#endif
/* Function definition commands are saved in the history only when this option
* is set.
* Corresponds to the --log option.
* THIS OPTION IS NOT SUPPORTED. */
static bool shopt_log = true;
/* If set, the shell performs filename expansion.
* Corresponds to the +f/--glob option. */
bool shopt_glob = true;
/* If set, filename expansion is case-sensitive.
* Corresponds to the --caseglob option. */
bool shopt_caseglob = true;
/* If set, in filename expansion, a wildcard character matches a dot at the
* beginning of a filename.
* Corresponds to the --dotglob option. */
bool shopt_dotglob = false;
/* If set, in filename expansion, a directory name is expanded with a slash
* appended.
* Corresponds to the --markdirs option. */
bool shopt_markdirs = false;
/* If set, extended features in filename expansion are enabled.
* Corresponds to the --extendedglob option. */
bool shopt_extendedglob = false;
/* If set, a glob pattern is removed from the command line rather than left
* intact when there are no matches for it.
* Corresponds to the --nullglob option. */
bool shopt_nullglob = false;
/* If set, brace expansion is enabled.
* Corresponds to the --braceexpand option. */
bool shopt_braceexpand = false;
/* If set, empty last field is preserved in field splitting.
* Corresponds to the --emptylastfield option. */
bool shopt_emptylastfield = false;
/* If set, it is allowed to overwrite existing files by redirections.
* Corresponds to the +C/--clobber option. */
bool shopt_clobber = true;
#if YASH_ENABLE_LINEEDIT
/* When line-editing is disabled, `shopt_lineedit' is SHOPT_NOLINEEDIT.
* When line-editing is enabled, `shopt_lineedit' is set to another value that
* specifies the type of key bindings.
* `shopt_vi' and `shopt_emacs' correspond to the --vi and --emacs options,
* respectively. `shopt_lineedit' is set according to the values of them. */
enum shopt_lineedit_T shopt_lineedit = SHOPT_NOLINEEDIT;
static bool shopt_vi = false, shopt_emacs = false;
/* `shopt_le_convmeta' defines treatment of the 8th bit of input characters in
* line-editing. The value of `shopt_le_convmeta' is set according to those of
* `shopt_le_yesconvmeta' and `shopt_le_noconvmeta', which correspond to the
* --le-convmeta and --le-noconvmeta options, respectively. */
enum shopt_yesnoauto_T shopt_le_convmeta = SHOPT_AUTO;
static bool shopt_le_yesconvmeta = false, shopt_le_noconvmeta = false;
/* If set, line-editing uses a flash (instead of a bell) to alert the user. */
bool shopt_le_visiblebell = false;
/* If set, a special character sequence is printed when starting line-editing
* to make sure the prompt starts at the beginning of line. */
bool shopt_le_promptsp = true;
/* If set, the right prompt is always visible on the screen. */
bool shopt_le_alwaysrp = false;
/* If set, auto-suggest the most probable command line as the user enters a
* command line. */
bool shopt_le_predict = false;
/* If set, auto-suggest also provides suggestions for empty input lines. */
bool shopt_le_predictempty = false;
/* If set, debugging information is printed during command completion. */
bool shopt_le_compdebug = false;
/* If set, the right prompt will trim the extra space left at end for cursor
* to sit if prompt is too large */
bool shopt_le_trimright = false;
#endif
struct option_T {
wchar_t shortopt_enable, shortopt_disable;
const wchar_t *longopt;
bool *optp;
bool resettable;
};
/* List of the shell options. */
static const struct option_T shell_options[] = {
{ L'a', 0, L"allexport", &shopt_allexport, true, },
{ 0, 0, L"braceexpand", &shopt_braceexpand, true, },
{ 0, 0, L"caseglob", &shopt_caseglob, true, },
{ 0, L'C', L"clobber", &shopt_clobber, true, },
{ L'c', 0, L"cmdline", &shopt_cmdline, false, },
{ 0, 0, L"curasync", &shopt_curasync, true, },
{ 0, 0, L"curbg", &shopt_curbg, true, },
{ 0, 0, L"curstop", &shopt_curstop, true, },
{ 0, 0, L"dotglob", &shopt_dotglob, true, },
#if YASH_ENABLE_LINEEDIT
{ 0, 0, L"emacs", &shopt_emacs, true, },
#endif
{ 0, 0, L"emptylastfield", &shopt_emptylastfield, true, },
{ L'e', 0, L"errexit", &shopt_errexit, true, },
{ 0, 0, L"errreturn", &shopt_errreturn, true, },
{ 0, L'n', L"exec", &shopt_exec, true, },
{ 0, 0, L"extendedglob", &shopt_extendedglob, true, },
{ 0, 0, L"forlocal", &shopt_forlocal, true, },
{ 0, L'f', L"glob", &shopt_glob, true, },
{ L'h', 0, L"hashondef", &shopt_hashondef, true, },
#if YASH_ENABLE_HISTORY
{ 0, 0, L"histspace", &shopt_histspace, true, },
#endif
{ 0, 0, L"ignoreeof", &shopt_ignoreeof, true, },
{ L'i', 0, L"interactive", &is_interactive, false, },
#if YASH_ENABLE_LINEEDIT
{ 0, 0, L"lealwaysrp", &shopt_le_alwaysrp, true, },
{ 0, 0, L"lecompdebug", &shopt_le_compdebug, true, },
{ 0, 0, L"leconvmeta", &shopt_le_yesconvmeta, true, },
{ 0, 0, L"lenoconvmeta", &shopt_le_noconvmeta, true, },
{ 0, 0, L"lepredict", &shopt_le_predict, true, },
{ 0, 0, L"lepredictempty", &shopt_le_predictempty,true, },
{ 0, 0, L"lepromptsp", &shopt_le_promptsp, true, },
{ 0, 0, L"letrimright", &shopt_le_trimright, true, },
{ 0, 0, L"levisiblebell", &shopt_le_visiblebell, true, },
#endif
{ 0, 0, L"log", &shopt_log, true, },
{ L'l', 0, L"login", &is_login_shell, false, },
{ 0, 0, L"markdirs", &shopt_markdirs, true, },
{ L'm', 0, L"monitor", &do_job_control, true, },
{ L'b', 0, L"notify", &shopt_notify, true, },
#if YASH_ENABLE_LINEEDIT
{ 0, 0, L"notifyle", &shopt_notifyle, true, },
#endif
{ 0, 0, L"nullglob", &shopt_nullglob, true, },
{ 0, 0, L"pipefail", &shopt_pipefail, true, },
{ 0, 0, L"posixlycorrect", &posixly_correct, true, },
{ L's', 0, L"stdin", &shopt_stdin, false, },
{ 0, 0, L"traceall", &shopt_traceall, true, },
{ 0, L'u', L"unset", &shopt_unset, true, },
{ L'v', 0, L"verbose", &shopt_verbose, true, },
#if YASH_ENABLE_LINEEDIT
{ 0, 0, L"vi", &shopt_vi, true, },
#endif
{ L'x', 0, L"xtrace", &shopt_xtrace, true, },
{ 0, 0, NULL, NULL, false, },
};
enum normal_option_index_T {
#if YASH_ENABLE_HELP
NOI_HELP,
#endif
NOI_VERSION,
NOI_NOPROFILE,
NOI_NORCFILE,
NOI_PROFILE,
NOI_RCFILE,
NOI_N,
};
/* List of the normal options.
* The normal options are the options that can be used on the shell invocation
* but that are not shell options. */
/* A non-NULL `ptr' member indicates that the option can be used for the "set"
* built-in. */
static const struct xgetopt_T normal_options[] = {
#if YASH_ENABLE_HELP
[NOI_HELP] = { L'-', L"help", OPTARG_NONE, false,
(void *) &normal_options, },
#endif
[NOI_VERSION] = { L'V', L"version", OPTARG_NONE, false, NULL, },
[NOI_NOPROFILE] = { L'-', L"noprofile", OPTARG_NONE, false, NULL, },
[NOI_NORCFILE] = { L'-', L"norcfile", OPTARG_NONE, false, NULL, },
[NOI_PROFILE] = { L'-', L"profile", OPTARG_REQUIRED, false, NULL, },
[NOI_RCFILE] = { L'-', L"rcfile", OPTARG_REQUIRED, false, NULL, },
[NOI_N] = { L'\0', NULL, 0, false, NULL, },
};
/********** Functions **********/
static wchar_t *normalize_option_name(const wchar_t *optname)
__attribute__((nonnull,malloc,warn_unused_result));
static int parse_short_option(void *const *argv, bool enable,
struct shell_invocation_T *shell_invocation)
__attribute__((nonnull(1)));
static int parse_option_character(
wchar_t opt, bool enable, struct shell_invocation_T *shell_invocation);
static int parse_long_option(void *const *argv, bool enable,
struct shell_invocation_T *shell_invocation)
__attribute__((nonnull(1)));
static size_t collect_matching_shell_options(
const wchar_t *optstr, plist_T *options)
__attribute__((nonnull));
static void search_shell_options(const wchar_t *optname, plist_T *resultlist)
__attribute__((nonnull));
static void search_normal_options(const wchar_t *optname, plist_T *resultlist,
const struct shell_invocation_T *shell_invocation)
__attribute__((nonnull(1,2)));
static int handle_search_result(plist_T *options, void *const *argv,
bool enable, size_t shelloptindex, size_t noshelloptindex,
struct shell_invocation_T *shell_invocation)
__attribute__((nonnull(1, 2)));
static int set_shell_option(const struct option_T *option, bool enable,
struct shell_invocation_T *shell_invocation)
__attribute__((nonnull(1)));
#if YASH_ENABLE_LINEEDIT
static void update_lineedit_option(void);
static void update_le_convmeta_option(void);
#endif
static int set_normal_option(const struct xgetopt_T *opt, const wchar_t *arg,
struct shell_invocation_T *shell_invocation)
__attribute__((nonnull(1)));
#if YASH_ENABLE_TEST
static int test_option_unique(const wchar_t *s)
__attribute__((nonnull,pure));
#endif
/* Parses the specified command line arguments and accordingly sets the shell
* options and positional parameters.
* When this function is called from the `main' function of the shell,
* `shell_invocation' must be a pointer to a structure to which parse result is
* stored. Otherwise, that is, when called from the set built-in,
* `shell_invocation' must be NULL.
* Positional parameters are set only when `shell_invocation' is NULL.
* `xoptind' is set to the index of the first operand.
* Returns Exit_SUCCESS if successful. Otherwise, returns Exit_FAILURE or
* Exit_ERROR after printing an error message. */
int parse_shell_options(int argc, void *const *argv,
struct shell_invocation_T *shell_invocation)
{
/* We don't use the xgetopt function because of the non-standard syntax. */
for (xoptind = 1; xoptind < argc; xoptind++) {
const wchar_t *arg = ARGV(xoptind);
bool enable;
switch (arg[0]) {
case L'-': enable = true; break;
case L'+': enable = false; break;
default: goto main;
}
if (arg[1] == L'\0') {
if (enable && shell_invocation != NULL) /* ignore first "-" */
xoptind++;
goto main;
}
int result;
if (arg[0] == arg[1]) {
if (arg[2] == L'\0') {
if (enable) /* `arg' is L"--" */
xoptind++;
goto set_positional_parameters;
}
result = parse_long_option(argv, enable, shell_invocation);
} else {
result = parse_short_option(argv, enable, shell_invocation);
}
if (result != Exit_SUCCESS)
return result;
}
main:
if (xoptind < argc) {
set_positional_parameters:
assert(xoptind <= argc);
if (shell_invocation == NULL)
set_positional_parameters(&argv[xoptind]);
}
return Exit_SUCCESS;
}
/* Returns a newly malloced string containing the same string as the argument
* string except that the returned string does not contain non-alphanumeric
* characters. */
wchar_t *normalize_option_name(const wchar_t *optname)
{
xwcsbuf_T result;
wb_initwithmax(&result, wcslen(optname));
for (const wchar_t *s = optname; *s != L'\0'; s++)
if (iswalnum(*s))
wb_wccat(&result, towlower(*s));
return wb_towcs(&result);
}
/* Parses the single-character option at `xoptind' in the specified arguments
* and enables/disables the option.
* Returns Exit_SUCCESS iff successful. Prints an error message on error. */
int parse_short_option(void *const *argv, bool enable,
struct shell_invocation_T *shell_invocation)
{
const wchar_t *optstr = ARGV(xoptind);
assert(optstr[0] != L'\0');
for (size_t i = 1; optstr[i] != L'\0'; i++) {
if (optstr[i] == L'o') {
const wchar_t *optname = &optstr[i + 1];
if (*optname == L'\0') {
optname = ARGV(++xoptind);;
if (optname == NULL) {
xerror(0, Ngt("the -%lc option requires an argument"),
(wint_t) L'o');
return special_builtin_error(Exit_ERROR);
}
}
plist_T options;
pl_init(&options);
size_t shelloptindex =
collect_matching_shell_options(optname, &options);
return handle_search_result(&options, argv, enable,
shelloptindex, options.length, shell_invocation);
} else {
int result = parse_option_character(
optstr[i], enable, shell_invocation);
if (result != Exit_SUCCESS)
return result;
}
}
return Exit_SUCCESS;
}
/* Parses the specified single-character option and enables/disables the option.
* Returns Exit_SUCCESS iff successful. Prints an error message on error. */
int parse_option_character(
wchar_t opt, bool enable, struct shell_invocation_T *shell_invocation)
{
assert(opt != L'\0');
for (const struct option_T *o = shell_options; o->longopt != NULL; o++) {
if (opt == o->shortopt_enable)
return set_shell_option(o, enable, shell_invocation);
else if (opt == o->shortopt_disable)
return set_shell_option(o, !enable, shell_invocation);
}
if (opt != L'-') {
const struct xgetopt_T *o;
for (o = normal_options; o->shortopt != L'\0'; o++) {
if (!o->posix && posixly_correct)
continue;
if (opt == o->shortopt &&
(shell_invocation != NULL || o->ptr != NULL))
return set_normal_option(o, NULL, shell_invocation);
}
}
xerror(0, Ngt("`%ls' is not a valid option"), (wchar_t[]) { opt, L'\0' });
return special_builtin_error(Exit_ERROR);
}
/* Parses the long option at `xoptind' in the specified arguments and
* enables/disables the option.
* Returns Exit_SUCCESS iff successful. Prints an error message on error. */
int parse_long_option(void *const *argv, bool enable,
struct shell_invocation_T *shell_invocation)
{
const wchar_t *optstr = ARGV(xoptind);
plist_T options;
size_t shelloptindex, noshelloptindex;
pl_init(&options);
if (posixly_correct) {
/* No long options are available in the POSIXly correct mode. */
shelloptindex = noshelloptindex = 0;
} else {
shelloptindex = collect_matching_shell_options(optstr, &options);
noshelloptindex = options.length;
if (enable) {
assert(matchwcsprefix(optstr, L"--"));
search_normal_options(&optstr[2], &options, shell_invocation);
}
}
return handle_search_result(&options, argv, enable,
shelloptindex, noshelloptindex, shell_invocation);
}
/* Collects shell options that match the specified name.
* This function adds to `options' pointers to shell options (struct option_T)
* whose names start with `optname'.
* If `optname' starts with "no", options whose names start with `optname'
* without "no" are also added. The index of the first such option is returned.
*/
size_t collect_matching_shell_options(const wchar_t *optname, plist_T *options)
{
wchar_t *normoptname = normalize_option_name(optname);
search_shell_options(normoptname, options);
size_t nooptindex = options->length;
if (matchwcsprefix(normoptname, L"no"))
search_shell_options(&normoptname[2], options);
free(normoptname);
return nooptindex;
}
/* Adds pointers to shell options (struct option_T) whose names match `optname'
* to the specified list.
* If `optname' exactly matches one of the shell option names, `resultlist' is
* cleared and only a pointer to that option is added. */
void search_shell_options(const wchar_t *optname, plist_T *resultlist)
{
for (const struct option_T *o = shell_options; o->longopt != NULL; o++) {
const wchar_t *s = matchwcsprefix(o->longopt, optname);
if (s != NULL) {
if (*s == L'\0') {
/* exact match! */
pl_truncate(resultlist, 0);
pl_add(resultlist, o);
break;
} else {
/* partial match */
pl_add(resultlist, o);
}
}
}
}
/* Adds to `resultlist' pointers to shell options (struct option_T) whose names
* match `optname'.
* If `optname' exactly matches one of shell option names, `resultlist' is
* cleared and only a pointer to that option is added. */
void search_normal_options(const wchar_t *optname, plist_T *resultlist,
const struct shell_invocation_T *shell_invocation)
{
size_t namelen = wcscspn(optname, L"=");
for (const struct xgetopt_T *o = normal_options; o->longopt != NULL; o++) {
if (!o->posix && posixly_correct)
continue;
if (wcsncmp(o->longopt, optname, namelen) == 0 &&
(shell_invocation != NULL || o->ptr != NULL)) {
if (o->longopt[namelen] == L'\0') {
/* exact match! */
pl_truncate(resultlist, 0);
pl_add(resultlist, o);
break;
} else {
/* partial match */
pl_add(resultlist, o);
}
}
}
}
/* Handles the result of option search.
* List `options' must contain pointers to options (struct option_T/xgetopt_T)
* that match the currently parsed option name. List members with indices less
* than `noshelloptindex' are pointers to shell options (struct option_T) and
* the others to normal options (struct xgetopt_T). Inverting shell options,
* which have the "no" prefix in their names, must have indices no less than
* `shelloptindex'. The list is destroyed in this function. */
int handle_search_result(plist_T *options, void *const *argv, bool enable,
size_t shelloptindex, size_t noshelloptindex,
struct shell_invocation_T *shell_invocation)
{
assert(shelloptindex <= noshelloptindex);
assert(noshelloptindex <= options->length);
const wchar_t *optstr = ARGV(xoptind);
switch (options->length) {
case 0:
pl_destroy(options);
xerror(0, Ngt("`%ls' is not a valid option"), optstr);
return special_builtin_error(Exit_ERROR);
case 1:
if (noshelloptindex > 0) {
const struct option_T *opt = options->contents[0];
pl_destroy(options);
if (shelloptindex == 0)
enable = !enable;
return set_shell_option(opt, enable, shell_invocation);
} else {
const struct xgetopt_T *opt = options->contents[0];
const wchar_t *eq = wcschr(optstr, L'=');
const wchar_t *optarg;
pl_destroy(options);
if (opt->optarg != OPTARG_NONE) {
if (eq == NULL) {
switch (opt->optarg) {
case OPTARG_OPTIONAL:
optarg = NULL;
break;
case OPTARG_REQUIRED:
optarg = ARGV(++xoptind);
if (optarg == NULL) {
xerror(0, Ngt("the --%ls option requires "
"an argument"), opt->longopt);
return special_builtin_error(Exit_ERROR);
}
break;
default:
assert(false);
}
} else {
optarg = &eq[1];
}
} else {
if (eq != NULL) {
xerror(0, Ngt("%ls: the --%ls option does not take "
"an argument"), optstr, opt->longopt);
return special_builtin_error(Exit_ERROR);
}
optarg = NULL;
}
return set_normal_option(opt, optarg, shell_invocation);
}
default:
xerror(0, Ngt("option `%ls' is ambiguous"), optstr);
#if LIST_AMBIGUOUS_OPTIONS
size_t i = 0;
for (; i < shelloptindex; i++)
fprintf(stderr, "\t--%ls\n",
((const struct option_T *) options->contents[i])->longopt);
for (; i < noshelloptindex; i++)
fprintf(stderr, "\t--no%ls\n",
((const struct option_T *) options->contents[i])->longopt);
for (; i < options->length; i++)
fprintf(stderr, "\t--%ls\n",
((const struct xgetopt_T *) options->contents[i])->longopt);
#endif
pl_destroy(options);
return special_builtin_error(Exit_ERROR);
}
}
/* Sets the specified shell option.
* Returns Exit_SUCCESS iff successful. Print an error message on error. */
int set_shell_option(const struct option_T *option, bool enable,
struct shell_invocation_T *shell_invocation)
{
if (shell_invocation == NULL && !option->resettable) {
xerror(0, Ngt("the %ls option cannot be changed "
"once the shell has been initialized"),
option->longopt);
return special_builtin_error(Exit_ERROR);
}
*option->optp = enable;
if (shell_invocation != NULL) {
if (option->optp == &is_interactive)
shell_invocation->is_interactive_set = true;
else if (option->optp == &do_job_control)
shell_invocation->do_job_control_set = true;
}
if (shell_invocation == NULL && option->optp == &do_job_control) {
if (doing_job_control_now) {
open_ttyfd();
ensure_foreground();
}
reset_job_signals();
}
#if YASH_ENABLE_LINEEDIT
if (option->optp == &shopt_vi) {
if (enable)
shopt_emacs = false;
update_lineedit_option();
if (shell_invocation != NULL)
shell_invocation->lineedit_set = true;
} else if (option->optp == &shopt_emacs) {
if (enable)
shopt_vi = false;
update_lineedit_option();
if (shell_invocation != NULL)
shell_invocation->lineedit_set = true;
} else if (option->optp == &shopt_le_yesconvmeta) {
if (enable)
shopt_le_noconvmeta = false;
update_le_convmeta_option();
} else if (option->optp == &shopt_le_noconvmeta) {
if (enable)
shopt_le_yesconvmeta = false;
update_le_convmeta_option();
}
#endif
return (*option->optp == enable) ?
Exit_SUCCESS : special_builtin_error(Exit_FAILURE);
}
#if YASH_ENABLE_LINEEDIT
/* Updates the value of `shopt_lineedit' according to the values of `shopt_vi'
* and `shopt_emacs'. */
void update_lineedit_option(void)
{
if (shopt_vi)
shopt_lineedit = SHOPT_VI;
else if (shopt_emacs)
shopt_lineedit = SHOPT_EMACS;
else
shopt_lineedit = SHOPT_NOLINEEDIT;
}
/* Updates the value of `shopt_le_convmeta' according to the values of
* `shopt_le_yesconvmeta' and `shopt_le_noconvmeta'. */
void update_le_convmeta_option(void)
{
if (shopt_le_yesconvmeta)
shopt_le_convmeta = SHOPT_YES;
else if (shopt_le_noconvmeta)
shopt_le_convmeta = SHOPT_NO;
else
shopt_le_convmeta = SHOPT_AUTO;
}
#endif
/* Sets the specified normal option.
* Returns Exit_SUCCESS iff successful. Print an error message on error. */
int set_normal_option(const struct xgetopt_T *opt, const wchar_t *arg,
struct shell_invocation_T *shell_invocation)
{
enum normal_option_index_T index = opt - normal_options;
if (shell_invocation == NULL) {
switch (index) {
#if YASH_ENABLE_HELP
case NOI_HELP:
return print_builtin_help(L"set");
#endif
default:
assert(false);
}
} else {
switch (index) {
#if YASH_ENABLE_HELP
case NOI_HELP:
shell_invocation->help = true;
break;
#endif
case NOI_VERSION:
shell_invocation->version = true;
break;
case NOI_NOPROFILE:
shell_invocation->noprofile = true;
break;
case NOI_NORCFILE:
shell_invocation->norcfile = true;
break;
case NOI_PROFILE:
assert(arg != NULL);
shell_invocation->profile = arg;
break;
case NOI_RCFILE:
assert(arg != NULL);
shell_invocation->rcfile = arg;
break;
case NOI_N:
assert(false);
}
}
return Exit_SUCCESS;
}
#if YASH_ENABLE_LINEEDIT
/* Sets the line-editing option to the specified value. */
void set_lineedit_option(enum shopt_lineedit_T v)
{
shopt_vi = shopt_emacs = false;
shopt_lineedit = v;
switch (v) {
case SHOPT_VI: shopt_vi = true; break;
case SHOPT_EMACS: shopt_emacs = true; break;
case SHOPT_NOLINEEDIT: break;
}
}
#endif
#if YASH_ENABLE_TEST
/* Returns true iff the specified string is a valid option name that can be used
* as the argument to the "-o" option. */
bool is_valid_option_name(const wchar_t *s)
{
return test_option_unique(s);
}
/* Returns true iff the specified string is a valid option name that can be used
* as the argument to the "-o" option and the option is enabled. */
bool option_is_enabled(const wchar_t *s)
{
return test_option_unique(s) == 2;
}
/* Tests if the specified string is a valid option name that can be used
* as the argument to the "-o" option. Returns:
* 0 if the string is not a valid option name,
* 1 if it is a valid option that is currently disabled, or
* 2 if it is a valid option that is currently enabled. */
int test_option_unique(const wchar_t *s)
{
plist_T options;
pl_init(&options);
size_t nooptindex = collect_matching_shell_options(s, &options);
int result = (options.length == 1);
if (result) {
struct option_T *opt = options.contents[0];
bool negated = (nooptindex == 0);
result += *opt->optp ^ negated;
}
pl_destroy(&options);
return result;
}
#endif /* YASH_ENABLE_TEST */
/* Returns the current value of special parameter $- as a newly malloced string.
*/
wchar_t *get_hyphen_parameter(void)
{
xwcsbuf_T buf;
wb_init(&buf);
for (const struct option_T *o = shell_options; o->longopt != NULL; o++) {
if (o->shortopt_enable != L'\0' && *o->optp)
wb_wccat(&buf, o->shortopt_enable);
if (o->shortopt_disable != L'\0' && !*o->optp)
wb_wccat(&buf, o->shortopt_disable);
}
return wb_towcs(&buf);
}
#if YASH_ENABLE_HELP
/* Prints a list of all shell options to the standard output.
* Returns true iff successful. */
bool print_shopts_body(bool include_normal_options)
{
if (include_normal_options)
if (!print_option_list(normal_options))
return false;
for (const struct option_T *o = shell_options; o->longopt != NULL; o++) {
if (!xprintf("\t"))
return false;
if (o->shortopt_enable != L'\0') {
if (!xprintf("-%lc", o->shortopt_enable))
return false;
} else if (o->shortopt_disable != L'\0') {
if (!xprintf("+%lc", o->shortopt_disable))
return false;
} else {
if (!xprintf(" "))
return false;
}
if (!xprintf(" -o %ls\n", o->longopt))
return false;
}
return true;
}
#endif /* YASH_ENABLE_HELP */
/********** Built-in **********/
const struct xgetopt_T all_help_options[] = {
{ L'a', L"all", OPTARG_NONE, true, NULL, },
#if YASH_ENABLE_HELP
{ L'-', L"help", OPTARG_NONE, false, NULL, },
#endif
{ L'\0', NULL, 0, false, NULL, },
};
/* Note: `help_option' is defined as (&all_help_options[1]). */
static int set_builtin_print_current_settings(void);
static int set_builtin_print_restoring_commands(void);
/* The "set" built-in. */
int set_builtin(int argc, void **argv)
{
if (argc <= 1) {
return typeset_builtin(argc, argv);
} else if (argc == 2) {
if (wcscmp(ARGV(1), L"-o") == 0)
return set_builtin_print_current_settings();
else if (wcscmp(ARGV(1), L"+o") == 0)
return set_builtin_print_restoring_commands();
}
return parse_shell_options(argc, argv, NULL);
}
int set_builtin_print_current_settings(void)
{
const char *vals[] = {
[true] = gt("on"),
[false] = gt("off"),
};
for (const struct option_T *o = shell_options; o->longopt != NULL; o++) {
if (!xprintf("%-15ls %s\n", o->longopt, vals[(bool) *o->optp]))
return special_builtin_error(Exit_FAILURE);
}
return Exit_SUCCESS;
}
int set_builtin_print_restoring_commands(void)
{
for (const struct option_T *o = shell_options; o->longopt != NULL; o++) {
if (o->resettable)
if (!xprintf("set %co %ls\n", *o->optp ? '-' : '+', o->longopt))
return special_builtin_error(Exit_FAILURE);
}
return Exit_SUCCESS;
}
#if YASH_ENABLE_HELP
const char set_help[] = Ngt(
"set shell options and positional parameters"
);
const char set_syntax[] = Ngt(
"\tset [option...] [--] [new_positional_parameter...]\n"
"\tset -o|+o # print current settings\n"
);
#endif
/* vim: set ts=8 sts=4 sw=4 et tw=80: */
|