]> git.ipfire.org Git - thirdparty/git.git/blob - alias.c
Merge branch 'rj/add-i-leak-fix'
[thirdparty/git.git] / alias.c
1 #include "git-compat-util.h"
2 #include "alias.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "strbuf.h"
6 #include "string-list.h"
7
8 struct config_alias_data {
9 const char *alias;
10 char *v;
11 struct string_list *list;
12 };
13
14 static int config_alias_cb(const char *key, const char *value,
15 const struct config_context *ctx UNUSED, void *d)
16 {
17 struct config_alias_data *data = d;
18 const char *p;
19
20 if (!skip_prefix(key, "alias.", &p))
21 return 0;
22
23 if (data->alias) {
24 if (!strcasecmp(p, data->alias))
25 return git_config_string((const char **)&data->v,
26 key, value);
27 } else if (data->list) {
28 string_list_append(data->list, p);
29 }
30
31 return 0;
32 }
33
34 char *alias_lookup(const char *alias)
35 {
36 struct config_alias_data data = { alias, NULL };
37
38 read_early_config(config_alias_cb, &data);
39
40 return data.v;
41 }
42
43 void list_aliases(struct string_list *list)
44 {
45 struct config_alias_data data = { NULL, NULL, list };
46
47 read_early_config(config_alias_cb, &data);
48 }
49
50 void quote_cmdline(struct strbuf *buf, const char **argv)
51 {
52 for (const char **argp = argv; *argp; argp++) {
53 if (argp != argv)
54 strbuf_addch(buf, ' ');
55 strbuf_addch(buf, '"');
56 for (const char *p = *argp; *p; p++) {
57 const char c = *p;
58
59 if (c == '"' || c =='\\')
60 strbuf_addch(buf, '\\');
61 strbuf_addch(buf, c);
62 }
63 strbuf_addch(buf, '"');
64 }
65 }
66
67 #define SPLIT_CMDLINE_BAD_ENDING 1
68 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
69 #define SPLIT_CMDLINE_ARGC_OVERFLOW 3
70 static const char *split_cmdline_errors[] = {
71 N_("cmdline ends with \\"),
72 N_("unclosed quote"),
73 N_("too many arguments"),
74 };
75
76 int split_cmdline(char *cmdline, const char ***argv)
77 {
78 size_t src, dst, count = 0, size = 16;
79 char quoted = 0;
80
81 ALLOC_ARRAY(*argv, size);
82
83 /* split alias_string */
84 (*argv)[count++] = cmdline;
85 for (src = dst = 0; cmdline[src];) {
86 char c = cmdline[src];
87 if (!quoted && isspace(c)) {
88 cmdline[dst++] = 0;
89 while (cmdline[++src]
90 && isspace(cmdline[src]))
91 ; /* skip */
92 ALLOC_GROW(*argv, count + 1, size);
93 (*argv)[count++] = cmdline + dst;
94 } else if (!quoted && (c == '\'' || c == '"')) {
95 quoted = c;
96 src++;
97 } else if (c == quoted) {
98 quoted = 0;
99 src++;
100 } else {
101 if (c == '\\' && quoted != '\'') {
102 src++;
103 c = cmdline[src];
104 if (!c) {
105 FREE_AND_NULL(*argv);
106 return -SPLIT_CMDLINE_BAD_ENDING;
107 }
108 }
109 cmdline[dst++] = c;
110 src++;
111 }
112 }
113
114 cmdline[dst] = 0;
115
116 if (quoted) {
117 FREE_AND_NULL(*argv);
118 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
119 }
120
121 if (count >= INT_MAX) {
122 FREE_AND_NULL(*argv);
123 return -SPLIT_CMDLINE_ARGC_OVERFLOW;
124 }
125
126 ALLOC_GROW(*argv, count + 1, size);
127 (*argv)[count] = NULL;
128
129 return count;
130 }
131
132 const char *split_cmdline_strerror(int split_cmdline_errno)
133 {
134 return split_cmdline_errors[-split_cmdline_errno - 1];
135 }