Merge git://repo.or.cz/git-gui
[git] / alias.c
1 #include "cache.h"
2
3 static const char *alias_key;
4 static char *alias_val;
5
6 static int alias_lookup_cb(const char *k, const char *v, void *cb)
7 {
8         if (!prefixcmp(k, "alias.") && !strcmp(k+6, alias_key)) {
9                 if (!v)
10                         return config_error_nonbool(k);
11                 alias_val = xstrdup(v);
12                 return 0;
13         }
14         return 0;
15 }
16
17 char *alias_lookup(const char *alias)
18 {
19         alias_key = alias;
20         alias_val = NULL;
21         git_config(alias_lookup_cb, NULL);
22         return alias_val;
23 }
24
25 int split_cmdline(char *cmdline, const char ***argv)
26 {
27         int src, dst, count = 0, size = 16;
28         char quoted = 0;
29
30         *argv = xmalloc(sizeof(char *) * size);
31
32         /* split alias_string */
33         (*argv)[count++] = cmdline;
34         for (src = dst = 0; cmdline[src];) {
35                 char c = cmdline[src];
36                 if (!quoted && isspace(c)) {
37                         cmdline[dst++] = 0;
38                         while (cmdline[++src]
39                                         && isspace(cmdline[src]))
40                                 ; /* skip */
41                         ALLOC_GROW(*argv, count+1, size);
42                         (*argv)[count++] = cmdline + dst;
43                 } else if (!quoted && (c == '\'' || c == '"')) {
44                         quoted = c;
45                         src++;
46                 } else if (c == quoted) {
47                         quoted = 0;
48                         src++;
49                 } else {
50                         if (c == '\\' && quoted != '\'') {
51                                 src++;
52                                 c = cmdline[src];
53                                 if (!c) {
54                                         free(*argv);
55                                         *argv = NULL;
56                                         return error("cmdline ends with \\");
57                                 }
58                         }
59                         cmdline[dst++] = c;
60                         src++;
61                 }
62         }
63
64         cmdline[dst] = 0;
65
66         if (quoted) {
67                 free(*argv);
68                 *argv = NULL;
69                 return error("unclosed quote");
70         }
71
72         ALLOC_GROW(*argv, count+1, size);
73         (*argv)[count] = NULL;
74
75         return count;
76 }
77