summaryrefslogtreecommitdiff
path: root/alias.c
diff options
context:
space:
mode:
authorMiklos Vajna <vmiklos@frugalware.org>2008-06-27 18:21:54 +0200
committerJunio C Hamano <gitster@pobox.com>2008-06-30 22:45:50 -0700
commit0989fe9623dc8d98033f6acdcc0c84ec28571b19 (patch)
tree663e320d3a58266368eaeddaea3e1d7c29e8518f /alias.c
parent66037991d3fd2ac7e699c7bd21d939b9e397f6a4 (diff)
downloadgit-0989fe9623dc8d98033f6acdcc0c84ec28571b19.tar.gz
Move split_cmdline() to alias.c
split_cmdline() is currently used for aliases only, but later it can be useful for other builtins as well. Move it to alias.c for now, indicating that originally it's for aliases, but we'll have it in libgit this way. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'alias.c')
-rw-r--r--alias.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/alias.c b/alias.c
index 995f3e6a0a..ccb1108c94 100644
--- a/alias.c
+++ b/alias.c
@@ -21,3 +21,57 @@ char *alias_lookup(const char *alias)
git_config(alias_lookup_cb, NULL);
return alias_val;
}
+
+int split_cmdline(char *cmdline, const char ***argv)
+{
+ int src, dst, count = 0, size = 16;
+ char quoted = 0;
+
+ *argv = xmalloc(sizeof(char*) * size);
+
+ /* split alias_string */
+ (*argv)[count++] = cmdline;
+ for (src = dst = 0; cmdline[src];) {
+ char c = cmdline[src];
+ if (!quoted && isspace(c)) {
+ cmdline[dst++] = 0;
+ while (cmdline[++src]
+ && isspace(cmdline[src]))
+ ; /* skip */
+ if (count >= size) {
+ size += 16;
+ *argv = xrealloc(*argv, sizeof(char*) * size);
+ }
+ (*argv)[count++] = cmdline + dst;
+ } else if (!quoted && (c == '\'' || c == '"')) {
+ quoted = c;
+ src++;
+ } else if (c == quoted) {
+ quoted = 0;
+ src++;
+ } else {
+ if (c == '\\' && quoted != '\'') {
+ src++;
+ c = cmdline[src];
+ if (!c) {
+ free(*argv);
+ *argv = NULL;
+ return error("cmdline ends with \\");
+ }
+ }
+ cmdline[dst++] = c;
+ src++;
+ }
+ }
+
+ cmdline[dst] = 0;
+
+ if (quoted) {
+ free(*argv);
+ *argv = NULL;
+ return error("unclosed quote");
+ }
+
+ return count;
+}
+