1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1982-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A/*
1N/A * bash specific extensions
1N/A * originally provided by Karsten Fleischer
1N/A */
1N/A
1N/A#include "defs.h"
1N/A#include "path.h"
1N/A#include "io.h"
1N/A#include "builtins.h"
1N/A#include "name.h"
1N/A
1N/A#ifndef BASH_MAJOR
1N/A# define BASH_MAJOR "1"
1N/A# define BASH_MINOR "0"
1N/A# define BASH_PATCH "0"
1N/A# define BASH_BUILD "0"
1N/A# define BASH_RELEASE "experimental"
1N/A#endif
1N/A#define BASH_VERSION BASH_MAJOR "." BASH_MINOR "." BASH_PATCH "(" BASH_BUILD ")-" BASH_RELEASE
1N/A
1N/A
1N/Aextern const char bash_pre_rc[];
1N/A
1N/Astatic char *login_files[4];
1N/A
1N/Aconst char sh_bash1[] =
1N/A "[B?Enable brace group expansion. This option is only availabe in bash "
1N/A "compatibility mode. In ksh mode, brace group expansion is always on.]"
1N/A "[P?Do not follow symbolic links, use physical directory structure "
1N/A "instead. Only available in bash compatibility mode.]";
1N/Aconst char sh_bash2[] =
1N/A"[O]:?[shopt_option?\ashopt_option\a is one of the shell options accepted by "
1N/A "the \bshopt\b builtin. If \ashopt_option\a is present, \b-O\b sets "
1N/A "the value of that option; \b+O\b unsets it. If \ashopt_option\a is "
1N/A "not supplied, the names and values of the shell options accepted by "
1N/A "\bshopt\b are printed on the standard output. If the invocation "
1N/A "option is \b+O\b, the output is displayed in a format that may be "
1N/A "reused as input. Only available if invoked as \bbash\b.]"
1N/A"[01:init-file|rcfile]:[file?Execute commands from \afile\a instead of the "
1N/A "standard personal initialization file ~/.bashrc if the shell is "
1N/A "interactive. Only available if invoked as \bbash\b.]"
1N/A"[02:editing?For option compatibility with \bbash\b only. Ignored.]"
1N/A"[03:profile?Read either the system-wide startup file or any of the "
1N/A "personal initialization files. On by default for interactive "
1N/A "shells. Only available if invoked as \bbash\b.]"
1N/A"[04:posix?If invoked as \bbash\b, turn on POSIX compatibility. \bBash\b in "
1N/A "POSIX mode is not the same as \bksh\b.]"
1N/A"[05:version?Print version number and exit.]";
1N/A
1N/Aconst char sh_optshopt[] =
1N/A"+[-1c?\n@(#)$Id: shopt (AT&T Research) 2003-02-13 $\n]"
1N/A"[-author?Karsten Fleischer <K.Fleischer@omnium.de>]"
1N/AUSAGE_LICENSE
1N/A"[+NAME?shopt - set/unset variables controlling optional shell behavior]"
1N/A"[+DESCRIPTION?\bshopt\b sets or unsets variables controlling optional shell "
1N/A "behavior. With no options, or with the \b-p\b option, a list of all "
1N/A "settable options is displayed, with an indication of whether or not "
1N/A "each is set.]"
1N/A"[p?Causes output to be displayed in a form that may be reused as input.]"
1N/A"[s?Set each \aoptname\a.]"
1N/A"[u?Unset each \aoptname\a.]"
1N/A"[q?Suppress output (quiet mode). The return status indicates whether the "
1N/A "\aoptname\a is set or unset. If multiple \aoptname\a arguments are "
1N/A "given with \b-q\b, the return status is zero if all \aoptname\as are "
1N/A "enabled; non-zero otherwise.]"
1N/A"[o?Restricts the values of \aoptname\a to be those defined for the \b-o\b "
1N/A "option to the set builtin.]"
1N/A"[+?If either \b-s\b or \b-u\b is used with no \aoptname\a arguments, the "
1N/A "display is limited to those options which are set or unset.]"
1N/A"[+?\bshopt\b supports all bash options. Some settings do not have any effect "
1N/A "or are are always on and cannot be changed.]"
1N/A"[+?The value of \aoptname\a must be one of the following:]{"
1N/A "[+cdable_vars?If set, arguments to the \bcd\b command are "
1N/A "assumed to be names of variables whose values are to "
1N/A "be used if the usual \bcd\b proceeding fails.]"
1N/A "[+cdspell?Currently ignored.]"
1N/A "[+checkhash?Always on.]"
1N/A "[+checkwinsize?Currently ignored.]"
1N/A "[+cmdhist?Always on.]"
1N/A "[+dotglob?If set, include filenames beginning with a \b.\b "
1N/A "in the results of pathname expansion.]"
1N/A "[+execfail?Always on.]"
1N/A "[+expand_aliases?Always on.]"
1N/A "[+extglob?Enable extended pattern matching features.]"
1N/A "[+histappend?Always on.]"
1N/A "[+histreedit?If set and an edit mode is selected, the user "
1N/A "is given the opportunity to re-edit a failed history "
1N/A "substitution.]"
1N/A "[+histverify?If set and an edit mode is selected, the result "
1N/A "of a history substitution will not be executed "
1N/A "immediately but be placed in the edit buffer for "
1N/A "further modifications.]"
1N/A "[+hostcomplete?Currently ignored.]"
1N/A "[+huponexit?Currently ignored.]"
1N/A "[+interactive_comments?Always on.]"
1N/A "[+lithist?Always on.]"
1N/A "[+login_shell?This option is set if the shell is started as "
1N/A "a login shell. The value cannot be changed.]"
1N/A "[+mailwarn?Currently ignored.]"
1N/A "[+no_empty_cmd_completion?Always on.]"
1N/A "[+nocaseglob?Match filenames in a case-insensitive fashion "
1N/A "when performing filename expansion.]"
1N/A "[+nullglob?Allows filename patterns which match no files to "
1N/A "expand to a null string, rather than themselves.]"
1N/A "[+progcomp?Currently ignored.]"
1N/A "[+promptvars?Currently ignored.]"
1N/A "[+restricted_shell?This option is set if the shell is started "
1N/A "as a restricted shell. The value cannot be changed. "
1N/A "It is not reset during execution of startup files, "
1N/A "allowing the startup files to determine whether the "
1N/A "shell is restricted.]"
1N/A "[+shift_verbose?Currently ignored.]"
1N/A "[+sourcepath?If set, the \b.\b builtin uses the value of PATH "
1N/A "to find the directory containing the file supplied "
1N/A "as an argument.]"
1N/A "[+xpg_echo?If set, the \becho\b and \bprint\b builtins "
1N/A "expand backslash-escape sequences.]"
1N/A"}"
1N/A"\n"
1N/A"\n[optname ...]\n"
1N/A"\n"
1N/A"[+EXIT STATUS?]{"
1N/A "[+?The return status when listing options is zero if all \aoptnames\a "
1N/A "are enabled, non-zero otherwise. When setting or unsetting options, "
1N/A "the return status is zero unless an \aoptname\a is not a valid shell "
1N/A "option.]"
1N/A"}"
1N/A
1N/A"[+SEE ALSO?\bset\b(1)]"
1N/A;
1N/A
1N/A/* GLOBIGNORE discipline. Turn on SH_DOTGLOB on set, turn off on unset. */
1N/A
1N/Astatic void put_globignore(register Namval_t* np, const char *val, int flags, Namfun_t *fp)
1N/A{
1N/A if(val)
1N/A sh_onoption(SH_DOTGLOB);
1N/A else
1N/A sh_offoption(SH_DOTGLOB);
1N/A
1N/A nv_putv(np,val,flags,fp);
1N/A}
1N/A
1N/Aconst Namdisc_t SH_GLOBIGNORE_disc = { sizeof(Namfun_t), put_globignore };
1N/A
1N/A/* FUNCNAME discipline */
1N/A
1N/Astruct funcname
1N/A{
1N/A Namfun_t hdr;
1N/A};
1N/A
1N/Astatic void put_funcname(register Namval_t* np,const char *val,int flags,Namfun_t *fp)
1N/A{
1N/A /* bash silently returns with an error when FUNCNAME is set,
1N/A unsetting FUNCNAME is allowed */
1N/A if(val && !(flags&NV_RDONLY))
1N/A error_info.exit(1);
1N/A
1N/A nv_putv(np,val,flags,fp);
1N/A}
1N/A
1N/Aconst Namdisc_t SH_FUNCNAME_disc = { sizeof(struct funcname), put_funcname };
1N/A
1N/A#define SET_SET 1
1N/A#define SET_UNSET 2
1N/A#define SET_NOARGS 4
1N/A
1N/A/* shopt builtin */
1N/A
1N/Aint b_shopt(int argc,register char *argv[],void *extra)
1N/A{
1N/A Shell_t *shp = (Shell_t*)extra;
1N/A int n, f, ret=0;
1N/A Shopt_t newflags=shp->options, opt;
1N/A int verbose=PRINT_SHOPT|PRINT_ALL|PRINT_NO_HEADER|PRINT_VERBOSE;
1N/A int setflag=0, quietflag=0, oflag=0;
1N/A memset(&opt,0,sizeof(opt));
1N/A#if SHOPT_RAWONLY
1N/A on_option(&newflags,SH_VIRAW);
1N/A#endif
1N/A while((n = optget(argv,sh_optshopt)))
1N/A {
1N/A switch(n)
1N/A {
1N/A case 'p':
1N/A verbose&=~PRINT_VERBOSE;
1N/A break;
1N/A case 's':
1N/A case 'u':
1N/A setflag|=n=='s'?SET_SET:SET_UNSET;
1N/A if(setflag==(SET_SET|SET_UNSET))
1N/A {
1N/A errormsg(SH_DICT,ERROR_ERROR,"cannot set and unset options simultaneously");
1N/A error_info.errors++;
1N/A }
1N/A break;
1N/A case 'q':
1N/A quietflag=1;
1N/A break;
1N/A case 'o':
1N/A oflag=1;
1N/A verbose&=~PRINT_SHOPT;
1N/A break;
1N/A case ':':
1N/A errormsg(SH_DICT,2, "%s", opt_info.arg);
1N/A continue;
1N/A case '?':
1N/A errormsg(SH_DICT,ERROR_usage(0), "%s", opt_info.arg);
1N/A return(-1);
1N/A }
1N/A }
1N/A if(error_info.errors)
1N/A errormsg(SH_DICT,ERROR_usage(2),"%s",optusage(NIL(char*)));
1N/A argc -= opt_info.index;
1N/A if(argc==0)
1N/A {
1N/A /* no args, -s => mask=current options, -u mask=~(current options)
1N/A else mask=all bits */
1N/A if(setflag&SET_SET)
1N/A opt=newflags;
1N/A else if(setflag&SET_UNSET)
1N/A for(n=0;n<4;n++)
1N/A opt.v[n]=~newflags.v[n];
1N/A else
1N/A memset(&opt,0xff,sizeof(opt));
1N/A setflag=SET_NOARGS;
1N/A }
1N/A while(argc>0)
1N/A {
1N/A f=1;
1N/A n=sh_lookopt(argv[opt_info.index],&f);
1N/A if(n<=0||(setflag
1N/A && (is_option(&opt,SH_INTERACTIVE)
1N/A || is_option(&opt,SH_RESTRICTED)
1N/A || is_option(&opt,SH_RESTRICTED2)
1N/A || is_option(&opt,SH_BASH)
1N/A || is_option(&opt,SH_LOGIN_SHELL)))
1N/A ||(oflag&&(n&SH_BASHOPT)))
1N/A {
1N/A errormsg(SH_DICT,ERROR_ERROR, e_option, argv[opt_info.index]);
1N/A error_info.errors++;
1N/A ret=1;
1N/A }
1N/A else if(f)
1N/A on_option(&opt,n&0xff);
1N/A else
1N/A off_option(&opt,n&0xff);
1N/A opt_info.index++;
1N/A argc--;
1N/A }
1N/A if(setflag&(SET_SET|SET_UNSET))
1N/A {
1N/A if(setflag&SET_SET)
1N/A {
1N/A if(sh_isoption(SH_INTERACTIVE))
1N/A off_option(&opt,SH_NOEXEC);
1N/A if(is_option(&opt,SH_VI)||is_option(&opt,SH_EMACS)||is_option(&opt,SH_GMACS))
1N/A {
1N/A off_option(&newflags,SH_VI);
1N/A off_option(&newflags,SH_EMACS);
1N/A off_option(&newflags,SH_GMACS);
1N/A }
1N/A for(n=0;n<4;n++)
1N/A newflags.v[n] |= opt.v[n];
1N/A }
1N/A else if(setflag&SET_UNSET)
1N/A for(n=0;n<4;n++)
1N/A newflags.v[n] &= ~opt.v[n];
1N/A sh_applyopts(shp,newflags);
1N/A shp->options = newflags;
1N/A if(is_option(&newflags,SH_XTRACE))
1N/A sh_trace(shp,argv,1);
1N/A }
1N/A else if(!(setflag&SET_NOARGS)) /* no -s,-u but args, ret=0 if opt&mask==mask */
1N/A {
1N/A for(n=0;n<4;n++)
1N/A ret+=((newflags.v[n]&opt.v[n])!=opt.v[n]);
1N/A }
1N/A if(!quietflag&&!(setflag&(SET_SET|SET_UNSET)))
1N/A sh_printopts(newflags,verbose,&opt);
1N/A return(ret);
1N/A}
1N/A
1N/A/* mode = 0: init, called two times
1N/A before parsing shell args with SH_PREINIT state turned on
1N/A second time after sh_init() is through and with SH_PREINIT state turned off
1N/A mode > 1: re-init
1N/A mode < 0: shutdown
1N/A*/
1N/A
1N/Avoid bash_init(Shell_t *shp,int mode)
1N/A{
1N/A Sfio_t *iop;
1N/A Namval_t *np;
1N/A int n=0,xtrace,verbose;
1N/A if(mode>0)
1N/A goto reinit;
1N/A if(mode < 0)
1N/A {
1N/A /* termination code */
1N/A if(sh_isoption(SH_LOGIN_SHELL) && !sh_isoption(SH_POSIX))
1N/A sh_source(shp, NiL, sh_mactry(shp,(char*)e_bash_logout));
1N/A return;
1N/A }
1N/A
1N/A if(sh_isstate(SH_PREINIT))
1N/A { /* pre-init stage */
1N/A if(sh_isoption(SH_RESTRICTED))
1N/A sh_onoption(SH_RESTRICTED2);
1N/A sh_onoption(SH_HISTORY2);
1N/A sh_onoption(SH_INTERACTIVE_COMM);
1N/A sh_onoption(SH_SOURCEPATH);
1N/A sh_onoption(SH_HISTAPPEND);
1N/A sh_onoption(SH_CMDHIST);
1N/A sh_onoption(SH_LITHIST);
1N/A sh_onoption(SH_NOEMPTYCMDCOMPL);
1N/A if(shp->login_sh==2)
1N/A sh_onoption(SH_LOGIN_SHELL);
1N/A if(strcmp(astconf("CONFORMANCE",0,0),"standard")==0)
1N/A sh_onoption(SH_POSIX);
1N/A if(strcmp(astconf("UNIVERSE",0,0),"att")==0)
1N/A sh_onoption(SH_XPG_ECHO);
1N/A else
1N/A sh_offoption(SH_XPG_ECHO);
1N/A if(strcmp(astconf("PATH_RESOLVE",0,0),"physical")==0)
1N/A sh_onoption(SH_PHYSICAL);
1N/A else
1N/A sh_offoption(SH_PHYSICAL);
1N/A
1N/A /* add builtins */
1N/A sh_addbuiltin("shopt", b_shopt, &sh);
1N/A
1N/A /* set up some variables needed for --version
1N/A * needs to go here because --version option is parsed before the init script.
1N/A */
1N/A if(np=nv_open("HOSTTYPE",shp->var_tree,0))
1N/A nv_putval(np, BASH_HOSTTYPE, NV_NOFREE);
1N/A if(np=nv_open("MACHTYPE",shp->var_tree,0))
1N/A nv_putval(np, BASH_MACHTYPE, NV_NOFREE);
1N/A if(np=nv_open("BASH_VERSION",shp->var_tree,0))
1N/A nv_putval(np, BASH_VERSION, NV_NOFREE);
1N/A if(np=nv_open("BASH_VERSINFO",shp->var_tree,0))
1N/A {
1N/A char *argv[7];
1N/A argv[0] = BASH_MAJOR;
1N/A argv[1] = BASH_MINOR;
1N/A argv[2] = BASH_PATCH;
1N/A argv[3] = BASH_BUILD;
1N/A argv[4] = BASH_RELEASE;
1N/A argv[5] = BASH_MACHTYPE;
1N/A argv[6] = 0;
1N/A nv_setvec(np, 0, 6, argv);
1N/A nv_onattr(np,NV_RDONLY);
1N/A }
1N/A return;
1N/A }
1N/A
1N/A /* rest of init stage */
1N/A
1N/A /* restrict BASH_ENV */
1N/A if(np=nv_open("BASH_ENV",shp->var_tree,0))
1N/A {
1N/A const Namdisc_t *dp = nv_discfun(NV_DCRESTRICT);
1N/A Namfun_t *fp = calloc(dp->dsize,1);
1N/A fp->disc = dp;
1N/A nv_disc(np, fp, 0);
1N/A }
1N/A
1N/A /* open GLOBIGNORE node */
1N/A if(np=nv_open("GLOBIGNORE",shp->var_tree,0))
1N/A {
1N/A const Namdisc_t *dp = &SH_GLOBIGNORE_disc;
1N/A Namfun_t *fp = calloc(dp->dsize,1);
1N/A fp->disc = dp;
1N/A nv_disc(np, fp, 0);
1N/A }
1N/A
1N/A /* set startup files */
1N/A n=0;
1N/A if(sh_isoption(SH_LOGIN_SHELL))
1N/A {
1N/A if(!sh_isoption(SH_POSIX))
1N/A {
1N/A login_files[n++] = (char*)e_bash_profile;
1N/A login_files[n++] = (char*)e_bash_login;
1N/A }
1N/A login_files[n++] = (char*)e_profile;
1N/A }
1N/A shp->login_files = login_files;
1N/Areinit:
1N/A xtrace = sh_isoption(SH_XTRACE);
1N/A sh_offoption(SH_XTRACE);
1N/A verbose = sh_isoption(SH_VERBOSE);
1N/A sh_offoption(SH_VERBOSE);
1N/A if(np = nv_open("SHELLOPTS", shp->var_tree, NV_NOADD))
1N/A nv_offattr(np,NV_RDONLY);
1N/A iop = sfopen(NULL, bash_pre_rc, "s");
1N/A sh_eval(iop,0);
1N/A if(xtrace)
1N/A sh_offoption(SH_XTRACE);
1N/A if(verbose)
1N/A sh_offoption(SH_VERBOSE);
1N/A}