~emersion/mrsh

ref: fb1a6e2ce995f61ca8e41e8ac3f591bc50922592 mrsh/shell/task_binop.c -rw-r--r-- 1.1 KiB
fb1a6e2cemersion readme: add link to overall project status 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
53
54
#include <stdlib.h>
#include "shell.h"

struct task_binop {
	struct task task;
	enum mrsh_binop_type type;
	struct task *left, *right;
};

static void task_binop_destroy(struct task *task) {
	struct task_binop *tb = (struct task_binop *)task;
	task_destroy(tb->left);
	task_destroy(tb->right);
	free(tb);
}

static int task_binop_poll(struct task *task, struct context *ctx) {
	struct task_binop *tb = (struct task_binop *)task;

	int left_status = task_poll(tb->left, ctx);
	if (left_status < 0) {
		return left_status;
	}

	switch (tb->type) {
	case MRSH_BINOP_AND:
		if (left_status != 0) {
			return left_status;
		}
		break;
	case MRSH_BINOP_OR:
		if (left_status == 0) {
			return 0;
		}
		break;
	}

	return task_poll(tb->right, ctx);
}

static const struct task_interface task_binop_impl = {
	.destroy = task_binop_destroy,
	.poll = task_binop_poll,
};

struct task *task_binop_create(enum mrsh_binop_type type,
		struct task *left, struct task *right) {
	struct task_binop *tb = calloc(1, sizeof(struct task_binop));
	task_init(&tb->task, &task_binop_impl);
	tb->type = type;
	tb->left = left;
	tb->right = right;
	return &tb->task;
}