2 Copyright © 1995-2001, 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 #define SH_GLOBAL_DOSBASE 1
64 #include <aros/shcommands.h>
66 #include <aros/debug.h>
68 typedef struct CommandLineInterface
* CommandLineInterfacePtr
;
74 } PathEntry
, *PathEntryPtr
;
76 #define PE(x) ((PathEntry *)(BADDR(x)))
78 static PathEntryPtr
FindPathEntry(CommandLineInterfacePtr cli
, STRPTR pathName
,
79 PathEntryPtr
* predStorage
);
81 static PathEntryPtr
InsertPathEntry(PathEntryPtr predecessor
, STRPTR pathName
);
83 static BOOL
IsDirectory(BPTR lock
);
86 AROS_SHA(STRPTR
*, ,PATH
,/M
,NULL
),
87 AROS_SHA(BOOL
, ,ADD
,/S
,NULL
),
88 AROS_SHA(BOOL
, ,SHOW
,/S
,NULL
),
89 AROS_SHA(BOOL
, ,RESET
,/S
,NULL
),
90 AROS_SHA(BOOL
, ,REMOVE
,/S
,NULL
),
91 AROS_SHA(BOOL
, ,QUIET
,/S
,NULL
),
92 AROS_SHA(BOOL
, ,HEAD
,/S
,NULL
))
96 CommandLineInterfacePtr
101 PrintFault(ERROR_SCRIPT_ONLY
, "Path");
109 cur
= PE(cli
->cli_CommandDir
);
114 next
= PE(cur
->next
);
123 cli
->cli_CommandDir
= NULL
;
134 if (SHArg(REMOVE
) && !SHArg(RESET
))
140 for (idx
= 0; names
[idx
]; ++idx
)
142 entry
= FindPathEntry(cli
, names
[idx
], &pred
);
144 /* free the path entry */
149 pred
->next
= entry
->next
;
153 cli
->cli_CommandDir
= entry
->next
;
156 if (NULL
!= entry
->lock
)
168 insertAfter
= &cli
->cli_CommandDir
;
172 /* Search last entry */
173 while (NULL
!= insertAfter
->next
)
175 insertAfter
= PE(insertAfter
->next
);
179 for (idx
= 0; names
[idx
]; ++idx
)
181 if (NULL
!= FindPathEntry(cli
, names
[idx
], NULL
))
183 /* don't add if already in path */
188 insertAfter
= InsertPathEntry(insertAfter
, names
[idx
]);
195 SHArg(SHOW
) = SHArg(RESET
) == NULL
;
202 PutStr("Current Directory\n");
207 for (cur
= PE(cli
->cli_CommandDir
); cur
; cur
= PE(cur
->next
))
209 NameFromLock (cur
->lock
, Buffer
, sizeof (Buffer
));
224 /** find the specfied path name in the specified CLI's path list.
226 * @autodoc:function if the path specified by pathName is in the path list of
227 * the specified CLI the corresponding path entry is returned. the path entry's
228 * predecessor is stored in the specified location if the storage pointer is
233 FindPathEntry(CommandLineInterfacePtr cli
, STRPTR pathName
,
234 PathEntryPtr
* predStorage
)
239 if (NULL
!= cli
&& NULL
!= cli
->cli_CommandDir
&& NULL
!= pathName
)
242 pathLock
= Lock(pathName
, ACCESS_READ
);
244 if (NULL
!= pathLock
)
248 curr
= PE(cli
->cli_CommandDir
);
253 value
= SameLock(pathLock
, curr
->lock
);
255 if (LOCK_SAME
== value
)
259 if (NULL
!= predStorage
)
268 curr
= PE(curr
->next
);
280 /* insert a path entry for the specified path
282 * create and insert a path entry for the specified path name. the new path
283 * entry is inserted after the specified predecessor.
285 * returns the path entry or NULL for failure.
288 InsertPathEntry(PathEntryPtr predecessor
, STRPTR pathName
)
295 newEntry
= AllocVec(sizeof(PathEntry
), MEMF_ANY
);
298 lock
= Lock(pathName
, SHARED_LOCK
);
301 isDirectory
= (NULL
!= lock
)
305 if (newEntry
!= NULL
&& lock
!= NULL
&& isDirectory
)
307 newEntry
->lock
= lock
;
308 newEntry
->next
= predecessor
->next
;
310 predecessor
->next
= MKBADDR(newEntry
);
312 pathEntry
= newEntry
;
318 PrintFault(IoErr(), pathName
);
324 PrintFault(ERROR_OBJECT_WRONG_TYPE
, pathName
);
330 if (newEntry
!= NULL
)
341 /* check if the object specified is a directory */
343 IsDirectory(BPTR lock
)
348 struct FileInfoBlock
*
349 fib
= AllocDosObject(DOS_FIB
, NULL
);
351 if (Examine(lock
, fib
))
354 entryType
= fib
->fib_EntryType
;
356 if (entryType
>= ST_ROOT
&& entryType
<= ST_LINKDIR
)
358 if (entryType
!= ST_SOFTLINK
)
365 dupLock
= DupLock(lock
);
370 file
= OpenFromLock(dupLock
);
374 /* lock was on a file. dupLock is relinquished by OpenFromLock */
391 FreeDosObject(DOS_FIB
, fib
);