1

I'm new to Mathlink, and before integrating it in my code I tried to write a small REPL to get accustomed to it. The code is as follows (irrelevent parts omitted, and sorry for the horrible blend of C and C++):

int main(int argc,char **argv)
{
    init_and_openlink(argc,argv);

    while(!feof(stdin))
    {
        int pkt;
        char buf[1024];

        if(!fgets(buf,1024,stdin))
            continue;

        MLPutFunction(lp,"EnterTextPacket",1);
        MLPutString(lp,buf);
        MLEndPacket(lp);

        while(((pkt=MLNextPacket(lp),pkt))&&(pkt!=RETURNPKT))
        {
            MLNewPacket(lp);

            if(MLError(lp))
                return 1;
        }

        const char *result;
        MLGetString(lp,&result);
        printf("%s\n",result);
        MLReleaseString(lp,result);
    }

    return 0;
}

but it doesn't seem to work at all. I've tried replacing the while loop with a single MLNextPacket instruction but to no avail; I spent hours searching Mathlink documentation, but that one is a big mess! Where I'm doing it wrong?

zakk
  • 375
  • 1
  • 4
  • 14

1 Answers1

1

The EnterTextPacket MathLink packet will make the kernel return the result wrapped in a ReturnTextPacket MathLink packet. Try changing the condition in your while loop to:

while(((pkt=MLNextPacket(lp),pkt))&&(pkt!=RETURNTEXTPKT))
sakra
  • 62,199
  • 16
  • 168
  • 151
  • Works like a charm, thank you! But if I tried to exit the look with CTRL-D the program crashes... I've added outside the loop: MLPutFunction(lp,"Exit",0); closelink(); deinit(); but it doesn't help... – zakk Jan 23 '12 at 20:19