Move declaration for alias.c to alias.h
[git] / alias.c
1 #include "cache.h"
2 #include "alias.h"
3 #include "config.h"
4
5 struct config_alias_data {
6         const char *alias;
7         char *v;
8 };
9
10 static int config_alias_cb(const char *key, const char *value, void *d)
11 {
12         struct config_alias_data *data = d;
13         const char *p;
14
15         if (skip_prefix(key, "alias.", &p) && !strcasecmp(p, data->alias))
16                 return git_config_string((const char **)&data->v, key, value);
17
18         return 0;
19 }
20
21 char *alias_lookup(const char *alias)
22 {
23         struct config_alias_data data = { alias, NULL };
24
25         read_early_config(config_alias_cb, &data);
26
27         return data.v;
28 }
29
30 #define SPLIT_CMDLINE_BAD_ENDING 1
31 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
32 static const char *split_cmdline_errors[] = {
33         "cmdline ends with \\",
34         "unclosed quote"
35 };
36
37 int split_cmdline(char *cmdline, const char ***argv)
38 {
39         int src, dst, count = 0, size = 16;
40         char quoted = 0;
41
42         ALLOC_ARRAY(*argv, size);
43
44         /* split alias_string */
45         (*argv)[count++] = cmdline;
46         for (src = dst = 0; cmdline[src];) {
47                 char c = cmdline[src];
48                 if (!quoted && isspace(c)) {
49                         cmdline[dst++] = 0;
50                         while (cmdline[++src]
51                                         && isspace(cmdline[src]))
52                                 ; /* skip */
53                         ALLOC_GROW(*argv, count + 1, size);
54                         (*argv)[count++] = cmdline + dst;
55                 } else if (!quoted && (c == '\'' || c == '"')) {
56                         quoted = c;
57                         src++;
58                 } else if (c == quoted) {
59                         quoted = 0;
60                         src++;
61                 } else {
62                         if (c == '\\' && quoted != '\'') {
63                                 src++;
64                                 c = cmdline[src];
65                                 if (!c) {
66                                         FREE_AND_NULL(*argv);
67                                         return -SPLIT_CMDLINE_BAD_ENDING;
68                                 }
69                         }
70                         cmdline[dst++] = c;
71                         src++;
72                 }
73         }
74
75         cmdline[dst] = 0;
76
77         if (quoted) {
78                 FREE_AND_NULL(*argv);
79                 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
80         }
81
82         ALLOC_GROW(*argv, count + 1, size);
83         (*argv)[count] = NULL;
84
85         return count;
86 }
87
88 const char *split_cmdline_strerror(int split_cmdline_errno)
89 {
90         return split_cmdline_errors[-split_cmdline_errno - 1];
91 }