1 #!/usr/bin/emacs --script
3 ;;; Copyright (c) 2016 Cisco and/or its affiliates.
4 ;;; Licensed under the Apache License, Version 2.0 (the "License");
5 ;;; you may not use this file except in compliance with the License.
6 ;;; You may obtain a copy of the License at:
8 ;;; http://www.apache.org/licenses/LICENSE-2.0
10 ;;; Unless required by applicable law or agreed to in writing, software
11 ;;; distributed under the License is distributed on an "AS IS" BASIS,
12 ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ;;; See the License for the specific language governing permissions and
14 ;;; limitations under the License.
16 ;; Insert style boilerplate if it's not already there
18 ;; Breaking the string in half keeps emacs
19 ;; from trying to interpret the local variable
20 ;; settings e.g. when it reads the lisp source code
22 (defun insert-style-boilerplate () (interactive)
24 (goto-char (point-min))
25 (if (eq nil (search-forward "coding-style-patch-verification"
27 (let ((junk 0)) (goto-char (point-max))
30 * fd.io coding-style-patch-verification: ON
33 * eval: (c-set-style \"gnu\")
37 ;; (cons xxx <list>) means insert xxx at the head of <list>
38 ;; Build a sorted list of *INDENT-OFF* lines, by searching
39 ;; backwards. The initial (setq indent-offset-list nil)
40 ;; results in (cdr <last-cell>) nil, which makes it a proper list
42 (defun find-indent-offs () (interactive)
44 (if (boundp 'indent-offset-list)
45 (makunbound 'indent-offset-list))
46 (setq indent-offset-list nil)
47 (goto-char (point-max))
48 (while (search-backward "*INDENT-OFF*" (point-min) t)
49 (move-beginning-of-line nil)
50 (setq indent-offset-list (cons (point) indent-offset-list))
53 ;; Insert indent-off ... indent-on brackets around
54 ;; a certain xxx_foreach macro, etc. which "indent"
55 ;; completely screws up. Doesn't handle nesting, of which there
56 ;; are few examples (fortunately).
58 (defun fix-initializer (what) (interactive)
61 (goto-char (point-min))
62 (while (search-forward-regexp what (point-max) t)
63 (move-beginning-of-line nil)
65 (let ((index 0)(pointval 0))
66 (while (and (< pointval (point))(elt indent-offset-list index))
67 (setq pointval (elt indent-offset-list index))
68 (setq index (1+ index)))
69 (if (not (eq pointval (point)))
73 (c-indent-line-or-region)
74 (insert "/* *INDENT-OFF* */")
78 (move-end-of-line nil)
80 (c-indent-line-or-region)
81 (insert "/* *INDENT-ON* */")
83 (search-forward "*INDENT-ON*"))))))
85 (defun fix-pool-foreach () (interactive)
86 (fix-initializer "pool_foreach *("))
88 (defun fix-pool-foreach-index () (interactive)
89 (fix-initializer "pool_foreach_index *("))
91 (defun fix-hash-foreach () (interactive)
92 (fix-initializer "hash_foreach *("))
94 (defun fix-hash-foreach-pair () (interactive)
95 (fix-initializer "hash_foreach_pair *("))
97 (defun fix-hash-foreach-mem () (interactive)
98 (fix-initializer "hash_foreach_mem *("))
100 (defun fix-clib-fifo-foreach () (interactive)
101 (fix-initializer "clib_fifo_foreach *("))
103 (defun fix-clib-bitmap-foreach () (interactive)
104 (fix-initializer "clib_bitmap_foreach *("))
106 (defun fix-foreach-ip-interface-address () (interactive)
107 (fix-initializer "foreach_ip_interface_address *("))
109 (defun fix-vlib-register-thread () (interactive)
110 (fix-initializer "VLIB_REGISTER_THREAD *("))
112 (defun fix-vlib-cli-command () (interactive)
113 (fix-initializer "VLIB_CLI_COMMAND *("))
115 (defun fix-vlib-register-node () (interactive)
116 (fix-initializer "VLIB_REGISTER_NODE *("))
118 (defun fix-reply-macro2 () (interactive)
119 (fix-initializer "REPLY_MACRO2 *("))
121 (defun fix-vnet-device-class () (interactive)
122 (fix-initializer "VNET_DEVICE_CLASS *("))
124 (defun fix-vnet-hw-interface-class () (interactive)
125 (fix-initializer "VNET_HW_INTERFACE_CLASS *("))
127 (defun fix-clib-packed () (interactive)
128 (fix-initializer "CLIB_PACKED *("))
129 (defun fix-vl-api-packed () (interactive)
130 (fix-initializer "VL_API_PACKED *("))
132 ;; Driver routine which runs the set of functions
133 ;; defined above, as well as the bottom boilerplate function
135 (defun fd-io-styleify () (interactive)
137 (fix-pool-foreach-index)
139 (fix-hash-foreach-pair)
140 (fix-hash-foreach-mem)
141 (fix-foreach-ip-interface-address)
142 (fix-clib-fifo-foreach)
143 (fix-clib-bitmap-foreach)
144 (fix-vlib-register-thread)
145 (fix-vlib-cli-command)
146 (fix-vlib-register-node)
148 (fix-vnet-device-class)
149 (fix-vnet-hw-interface-class)
152 (insert-style-boilerplate)
153 (if (boundp 'indent-offset-list)
154 (makunbound 'indent-offset-list)))
156 ;; When run as a script, this sexp
157 ;; walks the list of files supplied on the command line.
159 ;; (elt argv index) returns nil if you M-x eval-buffer
160 ;; or M-x load-file the file, so we won't accidentally
161 ;; evaluate (save-buffers-kill-emacs)...
163 (let ((file-index 0))
164 (if (elt argv file-index)
165 (while (elt argv file-index)
166 (find-file (elt argv file-index))
168 (message "Done %s..." (elt argv file-index))
169 (setq file-index (1+ file-index))))
172 (message "Save and quit...")
173 (save-buffers-kill-emacs t))))