]> git.ipfire.org Git - thirdparty/git.git/blame - alias.c
alloc.h: move ALLOC_GROW() functions from cache.h
[thirdparty/git.git] / alias.c
CommitLineData
36bf1958 1#include "git-compat-util.h"
65b5f948 2#include "alias.h"
36bf1958 3#include "alloc.h"
b2141fc1 4#include "config.h"
36bf1958 5#include "gettext.h"
3301d36b 6#include "string-list.h"
94351118 7
a9bcf658
JS
8struct config_alias_data {
9 const char *alias;
10 char *v;
3301d36b 11 struct string_list *list;
a9bcf658
JS
12};
13
14static int config_alias_cb(const char *key, const char *value, void *d)
15{
16 struct config_alias_data *data = d;
17 const char *p;
18
3301d36b
NTND
19 if (!skip_prefix(key, "alias.", &p))
20 return 0;
21
22 if (data->alias) {
23 if (!strcasecmp(p, data->alias))
24 return git_config_string((const char **)&data->v,
25 key, value);
26 } else if (data->list) {
27 string_list_append(data->list, p);
28 }
a9bcf658
JS
29
30 return 0;
31}
32
94351118
JK
33char *alias_lookup(const char *alias)
34{
a9bcf658
JS
35 struct config_alias_data data = { alias, NULL };
36
37 read_early_config(config_alias_cb, &data);
38
39 return data.v;
94351118 40}
0989fe96 41
3301d36b
NTND
42void list_aliases(struct string_list *list)
43{
44 struct config_alias_data data = { NULL, NULL, list };
45
46 read_early_config(config_alias_cb, &data);
47}
48
ad9ac6db
GB
49#define SPLIT_CMDLINE_BAD_ENDING 1
50#define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
0ca6ead8 51#define SPLIT_CMDLINE_ARGC_OVERFLOW 3
ad9ac6db 52static const char *split_cmdline_errors[] = {
a7412ae1 53 N_("cmdline ends with \\"),
0ca6ead8
KB
54 N_("unclosed quote"),
55 N_("too many arguments"),
ad9ac6db
GB
56};
57
0989fe96
MV
58int split_cmdline(char *cmdline, const char ***argv)
59{
0ca6ead8 60 size_t src, dst, count = 0, size = 16;
0989fe96
MV
61 char quoted = 0;
62
b32fa95f 63 ALLOC_ARRAY(*argv, size);
0989fe96
MV
64
65 /* split alias_string */
66 (*argv)[count++] = cmdline;
67 for (src = dst = 0; cmdline[src];) {
68 char c = cmdline[src];
69 if (!quoted && isspace(c)) {
70 cmdline[dst++] = 0;
71 while (cmdline[++src]
72 && isspace(cmdline[src]))
73 ; /* skip */
cc108379 74 ALLOC_GROW(*argv, count + 1, size);
0989fe96
MV
75 (*argv)[count++] = cmdline + dst;
76 } else if (!quoted && (c == '\'' || c == '"')) {
77 quoted = c;
78 src++;
79 } else if (c == quoted) {
80 quoted = 0;
81 src++;
82 } else {
83 if (c == '\\' && quoted != '\'') {
84 src++;
85 c = cmdline[src];
86 if (!c) {
6a83d902 87 FREE_AND_NULL(*argv);
ad9ac6db 88 return -SPLIT_CMDLINE_BAD_ENDING;
0989fe96
MV
89 }
90 }
91 cmdline[dst++] = c;
92 src++;
93 }
94 }
95
96 cmdline[dst] = 0;
97
98 if (quoted) {
6a83d902 99 FREE_AND_NULL(*argv);
ad9ac6db 100 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
0989fe96
MV
101 }
102
0ca6ead8
KB
103 if (count >= INT_MAX) {
104 FREE_AND_NULL(*argv);
105 return -SPLIT_CMDLINE_ARGC_OVERFLOW;
106 }
107
cc108379 108 ALLOC_GROW(*argv, count + 1, size);
27d5438d
JK
109 (*argv)[count] = NULL;
110
0989fe96
MV
111 return count;
112}
113
cc108379
FC
114const char *split_cmdline_strerror(int split_cmdline_errno)
115{
116 return split_cmdline_errors[-split_cmdline_errno - 1];
ad9ac6db 117}