1 #include "git-compat-util.h"
6 #include "string-list.h"
8 struct config_alias_data
{
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
;
20 if (!skip_prefix(key
, "alias.", &p
))
24 if (!strcasecmp(p
, data
->alias
)) {
25 FREE_AND_NULL(data
->v
);
26 return git_config_string(&data
->v
,
29 } else if (data
->list
) {
30 string_list_append(data
->list
, p
);
36 char *alias_lookup(const char *alias
)
38 struct config_alias_data data
= { alias
, NULL
};
40 read_early_config(config_alias_cb
, &data
);
45 void list_aliases(struct string_list
*list
)
47 struct config_alias_data data
= { NULL
, NULL
, list
};
49 read_early_config(config_alias_cb
, &data
);
52 void quote_cmdline(struct strbuf
*buf
, const char **argv
)
54 for (const char **argp
= argv
; *argp
; argp
++) {
56 strbuf_addch(buf
, ' ');
57 strbuf_addch(buf
, '"');
58 for (const char *p
= *argp
; *p
; p
++) {
61 if (c
== '"' || c
=='\\')
62 strbuf_addch(buf
, '\\');
65 strbuf_addch(buf
, '"');
69 #define SPLIT_CMDLINE_BAD_ENDING 1
70 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
71 #define SPLIT_CMDLINE_ARGC_OVERFLOW 3
72 static const char *split_cmdline_errors
[] = {
73 N_("cmdline ends with \\"),
75 N_("too many arguments"),
78 int split_cmdline(char *cmdline
, const char ***argv
)
80 size_t src
, dst
, count
= 0, size
= 16;
83 ALLOC_ARRAY(*argv
, size
);
85 /* split alias_string */
86 (*argv
)[count
++] = cmdline
;
87 for (src
= dst
= 0; cmdline
[src
];) {
88 char c
= cmdline
[src
];
89 if (!quoted
&& isspace(c
)) {
92 && isspace(cmdline
[src
]))
94 ALLOC_GROW(*argv
, count
+ 1, size
);
95 (*argv
)[count
++] = cmdline
+ dst
;
96 } else if (!quoted
&& (c
== '\'' || c
== '"')) {
99 } else if (c
== quoted
) {
103 if (c
== '\\' && quoted
!= '\'') {
107 FREE_AND_NULL(*argv
);
108 return -SPLIT_CMDLINE_BAD_ENDING
;
119 FREE_AND_NULL(*argv
);
120 return -SPLIT_CMDLINE_UNCLOSED_QUOTE
;
123 if (count
>= INT_MAX
) {
124 FREE_AND_NULL(*argv
);
125 return -SPLIT_CMDLINE_ARGC_OVERFLOW
;
128 ALLOC_GROW(*argv
, count
+ 1, size
);
129 (*argv
)[count
] = NULL
;
134 const char *split_cmdline_strerror(int split_cmdline_errno
)
136 return split_cmdline_errors
[-split_cmdline_errno
- 1];