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 return git_config_string((const char **)&data
->v
,
27 } else if (data
->list
) {
28 string_list_append(data
->list
, p
);
34 char *alias_lookup(const char *alias
)
36 struct config_alias_data data
= { alias
, NULL
};
38 read_early_config(config_alias_cb
, &data
);
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
++) {
54 strbuf_addch(buf
, ' ');
55 strbuf_addch(buf
, '"');
56 for (const char *p
= *argp
; *p
; p
++) {
59 if (c
== '"' || c
=='\\')
60 strbuf_addch(buf
, '\\');
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 \\"),
73 N_("too many arguments"),
76 int split_cmdline(char *cmdline
, const char ***argv
)
78 size_t src
, dst
, count
= 0, size
= 16;
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
)) {
90 && isspace(cmdline
[src
]))
92 ALLOC_GROW(*argv
, count
+ 1, size
);
93 (*argv
)[count
++] = cmdline
+ dst
;
94 } else if (!quoted
&& (c
== '\'' || c
== '"')) {
97 } else if (c
== quoted
) {
101 if (c
== '\\' && quoted
!= '\'') {
105 FREE_AND_NULL(*argv
);
106 return -SPLIT_CMDLINE_BAD_ENDING
;
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
;
132 const char *split_cmdline_strerror(int split_cmdline_errno
)
134 return split_cmdline_errors
[-split_cmdline_errno
- 1];