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