]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / usb.c
1 /*
2 * "$Id: usb.c 6414 2007-03-30 13:40:57Z mike $"
3 *
4 * USB port backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * list_devices() - List all available USB devices to stdout.
29 * print_device() - Print a file to a USB device.
30 * main() - Send a file to the specified USB port.
31 */
32
33 /*
34 * Include necessary headers.
35 */
36
37 #ifdef __APPLE__
38 /* A header order dependency requires this be first */
39 # include <ApplicationServices/ApplicationServices.h>
40 #endif /* __APPLE__ */
41
42 #include <cups/backend.h>
43 #include <cups/cups.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <cups/string.h>
48 #include <cups/i18n.h>
49 #include <signal.h>
50
51 #ifdef WIN32
52 # include <io.h>
53 #else
54 # include <unistd.h>
55 # include <fcntl.h>
56 # include <termios.h>
57 #endif /* WIN32 */
58
59
60 /*
61 * Local functions...
62 */
63
64 void list_devices(void);
65 int print_device(const char *uri, const char *hostname,
66 const char *resource, const char *options,
67 int print_fd, int copies, int argc, char *argv[]);
68
69
70 /*
71 * Include the vendor-specific USB implementation...
72 */
73
74 #ifdef __APPLE__
75 # include "usb-darwin.c"
76 #elif defined(__linux) || defined(__sun) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
77 # include "usb-unix.c"
78 #else
79 /*
80 * Use dummy functions that do nothing on unsupported platforms...
81 * These can be used as templates for implementing USB printing on new
82 * platforms...
83 */
84
85 /*
86 * 'list_devices()' - List all available USB devices to stdout.
87 */
88
89 void
90 list_devices(void)
91 {
92 /*
93 * Don't have any devices to list... Use output of the form:
94 *
95 * direct usb:/make/model?serial=foo "Make Model" "USB Printer"
96 *
97 * Note that "Hewlett Packard" or any other variation MUST be mapped to
98 * "HP" for compatibility with the PPD and ICC specs.
99 */
100 }
101
102
103 /*
104 * 'print_device()' - Print a file to a USB device.
105 */
106
107 int /* O - Exit status */
108 print_device(const char *uri, /* I - Device URI */
109 const char *hostname, /* I - Hostname/manufacturer */
110 const char *resource, /* I - Resource/modelname */
111 const char *options, /* I - Device options/serial number */
112 int print_fd, /* I - File descriptor to print */
113 int copies, /* I - Copies to print */
114 int argc, /* I - Number of command-line arguments (6 or 7) */
115 char *argv[]) /* I - Command-line arguments */
116 {
117 /*
118 * Can't print, so just reference the arguments to eliminate compiler
119 * warnings and return and exit status of 1. Normally you would use the
120 * arguments to send a file to the printer and return 0 if everything
121 * worked OK and non-zero if there was an error.
122 */
123
124 (void)uri;
125 (void)hostname;
126 (void)resource;
127 (void)options;
128 (void)print_fd;
129 (void)copies;
130 (void)argc;
131 (void)argv;
132
133 return (CUPS_BACKEND_FAILED);
134 }
135 #endif /* __APPLE__ */
136
137
138 /*
139 * 'main()' - Send a file to the specified USB port.
140 *
141 * Usage:
142 *
143 * printer-uri job-id user title copies options [file]
144 */
145
146 int /* O - Exit status */
147 main(int argc, /* I - Number of command-line arguments (6 or 7) */
148 char *argv[]) /* I - Command-line arguments */
149 {
150 int print_fd; /* Print file */
151 int copies; /* Number of copies to print */
152 int status; /* Exit status */
153 int port; /* Port number (not used) */
154 const char *uri; /* Device URI */
155 char method[255], /* Method in URI */
156 hostname[1024], /* Hostname */
157 username[255], /* Username info (not used) */
158 resource[1024], /* Resource info (device and options) */
159 *options; /* Pointer to options */
160 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
161 struct sigaction action; /* Actions for POSIX signals */
162 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
163
164
165 /*
166 * Make sure status messages are not buffered...
167 */
168
169 setbuf(stderr, NULL);
170
171 /*
172 * Ignore SIGPIPE signals...
173 */
174
175 #ifdef HAVE_SIGSET
176 sigset(SIGPIPE, SIG_IGN);
177 #elif defined(HAVE_SIGACTION)
178 memset(&action, 0, sizeof(action));
179 action.sa_handler = SIG_IGN;
180 sigaction(SIGPIPE, &action, NULL);
181 #else
182 signal(SIGPIPE, SIG_IGN);
183 #endif /* HAVE_SIGSET */
184
185 /*
186 * Check command-line...
187 */
188
189 if (argc == 1)
190 {
191 list_devices();
192 return (CUPS_BACKEND_OK);
193 }
194 else if (argc < 6 || argc > 7)
195 {
196 fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
197 argv[0]);
198 return (CUPS_BACKEND_FAILED);
199 }
200
201 /*
202 * Extract the device name and options from the URI...
203 */
204
205 uri = cupsBackendDeviceURI(argv);
206
207 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri,
208 method, sizeof(method), username, sizeof(username),
209 hostname, sizeof(hostname), &port,
210 resource, sizeof(resource)) < HTTP_URI_OK)
211 {
212 fputs(_("ERROR: No device URI found in argv[0] or in DEVICE_URI "
213 "environment variable!\n"), stderr);
214 return (1);
215 }
216
217 /*
218 * See if there are any options...
219 */
220
221 if ((options = strchr(resource, '?')) != NULL)
222 {
223 /*
224 * Yup, terminate the device name string and move to the first
225 * character of the options...
226 */
227
228 *options++ = '\0';
229 }
230
231 /*
232 * If we have 7 arguments, print the file named on the command-line.
233 * Otherwise, send stdin instead...
234 */
235
236 if (argc == 6)
237 {
238 print_fd = 0;
239 copies = 1;
240 }
241 else
242 {
243 /*
244 * Try to open the print file...
245 */
246
247 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
248 {
249 fprintf(stderr, _("ERROR: Unable to open print file %s - %s\n"),
250 argv[6], strerror(errno));
251 return (CUPS_BACKEND_FAILED);
252 }
253
254 copies = atoi(argv[4]);
255 }
256
257 /*
258 * Finally, send the print file...
259 */
260
261 status = print_device(uri, hostname, resource, options, print_fd, copies,
262 argc, argv);
263
264 /*
265 * Close the input file and return...
266 */
267
268 if (print_fd != 0)
269 close(print_fd);
270
271 return (status);
272 }
273
274
275 /*
276 * End of "$Id: usb.c 6414 2007-03-30 13:40:57Z mike $".
277 */