t2020: abstract away SHA-1 specific constants
[git/debian.git] / alias.c
blobbf146e526329309df360b9f1028ca2f767743efa
1 #include "cache.h"
2 #include "config.h"
4 struct config_alias_data {
5 const char *alias;
6 char *v;
7 };
9 static int config_alias_cb(const char *key, const char *value, void *d)
11 struct config_alias_data *data = d;
12 const char *p;
14 if (skip_prefix(key, "alias.", &p) && !strcasecmp(p, data->alias))
15 return git_config_string((const char **)&data->v, key, value);
17 return 0;
20 char *alias_lookup(const char *alias)
22 struct config_alias_data data = { alias, NULL };
24 read_early_config(config_alias_cb, &data);
26 return data.v;
29 #define SPLIT_CMDLINE_BAD_ENDING 1
30 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
31 static const char *split_cmdline_errors[] = {
32 "cmdline ends with \\",
33 "unclosed quote"
36 int split_cmdline(char *cmdline, const char ***argv)
38 int src, dst, count = 0, size = 16;
39 char quoted = 0;
41 ALLOC_ARRAY(*argv, size);
43 /* split alias_string */
44 (*argv)[count++] = cmdline;
45 for (src = dst = 0; cmdline[src];) {
46 char c = cmdline[src];
47 if (!quoted && isspace(c)) {
48 cmdline[dst++] = 0;
49 while (cmdline[++src]
50 && isspace(cmdline[src]))
51 ; /* skip */
52 ALLOC_GROW(*argv, count + 1, size);
53 (*argv)[count++] = cmdline + dst;
54 } else if (!quoted && (c == '\'' || c == '"')) {
55 quoted = c;
56 src++;
57 } else if (c == quoted) {
58 quoted = 0;
59 src++;
60 } else {
61 if (c == '\\' && quoted != '\'') {
62 src++;
63 c = cmdline[src];
64 if (!c) {
65 FREE_AND_NULL(*argv);
66 return -SPLIT_CMDLINE_BAD_ENDING;
69 cmdline[dst++] = c;
70 src++;
74 cmdline[dst] = 0;
76 if (quoted) {
77 FREE_AND_NULL(*argv);
78 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
81 ALLOC_GROW(*argv, count + 1, size);
82 (*argv)[count] = NULL;
84 return count;
87 const char *split_cmdline_strerror(int split_cmdline_errno)
89 return split_cmdline_errors[-split_cmdline_errno - 1];