2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
7 /*****************************************************************************
11 Path [{<dir>}] [ADD] [SHOW] [RESET] [REMOVE] [QUIET] [HEAD]
15 PATH/M,ADD/S,SHOW/S,RESET/S,REMOVE/S,QUIET/S,HEAD/S
23 Changes the search path for commands. Without arguments it shows the path.
30 RESET -- removes existing path and replaces it by new path
31 REMOVE -- removes the given path
32 QUIET -- suppresses dialog when a path is not found
33 HEAD -- inserts path at beginning of path list
52 ******************************************************************************/
54 #include <exec/memory.h>
55 #include <proto/exec.h>
58 #include <dos/dosextens.h>
60 #include <proto/dos.h>
61 #include "dos_commanderrors.h"
63 #include <aros/shcommands.h>
65 #include <aros/debug.h>
67 typedef struct CommandLineInterface
* CommandLineInterfacePtr
;
73 } PathEntry
, *PathEntryPtr
;
75 #define PE(x) ((PathEntry *)(BADDR(x)))
77 static PathEntryPtr
FindPathEntry(CommandLineInterfacePtr cli
, STRPTR pathName
,
78 PathEntryPtr
* predStorage
, APTR DOSBase
);
80 static PathEntryPtr
InsertPathEntry(PathEntryPtr predecessor
, STRPTR pathName
, APTR SysBase
, APTR DOSBase
);
82 static BOOL
IsDirectory(BPTR lock
, APTR DOSBase
);
85 AROS_SHA(STRPTR
*, ,PATH
,/M
,NULL
),
86 AROS_SHA(BOOL
, ,ADD
,/S
,NULL
),
87 AROS_SHA(BOOL
, ,SHOW
,/S
,NULL
),
88 AROS_SHA(BOOL
, ,RESET
,/S
,NULL
),
89 AROS_SHA(BOOL
, ,REMOVE
,/S
,NULL
),
90 AROS_SHA(BOOL
, ,QUIET
,/S
,NULL
),
91 AROS_SHA(BOOL
, ,HEAD
,/S
,NULL
))
95 CommandLineInterfacePtr
100 PrintFault(ERROR_SCRIPT_ONLY
, "Path");
108 cur
= PE(cli
->cli_CommandDir
);
113 next
= PE(cur
->next
);
117 FreeVec(BADDR(cur
->next
));
122 cli
->cli_CommandDir
= BNULL
;
133 if (SHArg(REMOVE
) && !SHArg(RESET
))
139 for (idx
= 0; names
[idx
]; ++idx
)
141 entry
= FindPathEntry(cli
, names
[idx
], &pred
, DOSBase
);
143 /* free the path entry */
148 pred
->next
= entry
->next
;
152 cli
->cli_CommandDir
= entry
->next
;
155 if (BNULL
!= entry
->lock
)
167 insertAfter
= (PathEntryPtr
)&cli
->cli_CommandDir
;
171 /* Search last entry */
172 while (BNULL
!= insertAfter
->next
)
174 insertAfter
= PE(insertAfter
->next
);
178 for (idx
= 0; names
[idx
]; ++idx
)
180 if (NULL
!= FindPathEntry(cli
, names
[idx
], NULL
, DOSBase
))
182 /* don't add if already in path */
187 insertAfter
= InsertPathEntry(insertAfter
, names
[idx
], SysBase
, DOSBase
);
194 SHArg(SHOW
) = SHArg(RESET
) == 0;
201 PutStr("Current Directory\n");
206 for (cur
= PE(cli
->cli_CommandDir
); cur
; cur
= PE(cur
->next
))
208 NameFromLock (cur
->lock
, Buffer
, sizeof (Buffer
));
223 /** find the specfied path name in the specified CLI's path list.
225 * @autodoc:function if the path specified by pathName is in the path list of
226 * the specified CLI the corresponding path entry is returned. the path entry's
227 * predecessor is stored in the specified location if the storage pointer is
232 FindPathEntry(CommandLineInterfacePtr cli
, STRPTR pathName
,
233 PathEntryPtr
* predStorage
, APTR DOSBase
)
238 if (NULL
!= cli
&& BNULL
!= cli
->cli_CommandDir
&& NULL
!= pathName
)
241 pathLock
= Lock(pathName
, ACCESS_READ
);
243 if (BNULL
!= pathLock
)
247 curr
= PE(cli
->cli_CommandDir
);
252 value
= SameLock(pathLock
, curr
->lock
);
254 if (LOCK_SAME
== value
)
258 if (NULL
!= predStorage
)
267 curr
= PE(curr
->next
);
274 PrintFault(IoErr(), pathName
);
281 /* insert a path entry for the specified path
283 * create and insert a path entry for the specified path name. the new path
284 * entry is inserted after the specified predecessor.
286 * returns the path entry or NULL for failure.
289 InsertPathEntry(PathEntryPtr predecessor
, STRPTR pathName
, APTR SysBase
, APTR DOSBase
)
296 newEntry
= AllocVec(sizeof(PathEntry
), MEMF_ANY
);
299 lock
= Lock(pathName
, SHARED_LOCK
);
302 isDirectory
= (BNULL
!= lock
)
303 ? IsDirectory(lock
, DOSBase
)
306 if (newEntry
!= NULL
&& lock
!= BNULL
&& isDirectory
)
308 newEntry
->lock
= lock
;
309 newEntry
->next
= predecessor
->next
;
311 predecessor
->next
= MKBADDR(newEntry
);
313 pathEntry
= newEntry
;
321 PrintFault(ERROR_OBJECT_WRONG_TYPE
, pathName
);
327 if (newEntry
!= NULL
)
338 /* check if the object specified is a directory */
340 IsDirectory(BPTR lock
, APTR DOSBase
)
345 struct FileInfoBlock
*
346 fib
= AllocDosObject(DOS_FIB
, NULL
);
348 if (Examine(lock
, fib
))
351 entryType
= fib
->fib_EntryType
;
353 if (entryType
>= ST_ROOT
&& entryType
<= ST_LINKDIR
)
355 if (entryType
!= ST_SOFTLINK
)
362 dupLock
= DupLock(lock
);
364 if (BNULL
!= dupLock
)
367 file
= OpenFromLock(dupLock
);
371 /* lock was on a file. dupLock is relinquished by OpenFromLock */
388 FreeDosObject(DOS_FIB
, fib
);