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