| 1 | ;;; directory.lisp |
|---|
| 2 | ;;; |
|---|
| 3 | ;;; Copyright (C) 2004-2007 Peter Graves |
|---|
| 4 | ;;; Copyright (C) 2008 Ville Voutilainen |
|---|
| 5 | ;;; $Id: directory.lisp 12503 2010-02-22 16:32:45Z mevenson $ |
|---|
| 6 | ;;; |
|---|
| 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. |
|---|
| 11 | ;;; |
|---|
| 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. |
|---|
| 16 | ;;; |
|---|
| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 20 | ;;; |
|---|
| 21 | ;;; As a special exception, the copyright holders of this library give you |
|---|
| 22 | ;;; permission to link this library with independent modules to produce an |
|---|
| 23 | ;;; executable, regardless of the license terms of these independent |
|---|
| 24 | ;;; modules, and to copy and distribute the resulting executable under |
|---|
| 25 | ;;; terms of your choice, provided that you also meet, for each linked |
|---|
| 26 | ;;; independent module, the terms and conditions of the license of that |
|---|
| 27 | ;;; module. An independent module is a module which is not derived from |
|---|
| 28 | ;;; or based on this library. If you modify this library, you may extend |
|---|
| 29 | ;;; this exception to your version of the library, but you are not |
|---|
| 30 | ;;; obligated to do so. If you do not wish to do so, delete this |
|---|
| 31 | ;;; exception statement from your version. |
|---|
| 32 | |
|---|
| 33 | (in-package "SYSTEM") |
|---|
| 34 | |
|---|
| 35 | (defun pathname-as-file (pathname) |
|---|
| 36 | (let ((directory (pathname-directory pathname))) |
|---|
| 37 | (make-pathname :host nil |
|---|
| 38 | :device (pathname-device pathname) |
|---|
| 39 | :directory (butlast directory) |
|---|
| 40 | :name (car (last directory)) |
|---|
| 41 | :type nil |
|---|
| 42 | :version nil))) |
|---|
| 43 | |
|---|
| 44 | (defun list-directories-with-wildcards (pathname) |
|---|
| 45 | (let* ((directory (pathname-directory pathname)) |
|---|
| 46 | (first-wild (position-if #'wild-p directory)) |
|---|
| 47 | (wild (when first-wild (nthcdr first-wild directory))) |
|---|
| 48 | (non-wild (if first-wild |
|---|
| 49 | (nbutlast directory |
|---|
| 50 | (- (length directory) first-wild)) |
|---|
| 51 | directory)) |
|---|
| 52 | (newpath (make-pathname :directory non-wild |
|---|
| 53 | :name nil :type nil :defaults pathname)) |
|---|
| 54 | (entries (list-directory newpath))) |
|---|
| 55 | (if (not wild) |
|---|
| 56 | entries |
|---|
| 57 | (mapcan (lambda (entry) |
|---|
| 58 | (let* ((pathname (pathname entry)) |
|---|
| 59 | (directory (pathname-directory pathname)) |
|---|
| 60 | (rest-wild (cdr wild))) |
|---|
| 61 | (unless (pathname-name pathname) |
|---|
| 62 | (when (pathname-match-p (first (last directory)) |
|---|
| 63 | (if (eql (car wild) :wild) "*" (car wild))) |
|---|
| 64 | (when rest-wild |
|---|
| 65 | (setf directory (nconc directory rest-wild))) |
|---|
| 66 | (list-directories-with-wildcards |
|---|
| 67 | (make-pathname :directory directory |
|---|
| 68 | :defaults newpath)))))) |
|---|
| 69 | entries)))) |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | (defun directory (pathspec &key) |
|---|
| 73 | (let ((pathname (merge-pathnames pathspec))) |
|---|
| 74 | (when (logical-pathname-p pathname) |
|---|
| 75 | (setq pathname (translate-logical-pathname pathname))) |
|---|
| 76 | (if (or (position #\* (namestring pathname)) |
|---|
| 77 | (wild-pathname-p pathname)) |
|---|
| 78 | (if (pathname-jar-p pathname) |
|---|
| 79 | (match-wild-jar-pathname pathname) |
|---|
| 80 | (let ((namestring (directory-namestring pathname))) |
|---|
| 81 | (when (and namestring (> (length namestring) 0)) |
|---|
| 82 | (when (featurep :windows) |
|---|
| 83 | (let ((device (pathname-device pathname))) |
|---|
| 84 | (when device |
|---|
| 85 | (setq namestring (concatenate 'string device ":" namestring))))) |
|---|
| 86 | (let ((entries (list-directories-with-wildcards namestring)) |
|---|
| 87 | (matching-entries ())) |
|---|
| 88 | (dolist (entry entries) |
|---|
| 89 | (cond ((file-directory-p entry) |
|---|
| 90 | (when (pathname-match-p (file-namestring (pathname-as-file entry)) (file-namestring pathname)) |
|---|
| 91 | (push entry matching-entries))) |
|---|
| 92 | ((pathname-match-p (file-namestring entry) (file-namestring pathname)) |
|---|
| 93 | (push entry matching-entries)))) |
|---|
| 94 | matching-entries)))) |
|---|
| 95 | ;; Not wild. |
|---|
| 96 | (let ((truename (probe-file pathname))) |
|---|
| 97 | (if truename |
|---|
| 98 | (list (pathname truename)) |
|---|
| 99 | nil))))) |
|---|