blob: c4a7060bb03e36411cca86f35eacb3f75e8f24fc [file] [log] [blame]
Dave Barach9b8ffd92016-07-08 08:13:45 -04001#!/usr/bin/emacs --script
2
Dave Barach8d0f2f02018-03-12 09:31:36 -04003;;; 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:
7;;;
8;;; http://www.apache.org/licenses/LICENSE-2.0
9;;;
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.
15
Dave Barach2bc547c2016-08-11 12:13:55 -040016;; Insert style boilerplate if it's not already there
Dave Barach987fdfa2016-07-17 11:52:10 -040017;;
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
21
Dave Barach9b8ffd92016-07-08 08:13:45 -040022(defun insert-style-boilerplate () (interactive)
Dave Barach2bc547c2016-08-11 12:13:55 -040023 (save-excursion
24 (goto-char (point-min))
25 (if (eq nil (search-forward "coding-style-patch-verification"
26 (point-max) t))
27 (let ((junk 0)) (goto-char (point-max))
28 (insert "
Dave Barach9b8ffd92016-07-08 08:13:45 -040029/*
30 * fd.io coding-style-patch-verification: ON
31 *
Dave Barach987fdfa2016-07-17 11:52:10 -040032 * Local Var" "iables:
Dave Barach9b8ffd92016-07-08 08:13:45 -040033 * eval: (c-set-style \"gnu\")
34 * End:
Dave Barach2bc547c2016-08-11 12:13:55 -040035 */")))))
36
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
41
42(defun find-indent-offs () (interactive)
43 (save-excursion
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))
51 (previous-line))))
Dave Barach9b8ffd92016-07-08 08:13:45 -040052
Dave Barach987fdfa2016-07-17 11:52:10 -040053;; 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).
Dave Barach8a7fb0c2016-07-08 14:44:23 -040057
58(defun fix-initializer (what) (interactive)
Dave Barach2bc547c2016-08-11 12:13:55 -040059 (find-indent-offs)
Dave Barach8a7fb0c2016-07-08 14:44:23 -040060 (save-excursion
61 (goto-char (point-min))
62 (while (search-forward-regexp what (point-max) t)
63 (move-beginning-of-line nil)
Dave Barach2bc547c2016-08-11 12:13:55 -040064 (previous-line)
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)))
70 (let ((junk 0))
71 (next-line)
72 (open-line 1)
73 (c-indent-line-or-region)
74 (insert "/* *INDENT-OFF* */")
75 (search-forward "{")
76 (backward-char)
77 (forward-sexp)
78 (move-end-of-line nil)
79 (newline 1)
80 (c-indent-line-or-region)
81 (insert "/* *INDENT-ON* */")
82 (find-indent-offs))
83 (search-forward "*INDENT-ON*"))))))
Dave Barach8a7fb0c2016-07-08 14:44:23 -040084
Dave Barach987fdfa2016-07-17 11:52:10 -040085(defun fix-pool-foreach () (interactive)
86 (fix-initializer "pool_foreach *("))
87
Keith Burns (alagalah)5f3ca642016-08-06 11:03:59 -070088(defun fix-pool-foreach-index () (interactive)
89 (fix-initializer "pool_foreach_index *("))
90
Dave Barach987fdfa2016-07-17 11:52:10 -040091(defun fix-hash-foreach () (interactive)
92 (fix-initializer "hash_foreach *("))
93
94(defun fix-hash-foreach-pair () (interactive)
95 (fix-initializer "hash_foreach_pair *("))
96
Keith Burns (alagalah)114e8a92016-08-04 09:27:23 -070097(defun fix-hash-foreach-mem () (interactive)
98 (fix-initializer "hash_foreach_mem *("))
99
Dave Barach987fdfa2016-07-17 11:52:10 -0400100(defun fix-clib-fifo-foreach () (interactive)
101 (fix-initializer "clib_fifo_foreach *("))
102
103(defun fix-clib-bitmap-foreach () (interactive)
104 (fix-initializer "clib_bitmap_foreach *("))
105
106(defun fix-foreach-ip-interface-address () (interactive)
107 (fix-initializer "foreach_ip_interface_address *("))
108
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400109(defun fix-vlib-register-thread () (interactive)
110 (fix-initializer "VLIB_REGISTER_THREAD *("))
111
112(defun fix-vlib-cli-command () (interactive)
113 (fix-initializer "VLIB_CLI_COMMAND *("))
114
115(defun fix-vlib-register-node () (interactive)
116 (fix-initializer "VLIB_REGISTER_NODE *("))
117
Dave Baracha8d77ed2016-08-05 18:09:54 -0400118(defun fix-reply-macro2 () (interactive)
119 (fix-initializer "REPLY_MACRO2 *("))
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400120
Dave Barachba868bb2016-08-08 09:51:21 -0400121(defun fix-vnet-device-class () (interactive)
122 (fix-initializer "VNET_DEVICE_CLASS *("))
123
124(defun fix-vnet-hw-interface-class () (interactive)
125 (fix-initializer "VNET_HW_INTERFACE_CLASS *("))
126
Dave Barach75f69042016-08-11 16:03:03 -0400127(defun fix-clib-packed () (interactive)
128 (fix-initializer "CLIB_PACKED *("))
Ed Warnickec1be59d2016-08-11 13:30:57 -0700129(defun fix-vl-api-packed () (interactive)
130 (fix-initializer "VL_API_PACKED *("))
Dave Barach75f69042016-08-11 16:03:03 -0400131
Dave Barach987fdfa2016-07-17 11:52:10 -0400132;; Driver routine which runs the set of functions
133;; defined above, as well as the bottom boilerplate function
Dave Barach9b8ffd92016-07-08 08:13:45 -0400134
135(defun fd-io-styleify () (interactive)
Dave Barach987fdfa2016-07-17 11:52:10 -0400136 (fix-pool-foreach)
Keith Burns (alagalah)5f3ca642016-08-06 11:03:59 -0700137 (fix-pool-foreach-index)
Dave Barach987fdfa2016-07-17 11:52:10 -0400138 (fix-hash-foreach)
139 (fix-hash-foreach-pair)
Keith Burns (alagalah)114e8a92016-08-04 09:27:23 -0700140 (fix-hash-foreach-mem)
Dave Barach987fdfa2016-07-17 11:52:10 -0400141 (fix-foreach-ip-interface-address)
142 (fix-clib-fifo-foreach)
143 (fix-clib-bitmap-foreach)
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400144 (fix-vlib-register-thread)
145 (fix-vlib-cli-command)
146 (fix-vlib-register-node)
Dave Baracha8d77ed2016-08-05 18:09:54 -0400147 (fix-reply-macro2)
Dave Barachba868bb2016-08-08 09:51:21 -0400148 (fix-vnet-device-class)
149 (fix-vnet-hw-interface-class)
Dave Barach75f69042016-08-11 16:03:03 -0400150 (fix-clib-packed)
Ed Warnickec1be59d2016-08-11 13:30:57 -0700151 (fix-vl-api-packed)
Dave Barach2bc547c2016-08-11 12:13:55 -0400152 (insert-style-boilerplate)
153 (if (boundp 'indent-offset-list)
154 (makunbound 'indent-offset-list)))
Dave Barach987fdfa2016-07-17 11:52:10 -0400155
156;; When run as a script, this sexp
157;; walks the list of files supplied on the command line.
158;;
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)...
162
Dave Barach2bc547c2016-08-11 12:13:55 -0400163(let ((file-index 0))
164 (if (elt argv file-index)
165 (while (elt argv file-index)
166 (find-file (elt argv file-index))
Dave Barach987fdfa2016-07-17 11:52:10 -0400167 (fd-io-styleify)
Dave Barach2bc547c2016-08-11 12:13:55 -0400168 (message "Done %s..." (elt argv file-index))
169 (setq file-index (1+ file-index))))
170 (if (> file-index 0)
171 (let ((junk 0))
172 (message "Save and quit...")
173 (save-buffers-kill-emacs t))))
Dave Baracha8d77ed2016-08-05 18:09:54 -0400174
175