completion: reduce completable command list
[git/git.git] / alias.c
CommitLineData
94351118 1#include "cache.h"
b2141fc1 2#include "config.h"
94351118 3
a9bcf658
JS
4struct config_alias_data {
5 const char *alias;
6 char *v;
7};
8
9static int config_alias_cb(const char *key, const char *value, void *d)
10{
11 struct config_alias_data *data = d;
12 const char *p;
13
643df7e2 14 if (skip_prefix(key, "alias.", &p) && !strcasecmp(p, data->alias))
a9bcf658
JS
15 return git_config_string((const char **)&data->v, key, value);
16
17 return 0;
18}
19
94351118
JK
20char *alias_lookup(const char *alias)
21{
a9bcf658
JS
22 struct config_alias_data data = { alias, NULL };
23
24 read_early_config(config_alias_cb, &data);
25
26 return data.v;
94351118 27}
0989fe96 28
ad9ac6db
GB
29#define SPLIT_CMDLINE_BAD_ENDING 1
30#define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
31static const char *split_cmdline_errors[] = {
32 "cmdline ends with \\",
33 "unclosed quote"
34};
35
0989fe96
MV
36int split_cmdline(char *cmdline, const char ***argv)
37{
38 int src, dst, count = 0, size = 16;
39 char quoted = 0;
40
b32fa95f 41 ALLOC_ARRAY(*argv, size);
0989fe96
MV
42
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 */
cc108379 52 ALLOC_GROW(*argv, count + 1, size);
0989fe96
MV
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) {
6a83d902 65 FREE_AND_NULL(*argv);
ad9ac6db 66 return -SPLIT_CMDLINE_BAD_ENDING;
0989fe96
MV
67 }
68 }
69 cmdline[dst++] = c;
70 src++;
71 }
72 }
73
74 cmdline[dst] = 0;
75
76 if (quoted) {
6a83d902 77 FREE_AND_NULL(*argv);
ad9ac6db 78 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
0989fe96
MV
79 }
80
cc108379 81 ALLOC_GROW(*argv, count + 1, size);
27d5438d
JK
82 (*argv)[count] = NULL;
83
0989fe96
MV
84 return count;
85}
86
cc108379
FC
87const char *split_cmdline_strerror(int split_cmdline_errno)
88{
89 return split_cmdline_errors[-split_cmdline_errno - 1];
ad9ac6db 90}