-
Notifications
You must be signed in to change notification settings - Fork 1
/
.kshlogin
207 lines (194 loc) · 6.56 KB
/
.kshlogin
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
#
# .kshlogin - login shell startup for ksh, sourced by ~/.profile
#
#ident "@(#)HOME:.kshlogin 37.7 24/10/16 17:01:42 (woods)"
# Note I allow for /etc/profile to be explicitly sourced by non-interactive,
# non-login shells, e.g. in ~/.xinitrc and from remote invocations of
# onx11server (defined in ~/.shrc), so here we're testing to see if a shell is
# truly a login shell or an interactive shell so we can determine what
# additional setup we want for such shells, such as setting $ENV and exporting
# it.
# ~/.shrc, source early by ~/.profile, which sources this file, did the tests
# for interactive/login shells
#
if ${sh_is_login}; then
#
# Commands for login shells
#
echo "$0: setup for Korn Shell (${KSH_VERSION})"
set -o ignoreeof
fi
# n.b.: it is expected all ksh-like shells add 'i' to $- if they are interactive
# and do so before sourcing ~/.profile, but we'll use the ~/.shrc tests anyway.
#
if ${sh_is_interactive} || ${sh_is_login}; then
#
# Commands for interactive shells.
#
export FCEDIT="$(whence ed)"
# for emacs timeclock.el
#
if [ ! -f ~/.timelog ] ; then
touch ~/.timelog
fi
# Keep them separated!
#
# NOTE: many implementations have HISTFILE inter-locking bugs amongst
# themselves! Some only read on startup and write on exit.
#
case "${KSH_VERSION}" in
*PD*KSH*)
case ${SHELL} in
*oksh)
# xxx OpenBSD is special!
KSHTYPE=oksh
;;
*)
KSHTYPE=pdksh
;;
esac
;;
*MIRBSD*)
KSHTYPE=mksh
;;
Version*)
KSHTYPE=attksh # defaults to ~/.sh_history!
;;
*)
KSHTYPE=ksh
;;
esac
HISTFILE=${HOME}/.${KSHTYPE}_history
HISTSIZE=2000
export ENVFILE=${HOME}/.kshrc
fi
export FPATH=$HOME/lib/ksh
# Setup for global $ENV
#
if [ -n "${ENVFILE}" -a -r "${ENVFILE}" ] ; then
#
# This magic expression prevents a non-interactive Ksh
# instance from ever even trying to open and read any file.
#
# The original Korn & Bolsky book [1989] suggests....
#
#export ENV='${ENVFILE[(_$-=0)+(_=1)-_${-%%*i*}]}'
#
# However the expression below is better as it handles the
# empty $- case, which the one above fails on (0+1-1 = 0).
#
# According to a post by Jon H. LaBadie to comp.unix.questions
# on 1989/12/06 (with the Message-ID <826@jonlab.UUCP>) the
# one below is what David Korn actually suggested to him. I'm
# reasonably certain I read a post by Korn suggesting this
# expression as well, but I may simply have read Jon's post.
#
# It does work with almost all of the versions of Ksh I've run across,
# including ksh-85 and the one AIX-3.2, the most recent ksh93, as well
# as pdksh, but not MKsh since mksh-R56c (main.c:1.344:2017-10-13). As
# of mksh-R59 2020/05/16 it only seems to process $ENV for interactive
# shells anyway.
#
# Note that since ksh-93 (as of 12/28/93) $ENV is only processed for
# interactive shell invocations, making this trick now redundant for
# AT&T Ksh.
#
export ENV='${ENVFILE[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'
# The following explanation is derived from a Usenet post by
# David Young to comp.sys.apollo (of all places) 1992/10/21:
#
# The idea behind this scheme is to set up an array (ENVFILE)
# whose first element is the file we want executed at startup,
# and whose second element is empty. Note that any string
# variable can be accessed as an array with one element in the
# first (zero) position and any attempt to access other
# elements will result in an empty value.
#
# $ export ENVFILE='~/.envfile'
# $ echo ${ENVFILE[0]}
# ~/.envfile
# $ echo ${ENVFILE[1]}
#
# $
#
# So, we want to expand $ENV, when it is referenced by the
# shell during startup, to ENVFILE[0] for interactive shells
# and to ENVFILE[1] for non-interactive shells. To do this we
# need an expression that will evaluate to "0" for an
# interactive shell but which will evaluate to "1" for a
# non-interactive shell.
#
# Keep in mind the two environment variables ENV and PS1 are
# special in Ksh insofar as they get evaluated when they are
# used! So if we assign the value of ENV using single quotes
# as a fixed string it will only be evaluated when it is used
# by the shell.
#
# Also remember that arithmetic expressions are evaluated
# inside array index references (i.e. inside the square
# brackets), but only after '$' variable refs are expanded.
#
# The flags variable ($-) is the key to forming our magic
# expression. If the shell is interactive, the flags will
# contain an 'i'. The expression used as the index consists
# of three parts that are combined using normal arithmetic
# operators to form the final index value:
#
# (_$-=1) + (_=0) - (_$- != _${-%%*i*})
#
# (_$-=1) This creates a variable named "_BLAH", where
# "BLAH" is the value of the variable "$-", and
# assigns it the value "1". The expression has
# the result "1" as well.
#
# (_=0) This creates a variable named "_" and assigns
# it the value "0". The expression has the result
# "0" as well.
#
# (_$- != _${-%%*i*}) This compares the value of _BLAH with
# the value of another variable that
# will either have the same name (and
# thus the same value), or the name "_".
#
# For an interactive shell these variables will be guaranteed
# to have different names, i.e. the first variable will have
# the name of the variable created in the first expression
# ("_BLAH") and the second will have name of the variable
# created in the second expression ("_"), respectively. Since
# the first variable has been assigned the value "1" and the
# second one has the value "0", result of this last expression
# will always be 1 for any interactive shell.
#
# For a non-interactive shell this last expression will have
# the value 0 because both parameters will have the same name
# (there was no "i" to match when forming the name of the
# second var, regardless of whether $- is empty or not).
#
# So for an interactive shell, e.g. when $- is "i", the index is:
#
# (_i=1) + (_=0) - (_i != _)
# 1 + 0 - (1 != 0)
# i.e. 0, thus $ENV[0], for the first and only element
#
# and for a non-interactive shell (e.g. $i is "BLAH") the index is:
#
# (_BLAH=1) + (_=0) - (_BLAH != _BLAH)
# 1 + 0 - (0 != 0)
# i.e. 1, thus $ENV[1], for the second, empty, element
#
# or for a non-interactive shell where $i is "", the index is:
#
# (_=1) + (_=0) - (_ != _)
# 1 + 0 - (0 != 0)
# i.e. 1, thus $ENV[1], for the second, empty, element
#
# Remember class, the test is tomorrow!
fi
if [ -z "$LOGNAME" ] ; then
# some systems won't allow this...
export LOGNAME=${HOME##*/}
fi
# Note: this depends on, and requires, the "cd" at the end of ~/.profile
#
export PWD=$HOME
export OLDPWD=$HOME