Title.mui: minor code cleanup
[AROS.git] / workbench / libs / rexxsupport / openport.c
blob1e85d019ababf30977fbaa962fb3ef83e5918dba
1 /*
2 Copyright © 1995-2002, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Rexx function to create a MsgPort
6 Lang: English
7 */
9 #include <proto/exec.h>
10 #include <proto/rexxsyslib.h>
11 #include <proto/alib.h>
13 #include <exec/types.h>
14 #include <exec/memory.h>
15 #include <rexx/storage.h>
16 #include <rexx/errors.h>
17 #include <aros/debug.h>
19 #include <string.h>
21 #include "rexxsupport_intern.h"
22 #include "portnode.h"
24 LONG rxsupp_openport(struct Library *RexxSupportBase, struct RexxMsg *msg, UBYTE **argstring)
26 struct PortNode *node;
27 struct PortNodeData *data;
28 UBYTE *name;
30 data = (struct PortNodeData *)FindName(&RSBI(RexxSupportBase)->openports, ARG1(msg));
31 if (data == NULL)
33 struct MsgPort *replyport, *rexxport, *port;
34 struct RexxMsg *msg2 = NULL, *msg3;
35 if (FindPort(ARG1(msg)) != NULL)
37 *argstring = CreateArgstring("0", 1);
38 return RC_OK;
40 rexxport = FindPort("REXX");
41 if (rexxport == NULL)
43 *argstring = NULL;
44 return ERR10_013;
46 port = CreateMsgPort();
47 if (port)
49 node = (struct PortNode *)AllocMem(sizeof(struct PortNode), MEMF_PUBLIC);
50 name = AllocMem(LengthArgstring(ARG1(msg))+1, MEMF_PUBLIC);
51 strcpy(name, ARG1(msg));
52 node->rsrc.rr_Node.ln_Name = name;
53 node->rsrc.rr_Base = RexxSupportBase;
54 node->rsrc.rr_Func = -36;
55 node->data.node.ln_Name = name;
56 node->data.self = (struct RexxRsrc *)node;
57 node->data.port = port;
58 NewList(&node->data.msgs);
59 /* Lock lib in memory because it can be called by rexx */
60 if (IsListEmpty(&RSBI(RexxSupportBase)->openports))
61 RexxSupportBase->lib_OpenCnt++;
63 AddTail(&RSBI(RexxSupportBase)->openports, &node->data.node);
64 port->mp_Node.ln_Name = name;
65 port->mp_Node.ln_Pri = 0;
66 AddPort(port);
68 replyport = CreateMsgPort();
69 if (replyport != NULL)
71 replyport->mp_Node.ln_Name = NULL;
72 replyport->mp_Node.ln_Pri = 0;
73 msg2 = CreateRexxMsg(replyport, NULL, NULL);
75 if (replyport == NULL || msg2 == NULL)
77 *argstring = NULL;
78 return ERR10_003;
80 msg2->rm_Private1 = msg->rm_Private1;
81 msg2->rm_Private2 = msg->rm_Private2;
82 msg2->rm_Action = RXADDRSRC;
83 msg2->rm_Args[0] = (IPTR)node;
84 PutMsg(rexxport, (struct Message *)msg2);
85 do {
86 WaitPort(replyport);
87 msg3 = (struct RexxMsg *)GetMsg(replyport);
88 } while (msg3 != msg2);
89 if (msg2->rm_Result1 != RC_OK)
90 kprintf("ClosePort ADDRSRC Result1 != RC_OK\n");
91 DeleteRexxMsg(msg2);
92 DeleteMsgPort(replyport);
94 *argstring = CreateArgstring("1", 1);
95 return RC_OK;
97 else
99 *argstring = CreateArgstring("0", 1);
100 return RC_OK;
103 else
105 *argstring = CreateArgstring("0", 1);
106 return RC_OK;