vcs-svn: suppress a signed/unsigned comparison warning
[alt-git.git] / userdiff.c
blob76109da4bcb7abc26ed20508692051a40e8addd7
1 #include "cache.h"
2 #include "userdiff.h"
3 #include "cache.h"
4 #include "attr.h"
6 static struct userdiff_driver *drivers;
7 static int ndrivers;
8 static int drivers_alloc;
10 #define PATTERNS(name, pattern, word_regex) \
11 { name, NULL, -1, { pattern, REG_EXTENDED }, \
12 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
13 #define IPATTERN(name, pattern, word_regex) \
14 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
15 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
16 static struct userdiff_driver builtin_drivers[] = {
17 IPATTERN("fortran",
18 "!^([C*]|[ \t]*!)\n"
19 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
20 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
21 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
22 /* -- */
23 "[a-zA-Z][a-zA-Z0-9_]*"
24 "|\\.([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])\\."
25 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
26 * Don't worry about format statements without leading digits since
27 * they would have been matched above as a variable anyway. */
28 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
29 "|//|\\*\\*|::|[/<>=]="),
30 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
31 "[^<>= \t]+"),
32 PATTERNS("java",
33 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
34 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
35 /* -- */
36 "[a-zA-Z_][a-zA-Z0-9_]*"
37 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
38 "|[-+*/<>%&^|=!]="
39 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
40 PATTERNS("matlab",
41 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
42 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
43 PATTERNS("objc",
44 /* Negate C statements that can look like functions */
45 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
46 /* Objective-C methods */
47 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
48 /* C functions */
49 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
50 /* Objective-C class/protocol definitions */
51 "^(@(implementation|interface|protocol)[ \t].*)$",
52 /* -- */
53 "[a-zA-Z_][a-zA-Z0-9_]*"
54 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
55 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
56 PATTERNS("pascal",
57 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
58 "implementation|initialization|finalization)[ \t]*.*)$"
59 "\n"
60 "^(.*=[ \t]*(class|record).*)$",
61 /* -- */
62 "[a-zA-Z_][a-zA-Z0-9_]*"
63 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
64 "|<>|<=|>=|:=|\\.\\."),
65 PATTERNS("perl",
66 "^package .*\n"
67 "^sub [[:alnum:]_':]+[ \t]*"
68 "(\\([^)]*\\)[ \t]*)?" /* prototype */
70 * Attributes. A regex can't count nested parentheses,
71 * so just slurp up whatever we see, taking care not
72 * to accept lines like "sub foo; # defined elsewhere".
74 * An attribute could contain a semicolon, but at that
75 * point it seems reasonable enough to give up.
77 "(:[^;#]*)?"
78 "(\\{[ \t]*)?" /* brace can come here or on the next line */
79 "(#.*)?$\n" /* comment */
80 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
81 "(\\{[ \t]*)?" /* brace can come here or on the next line */
82 "(#.*)?$\n"
83 "^=head[0-9] .*", /* POD */
84 /* -- */
85 "[[:alpha:]_'][[:alnum:]_']*"
86 "|0[xb]?[0-9a-fA-F_]*"
87 /* taking care not to interpret 3..5 as (3.)(.5) */
88 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
89 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
90 "|&&=|\\|\\|=|//=|\\*\\*="
91 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
92 "|[-+*/%.^&<>=!|]="
93 "|=~|!~"
94 "|<<|<>|<=>|>>"),
95 PATTERNS("php",
96 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
97 "^[\t ]*(class.*)$",
98 /* -- */
99 "[a-zA-Z_][a-zA-Z0-9_]*"
100 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
101 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
102 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
103 /* -- */
104 "[a-zA-Z_][a-zA-Z0-9_]*"
105 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
106 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
107 /* -- */
108 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
109 /* -- */
110 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
111 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
112 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
113 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
114 "[={}\"]|[^={}\" \t]+"),
115 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
116 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
117 PATTERNS("cpp",
118 /* Jump targets or access declarations */
119 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
120 /* C/++ functions/methods at top level */
121 "^([A-Za-z_][A-Za-z_0-9]*([ \t*]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
122 /* compound type at top level */
123 "^((struct|class|enum)[^;]*)$",
124 /* -- */
125 "[a-zA-Z_][a-zA-Z0-9_]*"
126 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
127 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
128 PATTERNS("csharp",
129 /* Keywords */
130 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
131 /* Methods and constructors */
132 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
133 /* Properties */
134 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
135 /* Type definitions */
136 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
137 /* Namespace */
138 "^[ \t]*(namespace[ \t]+.*)$",
139 /* -- */
140 "[a-zA-Z_][a-zA-Z0-9_]*"
141 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
142 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
143 { "default", NULL, -1, { NULL, 0 } },
145 #undef PATTERNS
146 #undef IPATTERN
148 static struct userdiff_driver driver_true = {
149 "diff=true",
150 NULL,
152 { NULL, 0 }
155 static struct userdiff_driver driver_false = {
156 "!diff",
157 NULL,
159 { NULL, 0 }
162 static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
164 int i;
165 for (i = 0; i < ndrivers; i++) {
166 struct userdiff_driver *drv = drivers + i;
167 if (!strncmp(drv->name, k, len) && !drv->name[len])
168 return drv;
170 for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
171 struct userdiff_driver *drv = builtin_drivers + i;
172 if (!strncmp(drv->name, k, len) && !drv->name[len])
173 return drv;
175 return NULL;
178 static struct userdiff_driver *parse_driver(const char *var,
179 const char *value, const char *type)
181 struct userdiff_driver *drv;
182 const char *dot;
183 const char *name;
184 int namelen;
186 if (prefixcmp(var, "diff."))
187 return NULL;
188 dot = strrchr(var, '.');
189 if (dot == var + 4)
190 return NULL;
191 if (strcmp(type, dot+1))
192 return NULL;
194 name = var + 5;
195 namelen = dot - name;
196 drv = userdiff_find_by_namelen(name, namelen);
197 if (!drv) {
198 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
199 drv = &drivers[ndrivers++];
200 memset(drv, 0, sizeof(*drv));
201 drv->name = xmemdupz(name, namelen);
202 drv->binary = -1;
204 return drv;
207 static int parse_funcname(struct userdiff_funcname *f, const char *k,
208 const char *v, int cflags)
210 if (git_config_string(&f->pattern, k, v) < 0)
211 return -1;
212 f->cflags = cflags;
213 return 1;
216 static int parse_string(const char **d, const char *k, const char *v)
218 if (git_config_string(d, k, v) < 0)
219 return -1;
220 return 1;
223 static int parse_tristate(int *b, const char *k, const char *v)
225 if (v && !strcasecmp(v, "auto"))
226 *b = -1;
227 else
228 *b = git_config_bool(k, v);
229 return 1;
232 static int parse_bool(int *b, const char *k, const char *v)
234 *b = git_config_bool(k, v);
235 return 1;
238 int userdiff_config(const char *k, const char *v)
240 struct userdiff_driver *drv;
242 if ((drv = parse_driver(k, v, "funcname")))
243 return parse_funcname(&drv->funcname, k, v, 0);
244 if ((drv = parse_driver(k, v, "xfuncname")))
245 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
246 if ((drv = parse_driver(k, v, "binary")))
247 return parse_tristate(&drv->binary, k, v);
248 if ((drv = parse_driver(k, v, "command")))
249 return parse_string(&drv->external, k, v);
250 if ((drv = parse_driver(k, v, "textconv")))
251 return parse_string(&drv->textconv, k, v);
252 if ((drv = parse_driver(k, v, "cachetextconv")))
253 return parse_bool(&drv->textconv_want_cache, k, v);
254 if ((drv = parse_driver(k, v, "wordregex")))
255 return parse_string(&drv->word_regex, k, v);
257 return 0;
260 struct userdiff_driver *userdiff_find_by_name(const char *name) {
261 int len = strlen(name);
262 return userdiff_find_by_namelen(name, len);
265 struct userdiff_driver *userdiff_find_by_path(const char *path)
267 static struct git_attr *attr;
268 struct git_attr_check check;
270 if (!attr)
271 attr = git_attr("diff");
272 check.attr = attr;
274 if (!path)
275 return NULL;
276 if (git_check_attr(path, 1, &check))
277 return NULL;
279 if (ATTR_TRUE(check.value))
280 return &driver_true;
281 if (ATTR_FALSE(check.value))
282 return &driver_false;
283 if (ATTR_UNSET(check.value))
284 return NULL;
285 return userdiff_find_by_name(check.value);
288 struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
290 if (!driver->textconv)
291 return NULL;
293 if (driver->textconv_want_cache && !driver->textconv_cache) {
294 struct notes_cache *c = xmalloc(sizeof(*c));
295 struct strbuf name = STRBUF_INIT;
297 strbuf_addf(&name, "textconv/%s", driver->name);
298 notes_cache_init(c, name.buf, driver->textconv);
299 driver->textconv_cache = c;
302 return driver;