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