8e2b05cf99b27bf1a5352e8be905297d71918d9b
[platform/upstream/bash.git] / examples / misc / cshtobash
1 #! /bin/bash
2 #
3 # cshtobash - convert csh aliases, environment variables, and variables to
4 #             bash equivalents
5 #
6 # usage: cshtobash [filename]
7 #
8 # If filename is given, that file is sourced.  Note that csh always
9 # sources .cshrc.  To recreate your csh login environment, run
10 # `cshtobash ~/.login'.
11 #
12 # Inspired by (and some borrowed from) a similar program distributed with
13 # zsh-3.0.
14 #
15 # Chet Ramey
16 # chet@po.cwru.edu
17 #
18 trap 'rm -f /tmp/cb$$.? cshout cshin' 0 1 2 3 6 15
19
20 T=$'\t'
21
22 SOURCE="${1:+source $1}"
23
24 cat << EOF >cshin
25 $SOURCE
26 alias >! /tmp/cb$$.a
27 setenv >! /tmp/cb$$.e
28 set >! /tmp/cb$$.v
29 EOF
30
31 # give csh a minimal environment, similar to what login would provide
32 /usr/bin/env - USER=$USER HOME=$HOME PATH=/usr/bin:/bin:/usr/ucb:. TERM=$TERM SHELL=$SHELL /bin/csh -i < ./cshin > cshout 2>&1
33
34 # First convert aliases
35
36 cat << \EOF >/tmp/cb$$.1
37 mkalias ()
38 {
39         case $2 in
40         '')     echo alias ${1}="''" ;;
41         *[#\!]*)
42                 comm=$(echo $2 | sed  's/\!\*/"$\@"/g
43                                        s/\!:\([1-9]\)/"$\1"/g
44                                        s/#/\#/g')
45                 echo $1 \(\) "{" command "$comm"  "; }"
46                 ;;
47         *)      echo alias ${1}=\'$(echo "${2}" | sed "s:':'\\\\'':")\' ;;
48         esac
49 }
50 EOF
51
52 sed "s/^\([a-zA-Z0-9_]*\)$T\(.*\)$/mkalias \1 '\2'/" < /tmp/cb$$.a >>/tmp/cb$$.1
53
54 echo '# csh aliases'
55 echo
56
57 $BASH /tmp/cb$$.1 | sed -e 's/\$cwd/\$PWD/g' \
58                    -e 's/\$term/\$TERM/g' \
59                    -e 's/\$home/\$HOME/g' \
60                    -e 's/\$user/\$USER/g' \
61                    -e 's/\$prompt/\$PS1/g'
62
63 # Next, convert environment variables
64 echo
65 echo '# csh environment variables'
66 echo
67
68 # Would be nice to deal with embedded newlines, e.g. in TERMCAP, but ...
69 sed -e '/^SHLVL/d' \
70     -e '/^PWD/d' \
71     -e "s/'/'"\\\\"''"/g \
72     -e "s/^\([A-Za-z0-9_]*=\)/export \1'/" \
73     -e "s/$/'/" < /tmp/cb$$.e
74
75 # Finally, convert local variables
76 echo
77 echo '# csh variables'
78 echo
79
80 sed -e 's/'"$T"'/=/' \
81     -e "s/'/'"\\\\"''"/g \
82     -e '/^[A-Za-z0-9_]*=[^(]/{
83         s/=/='"'/"'
84         s/$/'"'/"'
85         }' < /tmp/cb$$.v |
86 sed -e '/^argv=/d' -e '/^cwd=/d' -e '/^filec=/d' -e '/^status=/d' \
87          -e '/^verbose=/d' \
88          -e '/^term=/d' \
89          -e '/^home=/d' \
90          -e '/^path=/d' \
91          -e '/^user=/d' \
92          -e '/^shell=/d' \
93          -e '/^cdpath=/d' \
94          -e '/^mail=/d' \
95          -e '/^home=/s//HOME=/' \
96          -e '/^prompt=/s//PS1=/' \
97          -e '/^histfile=/s//HISTFILE=/' \
98          -e '/^history=/s//HISTSIZE=/' \
99          -e '/^savehist=$/s//HISTFILESIZE=${HISTSIZE-500}/' \
100          -e '/^savehist=/s//HISTFILESIZE=/' \
101          -e '/^ignoreeof=$/s/^.*$/set -o ignoreeof # ignoreeof/' \
102          -e '/^ignoreeof=/s//IGNOREEOF=/' \
103          -e '/^noclobber=/s/^.*$/set -C # noclobber/' \
104          -e '/^notify=/s/^.*$/set -b # notify/' \
105          -e '/^noglob=/s/^.*$/set -f # noglob/' \
106
107
108 # now some special csh variables converted to bash equivalents
109 echo
110 echo '# special csh variables converted to bash equivalents'
111 echo
112
113 sed -e 's/'"$T"'/=/' < /tmp/cb$$.v |
114 grep "^cdpath=" |
115 sed 's/(//
116      s/ /:/g
117      s/)//
118      s/cdpath=/CDPATH=/'
119
120
121 sed -e 's/'"$T"'/=/' < /tmp/cb$$.v |
122 grep "^mail=" |
123 sed 's/(//
124      s/ /:/g
125      s/)//
126      s/mail=/MAILPATH=/' |
127 sed -e 's/MAILPATH=\([0-9][0-9][^:]*\)$/MAILCHECK=\1/' \
128     -e 's/MAILPATH=\([0-9][0-9][^:]*\):\(.*\)/MAILCHECK=\1 MAILPATH=\2/'
129
130 exit 0