Commit | Line | Data |
---|---|---|
0904599e | 1 | ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files |
3731a850 | 2 | |
ceb4c4d3 | 3 | ;; Copyright (C) 2000, 2001, 2004, 2005, 2006 Free Software Foundation, Inc. |
0904599e SJ |
4 | |
5 | ;; Author: Simon Josefsson <simon@josefsson.org> | |
6 | ;; Keywords: DNS master zone file SOA | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
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) | |
13 | ;; any later version. | |
14 | ||
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. | |
19 | ||
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 | |
4fc5845f LK |
22 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 | ;; Boston, MA 02110-1301, USA. | |
0904599e SJ |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; Use M-x dns-mode RET to invoke in master files. | |
28 | ;; | |
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. | |
32 | ;; | |
33 | ;; Put something similar to the following in your ~/.emacs to use this file: | |
34 | ;; | |
35 | ;; (load "~/path/to/dns-mode.el") | |
36 | ;; (setq auto-mode-alist (cons '("\\.soa\\'" . dns-mode) auto-mode-alist)) | |
37 | ||
38 | ;;; References: | |
39 | ||
40 | ;; RFC 1034, "DOMAIN NAMES - CONCEPTS AND FACILITIES", P. Mockapetris. | |
41 | ;; RFC 1035, "DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION", P. Mockapetris. | |
42 | ||
43 | ;;; Release history: | |
44 | ||
45 | ;; 2004-09-11 Posted on gnu.emacs.sources. | |
46 | ;; 2004-09-13 Ported to XEmacs. | |
47 | ;; 2004-09-14 Installed in Emacs CVS. | |
48 | ||
49 | ;;; Code: | |
50 | ||
51 | (defgroup dns-mode nil | |
55d56d32 LK |
52 | "DNS master file mode configuration." |
53 | :group 'data) | |
0904599e SJ |
54 | |
55 | (defconst dns-mode-classes '("IN" "CS" "CH" "HS") | |
56 | "List of strings with known DNS classes.") | |
57 | ||
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" | |
66 | "MAILA") | |
67 | "List of strings with known DNS types.") | |
68 | ||
69 | ;; Font lock. | |
70 | ||
71 | (defvar dns-mode-control-entity-face 'font-lock-keyword-face | |
72 | "Name of face used for control entities, e.g. $ORIGIN.") | |
73 | ||
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.") | |
76 | ||
77 | (defvar dns-mode-type-face 'font-lock-type-face | |
78 | "Name of face used for DNS types, e.g., SOA.") | |
79 | ||
80 | (defvar dns-mode-class-face 'font-lock-constant-face | |
81 | "Name of face used for DNS classes, e.g., IN.") | |
82 | ||
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." | |
90 | :type 'sexp | |
91 | :group 'dns-mode) | |
92 | ||
93 | ;; Syntax table. | |
94 | ||
95 | (defvar dns-mode-syntax-table | |
96 | (let ((table (make-syntax-table))) | |
97 | (modify-syntax-entry ?\; "< " table) | |
98 | (modify-syntax-entry ?\n "> " table) | |
99 | table) | |
100 | "Syntax table in use in DNS master file buffers.") | |
101 | ||
102 | ;; Keymap. | |
103 | ||
104 | (defvar dns-mode-map | |
105 | (let ((map (make-sparse-keymap))) | |
106 | (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial) | |
107 | map) | |
108 | "Keymap for DNS master file mode.") | |
109 | ||
110 | ;; Menu. | |
111 | ||
112 | (defvar dns-mode-menu nil | |
113 | "Menubar used in DNS master file mode.") | |
114 | ||
115 | (easy-menu-define dns-mode-menu dns-mode-map | |
116 | "DNS Menu." | |
117 | '("DNS" | |
118 | ["Increment SOA serial" dns-mode-soa-increment-serial t])) | |
119 | ||
120 | ;; Mode. | |
121 | ||
122 | ;;;###autoload | |
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. | |
130 | ||
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)) | |
139 | ||
140 | ;; Tools. | |
141 | ||
142 | ;;;###autoload | |
143 | (defun dns-mode-soa-increment-serial () | |
144 | "Locate SOA record and increment the serial field." | |
145 | (interactive) | |
146 | (save-excursion | |
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 "\\<\\(" | |
153 | ;; year | |
154 | "\\(198\\|199\\|20[0-9]\\)[0-9]" | |
155 | ;; month | |
156 | "\\(0[0-9]\\|10\\|11\\|12\\)" | |
157 | ;; day | |
158 | "\\([012][0-9]\\|30\\|31\\)" | |
159 | ;; counter | |
160 | "\\([0-9]\\{1,3\\}\\)" | |
161 | "\\)\\>") | |
162 | nil t) | |
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)) | |
175 | now (1+ counter)))) | |
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) | |
180 | ;; Unix time | |
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")))))) | |
194 | ||
195 | ;;;###autoload(add-to-list 'auto-mode-alist '("\\.soa\\'" . dns-mode)) | |
196 | ||
197 | (provide 'dns-mode) | |
198 | ||
199 | ;; arch-tag: 6a179f0a-072f-49db-8b01-37b8f23998c0 | |
200 | ;;; dns-mode.el ends here |