Function: netrc-parse

netrc-parse is an interactive and byte-compiled function defined in netrc.el.gz.

Signature

(netrc-parse &optional FILE)

Documentation

Parse FILE and return a list of all entries in the file.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/net/netrc.el.gz
(defun netrc-parse (&optional file)
  "Parse FILE and return a list of all entries in the file."
  (interactive "fFile to Parse: ")
  (unless file
    (setq file netrc-file))
  (if (listp file)
      ;; We got already parsed contents; just return it.
      file
    (when (file-exists-p file)
      (with-temp-buffer
	(let ((tokens '("machine" "default" "login"
			"password" "account" "macdef" "force"
			"port"))
	      alist elem result pair)
          (if (and netrc-cache
		   (equal (car netrc-cache) (file-attribute-modification-time
                                             (file-attributes file))))
	      (insert (base64-decode-string (rot13-string (cdr netrc-cache))))
	    (insert-file-contents file)
	    (when (string-match "\\.gpg\\'" file)
	      ;; Store the contents of the file heavily encrypted in memory.
	      (setq netrc-cache (cons (file-attribute-modification-time
                                       (file-attributes file))
				      (rot13-string
				       (base64-encode-string
					(buffer-string)))))))
	  (goto-char (point-min))
	  ;; Go through the file, line by line.
	  (while (not (eobp))
	    (narrow-to-region (point) (point-at-eol))
	    ;; For each line, get the tokens and values.
	    (while (not (eobp))
	      (skip-chars-forward "\t ")
	      ;; Skip lines that begin with a "#".
	      (if (eq (char-after) ?#)
		  (goto-char (point-max))
		(unless (eobp)
		  (setq elem
			(if (= (following-char) ?\")
			    (read (current-buffer))
			  (buffer-substring
			   (point) (progn (skip-chars-forward "^\t ")
					  (point)))))
		  (cond
		   ((equal elem "macdef")
		    ;; We skip past the macro definition.
		    (widen)
		    (while (and (zerop (forward-line 1))
				(looking-at "$")))
		    (narrow-to-region (point) (point)))
		   ((member elem tokens)
		    ;; Tokens that don't have a following value are ignored,
		    ;; except "default".
		    (when (and pair (or (cdr pair)
					(equal (car pair) "default")))
		      (push pair alist))
		    (setq pair (list elem)))
		   (t
		    ;; Values that haven't got a preceding token are ignored.
		    (when pair
		      (setcdr pair elem)
		      (push pair alist)
		      (setq pair nil)))))))
	    (when alist
	      (push (nreverse alist) result))
	    (setq alist nil
		  pair nil)
	    (widen)
	    (forward-line 1))
	  (nreverse result))))))