t7006: demonstrate a problem with aliases in subdirectories
[git/git.git] / alias.c
CommitLineData
94351118
JK
1#include "cache.h"
2
94351118
JK
3char *alias_lookup(const char *alias)
4{
11179155
TA
5 char *v = NULL;
6 struct strbuf key = STRBUF_INIT;
7 strbuf_addf(&key, "alias.%s", alias);
9e9de18f
JK
8 if (git_config_key_is_valid(key.buf))
9 git_config_get_string(key.buf, &v);
11179155
TA
10 strbuf_release(&key);
11 return v;
94351118 12}
0989fe96 13
ad9ac6db
GB
14#define SPLIT_CMDLINE_BAD_ENDING 1
15#define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
16static const char *split_cmdline_errors[] = {
17 "cmdline ends with \\",
18 "unclosed quote"
19};
20
0989fe96
MV
21int split_cmdline(char *cmdline, const char ***argv)
22{
23 int src, dst, count = 0, size = 16;
24 char quoted = 0;
25
b32fa95f 26 ALLOC_ARRAY(*argv, size);
0989fe96
MV
27
28 /* split alias_string */
29 (*argv)[count++] = cmdline;
30 for (src = dst = 0; cmdline[src];) {
31 char c = cmdline[src];
32 if (!quoted && isspace(c)) {
33 cmdline[dst++] = 0;
34 while (cmdline[++src]
35 && isspace(cmdline[src]))
36 ; /* skip */
cc108379 37 ALLOC_GROW(*argv, count + 1, size);
0989fe96
MV
38 (*argv)[count++] = cmdline + dst;
39 } else if (!quoted && (c == '\'' || c == '"')) {
40 quoted = c;
41 src++;
42 } else if (c == quoted) {
43 quoted = 0;
44 src++;
45 } else {
46 if (c == '\\' && quoted != '\'') {
47 src++;
48 c = cmdline[src];
49 if (!c) {
50 free(*argv);
51 *argv = NULL;
ad9ac6db 52 return -SPLIT_CMDLINE_BAD_ENDING;
0989fe96
MV
53 }
54 }
55 cmdline[dst++] = c;
56 src++;
57 }
58 }
59
60 cmdline[dst] = 0;
61
62 if (quoted) {
63 free(*argv);
64 *argv = NULL;
ad9ac6db 65 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
0989fe96
MV
66 }
67
cc108379 68 ALLOC_GROW(*argv, count + 1, size);
27d5438d
JK
69 (*argv)[count] = NULL;
70
0989fe96
MV
71 return count;
72}
73
cc108379
FC
74const char *split_cmdline_strerror(int split_cmdline_errno)
75{
76 return split_cmdline_errors[-split_cmdline_errno - 1];
ad9ac6db 77}