coccinelle: make use of the "type" FREE_AND_NULL() rule
[git] / alias.c
1 #include "cache.h"
2
3 char *alias_lookup(const char *alias)
4 {
5         char *v = NULL;
6         struct strbuf key = STRBUF_INIT;
7         strbuf_addf(&key, "alias.%s", alias);
8         if (git_config_key_is_valid(key.buf))
9                 git_config_get_string(key.buf, &v);
10         strbuf_release(&key);
11         return v;
12 }
13
14 #define SPLIT_CMDLINE_BAD_ENDING 1
15 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
16 static const char *split_cmdline_errors[] = {
17         "cmdline ends with \\",
18         "unclosed quote"
19 };
20
21 int split_cmdline(char *cmdline, const char ***argv)
22 {
23         int src, dst, count = 0, size = 16;
24         char quoted = 0;
25
26         ALLOC_ARRAY(*argv, size);
27
28         /* split alias_string */
29         (*argv)[count++] = cmdline;
30         for (src = dst = 0; cmdline[src];) {
31                 char c = cmdline[src];
32                 if (!quoted && isspace(c)) {
33                         cmdline[dst++] = 0;
34                         while (cmdline[++src]
35                                         && isspace(cmdline[src]))
36                                 ; /* skip */
37                         ALLOC_GROW(*argv, count + 1, size);
38                         (*argv)[count++] = cmdline + dst;
39                 } else if (!quoted && (c == '\'' || c == '"')) {
40                         quoted = c;
41                         src++;
42                 } else if (c == quoted) {
43                         quoted = 0;
44                         src++;
45                 } else {
46                         if (c == '\\' && quoted != '\'') {
47                                 src++;
48                                 c = cmdline[src];
49                                 if (!c) {
50                                         FREE_AND_NULL(*argv);
51                                         return -SPLIT_CMDLINE_BAD_ENDING;
52                                 }
53                         }
54                         cmdline[dst++] = c;
55                         src++;
56                 }
57         }
58
59         cmdline[dst] = 0;
60
61         if (quoted) {
62                 FREE_AND_NULL(*argv);
63                 return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
64         }
65
66         ALLOC_GROW(*argv, count + 1, size);
67         (*argv)[count] = NULL;
68
69         return count;
70 }
71
72 const char *split_cmdline_strerror(int split_cmdline_errno)
73 {
74         return split_cmdline_errors[-split_cmdline_errno - 1];
75 }