#!/bin/sh

# @@ This will be an aid for translating file.c to Pascal. It is no
#    generic C to Pascal translator. Its output won't even compile.
#    It is only a basis for a manual translation.
#
# Copyright (C) 2001-2004 Free Software Foundation, Inc.
#
# Author: Frank Heckenbach <frank@pascal.gnu.de>
#
# This file is part of GNU Pascal.
#
# GNU Pascal is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# GNU Pascal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Pascal; see the file COPYING. If not, write to the
# Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

# This should work even with a crippled sed ...
dir="`echo "$0" | sed -e 's,\(.\)/*[^/]*$,\1,'`" || exit 1
sed="`"$dir"/find-sed`" || exit 1

NOID='\([^A-Za-z0-9_]\)'
ID='\([A-Za-z0-9_]\+\)'
PAR='\(\([^()]\|([^()]*)\)*\)'

"$sed" -e "

/{/{x;/./{s/.*//;x;s/ *{ *//;/^$/d;x;};x;s/{/begin/;}
s/}/end;/g;

/^ *#/{
  :l1; /\\\\\$/ { s///; N; b l1; }
  s/{/begin/g;
  s/}/end/g;
  s/^\( *\)#\(.*\)/\1{\$\2}/;
  s/{\\\$include/{.\$include/;
}

s/\/\*/{/g;
s/\*\//}/g;
s/\(^ *\)do$/\1repeat/g;
/if/{
 /if *(\(\([^()]\|(\([^()]\|([^()]*)\)*)\)*\))/{s//if \1 then/;b l2;}
 s/if *(\(.*\))/if \1 then/;
 :l2
}
s/while *(\(.*\));/until not (\1);/
s/while *(\(.*\))/while \1 do/
s/case *//g;
/switch/{s/switch *(\(.*\))/case \1 of/;x;s/^/./;x;}

s/!=/<>/g;
s/ \?! \?/ not /g;
s/==/#=#/g;
s/=/:=/g;
s/#:=#/=/g;
s/(\*\([^)]*\))/(\1)^/g;
s/>:=/>=/g;
s/<:=/<=/g;
/+:=/s/\( *\)\(.*[^ ]\) *+:= *\(.*\);/\1Inc (\2, \3);/g;
/-:=/s/\( *\)\(.*[^ ]\) *-:= *\(.*\);/\1Dec (\2, \3);/g;
s/ \?&& \?/ and /g;
s/ \?||\? \?/ or /g;
s/->/^./g;
s/&/@/g;

/^\(GLOBAL\|static\)/ {
  :l3; /[^)]\$/ { N; b l3; }
  x;s/.*//;x;
  /^static */{h;s///;}
  s/^GLOBAL *(\(.*\))\$/\1/;
  s/^GLOBAL_ATTR *(\(.*\), *\([A-Za-z_0-9,]*\))\$/\1 attribute (\2);/;
  s/ UNUSED//g;
  s/unsigned *int/Cardinal/g;
  s/$NOID""int$NOID/\1Integer\2/g;
  s/^int$NOID/Integer\1/g;
  s/float/ShortReal/g;
  s/long \+double/LongReal/g;
  s/double/Real/g;
  s/ssize_t/SignedSizeType/g;
  s/size_t/SizeType/g;
  s/void *\* */Pointer /g;
  s/DIR *\* */Pointer /g;
  s/const \+\(unsigned \+\)\?char *\* */CString /g;
  s/\(unsigned \+\)\?char *\* */CString /g;
  s/$ID \+$ID\( *[,()]\)/\2: \1\3/g;
  s/$NOID$ID *\* *$ID/\1var \3: \2/g;
  s/void *(\*$ID) *(\(.*\))/procedure \1 (\2)/
  s/const/protected/g;
  s/attribute (protected)/attribute (const)/g;
  s/,/;/g;
  s/$ID *: *void *( *$PAR *)/procedure #\1 (\2); attribute (name = '\1');/g;
  s/$ID *: *$ID *( *$PAR *)/function  #\1 (\3): \2; attribute (name = '\1');/g;
  s/#_p_//g;
  s/#//g;
  s/ *( *void *)//g;
  x;/^$/{x;h;s/^/#/;s/\n/&#/g;p;};s/.*//;x;
  s/attribute.*;//
}

s/\([^']\)_p_/\1/g;
s/if InOutRes then/if InOutRes <> 0 then/g;
" file.c > tmp.tmp

cat << EOF
unit FileC;

interface

EOF

sed -ne '/^#/s///p' tmp.tmp

cat << EOF

implementation

EOF

grep -v '^#' tmp.tmp

cat << EOF

end.
EOF

rm tmp.tmp
