*.sh: avoid hardcoding $GIT_DIR/hooks/...
[git/jrn.git] / alias.c
blob758c8671494ad003a6e7e33f8a14b5450311bf54
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 const char *name;
9 if (skip_prefix(k, "alias.", &name) && !strcmp(name, alias_key)) {
10 if (!v)
11 return config_error_nonbool(k);
12 alias_val = xstrdup(v);
13 return 0;
15 return 0;
18 char *alias_lookup(const char *alias)
20 alias_key = alias;
21 alias_val = NULL;
22 git_config(alias_lookup_cb, NULL);
23 return alias_val;
26 #define SPLIT_CMDLINE_BAD_ENDING 1
27 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
28 static const char *split_cmdline_errors[] = {
29 "cmdline ends with \\",
30 "unclosed quote"
33 int split_cmdline(char *cmdline, const char ***argv)
35 int src, dst, count = 0, size = 16;
36 char quoted = 0;
38 *argv = xmalloc(sizeof(**argv) * size);
40 /* split alias_string */
41 (*argv)[count++] = cmdline;
42 for (src = dst = 0; cmdline[src];) {
43 char c = cmdline[src];
44 if (!quoted && isspace(c)) {
45 cmdline[dst++] = 0;
46 while (cmdline[++src]
47 && isspace(cmdline[src]))
48 ; /* skip */
49 ALLOC_GROW(*argv, count + 1, size);
50 (*argv)[count++] = cmdline + dst;
51 } else if (!quoted && (c == '\'' || c == '"')) {
52 quoted = c;
53 src++;
54 } else if (c == quoted) {
55 quoted = 0;
56 src++;
57 } else {
58 if (c == '\\' && quoted != '\'') {
59 src++;
60 c = cmdline[src];
61 if (!c) {
62 free(*argv);
63 *argv = NULL;
64 return -SPLIT_CMDLINE_BAD_ENDING;
67 cmdline[dst++] = c;
68 src++;
72 cmdline[dst] = 0;
74 if (quoted) {
75 free(*argv);
76 *argv = NULL;
77 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
80 ALLOC_GROW(*argv, count + 1, size);
81 (*argv)[count] = NULL;
83 return count;
86 const char *split_cmdline_strerror(int split_cmdline_errno)
88 return split_cmdline_errors[-split_cmdline_errno - 1];