📄 set.def
字号:
This file is set.def, from which is created set.c.It implements the "set" and "unset" builtins in Bash.Copyright (C) 1987-2009 Free Software Foundation, Inc.This file is part of GNU Bash, the Bourne Again SHell.Bash is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.Bash is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Bash. If not, see <http://www.gnu.org/licenses/>.$PRODUCES set.c#include <config.h>#if defined (HAVE_UNISTD_H)# ifdef _MINIX# include <sys/types.h># endif# include <unistd.h>#endif#include <stdio.h>#include "../bashansi.h"#include "../bashintl.h"#include "../shell.h"#include "../flags.h"#include "common.h"#include "bashgetopt.h"#if defined (READLINE)# include "../input.h"# include "../bashline.h"# include <readline/readline.h>#endif#if defined (HISTORY)# include "../bashhist.h"#endifextern int posixly_correct, ignoreeof, eof_encountered_limit;#if defined (HISTORY)extern int dont_save_function_defs;#endif#if defined (READLINE)extern int no_line_editing;#endif /* READLINE */$BUILTIN set$FUNCTION set_builtin$SHORT_DOC set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]Set or unset values of shell options and positional parameters.Change the value of shell attributes and positional parameters, ordisplay the names and values of shell variables.Options: -a Mark variables which are modified or created for export. -b Notify of job termination immediately. -e Exit immediately if a command exits with a non-zero status. -f Disable file name generation (globbing). -h Remember the location of commands as they are looked up. -k All assignment arguments are placed in the environment for a command, not just those that precede the command name. -m Job control is enabled. -n Read commands but do not execute them. -o option-name Set the variable corresponding to option-name: allexport same as -a braceexpand same as -B#if defined (READLINE) emacs use an emacs-style line editing interface#endif /* READLINE */ errexit same as -e errtrace same as -E functrace same as -T hashall same as -h#if defined (BANG_HISTORY) histexpand same as -H#endif /* BANG_HISTORY */#if defined (HISTORY) history enable command history#endif ignoreeof the shell will not exit upon reading EOF interactive-comments allow comments to appear in interactive commands keyword same as -k monitor same as -m noclobber same as -C noexec same as -n noglob same as -f nolog currently accepted but ignored notify same as -b nounset same as -u onecmd same as -t physical same as -P pipefail the return value of a pipeline is the status of the last command to exit with a non-zero status, or zero if no command exited with a non-zero status posix change the behavior of bash where the default operation differs from the Posix standard to match the standard privileged same as -p verbose same as -v#if defined (READLINE) vi use a vi-style line editing interface#endif /* READLINE */ xtrace same as -x -p Turned on whenever the real and effective user ids do not match. Disables processing of the $ENV file and importing of shell functions. Turning this option off causes the effective uid and gid to be set to the real uid and gid. -t Exit after reading and executing one command. -u Treat unset variables as an error when substituting. -v Print shell input lines as they are read. -x Print commands and their arguments as they are executed.#if defined (BRACE_EXPANSION) -B the shell will perform brace expansion#endif /* BRACE_EXPANSION */ -C If set, disallow existing regular files to be overwritten by redirection of output. -E If set, the ERR trap is inherited by shell functions.#if defined (BANG_HISTORY) -H Enable ! style history substitution. This flag is on by default when the shell is interactive.#endif /* BANG_HISTORY */ -P If set, do not follow symbolic links when executing commands such as cd which change the current directory. -T If set, the DEBUG trap is inherited by shell functions. -- Assign any remaining arguments to the positional parameters. If there are no remaining arguments, the positional parameters are unset. - Assign any remaining arguments to the positional parameters. The -x and -v options are turned off.Using + rather than - causes these flags to be turned off. Theflags can also be used upon invocation of the shell. The currentset of flags may be found in $-. The remaining n ARGs are positionalparameters and are assigned, in order, to $1, $2, .. $n. If noARGs are given, all shell variables are printed.Exit Status:Returns success unless an invalid option is given.$ENDtypedef int setopt_set_func_t __P((int, char *));typedef int setopt_get_func_t __P((char *));static void print_minus_o_option __P((char *, int, int));static void print_all_shell_variables __P((void));static int set_ignoreeof __P((int, char *));static int set_posix_mode __P((int, char *));#if defined (READLINE)static int set_edit_mode __P((int, char *));static int get_edit_mode __P((char *));#endif#if defined (HISTORY)static int bash_set_history __P((int, char *));#endifstatic const char * const on = "on";static const char * const off = "off";/* A struct used to match long options for set -o to the corresponding option letter or internal variable. The functions can be called to dynamically generate values. */const struct { char *name; int letter; int *variable; setopt_set_func_t *set_func; setopt_get_func_t *get_func;} o_options[] = { { "allexport", 'a', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },#if defined (BRACE_EXPANSION) { "braceexpand",'B', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },#endif#if defined (READLINE) { "emacs", '\0', (int *)NULL, set_edit_mode, get_edit_mode },#endif { "errexit", 'e', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "errtrace", 'E', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "functrace", 'T', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "hashall", 'h', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },#if defined (BANG_HISTORY) { "histexpand", 'H', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },#endif /* BANG_HISTORY */#if defined (HISTORY) { "history", '\0', &enable_history_list, bash_set_history, (setopt_get_func_t *)NULL },#endif { "ignoreeof", '\0', &ignoreeof, set_ignoreeof, (setopt_get_func_t *)NULL }, { "interactive-comments", '\0', &interactive_comments, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "keyword", 'k', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "monitor", 'm', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "noclobber", 'C', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "noexec", 'n', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "noglob", 'f', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },#if defined (HISTORY) { "nolog", '\0', &dont_save_function_defs, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },#endif#if defined (JOB_CONTROL) { "notify", 'b', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },#endif /* JOB_CONTROL */ { "nounset", 'u', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "onecmd", 't', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "physical", 'P', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "pipefail", '\0', &pipefail_opt, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "posix", '\0', &posixly_correct, set_posix_mode, (setopt_get_func_t *)NULL }, { "privileged", 'p', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, { "verbose", 'v', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },#if defined (READLINE) { "vi", '\0', (int *)NULL, set_edit_mode, get_edit_mode },#endif { "xtrace", 'x', (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL }, {(char *)NULL, 0 , (int *)NULL, (setopt_set_func_t *)NULL, (setopt_get_func_t *)NULL },};#define N_O_OPTIONS (sizeof (o_options) / sizeof (o_options[0]))#define GET_BINARY_O_OPTION_VALUE(i, name) \ ((o_options[i].get_func) ? (*o_options[i].get_func) (name) \ : (*o_options[i].variable))#define SET_BINARY_O_OPTION_VALUE(i, onoff, name) \ ((o_options[i].set_func) ? (*o_options[i].set_func) (onoff, name) \ : (*o_options[i].variable = (onoff == FLAG_ON)))intminus_o_option_value (name) char *name;{ register int i; int *on_or_off; for (i = 0; o_options[i].name; i++) { if (STREQ (name, o_options[i].name)) { if (o_options[i].letter) { on_or_off = find_flag (o_options[i].letter); return ((on_or_off == FLAG_UNKNOWN) ? -1 : *on_or_off); } else return (GET_BINARY_O_OPTION_VALUE (i, name)); } } return (-1);}#define MINUS_O_FORMAT "%-15s\t%s\n"static voidprint_minus_o_option (name, value, pflag) char *name; int value, pflag;{ if (pflag == 0) printf (MINUS_O_FORMAT, name, value ? on : off); else printf ("set %co %s\n", value ? '-' : '+', name);}voidlist_minus_o_opts (mode, reusable) int mode, reusable;{ register int i; int *on_or_off, value; for (i = 0; o_options[i].name; i++) { if (o_options[i].letter) { value = 0; on_or_off = find_flag (o_options[i].letter); if (on_or_off == FLAG_UNKNOWN) on_or_off = &value; if (mode == -1 || mode == *on_or_off) print_minus_o_option (o_options[i].name, *on_or_off, reusable); } else { value = GET_BINARY_O_OPTION_VALUE (i, o_options[i].name); if (mode == -1 || mode == value) print_minus_o_option (o_options[i].name, value, reusable); } }}char **get_minus_o_opts (){ char **ret; int i; ret = strvec_create (N_O_OPTIONS + 1); for (i = 0; o_options[i].name; i++) ret[i] = o_options[i].name; ret[i] = (char *)NULL; return ret;}static intset_ignoreeof (on_or_off, option_name) int on_or_off; char *option_name;{ ignoreeof = on_or_off == FLAG_ON; unbind_variable ("ignoreeof"); if (ignoreeof) bind_variable ("IGNOREEOF", "10", 0); else unbind_variable ("IGNOREEOF"); sv_ignoreeof ("IGNOREEOF"); return 0;}static intset_posix_mode (on_or_off, option_name) int on_or_off; char *option_name;{ posixly_correct = on_or_off == FLAG_ON; if (posixly_correct == 0) unbind_variable ("POSIXLY_CORRECT"); else bind_variable ("POSIXLY_CORRECT", "y", 0); sv_strict_posix ("POSIXLY_CORRECT"); return (0);}#if defined (READLINE)/* Magic. This code `knows' how readline handles rl_editing_mode. */static intset_edit_mode (on_or_off, option_name) int on_or_off; char *option_name;{ int isemacs; if (on_or_off == FLAG_ON) { rl_variable_bind ("editing-mode", option_name); if (interactive) with_input_from_stdin (); no_line_editing = 0; } else { isemacs = rl_editing_mode == 1; if ((isemacs && *option_name == 'e') || (!isemacs && *option_name == 'v')) { if (interactive) with_input_from_stream (stdin, "stdin"); no_line_editing = 1; } } return 1-no_line_editing;}static intget_edit_mode (name) char *name;{ return (*name == 'e' ? no_line_editing == 0 && rl_editing_mode == 1 : no_line_editing == 0 && rl_editing_mode == 0);}#endif /* READLINE */#if defined (HISTORY)static intbash_set_history (on_or_off, option_name) int on_or_off; char *option_name;{ if (on_or_off == FLAG_ON) { enable_history_list = 1; bash_history_enable (); if (history_lines_this_session == 0) load_history (); } else { enable_history_list = 0; bash_history_disable (); } return (1 - enable_history_list);}#endifintset_minus_o_option (on_or_off, option_name) int on_or_off; char *option_name;{ register int i; for (i = 0; o_options[i].name; i++) { if (STREQ (option_name, o_options[i].name)) { if (o_options[i].letter == 0) { SET_BINARY_O_OPTION_VALUE (i, on_or_off, option_name); return (EXECUTION_SUCCESS); } else { if (change_flag (o_options[i].letter, on_or_off) == FLAG_ERROR) { sh_invalidoptname (option_name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -