Home | History | Annotate | Line # | Download | only in dist
      1 XCOMM!SHELL_CMD
      2 
      3 XCOMM
      4 XCOMM This is just a sample implementation of a slightly less primitive
      5 XCOMM interface than xinit.  It looks for XINITRC and XSERVERRC environment
      6 XCOMM variables, then user .xinitrc and .xserverrc files, and then system
      7 XCOMM xinitrc and xserverrc files, else lets xinit choose its default.
      8 XCOMM The system xinitrc should probably do things like check for
      9 XCOMM .Xresources files and merge them in, start up a window manager, and
     10 XCOMM pop a clock and several xterms.
     11 XCOMM
     12 XCOMM Site administrators are STRONGLY urged to write nicer versions.
     13 XCOMM
     14 
     15 xinitdir=XINITDIR
     16 xterm=XTERM
     17 xserver=XSERVER
     18 xinit=XINIT
     19 bundle_id_prefix=BUNDLE_ID_PREFIX
     20 xauth=XAUTH
     21 bindir=__bindir__
     22 libexecdir=__libexecdir__
     23 # XXX using $mk_cookie here expands bad.
     24 # mk_cookie=MK_COOKIE
     25 has_cookie_maker=HAS_COOKIE_MAKER
     26 
     27 unset SESSION_MANAGER
     28 
     29 if [ "$(uname -s)" = "Darwin" ] ; then
     30 
     31     XCOMM Check for /usr/bin/X11 and BINDIR in the path, if not add them.
     32     XCOMM This allows startx to be placed in a place like /usr/bin or /usr/local/bin
     33     XCOMM and people may use X without changing their PATH.
     34     XCOMM Note that we put our own bin directory at the front of the path, and
     35     XCOMM the standard system path at the back, since if you are using the Xorg
     36     XCOMM server there's a pretty good chance you want to bias the Xorg clients
     37     XCOMM over the old system's clients.
     38 
     39     case $PATH in
     40         *:$bindir | *:$bindir:* | $bindir:*) ;;
     41         *) PATH=$bindir:$PATH ;;
     42     esac
     43 
     44     XCOMM Now the "old" compiled path
     45     oldbindir=/usr/X11R6/bin
     46 
     47     if [ -d "$oldbindir" ] ; then
     48         case $PATH in
     49             *:$oldbindir | *:$oldbindir:* | $oldbindir:*) ;;
     50             *) PATH=$PATH:$oldbindir ;;
     51         esac
     52     fi
     53 
     54     XCOMM Bourne shell does not automatically export modified environment variables
     55     XCOMM so export the new PATH just in case the user changes the shell
     56     export PATH
     57 fi
     58 
     59 userclientrc=$HOME/.xinitrc
     60 [ -f "${XINITRC}" ] && userclientrc="${XINITRC}"
     61 sysclientrc=XINITDIR/xinitrc
     62 
     63 userserverrc=$HOME/.xserverrc
     64 [ -f "${XSERVERRC}" ] && userserverrc="${XSERVERRC}"
     65 sysserverrc=$xinitdir/xserverrc
     66 defaultclient=$xterm
     67 defaultserver=$xserver
     68 defaultclientargs=""
     69 defaultserverargs="-noretro"
     70 defaultdisplay=""
     71 clientargs=""
     72 serverargs=""
     73 vtarg=""
     74 
     75 
     76 if [ "$(uname -s)" = "Darwin" ] ; then
     77 
     78     if [ "$X11_PREFS_DOMAIN" = "" ] ; then
     79         export X11_PREFS_DOMAIN=$bundle_id_prefix".X11"
     80     fi
     81 
     82     XCOMM Initialize defaults (this will cut down on "safe" error messages)
     83     if ! defaults read $X11_PREFS_DOMAIN cache_fonts > /dev/null 2>&1 ; then
     84         defaults write $X11_PREFS_DOMAIN cache_fonts -bool true
     85     fi
     86 
     87     if ! defaults read $X11_PREFS_DOMAIN no_auth > /dev/null 2>&1 ; then
     88         defaults write $X11_PREFS_DOMAIN no_auth -bool false
     89     fi
     90 
     91     if ! defaults read $X11_PREFS_DOMAIN nolisten_tcp > /dev/null 2>&1 ; then
     92         defaults write $X11_PREFS_DOMAIN nolisten_tcp -bool true
     93     fi
     94 
     95     if ! defaults read $X11_PREFS_DOMAIN enable_iglx > /dev/null 2>&1 ; then
     96         defaults write $X11_PREFS_DOMAIN enable_iglx -bool false
     97     fi
     98 
     99     XCOMM First, start caching fonts
    100     if [ "$(defaults read $X11_PREFS_DOMAIN cache_fonts)" = 1 ] ; then
    101         if [ -x $bindir/font_cache ] ; then
    102             $bindir/font_cache
    103         elif [ -x $bindir/font_cache.sh ] ; then
    104             $bindir/font_cache.sh
    105         elif [ -x $bindir/fc-cache ] ; then
    106             $bindir/fc-cache
    107         fi
    108     fi
    109 
    110     if [ -x $libexecdir/privileged_startx ] ; then
    111 	XCOMM Don't push this into the background because it can cause
    112 	XCOMM a race to create /tmp/.X11-unix
    113 	$libexecdir/privileged_startx
    114     fi
    115 
    116     if [ "$(defaults read $X11_PREFS_DOMAIN no_auth)" = 0 ] ; then
    117         enable_xauth=1
    118     else
    119         enable_xauth=0
    120     fi
    121 
    122     if [ "$(defaults read $X11_PREFS_DOMAIN nolisten_tcp)" = 1 ] ; then
    123         defaultserverargs="$defaultserverargs -nolisten tcp"
    124     else
    125         defaultserverargs="$defaultserverargs -listen tcp"
    126     fi
    127 
    128     if [ "$(defaults read $X11_PREFS_DOMAIN enable_iglx)" = 1 ] ; then
    129         defaultserverargs="$defaultserverargs +iglx +extension GLX"
    130     else
    131         defaultserverargs="$defaultserverargs -iglx"
    132     fi
    133 
    134     XCOMM The second check is the real one.  The first is to hopefully avoid
    135     XCOMM needless syslog spamming.
    136     if defaults read $X11_PREFS_DOMAIN 2> /dev/null | grep -q 'dpi' && defaults read $X11_PREFS_DOMAIN dpi > /dev/null 2>&1 ; then
    137         defaultserverargs="$defaultserverargs -dpi $(defaults read $X11_PREFS_DOMAIN dpi)"
    138     fi
    139 
    140 else
    141     enable_xauth=1
    142 fi
    143 
    144 XCOMM Automatically determine an unused $DISPLAY
    145 d=0
    146 while true ; do
    147     [ -e "/tmp/.X$d-lock" -o -S "/tmp/.X11-unix/X$d" ] || break
    148     d=$(($d + 1))
    149 done
    150 defaultdisplay=":$d"
    151 unset d
    152 
    153 whoseargs="client"
    154 while [ "$1" != "" ]; do
    155     case "$1" in
    156     XCOMM '' required to prevent cpp from treating "/*" as a C comment.
    157     /''*|\./''*)
    158 	if [ "$whoseargs" = "client" ]; then
    159 	    if [ "$client" = "" ] && [ "$clientargs" = "" ]; then
    160 		client="$1"
    161 	    else
    162 		clientargs="$clientargs $1"
    163 	    fi
    164 	else
    165 	    if [ "$server" = "" ] && [ "$serverargs" = "" ]; then
    166 		server="$1"
    167 	    else
    168 		serverargs="$serverargs $1"
    169 	    fi
    170 	fi
    171 	;;
    172     --)
    173 	whoseargs="server"
    174 	;;
    175     *)
    176 	if [ "$whoseargs" = "client" ]; then
    177 	    clientargs="$clientargs $1"
    178 	else
    179 	    XCOMM display must be the FIRST server argument
    180 	    if [ "$serverargs" = "" ] && @@
    181 		 expr "$1" : ':[0-9][0-9]*$' > /dev/null 2>&1; then
    182 		display="$1"
    183 	    else
    184 		serverargs="$serverargs $1"
    185 	    fi
    186 	fi
    187 	;;
    188     esac
    189     shift
    190 done
    191 
    192 XCOMM process client arguments
    193 if [ "$client" = "" ]; then
    194     client=$defaultclient
    195 
    196     XCOMM For compatibility reasons, only use startxrc if there were no client command line arguments
    197     if [ "$clientargs" = "" ]; then
    198         if [ -f "$userclientrc" ]; then
    199             client=$userclientrc
    200         elif [ -f "$sysclientrc" ]; then
    201             client=$sysclientrc
    202         fi
    203     fi
    204 fi
    205 
    206 XCOMM if no client arguments, use defaults
    207 if [ "$clientargs" = "" ]; then
    208     clientargs=$defaultclientargs
    209 fi
    210 
    211 XCOMM process server arguments
    212 if [ "$server" = "" ]; then
    213     server=$defaultserver
    214 
    215 if [ "$(uname -s)" = "Linux" ] ; then
    216     XCOMM When starting the defaultserver start X on the current tty to avoid
    217     XCOMM the startx session being seen as inactive:
    218     XCOMM "https://bugzilla.redhat.com/show_bug.cgi?id=806491"
    219     tty=$(tty)
    220     if expr "$tty" : '/dev/tty[0-9][0-9]*$' > /dev/null; then
    221         tty_num=${tty#/dev/tty}
    222         vtarg="vt$tty_num -keeptty"
    223     fi
    224 fi
    225 
    226     XCOMM For compatibility reasons, only use xserverrc if there were no server command line arguments
    227     if [ "$serverargs" = "" ] && [ "$display" = "" ]; then
    228 	if [ -f "$userserverrc" ]; then
    229 	    server=$userserverrc
    230 	elif [ -f "$sysserverrc" ]; then
    231 	    server=$sysserverrc
    232 	fi
    233     fi
    234 fi
    235 
    236 XCOMM if no server arguments, use defaults
    237 if [ "$serverargs" = "" ]; then
    238     serverargs=$defaultserverargs
    239 fi
    240 
    241 XCOMM if no vt is specified add vtarg (which may be empty)
    242 have_vtarg="no"
    243 for i in $serverargs; do
    244     if expr "$i" : 'vt[0-9][0-9]*$' > /dev/null; then
    245         have_vtarg="yes"
    246     fi
    247 done
    248 if [ "$have_vtarg" = "no" ]; then
    249     serverargs="$serverargs $vtarg"
    250 fi
    251 
    252 XCOMM if no display, use default
    253 if [ "$display" = "" ]; then
    254     display=$defaultdisplay
    255 fi
    256 
    257 if [ "$enable_xauth" = 1 ] ; then
    258     if [ "$XAUTHORITY" = "" ]; then
    259         XAUTHORITY=$HOME/.Xauthority
    260         export XAUTHORITY
    261     fi
    262 
    263     removelist=
    264 
    265     XCOMM set up default Xauth info for this machine
    266     hostname="$(uname -n)"
    267 
    268     authdisplay=${display:-:0}
    269     if [ -n "$has_cookie_maker" ] && [ -n 'MK_COOKIE' ] ; then
    270         mcookie=`MK_COOKIE`
    271     else
    272         if [ -r /dev/urandom ]; then
    273             mcookie=$(dd if=dev/urandom bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\")
    274         else
    275             mcookie=$(dd if=/dev/random bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\")
    276         fi
    277     fi
    278     if [ "$mcookie" = "" ]; then
    279         echo "Couldn't create cookie"
    280         exit 1
    281     fi
    282     dummy=0
    283 
    284     XCOMM create a file with auth information for the server. ':0' is a dummy.
    285     xserverauthfile=$HOME/.serverauth.$$
    286     trap "rm -f '$xserverauthfile'" HUP INT QUIT ILL TRAP BUS TERM
    287     xauth -q -f "$xserverauthfile" << EOF
    288 add :$dummy . $mcookie
    289 EOF
    290 
    291     case "$(uname -s)" in
    292     CYGWIN*|Darwin)
    293         xserverauthfilequoted=$(echo ${xserverauthfile} | sed "s/'/'\\\\''/g")
    294         serverargs=${serverargs}" -auth '"${xserverauthfilequoted}"'"
    295         ;;
    296     *)
    297         serverargs=${serverargs}" -auth "${xserverauthfile}
    298         ;;
    299     esac
    300 
    301     XCOMM now add the same credentials to the client authority file
    302     XCOMM if '$displayname' already exists do not overwrite it as another
    303     XCOMM server may need it. Add them to the '$xserverauthfile' instead.
    304     for displayname in $authdisplay $hostname$authdisplay; do
    305         authcookie=$(xauth list "$displayname" @@
    306         | sed -n 's/.*'"$displayname"'[[:space:]*].*[[:space:]*]//p' 2>/dev/null);
    307         if [ "z${authcookie}" = "z" ] ; then
    308             $xauth -q << EOF
    309 add $displayname . $mcookie
    310 EOF
    311         removelist="$displayname $removelist"
    312         else
    313             dummy=$(($dummy+1));
    314             $xauth -q -f "$xserverauthfile" << EOF
    315 add :$dummy . $authcookie
    316 EOF
    317         fi
    318     done
    319 fi
    320 
    321 case "$(uname -s)" in
    322 CYGWIN_NT*|Darwin)
    323     eval $xinit \"$client\" $clientargs -- \"$server\" $display $serverargs
    324     ;;
    325 *)
    326     $xinit "$client" $clientargs -- "$server" $display $serverargs
    327     ;;
    328 esac
    329 retval=$?
    330 
    331 if [ "$enable_xauth" = 1 ] ; then
    332     if [ "$removelist" != "" ]; then
    333         $xauth remove $removelist
    334     fi
    335     if [ "$xserverauthfile" != "" ]; then
    336         rm -f "$xserverauthfile"
    337     fi
    338 fi
    339 
    340 XCOMM various machines need special cleaning up
    341 if [ "$(uname -s)" = "Linux" ]; then
    342     if command -v deallocvt > /dev/null 2>&1; then
    343         deallocvt
    344     fi
    345 fi
    346 
    347 if [ "$(uname -s)" = "SunOS" ]; then
    348     kbd_mode -a
    349 fi
    350 
    351 exit $retval
    352