~emersion/mrsh

ref: 9371d6094e78645f0ec0c85b6cd5b6c8ec9638fc mrsh/shell/task_list.c -rw-r--r-- 1.1 KiB
9371d609Drew DeVault Implement times, : builtins 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdlib.h>
#include "shell.h"

struct task_list {
	struct task task;
	struct mrsh_array children;
	size_t current;
};

static void task_list_destroy(struct task *task) {
	struct task_list *tl = (struct task_list *)task;
	for (size_t i = 0; i < tl->children.len; ++i) {
		struct task *child = tl->children.data[i];
		task_destroy(child);
	}
	mrsh_array_finish(&tl->children);
	free(tl);
}

static int task_list_poll(struct task *task, struct context *ctx) {
	struct task_list *tl = (struct task_list *)task;

	int ret = 0;
	while (tl->current < tl->children.len) {
		struct task *child = tl->children.data[tl->current];

		ret = task_poll(child, ctx);
		if (ret < 0) {
			return ret;
		}

		++tl->current;
	}

	return ret;
}

static const struct task_interface task_list_impl = {
	.destroy = task_list_destroy,
	.poll = task_list_poll,
};

struct task *task_list_create(void) {
	struct task_list *tl = calloc(1, sizeof(struct task_list));
	task_init(&tl->task, &task_list_impl);
	return &tl->task;
}

void task_list_add(struct task *task, struct task *child) {
	struct task_list *tl = (struct task_list *)task;
	mrsh_array_add(&tl->children, child);
}