1 ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
3 ;; Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Keywords: DNS master zone file SOA
8 ;; This file is part of GNU Emacs.
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)
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.
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
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; Use M-x dns-mode RET to invoke in master files.
29 ;; C-c C-s Increment SOA serial.
30 ;; Understands YYYYMMDDNN, Unix time, and serial number formats,
31 ;; and complains if it fail to find SOA serial.
33 ;; Put something similar to the following in your ~/.emacs to use this file:
35 ;; (load "~/path/to/dns-mode.el")
36 ;; (setq auto-mode-alist (cons '("\\.soa\\'" . dns-mode) auto-mode-alist))
40 ;; RFC 1034, "DOMAIN NAMES - CONCEPTS AND FACILITIES", P. Mockapetris.
41 ;; RFC 1035, "DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION", P. Mockapetris.
45 ;; 2004-09-11 Posted on gnu.emacs.sources.
46 ;; 2004-09-13 Ported to XEmacs.
47 ;; 2004-09-14 Installed in Emacs CVS.
51 (defgroup dns-mode nil
52 "DNS master file mode configuration."
55 (defconst dns-mode-classes
'("IN" "CS" "CH" "HS")
56 "List of strings with known DNS classes.")
58 (defconst dns-mode-types
'("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
59 "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
60 "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP" "NSAP"
61 "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
62 "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
63 "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
64 "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
65 "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
67 "List of strings with known DNS types.")
71 (defvar dns-mode-control-entity-face
'font-lock-keyword-face
72 "Name of face used for control entities, e.g. $ORIGIN.")
74 (defvar dns-mode-bad-control-entity-face
'font-lock-warning-face
75 "Name of face used for non-standard control entities, e.g. $FOO.")
77 (defvar dns-mode-type-face
'font-lock-type-face
78 "Name of face used for DNS types, e.g., SOA.")
80 (defvar dns-mode-class-face
'font-lock-constant-face
81 "Name of face used for DNS classes, e.g., IN.")
83 (defcustom dns-mode-font-lock-keywords
84 `(("^$ORIGIN" 0 ,dns-mode-control-entity-face
)
85 ("^$INCLUDE" 0 ,dns-mode-control-entity-face
)
86 ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face
)
87 (,(regexp-opt dns-mode-classes
) 0 ,dns-mode-class-face
)
88 (,(regexp-opt dns-mode-types
) 0 ,dns-mode-type-face
))
89 "Font lock keywords used to highlight text in DNS master file mode."
93 (defcustom dns-mode-soa-auto-increment-serial t
94 "Whether to increment the SOA serial number automatically.
96 If this variable is t, the serial number is incremented upon each save of
97 the file. If it is `ask', Emacs asks for confirmation whether it should
98 increment the serial upon saving. If nil, serials must be incremented
99 manually with \\[dns-mode-soa-increment-serial]."
100 :type
'(choice (const :tag
"Always" t
)
101 (const :tag
"Ask" ask
)
102 (const :tag
"Never" nil
))
107 (defvar dns-mode-syntax-table
108 (let ((table (make-syntax-table)))
109 (modify-syntax-entry ?\
; "< " table)
110 (modify-syntax-entry ?
\n "> " table
)
112 "Syntax table in use in DNS master file buffers.")
117 (let ((map (make-sparse-keymap)))
118 (define-key map
"\C-c\C-s" 'dns-mode-soa-increment-serial
)
120 "Keymap for DNS master file mode.")
124 (defvar dns-mode-menu nil
125 "Menubar used in DNS master file mode.")
127 (easy-menu-define dns-mode-menu dns-mode-map
130 ["Increment SOA serial" dns-mode-soa-increment-serial t
]))
135 (define-derived-mode dns-mode text-mode
"DNS"
136 "Major mode for viewing and editing DNS master files.
137 This mode is inherited from text mode. It add syntax
138 highlighting, and some commands for handling DNS master files.
139 Its keymap inherits from `text-mode' and it has the same
140 variables for customizing indentation. It has its own abbrev
141 table and its own syntax table.
143 Turning on DNS mode runs `dns-mode-hook'."
144 (set (make-local-variable 'comment-start
) ";")
145 (set (make-local-variable 'comment-end
) "")
146 (set (make-local-variable 'comment-start-skip
) ";+ *")
147 (unless (featurep 'xemacs
)
148 (set (make-local-variable 'font-lock-defaults
)
149 '(dns-mode-font-lock-keywords nil nil
((?_ .
"w")))))
150 (add-hook 'before-save-hook
'dns-mode-soa-maybe-increment-serial
152 (easy-menu-add dns-mode-menu dns-mode-map
))
154 ;;;###autoload (defalias 'zone-mode 'dns-mode)
159 (defun dns-mode-soa-increment-serial ()
160 "Locate SOA record and increment the serial field."
163 (goto-char (point-min))
164 (unless (re-search-forward
165 (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
166 "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t
)
167 (error "Cannot locate SOA record"))
168 (if (re-search-forward (concat "\\<\\("
170 "\\(198\\|199\\|20[0-9]\\)[0-9]"
172 "\\(0[0-9]\\|10\\|11\\|12\\)"
174 "\\([012][0-9]\\|30\\|31\\)"
176 "\\([0-9]\\{1,3\\}\\)"
179 ;; YYYYMMDDIII format, one to three I's.
180 (let* ((serial (match-string 1))
181 (counterstr (match-string 5))
182 (counter (string-to-number counterstr
))
183 (now (format-time-string "%Y%m%d"))
184 (nowandoldserial (concat now counterstr
)))
185 (if (string< serial nowandoldserial
)
186 (let ((new (format "%s00" now
)))
187 (replace-match new nil nil nil
1)
188 (message "Replaced old serial %s with %s" serial new
))
189 (if (string= serial nowandoldserial
)
190 (let ((new (format (format "%%s%%0%dd" (length counterstr
))
192 (replace-match new nil nil nil
1)
193 (message "Replaced old serial %s with %s" serial new
))
194 (error "Current SOA serial is in the future"))))
195 (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t
)
197 (let* ((serial (match-string 1))
198 (new (format-time-string "%s")))
199 (if (not (string< serial new
))
200 (error "Current SOA serial is in the future")
201 (replace-match new nil nil nil
1)
202 (message "Replaced old serial %s with %s" serial new
)))
203 (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t
)
204 ;; Just any serial number.
205 (let* ((serial (match-string 1))
206 (new (format "%d" (1+ (string-to-number serial
)))))
207 (replace-match new nil nil nil
1)
208 (message "Replaced old serial %s with %s" serial new
))
209 (error "Cannot locate serial number in SOA record"))))))
211 (defun dns-mode-soa-maybe-increment-serial ()
212 "Increment SOA serial if needed.
214 This function is run from `before-save-hook'."
215 (when (and (buffer-modified-p)
216 dns-mode-soa-auto-increment-serial
217 (or (eq dns-mode-soa-auto-increment-serial t
)
218 (y-or-n-p "Increment SOA serial? ")))
219 ;; If `dns-mode-soa-increment-serial' signals an error saving will
220 ;; fail but that probably means that the serial should be fixed to
221 ;; comply with the RFC anyway! -rfr
222 (progn (dns-mode-soa-increment-serial)
223 ;; We return nil in case this is used in write-contents-functions.
226 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.soa\\'" . dns-mode))
230 ;; arch-tag: 6a179f0a-072f-49db-8b01-37b8f23998c0
231 ;;; dns-mode.el ends here