Merge branch 'nd/git-daemon-error-msgs'
[git/dscho.git] / alias.c
blobeb9f08b912b2089d434926fc6083ac5ab90c73bc
1 #include "cache.h"
3 static const char *alias_key;
4 static char *alias_val;
6 static int alias_lookup_cb(const char *k, const char *v, void *cb)
8 if (!prefixcmp(k, "alias.") && !strcmp(k+6, alias_key)) {
9 if (!v)
10 return config_error_nonbool(k);
11 alias_val = xstrdup(v);
12 return 0;
14 return 0;
17 char *alias_lookup(const char *alias)
19 alias_key = alias;
20 alias_val = NULL;
21 git_config(alias_lookup_cb, NULL);
22 return alias_val;
25 #define SPLIT_CMDLINE_BAD_ENDING 1
26 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
27 static const char *split_cmdline_errors[] = {
28 "cmdline ends with \\",
29 "unclosed quote"
32 int split_cmdline(char *cmdline, const char ***argv)
34 int src, dst, count = 0, size = 16;
35 char quoted = 0;
37 *argv = xmalloc(sizeof(char *) * size);
39 /* split alias_string */
40 (*argv)[count++] = cmdline;
41 for (src = dst = 0; cmdline[src];) {
42 char c = cmdline[src];
43 if (!quoted && isspace(c)) {
44 cmdline[dst++] = 0;
45 while (cmdline[++src]
46 && isspace(cmdline[src]))
47 ; /* skip */
48 ALLOC_GROW(*argv, count+1, size);
49 (*argv)[count++] = cmdline + dst;
50 } else if (!quoted && (c == '\'' || c == '"')) {
51 quoted = c;
52 src++;
53 } else if (c == quoted) {
54 quoted = 0;
55 src++;
56 } else {
57 if (c == '\\' && quoted != '\'') {
58 src++;
59 c = cmdline[src];
60 if (!c) {
61 free(*argv);
62 *argv = NULL;
63 return -SPLIT_CMDLINE_BAD_ENDING;
66 cmdline[dst++] = c;
67 src++;
71 cmdline[dst] = 0;
73 if (quoted) {
74 free(*argv);
75 *argv = NULL;
76 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
79 ALLOC_GROW(*argv, count+1, size);
80 (*argv)[count] = NULL;
82 return count;
85 const char *split_cmdline_strerror(int split_cmdline_errno) {
86 return split_cmdline_errors[-split_cmdline_errno-1];