; ASM2MSG.ASM--
; Parse output from Edit command
;
; WCC    '<file>(<line>): Error! <id>: <message>'
; JWasm  '<file>(<line>) : Error <id>: <message>: <text>'
; Masm   '<file>(<line>) : error <id>: <message>'
;
include	clib.inc
include	io.inc
include	dir.inc
include	string.inc
include	stdio.inc
include	stdlib.inc

Message	struc
m_file	db WMAXPATH dup(?)
m_info	db 256 dup(?)
m_line	dd ?
Message	ends

	.data
	hPipe	dd 0
	nPipe	db "pipe.err",0
	chars	db "\abcdefghijklmnopqrstuvwxyz0123456789_@",0

	.code

TestChar proc uses edi ecx eax
	.if al >= 'A' && al <= 'Z'
	    or al,20h
	.endif
	mov edi,offset chars
	mov ecx,sizeof chars
	repne scasb
	cmp byte ptr [edi-1],0
	ret
TestChar endp

ParseLine proc uses ecx edi esi
local	M:Message
	mov edi,esi
	mov al,[edi]
	call TestChar
	.if !ZERO?
	    invoke memzero,addr M,SIZE Message
	    .if func(strchr,edi,40)
		mov cl,[eax+1]
		.if (cl >= '0' && cl <= '9')
		    xchg esi,eax
		    invoke _print,cstr(<"%s",10>),eax
		    mov byte ptr [esi],0
		    inc esi
		    invoke atol,esi
		    mov ecx,eax
		    inc esi
		    mov M.m_line,ecx
		    invoke strncpy,addr M.m_file,edi,WMAXPATH-1
		    .if func(strchr,esi,':')
			inc eax
			mov esi,eax
		    .endif
		    invoke strncpy,addr M.m_info,esi,255
		    .if !hPipe
		    	.if func(osopen,addr nPipe,0,M_WRONLY,A_CREATETRUNC) != -1
			    mov hPipe,eax
			.else
			    jmp @F
			.endif
		    .endif
		    invoke oswrite,hPipe,addr M,SIZE Message
		.endif
	    .endif
	.endif
      @@:
	ret
ParseLine endp

ParseOutput proc stdcall uses esi edi ebx pBuffer:dword, bSize:dword
	mov edi,pBuffer
	mov esi,edi
	mov ecx,bSize
	mov byte ptr [edi+ecx],0
	.repeat
	    mov al,10
	    repne scasb
	    .if ecx
		mov byte ptr [edi-2],0
		call ParseLine
	    .else
		call ParseLine
		.break
	    .endif
	    mov esi,edi
	.until 0
	.if hPipe
	    invoke close,hPipe
	.endif
	ret
ParseOutput endp

	END
