' CGI API

public cgi_init()
public cgi_get_command()
public cgi_send_array()
public cgi_terminate()
public cgi_send_line() '(#srv_id,#pid,$str)
public cgi_close() '(#srv_id,#pid)


' INTER PROCESS MESSAGING - API
external ipm_close()
external ipm_open()
external ipm_read()
external ipm_write()

external ipm_accept()
external ipm_end_accept() '

' MESSAGES
external message_do()
external message_alive()

main
	"@(#)cgiserv.pf3	3.1 - 95/12/12" '	SID
end main

' *************************************************
function cgi_init(VP_srv_id)
' *************************************************
	clearerror
	message_alive()
	if lerror
		message "Messaging library must be open"
		return 0
	end if

	load "ipcserv.rf3" in-memory
	if lerror
		message_do("app error",0,"Error loading IPC library.",0)
		return 0
	end if

	if ipm_accept(0,VP_srv_id)
		message_do("app error",0,"Error starting IPM messaging.",0)
		return 0
	end if
	return 1
end function

' *************************************************
function cgi_terminate(#srv_id)
' *************************************************
	ipm_end_accept(#srv_id)
	unload "ipcserv.rf3"
end function

' *************************************************
function cgi_get_command(#srv_id,#block,#timeout,#delay,AP_cmds)
' *************************************************
local #pid $str #cmds
	if ipm_open(#srv_id,varptr(#pid),#block,#timeout,#delay)
		while ipm_read(#srv_id,#pid,varptr($str),#block,#timeout,#delay)
			if $str = "EOF"
				exit while
			end if
			#cmds = #cmds + 1
			writeptr(AP_cmds,#cmds+1,$str)
		end while
		writeptr(AP_cmds,1,#cmds+1)
		return #pid
	end if
	writeptr(AP_cmds,1,1)
	return 0
end function

' *************************************************
function cgi_send_array(#srv_id,#pid,AP_html)
' *************************************************
local #ct
	#ct = 1
	while true
		if readptr(AP_html,#ct)<>0
			ipm_write(#srv_id,#pid,readptr(AP_html,#ct))
			#ct = #ct + 1
		else
			exit while
		end if
	end while
	ipm_close(#srv_id,#pid)
end function

' *************************************************
function cgi_send_line(#srv_id,#pid,$str)
' *************************************************
	ipm_write(#srv_id,#pid,$str)
end function

' *************************************************
function cgi_close(#srv_id,#pid)
' *************************************************
	ipm_close(#srv_id,#pid)
end function



