Merge tag 'v2.41.0-rc0'
[alt-git.git] / alias.c
blob54a1a23d2cf1a5983e47f4369fe08ef935616508
1 #include "git-compat-util.h"
2 #include "alias.h"
3 #include "alloc.h"
4 #include "config.h"
5 #include "gettext.h"
6 #include "strbuf.h"
7 #include "string-list.h"
9 struct config_alias_data {
10 const char *alias;
11 char *v;
12 struct string_list *list;
15 static int config_alias_cb(const char *key, const char *value, void *d)
17 struct config_alias_data *data = d;
18 const char *p;
20 if (!skip_prefix(key, "alias.", &p))
21 return 0;
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);
31 return 0;
34 char *alias_lookup(const char *alias)
36 struct config_alias_data data = { alias, NULL };
38 read_early_config(config_alias_cb, &data);
40 return data.v;
43 void list_aliases(struct string_list *list)
45 struct config_alias_data data = { NULL, NULL, list };
47 read_early_config(config_alias_cb, &data);
50 void quote_cmdline(struct strbuf *buf, const char **argv)
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;
59 if (c == '"' || c =='\\')
60 strbuf_addch(buf, '\\');
61 strbuf_addch(buf, c);
63 strbuf_addch(buf, '"');
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"),
76 int split_cmdline(char *cmdline, const char ***argv)
78 size_t src, dst, count = 0, size = 16;
79 char quoted = 0;
81 ALLOC_ARRAY(*argv, size);
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;
109 cmdline[dst++] = c;
110 src++;
114 cmdline[dst] = 0;
116 if (quoted) {
117 FREE_AND_NULL(*argv);
118 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
121 if (count >= INT_MAX) {
122 FREE_AND_NULL(*argv);
123 return -SPLIT_CMDLINE_ARGC_OVERFLOW;
126 ALLOC_GROW(*argv, count + 1, size);
127 (*argv)[count] = NULL;
129 return count;
132 const char *split_cmdline_strerror(int split_cmdline_errno)
134 return split_cmdline_errors[-split_cmdline_errno - 1];