File: input_async.c

package info (click to toggle)
mspdebug 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,700 kB
  • sloc: ansic: 67,821; makefile: 201; xml: 19
file content (169 lines) | stat: -rw-r--r-- 3,931 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* MSPDebug - debugging tool for MSP430 MCUs
 * Copyright (C) 2009-2012 Daniel Beer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "input_async.h"
#include "util.h"
#include "ctrlc.h"
#include "thread.h"

#define MAX_LINE_LENGTH		1024

typedef enum {
	STATE_BLOCKED,
	STATE_RECEIVING,
	STATE_READY,
	STATE_EOF
} mailbox_state_t;

struct mailbox {
	thread_lock_t		lock_ack;
	thread_cond_t		cond_ack;
	int			ack;

	thread_lock_t		lock_text;
	thread_cond_t		cond_text;
	int			text_len;
	int			text_eof;
	char			text[MAX_LINE_LENGTH];
};

static struct mailbox linebox;

static void handle_special(const char *text)
{
	if (!strcmp(text, "break"))
		ctrlc_raise();
}

static void handle_command(const char *text)
{
	int len = strlen(text);

	if (len >= sizeof(linebox.text))
		len = sizeof(linebox.text) - 1;

	/* Deliver the command to the mailbox */
	thread_lock_acquire(&linebox.lock_text);
	memcpy(linebox.text, text, len);
	linebox.text[len] = 0;
	linebox.text_len = len;
	thread_lock_release(&linebox.lock_text);
	thread_cond_notify(&linebox.cond_text);

	/* Wait for ACK */
	thread_lock_acquire(&linebox.lock_ack);
	while (!linebox.ack)
		thread_cond_wait(&linebox.cond_ack, &linebox.lock_ack);
	linebox.ack = 0;
	thread_lock_release(&linebox.lock_ack);
}

static void io_worker(void *thread_arg)
{
	char buf[MAX_LINE_LENGTH];
	(void)thread_arg;

	/* Read commands from stdin and dispatch them */
	while (fgets(buf, sizeof(buf), stdin)) {
		int len = strlen(buf);

		while (len > 0 && isspace(buf[len - 1]))
			len--;
		buf[len] = 0;

		if (buf[0] == '\\')
			handle_special(buf + 1);
		else if (buf[0] == ':')
			handle_command(buf + 1);
		else
			handle_command(buf);
	}

	/* Deliver EOF */
	thread_lock_acquire(&linebox.lock_text);
	linebox.text_eof = 1;
	thread_lock_release(&linebox.lock_text);
	thread_cond_notify(&linebox.cond_text);
}

static int async_init(void)
{
	thread_t thr;

	thread_lock_init(&linebox.lock_text);
	thread_lock_init(&linebox.lock_ack);
	thread_cond_init(&linebox.cond_text);
	thread_cond_init(&linebox.cond_ack);
	linebox.text_len = -1;
	linebox.text_eof = 0;
	linebox.ack = 0;

	if (thread_create(&thr, io_worker, NULL) < 0) {
		fprintf(stderr, "async_init: failed to "
			"start reader thread: %s\n", last_error());
		return -1;
	}

	return 0;
}

static void async_exit(void) { }

static int async_read_command(char *buf, int max_len)
{
	/* Wait for text or EOF */
	thread_lock_acquire(&linebox.lock_text);
	while (!linebox.text_eof && (linebox.text_len < 0))
		thread_cond_wait(&linebox.cond_text, &linebox.lock_text);

	if (linebox.text_eof) {
		thread_lock_release(&linebox.lock_text);
		return 1;
	}

	strncpy(buf, linebox.text, max_len);
	buf[max_len - 1] = 0;
	linebox.text_len = -1;
	thread_lock_release(&linebox.lock_text);

	/* Send ACK */
	thread_lock_acquire(&linebox.lock_ack);
	linebox.ack = 1;
	thread_lock_release(&linebox.lock_ack);
	thread_cond_notify(&linebox.cond_ack);

	return 0;
}

static int async_prompt_abort(const char *message)
{
	(void)message;

	return 0;
}

const struct input_interface input_async = {
	.init		= async_init,
	.exit		= async_exit,
	.read_command	= async_read_command,
	.prompt_abort	= async_prompt_abort
};