blob: 6010ac2824443d74ca9d1172c1fb80e06ff4fc26 [file] [log] [blame]
Dave Barach9b8ffd92016-07-08 08:13:45 -04001#!/usr/bin/emacs --script
2
Dave Barach987fdfa2016-07-17 11:52:10 -04003;; Insert style boilerplate
4;;
5;; Breaking the string in half keeps emacs
6;; from trying to interpret the local variable
7;; settings e.g. when it reads the lisp source code
8
Dave Barach9b8ffd92016-07-08 08:13:45 -04009(defun insert-style-boilerplate () (interactive)
10 (save-excursion (goto-char (point-max))
11 (insert "
12/*
13 * fd.io coding-style-patch-verification: ON
14 *
Dave Barach987fdfa2016-07-17 11:52:10 -040015 * Local Var" "iables:
Dave Barach9b8ffd92016-07-08 08:13:45 -040016 * eval: (c-set-style \"gnu\")
17 * End:
18 */")))
19
Dave Barach987fdfa2016-07-17 11:52:10 -040020;; Insert indent-off ... indent-on brackets around
21;; a certain xxx_foreach macro, etc. which "indent"
22;; completely screws up. Doesn't handle nesting, of which there
23;; are few examples (fortunately).
Dave Barach8a7fb0c2016-07-08 14:44:23 -040024
25(defun fix-initializer (what) (interactive)
26 (save-excursion
27 (goto-char (point-min))
28 (while (search-forward-regexp what (point-max) t)
29 (move-beginning-of-line nil)
30 (open-line 1)
31 (c-indent-line-or-region)
32 (insert "/* *INDENT-OFF* */")
33 (search-forward "{")
34 (backward-char)
35 (forward-sexp)
36 (move-end-of-line nil)
37 (newline 1)
38 (c-indent-line-or-region)
39 (insert "/* *INDENT-ON* */"))))
40
Dave Barach987fdfa2016-07-17 11:52:10 -040041(defun fix-pool-foreach () (interactive)
42 (fix-initializer "pool_foreach *("))
43
44(defun fix-hash-foreach () (interactive)
45 (fix-initializer "hash_foreach *("))
46
47(defun fix-hash-foreach-pair () (interactive)
48 (fix-initializer "hash_foreach_pair *("))
49
Keith Burns (alagalah)114e8a92016-08-04 09:27:23 -070050(defun fix-hash-foreach-mem () (interactive)
51 (fix-initializer "hash_foreach_mem *("))
52
Dave Barach987fdfa2016-07-17 11:52:10 -040053(defun fix-clib-fifo-foreach () (interactive)
54 (fix-initializer "clib_fifo_foreach *("))
55
56(defun fix-clib-bitmap-foreach () (interactive)
57 (fix-initializer "clib_bitmap_foreach *("))
58
59(defun fix-foreach-ip-interface-address () (interactive)
60 (fix-initializer "foreach_ip_interface_address *("))
61
Dave Barach8a7fb0c2016-07-08 14:44:23 -040062(defun fix-vlib-register-thread () (interactive)
63 (fix-initializer "VLIB_REGISTER_THREAD *("))
64
65(defun fix-vlib-cli-command () (interactive)
66 (fix-initializer "VLIB_CLI_COMMAND *("))
67
68(defun fix-vlib-register-node () (interactive)
69 (fix-initializer "VLIB_REGISTER_NODE *("))
70
71
Dave Barach987fdfa2016-07-17 11:52:10 -040072;; Driver routine which runs the set of functions
73;; defined above, as well as the bottom boilerplate function
Dave Barach9b8ffd92016-07-08 08:13:45 -040074
75(defun fd-io-styleify () (interactive)
Dave Barach987fdfa2016-07-17 11:52:10 -040076 (fix-pool-foreach)
77 (fix-hash-foreach)
78 (fix-hash-foreach-pair)
Keith Burns (alagalah)114e8a92016-08-04 09:27:23 -070079 (fix-hash-foreach-mem)
Dave Barach987fdfa2016-07-17 11:52:10 -040080 (fix-foreach-ip-interface-address)
81 (fix-clib-fifo-foreach)
82 (fix-clib-bitmap-foreach)
Dave Barach8a7fb0c2016-07-08 14:44:23 -040083 (fix-vlib-register-thread)
84 (fix-vlib-cli-command)
85 (fix-vlib-register-node)
Dave Barach9b8ffd92016-07-08 08:13:45 -040086 (insert-style-boilerplate))
87
Dave Barach987fdfa2016-07-17 11:52:10 -040088
89;; When run as a script, this sexp
90;; walks the list of files supplied on the command line.
91;;
92;; (elt argv index) returns nil if you M-x eval-buffer
93;; or M-x load-file the file, so we won't accidentally
94;; evaluate (save-buffers-kill-emacs)...
95
96(let ((index 0))
97 (if (elt argv index)
98 (while (elt argv index)
99 (message "Processing %s..." (elt argv index))
100 (find-file (elt argv index))
101 (fd-io-styleify)
102 (setq index (1+ index))
103 (save-buffers-kill-emacs t))))