[Home] Zsh logo

c2z

Zsh Wizard

Download c2z Return to Examples
  1. #! /bin/sh
  2. #
  3. # c2z - environment conversion tool
  4. # Contributed by Bart Schaefer
  5. # (Tweaked a bit by Paul Falstad)
  6. # (Tweaked again by Bart Schaefer)
  7. #
  8. # This is a quick script to convert csh aliases to zsh aliases/functions.
  9. # It also converts the csh environment and local variables to zsh. c2z
  10. # uses the csh to parse its own dot-files, then processes csh output to
  11. # convert the csh settings to zsh.
  12. #
  13. # When run as a zsh function, c2z runs csh as if it were an interactive
  14. # shell whenever the parent zsh is interactive. When run as a shell
  15. # script, the -i switch can be used to force this behavior.
  16. #
  17. # The -l (login) switch causes csh to run as if it were a login shell.
  18. # This is done "properly" if c2z is used as a zsh function, otherwise
  19. # it's faked by explicitly sourcing .login. Use with caution if your
  20. # .login initializes an X server or does other one-time-only startup
  21. # procedures.
  22. #
  23. # usage:
  24. # c2z [-i | -l | filename]
  25. #
  26. # You can use this script in your .zshrc or .zlogin files to load your
  27. # regular csh environment into zsh; for example, in .zlogin:
  28. #
  29. # . =(c2z -l)
  30. #
  31. # This is not perfect, but it gets most common aliases and variables.
  32. # It's also rather time-consuming to do this every time you log in.
  33. # However, if you're moving from csh to zsh for the first time, this
  34. # can get you started with a familiar environment right away.
  35. #
  36. # In case your mailer eats tabs, $T is set to expand to a tab.
  37. #
  38. T="`echo x | tr x '\011'`"
  39. # If we're zsh, we can run "- csh" to get the complete environment.
  40. #
  41. MINUS=""
  42. LOADFILE=""
  43. INTERACT=""
  44. CSH=csh
  45. case "$ZSH_NAME$ZSH_VERSION$VERSION" in
  46. zsh*)
  47. case $1 in
  48. -l*) MINUS="-" ;;
  49. -i*) INTERACT="-i" ;;
  50. *) LOADFILE="source $1" CSH="csh -f";;
  51. esac
  52. if [[ -o INTERACTIVE ]]; then INTERACT="-i"; fi
  53. setopt nobanghist
  54. ;;
  55. *)
  56. case $1 in
  57. -l*) LOADFILE="source ~/.login" ;;
  58. -i*) INTERACT="-i" ;;
  59. *) LOADFILE="source $1" CSH="csh -f";;
  60. esac
  61. ;;
  62. esac
  63. ( eval $MINUS $CSH $INTERACT ) <<EOF 2>&1 >/dev/null
  64. $LOADFILE
  65. alias >! /tmp/cz$$.a
  66. setenv >! /tmp/cz$$.e
  67. set >! /tmp/cz$$.v
  68. EOF
  69. # save stdin
  70. exec 9<&0
  71. # First convert aliases
  72. exec < /tmp/cz$$.a
  73. # Taken straight from ctoz except for $T and "alias --"
  74. sed -e 's/'"$T"'(\(.*\))/'"$T"'\1/' >/tmp/cz$$.1
  75. grep ! /tmp/cz$$.1 >/tmp/cz$$.2
  76. grep -v ! /tmp/cz$$.1 >/tmp/cz$$.3
  77. sed -e "s/'/'"\\\\"''"/g \
  78. -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/alias -- \1='"'\2'/" \
  79. /tmp/cz$$.3
  80. sed -e 's/>\(&*\)!/>\1|/g' \
  81. -e 's/!\*:q/"$@"/g' \
  82. -e 's/\(![:#]*[^'"$T"']*\):q/"\1"/g' \
  83. -e 's/!\([-0-9][0-9]*\)\(:x\)*/$\2(fc -nl \1)/g' \
  84. -e 's/\$:x(fc/$=(fc/g' \
  85. -e 's/![:#]*\([^'"$T"']\)/$==\1/g' \
  86. -e 's/\$=\(=[^'"$T"']*\):x/$\1/g' \
  87. -e 's/\$cwd/$PWD/' \
  88. -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/\1 () { \2 }/' \
  89. /tmp/cz$$.2
  90. # Next, convert environment variables
  91. exec < /tmp/cz$$.e
  92. # Would be nice to deal with embedded newlines, e.g. in TERMCAP, but ...
  93. sed -e '/^SHLVL/d' \
  94. -e '/^PWD/d' \
  95. -e '/^_=/d' \
  96. -e "s/'/'"\\\\"''"/g \
  97. -e "s/^\([A-Za-z0-9_]*=\)/export \1'/" \
  98. -e "s/$/'/"
  99. # Finally, convert local variables
  100. exec < /tmp/cz$$.v
  101. sed -e 's/'"$T"'/=/' \
  102. -e "s/'/'"\\\\"''"/g \
  103. -e '/^[A-Za-z0-9_]*=[^(]/{
  104. s/=/='"'/"'
  105. s/$/'"'/"'
  106. }' |
  107. sed -e '/^argv=/d' -e '/^cwd=/d' -e '/^filec=/d' -e '/^status=/d' \
  108. -e '/^autolist=/s/.*/setopt autolist/' \
  109. -e '/^correct=all/s//setopt correctall/' \
  110. -e '/^correct=/s//setopt correct/' \
  111. -e '/^histchars=/s//HISTCHARS=/' \
  112. -e '/^history=/s//HISTSIZE=/' \
  113. -e '/^home=/s//HOME=/' \
  114. -e '/^ignoreeof=/s/.*/setopt ignoreeof/' \
  115. -e '/^noclobber=/s/.*/setopt noclobber/' \
  116. -e '/^notify=/d' \
  117. -e '/^prompt=/s/!/%h/' \
  118. -e 's/^prompt/PROMPT/' \
  119. -e '/^showdots=/s/.*/setopt globdots/' \
  120. -e '/^savehist=/s//HISTFILE=\~\/.zhistory SAVEHIST=/' \
  121. -e '/^who=/s//WATCHFMT=/'
  122. exec 0<&9
  123. rm /tmp/cz$$.?
  124. exit