]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/usb.c
Import CUPS 1.4svn r7023 into easysw/current.
[thirdparty/cups.git] / backend / usb.c
CommitLineData
ef416fc2 1/*
2e4ff8af 2 * "$Id: usb.c 6910 2007-09-04 20:34:29Z mike $"
ef416fc2 3 *
4 * USB port backend for the Common UNIX Printing System (CUPS).
5 *
bc44d920 6 * Copyright 2007 by Apple Inc.
c0e1af83 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
ef416fc2 12 * "LICENSE" which should have been included with this file. If this
bc44d920 13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 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>
c0e1af83 39#include <cups/i18n.h>
ef416fc2 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
55void list_devices(void);
56int print_device(const char *uri, const char *hostname,
db1f069b 57 const char *resource, char *options,
ed486911 58 int print_fd, int copies, int argc, char *argv[]);
ef416fc2 59
60
61/*
62 * Include the vendor-specific USB implementation...
63 */
64
65#ifdef __APPLE__
66# include "usb-darwin.c"
2e4ff8af 67#elif defined(__linux) || defined(__sun) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
ef416fc2 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
80void
81list_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
98int /* O - Exit status */
99print_device(const char *uri, /* I - Device URI */
100 const char *hostname, /* I - Hostname/manufacturer */
101 const char *resource, /* I - Resource/modelname */
db1f069b 102 char *options, /* I - Device options/serial number */
ed486911 103 int print_fd, /* I - File descriptor to print */
e53920b9 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 */
ef416fc2 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;
ed486911 119 (void)print_fd;
ef416fc2 120 (void)copies;
e53920b9 121 (void)argc;
122 (void)argv;
ef416fc2 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
137int /* O - Exit status */
138main(int argc, /* I - Number of command-line arguments (6 or 7) */
139 char *argv[]) /* I - Command-line arguments */
140{
ed486911 141 int print_fd; /* Print file */
ef416fc2 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 {
db1f069b
MS
187 _cupsLangPrintf(stderr,
188 _("Usage: %s job-id user title copies options [file]\n"),
189 argv[0]);
ef416fc2 190 return (CUPS_BACKEND_FAILED);
191 }
192
193 /*
194 * Extract the device name and options from the URI...
195 */
196
a4d04587 197 uri = cupsBackendDeviceURI(argv);
ef416fc2 198
a4d04587 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)
ef416fc2 203 {
db1f069b
MS
204 _cupsLangPuts(stderr,
205 _("ERROR: No device URI found in argv[0] or in DEVICE_URI "
206 "environment variable!\n"));
ef416fc2 207 return (1);
208 }
209
ef416fc2 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 {
ed486911 231 print_fd = 0;
232 copies = 1;
ef416fc2 233 }
234 else
235 {
236 /*
237 * Try to open the print file...
238 */
239
ed486911 240 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
ef416fc2 241 {
db1f069b
MS
242 _cupsLangPrintf(stderr, _("ERROR: Unable to open print file %s - %s\n"),
243 argv[6], strerror(errno));
ef416fc2 244 return (CUPS_BACKEND_FAILED);
245 }
246
247 copies = atoi(argv[4]);
248 }
249
250 /*
251 * Finally, send the print file...
252 */
253
ed486911 254 status = print_device(uri, hostname, resource, options, print_fd, copies,
255 argc, argv);
ef416fc2 256
257 /*
258 * Close the input file and return...
259 */
260
ed486911 261 if (print_fd != 0)
262 close(print_fd);
ef416fc2 263
264 return (status);
265}
266
267
268/*
2e4ff8af 269 * End of "$Id: usb.c 6910 2007-09-04 20:34:29Z mike $".
ef416fc2 270 */