Merge branch 'rs/clear-commit-marks-cleanup' into maint-2.39
[git.git] / alias.c
blob00abde081739436236aa077412c3b5b686144f42
1 #include "cache.h"
2 #include "alias.h"
3 #include "config.h"
4 #include "string-list.h"
6 struct config_alias_data {
7 const char *alias;
8 char *v;
9 struct string_list *list;
12 static int config_alias_cb(const char *key, const char *value, void *d)
14 struct config_alias_data *data = d;
15 const char *p;
17 if (!skip_prefix(key, "alias.", &p))
18 return 0;
20 if (data->alias) {
21 if (!strcasecmp(p, data->alias))
22 return git_config_string((const char **)&data->v,
23 key, value);
24 } else if (data->list) {
25 string_list_append(data->list, p);
28 return 0;
31 char *alias_lookup(const char *alias)
33 struct config_alias_data data = { alias, NULL };
35 read_early_config(config_alias_cb, &data);
37 return data.v;
40 void list_aliases(struct string_list *list)
42 struct config_alias_data data = { NULL, NULL, list };
44 read_early_config(config_alias_cb, &data);
47 #define SPLIT_CMDLINE_BAD_ENDING 1
48 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
49 #define SPLIT_CMDLINE_ARGC_OVERFLOW 3
50 static const char *split_cmdline_errors[] = {
51 N_("cmdline ends with \\"),
52 N_("unclosed quote"),
53 N_("too many arguments"),
56 int split_cmdline(char *cmdline, const char ***argv)
58 size_t src, dst, count = 0, size = 16;
59 char quoted = 0;
61 ALLOC_ARRAY(*argv, size);
63 /* split alias_string */
64 (*argv)[count++] = cmdline;
65 for (src = dst = 0; cmdline[src];) {
66 char c = cmdline[src];
67 if (!quoted && isspace(c)) {
68 cmdline[dst++] = 0;
69 while (cmdline[++src]
70 && isspace(cmdline[src]))
71 ; /* skip */
72 ALLOC_GROW(*argv, count + 1, size);
73 (*argv)[count++] = cmdline + dst;
74 } else if (!quoted && (c == '\'' || c == '"')) {
75 quoted = c;
76 src++;
77 } else if (c == quoted) {
78 quoted = 0;
79 src++;
80 } else {
81 if (c == '\\' && quoted != '\'') {
82 src++;
83 c = cmdline[src];
84 if (!c) {
85 FREE_AND_NULL(*argv);
86 return -SPLIT_CMDLINE_BAD_ENDING;
89 cmdline[dst++] = c;
90 src++;
94 cmdline[dst] = 0;
96 if (quoted) {
97 FREE_AND_NULL(*argv);
98 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
101 if (count >= INT_MAX) {
102 FREE_AND_NULL(*argv);
103 return -SPLIT_CMDLINE_ARGC_OVERFLOW;
106 ALLOC_GROW(*argv, count + 1, size);
107 (*argv)[count] = NULL;
109 return count;
112 const char *split_cmdline_strerror(int split_cmdline_errno)
114 return split_cmdline_errors[-split_cmdline_errno - 1];