]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
71e16022 | 4 | * Backchannel functions for CUPS. |
ef416fc2 | 5 | * |
f3c17241 | 6 | * Copyright 2007-2012 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * cupsBackChannelRead() - Read data from the backchannel. | |
20 | * cupsBackChannelWrite() - Write data to the backchannel. | |
f3c17241 | 21 | * cups_setup() - Setup select() |
ef416fc2 | 22 | */ |
23 | ||
24 | /* | |
25 | * Include necessary headers... | |
26 | */ | |
27 | ||
28 | #include "cups.h" | |
29 | #include <errno.h> | |
30 | #ifdef WIN32 | |
31 | # include <io.h> | |
32 | # include <fcntl.h> | |
33 | #else | |
34 | # include <sys/time.h> | |
35 | #endif /* WIN32 */ | |
36 | ||
37 | ||
38 | /* | |
39 | * Local functions... | |
40 | */ | |
41 | ||
42 | static void cups_setup(fd_set *set, struct timeval *tval, | |
43 | double timeout); | |
44 | ||
45 | ||
46 | /* | |
47 | * 'cupsBackChannelRead()' - Read data from the backchannel. | |
48 | * | |
79e1d494 MS |
49 | * Reads up to "bytes" bytes from the backchannel/backend. The "timeout" |
50 | * parameter controls how many seconds to wait for the data - use 0.0 to | |
51 | * return immediately if there is no data, -1.0 to wait for data indefinitely. | |
ef416fc2 | 52 | * |
f3c17241 | 53 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 54 | */ |
55 | ||
ecdc0628 | 56 | ssize_t /* O - Bytes read or -1 on error */ |
79e1d494 | 57 | cupsBackChannelRead(char *buffer, /* I - Buffer to read into */ |
ecdc0628 | 58 | size_t bytes, /* I - Bytes to read */ |
79e1d494 | 59 | double timeout) /* I - Timeout in seconds, typically 0.0 to poll */ |
ef416fc2 | 60 | { |
61 | fd_set input; /* Input set */ | |
62 | struct timeval tval; /* Timeout value */ | |
63 | int status; /* Select status */ | |
64 | ||
65 | ||
66 | /* | |
67 | * Wait for input ready. | |
68 | */ | |
69 | ||
70 | do | |
71 | { | |
72 | cups_setup(&input, &tval, timeout); | |
73 | ||
74 | if (timeout < 0.0) | |
75 | status = select(4, &input, NULL, NULL, NULL); | |
76 | else | |
77 | status = select(4, &input, NULL, NULL, &tval); | |
78 | } | |
e07d4801 | 79 | while (status < 0 && errno != EINTR && errno != EAGAIN); |
ef416fc2 | 80 | |
81 | if (status < 0) | |
82 | return (-1); /* Timeout! */ | |
83 | ||
84 | /* | |
85 | * Read bytes from the pipe... | |
86 | */ | |
87 | ||
b86bc4cf | 88 | #ifdef WIN32 |
536bc2c6 | 89 | return ((ssize_t)_read(3, buffer, (unsigned)bytes)); |
b86bc4cf | 90 | #else |
ef416fc2 | 91 | return (read(3, buffer, bytes)); |
b86bc4cf | 92 | #endif /* WIN32 */ |
ef416fc2 | 93 | } |
94 | ||
95 | ||
96 | /* | |
97 | * 'cupsBackChannelWrite()' - Write data to the backchannel. | |
98 | * | |
79e1d494 | 99 | * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter |
ef416fc2 | 100 | * controls how many seconds to wait for the data to be written - use |
101 | * 0.0 to return immediately if the data cannot be written, -1.0 to wait | |
102 | * indefinitely. | |
103 | * | |
f3c17241 | 104 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 105 | */ |
106 | ||
ecdc0628 | 107 | ssize_t /* O - Bytes written or -1 on error */ |
bd7854cb | 108 | cupsBackChannelWrite( |
ef416fc2 | 109 | const char *buffer, /* I - Buffer to write */ |
ecdc0628 | 110 | size_t bytes, /* I - Bytes to write */ |
79e1d494 | 111 | double timeout) /* I - Timeout in seconds, typically 1.0 */ |
ef416fc2 | 112 | { |
113 | fd_set output; /* Output set */ | |
114 | struct timeval tval; /* Timeout value */ | |
115 | int status; /* Select status */ | |
ecdc0628 | 116 | ssize_t count; /* Current bytes */ |
117 | size_t total; /* Total bytes */ | |
ef416fc2 | 118 | |
119 | ||
120 | /* | |
121 | * Write all bytes... | |
122 | */ | |
123 | ||
124 | total = 0; | |
125 | ||
126 | while (total < bytes) | |
127 | { | |
128 | /* | |
129 | * Wait for write-ready... | |
130 | */ | |
131 | ||
132 | do | |
133 | { | |
134 | cups_setup(&output, &tval, timeout); | |
135 | ||
136 | if (timeout < 0.0) | |
137 | status = select(4, NULL, &output, NULL, NULL); | |
138 | else | |
139 | status = select(4, NULL, &output, NULL, &tval); | |
140 | } | |
e07d4801 | 141 | while (status < 0 && errno != EINTR && errno != EAGAIN); |
ef416fc2 | 142 | |
e07d4801 | 143 | if (status <= 0) |
ef416fc2 | 144 | return (-1); /* Timeout! */ |
145 | ||
146 | /* | |
147 | * Write bytes to the pipe... | |
148 | */ | |
149 | ||
b86bc4cf | 150 | #ifdef WIN32 |
536bc2c6 | 151 | count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total)); |
b86bc4cf | 152 | #else |
ef416fc2 | 153 | count = write(3, buffer, bytes - total); |
b86bc4cf | 154 | #endif /* WIN32 */ |
ef416fc2 | 155 | |
156 | if (count < 0) | |
157 | { | |
158 | /* | |
159 | * Write error - abort on fatal errors... | |
160 | */ | |
161 | ||
e07d4801 | 162 | if (errno != EINTR && errno != EAGAIN) |
ef416fc2 | 163 | return (-1); |
164 | } | |
165 | else | |
166 | { | |
167 | /* | |
168 | * Write succeeded, update buffer pointer and total count... | |
169 | */ | |
170 | ||
171 | buffer += count; | |
172 | total += count; | |
173 | } | |
174 | } | |
175 | ||
b86bc4cf | 176 | return ((ssize_t)bytes); |
ef416fc2 | 177 | } |
178 | ||
179 | ||
180 | /* | |
f3c17241 | 181 | * 'cups_setup()' - Setup select() |
ef416fc2 | 182 | */ |
183 | ||
184 | static void | |
185 | cups_setup(fd_set *set, /* I - Set for select() */ | |
186 | struct timeval *tval, /* I - Timer value */ | |
187 | double timeout) /* I - Timeout in seconds */ | |
188 | { | |
189 | tval->tv_sec = (int)timeout; | |
190 | tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec)); | |
191 | ||
192 | FD_ZERO(set); | |
193 | FD_SET(3, set); | |
194 | } | |
195 | ||
196 | ||
197 | /* | |
f2d18633 | 198 | * End of "$Id$". |
ef416fc2 | 199 | */ |