/************************************************************************
 *
 * Compile.ced						Copyright (c) 1989, Peter Cherna
 *
 * ARexx program for CygnusEd Professional.  Scans the current file
 * for embedded "Auto:" commands and executes them.  Permits a
 * C-language source file to be compiled.  Will detect any errors, and
 * launch NextError.ced if any compiler errors are detected.
 *
 * Version 1.30:  May 7, 1989		Release 1.2:  August 29, 1989
 *
 ************************************************************************/


/* Allow CygnusEd to pass status variables */
options results

/* Tell ARexx to talk to CygnusEd */
address 'rexx_ced'

cr = '0A'X

/*	Get path of current file path, and append a slash if needed: */
status 20
path = result
if length(path) > 0 & right(path,1) ~= ':' then path = path || '/'

/*	Get name of current file: */
status 21
sourcefile = result

say cr || 'Scanning' sourcefile
/*	ErrorOffset will be the byte offset into AztecC.err of the next error */
call setclip 'ErrorOffset','0'

if exists('AztecC.err') then address command "delete AztecC.err"

/*	Get number of changes to current file, to see if file has been modified
	since the last save: */
status 18
nchanges = result
plural = ' has'
if nchanges ~= 1 then plural = 's have'
if result > 0 then
DO
	okay2 nchanges 'change' || plural 'been made to' sourcefile || cr'Save before compiling?'
	if result = 1 then Save
END

/*	Chop .extension off filename: */
if lastpos('.',sourcefile) = 0 then
	filename = sourcefile
else
	filename = left(sourcefile,lastpos('.',sourcefile)-1);

/*	Open the source file, and look for "Auto:" commands in first 30 lines: */
if open('source',path || sourcefile) then
DO
	address command 'WBTF'

	maxcheck = 30
	done = 0
	inauto = 0
	DO until (done ~= 0)
		line = readln('source')
		/*	Look for an "Auto:" command to perform: */
		auto = index(line,'Auto:')
		if auto > 0 then
		DO
			inauto = 1
			cmd = substr(line,auto+5)

			find = index(cmd,'<path>')
			DO while find > 0
				cmd = delstr(cmd,find,6)
				cmd = insert(path,cmd,find-1)
				find = index(cmd,'<path>')
			END
			find = index(cmd,'<file>')
			DO while find > 0
				cmd = delstr(cmd,find,6)
				cmd = insert(filename,cmd,find-1)
				find = index(cmd,'<file>')
			END

			/*	Perform the "Auto:" command: */
			options failat 999
			address command cmd
			options failat 10

			/*	If you aren't using WShell, then the only check we can
				do is to see if AztecC.err exists.  If it does, then we
				know cc failed. */
			if exists('AztecC.err') then
				done = 1
		END
		else
		DO
			/*	No "Auto:" command found on this line.  If some "Auto:"
				commands were already found, then we're done */
			if inauto then done = 1
			else
			DO
				maxcheck = maxcheck - 1
				if maxcheck = 0 then done = 1
			END
		END
	END
END

if inauto = 0 then
DO
	say 'No "Auto:" commands found.' || cr
	cedtofront
	okay1 'No "Auto:" commands found.'
	exit
END
else
DO
	if exists('AztecC.err') then
	DO
		say 'Compilation failed.' || cr
		cedtofront
		/*	Get the current file's name to see if we are in this file's view? */
		status 21
		if sourcefile ~= result then
		DO
			okay2 'Compilation failed.'cr'Show errors?'
			if result = 1 then
				call NextError.ced
		END
		else
			call NextError.ced
		exit
	END
	else
	DO
		say 'Done.' || cr
		cedtofront
		/*	Under WShell, we can guarantee that all the "Auto:"
			commands worked, so it's fair to report that the
			compilation was successful.  Under other shells, the
			only thing we know is that the compiler didn't generate
			the AztecC.err file - i.e. it didn't find any errors.
			Some other "Auto:" command might have failed (for example,
			the linker), so we can only report that there were
			'No compiler errors.' */

		okay2 'No compiler errors.' || cr'Return to Workbench screen?'
		if result = 1 then
			address command 'WBTF'
	END
END
exit
