Merge branch 'jk/test-with-x' into maint
[alt-git.git] / alias.c
blob6aa164a362427ffa5dc8616bcb01aec3f15b462b
1 #include "cache.h"
3 char *alias_lookup(const char *alias)
5 char *v = NULL;
6 struct strbuf key = STRBUF_INIT;
7 strbuf_addf(&key, "alias.%s", alias);
8 git_config_get_string(key.buf, &v);
9 strbuf_release(&key);
10 return v;
13 #define SPLIT_CMDLINE_BAD_ENDING 1
14 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
15 static const char *split_cmdline_errors[] = {
16 "cmdline ends with \\",
17 "unclosed quote"
20 int split_cmdline(char *cmdline, const char ***argv)
22 int src, dst, count = 0, size = 16;
23 char quoted = 0;
25 *argv = xmalloc(sizeof(**argv) * size);
27 /* split alias_string */
28 (*argv)[count++] = cmdline;
29 for (src = dst = 0; cmdline[src];) {
30 char c = cmdline[src];
31 if (!quoted && isspace(c)) {
32 cmdline[dst++] = 0;
33 while (cmdline[++src]
34 && isspace(cmdline[src]))
35 ; /* skip */
36 ALLOC_GROW(*argv, count + 1, size);
37 (*argv)[count++] = cmdline + dst;
38 } else if (!quoted && (c == '\'' || c == '"')) {
39 quoted = c;
40 src++;
41 } else if (c == quoted) {
42 quoted = 0;
43 src++;
44 } else {
45 if (c == '\\' && quoted != '\'') {
46 src++;
47 c = cmdline[src];
48 if (!c) {
49 free(*argv);
50 *argv = NULL;
51 return -SPLIT_CMDLINE_BAD_ENDING;
54 cmdline[dst++] = c;
55 src++;
59 cmdline[dst] = 0;
61 if (quoted) {
62 free(*argv);
63 *argv = NULL;
64 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
67 ALLOC_GROW(*argv, count + 1, size);
68 (*argv)[count] = NULL;
70 return count;
73 const char *split_cmdline_strerror(int split_cmdline_errno)
75 return split_cmdline_errors[-split_cmdline_errno - 1];