config: don't implicitly use gitdir or commondir
[git.git] / alias.c
blobde8e6a3f496accd9bd06916411ebb7e10b27d4cb
1 #include "cache.h"
2 #include "config.h"
4 struct config_alias_data {
5 const char *alias;
6 char *v;
7 };
9 static int config_alias_cb(const char *key, const char *value, void *d)
11 struct config_alias_data *data = d;
12 const char *p;
14 if (skip_prefix(key, "alias.", &p) && !strcmp(p, data->alias))
15 return git_config_string((const char **)&data->v, key, value);
17 return 0;
20 char *alias_lookup(const char *alias)
22 struct config_alias_data data = { alias, NULL };
24 read_early_config(config_alias_cb, &data);
26 return data.v;
29 #define SPLIT_CMDLINE_BAD_ENDING 1
30 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
31 static const char *split_cmdline_errors[] = {
32 "cmdline ends with \\",
33 "unclosed quote"
36 int split_cmdline(char *cmdline, const char ***argv)
38 int src, dst, count = 0, size = 16;
39 char quoted = 0;
41 ALLOC_ARRAY(*argv, size);
43 /* split alias_string */
44 (*argv)[count++] = cmdline;
45 for (src = dst = 0; cmdline[src];) {
46 char c = cmdline[src];
47 if (!quoted && isspace(c)) {
48 cmdline[dst++] = 0;
49 while (cmdline[++src]
50 && isspace(cmdline[src]))
51 ; /* skip */
52 ALLOC_GROW(*argv, count + 1, size);
53 (*argv)[count++] = cmdline + dst;
54 } else if (!quoted && (c == '\'' || c == '"')) {
55 quoted = c;
56 src++;
57 } else if (c == quoted) {
58 quoted = 0;
59 src++;
60 } else {
61 if (c == '\\' && quoted != '\'') {
62 src++;
63 c = cmdline[src];
64 if (!c) {
65 free(*argv);
66 *argv = NULL;
67 return -SPLIT_CMDLINE_BAD_ENDING;
70 cmdline[dst++] = c;
71 src++;
75 cmdline[dst] = 0;
77 if (quoted) {
78 free(*argv);
79 *argv = NULL;
80 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
83 ALLOC_GROW(*argv, count + 1, size);
84 (*argv)[count] = NULL;
86 return count;
89 const char *split_cmdline_strerror(int split_cmdline_errno)
91 return split_cmdline_errors[-split_cmdline_errno - 1];