1 ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
2 ;; Copyright (c) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: DNS master zone file SOA
7 ;; This file is not part of GNU Emacs.
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
14 ;; This file is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with This file; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
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))
39 ;; RFC 1034, "DOMAIN NAMES - CONCEPTS AND FACILITIES", P. Mockapetris.
40 ;; RFC 1035, "DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION", P. Mockapetris.
44 ;; 2004-09-11 posted on gnu.emacs.sources
48 (defgroup dns-mode nil
49 "DNS master file mode configuration.")
51 (defconst dns-mode-classes '("IN" "CS" "CH" "HS")
52 "List of strings with known DNS classes.")
54 (defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
55 "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
56 "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP" "NSAP"
57 "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
58 "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
59 "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
60 "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
61 "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
63 "List of strings with known DNS types.")
67 (defvar dns-mode-control-entity-face 'font-lock-keyword-face
68 "Name of face used for control entities, e.g. $ORIGIN.")
70 (defvar dns-mode-bad-control-entity-face 'font-lock-warning-face
71 "Name of face used for non-standard control entities, e.g. $FOO.")
73 (defvar dns-mode-type-face 'font-lock-type-face
74 "Name of face used for DNS types, e.g., SOA.")
76 (defvar dns-mode-class-face 'font-lock-constant-face
77 "Name of face used for DNS classes, e.g., IN.")
79 (defcustom dns-mode-font-lock-keywords
80 `(("^$ORIGIN" 0 dns-mode-control-entity-face)
81 ("^$INCLUDE" 0 dns-mode-control-entity-face)
82 ("^$[a-z0-9A-Z]+" 0 dns-mode-bad-control-entity-face)
83 (,(regexp-opt dns-mode-classes) 0 dns-mode-class-face)
84 (,(regexp-opt dns-mode-types) 0 dns-mode-type-face))
85 "Font lock keywords used to highlight text in DNS master file mode."
91 (defvar dns-mode-syntax-table
92 (let ((table (make-syntax-table)))
93 (modify-syntax-entry ?\; "< " table)
94 (modify-syntax-entry ?\n "> " table)
96 "Syntax table in use in DNS master file buffers.")
101 (let ((map (make-sparse-keymap)))
102 (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
104 "Keymap for DNS master file mode.")
108 (defvar dns-mode-menu nil
109 "Menubar used in DNS master file mode.")
111 (easy-menu-define dns-mode-menu dns-mode-map
114 ["Increment SOA serial" dns-mode-soa-increment-serial t]))
119 (define-derived-mode dns-mode text-mode "DNS"
120 "Major mode for viewing and editing DNS master files.
121 This mode is inherited from text mode. It add syntax
122 highlighting, and some commands for handling DNS master files.
123 Its keymap inherits from `text-mode' and it has the same
124 variables for customizing indentation. It has its own abbrev
125 table and its own syntax table.
127 Turning on DNS mode runs `dns-mode-hook'."
128 (set (make-local-variable 'comment-start) ";")
129 (set (make-local-variable 'comment-end) "")
130 (set (make-local-variable 'comment-start-skip) ";+ *")
131 (unless (featurep 'xemacs)
132 (set (make-local-variable 'font-lock-defaults)
133 '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
134 (easy-menu-add-item nil nil dns-mode-menu))
139 (defun dns-mode-soa-increment-serial ()
140 "Locate SOA record and increment the serial field."
143 (goto-char (point-min))
144 (unless (re-search-forward
145 (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
146 "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
147 (error "Cannot locate SOA record"))
148 (if (re-search-forward (concat "\\<\\("
150 "\\(198\\|199\\|20[0-9]\\)[0-9]"
152 "\\(0[0-9]\\|10\\|11\\|12\\)"
154 "\\([012][0-9]\\|30\\|31\\)"
156 "\\([0-9]\\{1,3\\}\\)"
159 ;; YYYYMMDDIII format, one to three I's.
160 (let* ((serial (match-string 1))
161 (counterstr (match-string 5))
162 (counter (string-to-number counterstr))
163 (now (format-time-string "%Y%m%d"))
164 (nowandoldserial (concat now counterstr)))
165 (if (string< serial nowandoldserial)
166 (let ((new (format "%s00" now)))
167 (replace-match new nil nil nil 1)
168 (message "Replaced old serial %s with %s" serial new))
169 (if (string= serial nowandoldserial)
170 (let ((new (format (format "%%s%%0%dd" (length counterstr))
172 (replace-match new nil nil nil 1)
173 (message "Replaced old serial %s with %s" serial new))
174 (error "Current SOA serial is in the future"))))
175 (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
177 (let* ((serial (match-string 1))
178 (new (format-time-string "%s")))
179 (if (not (string< serial new))
180 (error "Current SOA serial is in the future")
181 (replace-match new nil nil nil 1)
182 (message "Replaced old serial %s with %s" serial new)))
183 (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
184 ;; Just any serial number.
185 (let* ((serial (match-string 1))
186 (new (format "%d" (1+ (string-to-number serial)))))
187 (replace-match new nil nil nil 1)
188 (message "Replaced old serial %s with %s" serial new))
189 (error "Cannot locate serial number in SOA record"))))))
193 ;;; dns-mode.el ends here