-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_common.c
124 lines (111 loc) · 3.44 KB
/
test_common.c
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
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <[email protected]>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library 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
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <[email protected]>.
**
** test_common.c: Pth common test program stuff
*/
/* ``It doesn't need to be tested,
because it works.''
-- Richard Holloway */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pth.h"
#include "test_common.h"
/*
* implementation of a convinient greedy tread-safe line reading function to
* avoid slow byte-wise reading from filedescriptors - which is important for
* high-performance situations.
*/
#define READLINE_MAXLEN 1024
static pth_key_t readline_key;
static pth_once_t readline_once_ctrl = PTH_ONCE_INIT;
typedef struct {
int rl_cnt;
char *rl_bufptr;
char rl_buf[READLINE_MAXLEN];
} readline_buf;
static void readline_buf_destroy(void *vp)
{
free(vp);
return;
}
static void readline_init(void *vp)
{
pth_key_create(&readline_key, readline_buf_destroy);
return;
}
ssize_t pth_readline(int fd, void *buf, size_t buflen)
{
return pth_readline_ev(fd, buf, buflen, NULL);
}
ssize_t pth_readline_ev(int fd, void *buf, size_t buflen, pth_event_t ev_extra)
{
size_t n;
ssize_t rc;
char c = '\0', *cp;
readline_buf *rl;
pth_once(&readline_once_ctrl, readline_init, NULL);
if ((rl = (readline_buf *)pth_key_getdata(readline_key)) == NULL) {
rl = (readline_buf *)malloc(sizeof(readline_buf));
rl->rl_cnt = 0;
rl->rl_bufptr = NULL;
pth_key_setdata(readline_key, rl);
}
cp = (char *)buf;
for (n = 1; n < buflen; n++) {
/* fetch one character (but read more) */
rc = 1;
if (rl->rl_cnt <= 0) {
if ((rl->rl_cnt = pth_read_ev(fd, rl->rl_buf, READLINE_MAXLEN, ev_extra)) < 0)
rc = -1;
else if (rl->rl_cnt == 0)
rc = 0;
else
rl->rl_bufptr = rl->rl_buf;
}
if (rc == 1) {
rl->rl_cnt--;
c = *rl->rl_bufptr++;
}
/* act on fetched character */
if (rc == 1) {
if (c == '\r') {
n--;
continue;
}
*cp++ = c;
if (c == '\n')
break;
}
else if (rc == 0) {
if (n == 1)
return 0;
else
break;
}
else
return -1;
}
*cp = NUL;
return n;
}