Hamilton LaboratoriesHamilton C shell 2012User guideSamples


Oregon Coast

Previous | Next

#  Convert Bourne or Korn shell scripts to Hamilton C shell scripts
#  Copyright (c) 1992-2012 by Hamilton Laboratories.  All rights reserved.

#  Usage:   sh2csh [ files ]
#  Translate an sh script to C shell syntax, writing to stdout.  If
#  multiple files are specified, they're concatenated.
#  This is a relatively simple-minded translation using a series of sed
#  filters to rewrite if statements, procedures, case statements, loops,
#  miscellaneous differences, expressions and quotes.
#  Each stage is using sed to fixup a specific type of language difference
#  between sh and csh.  Order is important and the stages must be connected
#  thru pipes rather than simply concatenating the scripts.  But any individual
#  stage can be run independently for testing or just to see what it does.
#  The result will (hopefully) be fairly good if the input is in clear,
#  well-formatted style, but with these limitations:
#  1. The output will depend on the C shell being run with the escape symbol
#     set as follows:
#        set escapesym = \       # Use the Unix-style \, not the Windows
#                                #  default ^, as the escape symbol.
#  2. break statements will have to be manually rewritten, since sh
#     interprets break to mean break out of loops only, not case
#     statements.  Also, break n means break out of n levels, even
#     breaking out of loops entered in calling procedures.  The sed
#     scripts will turn break into FixBreak in the output.
#  3. Under the C shell, variables are either shell variable or
#     environment variables from the moment they're created.
#  4. The scripts do a pretty good job editing most things, but some
#     things just don't go thru very well and will require hand tuning.
#     E.g., backquoted expr expressions with embedded escaped quotes
#     are garbled and escaped newlines may cause trouble.

local seddir, stageone

set seddir = $scriptname:h\sh2csh
if (! -e $seddir || ! -D $seddir) then
   echo -2 "sh2csh:  Couldn't find the sed scripts."
   exit 1
if ($#argv) then
   set stageone = expr.sed $argv:gf
   set stageone = expr.sed

cd +c $seddir
sed -f $stageone | sed -f if.sed | sed -f proc.sed | sed -f case.sed | ^
   sed -f loop.sed | sed -f misc.sed | sed -f expr2.sed | sed -f quotes.sed

Previous | Next