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