]> git.ipfire.org Git - thirdparty/git.git/blame - alias.c
builtin/merge.c: free "&buf" on "Your local changes..." error
[thirdparty/git.git] / alias.c
CommitLineData
94351118 1#include "cache.h"
65b5f948 2#include "alias.h"
b2141fc1 3#include "config.h"
3301d36b 4#include "string-list.h"
94351118 5
a9bcf658
JS
6struct config_alias_data {
7 const char *alias;
8 char *v;
3301d36b 9 struct string_list *list;
a9bcf658
JS
10};
11
12static int config_alias_cb(const char *key, const char *value, void *d)
13{
14 struct config_alias_data *data = d;
15 const char *p;
16
3301d36b
NTND
17 if (!skip_prefix(key, "alias.", &p))
18 return 0;
19
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);
26 }
a9bcf658
JS
27
28 return 0;
29}
30
94351118
JK
31char *alias_lookup(const char *alias)
32{
a9bcf658
JS
33 struct config_alias_data data = { alias, NULL };
34
35 read_early_config(config_alias_cb, &data);
36
37 return data.v;
94351118 38}
0989fe96 39
3301d36b
NTND
40void list_aliases(struct string_list *list)
41{
42 struct config_alias_data data = { NULL, NULL, list };
43
44 read_early_config(config_alias_cb, &data);
45}
46
ad9ac6db
GB
47#define SPLIT_CMDLINE_BAD_ENDING 1
48#define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
0ca6ead8 49#define SPLIT_CMDLINE_ARGC_OVERFLOW 3
ad9ac6db 50static const char *split_cmdline_errors[] = {
a7412ae1 51 N_("cmdline ends with \\"),
0ca6ead8
KB
52 N_("unclosed quote"),
53 N_("too many arguments"),
ad9ac6db
GB
54};
55
0989fe96
MV
56int split_cmdline(char *cmdline, const char ***argv)
57{
0ca6ead8 58 size_t src, dst, count = 0, size = 16;
0989fe96
MV
59 char quoted = 0;
60
b32fa95f 61 ALLOC_ARRAY(*argv, size);
0989fe96
MV
62
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 */
cc108379 72 ALLOC_GROW(*argv, count + 1, size);
0989fe96
MV
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) {
6a83d902 85 FREE_AND_NULL(*argv);
ad9ac6db 86 return -SPLIT_CMDLINE_BAD_ENDING;
0989fe96
MV
87 }
88 }
89 cmdline[dst++] = c;
90 src++;
91 }
92 }
93
94 cmdline[dst] = 0;
95
96 if (quoted) {
6a83d902 97 FREE_AND_NULL(*argv);
ad9ac6db 98 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
0989fe96
MV
99 }
100
0ca6ead8
KB
101 if (count >= INT_MAX) {
102 FREE_AND_NULL(*argv);
103 return -SPLIT_CMDLINE_ARGC_OVERFLOW;
104 }
105
cc108379 106 ALLOC_GROW(*argv, count + 1, size);
27d5438d
JK
107 (*argv)[count] = NULL;
108
0989fe96
MV
109 return count;
110}
111
cc108379
FC
112const char *split_cmdline_strerror(int split_cmdline_errno)
113{
114 return split_cmdline_errors[-split_cmdline_errno - 1];
ad9ac6db 115}