corrections to check for existing files.
[AROS-Contrib.git] / dopus / DOpusRT / DOpusRT.c
blob4487cef3487cc332e47806e97d60fbecc570f390
1 /*
3 Directory Opus 4
4 Original GPL release version 4.12
5 Copyright 1993-2000 Jonathan Potter
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 All users of Directory Opus 4 (including versions distributed
22 under the GPL) are entitled to upgrade to the latest version of
23 Directory Opus version 5 at a reduced price. Please see
24 http://www.gpsoft.com.au for more information.
26 The release of Directory Opus 4 under the GPL in NO WAY affects
27 the existing commercial status of Directory Opus 5.
31 //#include <fctype.h>
32 #include <string.h>
33 //#include <stdlib.h>
35 //#include <exec/types.h>
36 //#include <exec/memory.h>
37 //#include <exec/execbase.h>
38 //#include <dos/dos.h>
39 //#include <dos/dosextens.h>
40 //#include <dos/exall.h>
41 #include <intuition/intuitionbase.h>
42 #include <graphics/gfxbase.h>
43 #include <devices/input.h>
44 #include <workbench/startup.h>
45 #include <dopus/dopusbase.h>
47 #include <proto/dos.h>
48 #include <proto/exec.h>
49 #include <proto/graphics.h>
50 #include <proto/icon.h>
51 #include <proto/dopus.h>
53 #ifndef __AROS__
54 #include <debug.h>
55 #endif
57 #if defined(__PPC__) || defined(__AROS__)
58 #undef __saveds
59 #define __saveds
60 #define __chip
61 #define __aligned __attribute__((__aligned__(4)))
62 #define lsprintf sprintf
63 #define __asm(A)
64 #endif
66 //extern struct ExecBase *SysBase;
67 //extern struct DosLibrary *DOSBase;
68 struct DOpusBase *DOpusBase = NULL;
70 void WBRun(int,char **);
71 int setarg(struct WBArg *,char *,BPTR);
72 BPTR CloneCommandDir(const char *);
74 static const char *version="$VER: DOpusRT 4.8 (" __DATE__ ") by Jacek Rzeuski";
76 static const char *pathlists[7]={
77 "Workbench",
78 "Initial CLI",
79 "Shell Process",
80 "New CLI",
81 "AmigaShell",
82 "New_WShell",
83 "Background CLI"};
85 struct InputEvent *InputHandler(register struct InputEvent *ie __asm("a0"),register struct Task *task __asm("a1"))
87 if (ie->ie_Class == IECLASS_RAWMOUSE)
89 if (ie->ie_Code & IECODE_LBUTTON)
91 #if !defined(__AROS__)
92 register struct ExecBase *SysBase __asm("a6") = *(struct ExecBase **)4L;
93 #endif
95 Signal(task,(ie->ie_Code & IECODE_UP_PREFIX)?2:1);
98 return ie;
101 static inline LONG atoi(char *str)
103 LONG i;
105 StrToLong((STRPTR)str,&i);
107 return i;
110 int main(argc,argv)
111 int argc;
112 char *argv[];
114 int a,out,x,y,flag;
115 struct MsgPort *port,*port1,*cont;
116 struct Process *myproc;
117 struct Message msg;
118 struct Window *win=NULL;
119 struct CommandLineInterface *cli;
120 struct IntuitionBase *IntuitionBase;
121 struct GfxBase *GfxBase;
122 struct Interrupt *interrupt;
123 struct IOStdReq *inputreq;
125 if (argc<2 || !(DOpusBase=(struct DOpusBase *)OpenLibrary("dopus.library",0))) return 1000000/*_exit(0)*/;
126 IntuitionBase=DOpusBase->IntuitionBase;
127 GfxBase=DOpusBase->GfxBase;
129 myproc=(struct Process *)FindTask(NULL);
130 cli=BADDR(myproc->pr_CLI);
131 if (cont=GetConsoleTask()) {
132 struct InfoData __aligned ind;
133 ULONG arg=MKBADDR(&ind);
135 if (SendPacket(cont,ACTION_DISK_INFO,&arg,1))
136 win=(struct Window *)ind.id_VolumeNode;
138 out=Output();
140 if (argv[1][0]=='-') switch(LToLower(argv[1][1]))
142 case 's':
143 if (win) {
144 if ((win->WScreen->Flags&SCREENTYPE)!=WBENCHSCREEN) {
145 myproc->pr_WindowPtr=(APTR)win;
146 SetFont(win->RPort,GfxBase->DefaultFont);
147 Write(out,"\x1b\x63",2);
150 if (cli && !cli->cli_CommandDir) {
151 BPTR path = NULL;
153 for (a=0;a<7;a++) if ((path=CloneCommandDir(pathlists[a]))) break;
154 cli->cli_CommandDir=path;
156 break;
157 case 'w':
158 if ((a=atoi(argv[2]))>60) a=60;
159 if (a>0) Delay(a*50);
160 else if (a==-1) {
161 if (out) {
162 if (port=LCreatePort(NULL,0)) {
163 if (inputreq=(struct IOStdReq *)LCreateExtIO(port,sizeof(struct IOStdReq))) {
164 if (!(OpenDevice("input.device",NULL,(struct IORequest *)inputreq,NULL))) {
165 if (interrupt=AllocMem(sizeof(struct Interrupt),MEMF_CLEAR|MEMF_PUBLIC)) {
166 interrupt->is_Code=(APTR)InputHandler;
167 interrupt->is_Node.ln_Pri=51;
168 interrupt->is_Node.ln_Name="DOpusRT";
169 interrupt->is_Data=myproc;
170 if (argc>3) {
171 Write(out,"\n",1);
172 Write(out,argv[3],strlen(argv[3]));
174 else Write(out,"\nPress left mouse button to continue...",39);
175 inputreq->io_Data=(APTR)interrupt;
176 inputreq->io_Command=IND_ADDHANDLER;
177 DoIO((struct IORequest *)inputreq);
178 flag=0;
179 FOREVER {
180 a=Wait(3);
181 if (!win) break;
182 if (IntuitionBase->ActiveWindow==win) {
183 x=win->MouseX; y=win->MouseY;
184 if (a&1) {
185 if ((x<=win->BorderLeft) || (x>=win->Width-win->BorderRight) ||
186 (y<=win->BorderTop) || (y>=win->Height-win->BorderBottom)) flag=0;
187 else flag=1;
189 else if (a&2 && flag) {
190 flag=0;
191 if (x>win->BorderLeft && x<win->Width-win->BorderRight &&
192 y>win->BorderTop && y<win->Height-win->BorderBottom) break;
196 // inputreq->io_Data=(APTR)interrupt;
197 inputreq->io_Command=IND_REMHANDLER;
198 DoIO((struct IORequest *)inputreq);
199 FreeMem(interrupt,sizeof(struct Interrupt));
201 CloseDevice((struct IORequest *)inputreq);
203 LDeleteExtIO((struct IORequest *)inputreq);
205 LDeletePort(port);
209 break;
210 case 'p':
211 Forbid();
212 if ((port=FindPort(argv[2]))) {
213 Permit();
214 port1=LCreatePort(0,0);
215 msg.mn_Node.ln_Type=NT_MESSAGE;
216 msg.mn_ReplyPort=port1;
217 msg.mn_Length=sizeof(struct Message);
218 PutMsg(port,&msg);
219 WaitPort(port1);
220 LDeletePort(port1);
222 else Permit();
223 break;
224 case 'r':
225 WBRun(argc-2,&argv[2]);
226 break;
228 CloseLibrary((struct Library *)DOpusBase);
229 return 0;
230 //_exit(0);
233 struct PathList
235 BPTR nextPath;
236 BPTR pathLock;
239 BPTR CloneCommandDir(const char *taskname)
241 struct Process *teacher;
242 struct CommandLineInterface *teachcli;
243 struct PathList *wext,*mext,*lastmext=NULL;
244 BPTR newpath=NULL;
246 Forbid();
247 if (teacher=(struct Process *)FindTask(taskname))
249 if (teachcli=(struct CommandLineInterface *)BADDR(teacher->pr_CLI))
251 for (wext=BADDR(teachcli->cli_CommandDir); wext; wext=BADDR(wext->nextPath))
253 if (!(mext = AllocVec(sizeof(struct PathList),MEMF_PUBLIC))) break;
255 mext->nextPath=NULL;
256 mext->pathLock=DupLock(wext->pathLock);
257 if (!newpath) newpath=MKBADDR(mext);
259 if (lastmext) lastmext->nextPath=MKBADDR(mext);
260 lastmext=mext;
264 Permit();
265 return newpath;
268 void WBRun(argc,argv)
269 int argc;
270 char **argv;
272 struct WBStartup *WBStartup;
273 struct DiskObject *diskobj=NULL;
274 char namebuf[256];
275 int stacksize,i,ok=1;
276 struct Process *ourtask;
277 struct MsgPort *replyport=NULL;
278 BPTR olddir=-1;
279 struct IconBase *IconBase;
280 struct DOpusRemember *key=NULL;
281 struct CommandLineInterface *cli;
283 if (argc<1 || !(IconBase=(struct IconBase *)OpenLibrary("icon.library",0))) return;
285 ourtask=(struct Process *)FindTask(NULL);
287 if ((WBStartup=LAllocRemember(&key,sizeof(struct WBStartup),MEMF_CLEAR)) &&
288 (WBStartup->sm_ArgList=LAllocRemember(&key,sizeof(struct WBArg)*(argc+1),MEMF_CLEAR)) &&
289 (replyport=LCreatePort(NULL,0)))
291 WBStartup->sm_Message.mn_ReplyPort=replyport;
292 WBStartup->sm_NumArgs=argc;
294 for (i=0;i<argc;i++)
295 if (setarg(&WBStartup->sm_ArgList[i],argv[i],ourtask->pr_CurrentDir))
296 break;
298 if (i==argc)
300 olddir=CurrentDir(WBStartup->sm_ArgList[0].wa_Lock);
302 if (!(diskobj=GetDiskObject(WBStartup->sm_ArgList[0].wa_Name)))
304 if (!(SearchPathList(argv[0],namebuf,256)))
305 ok=0;
307 else if (diskobj->do_Type==WBTOOL) LStrCpy(namebuf,argv[0]);
308 else if (diskobj->do_Type==WBPROJECT)
310 if (SearchPathList(diskobj->do_DefaultTool,namebuf,256))
312 for (i = argc - 1; i >= 0; i--)
313 CopyMem(&WBStartup->sm_ArgList[i],&WBStartup->sm_ArgList[i+1],sizeof(struct WBArg));
314 if (setarg(&WBStartup->sm_ArgList[0],namebuf,WBStartup->sm_ArgList[0].wa_Lock))
315 ok=0;
316 else ++WBStartup->sm_NumArgs;
318 else ok=0;
320 else ok=0;
322 if (ok)
324 if (diskobj) stacksize=diskobj->do_StackSize;
325 else
327 if (cli = BADDR(ourtask->pr_CLI))
328 stacksize = cli->cli_DefaultStack*4;
329 else stacksize = 4096;
332 stacksize = (stacksize+3)&(~3);
333 if (stacksize < 4096) stacksize = 4096;
335 WBStartup->sm_ToolWindow=(diskobj)?diskobj->do_ToolWindow:NULL;
337 if (WBStartup->sm_Segment = LoadSeg(namebuf))
339 char *ptr = BaseName(namebuf);
341 Forbid();
342 if (WBStartup->sm_Process=CreateProc(ptr,
343 ourtask->pr_Task.tc_Node.ln_Pri,WBStartup->sm_Segment,stacksize))
345 if (ptr) *ptr=0;
346 ((struct Process *)(WBStartup->sm_Process->mp_SigTask))->pr_HomeDir=Lock(ptr,ACCESS_READ);
348 PutMsg(WBStartup->sm_Process,(struct Message *)WBStartup);
350 Permit();
351 WaitPort(replyport);
353 else Permit();
358 if (replyport) LDeletePort(replyport);
359 if (diskobj) FreeDiskObject(diskobj);
360 if (olddir!=-1) CurrentDir(olddir);
361 if (WBStartup) {
362 /*if (WBStartup->sm_Segment)*/ UnLoadSeg(WBStartup->sm_Segment);
363 if (WBStartup->sm_ArgList) {
364 for (i=0;i<WBStartup->sm_NumArgs;i++) {
365 // if (WBStartup->sm_ArgList[i].wa_Lock)
366 UnLock(WBStartup->sm_ArgList[i].wa_Lock);
370 LFreeRemember(&key);
371 /*if (IconBase)*/ CloseLibrary((struct Library *)IconBase);
374 int setarg(WBArg,name,curdir)
375 struct WBArg *WBArg;
376 char *name;
377 BPTR curdir;
379 char *p,*lastc;
380 unsigned char c;
382 if (!name || !name[0]) return(1);
384 lastc=NULL;
385 for (p=name;*p;p++)
386 if (*p==':' || *p=='/') lastc=p+1;
388 if (!lastc) {
389 WBArg->wa_Lock=DupLock(curdir);
390 WBArg->wa_Name=name;
392 else {
393 if (!lastc[0]) return(1);
394 WBArg->wa_Name=lastc;
395 c=lastc[0];
396 lastc[0]=0;
397 if (!(WBArg->wa_Lock=Lock(name,ACCESS_READ))) return(1);
398 *lastc=c;
400 return(0);