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