#!/usr/bin/perl -dw
# Given a list of file names ending with .c, this script converts them to .b
# files via a series of heuristics.
#
# Copyright (c) 1992 by John Chambers ...{bu.edu,harvard,mit-eddie}minya.uucp!jc
#
$OB = '{';
$CB = '}';
foreach $file ( @ARGV ) {
	chop($base = `basename $file .c`);
	open(IFILE, $file) 
		|| die "Can't read $file: $!\n";
	print "Reading `$file'\n";
	open(OFILE,"> $base.b") 
		|| die "Can't write $base: $!\n";
	print "Writing `$base.b'\n";
	select OFILE;
	$| = 1;
	select STDOUT;
	while  ($line = <IFILE>) {
		$lines ++;
		print "Line $lines: $line";
		if ($incom) {		# Are we inside a comment?
			print OFILE $line;	# If so, just copy the line.
			$incom = 0		# Comment terminator?
				if ($line =~ /.*\*\//);
			next;
		}
		if ($line =~ /\s*\/\*/) {	# Comment initiator?
			print OFILE $line;
			$incom = 1;		# Note start of comment.
			$incom = 0		# Comment immediately terminated?
				if ($line =~ /.*\*\//);
			next;
		}
		if ($infct) {	# Are we inside a function?
			if ($line =~ m"^}") {	# End of function?
				if ($lines - $fexit > 1) {
					print OFILE ":1done:";
					print OFILE ":1fail:";
					print OFILE ":1	Fexit;";
					$fexit = $lines;
				}
				$infct = 0;
			}
		} else {
			if ($line =~ m"[A-Za-z_] *\(") {
				$fctnam = $1;
				$infct = 1;
				$fexit = 0;
				print "Fct $fctnam()\n";
			}
		}
		if ($line =~ /^\s*([DHPVS])([0-9])\(/) {
			$mark = 1;
		}
		if ($mark) {
			print OFILE ':',$2;
		}
		print "Unmatched.\n";
		print OFILE $line;
	}
	close(OFILE);
	close(IFILE);
}

sub comment {
	print "Comment ...\n";
}
