]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/backchannel.c
Greatly simplify the man page handling.
[thirdparty/cups.git] / cups / backchannel.c
CommitLineData
ef416fc2 1/*
7e86f2f6 2 * Backchannel functions for CUPS.
ef416fc2 3 *
7e86f2f6
MS
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 6 *
e3101897 7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
ef416fc2 8 */
9
10/*
11 * Include necessary headers...
12 */
13
14#include "cups.h"
ec7e5bdd 15#include "sidechannel.h"
ef416fc2 16#include <errno.h>
19dc16f7 17#ifdef _WIN32
ef416fc2 18# include <io.h>
19# include <fcntl.h>
20#else
21# include <sys/time.h>
19dc16f7 22#endif /* _WIN32 */
ef416fc2 23
24
25/*
26 * Local functions...
27 */
28
29static void cups_setup(fd_set *set, struct timeval *tval,
30 double timeout);
31
32
33/*
34 * 'cupsBackChannelRead()' - Read data from the backchannel.
35 *
79e1d494
MS
36 * Reads up to "bytes" bytes from the backchannel/backend. The "timeout"
37 * parameter controls how many seconds to wait for the data - use 0.0 to
38 * return immediately if there is no data, -1.0 to wait for data indefinitely.
ef416fc2 39 *
8072030b 40 * @since CUPS 1.2/macOS 10.5@
ef416fc2 41 */
42
ecdc0628 43ssize_t /* O - Bytes read or -1 on error */
79e1d494 44cupsBackChannelRead(char *buffer, /* I - Buffer to read into */
ecdc0628 45 size_t bytes, /* I - Bytes to read */
79e1d494 46 double timeout) /* I - Timeout in seconds, typically 0.0 to poll */
ef416fc2 47{
48 fd_set input; /* Input set */
49 struct timeval tval; /* Timeout value */
50 int status; /* Select status */
51
52
53 /*
54 * Wait for input ready.
55 */
56
57 do
58 {
59 cups_setup(&input, &tval, timeout);
60
61 if (timeout < 0.0)
62 status = select(4, &input, NULL, NULL, NULL);
63 else
64 status = select(4, &input, NULL, NULL, &tval);
65 }
e07d4801 66 while (status < 0 && errno != EINTR && errno != EAGAIN);
ef416fc2 67
68 if (status < 0)
69 return (-1); /* Timeout! */
70
71 /*
72 * Read bytes from the pipe...
73 */
74
19dc16f7 75#ifdef _WIN32
536bc2c6 76 return ((ssize_t)_read(3, buffer, (unsigned)bytes));
b86bc4cf 77#else
ef416fc2 78 return (read(3, buffer, bytes));
19dc16f7 79#endif /* _WIN32 */
ef416fc2 80}
81
82
83/*
84 * 'cupsBackChannelWrite()' - Write data to the backchannel.
85 *
79e1d494 86 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
ef416fc2 87 * controls how many seconds to wait for the data to be written - use
88 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
89 * indefinitely.
90 *
8072030b 91 * @since CUPS 1.2/macOS 10.5@
ef416fc2 92 */
93
ecdc0628 94ssize_t /* O - Bytes written or -1 on error */
bd7854cb 95cupsBackChannelWrite(
ef416fc2 96 const char *buffer, /* I - Buffer to write */
ecdc0628 97 size_t bytes, /* I - Bytes to write */
79e1d494 98 double timeout) /* I - Timeout in seconds, typically 1.0 */
ef416fc2 99{
100 fd_set output; /* Output set */
101 struct timeval tval; /* Timeout value */
102 int status; /* Select status */
ecdc0628 103 ssize_t count; /* Current bytes */
104 size_t total; /* Total bytes */
ef416fc2 105
106
107 /*
108 * Write all bytes...
109 */
110
111 total = 0;
112
113 while (total < bytes)
114 {
115 /*
116 * Wait for write-ready...
117 */
118
119 do
120 {
121 cups_setup(&output, &tval, timeout);
122
123 if (timeout < 0.0)
124 status = select(4, NULL, &output, NULL, NULL);
125 else
126 status = select(4, NULL, &output, NULL, &tval);
127 }
e07d4801 128 while (status < 0 && errno != EINTR && errno != EAGAIN);
ef416fc2 129
e07d4801 130 if (status <= 0)
ef416fc2 131 return (-1); /* Timeout! */
132
133 /*
134 * Write bytes to the pipe...
135 */
136
19dc16f7 137#ifdef _WIN32
536bc2c6 138 count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total));
b86bc4cf 139#else
ef416fc2 140 count = write(3, buffer, bytes - total);
19dc16f7 141#endif /* _WIN32 */
ef416fc2 142
143 if (count < 0)
144 {
145 /*
146 * Write error - abort on fatal errors...
147 */
148
e07d4801 149 if (errno != EINTR && errno != EAGAIN)
ef416fc2 150 return (-1);
151 }
152 else
153 {
154 /*
155 * Write succeeded, update buffer pointer and total count...
156 */
157
158 buffer += count;
7e86f2f6 159 total += (size_t)count;
ef416fc2 160 }
161 }
162
b86bc4cf 163 return ((ssize_t)bytes);
ef416fc2 164}
165
166
167/*
f3c17241 168 * 'cups_setup()' - Setup select()
ef416fc2 169 */
170
171static void
172cups_setup(fd_set *set, /* I - Set for select() */
173 struct timeval *tval, /* I - Timer value */
174 double timeout) /* I - Timeout in seconds */
175{
176 tval->tv_sec = (int)timeout;
177 tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec));
178
179 FD_ZERO(set);
180 FD_SET(3, set);
181}