about summary refs log tree commit diff
path: root/Misc/c2z
blob: 5341372967facec91bee732206fd828aaa438d5e (plain) (blame)
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
#! /bin/sh
#
# c2z - environment conversion tool
# Contributed by Bart Schaefer
# (Tweaked a bit by Paul Falstad)
# (Tweaked again by Bart Schaefer)
#
# This is a quick script to convert csh aliases to zsh aliases/functions.
# It also converts the csh environment and local variables to zsh.  c2z
# uses the csh to parse its own dot-files, then processes csh output to
# convert the csh settings to zsh.
#
# When run as a zsh fuction, c2z runs csh as if it were an interactive
# shell whenever the parent zsh is interactive.  When run as a shell
# script, the -i switch can be used to force this behavior.
#
# The -l (login) switch causes csh to run as if it were a login shell.
# This is done "properly" if c2z is used as a zsh function, otherwise
# it's faked by explicitly sourcing .login.  Use with caution if your
# .login initializes an X server or does other one-time-only startup
# procedures.
#
# usage:
#	c2z [-i | -l | filename]
#
# You can use this script in your .zshrc or .zlogin files to load your
# regular csh environment into zsh; for example, in .zlogin:
#
#	. =(c2z -l)
#
# This is not perfect, but it gets most common aliases and variables.
# It's also rather time-consuming to do this every time you log in.
# However, if you're moving from csh to zsh for the first time, this
# can get you started with a familiar environment right away.
#
# In case your mailer eats tabs, $T is set to expand to a tab.
#
T="`echo x | tr x '\011'`"

# If we're zsh, we can run "- csh" to get the complete environment.
#
MINUS=""
LOADFILE=""
INTERACT=""
CSH=csh
case "$ZSH_NAME$ZSH_VERSION$VERSION" in
zsh*)
    case $1 in
    -l*) MINUS="-" ;;
    -i*) INTERACT="-i" ;;
    *) LOADFILE="source $1" CSH="csh -f";;
    esac
    if [[ -o INTERACTIVE ]]; then INTERACT="-i"; fi
    setopt nobanghist
    ;;
*)
    case $1 in
    -l*) LOADFILE="source ~/.login" ;;
    -i*) INTERACT="-i" ;;
    *) LOADFILE="source $1" CSH="csh -f";;
    esac
    ;;
esac

( eval $MINUS $CSH $INTERACT ) <<EOF 2>&1 >/dev/null
$LOADFILE
alias >! /tmp/cz$$.a
setenv >! /tmp/cz$$.e
set >! /tmp/cz$$.v
EOF

# save stdin
exec 9<&0

# First convert aliases
exec < /tmp/cz$$.a

# Taken straight from ctoz except for $T and "alias --"
sed -e 's/'"$T"'(\(.*\))/'"$T"'\1/' >/tmp/cz$$.1
grep ! /tmp/cz$$.1 >/tmp/cz$$.2
grep -v ! /tmp/cz$$.1 >/tmp/cz$$.3
sed -e "s/'/'"\\\\"''"/g \
    -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/alias -- \1='"'\2'/" \
    /tmp/cz$$.3
sed -e 's/>\(&*\)!/>\1|/g' \
    -e 's/!\*:q/"$@"/g' \
    -e 's/\(![:#]*[^'"$T"']*\):q/"\1"/g' \
    -e 's/!\([-0-9][0-9]*\)\(:x\)*/$\2(fc -nl \1)/g' \
    -e 's/\$:x(fc/$=(fc/g' \
    -e 's/![:#]*\([^'"$T"']\)/$==\1/g' \
    -e 's/\$=\(=[^'"$T"']*\):x/$\1/g' \
    -e 's/\$cwd/$PWD/' \
    -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/\1 () { \2 }/' \
    /tmp/cz$$.2

# Next, convert environment variables
exec < /tmp/cz$$.e

# Would be nice to deal with embedded newlines, e.g. in TERMCAP, but ...
sed -e '/^SHLVL/d' \
    -e '/^PWD/d' \
    -e '/^_=/d' \
    -e "s/'/'"\\\\"''"/g \
    -e "s/^\([A-Za-z0-9_]*=\)/export \1'/" \
    -e "s/$/'/"

# Finally, convert local variables
exec < /tmp/cz$$.v

sed -e 's/'"$T"'/=/' \
    -e "s/'/'"\\\\"''"/g \
    -e '/^[A-Za-z0-9_]*=[^(]/{
	s/=/='"'/"'
	s/$/'"'/"'
	}' |
sed -e '/^argv=/d' -e '/^cwd=/d' -e '/^filec=/d' -e '/^status=/d' \
	 -e '/^autolist=/s/.*/setopt autolist/' \
	 -e '/^correct=all/s//setopt correctall/' \
	 -e '/^correct=/s//setopt correct/' \
	 -e '/^histchars=/s//HISTCHARS=/' \
	 -e '/^history=/s//HISTSIZE=/' \
	 -e '/^home=/s//HOME=/' \
	 -e '/^ignoreeof=/s/.*/setopt ignoreeof/' \
	 -e '/^noclobber=/s/.*/setopt noclobber/' \
	 -e '/^notify=/d' \
	 -e '/^prompt=/s/!/%h/' \
	 -e 's/^prompt/PROMPT/' \
	 -e '/^showdots=/s/.*/setopt globdots/' \
	 -e '/^savehist=/s//HISTFILE=\~\/.zhistory SAVEHIST=/' \
	 -e '/^who=/s//WATCHFMT=/'


exec 0<&9

rm /tmp/cz$$.?
exit