refs: implement removal of ref storages
[git/gitster.git] / alias.c
blob5a238f2e3012d2fb12443b27ed02831e9edee476
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"
8 struct config_alias_data {
9 const char *alias;
10 char *v;
11 struct string_list *list;
14 static int config_alias_cb(const char *key, const char *value,
15 const struct config_context *ctx UNUSED, 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];