- Set up the real-mode IDT.
[AROS.git] / workbench / rexxc / RX.c
blob3b967a6b27685ff1c7bd0a8038ad196302285d3d
1 /*
2 Copyright © 2007-2011, 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 <dos/dostags.h>
11 #include <rexx/storage.h>
12 #include <rexx/errors.h>
13 #include <workbench/startup.h>
15 #include <proto/exec.h>
16 #include <proto/dos.h>
17 #include <proto/rexxsyslib.h>
18 #include <proto/alib.h>
20 #include <string.h>
21 #include <ctype.h>
23 #ifndef __AROS__
24 #define IPTR void *
25 #endif
27 static struct RexxMsg *msg = NULL;
28 static struct MsgPort *rexxport = NULL, *replyport = NULL;
29 static BPTR out;
30 static BOOL closestdout = FALSE;
31 static BPTR olddir = (BPTR)-1;
33 static BOOL init(void)
35 #ifdef __AROS__
36 out = ErrorOutput();
37 if (out == BNULL)
38 out = Output();
39 #else
40 out = Output();
41 #endif
43 rexxport = FindPort("REXX");
44 if (rexxport == NULL)
46 if (SystemTags("RexxMast", SYS_Asynch, TRUE, TAG_DONE) >= 0)
48 SystemTags("WaitForPort REXX", TAG_DONE);
51 rexxport = FindPort("REXX");
52 if (rexxport == NULL)
54 FPuts(out, "Could not start RexxMast\n");
55 return FALSE;
58 replyport = CreatePort(NULL, 0);
59 if (replyport == NULL)
61 FPuts(out, "Could not create a port\n");
62 return FALSE;
65 msg = CreateRexxMsg(replyport, NULL, NULL);
66 if (msg == NULL)
68 FPuts(out, "Could not create RexxMsg\n");
69 return FALSE;
71 msg->rm_Action = RXCOMM | RXFF_RESULT;
72 msg->rm_Stdin = Input();
73 msg->rm_Stdout = Output();
75 return TRUE;
78 void cleanup(void)
80 if (closestdout)
81 Close(msg->rm_Stdout);
82 if (msg)
83 DeleteRexxMsg(msg);
84 if (replyport)
85 DeletePort(replyport);
86 if (olddir != (BPTR)-1)
87 CurrentDir(olddir);
90 int main(int argc, char **argv)
92 struct RexxMsg *reply;
93 int ret;
95 if (!init())
97 cleanup();
98 return RC_ERROR;
101 if (argc == 1)
103 FPuts(out, "Usage: RX <filename> [arguments]\n"
104 " RX \"commands\"\n");
105 cleanup();
106 return RC_ERROR;
109 if (argc == 0)
111 struct WBStartup *startup = (struct WBStartup *) argv;
112 char *s = startup->sm_ArgList[1].wa_Name;
114 if (startup->sm_NumArgs < 2)
116 cleanup();
117 return RC_ERROR;
120 olddir = CurrentDir(startup->sm_ArgList[1].wa_Lock);
121 out = msg->rm_Stdout = Open("CON:////RX Output/CLOSE/WAIT/AUTO", MODE_READWRITE);
122 closestdout = TRUE;
124 msg->rm_Args[0] = (IPTR)CreateArgstring(s, strlen(s));
125 msg->rm_Action |= 1;
127 else
129 UBYTE *s;
130 struct Process *me = (struct Process *)FindTask(NULL);
131 ULONG length = 0;
133 s = me->pr_Arguments;
134 while(isspace(*s)) s++;
136 if (*s == '"')
138 s++;
139 while((s[length] != '"') && (s[length] != '\0')) length++;
140 if (length == 0)
142 FPuts(out, "Empty command\n");
143 cleanup();
144 return RC_ERROR;
146 if (s[length] == '\0')
148 FPuts(out, "Unterminated string\n");
149 cleanup();
150 return RC_ERROR;
153 msg->rm_Args[0] = (IPTR)CreateArgstring(s, length);
154 /* It is a literal command with 1 argument */
155 msg->rm_Action |= (RXFF_STRING | 1);
157 else if (*s == '\'')
159 s++;
160 while((s[length] != '\'')
161 && (s[length] != '\0')
162 && (s[length] != '\n')
164 length++;
166 msg->rm_Args[0] = (IPTR)CreateArgstring(s, length);
167 /* It is a literal command with 1 argument */
168 msg->rm_Action |= (RXFF_STRING | 1);
170 else
172 if (s[strlen(s)-1] == '\n')
173 s[strlen(s)-1] = '\0';
175 msg->rm_Args[0] = (IPTR)CreateArgstring(s, strlen(s));
176 msg->rm_Action |= 1;
181 PutMsg(rexxport, (struct Message *)msg);
182 do {
183 reply = (struct RexxMsg *)WaitPort(replyport);
184 } while (reply != msg);
186 ret = msg->rm_Result1;
187 if (msg->rm_Result1 == RC_OK)
188 FPrintf(out, "Script executed and returned: %ld\n", msg->rm_Result2);
189 else
190 FPrintf(out, "Error executing script %ld/%ld\n",
191 msg->rm_Result1, msg->rm_Result2
194 ClearRexxMsg(msg, msg->rm_Action & RXARGMASK);
195 cleanup();
197 return ret;