/************************************************************************
 *
 * NextError.ced					Copyright (c) 1989, Peter Cherna
 *
 * ARexx program that scans the AztecC.err file put out by the Aztec C
 * Version 3.6a compiler, and puts the cursor on successive errors,
 * opening a requester with the error message text.
 *
 * Version 1.33:  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
tabchar = '09'X

if open('errfile','AztecC.err') then
DO
	offset = getclip('ErrorOffset')

	offset = seek('errfile',offset,'B')
	errorstr = readln('errfile')
	offset = seek('errfile',0,'C')
	call setclip 'ErrorOffset',offset

	done = 0
	DO until done = 1
		if length(errorstr) > 1 then
		DO	
			/*	For Aztec C, the error file contains lines of the form
				"filename:line:char:error:errormsg"
				where filename is the name of the source module, line
				is the line number of the error, char is the character
				position of the error, error is the error number, and
				errormsg is the error message.
				We must be careful because the filename may contain a
				colon.  To begin, we assume it does, and parse accordingly: */

			parse var errorstr root ':' file ':' line ':' char ':' error ':' errormsg
	
			/*	Check if file's name had no colon.  True if 5th colon not
				found or very far over (it could be part of error msg.)
				If this is so, then parse over again */

			if (length(errormsg) = 0) | (length(error) > 4) then
			DO
				root = ''
				parse var errorstr file ':' line ':' char ':' error ':' errormsg
			END
			else
				root = root':'

			sourcefile = file
			/*	Strip off any leading directories: */
			slash = lastpos('/',sourcefile)
			if (slash > 0) then
				sourcefile = substr(sourcefile,slash+1)

			/*	Get current file's name: */
			status 21
			if upper(sourcefile) ~= upper(result) then
			DO
				/* Find out how many views are displayed */
				status 66

				/* Try to find the view containing the error file */
				DO numwins=result-1 to 1 by -1 until upper(result) = upper(sourcefile)
					next view
					/*	Get current file's name: */
					status 21
				END

				/* If we can't find the file, then ask to open it */
				if upper(result) ~= upper(sourcefile) then
				DO
					next view		/* bring back to original view */
					okay2 "Error in line" line "of file" sourcefile || cr || errormsg || cr || "Bring it in?"
					if result = 1 then
					DO
						open new
						open root || file
					END
					else
					DO
						errorstr = readln('errfile')
						offset = seek('errfile',0,'C')
						call setclip 'ErrorOffset',offset
					END
				END
			END

			/*	Get current file's name: */
			status 21
			if upper(sourcefile) = upper(result) then
			DO
				/*	We want to put the cursor on the line containing the
					error.  Check if we're already there.  Get the current
					line (relative to first line = 0) */
				status 47

				if (line ~= result+1) then
					jumpto line 1

				/*	Get tab size */
				status 8
				tabsize = result

				/*	Get contents of current line */
				status 55
				tab = 0
				DO until tab = 0
					tab = index(result,tabchar,tab+1)
					if tab > 0 then char = char + tabsize - 1
				END

				jumpto line char+1

				okay2 root || file':  Line' line', Error' error || cr || errormsg || cr'Go to next error?'
				if result = 0 then
					done = 1
				else
				DO
					errorstr = readln('errfile')
					offset = seek('errfile',0,'C')
					call setclip 'ErrorOffset',offset
				END
			END
		END
		else
		DO
			okay1 'No more errors.'
			call setclip 'ErrorOffset','0'
			done = 1
		END
	END

	call close 'errfile'
END
else
	okay1 'Error Messages not found.'
exit
