xref: /titanic_41/usr/src/cmd/nsadmin/ksh.kshrc (revision 7c2fbfb345896881c631598ee3852ce9ce33fb07)
1da2e3ebdSchin#
2da2e3ebdSchin# CDDL HEADER START
3da2e3ebdSchin#
4da2e3ebdSchin# The contents of this file are subject to the terms of the
5da2e3ebdSchin# Common Development and Distribution License (the "License").
6da2e3ebdSchin# You may not use this file except in compliance with the License.
7da2e3ebdSchin#
8da2e3ebdSchin# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da2e3ebdSchin# or http://www.opensolaris.org/os/licensing.
10da2e3ebdSchin# See the License for the specific language governing permissions
11da2e3ebdSchin# and limitations under the License.
12da2e3ebdSchin#
13da2e3ebdSchin# When distributing Covered Code, include this CDDL HEADER in each
14da2e3ebdSchin# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da2e3ebdSchin# If applicable, add the following below this CDDL HEADER, with the
16da2e3ebdSchin# fields enclosed by brackets "[]" replaced with your own identifying
17da2e3ebdSchin# information: Portions Copyright [yyyy] [name of copyright owner]
18da2e3ebdSchin#
19da2e3ebdSchin# CDDL HEADER END
20da2e3ebdSchin#
21da2e3ebdSchin
22da2e3ebdSchin#
23*7c2fbfb3SApril Chin# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24da2e3ebdSchin# Use is subject to license terms.
25da2e3ebdSchin#
26da2e3ebdSchin
27da2e3ebdSchin#
28da2e3ebdSchin# This file is sourced by interactive ksh93 shells before ${HOME}/.kshrc
29da2e3ebdSchin#
30da2e3ebdSchin
31*7c2fbfb3SApril Chin# Enable "gmacs"+"multiline" editor mode if the user did not set an
32*7c2fbfb3SApril Chin# input mode yet (for example via ${EDITOR}, ${VISUAL} or any
33*7c2fbfb3SApril Chin# "set -o" flag)
34*7c2fbfb3SApril Chinif [[ "$(set +o)" != ~(Er)--(gmacs|emacs|vi)( .*|) ]] ; then
35da2e3ebdSchin	set -o gmacs
36*7c2fbfb3SApril Chin	# enable multiline input mode
37*7c2fbfb3SApril Chin	set -o multiline
38da2e3ebdSchinfi
39da2e3ebdSchin
40*7c2fbfb3SApril Chin# Set a default prompt (<username>@<hostname>:<path><"($|#) ">) if
41*7c2fbfb3SApril Chin# then variable does not exist in the environment.
42*7c2fbfb3SApril Chin#
43*7c2fbfb3SApril Chin# Algorithm:
44*7c2fbfb3SApril Chin# 1. Define "ellipsis", either Unicode #2026 for unicode locales
45*7c2fbfb3SApril Chin# and "..." otherwise
46*7c2fbfb3SApril Chin# ([[ "${LC_ALL}/${LANG}" = ~(Elr)(.*UTF-8/.*|/.*UTF-8) ]]
47*7c2fbfb3SApril Chin# ensures that the pattern matches the leftmost sequence
48*7c2fbfb3SApril Chin# containing *.UTF-8, allowing to match either LC_ALL or
49*7c2fbfb3SApril Chin# LANG when LC_ALL is not set)
50*7c2fbfb3SApril Chin# 2. If PWD is within HOME replace value of HOME with '~'
51*7c2fbfb3SApril Chin# If the PWD is longer than 30 charatcers shorten it to 30 chars
52*7c2fbfb3SApril Chin# print '#' for user "root" and '$' for normal users
53*7c2fbfb3SApril Chin# Notes:
54*7c2fbfb3SApril Chin# - printf "%*s\r%s" COLUMNS "") # is used at the beginning to
55*7c2fbfb3SApril Chin#   work around a bug in the "multiline" handling code which
56*7c2fbfb3SApril Chin#   causes the shell to override its own prompt when the edit
57*7c2fbfb3SApril Chin#   line overflows (this happens if the terminal cursor
58*7c2fbfb3SApril Chin#   position is not 0 when PS1 is printed).
59*7c2fbfb3SApril Chin# - PS1 will initially be empty until either...
60*7c2fbfb3SApril Chin#   a) ... someone sets the variable
61*7c2fbfb3SApril Chin#       or
62*7c2fbfb3SApril Chin#   b) ... the prompt is displayed for the first time (default is
63*7c2fbfb3SApril Chin#     '$ ' for normal users and '# ' for user "root")
64*7c2fbfb3SApril Chin# - The statement below will not work if someone sources /etc/ksh.kshrc
65*7c2fbfb3SApril Chin#   unless PS1 gets "unset" first.
66*7c2fbfb3SApril Chin# - Make sure to use absolute paths (e.g. /usr/bin/hostname) to make
67*7c2fbfb3SApril Chin#   sure PS1 works in cases where PATH does not contain /usr/bin/
68*7c2fbfb3SApril Chinif [[ "$(set)" != ~(E)PS1= && "${PS1}" == '' ]] ; then
69*7c2fbfb3SApril Chin	PS1='$(printf "%*s\r%s" COLUMNS "")${LOGNAME}@$(/usr/bin/hostname):$(
70*7c2fbfb3SApril Chin		ellip="$(
71*7c2fbfb3SApril Chin			[[ "${LC_ALL}/${LANG}" == ~(Elr)(.*UTF-8/.*|/.*UTF-8) ]] &&
72*7c2fbfb3SApril Chin				printf "\u[2026]\n" || print "..." )"
73*7c2fbfb3SApril Chin		p="${PWD/~(El)${HOME}/\~}"
74*7c2fbfb3SApril Chin		(( ${#p} > 30 )) &&
75*7c2fbfb3SApril Chin			print -r -n -- "${ellip}${p:${#p}-30:30}" ||
76*7c2fbfb3SApril Chin			print -r -n -- "${p}"
77*7c2fbfb3SApril Chin		[[ "${LOGNAME}" == "root" ]] && print -n "# " || print -n "\$ "
78*7c2fbfb3SApril Chin		)'
79*7c2fbfb3SApril Chinfi
80