#include <stdio.h>
#include <rpc/rpc.h>
#include "stdfm_rpc.h"

static void
fm_rpc_prog_1(rqstp, transp)
	struct svc_req *rqstp;
	SVCXPRT *transp;
{
	union {
		FmRpcCommandsArgT fm_rpc_commands_1_arg;
		FmRpcNewDocArgT fm_rpc_new_doc_1_arg;
		FmRpcOpenDocArgT fm_rpc_open_doc_1_arg;
		FmRpcImportArgT fm_rpc_import_1_arg;
		FmRpcSaveDocArgT fm_rpc_save_doc_1_arg;
		FmRpcSaveTextArgT fm_rpc_save_text_1_arg;
		FmRpcSaveMifArgT fm_rpc_save_mif_1_arg;
		FmRpcListExternalsArgT fm_rpc_list_externals_1_arg;
		FmRpcGoToLinkArgT fm_rpc_go_to_link_1_arg;
		FmRpcMessageArgT fm_rpc_message_1_arg;
		FmRpcEditInsetArgT fm_rpc_edit_inset_1_arg;
		FmRpcUpdateInsetArgT fm_rpc_update_inset_1_arg;
	} argument;
	char *result;
	bool_t (*xdr_argument)(), (*xdr_result)();
	char *(*local)();
	extern FmRpcNiceErrorT *fm_rpc_commands_1();
	extern FmRpcNewDocReturnT *fm_rpc_new_doc_1();
	extern FmRpcOpenDocReturnT *fm_rpc_open_doc_1();
	extern FmRpcNiceErrorT *fm_rpc_import_1();
	extern FmRpcNiceErrorT *fm_rpc_save_doc_1();
	extern FmRpcNiceErrorT *fm_rpc_save_text_1();
	extern FmRpcNiceErrorT *fm_rpc_save_mif_1();
	extern FmRpcNiceErrorT *fm_rpc_list_externals_1();
	extern FmRpcNiceErrorT *fm_rpc_go_to_link_1();
	extern FmRpcNiceErrorT *fm_rpc_message_1();
	extern FmRpcNiceErrorT *fm_rpc_launch_1();
	extern FmRpcNiceErrorT *fm_rpc_edit_inset_1();
	extern FmRpcNiceErrorT *fm_rpc_update_inset_1();

	switch (rqstp->rq_proc) {
	case NULLPROC:
		svc_sendreply(transp, xdr_void, NULL);
		return;

	case FM_RPC_COMMANDS:
		xdr_argument = xdr_FmRpcCommandsArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_commands_1;
		break;

	case FM_RPC_NEW_DOC:
		xdr_argument = xdr_FmRpcNewDocArgT;
		xdr_result = xdr_FmRpcNewDocReturnT;
		local = (char *(*)()) fm_rpc_new_doc_1;
		break;

	case FM_RPC_OPEN_DOC:
		xdr_argument = xdr_FmRpcOpenDocArgT;
		xdr_result = xdr_FmRpcOpenDocReturnT;
		local = (char *(*)()) fm_rpc_open_doc_1;
		break;

	case FM_RPC_IMPORT:
		xdr_argument = xdr_FmRpcImportArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_import_1;
		break;

	case FM_RPC_SAVE_DOC:
		xdr_argument = xdr_FmRpcSaveDocArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_save_doc_1;
		break;

	case FM_RPC_SAVE_TEXT:
		xdr_argument = xdr_FmRpcSaveTextArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_save_text_1;
		break;

	case FM_RPC_SAVE_MIF:
		xdr_argument = xdr_FmRpcSaveMifArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_save_mif_1;
		break;

	case FM_RPC_LIST_EXTERNALS:
		xdr_argument = xdr_FmRpcListExternalsArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_list_externals_1;
		break;

	case FM_RPC_GO_TO_LINK:
		xdr_argument = xdr_FmRpcGoToLinkArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_go_to_link_1;
		break;

	case FM_RPC_MESSAGE:
		xdr_argument = xdr_FmRpcMessageArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_message_1;
		break;

	case FM_RPC_LAUNCH:
		xdr_argument = xdr_void;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_launch_1;
		break;

	case FM_RPC_EDIT_INSET:
		xdr_argument = xdr_FmRpcEditInsetArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_edit_inset_1;
		break;

	case FM_RPC_UPDATE_INSET:
		xdr_argument = xdr_FmRpcUpdateInsetArgT;
		xdr_result = xdr_FmRpcNiceErrorT;
		local = (char *(*)()) fm_rpc_update_inset_1;
		break;

	default:
		svcerr_noproc(transp);
		return;
	}
	bzero(&argument, sizeof(argument));
	if (! svc_getargs(transp, xdr_argument, &argument)) {
		svcerr_decode(transp);
		return;
	}
	result = (*local)(&argument);
	if (! svc_sendreply(transp, xdr_result, result)) {
		svcerr_systemerr(transp);
	}
	if (! svc_freeargs(transp, xdr_argument, &argument)) {
		fprintf(stderr,"unable to free arguments\n");
	}
}



InitServer()
{
	SVCXPRT *transp;

	pmap_unset(300007, FM_RPC_VERS);

	transp = svcudp_create(RPC_ANYSOCK);
	if (transp == NULL) {
		fprintf(stderr,"cannot create udp service.\n");
	}
	if (! svc_register(transp, 300007, FM_RPC_VERS, fm_rpc_prog_1, IPPROTO_UDP)) {
		fprintf(stderr,"unable to register (300007, FM_RPC_VERS, udp).\n");
	}
else { extern ServeMe(); notify_set_input_func(ServeMe, ServeMe, transp->xp_sock); }
}
