]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/ieee1394.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / ieee1394.c
1 /*
2 * "$Id: ieee1394.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * IEEE-1394 backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 2002 by Easy Software Products, all rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the
11 * following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the
15 * following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the
18 * above copyright notice, this list of conditions and
19 * the following disclaimer in the documentation and/or
20 * other materials provided with the distribution.
21 *
22 * 3. All advertising materials mentioning features or use
23 * of this software must display the following
24 * acknowledgement:
25 *
26 * This product includes software developed by Easy
27 * Software Products.
28 *
29 * 4. The name of Easy Software Products may not be used to
30 * endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS
34 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
35 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
38 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
44 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46 * DAMAGE.
47 *
48 * Contents:
49 *
50 * main() - Send a file to the printer.
51 * list_devices() - List all known printer devices...
52 */
53
54 /*
55 * Include necessary headers.
56 */
57
58 #include "ieee1394.h"
59
60
61 /*
62 * Local functions...
63 */
64
65 void list_devices(void);
66
67
68 /*
69 * 'main()' - Send a file to the printer.
70 *
71 * Usage:
72 *
73 * printer-uri job-id user title copies options [file]
74 */
75
76 int /* O - Exit status */
77 main(int argc, /* I - Number of command-line arguments (6 or 7) */
78 char *argv[]) /* I - Command-line arguments */
79 {
80 ieee1394_dev_t dev; /* Printer device */
81 int fp; /* Print file */
82 int copies; /* Number of copies to print */
83 int rbytes; /* Number of bytes read from device */
84 size_t nbytes, /* Number of bytes read from file */
85 tbytes; /* Total number of bytes written */
86 char buffer[8192]; /* Input/output buffer */
87 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
88 struct sigaction action; /* Actions for POSIX signals */
89 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
90
91
92 /*
93 * Make sure status messages are not buffered...
94 */
95
96 setbuf(stderr, NULL);
97
98 /*
99 * Check command-line...
100 */
101
102 if (argc == 1)
103 {
104 list_devices();
105
106 return (0);
107 }
108 else if (argc < 6 || argc > 7)
109 {
110 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
111 argv[0]);
112 return (1);
113 }
114
115 /*
116 * If we have 7 arguments, print the file named on the command-line.
117 * Otherwise, send stdin instead...
118 */
119
120 if (argc == 6)
121 {
122 fp = 0;
123 copies = 1;
124 }
125 else
126 {
127 /*
128 * Try to open the print file...
129 */
130
131 if ((fp = open(argv[6], O_RDONLY)) < 0)
132 {
133 perror("ERROR: unable to open print file");
134 return (1);
135 }
136
137 copies = atoi(argv[4]);
138 }
139
140 /*
141 * Try to open the printer device...
142 */
143
144 fputs("STATE: +connecting-to-device\n", stderr);
145
146 do
147 {
148 if ((dev = ieee1394_open(argv[0])) == NULL)
149 {
150 fputs("INFO: Firewire printer busy; will retry in 30 seconds...\n", stderr);
151 sleep(30);
152 }
153 }
154 while (dev == NULL);
155
156 fputs("STATE: -connecting-to-device\n", stderr);
157
158 /*
159 * Now that we are "connected" to the port, ignore SIGTERM so that we
160 * can finish out any page data the driver sends (e.g. to eject the
161 * current page... Only ignore SIGTERM if we are printing data from
162 * stdin (otherwise you can't cancel raw jobs...)
163 */
164
165 if (argc < 7)
166 {
167 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
168 sigset(SIGTERM, SIG_IGN);
169 #elif defined(HAVE_SIGACTION)
170 memset(&action, 0, sizeof(action));
171
172 sigemptyset(&action.sa_mask);
173 action.sa_handler = SIG_IGN;
174 sigaction(SIGTERM, &action, NULL);
175 #else
176 signal(SIGTERM, SIG_IGN);
177 #endif /* HAVE_SIGSET */
178 }
179
180 /*
181 * Finally, send the print file...
182 */
183
184 while (copies > 0)
185 {
186 copies --;
187
188 if (fp != 0)
189 {
190 fputs("PAGE: 1 1\n", stderr);
191 lseek(fp, 0, SEEK_SET);
192 }
193
194 tbytes = 0;
195 while ((nbytes = read(fp, buffer, sizeof(buffer))) > 0)
196 {
197 /*
198 * Write the print data to the printer...
199 */
200
201 tbytes += nbytes;
202
203 if (ieee1394_write(dev, buffer, nbytes) < 0)
204 {
205 perror("ERROR: Unable to send print file to printer");
206 break;
207 }
208
209 if ((rbytes = ieee1394_read(dev, buffer, sizeof(buffer))) > 0)
210 fprintf(stderr, "INFO: Read %d bytes from printer...\n", rbytes);
211
212 if (argc > 6)
213 fprintf(stderr, "INFO: Sending print file, %lu bytes...\n",
214 (unsigned long)tbytes);
215 }
216 }
217
218 /*
219 * Close the printer device and input file and return...
220 */
221
222 ieee1394_close(dev);
223
224 if (fp != 0)
225 close(fp);
226
227 fputs("INFO: Ready to print.\n", stderr);
228
229 return (0);
230 }
231
232
233 /*
234 * 'list_devices()' - List all known devices...
235 */
236
237 void
238 list_devices(void)
239 {
240 int i, /* Looping var */
241 num_info; /* Number of devices */
242 ieee1394_info_t *info; /* Devices... */
243
244
245 /*
246 * Get the available devices...
247 */
248
249 info = ieee1394_list(&num_info);
250
251 /*
252 * List them as needed...
253 */
254
255 if (num_info > 0)
256 {
257 for (i = 0; i < num_info; i ++)
258 printf("direct %s \"%s\" \"%s\"\n", info[i].uri,
259 info[i].make_model, info[i].description);
260
261 free(info);
262 }
263 }
264
265
266 /*
267 * End of "$Id: ieee1394.c 6649 2007-07-11 21:46:42Z mike $".
268 */