Elijah Newren | 36bf195 | 2023-02-24 00:09:24 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "alloc.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 4 | #include "userdiff.h" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 5 | #include "attr.h" |
Elijah Newren | 36bf195 | 2023-02-24 00:09:24 +0000 | [diff] [blame] | 6 | #include "strbuf.h" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 7 | |
| 8 | static struct userdiff_driver *drivers; |
| 9 | static int ndrivers; |
| 10 | static int drivers_alloc; |
| 11 | |
Ævar Arnfjörð Bjarmason | 2dd75f1 | 2022-02-24 10:33:03 +0100 | [diff] [blame] | 12 | #define PATTERNS(lang, rx, wrx) { \ |
| 13 | .name = lang, \ |
| 14 | .binary = -1, \ |
| 15 | .funcname = { \ |
| 16 | .pattern = rx, \ |
| 17 | .cflags = REG_EXTENDED, \ |
| 18 | }, \ |
| 19 | .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \ |
René Scharfe | be39144 | 2023-04-06 22:19:11 +0200 | [diff] [blame] | 20 | .word_regex_multi_byte = wrx "|[^[:space:]]", \ |
Ævar Arnfjörð Bjarmason | 2dd75f1 | 2022-02-24 10:33:03 +0100 | [diff] [blame] | 21 | } |
| 22 | #define IPATTERN(lang, rx, wrx) { \ |
| 23 | .name = lang, \ |
| 24 | .binary = -1, \ |
| 25 | .funcname = { \ |
| 26 | .pattern = rx, \ |
| 27 | .cflags = REG_EXTENDED | REG_ICASE, \ |
| 28 | }, \ |
| 29 | .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \ |
René Scharfe | be39144 | 2023-04-06 22:19:11 +0200 | [diff] [blame] | 30 | .word_regex_multi_byte = wrx "|[^[:space:]]", \ |
Ævar Arnfjörð Bjarmason | 2dd75f1 | 2022-02-24 10:33:03 +0100 | [diff] [blame] | 31 | } |
Junio C Hamano | b6029b3 | 2021-08-10 15:12:01 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Built-in drivers for various languages, sorted by their names |
| 35 | * (except that the "default" is left at the end). |
| 36 | * |
| 37 | * When writing or updating patterns, assume that the contents these |
| 38 | * patterns are applied to are syntactically correct. The patterns |
| 39 | * can be simple without implementing all syntactical corner cases, as |
| 40 | * long as they are sufficiently permissive. |
| 41 | */ |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 42 | static struct userdiff_driver builtin_drivers[] = { |
Adrian Johnson | e90d065 | 2012-09-16 13:24:15 +0930 | [diff] [blame] | 43 | IPATTERN("ada", |
Adrian Johnson | 39a87a2 | 2014-02-03 22:03:16 +1030 | [diff] [blame] | 44 | "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n" |
Adrian Johnson | e90d065 | 2012-09-16 13:24:15 +0930 | [diff] [blame] | 45 | "!^[ \t]*with[ \t].*$\n" |
| 46 | "^[ \t]*((procedure|function)[ \t]+.*)$\n" |
| 47 | "^[ \t]*((package|protected|task)[ \t]+.*)$", |
| 48 | /* -- */ |
| 49 | "[a-zA-Z][a-zA-Z0-9_]*" |
Adrian Johnson | 39a87a2 | 2014-02-03 22:03:16 +1030 | [diff] [blame] | 50 | "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?" |
Adrian Johnson | e90d065 | 2012-09-16 13:24:15 +0930 | [diff] [blame] | 51 | "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"), |
Victor Engmark | 2ff6c34 | 2020-10-22 12:45:08 +1300 | [diff] [blame] | 52 | PATTERNS("bash", |
| 53 | /* Optional leading indentation */ |
| 54 | "^[ \t]*" |
| 55 | /* Start of captured text */ |
| 56 | "(" |
| 57 | "(" |
| 58 | /* POSIX identifier with mandatory parentheses */ |
| 59 | "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))" |
| 60 | "|" |
| 61 | /* Bashism identifier with optional parentheses */ |
| 62 | "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))" |
| 63 | ")" |
| 64 | /* Optional whitespace */ |
| 65 | "[ \t]*" |
| 66 | /* Compound command starting with `{`, `(`, `((` or `[[` */ |
| 67 | "(\\{|\\(\\(?|\\[\\[)" |
| 68 | /* End of captured text */ |
| 69 | ")", |
| 70 | /* -- */ |
| 71 | /* Characters not in the default $IFS value */ |
| 72 | "[^ \t]+"), |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 73 | PATTERNS("bibtex", |
| 74 | "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$", |
| 75 | /* -- */ |
Ævar Arnfjörð Bjarmason | ddd164d | 2021-04-08 17:04:16 +0200 | [diff] [blame] | 76 | "[={}\"]|[^={}\" \t]+"), |
| 77 | PATTERNS("cpp", |
| 78 | /* Jump targets or access declarations */ |
| 79 | "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n" |
| 80 | /* functions/methods, variables, and compounds at top level */ |
| 81 | "^((::[[:space:]]*)?[A-Za-z_].*)$", |
| 82 | /* -- */ |
Johannes Sixt | 350b87c | 2021-10-08 19:09:55 +0000 | [diff] [blame] | 83 | /* identifiers and keywords */ |
Ævar Arnfjörð Bjarmason | ddd164d | 2021-04-08 17:04:16 +0200 | [diff] [blame] | 84 | "[a-zA-Z_][a-zA-Z0-9_]*" |
Johannes Sixt | 350b87c | 2021-10-08 19:09:55 +0000 | [diff] [blame] | 85 | /* decimal and octal integers as well as floatingpoint numbers */ |
Johannes Sixt | 386076e | 2021-10-24 11:56:43 +0200 | [diff] [blame] | 86 | "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*" |
Johannes Sixt | 350b87c | 2021-10-08 19:09:55 +0000 | [diff] [blame] | 87 | /* hexadecimal and binary integers */ |
Johannes Sixt | 386076e | 2021-10-24 11:56:43 +0200 | [diff] [blame] | 88 | "|0[xXbB][0-9a-fA-F]+[lLuU]*" |
Johannes Sixt | 350b87c | 2021-10-08 19:09:55 +0000 | [diff] [blame] | 89 | /* floatingpoint numbers that begin with a decimal point */ |
Johannes Sixt | 386076e | 2021-10-24 11:56:43 +0200 | [diff] [blame] | 90 | "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?" |
Johannes Sixt | c4fdba3 | 2021-10-10 17:03:04 +0000 | [diff] [blame] | 91 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"), |
Ævar Arnfjörð Bjarmason | ddd164d | 2021-04-08 17:04:16 +0200 | [diff] [blame] | 92 | PATTERNS("csharp", |
| 93 | /* Keywords */ |
| 94 | "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n" |
| 95 | /* Methods and constructors */ |
| 96 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n" |
| 97 | /* Properties */ |
| 98 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n" |
| 99 | /* Type definitions */ |
Julian Verdurmen | c4e3178 | 2021-03-02 00:58:09 +0000 | [diff] [blame] | 100 | "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n" |
Ævar Arnfjörð Bjarmason | ddd164d | 2021-04-08 17:04:16 +0200 | [diff] [blame] | 101 | /* Namespace */ |
| 102 | "^[ \t]*(namespace[ \t]+.*)$", |
| 103 | /* -- */ |
| 104 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 105 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" |
| 106 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), |
| 107 | IPATTERN("css", |
| 108 | "![:;][[:space:]]*$\n" |
| 109 | "^[:[@.#]?[_a-z0-9].*$", |
| 110 | /* -- */ |
| 111 | /* |
| 112 | * This regex comes from W3C CSS specs. Should theoretically also |
| 113 | * allow ISO 10646 characters U+00A0 and higher, |
| 114 | * but they are not handled in this regex. |
| 115 | */ |
| 116 | "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */ |
| 117 | "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */ |
| 118 | ), |
Stephen Boyd | 3c81760 | 2019-08-19 14:22:43 -0700 | [diff] [blame] | 119 | PATTERNS("dts", |
| 120 | "!;\n" |
Stephen Boyd | 8da56a4 | 2019-10-20 11:52:30 -0700 | [diff] [blame] | 121 | "!=\n" |
Stephen Boyd | 3c81760 | 2019-08-19 14:22:43 -0700 | [diff] [blame] | 122 | /* lines beginning with a word optionally preceded by '&' or the root */ |
Stephen Boyd | 8da56a4 | 2019-10-20 11:52:30 -0700 | [diff] [blame] | 123 | "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)", |
Stephen Boyd | 3c81760 | 2019-08-19 14:22:43 -0700 | [diff] [blame] | 124 | /* -- */ |
| 125 | /* Property names and math operators */ |
| 126 | "[a-zA-Z0-9,._+?#-]+" |
| 127 | "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"), |
Łukasz Niemier | a807200 | 2019-11-08 22:38:24 +0100 | [diff] [blame] | 128 | PATTERNS("elixir", |
| 129 | "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$", |
Ed Maste | d1b1384 | 2019-12-13 17:55:35 +0000 | [diff] [blame] | 130 | /* -- */ |
Łukasz Niemier | a807200 | 2019-11-08 22:38:24 +0100 | [diff] [blame] | 131 | /* Atoms, names, and module attributes */ |
Ed Maste | d1b1384 | 2019-12-13 17:55:35 +0000 | [diff] [blame] | 132 | "[@:]?[a-zA-Z0-9@_?!]+" |
Łukasz Niemier | a807200 | 2019-11-08 22:38:24 +0100 | [diff] [blame] | 133 | /* Numbers with specific base */ |
| 134 | "|[-+]?0[xob][0-9a-fA-F]+" |
| 135 | /* Numbers */ |
| 136 | "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?" |
| 137 | /* Operators and atoms that represent them */ |
| 138 | "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)" |
| 139 | /* Not real operators, but should be grouped */ |
| 140 | "|:?%[A-Za-z0-9_.]\\{\\}?"), |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 141 | IPATTERN("fortran", |
Philippe Blain | b79e692 | 2020-08-12 22:30:28 +0000 | [diff] [blame] | 142 | /* Don't match comment lines */ |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 143 | "!^([C*]|[ \t]*!)\n" |
Philippe Blain | b79e692 | 2020-08-12 22:30:28 +0000 | [diff] [blame] | 144 | /* Don't match 'module procedure' lines */ |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 145 | "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n" |
Philippe Blain | b79e692 | 2020-08-12 22:30:28 +0000 | [diff] [blame] | 146 | /* Program, module, block data */ |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 147 | "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA" |
Philippe Blain | b79e692 | 2020-08-12 22:30:28 +0000 | [diff] [blame] | 148 | /* Subroutines and functions */ |
Philippe Blain | 75c3b6b | 2020-08-12 22:30:29 +0000 | [diff] [blame] | 149 | "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$", |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 150 | /* -- */ |
| 151 | "[a-zA-Z][a-zA-Z0-9_]*" |
| 152 | "|\\.([Ee][Qq]|[Nn][Ee]|[Gg][TtEe]|[Ll][TtEe]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|[Aa][Nn][Dd]|[Oo][Rr]|[Nn]?[Ee][Qq][Vv]|[Nn][Oo][Tt])\\." |
| 153 | /* numbers and format statements like 2E14.4, or ES12.6, 9X. |
| 154 | * Don't worry about format statements without leading digits since |
| 155 | * they would have been matched above as a variable anyway. */ |
| 156 | "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 157 | "|//|\\*\\*|::|[/<>=]="), |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 158 | IPATTERN("fountain", |
| 159 | "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$", |
| 160 | /* -- */ |
Zoë Blade | 69f9c87 | 2015-07-21 14:22:46 +0100 | [diff] [blame] | 161 | "[^ \t-]+"), |
Alban Gruin | 1dbf0c0 | 2018-03-01 12:19:07 +0100 | [diff] [blame] | 162 | PATTERNS("golang", |
| 163 | /* Functions */ |
| 164 | "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n" |
| 165 | /* Structs and interfaces */ |
| 166 | "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)", |
| 167 | /* -- */ |
| 168 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 169 | "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?" |
| 170 | "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"), |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 171 | PATTERNS("html", |
| 172 | "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$", |
| 173 | /* -- */ |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 174 | "[^<>= \t]+"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 175 | PATTERNS("java", |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 176 | "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" |
Andrei Rybak | 575e6fc | 2023-02-08 00:42:58 +0100 | [diff] [blame] | 177 | /* Class, enum, interface, and record declarations */ |
Andrei Rybak | 93d52ed | 2023-02-08 00:42:59 +0100 | [diff] [blame] | 178 | "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n" |
Tassilo Horn | a8cbc89 | 2021-08-11 19:51:04 +0200 | [diff] [blame] | 179 | /* Method definitions; note that constructor signatures are not */ |
| 180 | /* matched because they are indistinguishable from method calls. */ |
| 181 | "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", |
Paolo Bonzini | 959e2e6 | 2009-06-17 16:26:06 +0200 | [diff] [blame] | 182 | /* -- */ |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 183 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 184 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" |
| 185 | "|[-+*/<>%&^|=!]=" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 186 | "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"), |
Jaydeep P Das | 09188ed | 2022-03-12 10:18:32 +0530 | [diff] [blame] | 187 | PATTERNS("kotlin", |
| 188 | "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$", |
| 189 | /* -- */ |
| 190 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 191 | /* hexadecimal and binary numbers */ |
| 192 | "|0[xXbB][0-9a-fA-F_]+[lLuU]*" |
| 193 | /* integers and floats */ |
| 194 | "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*" |
| 195 | /* floating point numbers beginning with decimal point */ |
| 196 | "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?" |
| 197 | /* unary and binary operators */ |
| 198 | "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"), |
Ash Holland | 09dad92 | 2020-05-02 14:15:43 +0100 | [diff] [blame] | 199 | PATTERNS("markdown", |
| 200 | "^ {0,3}#{1,6}[ \t].*", |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 201 | /* -- */ |
Ash Holland | 09dad92 | 2020-05-02 14:15:43 +0100 | [diff] [blame] | 202 | "[^<>= \t]+"), |
Gustaf Hendeby | 53b10a1 | 2011-11-15 21:15:03 +0100 | [diff] [blame] | 203 | PATTERNS("matlab", |
Boxuan Li | 2731a78 | 2019-05-30 00:15:39 +0800 | [diff] [blame] | 204 | /* |
| 205 | * Octave pattern is mostly the same as matlab, except that '%%%' and |
Boxuan Li | 91bf382 | 2019-05-18 11:46:23 +0800 | [diff] [blame] | 206 | * '##' can also be used to begin code sections, in addition to '%%' |
Boxuan Li | 2731a78 | 2019-05-30 00:15:39 +0800 | [diff] [blame] | 207 | * that is understood by both. |
| 208 | */ |
Boxuan Li | 91bf382 | 2019-05-18 11:46:23 +0800 | [diff] [blame] | 209 | "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$", |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 210 | /* -- */ |
Gustaf Hendeby | 53b10a1 | 2011-11-15 21:15:03 +0100 | [diff] [blame] | 211 | "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 212 | PATTERNS("objc", |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 213 | /* Negate C statements that can look like functions */ |
| 214 | "!^[ \t]*(do|for|if|else|return|switch|while)\n" |
| 215 | /* Objective-C methods */ |
| 216 | "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n" |
| 217 | /* C functions */ |
Paolo Bonzini | 959e2e6 | 2009-06-17 16:26:06 +0200 | [diff] [blame] | 218 | "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 219 | /* Objective-C class/protocol definitions */ |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 220 | "^(@(implementation|interface|protocol)[ \t].*)$", |
| 221 | /* -- */ |
| 222 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 223 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 224 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 225 | PATTERNS("pascal", |
Ævar Arnfjörð Bjarmason | 82512e0 | 2021-04-08 17:04:18 +0200 | [diff] [blame] | 226 | "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface" |
| 227 | "|implementation|initialization|finalization)[ \t]*.*)$\n" |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 228 | "^(.*=[ \t]*(class|record).*)$", |
| 229 | /* -- */ |
| 230 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 231 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 232 | "|<>|<=|>=|:=|\\.\\."), |
Jonathan Nieder | 71a5d4b | 2010-12-26 03:07:31 -0600 | [diff] [blame] | 233 | PATTERNS("perl", |
Jonathan Nieder | ea2ca44 | 2011-05-21 14:38:26 -0500 | [diff] [blame] | 234 | "^package .*\n" |
| 235 | "^sub [[:alnum:]_':]+[ \t]*" |
| 236 | "(\\([^)]*\\)[ \t]*)?" /* prototype */ |
| 237 | /* |
| 238 | * Attributes. A regex can't count nested parentheses, |
| 239 | * so just slurp up whatever we see, taking care not |
| 240 | * to accept lines like "sub foo; # defined elsewhere". |
| 241 | * |
| 242 | * An attribute could contain a semicolon, but at that |
| 243 | * point it seems reasonable enough to give up. |
| 244 | */ |
| 245 | "(:[^;#]*)?" |
| 246 | "(\\{[ \t]*)?" /* brace can come here or on the next line */ |
| 247 | "(#.*)?$\n" /* comment */ |
Jonathan Nieder | f143d9c | 2011-05-22 12:29:32 -0500 | [diff] [blame] | 248 | "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*" |
Jonathan Nieder | ea2ca44 | 2011-05-21 14:38:26 -0500 | [diff] [blame] | 249 | "(\\{[ \t]*)?" /* brace can come here or on the next line */ |
| 250 | "(#.*)?$\n" |
Jonathan Nieder | 12f0967 | 2011-05-21 14:35:51 -0500 | [diff] [blame] | 251 | "^=head[0-9] .*", /* POD */ |
Jonathan Nieder | 71a5d4b | 2010-12-26 03:07:31 -0600 | [diff] [blame] | 252 | /* -- */ |
| 253 | "[[:alpha:]_'][[:alnum:]_']*" |
| 254 | "|0[xb]?[0-9a-fA-F_]*" |
| 255 | /* taking care not to interpret 3..5 as (3.)(.5) */ |
| 256 | "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?" |
| 257 | "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::" |
| 258 | "|&&=|\\|\\|=|//=|\\*\\*=" |
| 259 | "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?" |
| 260 | "|[-+*/%.^&<>=!|]=" |
| 261 | "|=~|!~" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 262 | "|<<|<>|<=>|>>"), |
Björn Steinbrink | 6d2f208 | 2010-05-23 20:05:40 +0200 | [diff] [blame] | 263 | PATTERNS("php", |
Javier Spagnoletti | aff9282 | 2020-10-07 03:38:18 +0000 | [diff] [blame] | 264 | "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n" |
USAMI Kenta | 2c7f3aa | 2021-08-31 06:01:25 +0000 | [diff] [blame] | 265 | "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$", |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 266 | /* -- */ |
| 267 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 268 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 269 | "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"), |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 270 | PATTERNS("python", |
| 271 | "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$", |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 272 | /* -- */ |
| 273 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 274 | "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 275 | "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 276 | /* -- */ |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 277 | PATTERNS("ruby", |
| 278 | "^[ \t]*((class|module|def)[ \t].*)$", |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 279 | /* -- */ |
| 280 | "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" |
| 281 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 282 | "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), |
Marc-André Lureau | d74e786 | 2019-05-17 01:58:15 +0200 | [diff] [blame] | 283 | PATTERNS("rust", |
Konrad Borowski | a04c7e0 | 2020-10-07 13:26:11 +0000 | [diff] [blame] | 284 | "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$", |
Marc-André Lureau | d74e786 | 2019-05-17 01:58:15 +0200 | [diff] [blame] | 285 | /* -- */ |
| 286 | "[a-zA-Z_][a-zA-Z0-9_]*" |
Johannes Sixt | 33be7b3 | 2019-05-30 18:44:35 +0200 | [diff] [blame] | 287 | "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?" |
Marc-André Lureau | d74e786 | 2019-05-17 01:58:15 +0200 | [diff] [blame] | 288 | "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"), |
Atharva Raykar | a437390 | 2021-04-08 14:44:43 +0530 | [diff] [blame] | 289 | PATTERNS("scheme", |
| 290 | "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$", |
| 291 | /* |
| 292 | * R7RS valid identifiers include any sequence enclosed |
| 293 | * within vertical lines having no backslashes |
| 294 | */ |
| 295 | "\\|([^\\\\]*)\\|" |
| 296 | /* All other words should be delimited by spaces or parentheses */ |
| 297 | "|([^][)(}{[ \t])+"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 298 | PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$", |
René Scharfe | be39144 | 2023-04-06 22:19:11 +0200 | [diff] [blame] | 299 | "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"), |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 300 | { "default", NULL, NULL, -1, { NULL, 0 } }, |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 301 | }; |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 302 | #undef PATTERNS |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 303 | #undef IPATTERN |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 304 | |
| 305 | static struct userdiff_driver driver_true = { |
Ævar Arnfjörð Bjarmason | 2dd75f1 | 2022-02-24 10:33:03 +0100 | [diff] [blame] | 306 | .name = "diff=true", |
| 307 | .binary = 0, |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 308 | }; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 309 | |
| 310 | static struct userdiff_driver driver_false = { |
Ævar Arnfjörð Bjarmason | 2dd75f1 | 2022-02-24 10:33:03 +0100 | [diff] [blame] | 311 | .name = "!diff", |
| 312 | .binary = 1, |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 313 | }; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 314 | |
Ævar Arnfjörð Bjarmason | f12fa9e | 2021-04-08 17:04:19 +0200 | [diff] [blame] | 315 | struct find_by_namelen_data { |
| 316 | const char *name; |
| 317 | size_t len; |
| 318 | struct userdiff_driver *driver; |
| 319 | }; |
| 320 | |
| 321 | static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver, |
Jeff King | c25d9e5 | 2022-12-13 06:16:57 -0500 | [diff] [blame] | 322 | enum userdiff_driver_type type UNUSED, |
| 323 | void *priv) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 324 | { |
Ævar Arnfjörð Bjarmason | f12fa9e | 2021-04-08 17:04:19 +0200 | [diff] [blame] | 325 | struct find_by_namelen_data *cb_data = priv; |
| 326 | |
| 327 | if (!strncmp(driver->name, cb_data->name, cb_data->len) && |
| 328 | !driver->name[cb_data->len]) { |
| 329 | cb_data->driver = driver; |
| 330 | return 1; /* tell the caller to stop iterating */ |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 331 | } |
Ævar Arnfjörð Bjarmason | f12fa9e | 2021-04-08 17:04:19 +0200 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
René Scharfe | be39144 | 2023-04-06 22:19:11 +0200 | [diff] [blame] | 335 | static int regexec_supports_multi_byte_chars(void) |
| 336 | { |
| 337 | static const char not_space[] = "[^[:space:]]"; |
| 338 | static const char utf8_multi_byte_char[] = "\xc2\xa3"; |
| 339 | regex_t re; |
| 340 | regmatch_t match; |
| 341 | static int result = -1; |
| 342 | |
| 343 | if (result != -1) |
| 344 | return result; |
| 345 | if (regcomp(&re, not_space, REG_EXTENDED)) |
| 346 | BUG("invalid regular expression: %s", not_space); |
| 347 | result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) && |
| 348 | match.rm_so == 0 && |
| 349 | match.rm_eo == strlen(utf8_multi_byte_char); |
| 350 | regfree(&re); |
| 351 | return result; |
| 352 | } |
| 353 | |
Ævar Arnfjörð Bjarmason | f12fa9e | 2021-04-08 17:04:19 +0200 | [diff] [blame] | 354 | static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len) |
| 355 | { |
| 356 | struct find_by_namelen_data udcbdata = { |
| 357 | .name = name, |
| 358 | .len = len, |
| 359 | }; |
| 360 | for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata); |
| 361 | return udcbdata.driver; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 364 | static int parse_funcname(struct userdiff_funcname *f, const char *k, |
| 365 | const char *v, int cflags) |
| 366 | { |
| 367 | if (git_config_string(&f->pattern, k, v) < 0) |
| 368 | return -1; |
| 369 | f->cflags = cflags; |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 370 | return 0; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 371 | } |
| 372 | |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 373 | static int parse_tristate(int *b, const char *k, const char *v) |
| 374 | { |
| 375 | if (v && !strcasecmp(v, "auto")) |
| 376 | *b = -1; |
| 377 | else |
| 378 | *b = git_config_bool(k, v); |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 379 | return 0; |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 380 | } |
| 381 | |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 382 | static int parse_bool(int *b, const char *k, const char *v) |
| 383 | { |
| 384 | *b = git_config_bool(k, v); |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 385 | return 0; |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Jeff King | c7534ef | 2008-10-26 00:45:55 -0400 | [diff] [blame] | 388 | int userdiff_config(const char *k, const char *v) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 389 | { |
| 390 | struct userdiff_driver *drv; |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 391 | const char *name, *type; |
Jeff King | f5914f4 | 2020-04-10 15:44:28 -0400 | [diff] [blame] | 392 | size_t namelen; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 393 | |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 394 | if (parse_config_key(k, "diff", &name, &namelen, &type) || !name) |
| 395 | return 0; |
| 396 | |
| 397 | drv = userdiff_find_by_namelen(name, namelen); |
| 398 | if (!drv) { |
| 399 | ALLOC_GROW(drivers, ndrivers+1, drivers_alloc); |
| 400 | drv = &drivers[ndrivers++]; |
| 401 | memset(drv, 0, sizeof(*drv)); |
| 402 | drv->name = xmemdupz(name, namelen); |
| 403 | drv->binary = -1; |
| 404 | } |
| 405 | |
| 406 | if (!strcmp(type, "funcname")) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 407 | return parse_funcname(&drv->funcname, k, v, 0); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 408 | if (!strcmp(type, "xfuncname")) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 409 | return parse_funcname(&drv->funcname, k, v, REG_EXTENDED); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 410 | if (!strcmp(type, "binary")) |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 411 | return parse_tristate(&drv->binary, k, v); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 412 | if (!strcmp(type, "command")) |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 413 | return git_config_string(&drv->external, k, v); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 414 | if (!strcmp(type, "textconv")) |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 415 | return git_config_string(&drv->textconv, k, v); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 416 | if (!strcmp(type, "cachetextconv")) |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 417 | return parse_bool(&drv->textconv_want_cache, k, v); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 418 | if (!strcmp(type, "wordregex")) |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 419 | return git_config_string(&drv->word_regex, k, v); |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 420 | if (!strcmp(type, "algorithm")) |
| 421 | return git_config_string(&drv->algorithm, k, v); |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 426 | struct userdiff_driver *userdiff_find_by_name(const char *name) |
| 427 | { |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 428 | int len = strlen(name); |
René Scharfe | be39144 | 2023-04-06 22:19:11 +0200 | [diff] [blame] | 429 | struct userdiff_driver *driver = userdiff_find_by_namelen(name, len); |
| 430 | if (driver && driver->word_regex_multi_byte) { |
| 431 | if (regexec_supports_multi_byte_chars()) |
| 432 | driver->word_regex = driver->word_regex_multi_byte; |
| 433 | driver->word_regex_multi_byte = NULL; |
| 434 | } |
| 435 | return driver; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 438 | struct userdiff_driver *userdiff_find_by_path(struct index_state *istate, |
| 439 | const char *path) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 440 | { |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 441 | static struct attr_check *check; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 442 | |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 443 | if (!check) |
| 444 | check = attr_check_initl("diff", NULL); |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 445 | if (!path) |
| 446 | return NULL; |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 447 | git_check_attr(istate, path, check); |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 448 | |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 449 | if (ATTR_TRUE(check->items[0].value)) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 450 | return &driver_true; |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 451 | if (ATTR_FALSE(check->items[0].value)) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 452 | return &driver_false; |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 453 | if (ATTR_UNSET(check->items[0].value)) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 454 | return NULL; |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 455 | return userdiff_find_by_name(check->items[0].value); |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 456 | } |
Jeff King | 3813e69 | 2011-05-23 16:30:14 -0400 | [diff] [blame] | 457 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 458 | struct userdiff_driver *userdiff_get_textconv(struct repository *r, |
| 459 | struct userdiff_driver *driver) |
Jeff King | 3813e69 | 2011-05-23 16:30:14 -0400 | [diff] [blame] | 460 | { |
| 461 | if (!driver->textconv) |
| 462 | return NULL; |
| 463 | |
| 464 | if (driver->textconv_want_cache && !driver->textconv_cache) { |
| 465 | struct notes_cache *c = xmalloc(sizeof(*c)); |
| 466 | struct strbuf name = STRBUF_INIT; |
| 467 | |
| 468 | strbuf_addf(&name, "textconv/%s", driver->name); |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 469 | notes_cache_init(r, c, name.buf, driver->textconv); |
Jeff King | 3813e69 | 2011-05-23 16:30:14 -0400 | [diff] [blame] | 470 | driver->textconv_cache = c; |
Rene Scharfe | 460c7eb | 2017-08-30 20:20:15 +0200 | [diff] [blame] | 471 | strbuf_release(&name); |
Jeff King | 3813e69 | 2011-05-23 16:30:14 -0400 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | return driver; |
| 475 | } |
Ævar Arnfjörð Bjarmason | f12fa9e | 2021-04-08 17:04:19 +0200 | [diff] [blame] | 476 | |
| 477 | static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn, |
| 478 | enum userdiff_driver_type type, void *cb_data, |
| 479 | struct userdiff_driver *drv, |
| 480 | int drv_size) |
| 481 | { |
| 482 | int i; |
| 483 | int ret; |
| 484 | for (i = 0; i < drv_size; i++) { |
| 485 | struct userdiff_driver *item = drv + i; |
| 486 | if ((ret = fn(item, type, cb_data))) |
| 487 | return ret; |
| 488 | } |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data) |
| 493 | { |
| 494 | int ret; |
| 495 | |
| 496 | ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM, |
| 497 | cb_data, drivers, ndrivers); |
| 498 | if (ret) |
| 499 | return ret; |
| 500 | |
| 501 | ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN, |
| 502 | cb_data, builtin_drivers, |
| 503 | ARRAY_SIZE(builtin_drivers)); |
| 504 | if (ret) |
| 505 | return ret; |
| 506 | |
| 507 | return 0; |
| 508 | } |