1 ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
3 ;; Copyright (C) 2000, 2001, 2004, 2005, 2006 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."
95 (defvar dns-mode-syntax-table
96 (let ((table (make-syntax-table)))
97 (modify-syntax-entry ?\
; "< " table)
98 (modify-syntax-entry ?
\n "> " table
)
100 "Syntax table in use in DNS master file buffers.")
105 (let ((map (make-sparse-keymap)))
106 (define-key map
"\C-c\C-s" 'dns-mode-soa-increment-serial
)
108 "Keymap for DNS master file mode.")
112 (defvar dns-mode-menu nil
113 "Menubar used in DNS master file mode.")
115 (easy-menu-define dns-mode-menu dns-mode-map
118 ["Increment SOA serial" dns-mode-soa-increment-serial t
]))
123 (define-derived-mode dns-mode text-mode
"DNS"
124 "Major mode for viewing and editing DNS master files.
125 This mode is inherited from text mode. It add syntax
126 highlighting, and some commands for handling DNS master files.
127 Its keymap inherits from `text-mode' and it has the same
128 variables for customizing indentation. It has its own abbrev
129 table and its own syntax table.
131 Turning on DNS mode runs `dns-mode-hook'."
132 (set (make-local-variable 'comment-start
) ";")
133 (set (make-local-variable 'comment-end
) "")
134 (set (make-local-variable 'comment-start-skip
) ";+ *")
135 (unless (featurep 'xemacs
)
136 (set (make-local-variable 'font-lock-defaults
)
137 '(dns-mode-font-lock-keywords nil nil
((?_ .
"w")))))
138 (easy-menu-add dns-mode-menu dns-mode-map
))
143 (defun dns-mode-soa-increment-serial ()
144 "Locate SOA record and increment the serial field."
147 (goto-char (point-min))
148 (unless (re-search-forward
149 (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
150 "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t
)
151 (error "Cannot locate SOA record"))
152 (if (re-search-forward (concat "\\<\\("
154 "\\(198\\|199\\|20[0-9]\\)[0-9]"
156 "\\(0[0-9]\\|10\\|11\\|12\\)"
158 "\\([012][0-9]\\|30\\|31\\)"
160 "\\([0-9]\\{1,3\\}\\)"
163 ;; YYYYMMDDIII format, one to three I's.
164 (let* ((serial (match-string 1))
165 (counterstr (match-string 5))
166 (counter (string-to-number counterstr
))
167 (now (format-time-string "%Y%m%d"))
168 (nowandoldserial (concat now counterstr
)))
169 (if (string< serial nowandoldserial
)
170 (let ((new (format "%s00" now
)))
171 (replace-match new nil nil nil
1)
172 (message "Replaced old serial %s with %s" serial new
))
173 (if (string= serial nowandoldserial
)
174 (let ((new (format (format "%%s%%0%dd" (length counterstr
))
176 (replace-match new nil nil nil
1)
177 (message "Replaced old serial %s with %s" serial new
))
178 (error "Current SOA serial is in the future"))))
179 (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t
)
181 (let* ((serial (match-string 1))
182 (new (format-time-string "%s")))
183 (if (not (string< serial new
))
184 (error "Current SOA serial is in the future")
185 (replace-match new nil nil nil
1)
186 (message "Replaced old serial %s with %s" serial new
)))
187 (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t
)
188 ;; Just any serial number.
189 (let* ((serial (match-string 1))
190 (new (format "%d" (1+ (string-to-number serial
)))))
191 (replace-match new nil nil nil
1)
192 (message "Replaced old serial %s with %s" serial new
))
193 (error "Cannot locate serial number in SOA record"))))))
195 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.soa\\'" . dns-mode))
199 ;; arch-tag: 6a179f0a-072f-49db-8b01-37b8f23998c0
200 ;;; dns-mode.el ends here