index f364930235546fc7c22bcc7e9a596097a94ad67e..3e4d369063259d67589aabae0c8a8a47d871148a 100644 (file)
--- a/tig.c
+++ b/tig.c
{
int argc;
+ if (!argv)
+ return;
for (argc = 0; argv[argc]; argc++)
free((void *) argv[argc]);
argv[0] = NULL;
}
+DEFINE_ALLOCATOR(argv_realloc, const char *, SIZEOF_ARG)
+
+static bool
+argv_append(const char ***argv, const char *arg)
+{
+ int argc = 0;
+
+ while (*argv && (*argv)[argc])
+ argc++;
+
+ if (!argv_realloc(argv, argc, 2))
+ return FALSE;
+
+ (*argv)[argc++] = strdup(arg);
+ (*argv)[argc] = NULL;
+ return TRUE;
+}
+
static bool
-argv_copy(const char *dst[], const char *src[])
+argv_copy(const char ***dst, const char *src[])
{
int argc;
for (argc = 0; src[argc]; argc++)
- if (!(dst[argc] = strdup(src[argc])))
+ if (!argv_append(dst, src[argc]))
return FALSE;
return TRUE;
}
};
struct io {
- pid_t pid; /* PID of spawned process. */
int pipe; /* Pipe end for reading or writing. */
+ pid_t pid; /* PID of spawned process. */
int error; /* Error status. */
char *buf; /* Read buffer. */
size_t bufalloc; /* Allocated buffer size. */
unsigned int eof:1; /* Has end of file been reached. */
};
-static void
-io_reset(struct io *io)
-{
- io->pipe = -1;
- io->pid = 0;
- io->buf = io->bufpos = NULL;
- io->bufalloc = io->bufsize = 0;
- io->error = 0;
- io->eof = 0;
-}
-
static void
io_init(struct io *io)
{
- io_reset(io);
+ memset(io, 0, sizeof(*io));
+ io->pipe = -1;
}
static bool
if (io->pipe != -1)
close(io->pipe);
free(io->buf);
- io_reset(io);
+ io_init(io);
while (pid > 0) {
int status;
}
static bool
-io_start(struct io *io, enum io_type type, const char *dir, const char *argv[])
+io_run(struct io *io, enum io_type type, const char *dir, const char *argv[], ...)
{
int pipefds[2] = { -1, -1 };
+ va_list args;
+
+ io_init(io);
if ((type == IO_RD || type == IO_WR) && pipe(pipefds) < 0) {
io->error = errno;
return FALSE;
} else if (type == IO_AP) {
- pipefds[1] = io->pipe;
+ va_start(args, argv);
+ pipefds[1] = va_arg(args, int);
+ va_end(args);
}
if ((io->pid = fork())) {
@@ -845,21 +860,12 @@ io_start(struct io *io, enum io_type type, const char *dir, const char *argv[])
return FALSE;
}
-static bool
-io_run(struct io *io, const char **argv, const char *dir, enum io_type type)
-{
- io_init(io);
- return io_start(io, type, dir, argv);
-}
-
static bool
io_complete(enum io_type type, const char **argv, const char *dir, int fd)
{
- struct io io = {};
+ struct io io;
- io_init(&io);
- io.pipe = fd;
- return io_start(&io, type, dir, argv) && io_done(&io);
+ return io_run(&io, type, dir, argv, fd) && io_done(&io);
}
static bool
static bool
io_run_append(const char **argv, int fd)
{
- return io_complete(IO_AP, argv, NULL, -1);
+ return io_complete(IO_AP, argv, NULL, fd);
}
static bool
static bool
io_run_buf(const char **argv, char buf[], size_t bufsize)
{
- struct io io = {};
+ struct io io;
- io_init(&io);
- return io_start(&io, IO_RD, NULL, argv) && io_read_buf(&io, buf, bufsize);
+ return io_run(&io, IO_RD, NULL, argv) && io_read_buf(&io, buf, bufsize);
}
static int
io_run_load(const char **argv, const char *separators,
int (*read_property)(char *, size_t, char *, size_t))
{
- struct io io = {};
+ struct io io;
- io_init(&io);
- if (!io_start(&io, IO_RD, NULL, argv))
+ if (!io_run(&io, IO_RD, NULL, argv))
return ERR;
return io_load(&io, separators, read_property);
}
struct run_request {
enum keymap keymap;
int key;
- const char *argv[SIZEOF_ARG];
+ const char **argv;
};
static struct run_request *run_request;
DEFINE_ALLOCATOR(realloc_run_requests, struct run_request, 8)
static enum request
-add_run_request(enum keymap keymap, int key, int argc, const char **argv)
+add_run_request(enum keymap keymap, int key, const char **argv)
{
struct run_request *req;
- if (argc >= ARRAY_SIZE(req->argv) - 1)
- return REQ_NONE;
-
if (!realloc_run_requests(&run_request, run_requests, 1))
return REQ_NONE;
req = &run_request[run_requests];
req->keymap = keymap;
req->key = key;
- req->argv[0] = NULL;
+ req->argv = NULL;
- if (!argv_copy(req->argv, argv))
+ if (!argv_copy(&req->argv, argv))
return REQ_NONE;
return REQ_NONE + ++run_requests;
const char *checkout[] = { "git", "checkout", "%(branch)", NULL };
const char *commit[] = { "git", "commit", NULL };
const char *gc[] = { "git", "gc", NULL };
- struct {
- enum keymap keymap;
- int key;
- int argc;
- const char **argv;
- } reqs[] = {
- { KEYMAP_MAIN, 'C', ARRAY_SIZE(cherry_pick) - 1, cherry_pick },
- { KEYMAP_STATUS, 'C', ARRAY_SIZE(commit) - 1, commit },
- { KEYMAP_BRANCH, 'C', ARRAY_SIZE(checkout) - 1, checkout },
- { KEYMAP_GENERIC, 'G', ARRAY_SIZE(gc) - 1, gc },
+ struct run_request reqs[] = {
+ { KEYMAP_MAIN, 'C', cherry_pick },
+ { KEYMAP_STATUS, 'C', commit },
+ { KEYMAP_BRANCH, 'C', checkout },
+ { KEYMAP_GENERIC, 'G', gc },
};
int i;
if (req != reqs[i].key)
continue;
- req = add_run_request(reqs[i].keymap, reqs[i].key, reqs[i].argc, reqs[i].argv);
+ req = add_run_request(reqs[i].keymap, reqs[i].key, reqs[i].argv);
if (req != REQ_NONE)
add_keybinding(reqs[i].keymap, req, reqs[i].key);
}
}
}
if (request == REQ_UNKNOWN && *argv[2]++ == '!')
- request = add_run_request(keymap, key, argc - 2, argv + 2);
+ request = add_run_request(keymap, key, argv + 2);
if (request == REQ_UNKNOWN) {
config_msg = "Unknown request name";
return ERR;
static void
load_option_file(const char *path)
{
- struct io io = {};
+ struct io io;
/* It's OK that the file doesn't exist. */
if (!io_open(&io, "%s", path))
bool has_scrolled; /* View was scrolled. */
/* Loading */
- const char *argv[SIZEOF_ARG]; /* Shell command arguments. */
+ const char **argv; /* Shell command arguments. */
const char *dir; /* Directory from which to execute. */
struct io io;
struct io *pipe;
}
static bool
-format_argv(const char *dst_argv[], const char *src_argv[], bool replace)
+format_argv(const char ***dst_argv, const char *src_argv[], bool replace)
{
char buf[SIZEOF_STR];
int argc;
- argv_free(dst_argv);
+ argv_free(*dst_argv);
for (argc = 0; src_argv[argc]; argc++) {
const char *arg = src_argv[argc];
arg = next && replace ? strchr(next, ')') + 1 : NULL;
}
- dst_argv[argc] = strdup(buf);
- if (!dst_argv[argc])
+ if (!argv_append(dst_argv, buf))
break;
}
- dst_argv[argc] = NULL;
-
return src_argv[argc] == NULL;
}
static bool
prepare_io(struct view *view, const char *dir, const char *argv[], bool replace)
{
- io_init(&view->io);
view->dir = dir;
- return format_argv(view->argv, argv, replace);
+ return format_argv(&view->argv, argv, replace);
}
static bool
if (view->pipe)
io_done(view->pipe);
return prepare_io(view, dir, argv, FALSE) &&
- io_start(&view->io, IO_RD, dir, view->argv);
+ io_run(&view->io, IO_RD, dir, view->argv);
}
static bool
string_copy_rev(view->ref, view->id);
}
- if (view->argv[0] && !io_start(&view->io, IO_RD, view->dir, view->argv))
+ if (view->argv && view->argv[0] &&
+ !io_run(&view->io, IO_RD, view->dir, view->argv))
return FALSE;
setup_update(view, view->id);
open_run_request(enum request request)
{
struct run_request *req = get_run_request(request);
- const char *argv[ARRAY_SIZE(req->argv)] = { NULL };
+ const char **argv = NULL;
if (!req) {
report("Unknown run request");
return;
}
- if (format_argv(argv, req->argv, TRUE))
+ if (format_argv(&argv, req->argv, TRUE))
open_external_viewer(argv, NULL);
- argv_free(argv);
+ if (argv)
+ argv_free(argv);
+ free(argv);
}
/*
"git", "diff-tree", "-U0", blame->commit->id,
"--", blame->commit->filename, NULL
};
- struct io io = {};
+ struct io io;
int parent_lineno = -1;
int blamed_lineno = -1;
char *line;
- if (!io_run(&io, diff_tree_argv, NULL, IO_RD))
+ if (!io_run(&io, IO_RD, NULL, diff_tree_argv))
return;
while ((line = io_get(&io, '\n', TRUE))) {
return REQ_NONE;
case REQ_ENTER:
- if (branch->ref == &branch_all) {
- const char *all_branches_argv[] = {
- "git", "log", "--no-color", "--pretty=raw", "--parents",
- "--topo-order", "--all", NULL
- };
- struct view *main_view = VIEW(REQ_VIEW_MAIN);
+ {
+ const struct ref *ref = branch->ref;
+ const char *all_branches_argv[] = {
+ "git", "log", "--no-color", "--pretty=raw", "--parents",
+ "--topo-order",
+ ref == &branch_all ? "--all" : ref->name, NULL
+ };
+ struct view *main_view = VIEW(REQ_VIEW_MAIN);
- if (!prepare_update(main_view, all_branches_argv, NULL)) {
- report("Failed to load view of all branches");
- return REQ_NONE;
- }
+ if (!prepare_update(main_view, all_branches_argv, NULL))
+ report("Failed to load view of all branches");
+ else
open_view(view, REQ_VIEW_MAIN, OPEN_PREPARED | OPEN_SPLIT);
- } else {
- open_view(view, REQ_VIEW_MAIN, OPEN_SPLIT);
- }
return REQ_NONE;
-
+ }
default:
return request;
}
@@ -5547,9 +5540,9 @@ status_run(struct view *view, const char *argv[], char status, enum line_type ty
{
struct status *unmerged = NULL;
char *buf;
- struct io io = {};
+ struct io io;
- if (!io_run(&io, argv, opt_cdup, IO_RD))
+ if (!io_run(&io, IO_RD, opt_cdup, argv))
return FALSE;
add_line_data(view, NULL, type);
continue;
if (!*opt_head) {
- struct io io = {};
+ struct io io;
if (io_open(&io, "%s/rebase-merge/head-name", opt_git_dir) &&
io_read_buf(&io, buf, sizeof(buf))) {
switch (type) {
case LINE_STAT_STAGED:
- return io_run(io, staged_argv, opt_cdup, IO_WR);
+ return io_run(io, IO_WR, opt_cdup, staged_argv);
case LINE_STAT_UNSTAGED:
case LINE_STAT_UNTRACKED:
- return io_run(io, others_argv, opt_cdup, IO_WR);
+ return io_run(io, IO_WR, opt_cdup, others_argv);
default:
die("line type %d not handled in switch", type);
@@ -5987,7 +5980,7 @@ status_update_write(struct io *io, struct status *status, enum line_type type)
static bool
status_update_file(struct status *status, enum line_type type)
{
- struct io io = {};
+ struct io io;
bool result;
if (!status_update_prepare(&io, type))
status_update_files(struct view *view, struct line *line)
{
char buf[sizeof(view->ref)];
- struct io io = {};
+ struct io io;
bool result = TRUE;
struct line *pos = view->line + view->lines;
int files = 0;
"git", "apply", "--whitespace=nowarn", NULL
};
struct line *diff_hdr;
- struct io io = {};
+ struct io io;
int argc = 3;
diff_hdr = stage_diff_find(view, chunk, LINE_DIFF_HEADER);
apply_argv[argc++] = "-R";
apply_argv[argc++] = "-";
apply_argv[argc++] = NULL;
- if (!io_run(&io, apply_argv, opt_cdup, IO_WR))
+ if (!io_run(&io, IO_WR, opt_cdup, apply_argv))
return FALSE;
if (!stage_diff_write(&io, diff_hdr, chunk) ||