]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/usb.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / usb.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: usb.c 6649 2007-07-11 21:46:42Z 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,
57 const char *resource, const 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"
b423cd4c 67#elif defined(__linux) || defined(__sun) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
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 */
102 const 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 {
c0e1af83 187 fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
188 argv[0]);
ef416fc2 189 return (CUPS_BACKEND_FAILED);
190 }
191
192 /*
193 * Extract the device name and options from the URI...
194 */
195
a4d04587 196 uri = cupsBackendDeviceURI(argv);
ef416fc2 197
a4d04587 198 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri,
199 method, sizeof(method), username, sizeof(username),
200 hostname, sizeof(hostname), &port,
201 resource, sizeof(resource)) < HTTP_URI_OK)
ef416fc2 202 {
c0e1af83 203 fputs(_("ERROR: No device URI found in argv[0] or in DEVICE_URI "
204 "environment variable!\n"), stderr);
ef416fc2 205 return (1);
206 }
207
ef416fc2 208 /*
209 * See if there are any options...
210 */
211
212 if ((options = strchr(resource, '?')) != NULL)
213 {
214 /*
215 * Yup, terminate the device name string and move to the first
216 * character of the options...
217 */
218
219 *options++ = '\0';
220 }
221
222 /*
223 * If we have 7 arguments, print the file named on the command-line.
224 * Otherwise, send stdin instead...
225 */
226
227 if (argc == 6)
228 {
ed486911 229 print_fd = 0;
230 copies = 1;
ef416fc2 231 }
232 else
233 {
234 /*
235 * Try to open the print file...
236 */
237
ed486911 238 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
ef416fc2 239 {
c0e1af83 240 fprintf(stderr, _("ERROR: Unable to open print file %s - %s\n"),
ef416fc2 241 argv[6], strerror(errno));
242 return (CUPS_BACKEND_FAILED);
243 }
244
245 copies = atoi(argv[4]);
246 }
247
248 /*
249 * Finally, send the print file...
250 */
251
ed486911 252 status = print_device(uri, hostname, resource, options, print_fd, copies,
253 argc, argv);
ef416fc2 254
255 /*
256 * Close the input file and return...
257 */
258
ed486911 259 if (print_fd != 0)
260 close(print_fd);
ef416fc2 261
262 return (status);
263}
264
265
266/*
bc44d920 267 * End of "$Id: usb.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 268 */