int main(int argc, char **argv) { struct MsgPort *myport; char *PortName; dll_tMessage *msg; int expunge = FALSE; int opencount = 0; /* * If an argument was passed, use it as the port name, * otherwise use the program name */ if (argc > 1) { PortName = argv[1]; } else { PortName = argv[0]; } /* * Process symbol import table */ if (!dllImportSymbols()) { D(bug("[DLL] %s: dllImportSymbols failed\n", PortName)); //exit(0); return 0; } /* * Call DLL specific constructor */ if (!DLL_Init()) { D(bug("[DLL] %s: DLL_Init failed\n", PortName)); //exit(0); return 0; } /* * Create a (public) message port */ if (!(myport = CreatePort(PortName, 0))) { D(bug("[DLL] %s: can't create message port\n", PortName)); //exit(0); return 0; } /* ** Loop until DLL expunges (that is if a CloseMessage leads to opencount==0) ** and no pending Messages are left */ while ((msg = (dll_tMessage *)GetMsg(myport)) || (!expunge)) { if (msg) { switch (msg->dllMessageType) { case DLLMTYPE_Open: D(bug("[DLL] %s: received DLLMTYPE_Open\n", PortName)); if (msg->dllMessageData.dllOpen.StackType != DLLSTACK_DEFAULT) { msg->dllMessageData.dllOpen.ErrorCode = DLLERR_StackNotSupported; if (opencount <= 0) { D(bug("[DLL] %s: unsupported stack type %d, expunging\n", PortName, msg->dllMessageData.dllOpen.StackType)); expunge = TRUE; } break; } opencount++; D(bug("[DLL] %s: increased open count to %d\n", PortName, opencount)); if (opencount > 0) { expunge = FALSE; } msg->dllMessageData.dllOpen.ErrorCode = DLLERR_NoError; break; case DLLMTYPE_Close: D(bug("[DLL] %s: received DLLMTYPE_Close\n", PortName)); opencount--; D(bug("[DLL] %s: descreased open count to %d\n", PortName, opencount)); if (opencount <= 0) { D(bug("[DLL] %s: open count is 0, expunging\n", PortName)); expunge = TRUE; } break; case DLLMTYPE_SymbolQuery: D(bug("[DLL] %s: received DLLMTYPE_SymbolQuery\n", PortName)); dllExportSymbol(&msg->dllMessageData.dllSymbolQuery); D(bug("[DLL] %s: result of symbol query for '%s': %p\n", PortName, msg->dllMessageData.dllSymbolQuery.SymbolName, *msg->dllMessageData.dllSymbolQuery.SymbolPointer)); break; case DLLMTYPE_Kill: D(bug("[DLL] %s: received DLLMTYPE_Kill\n", PortName)); expunge = TRUE; break; } /* * Send the message back */ D(bug("[DLL] %s: replying message\n", PortName)); ReplyMsg((struct Message *)msg); } /* * Wait for messages to pop up * Note that if the DLL is expunged it doesn't wait anymore, * but it still processes all pending messages (including open messages * which can disable the expunge flag). * FIXME: Is this multithread safe ?? */ if (!expunge) { D(bug("[DLL] %s: Waiting for message...\n", PortName)); WaitPort(myport); } } /* * Delete public port */ DeletePort(myport); /* * Call DLL specific destructor */ DLL_DeInit(); return 0; }
int main(int argc, char **argv) { struct MsgPort *myport; char PortName[255]; dll_tMessage *msg; int expunge=0L; int opencount=0L; bug("[DynFile] %s('%s')\n", __PRETTY_FUNCTION__, argv[0]); /* * If an argument was passed, use it as the port name, * otherwise use the program name */ if (argc>1) { char *argPort = argv[1]; if (argPort[0] == '"') strncpy(PortName, &argPort[1], strlen(argPort) - 2); else strcpy(PortName, argPort); } else { strcpy(PortName, argv[0]); } bug("[DynFile] %s: Portname '%s'\n", __PRETTY_FUNCTION__, PortName); /* * Process symbol import table */ if(!dllImportSymbols()) exit(0L); bug("[DynFile] %s: symbols imported\n", __PRETTY_FUNCTION__); /* * Call DLL specific constructor */ if(!DLL_Init()) exit(0L); bug("[DynFile] %s: initialised\n", __PRETTY_FUNCTION__); /* * Create a (public) message port */ myport = CreatePort(PortName,0); if (!myport) exit(0l); bug("[DynFile] %s: port @ 0x%p\n", __PRETTY_FUNCTION__, myport); /* ** Loop until DLL expunges (that is if a CloseMessage leads to opencount==0) ** and no pending Messages are left */ while((msg=(dll_tMessage *)GetMsg(myport))||(!expunge)) { if (msg) { switch(msg->dllMessageType) { case DLLMTYPE_Open: bug("[DynFile] %s: DLLMTYPE_Open\n", __PRETTY_FUNCTION__); /* * Stack type checking should go here. Might be ommited for strictly * private DLLs, or when stack frame compatibility can be 100% assured. * FIXME: Not handled for now */ opencount++; if(opencount>0) expunge=0L; msg->dllMessageData.dllOpen.ErrorCode=DLLERR_NoError; break; case DLLMTYPE_Close: bug("[DynFile] %s: DLLMTYPE_Close\n", __PRETTY_FUNCTION__); opencount--; if(opencount<=0L) // <0 ???? expunge=1L; break; case DLLMTYPE_SymbolQuery: bug("[DynFile] %s: DLLMTYPE_SymbolQuery\n", __PRETTY_FUNCTION__); dllExportSymbol(&msg->dllMessageData.dllSymbolQuery); //printf("Symbol Query for %s : %p\n",msg->dllMessageData.dllSymbolQuery.SymbolName, // *msg->dllMessageData.dllSymbolQuery.SymbolPointer); break; case DLLMTYPE_Kill: bug("[DynFile] %s: DLLMTYPE_Kill\n", __PRETTY_FUNCTION__); expunge=1L; break; } /* * Send the message back */ ReplyMsg((struct Message *)msg); } /* * Wait for messages to pop up * Note that if the DLL is expunged it doesn't wait anymore, * but it still processes all pending messages (including open messages * which can disable the expunge flag). * FIXME: Is this multithread safe ?? */ if(!expunge) WaitPort(myport); } /* * Delete public port */ DeletePort(myport); /* * Call DLL specific destructor */ DLL_DeInit(); return 0L; }