* init.c - main loop and initialization routines
* This file is part of zsh, the Z shell.
* This software is Copyright 1992 by Paul Falstad
* Permission is hereby granted to copy, reproduce, redistribute or otherwise
* use this software as long as: there is no monetary profit gained
* specifically from the use or reproduction of this software, it is not
* sold, rented, traded or otherwise marketed, and this copyright notice is
* included prominently in any copy made.
* The author make no claims as to the fitness or correctness of this software
* for any use whatsoever, and it is provided as is. Any use of this software
* is at the user's own risk.
void main(argc
,argv
,envp
) /**/
int argc
; char **argv
; char **envp
;
if (!(isset(IGNOREEOF
) && interact
))
fputs(islogin
? "logout\n" : "exit\n",stderr
);
zerrnam("zsh",(!islogin
) ? "use 'exit' to exit."
: "use 'logout' to logout.",NULL
,0);
/* keep executing lists until EOF found */
Heap h
= (Heap
) peekfirst(heaplist
);
if (interact
&& isset(SHINSTDIN
))
hbegin(); /* init history mech */
intr(); /* interrupts on */
ainit(); /* init alias mech */
if (!(list
= parse_event()))
{ /* if we couldn't parse a list */
if (tok
== ENDINPUT
&& !errflag
)
if (stopmsg
) /* unset 'you have stopped jobs' flag */
zerr("write error",NULL
,0);
if (subsh
) /* how'd we get this far in a subshell? */
if ((!interact
&& errflag
) || retflag
)
if ((opts
['t'] == OPT_SET
) || (lastval
&& opts
[ERREXIT
] == OPT_SET
))
while ((Heap
) peekfirst(heaplist
) != h
)
for (c
= 0; c
!= 32; c
++) opts
[c
] = OPT_UNSET
;
for (c
= 32; c
!= 128; c
++) opts
[c
] = OPT_INVALID
;
for (c
= 'a'; c
<= 'z'; c
++) opts
[c
] = opts
[c
-'a'+'A'] = OPT_UNSET
;
for (c
= '0'; c
<= '9'; c
++) opts
[c
] = OPT_UNSET
;
opts
['i'] = (isatty(0)) ? OPT_SET
: OPT_UNSET
;
opts
[BGNICE
] = opts
[NOTIFY
] = OPT_SET
;
opts
[USEZLE
] = (interact
&& SHTTY
!= -1) ? OPT_SET
: OPT_UNSET
;
opts
[HASHCMDS
] = opts
[HASHLISTALL
] = opts
[HASHDIRS
] = OPT_SET
;
void parseargs(argv
) /**/
hackzero
= argzero
= *argv
;
opts
[LOGINSHELL
] = (**(argv
++) == '-') ? OPT_SET
: OPT_UNSET
;
while (!bk
&& *argv
&& (**argv
== '-' || **argv
== '+'))
action
= (**argv
== '-') ? OPT_SET
: OPT_UNSET
;
if (opts
[**argv
] == OPT_INVALID
) {
zerr("bad option: -%c",NULL
,**argv
);
if (bk
= **argv
== 'b') break;
if (**argv
== 'c') { /* -c command */
zerr("string expected after -c",NULL
,0);
opts
[INTERACTIVE
] = OPT_UNSET
;
} else if (**argv
== 'o') {
zerr("string expected after -o",NULL
,0);
zerr("no such option: %s",*argv
,0);
} else opts
[**argv
] = action
;
if (opts
[SHINSTDIN
] == OPT_UNSET
)
SHIN
= movefd(open(argzero
= *argv
,O_RDONLY
));
zerr("can't open input file: %s",*argv
,0);
opts
[INTERACTIVE
] = OPT_UNSET
;
addnode(paramlist
,ztrdup(*argv
++));
opts
[SHINSTDIN
] = OPT_SET
;
pparams
= x
= zcalloc((countnodes(paramlist
)+1)*sizeof(char *));
while (*x
++ = getnode(paramlist
));
argzero
= ztrdup(argzero
);
/* stdout,stderr fully buffered */
setvbuf(stdout
,malloc(BUFSIZ
),_IOFBF
,BUFSIZ
);
setvbuf(stderr
,malloc(BUFSIZ
),_IOFBF
,BUFSIZ
);
setbuffer(stdout
,malloc(BUFSIZ
),BUFSIZ
);
setbuffer(stderr
,malloc(BUFSIZ
),BUFSIZ
);
/* this works around a bug in some versions of in.rshd */
for (t0
= 3; t0
!= 10; t0
++)
opts
[MONITOR
] = (interact
) ? OPT_SET
: OPT_UNSET
;
SHTTY
= movefd((isatty(0)) ? dup(0) : open("/dev/tty",O_RDWR
));
opts
[MONITOR
] = OPT_UNSET
;
ioctl(SHTTY
, TIOCSETD
, &ldisc
);
gettyinfo(&shttyinfo
); /* get tty state */
if (shttyinfo
.tio
.c_cc
[VSWTCH
] <= 0) /* hack for irises */
shttyinfo
.tio
.c_cc
[VSWTCH
] = CSWTCH
;
savedttyinfo
= shttyinfo
;
if ((mypgrp
= getpgrp(0)) <= 0)
opts
[MONITOR
] = OPT_UNSET
;
else while ((ttpgrp
= gettygrp()) != -1 && ttpgrp
!= mypgrp
) {
if (mypgrp
== gettygrp()) break;
opts
[MONITOR
] = OPT_UNSET
;
SHTTY
= movefd((isatty(0)) ? dup(0) : open("/dev/tty",O_RDWR
));
savedttyinfo
= shttyinfo
;
0,50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400
histsiz
= DEFAULT_HISTSIZE
;
mailcheck
= logcheck
= 60;
curjob
= prevjob
= coprocin
= coprocout
= -1;
shtimer
= time(NULL
); /* init $SECONDS */
srand((unsigned int) shtimer
);
/* build various hash tables; argument to newhtable is table size */
aliastab
= newhtable(37);
paramtab
= newhtable(151);
cmdnamtab
= newhtable(37);
compctltab
= newhtable(13);
readnullcmd
= ztrdup("more");
prompt
= ztrdup("%m%# ");
sprompt
= ztrdup("zsh: correct `%R' to `%r' [nyae]? ");
baud
= cfgetospeed(&shttyinfo
.tio
);
if (baud
< 100) baud
= bauds
[baud
]; /* aren't "standards" great?? */
baud
= bauds
[shttyinfo
.tio
.c_cflag
& CBAUD
];
baud
= bauds
[shttyinfo
.sgttyb
.sg_ospeed
];
if (!(columns
= shttyinfo
.winsize
.ws_col
))
if (!(lines
= shttyinfo
.winsize
.ws_row
))
if (pswd
= getpwuid(getuid())) {
username
= ztrdup(pswd
->pw_name
);
home
= ztrdup(pswd
->pw_dir
);
if (ptr
= zgetenv("LOGNAME"))
logname
= ztrdup(username
);
timefmt
= ztrdup(DEFTIMEFMT
);
watchfmt
= ztrdup(DEFWATCHFMT
);
if (!(ttystrname
= ztrdup(ttyname(SHTTY
))))
wordchars
= ztrdup(DEFWORDCHARS
);
fceditparam
= ztrdup(DEFFCEDIT
);
tmpprefix
= ztrdup(DEFTMPPREFIX
);
if (ispwd(home
)) pwd
= ztrdup(home
);
else if ((ptr
= zgetenv("PWD")) && ispwd(ptr
)) pwd
= ztrdup(ptr
);
gethostname(hostnam
,256);
mailpath
= mkarray(NULL
);
userdirs
= (char **) zcalloc(sizeof(char *)*2);
usernames
= (char **) zcalloc(sizeof(char *)*2);
path
= (char **) zalloc(4*sizeof *path
);
path
[0] = ztrdup("/bin"); path
[1] = ztrdup("/usr/bin");
path
[2] = ztrdup("/usr/ucb"); path
[3] = NULL
;
if ((s
= zgetenv("EMACS")) && !strcmp(s
,"t") && !strcmp(term
,"emacs"))
opts
[USEZLE
] = OPT_UNSET
;
*hs
[] = {"telnet","rlogin","ftp","rup","rusers","rsh",NULL
},
*os
[] = {"setopt","unsetopt",NULL
},
*vs
[] = {"export","typeset","vared","unset",NULL
},
*cs
[] = {"which","builtin",NULL
},
*bs
[] = {"bindkey",NULL
};
compctl_process(hs
,CC_HOSTS
|CC_FILES
,NULL
);
compctl_process(os
,CC_OPTIONS
,NULL
);
compctl_process(vs
,CC_VARS
,NULL
);
compctl_process(bs
,CC_BINDINGS
,NULL
);
compctl_process(cs
,CC_COMMPATH
,NULL
);
cc_compos
.mask
= CC_COMMPATH
;
cc_default
.mask
= CC_FILES
;
lastmailcheck
= time(NULL
);
inbuf
= zalloc(inbufsz
= 256);
inbufptr
= inbuf
+inbufsz
;
for (t0
= 0; t0
!= RLIM_NLIMITS
; t0
++)
bshin
= fdopen(SHIN
,"r");
ioctl(SHTTY
, TIOCGPGRP
, &ttypgrp
);
ttypgrp
= tcgetpgrp(SHTTY
);
signal(SIGWINCH
,handler
);
static char *reswds
[] = {
"do", "done", "esac", "then", "elif", "else", "fi", "for", "case",
"if", "while", "function", "repeat", "time", "until", "exec", "command",
"select", "coproc", "noglob", "-", "nocorrect", "foreach", "end", NULL
for (t0
= 0; reswds
[t0
]; t0
++)
addhperm(reswds
[t0
],mkanode(NULL
,-1-t0
),aliastab
,NULL
);
if (opts
[NORCS
] == OPT_SET
) {
if (islogin
) source(GLOBALZPROFILE
);
if (islogin
) source(GLOBALZLOGIN
);
if (opts
[NORCS
] == OPT_SET
)
readhistfile(getsparam("HISTFILE"),0);
if (opts
['c'] == OPT_SET
)
SHIN
= movefd(open("/dev/null",O_RDONLY
));
if (!(columns
= shttyinfo
.winsize
.ws_col
))
if (!(lines
= shttyinfo
.winsize
.ws_row
))
alstackind
= 0; /* reset alias stack */