Free the thread specdl. Bug introduced with c466c557f39fc71c10b16e93b484a83187780228.
[emacs.git] / lisp / textmodes / dns-mode.el
blobbb9fedad206a06e236637ef2053f37cc4fb3605a
1 ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
3 ;; Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: DNS master zone file SOA comm
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Use M-x dns-mode RET to invoke in master files.
28 ;; C-c C-s Increment SOA serial.
29 ;; Understands YYYYMMDDNN, Unix time, and serial number formats,
30 ;; and complains if it fail to find SOA serial.
32 ;; Put something similar to the following in your ~/.emacs to use this file:
34 ;; (load "~/path/to/dns-mode.el")
35 ;; (setq auto-mode-alist (cons '("\\.soa\\'" . dns-mode) auto-mode-alist))
37 ;;; References:
39 ;; RFC 1034, "DOMAIN NAMES - CONCEPTS AND FACILITIES", P. Mockapetris.
40 ;; RFC 1035, "DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION", P. Mockapetris.
42 ;;; Release history:
44 ;; 2004-09-11 Posted on gnu.emacs.sources.
45 ;; 2004-09-13 Ported to XEmacs.
46 ;; 2004-09-14 Installed in Emacs CVS.
48 ;;; Code:
50 (defgroup dns-mode nil
51 "DNS master file mode configuration."
52 :group 'data)
54 (defconst dns-mode-classes '("IN" "CS" "CH" "HS")
55 "List of strings with known DNS classes.")
57 (defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
58 "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
59 "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP" "NSAP"
60 "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
61 "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
62 "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
63 "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
64 "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
65 "MAILA")
66 "List of strings with known DNS types.")
68 ;; Font lock.
70 (defvar dns-mode-control-entity-face 'font-lock-keyword-face
71 "Name of face used for control entities, e.g. $ORIGIN.")
73 (defvar dns-mode-bad-control-entity-face 'font-lock-warning-face
74 "Name of face used for non-standard control entities, e.g. $FOO.")
76 (defvar dns-mode-type-face 'font-lock-type-face
77 "Name of face used for DNS types, e.g., SOA.")
79 (defvar dns-mode-class-face 'font-lock-constant-face
80 "Name of face used for DNS classes, e.g., IN.")
82 (defcustom dns-mode-font-lock-keywords
83 `(("^$ORIGIN" 0 ,dns-mode-control-entity-face)
84 ("^$INCLUDE" 0 ,dns-mode-control-entity-face)
85 ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
86 (,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
87 (,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
88 "Font lock keywords used to highlight text in DNS master file mode."
89 :type 'sexp
90 :group 'dns-mode)
92 (defcustom dns-mode-soa-auto-increment-serial t
93 "Whether to increment the SOA serial number automatically.
95 If this variable is t, the serial number is incremented upon each save of
96 the file. If it is `ask', Emacs asks for confirmation whether it should
97 increment the serial upon saving. If nil, serials must be incremented
98 manually with \\[dns-mode-soa-increment-serial]."
99 :type '(choice (const :tag "Always" t)
100 (const :tag "Ask" ask)
101 (const :tag "Never" nil))
102 :group 'dns-mode)
104 ;; Syntax table.
106 (defvar dns-mode-syntax-table
107 (let ((table (make-syntax-table)))
108 (modify-syntax-entry ?\; "< " table)
109 (modify-syntax-entry ?\n "> " table)
110 table)
111 "Syntax table in use in DNS master file buffers.")
113 ;; Keymap.
115 (defvar dns-mode-map
116 (let ((map (make-sparse-keymap)))
117 (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
118 map)
119 "Keymap for DNS master file mode.")
121 ;; Menu.
123 (defvar dns-mode-menu nil
124 "Menubar used in DNS master file mode.")
126 (easy-menu-define dns-mode-menu dns-mode-map
127 "DNS Menu."
128 '("DNS"
129 ["Increment SOA serial" dns-mode-soa-increment-serial t]))
131 ;; Mode.
133 ;;;###autoload
134 (define-derived-mode dns-mode text-mode "DNS"
135 "Major mode for viewing and editing DNS master files.
136 This mode is inherited from text mode. It add syntax
137 highlighting, and some commands for handling DNS master files.
138 Its keymap inherits from `text-mode' and it has the same
139 variables for customizing indentation. It has its own abbrev
140 table and its own syntax table.
142 Turning on DNS mode runs `dns-mode-hook'."
143 (set (make-local-variable 'comment-start) ";")
144 (set (make-local-variable 'comment-end) "")
145 (set (make-local-variable 'comment-start-skip) ";+ *")
146 (unless (featurep 'xemacs)
147 (set (make-local-variable 'font-lock-defaults)
148 '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
149 (add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
150 nil t)
151 (easy-menu-add dns-mode-menu dns-mode-map))
153 ;;;###autoload (defalias 'zone-mode 'dns-mode)
155 ;; Tools.
157 ;;;###autoload
158 (defun dns-mode-soa-increment-serial ()
159 "Locate SOA record and increment the serial field."
160 (interactive)
161 (save-excursion
162 (goto-char (point-min))
163 (unless (re-search-forward
164 (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
165 "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
166 (error "Cannot locate SOA record"))
167 (if (re-search-forward (concat "\\<\\("
168 ;; year
169 "\\(198\\|199\\|20[0-9]\\)[0-9]"
170 ;; month
171 "\\(0[0-9]\\|10\\|11\\|12\\)"
172 ;; day
173 "\\([012][0-9]\\|30\\|31\\)"
174 ;; counter
175 "\\([0-9]\\{1,3\\}\\)"
176 "\\)\\>")
177 nil t)
178 ;; YYYYMMDDIII format, one to three I's.
179 (let* ((serial (match-string 1))
180 (counterstr (match-string 5))
181 (counter (string-to-number counterstr))
182 (now (format-time-string "%Y%m%d"))
183 (nowandoldserial (concat now counterstr)))
184 (if (string< serial nowandoldserial)
185 (let ((new (format "%s00" now)))
186 (replace-match new nil nil nil 1)
187 (message "Replaced old serial %s with %s" serial new))
188 (if (string= serial nowandoldserial)
189 (let ((new (format (format "%%s%%0%dd" (length counterstr))
190 now (1+ counter))))
191 (replace-match new nil nil nil 1)
192 (message "Replaced old serial %s with %s" serial new))
193 (error "Current SOA serial is in the future"))))
194 (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
195 ;; Unix time
196 (let* ((serial (match-string 1))
197 (new (format-time-string "%s")))
198 (if (not (string< serial new))
199 (error "Current SOA serial is in the future")
200 (replace-match new nil nil nil 1)
201 (message "Replaced old serial %s with %s" serial new)))
202 (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
203 ;; Just any serial number.
204 (let* ((serial (match-string 1))
205 (new (format "%d" (1+ (string-to-number serial)))))
206 (replace-match new nil nil nil 1)
207 (message "Replaced old serial %s with %s" serial new))
208 (error "Cannot locate serial number in SOA record"))))))
210 (defun dns-mode-soa-maybe-increment-serial ()
211 "Increment SOA serial if needed.
213 This function is run from `before-save-hook'."
214 (when (and (buffer-modified-p)
215 dns-mode-soa-auto-increment-serial
216 (or (eq dns-mode-soa-auto-increment-serial t)
217 (y-or-n-p "Increment SOA serial? ")))
218 ;; If `dns-mode-soa-increment-serial' signals an error saving will
219 ;; fail but that probably means that the serial should be fixed to
220 ;; comply with the RFC anyway! -rfr
221 (progn (dns-mode-soa-increment-serial)
222 ;; We return nil in case this is used in write-contents-functions.
223 nil)))
225 ;;;###autoload(add-to-list 'auto-mode-alist (purecopy '("\\.soa\\'" . dns-mode)))
227 (provide 'dns-mode)
229 ;; arch-tag: 6a179f0a-072f-49db-8b01-37b8f23998c0
230 ;;; dns-mode.el ends here