/*  Sendet stdin als Messages an vbrowse    */
/*  written in 1995 by Volker Barthelmann   */

#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <exec/libraries.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "vbrowse.h"

struct MsgPort *myport,*browseport;

#define BUFSIZE 256
char buf[BUFSIZE];

struct mymsg msg;

struct Library *DOSBase;

void closethings(void)
{
    if(myport) DeletePort(myport);
    if(DOSBase) CloseLibrary(DOSBase);
    exit(EXIT_SUCCESS);
}
int main(void)
{
    int len;long result=1;
    if(!(DOSBase=OpenLibrary("dos.library",0))) closethings();
    Forbid();
    if(!(browseport=FindPort(PORTNAME)))
        {Permit();Write(Output(),"vbrowse not running\n",20);closethings();}
    Permit();
    if(!(myport=CreatePort(0,0)))
        {Write(Output(),"Could get no port\n",18);closethings();}
    while(result){
        char *p,c;
        p=buf;len=0;
        do{
            *p=0;
            result=Read(Input(),p,1);
            c=*p;len++;
            if(c=='\n'||c=='\r'||c=='\f') *p=0;
        }while(result>0&&*p++);
/*        if((len=strlen(buf))==0) continue;*/
/*        if(isspace(buf[len-1])) buf[len-1]=0; else buf[len]=0;*/
/*        printf("ms: read %s\n",buf);*/
        msg.m.mn_Node.ln_Type=NT_MESSAGE;
        msg.m.mn_ReplyPort=myport;
        msg.m.mn_Length=sizeof(char *);
        msg.text=buf;
        Forbid();
        if(!(browseport=FindPort(PORTNAME)))
            {Permit();Write(Output(),"vbrowse not running\n",20);closethings();}
        PutMsg(browseport,&msg.m);
        Permit();
/*        puts("ms: Message sent");*/
        WaitPort(myport); /*    auf Reply warten    */
        while(GetMsg(myport));
/*        puts("ms: Got Reply");*/
    }
    closethings();
}

