summaryrefslogtreecommitdiff
path: root/lisp/net/snmp-mode.el
blob: 0ce0b91c037ac61f8359825f621dad20c074473e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
;;; snmp-mode.el --- SNMP & SNMPv2 MIB major mode

;; Copyright (C) 1995, 1998, 2001, 2002, 2003, 2004,
;;   2005, 2006, 2007 Free Software Foundation, Inc.

;; Author: Paul D. Smith <psmith@BayNetworks.com>
;; Keywords: data

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; INTRODUCTION
;; ------------
;; This package provides a major mode for editing SNMP MIBs.  It
;; provides all the modern Emacs 19 bells and whistles: default
;; fontification via font-lock, imenu search functions, etc.
;;
;; SNMP mode also uses tempo, a textual boilerplate insertion package
;; distributed with Emacs, to add in boilerplate SNMP MIB structures.
;; See tempo.el for more details about tempo.
;;
;; If you want to change or add new tempo templates, use the tempo tag
;; list `snmp-tempo-tags' (or `snmpv2-tempo-tags'): this list is
;; automatically installed when snmp-mode (or snmpv2-mode) is entered.
;;
;; The SNMPv2 mode in this version has been enhanced thanks to popular
;; demand.
;;
;; I'm very interested in new tempo macros for both v1 and v2, and any
;; other suggestions for enhancements (different syntax table items, new
;; keybindings, etc.)
;;
;;
;; USAGE
;; -----
;; Mostly, use it as you would any other mode.  There's a very
;; simplistic auto-indent feature; hopefully it'll help more than get in
;; your way.  For the most part it tries to indent to the same level as
;; the previous line.  It will try to recognize some very simple tokens
;; on the previous line that tell it to use extra indent or outdent.
;;
;; Templates
;; ---------
;; To use the Tempo templates, type the Tempo tag (or a unique prefix)
;; and use C-c C-i (C-c TAB) to complete it; if you don't have
;; tempo-interactive set to nil it will ask you to fill in values.
;; Fields with predefined values (SYNTAX, STATUS, etc.) will do
;; completing-reads on a list of valid values; use the normal SPC or TAB
;; to complete.
;;
;; Currently the following templates are available:
;;
;;  objectType -- Defines an OBJECT-TYPE macro.
;;
;;  tableType  -- Defines both a Table and Entry OBJECT-TYPE, and a
;;                SEQUENCE for the ASN.1 Entry definition.
;;
;; Once the template is done, you can use C-cC-f and C-cC-b to move back
;; and forth between the Tempo sequence points to fill in the rest of
;; the information.
;;
;; Font Lock
;; ------------
;;
;; If you want font-lock in your MIB buffers, add this:
;;
;;  (add-hook 'snmp-common-mode-hook 'turn-on-font-lock)
;;
;; Enabling global-font-lock-mode is also sufficient.
;;

;;; Code:

(eval-when-compile
  (require 'cl)
  (require 'imenu)   ; Need this stuff when compiling for imenu macros, etc.
  (require 'tempo))

;;;----------------------------------------------------------------------------
;;
;;                          Customize these:
;;
;;;----------------------------------------------------------------------------

(defgroup snmp nil
  "Mode for editing SNMP MIB files."
  :group 'data
  :version "20.4")

(defcustom snmp-special-indent t
  "If non-nil, use a simple heuristic to try to guess the right indentation.
If nil, then no special indentation is attempted."
  :type 'boolean
  :group 'snmp)

(defcustom snmp-indent-level 4
  "Indentation level for SNMP MIBs."
  :type 'integer
  :group 'snmp)

(defcustom snmp-tab-always-indent nil
  "Non-nil means TAB should always reindent the current line.
A value of nil means reindent if point is within the initial line indentation;
otherwise insert a TAB."
  :type 'boolean
  :group 'snmp)

(defcustom snmp-completion-ignore-case t
  "Non-nil means that case differences are ignored during completion.
A value of nil means that case is significant.
This is used during Tempo template completion."
  :type 'boolean
  :group 'snmp)

(defcustom snmp-common-mode-hook nil
  "Hook(s) evaluated when a buffer enters either SNMP or SNMPv2 mode."
  :type 'hook
  :group 'snmp)

(defcustom snmp-mode-hook nil
  "Hook(s) evaluated when a buffer enters SNMP mode."
  :type 'hook
  :group 'snmp)

(defcustom snmpv2-mode-hook nil
  "Hook(s) evaluated when a buffer enters SNMPv2 mode."
  :type 'hook
  :group 'snmp)

(defvar snmp-tempo-tags nil
  "*Tempo tags for SNMP mode.")

(defvar snmpv2-tempo-tags nil
  "*Tempo tags for SNMPv2 mode.")


;; Enable fontification for SNMP MIBs
;;

;; These are pretty basic fontifications.  Note we assume these macros
;; are first on a line (except whitespace), to speed up fontification.
;;
(defvar snmp-font-lock-keywords-1
  (list
   ;; OBJECT-TYPE, TRAP-TYPE, and OBJECT-IDENTIFIER macros
   '("^[ \t]*\\([a-z][-a-zA-Z0-9]+\\)[ \t]+\\(\\(MODULE-\\(COMPLIANCE\\|IDENTITY\\)\\|OBJECT-\\(COMPLIANCE\\|GROUP\\|IDENTITY\\|TYPE\\)\\|TRAP-\\(GROUP\\|TYPE\\)\\)\\|\\(OBJECT\\)[ \t]+\\(IDENTIFIER\\)[ \t]*::=\\)"
     (1 font-lock-variable-name-face) (3 font-lock-keyword-face nil t)
     (7 font-lock-keyword-face nil t) (8 font-lock-keyword-face nil t))

   ;; DEFINITIONS clause
   '("^[ \t]*\\([A-Z][-a-zA-Z0-9]+\\)[ \t]+\\(DEFINITIONS\\)[ \t]*::="
     (1 font-lock-function-name-face) (2 font-lock-keyword-face))
   )
  "Basic SNMP MIB mode expression highlighting.")

(defvar snmp-font-lock-keywords-2
  (append
   '(("ACCESS\\|BEGIN\\|DE\\(FVAL\\|SCRIPTION\\)\\|END\\|FROM\\|I\\(MPORTS\\|NDEX\\)\\|S\\(TATUS\\|YNTAX\\)"
      (0 font-lock-keyword-face)))
   snmp-font-lock-keywords-1)
  "Medium SNMP MIB mode expression highlighting.")

(defvar snmp-font-lock-keywords-3
  (append
   '(("\\([^\n]+\\)[ \t]+::=[ \t]+\\(SEQUENCE\\)[ \t]+{"
      (1 font-lock-reference-face) (2 font-lock-keyword-face))
     ("::=[ \t]*{[ \t]*\\([a-z0-9].*[ \t]+\\)?\\([0-9]+\\)[ \t]*}"
      (1 font-lock-reference-face nil t) (2 font-lock-variable-name-face)))
   snmp-font-lock-keywords-2)
  "Gaudy SNMP MIB mode expression highlighting.")

(defvar snmp-font-lock-keywords snmp-font-lock-keywords-1
  "Default SNMP MIB mode expression highlighting.")


;; These lists are used for the completion capabilities in the tempo
;; templates.
;;

(defvar snmp-mode-syntax-list nil
  "Predefined types for SYNTAX clauses.")

(defvar snmp-rfc1155-types
  '("INTEGER" "OCTET STRING" "OBJECT IDENTIFIER" "NULL" "IpAddress"
    "NetworkAddress" "Counter" "Gauge" "TimeTicks" "Opaque")
  "Types from RFC 1155 v1 SMI.")

(defvar snmp-rfc1213-types
  '("DisplayString")
  "Types from RFC 1213 MIB-II.")

(defvar snmp-rfc1902-types
  '("INTEGER" "OCTET STRING" "OBJECT IDENTIFIER" "Integer32"
    "IpAddress" "Counter32" "Gauge32" "Unsigned32" "TimeTicks"
    "Opaque" "Counter64")
  "Types from RFC 1902 v2 SMI.")

(defvar snmp-rfc1903-types
  '("DisplayString" "PhysAddress" "MacAddress" "TruthValue"
    "TestAndIncr" "AutonomousType" "InstancePointer"
    "VariablePointer" "RowPointer" "RowStatus" "TimeStamp"
    "TimeInterval" "DateAndTime" "StorageType" "TDomain"
    "TAddress")
  "Types from RFC 1903 Textual Conventions.")


(defvar snmp-mode-access-list nil
  "Predefined values for ACCESS clauses.")

(defvar snmp-rfc1155-access
  '("read-only" "read-write" "write-only" "not-accessible")
  "ACCESS values from RFC 1155 v1 SMI.")

(defvar snmp-rfc1902-access
  '("read-only" "read-write" "read-create" "not-accessible"
    "accessible-for-notify")
  "ACCESS values from RFC 1155 v1 SMI.")


(defvar snmp-mode-status-list nil
  "Predefined values for STATUS clauses.")

(defvar snmp-rfc1212-status
  '("mandatory" "obsolete" "deprecated")
  "STATUS values from RFC 1212 v1 SMI.")

(defvar snmp-rfc1902-status
  '("current" "obsolete" "deprecated")
  "STATUS values from RFC 1902 v2 SMI.")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;----------------------------------------------------------------------------
;;
;;                  Nothing to customize below here.
;;
;;;----------------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;; Create abbrev table for SNMP MIB mode
;;
(defvar snmp-mode-abbrev-table nil
  "Abbrev table in use in SNMP mode.")
(define-abbrev-table 'snmp-mode-abbrev-table ())


;; Create abbrev table for SNMPv2 mode
;;
(defvar snmpv2-mode-abbrev-table nil
  "Abbrev table in use in SNMPv2 mode.")
(define-abbrev-table 'snmpv2-mode-abbrev-table ())


;; Set up our keymap
;;
(defvar snmp-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\177"         'backward-delete-char-untabify)
    (define-key map "\C-c\C-i"     'tempo-complete-tag)
    (define-key map "\C-c\C-f"     'tempo-forward-mark)
    (define-key map "\C-c\C-b"     'tempo-backward-mark)
    map)
  "Keymap used in SNMP mode.")


;; Set up our syntax table
;;
(defvar snmp-mode-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?\\  "\\"     st)
    (modify-syntax-entry ?-   "_ 1234" st)
    (modify-syntax-entry ?\n  ">"      st)
    (modify-syntax-entry ?\^m ">"      st)
    (modify-syntax-entry ?_   "."      st)
    (modify-syntax-entry ?:   "."      st)
    (modify-syntax-entry ?=   "."      st)
    st)
  "Syntax table used for buffers in SNMP mode.")

;; Set up the stuff that's common between snmp-mode and snmpv2-mode
;;
(defun snmp-common-mode (name mode abbrev font-keywords imenu-index tempo-tags)
  (kill-all-local-variables)

  ;; Become the current major mode
  (setq mode-name name)
  (setq major-mode mode)

  ;; Activate keymap, syntax table, and abbrev table
  (use-local-map snmp-mode-map)
  (set-syntax-table snmp-mode-syntax-table)
  (setq local-abbrev-table abbrev)

  ;; Set up paragraphs (?)
  (make-local-variable 'paragraph-start)
  (setq paragraph-start (concat "$\\|" page-delimiter))
  (make-local-variable 'paragraph-separate)
  (setq paragraph-separate paragraph-start)
  (make-local-variable 'paragraph-ignore-fill-prefix)
  (setq paragraph-ignore-fill-prefix t)

  ;; Set up comments
  (make-local-variable 'comment-start)
  (setq comment-start "-- ")
  (make-local-variable 'comment-start-skip)
  (setq comment-start-skip "--+[ \t]*")
  (make-local-variable 'comment-column)
  (setq comment-column 40)
  (make-local-variable 'parse-sexp-ignore-comments)
  (setq parse-sexp-ignore-comments t)

  ;; Set up indentation
  (if snmp-special-indent
      (set (make-local-variable 'indent-line-function) 'snmp-indent-line))
  (set (make-local-variable 'tab-always-indent) snmp-tab-always-indent)

  ;; Font Lock
  (make-local-variable 'font-lock-defaults)
  (setq font-lock-defaults (cons font-keywords '(nil nil ((?- . "w 1234")))))

  ;; Imenu
  (make-local-variable 'imenu-create-index-function)
  (setq imenu-create-index-function imenu-index)

  ;; Tempo
  (tempo-use-tag-list tempo-tags)
  (make-local-variable 'tempo-match-finder)
  (setq tempo-match-finder "\\b\\(.+\\)\\=")
  (make-local-variable 'tempo-interactive)
  (setq tempo-interactive t)

  ;; Miscellaneous customization
  (make-local-variable 'require-final-newline)
  (setq require-final-newline mode-require-final-newline))


;; SNMPv1 MIB Editing Mode.
;;
;;;###autoload
(defun snmp-mode ()
  "Major mode for editing SNMP MIBs.
Expression and list commands understand all C brackets.
Tab indents for C code.
Comments start with -- and end with newline or another --.
Delete converts tabs to spaces as it moves back.
\\{snmp-mode-map}
Turning on snmp-mode runs the hooks in `snmp-common-mode-hook', then
`snmp-mode-hook'."
  (interactive)

  (snmp-common-mode "SNMP" 'snmp-mode
                   snmp-mode-abbrev-table
                   '(snmp-font-lock-keywords
                     snmp-font-lock-keywords-1
                     snmp-font-lock-keywords-2
                     snmp-font-lock-keywords-3)
                   'snmp-mode-imenu-create-index
                   'snmp-tempo-tags)

  ;; Completion lists
  (make-local-variable 'snmp-mode-syntax-list)
  (setq snmp-mode-syntax-list (append snmp-rfc1155-types
                                     snmp-rfc1213-types
                                     snmp-mode-syntax-list))
  (make-local-variable 'snmp-mode-access-list)
  (setq snmp-mode-access-list snmp-rfc1155-access)
  (make-local-variable 'snmp-mode-status-list)
  (setq snmp-mode-status-list snmp-rfc1212-status)

  ;; Run hooks
  (run-mode-hooks 'snmp-common-mode-hook 'snmp-mode-hook))


;;;###autoload
(defun snmpv2-mode ()
  "Major mode for editing SNMPv2 MIBs.
Expression and list commands understand all C brackets.
Tab indents for C code.
Comments start with -- and end with newline or another --.
Delete converts tabs to spaces as it moves back.
\\{snmp-mode-map}
Turning on snmp-mode runs the hooks in `snmp-common-mode-hook',
then `snmpv2-mode-hook'."
  (interactive)

  (snmp-common-mode "SNMPv2" 'snmpv2-mode
                   snmpv2-mode-abbrev-table
                   '(snmp-font-lock-keywords
                     snmp-font-lock-keywords-1
                     snmp-font-lock-keywords-2
                     snmp-font-lock-keywords-3)
                   'snmp-mode-imenu-create-index
                   'snmpv2-tempo-tags)

  ;; Completion lists
  (make-local-variable 'snmp-mode-syntax-list)
  (setq snmp-mode-syntax-list (append snmp-rfc1902-types
                                     snmp-rfc1903-types
                                     snmp-mode-syntax-list))
  (make-local-variable 'snmp-mode-access-list)
  (setq snmp-mode-access-list snmp-rfc1902-access)
  (make-local-variable 'snmp-mode-status-list)
  (setq snmp-mode-status-list snmp-rfc1902-status)

  ;; Run hooks
  (run-mode-hooks 'snmp-common-mode-hook 'snmpv2-mode-hook))


;;;----------------------------------------------------------------------------
;;
;;                           Indentation Setup
;;
;;;----------------------------------------------------------------------------

(defvar snmp-macro-open
  "[a-zA-Z][-a-zA-Z0-9]*[ \t]*\\(OBJECT\\|TRAP\\)-\\(TYPE\\|GROUP\\)\
\\|DESCRIPTION\\|IMPORTS\\|MODULE\\(-IDENTITY\\|-COMPLIANCE\\)\
\\|.*::=[ \t]*\\(BEGIN\\|TEXTUAL-CONVENTION\\)[ \t]*$")

(defvar snmp-macro-close
  "::=[ \t]*{\\|\\(END\\|.*[;\"]\\)[ \t]*$")

(defun snmp-calculate-indent ()
  "Calculate the current line indentation in SNMP MIB code.

We use a very simple scheme: if the previous non-empty line was a \"macro
open\" string, add `snmp-indent-level' to it.  If it was a \"macro close\"
string, subtract `snmp-indent-level'.  Otherwise, use the same indentation
as the previous non-empty line.  Note comments are considered empty
lines for the purposes of this function."
  (let ((empty (concat "\\([ \t]*\\)\\(" comment-start-skip "\\|$\\)"))
        (case-fold-search nil)) ; keywords must be in uppercase
    (save-excursion
      (while (and (>= (forward-line -1) 0)
                  (looking-at empty)))
      (skip-chars-forward " \t")
      (+ (current-column)
         ;; Are we looking at a macro open string?  If so, add more.
         (cond ((looking-at snmp-macro-open)
                snmp-indent-level)
               ;; macro close string?  If so, remove some.
               ((looking-at snmp-macro-close)
                (- snmp-indent-level))
               ;; Neither; just stay here.
               (t 0))))))

(defun snmp-indent-line ()
  "Indent current line as SNMP MIB code."
  (let ((indent (snmp-calculate-indent))
        (pos (- (point-max) (point)))
        shift-amt beg)
    (beginning-of-line)
    (setq beg (point))
    (skip-chars-forward " \t")
    (setq shift-amt (- indent (current-column)))
    (if (zerop shift-amt)
        nil
      (delete-region beg (point))
      (indent-to indent))
    ;; If initial point was within line's indentation,
    ;; position after the indentation.  Else stay at same point in text.
    (if (> (- (point-max) pos) (point))
        (goto-char (- (point-max) pos)))))


;;;----------------------------------------------------------------------------
;;
;;                              Imenu Setup
;;
;;;----------------------------------------------------------------------------

(defvar snmp-clause-regexp
  "^[ \t]*\\([a-zA-Z][-a-zA-Z0-9]*\\)[ \t\n]*\
\\(TRAP-TYPE\\|::=\\|OBJECT\\(-TYPE[ \t\n]+SYNTAX\\|[ \t\n]+IDENTIFIER[ \t\n]*::=\\)\\)")

(defun snmp-mode-imenu-create-index ()
  (let ((index-alist '())
	(index-oid-alist '())
	(index-tc-alist '())
	(index-table-alist '())
	(index-trap-alist '())
        (case-fold-search nil) ; keywords must be uppercase
	prev-pos token end)
    (goto-char (point-min))
    (imenu-progress-message prev-pos 0)
    ;; Search for a useful MIB item (that's not in a comment)
    (save-match-data
      (while (re-search-forward snmp-clause-regexp nil t)
        (imenu-progress-message prev-pos)
        (setq
         end (match-end 0)
         token (cons (match-string 1)
                     (set-marker (make-marker) (match-beginning 1))))
        (goto-char (match-beginning 2))
        (cond ((looking-at "OBJECT-TYPE[ \t\n]+SYNTAX")
               (push token index-alist))
              ((looking-at "OBJECT[ \t\n]+IDENTIFIER[ \t\n]*::=")
               (push token index-oid-alist))
              ((looking-at "::=[ \t\n]*SEQUENCE[ \t\n]*{")
               (push token index-table-alist))
              ((looking-at "TRAP-TYPE")
               (push token index-trap-alist))
              ((looking-at "::=")
               (push token index-tc-alist)))
        (goto-char end)))
    ;; Create the menu
    (imenu-progress-message prev-pos 100)
    (setq index-alist (nreverse index-alist))
    (and index-tc-alist
 	 (push (cons "Textual Conventions" (nreverse index-tc-alist))
  	       index-alist))
    (and index-trap-alist
	 (push (cons "Traps" (nreverse index-trap-alist))
               index-alist))
    (and index-table-alist
 	 (push (cons "Tables" (nreverse index-table-alist))
  	       index-alist))
    (and index-oid-alist
 	 (push (cons "Object IDs" (nreverse index-oid-alist))
  	       index-alist))
    index-alist))


;;;----------------------------------------------------------------------------
;;
;;                              Tempo Setup
;;
;;;----------------------------------------------------------------------------

(require 'tempo)

;; Perform a completing-read with info given
;;
(defun snmp-completing-read (prompt table &optional pred require init hist)
  "Read from the minibuffer, with completion.
Like `completing-read', but the variable `snmp-completion-ignore-case'
controls whether case is significant."
  (let ((completion-ignore-case snmp-completion-ignore-case))
    (completing-read prompt table pred require init hist)))

;; OBJECT-TYPE macro template
;;
(tempo-define-template "snmp-object-type"
  '(> (P "Object Label: ") " OBJECT-TYPE" n>
    "SYNTAX  "
    (if tempo-interactive
        (snmp-completing-read "Syntax: " snmp-mode-syntax-list nil nil)
      p) n>
    "ACCESS  "
    (if tempo-interactive
        (snmp-completing-read "Access: " snmp-mode-access-list nil t)
      p) n>
    "STATUS  "
    (if tempo-interactive
        (snmp-completing-read "Status: " snmp-mode-status-list nil t)
      p) n>
    "DESCRIPTION" n> "\"" p "\"" n>
    (P "Default Value: " defval t)
    (if (string= "" (tempo-lookup-named 'defval))
        nil
      '(l "DEFVAL { " (s defval) " }" n>))
    "::= { " (p "OID: ") " }" n)
  "objectType"
  "Insert an OBJECT-TYPE macro."
  'snmp-tempo-tags)

;; Table macro template
;;
(tempo-define-template "snmp-table-type"
  ;; First the table OBJECT-TYPE
  '(> (P "Table Name: " table)
    (P "Entry Name: " entry t)
    (let* ((entry (tempo-lookup-named 'entry))
           (seq (copy-sequence entry)))
      (aset entry 0 (downcase (aref entry 0)))
      (aset seq 0 (upcase (aref seq 0)))
      (tempo-save-named 'obj-entry entry)
      (tempo-save-named 'seq-entry seq)
      nil)
    " OBJECT-TYPE" n>
    "SYNTAX  SEQUENCE OF "
    (s seq-entry) n>
    "ACCESS  not-accessible" n>
    "STATUS  mandatory" n>
    "DESCRIPTION" n> "\"" p "\"" n>
    "::= { " (p "OID: ") " }" n n>
   ;; Next the row OBJECT-TYPE
    (s obj-entry) " OBJECT-TYPE" n>
    "SYNTAX  " (s seq-entry) n>
    "ACCESS  not-accessible" n>
    "STATUS  mandatory" n>
    "DESCRIPTION" n> "\"" p "\"" n>
    "INDEX   { " (p "Index List: ") " }" n>
    "::= {" (s table) " 1 }" n n>
   ;; Finally the SEQUENCE type
    (s seq-entry) " ::= SEQUENCE {" n> p n> "}" n)
  "tableType"
  "Insert an SNMP table."
  'snmp-tempo-tags)


;; v2 SMI OBJECT-TYPE macro template
;;
(tempo-define-template "snmpv2-object-type"
  '(> (P "Object Label: ") " OBJECT-TYPE" n>
    "SYNTAX      "
    (if tempo-interactive
        (snmp-completing-read "Syntax: " snmp-mode-syntax-list nil nil)
      p) n>
    "MAX-ACCESS  "
    (if tempo-interactive
        (snmp-completing-read "Max Access: " snmp-mode-access-list nil t)
      p) n>
    "STATUS      "
    (if tempo-interactive
        (snmp-completing-read "Status: " snmp-mode-status-list nil t)
      p) n>
    "DESCRIPTION" n> "\"" p "\"" n>
    (P "Default Value: " defval t)
    (if (string= "" (tempo-lookup-named 'defval))
        nil
      '(l "DEFVAL { " (s defval) " }" n>))
    "::= { " (p "OID: ") " }" n)
  "objectType"
  "Insert an v2 SMI OBJECT-TYPE macro."
  'snmpv2-tempo-tags)

;; v2 SMI Table macro template
;;
(tempo-define-template "snmpv2-table-type"
  ;; First the table OBJECT-TYPE
  '(> (P "Table Name: " table)
    (P "Entry Name: " entry t)
    (let* ((entry (tempo-lookup-named 'entry))
           (seq (copy-sequence entry)))
      (aset entry 0 (downcase (aref entry 0)))
      (aset seq 0 (upcase (aref seq 0)))
      (tempo-save-named 'obj-entry entry)
      (tempo-save-named 'seq-entry seq)
      nil)
    " OBJECT-TYPE" n>
    "SYNTAX      SEQUENCE OF "
    (s seq-entry) n>
    "MAX-ACCESS  not-accessible" n>
    "STATUS      current" n>
    "DESCRIPTION" n> "\"" p "\"" n>
    "::= { " (p "OID: ") " }" n n>
   ;; Next the row OBJECT-TYPE
    (s obj-entry) " OBJECT-TYPE" n>
    "SYNTAX      " (s seq-entry) n>
    "MAX-ACCESS  not-accessible" n>
    "STATUS      current" n>
    "DESCRIPTION" n> "\"" p "\"" n>
    "INDEX { " (p "Index List: ") " }" n>
    "::= { " (s table) " 1 }" n n>
   ;; Finally the SEQUENCE type
    (s seq-entry) " ::= SEQUENCE {" n> p n> "}" n)
  "tableType"
  "Insert an v2 SMI SNMP table."
  'snmpv2-tempo-tags)

;; v2 SMI TEXTUAL-CONVENTION macro template
;;
(tempo-define-template "snmpv2-textual-convention"
  '(> (P "Texual Convention Type: ") " ::= TEXTUAL-CONVENTION" n>
    "STATUS  "
    (if tempo-interactive
        (snmp-completing-read "Status: " snmp-mode-status-list nil t)
      p) n>
    "DESCRIPTION" n> "\"" p "\"" n>
    "SYNTAX  "
    (if tempo-interactive
        (snmp-completing-read "Syntax: " snmp-mode-syntax-list nil nil)
      p) n> )
  "textualConvention"
  "Insert an v2 SMI TEXTUAL-CONVENTION macro."
  'snmpv2-tempo-tags)


(provide 'snmp-mode)

;; arch-tag: eb6cc0f9-1e47-4023-8625-bc9aae6c3527
;;; snmp-mode.el ends here