OS no longer mentioned in information window.
[tangerine.git] / workbench / rexxc / RX.c
blob76446add961e676a33f2e67717d20f9a0560f5b6
1 /*
2 Copyright © 2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Run rexx scripts
6 */
8 #include <dos/dos.h>
9 #include <dos/dosextens.h>
10 #include <rexx/storage.h>
11 #include <rexx/errors.h>
12 #include <workbench/startup.h>
14 #include <proto/exec.h>
15 #include <proto/dos.h>
16 #include <proto/rexxsyslib.h>
17 #include <proto/alib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <ctype.h>
23 static struct RexxMsg *msg = NULL;
24 static struct MsgPort *rexxport = NULL, *replyport = NULL;
25 static BPTR out;
26 static BOOL closestdout = FALSE;
27 static BPTR olddir = (BPTR)-1;
29 static BOOL init(void)
31 #ifdef __AROS__
32 out = Error();
33 #else
34 out = Output();
35 #endif
37 rexxport = FindPort("REXX");
38 if (rexxport == NULL)
40 if (SystemTags("RexxMast", SYS_Asynch, TRUE, TAG_DONE) >= 0)
42 SystemTags("WaitForPort REXX", TAG_DONE);
45 rexxport = FindPort("REXX");
46 if (rexxport == NULL)
48 FPuts(out, "Could not start RexxMast\n");
49 return FALSE;
52 replyport = CreatePort(NULL, 0);
53 if (replyport == NULL)
55 FPuts(out, "Could not create a port\n");
56 return FALSE;
59 msg = CreateRexxMsg(replyport, NULL, NULL);
60 if (msg == NULL)
62 FPuts(out, "Could not create RexxMsg\n");
63 return FALSE;
65 msg->rm_Action = RXCOMM | RXFF_RESULT;
66 msg->rm_Stdin = Input();
67 msg->rm_Stdout = Output();
69 return TRUE;
72 void cleanup(void)
74 if (closestdout)
75 Close(msg->rm_Stdout);
76 if (msg)
77 DeleteRexxMsg(msg);
78 if (replyport)
79 DeletePort(replyport);
80 if (olddir != (BPTR)-1)
81 CurrentDir(olddir);
84 int main(int argc, char **argv)
86 struct RexxMsg *reply;
87 int ret;
89 if (!init())
91 cleanup();
92 return RC_ERROR;
95 if (argc == 1)
97 FPuts(out, "Required argument missing\n");
98 cleanup();
99 return RC_ERROR;
102 if (argc == 0)
104 struct WBStartup *startup = (struct WBStartup *) argv;
105 char *s = startup->sm_ArgList[1].wa_Name;
107 if (startup->sm_NumArgs < 2)
109 cleanup();
110 return RC_ERROR;
113 olddir = CurrentDir(startup->sm_ArgList[1].wa_Lock);
114 out = msg->rm_Stdout = Open("CON:////RX Output/CLOSE/WAIT/AUTO", MODE_READWRITE);
115 closestdout = TRUE;
117 msg->rm_Args[0] = (IPTR)CreateArgstring(s, strlen(s));
118 msg->rm_Action |= 1;
120 else
122 UBYTE *s;
123 struct Process *me = (struct Process *)FindTask(NULL);
124 ULONG length = 0;
126 s = me->pr_Arguments;
127 while(isspace(*s)) s++;
129 if (*s == '"')
131 s++;
132 while((s[length] != '"') && (s[length] != '\0')) length++;
133 if (length == 0)
135 FPuts(out, "Empty command\n");
136 cleanup();
137 return RC_ERROR;
139 if (s[length] == '\0')
141 FPuts(out, "Unterminated string\n");
142 cleanup();
143 return RC_ERROR;
146 msg->rm_Args[0] = (IPTR)CreateArgstring(s, length);
147 /* It is a literal command with 1 argument */
148 msg->rm_Action |= (RXFF_STRING | 1);
150 else if (*s == '\'')
152 s++;
153 while((s[length] != '\'')
154 && (s[length] != '\0')
155 && (s[length] != '\n')
157 length++;
159 msg->rm_Args[0] = (IPTR)CreateArgstring(s, length);
160 /* It is a literal command with 1 argument */
161 msg->rm_Action |= (RXFF_STRING | 1);
163 else
165 if (s[strlen(s)-1] == '\n')
166 s[strlen(s)-1] = '\0';
168 msg->rm_Args[0] = (IPTR)CreateArgstring(s, strlen(s));
169 msg->rm_Action |= 1;
174 PutMsg(rexxport, (struct Message *)msg);
175 do {
176 reply = (struct RexxMsg *)WaitPort(replyport);
177 } while (reply != msg);
179 ret = msg->rm_Result1;
180 if (msg->rm_Result1 == RC_OK)
181 FPrintf(out, "Script executed and returned: %ld\n", msg->rm_Result2);
182 else
183 FPrintf(out, "Error executing script %ld/%ld\n",
184 msg->rm_Result1, msg->rm_Result2
187 ClearRexxMsg(msg, msg->rm_Action & RXARGMASK);
188 cleanup();
190 return ret;