blob: c0b43ee40d95674fb5a5602c6f287ba2d8d8c5f5 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001#include "cache.h"
2
3static const char *alias_key;
4static char *alias_val;
5
Irina Tirdea1d037ca2012-09-11 01:15:03 +03006static int alias_lookup_cb(const char *k, const char *v,
7 void *cb __maybe_unused)
Ingo Molnar07800602009-04-20 15:00:56 +02008{
9 if (!prefixcmp(k, "alias.") && !strcmp(k+6, alias_key)) {
10 if (!v)
11 return config_error_nonbool(k);
12 alias_val = strdup(v);
13 return 0;
14 }
15 return 0;
16}
17
18char *alias_lookup(const char *alias)
19{
20 alias_key = alias;
21 alias_val = NULL;
22 perf_config(alias_lookup_cb, NULL);
23 return alias_val;
24}
25
26int split_cmdline(char *cmdline, const char ***argv)
27{
28 int src, dst, count = 0, size = 16;
29 char quoted = 0;
30
31 *argv = malloc(sizeof(char*) * size);
32
33 /* split alias_string */
34 (*argv)[count++] = cmdline;
35 for (src = dst = 0; cmdline[src];) {
36 char c = cmdline[src];
37 if (!quoted && isspace(c)) {
38 cmdline[dst++] = 0;
39 while (cmdline[++src]
40 && isspace(cmdline[src]))
41 ; /* skip */
42 if (count >= size) {
43 size += 16;
44 *argv = realloc(*argv, sizeof(char*) * size);
45 }
46 (*argv)[count++] = cmdline + dst;
47 } else if (!quoted && (c == '\'' || c == '"')) {
48 quoted = c;
49 src++;
50 } else if (c == quoted) {
51 quoted = 0;
52 src++;
53 } else {
54 if (c == '\\' && quoted != '\'') {
55 src++;
56 c = cmdline[src];
57 if (!c) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -030058 zfree(argv);
Ingo Molnar07800602009-04-20 15:00:56 +020059 return error("cmdline ends with \\");
60 }
61 }
62 cmdline[dst++] = c;
63 src++;
64 }
65 }
66
67 cmdline[dst] = 0;
68
69 if (quoted) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -030070 zfree(argv);
Ingo Molnar07800602009-04-20 15:00:56 +020071 return error("unclosed quote");
72 }
73
74 return count;
75}
76