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