(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / net / netrc.el
blob713c036ce7b509498f96774a27435d3b381206ab
1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7 ;; Modularized by Ted Zlatanov <tzz@lifelogs.com>
8 ;; when it was part of Gnus.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Just the .netrc parsing functionality, abstracted so other packages
30 ;; besides Gnus can use it.
32 ;;; Code:
34 ;;;
35 ;;; .netrc and .authinforc parsing
36 ;;;
38 (defalias 'netrc-point-at-eol
39 (if (fboundp 'point-at-eol)
40 'point-at-eol
41 'line-end-position))
43 (defun netrc-parse (file)
44 "Parse FILE and return a list of all entries in the file."
45 (when (file-exists-p file)
46 (with-temp-buffer
47 (let ((tokens '("machine" "default" "login"
48 "password" "account" "macdef" "force"
49 "port"))
50 alist elem result pair)
51 (insert-file-contents file)
52 (goto-char (point-min))
53 ;; Go through the file, line by line.
54 (while (not (eobp))
55 (narrow-to-region (point) (netrc-point-at-eol))
56 ;; For each line, get the tokens and values.
57 (while (not (eobp))
58 (skip-chars-forward "\t ")
59 ;; Skip lines that begin with a "#".
60 (if (eq (char-after) ?#)
61 (goto-char (point-max))
62 (unless (eobp)
63 (setq elem
64 (if (= (following-char) ?\")
65 (read (current-buffer))
66 (buffer-substring
67 (point) (progn (skip-chars-forward "^\t ")
68 (point)))))
69 (cond
70 ((equal elem "macdef")
71 ;; We skip past the macro definition.
72 (widen)
73 (while (and (zerop (forward-line 1))
74 (looking-at "$")))
75 (narrow-to-region (point) (point)))
76 ((member elem tokens)
77 ;; Tokens that don't have a following value are ignored,
78 ;; except "default".
79 (when (and pair (or (cdr pair)
80 (equal (car pair) "default")))
81 (push pair alist))
82 (setq pair (list elem)))
84 ;; Values that haven't got a preceding token are ignored.
85 (when pair
86 (setcdr pair elem)
87 (push pair alist)
88 (setq pair nil)))))))
89 (when alist
90 (push (nreverse alist) result))
91 (setq alist nil
92 pair nil)
93 (widen)
94 (forward-line 1))
95 (nreverse result)))))
97 (defun netrc-machine (list machine &optional port defaultport)
98 "Return the netrc values from LIST for MACHINE or for the default entry.
99 If PORT specified, only return entries with matching port tokens.
100 Entries without port tokens default to DEFAULTPORT."
101 (let ((rest list)
102 result)
103 (while list
104 (when (equal (cdr (assoc "machine" (car list))) machine)
105 (push (car list) result))
106 (pop list))
107 (unless result
108 ;; No machine name matches, so we look for default entries.
109 (while rest
110 (when (assoc "default" (car rest))
111 (push (car rest) result))
112 (pop rest)))
113 (when result
114 (setq result (nreverse result))
115 (while (and result
116 (not (equal (or port defaultport "nntp")
117 (or (netrc-get (car result) "port")
118 defaultport "nntp"))))
119 (pop result))
120 (car result))))
122 (defun netrc-get (alist type)
123 "Return the value of token TYPE from ALIST."
124 (cdr (assoc type alist)))
126 (provide 'netrc)
128 ;;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55
129 ;;; netrc.el ends here