add git_path_buf helper function
[git/git-svn.git] / alias.c
bloba11229db9e67b7b651356be3fb1d4b3d1014a8bd
1 #include "cache.h"
3 char *alias_lookup(const char *alias)
5 char *v = NULL;
6 struct strbuf key = STRBUF_INIT;
7 strbuf_addf(&key, "alias.%s", alias);
8 if (git_config_key_is_valid(key.buf))
9 git_config_get_string(key.buf, &v);
10 strbuf_release(&key);
11 return v;
14 #define SPLIT_CMDLINE_BAD_ENDING 1
15 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
16 static const char *split_cmdline_errors[] = {
17 "cmdline ends with \\",
18 "unclosed quote"
21 int split_cmdline(char *cmdline, const char ***argv)
23 int src, dst, count = 0, size = 16;
24 char quoted = 0;
26 *argv = xmalloc(sizeof(**argv) * size);
28 /* split alias_string */
29 (*argv)[count++] = cmdline;
30 for (src = dst = 0; cmdline[src];) {
31 char c = cmdline[src];
32 if (!quoted && isspace(c)) {
33 cmdline[dst++] = 0;
34 while (cmdline[++src]
35 && isspace(cmdline[src]))
36 ; /* skip */
37 ALLOC_GROW(*argv, count + 1, size);
38 (*argv)[count++] = cmdline + dst;
39 } else if (!quoted && (c == '\'' || c == '"')) {
40 quoted = c;
41 src++;
42 } else if (c == quoted) {
43 quoted = 0;
44 src++;
45 } else {
46 if (c == '\\' && quoted != '\'') {
47 src++;
48 c = cmdline[src];
49 if (!c) {
50 free(*argv);
51 *argv = NULL;
52 return -SPLIT_CMDLINE_BAD_ENDING;
55 cmdline[dst++] = c;
56 src++;
60 cmdline[dst] = 0;
62 if (quoted) {
63 free(*argv);
64 *argv = NULL;
65 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
68 ALLOC_GROW(*argv, count + 1, size);
69 (*argv)[count] = NULL;
71 return count;
74 const char *split_cmdline_strerror(int split_cmdline_errno)
76 return split_cmdline_errors[-split_cmdline_errno - 1];