
#import <appkit/Listener.h>
#include <stdio.h>
#import <servers/netname.h>
extern port_t       name_server_port;
#define TRUE -1
#define FALSE 0

int send_message(port_t send_port, char *selector, char *string_argument)
{
    static NXMessage msg_buf[MSG_SIZE_MAX];
    NXMessage *send_msg = msg_buf;
    int err,len, moffset, msize;
    msg_type_t *st;

    send_msg->header.msg_type = MSG_TYPE_NORMAL;
    send_msg->header.msg_id = NX_SELECTORPMSG;
    send_msg->header.msg_simple = TRUE;
    send_msg->header.msg_remote_port = send_port;
    send_msg->header.msg_local_port = PORT_NULL;

    send_msg->sequenceType.msg_type_name = MSG_TYPE_INTEGER_32;
    send_msg->sequenceType.msg_type_size = sizeof(int) * 8;
    send_msg->sequenceType.msg_type_number = 1;
    send_msg->sequenceType.msg_type_inline = TRUE;
    send_msg->sequenceType.msg_type_longform = FALSE;
    send_msg->sequenceType.msg_type_deallocate = FALSE;

    send_msg->actionType.msg_type_name = MSG_TYPE_BYTE;
    send_msg->actionType.msg_type_size = 8;
    send_msg->actionType.msg_type_inline = TRUE;
    send_msg->actionType.msg_type_longform = FALSE;
    send_msg->actionType.msg_type_deallocate = FALSE;

    send_msg->sequence = 1;

    len = strlen(selector) + 1;
    send_msg->actionType.msg_type_number = len;
    bcopy(selector, send_msg->action, len);

    moffset = (int)(send_msg->action - (char *)send_msg) + ((len + 3) / 4) * 4;
    msize = moffset + sizeof(msg_type_t) + ((len + 3) / 4);

    st = (msg_type_t *)((char *)send_msg + moffset);
    st->msg_type_inline = TRUE;
    st->msg_type_longform = FALSE;
    st->msg_type_deallocate = FALSE;

    len = strlen(string_argument) + 1;
    st->msg_type_name = MSG_TYPE_BYTE;
    st->msg_type_size = 8;
    st->msg_type_number = len;
    bcopy(string_argument, ((char *)st + sizeof(msg_type_t)), len);

    send_msg->header.msg_size = ((char *)st-(char *)send_msg) +
    				sizeof(msg_type_t) + ((len + 3) / 4) * 4;
    return msg_send(send_msg,SEND_TIMEOUT,30*1000); /* thirty sec timeout */
}

#define MAXSTRSIZE (4096)

main()
{
    char tmp[NX_MAXMESSAGE], *p = tmp;
    port_t send_port;
    int i, j=0,  done=0, err = netname_look_up(name_server_port, "", "Lisp", &send_port);
    if (err) exit(1);
    while (!done && (j<MAXSTRSIZE)) {
	for (i=0; i<(NX_MAXMESSAGE-3); i++) {
	    if ((*p++=getchar()) == EOF) {
		p--;
		done=TRUE;
		break;
	    }
	    j++;
	}
	if (done) {
	    if (p[-1] != '\n')  *p++ = '\n';
	    *p = '\0';}
	if (tmp[0]) {
	    
	    fprintf(stdout,"i = %d \n j = %d \n %s ",i,j,tmp);
	    
	    err = send_message(send_port,"appEvaluate:",tmp);
	    if (err) exit(1);
	    }
	for (i=0; i<(NX_MAXMESSAGE); i++) {
	    p[i] = '\0';}	
    }
    if (i != MAXSTRSIZE)
	exit(0);
}



