-
Notifications
You must be signed in to change notification settings - Fork 127
/
fdio.c
178 lines (152 loc) · 3.65 KB
/
fdio.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
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
170
171
172
173
174
175
176
177
178
/* vi: set sw=4 ts=4:
*
* fdio.c
*
* Functions for doing I/O on file descriptors.
*
* by Nick Patavalis ([email protected])
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
/**********************************************************************/
ssize_t
writen_ni(int fd, const void *buff, size_t n)
{
size_t nl;
ssize_t nw;
const char *p;
p = buff;
nl = n;
while (nl > 0) {
do {
nw = write(fd, p, nl);
} while ( nw < 0 && errno == EINTR );
if ( nw <= 0 ) break;
nl -= nw;
p += nw;
}
return n - nl;
}
int
fd_vprintf (int fd, const char *format, va_list ap)
{
char buf[256];
int len;
len = vsnprintf(buf, sizeof(buf), format, ap);
buf[sizeof(buf) - 1] = '\0';
return writen_ni(fd, buf, len);
}
int
fd_printf (int fd, const char *format, ...)
{
va_list args;
int len;
va_start(args, format);
len = fd_vprintf(fd, format, args);
va_end(args);
return len;
}
/**********************************************************************/
#ifndef LINENOISE
static int
cput(int fd, char c)
{
return write(fd, &c, 1);
}
static int
cdel (int fd)
{
const char del[] = "\b \b";
return write(fd, del, sizeof(del) - 1);
}
static int
xput (int fd, unsigned char c)
{
const char hex[] = "0123456789abcdef";
char b[4];
b[0] = '\\'; b[1] = 'x'; b[2] = hex[c >> 4]; b[3] = hex[c & 0x0f];
return write(fd, b, sizeof(b));
}
static int
xdel (int fd)
{
const char del[] = "\b\b\b\b \b\b\b\b";
return write(fd, del, sizeof(del) - 1);
}
int
fd_readline (int fdi, int fdo, char *b, int bsz)
{
int r;
unsigned char c;
unsigned char *bp, *bpe;
bp = (unsigned char *)b;
bpe = (unsigned char *)b + bsz - 1;
while (1) {
r = read(fdi, &c, 1);
if ( r <= 0 ) { r = -1; goto out; }
switch (c) {
case '\b':
case '\x7f':
if ( bp > (unsigned char *)b ) {
bp--;
if ( isprint(*bp) )
cdel(fdo);
else
xdel(fdo);
} else {
cput(fdo, '\x07');
}
break;
case '\x03': /* CTRL-c */
r = -1;
errno = EINTR;
goto out;
case '\r':
*bp = '\0';
r = bp - (unsigned char *)b;
goto out;
default:
if ( bp < bpe ) {
*bp++ = c;
if ( isprint(c) )
cput(fdo, c);
else
xput(fdo, c);
} else {
cput(fdo, '\x07');
}
break;
}
}
out:
return r;
}
#endif /* of LINENOISE */
/**********************************************************************/
/*
* Local Variables:
* mode:c
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/