]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/usb.c
Add SSL + LDAP support (STR #1967)
[thirdparty/cups.git] / backend / usb.c
CommitLineData
a2c6b8b1 1/*
c9d3f842 2 * "$Id$"
a2c6b8b1 3 *
4 * USB port backend for the Common UNIX Printing System (CUPS).
5 *
ff0295f0 6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
a2c6b8b1 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
c9d3f842 18 * Hollywood, Maryland 20636 USA
a2c6b8b1 19 *
cbeb5d2c 20 * Voice: (301) 373-9600
a2c6b8b1 21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
dab1a4d8 24 * This file is subject to the Apple OS-Developed Software exception.
25 *
a2c6b8b1 26 * Contents:
27 *
cbeb5d2c 28 * list_devices() - List all available USB devices to stdout.
29 * print_device() - Print a file to a USB device.
a2c6b8b1 30 * main() - Send a file to the specified USB port.
a2c6b8b1 31 */
32
33/*
34 * Include necessary headers.
35 */
36
cbeb5d2c 37#ifdef __APPLE__
38 /* A header order dependency requires this be first */
39# include <ApplicationServices/ApplicationServices.h>
40#endif /* __APPLE__ */
41
6248387b 42#include <cups/backend.h>
a2c6b8b1 43#include <cups/cups.h>
44#include <stdio.h>
45#include <stdlib.h>
a3e17a89 46#include <errno.h>
a2c6b8b1 47#include <cups/string.h>
3611e7ed 48#include <cups/i18n.h>
4ff40357 49#include <signal.h>
a2c6b8b1 50
779bdb5e 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
a2c6b8b1 59
cbeb5d2c 60/*
61 * Local functions...
62 */
63
64void list_devices(void);
65int print_device(const char *uri, const char *hostname,
66 const char *resource, const char *options,
43b5ad0d 67 int print_fd, int copies, int argc, char *argv[]);
cbeb5d2c 68
a9219725 69
70/*
cbeb5d2c 71 * Include the vendor-specific USB implementation...
a9219725 72 */
a9219725 73
cbeb5d2c 74#ifdef __APPLE__
75# include "usb-darwin.c"
fe688a06 76#elif defined(__linux) || defined(__sun) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
cbeb5d2c 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
89void
90list_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}
e1e31a38 101
a2c6b8b1 102
103/*
cbeb5d2c 104 * 'print_device()' - Print a file to a USB device.
a2c6b8b1 105 */
106
cbeb5d2c 107int /* O - Exit status */
108print_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 */
43b5ad0d 112 int print_fd, /* I - File descriptor to print */
7e826c4a 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 */
cbeb5d2c 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;
43b5ad0d 128 (void)print_fd;
cbeb5d2c 129 (void)copies;
7e826c4a 130 (void)argc;
131 (void)argv;
cbeb5d2c 132
6248387b 133 return (CUPS_BACKEND_FAILED);
cbeb5d2c 134}
135#endif /* __APPLE__ */
a2c6b8b1 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
84e51f51 146int /* O - Exit status */
147main(int argc, /* I - Number of command-line arguments (6 or 7) */
148 char *argv[]) /* I - Command-line arguments */
a2c6b8b1 149{
43b5ad0d 150 int print_fd; /* Print file */
84e51f51 151 int copies; /* Number of copies to print */
cbeb5d2c 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 */
4ff40357 160#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
84e51f51 161 struct sigaction action; /* Actions for POSIX signals */
4ff40357 162#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
a2c6b8b1 163
164
4b23f3b3 165 /*
166 * Make sure status messages are not buffered...
167 */
168
169 setbuf(stderr, NULL);
170
98904cd6 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
4b23f3b3 185 /*
186 * Check command-line...
187 */
188
a2c6b8b1 189 if (argc == 1)
190 {
191 list_devices();
6248387b 192 return (CUPS_BACKEND_OK);
a2c6b8b1 193 }
194 else if (argc < 6 || argc > 7)
195 {
ff0295f0 196 fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
197 argv[0]);
6248387b 198 return (CUPS_BACKEND_FAILED);
a2c6b8b1 199 }
200
201 /*
cbeb5d2c 202 * Extract the device name and options from the URI...
a2c6b8b1 203 */
204
00a1fad8 205 uri = cupsBackendDeviceURI(argv);
a2c6b8b1 206
00a1fad8 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)
a2c6b8b1 211 {
ff0295f0 212 fputs(_("ERROR: No device URI found in argv[0] or in DEVICE_URI "
213 "environment variable!\n"), stderr);
cbeb5d2c 214 return (1);
a2c6b8b1 215 }
216
095ce1ee 217 /*
cbeb5d2c 218 * See if there are any options...
095ce1ee 219 */
220
cbeb5d2c 221 if ((options = strchr(resource, '?')) != NULL)
964eb42f 222 {
223 /*
cbeb5d2c 224 * Yup, terminate the device name string and move to the first
225 * character of the options...
964eb42f 226 */
227
cbeb5d2c 228 *options++ = '\0';
964eb42f 229 }
a2c6b8b1 230
a9219725 231 /*
cbeb5d2c 232 * If we have 7 arguments, print the file named on the command-line.
233 * Otherwise, send stdin instead...
a9219725 234 */
a2c6b8b1 235
cbeb5d2c 236 if (argc == 6)
a9219725 237 {
43b5ad0d 238 print_fd = 0;
239 copies = 1;
a9219725 240 }
273e40d6 241 else
a9219725 242 {
753453e4 243 /*
cbeb5d2c 244 * Try to open the print file...
753453e4 245 */
246
43b5ad0d 247 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
a2c6b8b1 248 {
ff0295f0 249 fprintf(stderr, _("ERROR: Unable to open print file %s - %s\n"),
250 argv[6], strerror(errno));
6248387b 251 return (CUPS_BACKEND_FAILED);
a9219725 252 }
753453e4 253
cbeb5d2c 254 copies = atoi(argv[4]);
a2c6b8b1 255 }
93704f20 256
e1e31a38 257 /*
cbeb5d2c 258 * Finally, send the print file...
e1e31a38 259 */
260
43b5ad0d 261 status = print_device(uri, hostname, resource, options, print_fd, copies,
262 argc, argv);
a9219725 263
a9219725 264 /*
cbeb5d2c 265 * Close the input file and return...
a9219725 266 */
267
43b5ad0d 268 if (print_fd != 0)
269 close(print_fd);
a9219725 270
cbeb5d2c 271 return (status);
a9219725 272}
273
274
275/*
c9d3f842 276 * End of "$Id$".
a2c6b8b1 277 */