(../src/config.h): Fix error message.
[bpt/emacs.git] / lisp / url / url-parse.el
CommitLineData
8c8b8430 1;;; url-parse.el --- Uniform Resource Locator parser
ffc00a35 2
71ddfde5 3;; Copyright (C) 1996, 1997, 1998, 1999, 2004,
5b0d63bc 4;; 2005, 2006 Free Software Foundation, Inc.
ffc00a35 5
8c8b8430
SM
6;; Keywords: comm, data, processes
7
ffc00a35
SM
8;; This file is part of GNU Emacs.
9;;
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14;;
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19;;
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
4fc5845f
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
ffc00a35
SM
24
25;;; Commentary:
26
27;;; Code:
28
8c8b8430
SM
29(require 'url-vars)
30
31(autoload 'url-scheme-get-property "url-methods")
32
33(defmacro url-type (urlobj)
34 `(aref ,urlobj 0))
35
36(defmacro url-user (urlobj)
37 `(aref ,urlobj 1))
38
39(defmacro url-password (urlobj)
40 `(aref ,urlobj 2))
41
42(defmacro url-host (urlobj)
43 `(aref ,urlobj 3))
44
45(defmacro url-port (urlobj)
46 `(or (aref ,urlobj 4)
47 (if (url-fullness ,urlobj)
48 (url-scheme-get-property (url-type ,urlobj) 'default-port))))
49
50(defmacro url-filename (urlobj)
51 `(aref ,urlobj 5))
52
53(defmacro url-target (urlobj)
54 `(aref ,urlobj 6))
55
56(defmacro url-attributes (urlobj)
57 `(aref ,urlobj 7))
58
59(defmacro url-fullness (urlobj)
60 `(aref ,urlobj 8))
61
62(defmacro url-set-type (urlobj type)
63 `(aset ,urlobj 0 ,type))
64
65(defmacro url-set-user (urlobj user)
66 `(aset ,urlobj 1 ,user))
67
68(defmacro url-set-password (urlobj pass)
69 `(aset ,urlobj 2 ,pass))
70
71(defmacro url-set-host (urlobj host)
72 `(aset ,urlobj 3 ,host))
73
74(defmacro url-set-port (urlobj port)
75 `(aset ,urlobj 4 ,port))
76
77(defmacro url-set-filename (urlobj file)
78 `(aset ,urlobj 5 ,file))
79
80(defmacro url-set-target (urlobj targ)
81 `(aset ,urlobj 6 ,targ))
82
83(defmacro url-set-attributes (urlobj targ)
84 `(aset ,urlobj 7 ,targ))
85
86(defmacro url-set-full (urlobj val)
87 `(aset ,urlobj 8 ,val))
71ddfde5 88
8c8b8430
SM
89;;;###autoload
90(defun url-recreate-url (urlobj)
61bbdf64 91 "Recreate a URL string from the parsed URLOBJ."
8c8b8430
SM
92 (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "")
93 (if (url-user urlobj)
94 (concat (url-user urlobj)
95 (if (url-password urlobj)
96 (concat ":" (url-password urlobj)))
97 "@"))
98 (url-host urlobj)
99 (if (and (url-port urlobj)
100 (not (equal (url-port urlobj)
101 (url-scheme-get-property (url-type urlobj) 'default-port))))
102 (format ":%d" (url-port urlobj)))
fb7dc310
SM
103 (or (url-filename urlobj) "/")
104 (url-recreate-url-attributes urlobj)
8c8b8430 105 (if (url-target urlobj)
fb7dc310
SM
106 (concat "#" (url-target urlobj)))))
107
108(defun url-recreate-url-attributes (urlobj)
109 "Recreate the attributes of an URL string from the parsed URLOBJ."
110 (when (url-attributes urlobj)
111 (concat ";"
112 (mapconcat (lambda (x)
113 (if (cdr x)
114 (concat (car x) "=" (cdr x))
115 (car x)))
116 (url-attributes urlobj) ";"))))
8c8b8430
SM
117
118;;;###autoload
119(defun url-generic-parse-url (url)
120 "Return a vector of the parts of URL.
121Format is:
61bbdf64 122\[TYPE USER PASSWORD HOST PORT FILE TARGET ATTRIBUTES FULL\]"
8c8b8430
SM
123 (cond
124 ((null url)
125 (make-vector 9 nil))
126 ((or (not (string-match url-nonrelative-link url))
127 (= ?/ (string-to-char url)))
128 (let ((retval (make-vector 9 nil)))
129 (url-set-filename retval url)
130 (url-set-full retval nil)
131 retval))
132 (t
133 (save-excursion
134 (set-buffer (get-buffer-create " *urlparse*"))
135 (set-syntax-table url-parse-syntax-table)
136 (let ((save-pos nil)
137 (prot nil)
138 (user nil)
139 (pass nil)
140 (host nil)
141 (port nil)
142 (file nil)
143 (refs nil)
144 (attr nil)
145 (full nil)
146 (inhibit-read-only t))
147 (erase-buffer)
148 (insert url)
149 (goto-char (point-min))
150 (setq save-pos (point))
151 (if (not (looking-at "//"))
152 (progn
153 (skip-chars-forward "a-zA-Z+.\\-")
154 (downcase-region save-pos (point))
155 (setq prot (buffer-substring save-pos (point)))
156 (skip-chars-forward ":")
157 (setq save-pos (point))))
158
159 ;; We are doing a fully specified URL, with hostname and all
160 (if (looking-at "//")
161 (progn
162 (setq full t)
163 (forward-char 2)
164 (setq save-pos (point))
165 (skip-chars-forward "^/")
166 (setq host (buffer-substring save-pos (point)))
167 (if (string-match "^\\([^@]+\\)@" host)
168 (setq user (match-string 1 host)
169 host (substring host (match-end 0) nil)))
170 (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user))
171 (setq pass (match-string 2 user)
172 user (match-string 1 user)))
173 (if (string-match ":\\([0-9+]+\\)" host)
216d3806 174 (setq port (string-to-number (match-string 1 host))
8c8b8430
SM
175 host (substring host 0 (match-beginning 0))))
176 (if (string-match ":$" host)
177 (setq host (substring host 0 (match-beginning 0))))
178 (setq host (downcase host)
179 save-pos (point))))
180
181 (if (not port)
182 (setq port (url-scheme-get-property prot 'default-port)))
183
184 ;; Gross hack to preserve ';' in data URLs
185
186 (setq save-pos (point))
187
188 (if (string= "data" prot)
189 (goto-char (point-max))
190 ;; Now check for references
191 (skip-chars-forward "^#")
192 (if (eobp)
193 nil
194 (delete-region
195 (point)
196 (progn
197 (skip-chars-forward "#")
198 (setq refs (buffer-substring (point) (point-max)))
199 (point-max))))
200 (goto-char save-pos)
201 (skip-chars-forward "^;")
202 (if (not (eobp))
203 (setq attr (url-parse-args (buffer-substring (point) (point-max)) t)
204 attr (nreverse attr))))
205
206 (setq file (buffer-substring save-pos (point)))
207 (if (and host (string-match "%[0-9][0-9]" host))
208 (setq host (url-unhex-string host)))
209 (vector prot user pass host port file refs attr full))))))
210
211(provide 'url-parse)
e5566bd5 212
ffc00a35
SM
213;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403
214;;; url-parse.el ends here