completion: add and use --list-cmds=nohelpers
[git/git.git] / alias.c
CommitLineData
94351118 1#include "cache.h"
65b5f948 2#include "alias.h"
b2141fc1 3#include "config.h"
94351118 4
a9bcf658
JS
5struct config_alias_data {
6 const char *alias;
7 char *v;
8};
9
10static int config_alias_cb(const char *key, const char *value, void *d)
11{
12 struct config_alias_data *data = d;
13 const char *p;
14
643df7e2 15 if (skip_prefix(key, "alias.", &p) && !strcasecmp(p, data->alias))
a9bcf658
JS
16 return git_config_string((const char **)&data->v, key, value);
17
18 return 0;
19}
20
94351118
JK
21char *alias_lookup(const char *alias)
22{
a9bcf658
JS
23 struct config_alias_data data = { alias, NULL };
24
25 read_early_config(config_alias_cb, &data);
26
27 return data.v;
94351118 28}
0989fe96 29
ad9ac6db
GB
30#define SPLIT_CMDLINE_BAD_ENDING 1
31#define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
32static const char *split_cmdline_errors[] = {
33 "cmdline ends with \\",
34 "unclosed quote"
35};
36
0989fe96
MV
37int split_cmdline(char *cmdline, const char ***argv)
38{
39 int src, dst, count = 0, size = 16;
40 char quoted = 0;
41
b32fa95f 42 ALLOC_ARRAY(*argv, size);
0989fe96
MV
43
44 /* split alias_string */
45 (*argv)[count++] = cmdline;
46 for (src = dst = 0; cmdline[src];) {
47 char c = cmdline[src];
48 if (!quoted && isspace(c)) {
49 cmdline[dst++] = 0;
50 while (cmdline[++src]
51 && isspace(cmdline[src]))
52 ; /* skip */
cc108379 53 ALLOC_GROW(*argv, count + 1, size);
0989fe96
MV
54 (*argv)[count++] = cmdline + dst;
55 } else if (!quoted && (c == '\'' || c == '"')) {
56 quoted = c;
57 src++;
58 } else if (c == quoted) {
59 quoted = 0;
60 src++;
61 } else {
62 if (c == '\\' && quoted != '\'') {
63 src++;
64 c = cmdline[src];
65 if (!c) {
6a83d902 66 FREE_AND_NULL(*argv);
ad9ac6db 67 return -SPLIT_CMDLINE_BAD_ENDING;
0989fe96
MV
68 }
69 }
70 cmdline[dst++] = c;
71 src++;
72 }
73 }
74
75 cmdline[dst] = 0;
76
77 if (quoted) {
6a83d902 78 FREE_AND_NULL(*argv);
ad9ac6db 79 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
0989fe96
MV
80 }
81
cc108379 82 ALLOC_GROW(*argv, count + 1, size);
27d5438d
JK
83 (*argv)[count] = NULL;
84
0989fe96
MV
85 return count;
86}
87
cc108379
FC
88const char *split_cmdline_strerror(int split_cmdline_errno)
89{
90 return split_cmdline_errors[-split_cmdline_errno - 1];
ad9ac6db 91}