Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/emacs --script |
| 2 | |
Dave Barach | 8d0f2f0 | 2018-03-12 09:31:36 -0400 | [diff] [blame] | 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: |
| 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 Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 16 | ;; Insert style boilerplate if it's not already there |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 17 | ;; |
| 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 Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 22 | (defun insert-style-boilerplate () (interactive) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 23 | (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 Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 29 | /* |
| 30 | * fd.io coding-style-patch-verification: ON |
| 31 | * |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 32 | * Local Var" "iables: |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 33 | * eval: (c-set-style \"gnu\") |
| 34 | * End: |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 35 | */"))))) |
| 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 Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 52 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 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). |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 57 | |
| 58 | (defun fix-initializer (what) (interactive) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 59 | (find-indent-offs) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 60 | (save-excursion |
| 61 | (goto-char (point-min)) |
| 62 | (while (search-forward-regexp what (point-max) t) |
| 63 | (move-beginning-of-line nil) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 64 | (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 Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 84 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 85 | (defun fix-pool-foreach () (interactive) |
| 86 | (fix-initializer "pool_foreach *(")) |
| 87 | |
Keith Burns (alagalah) | 5f3ca64 | 2016-08-06 11:03:59 -0700 | [diff] [blame] | 88 | (defun fix-pool-foreach-index () (interactive) |
| 89 | (fix-initializer "pool_foreach_index *(")) |
| 90 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 91 | (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) | 114e8a9 | 2016-08-04 09:27:23 -0700 | [diff] [blame] | 97 | (defun fix-hash-foreach-mem () (interactive) |
| 98 | (fix-initializer "hash_foreach_mem *(")) |
| 99 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 100 | (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 Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 109 | (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 Barach | a8d77ed | 2016-08-05 18:09:54 -0400 | [diff] [blame] | 118 | (defun fix-reply-macro2 () (interactive) |
| 119 | (fix-initializer "REPLY_MACRO2 *(")) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 120 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 121 | (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 Barach | 75f6904 | 2016-08-11 16:03:03 -0400 | [diff] [blame] | 127 | (defun fix-clib-packed () (interactive) |
| 128 | (fix-initializer "CLIB_PACKED *(")) |
Ed Warnicke | c1be59d | 2016-08-11 13:30:57 -0700 | [diff] [blame] | 129 | (defun fix-vl-api-packed () (interactive) |
| 130 | (fix-initializer "VL_API_PACKED *(")) |
Dave Barach | 75f6904 | 2016-08-11 16:03:03 -0400 | [diff] [blame] | 131 | |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 132 | ;; Driver routine which runs the set of functions |
| 133 | ;; defined above, as well as the bottom boilerplate function |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 134 | |
| 135 | (defun fd-io-styleify () (interactive) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 136 | (fix-pool-foreach) |
Keith Burns (alagalah) | 5f3ca64 | 2016-08-06 11:03:59 -0700 | [diff] [blame] | 137 | (fix-pool-foreach-index) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 138 | (fix-hash-foreach) |
| 139 | (fix-hash-foreach-pair) |
Keith Burns (alagalah) | 114e8a9 | 2016-08-04 09:27:23 -0700 | [diff] [blame] | 140 | (fix-hash-foreach-mem) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 141 | (fix-foreach-ip-interface-address) |
| 142 | (fix-clib-fifo-foreach) |
| 143 | (fix-clib-bitmap-foreach) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 144 | (fix-vlib-register-thread) |
| 145 | (fix-vlib-cli-command) |
| 146 | (fix-vlib-register-node) |
Dave Barach | a8d77ed | 2016-08-05 18:09:54 -0400 | [diff] [blame] | 147 | (fix-reply-macro2) |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 148 | (fix-vnet-device-class) |
| 149 | (fix-vnet-hw-interface-class) |
Dave Barach | 75f6904 | 2016-08-11 16:03:03 -0400 | [diff] [blame] | 150 | (fix-clib-packed) |
Ed Warnicke | c1be59d | 2016-08-11 13:30:57 -0700 | [diff] [blame] | 151 | (fix-vl-api-packed) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 152 | (insert-style-boilerplate) |
| 153 | (if (boundp 'indent-offset-list) |
| 154 | (makunbound 'indent-offset-list))) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 155 | |
| 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 Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 163 | (let ((file-index 0)) |
| 164 | (if (elt argv file-index) |
| 165 | (while (elt argv file-index) |
| 166 | (find-file (elt argv file-index)) |
Dave Barach | 987fdfa | 2016-07-17 11:52:10 -0400 | [diff] [blame] | 167 | (fd-io-styleify) |
Dave Barach | 2bc547c | 2016-08-11 12:13:55 -0400 | [diff] [blame] | 168 | (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 Barach | a8d77ed | 2016-08-05 18:09:54 -0400 | [diff] [blame] | 174 | |
| 175 | |