Imported Upstream version 2.17.1
[platform/upstream/git.git] / alias.c
1 #include "cache.h"
2 #include "config.h"
3
4 struct config_alias_data {
5         const char *alias;
6         char *v;
7 };
8
9 static int config_alias_cb(const char *key, const char *value, void *d)
10 {
11         struct config_alias_data *data = d;
12         const char *p;
13
14         if (skip_prefix(key, "alias.", &p) && !strcasecmp(p, data->alias))
15                 return git_config_string((const char **)&data->v, key, value);
16
17         return 0;
18 }
19
20 char *alias_lookup(const char *alias)
21 {
22         struct config_alias_data data = { alias, NULL };
23
24         read_early_config(config_alias_cb, &data);
25
26         return data.v;
27 }
28
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"
34 };
35
36 int split_cmdline(char *cmdline, const char ***argv)
37 {
38         int src, dst, count = 0, size = 16;
39         char quoted = 0;
40
41         ALLOC_ARRAY(*argv, size);
42
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_AND_NULL(*argv);
66                                         return -SPLIT_CMDLINE_BAD_ENDING;
67                                 }
68                         }
69                         cmdline[dst++] = c;
70                         src++;
71                 }
72         }
73
74         cmdline[dst] = 0;
75
76         if (quoted) {
77                 FREE_AND_NULL(*argv);
78                 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
79         }
80
81         ALLOC_GROW(*argv, count + 1, size);
82         (*argv)[count] = NULL;
83
84         return count;
85 }
86
87 const char *split_cmdline_strerror(int split_cmdline_errno)
88 {
89         return split_cmdline_errors[-split_cmdline_errno - 1];
90 }