]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/usb.c
Add "None" choice for custom options.
[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 *
0e66904d 6 * Copyright 1997-2006 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>
4ff40357 48#include <signal.h>
a2c6b8b1 49
779bdb5e 50#ifdef WIN32
51# include <io.h>
52#else
53# include <unistd.h>
54# include <fcntl.h>
55# include <termios.h>
56#endif /* WIN32 */
57
a2c6b8b1 58
cbeb5d2c 59/*
60 * Local functions...
61 */
62
63void list_devices(void);
64int print_device(const char *uri, const char *hostname,
65 const char *resource, const char *options,
66 int fp, int copies);
67
a9219725 68
69/*
cbeb5d2c 70 * Include the vendor-specific USB implementation...
a9219725 71 */
a9219725 72
cbeb5d2c 73#ifdef __APPLE__
74# include "usb-darwin.c"
fe688a06 75#elif defined(__linux) || defined(__sun) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
cbeb5d2c 76# include "usb-unix.c"
77#else
78/*
79 * Use dummy functions that do nothing on unsupported platforms...
80 * These can be used as templates for implementing USB printing on new
81 * platforms...
82 */
83
84/*
85 * 'list_devices()' - List all available USB devices to stdout.
86 */
87
88void
89list_devices(void)
90{
91 /*
92 * Don't have any devices to list... Use output of the form:
93 *
94 * direct usb:/make/model?serial=foo "Make Model" "USB Printer"
95 *
96 * Note that "Hewlett Packard" or any other variation MUST be mapped to
97 * "HP" for compatibility with the PPD and ICC specs.
98 */
99}
e1e31a38 100
a2c6b8b1 101
102/*
cbeb5d2c 103 * 'print_device()' - Print a file to a USB device.
a2c6b8b1 104 */
105
cbeb5d2c 106int /* O - Exit status */
107print_device(const char *uri, /* I - Device URI */
108 const char *hostname, /* I - Hostname/manufacturer */
109 const char *resource, /* I - Resource/modelname */
110 const char *options, /* I - Device options/serial number */
111 int fp, /* I - File descriptor to print */
112 int copies) /* I - Copies to print */
113{
114 /*
115 * Can't print, so just reference the arguments to eliminate compiler
116 * warnings and return and exit status of 1. Normally you would use the
117 * arguments to send a file to the printer and return 0 if everything
118 * worked OK and non-zero if there was an error.
119 */
120
121 (void)uri;
122 (void)hostname;
123 (void)resource;
124 (void)options;
779bdb5e 125 (void)fp;
cbeb5d2c 126 (void)copies;
127
6248387b 128 return (CUPS_BACKEND_FAILED);
cbeb5d2c 129}
130#endif /* __APPLE__ */
a2c6b8b1 131
132
133/*
134 * 'main()' - Send a file to the specified USB port.
135 *
136 * Usage:
137 *
138 * printer-uri job-id user title copies options [file]
139 */
140
84e51f51 141int /* O - Exit status */
142main(int argc, /* I - Number of command-line arguments (6 or 7) */
143 char *argv[]) /* I - Command-line arguments */
a2c6b8b1 144{
84e51f51 145 int fp; /* Print file */
146 int copies; /* Number of copies to print */
cbeb5d2c 147 int status; /* Exit status */
148 int port; /* Port number (not used) */
149 const char *uri; /* Device URI */
150 char method[255], /* Method in URI */
151 hostname[1024], /* Hostname */
152 username[255], /* Username info (not used) */
153 resource[1024], /* Resource info (device and options) */
154 *options; /* Pointer to options */
4ff40357 155#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
84e51f51 156 struct sigaction action; /* Actions for POSIX signals */
4ff40357 157#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
a2c6b8b1 158
159
4b23f3b3 160 /*
161 * Make sure status messages are not buffered...
162 */
163
164 setbuf(stderr, NULL);
165
98904cd6 166 /*
167 * Ignore SIGPIPE signals...
168 */
169
170#ifdef HAVE_SIGSET
171 sigset(SIGPIPE, SIG_IGN);
172#elif defined(HAVE_SIGACTION)
173 memset(&action, 0, sizeof(action));
174 action.sa_handler = SIG_IGN;
175 sigaction(SIGPIPE, &action, NULL);
176#else
177 signal(SIGPIPE, SIG_IGN);
178#endif /* HAVE_SIGSET */
179
4b23f3b3 180 /*
181 * Check command-line...
182 */
183
a2c6b8b1 184 if (argc == 1)
185 {
186 list_devices();
6248387b 187 return (CUPS_BACKEND_OK);
a2c6b8b1 188 }
189 else if (argc < 6 || argc > 7)
190 {
cbeb5d2c 191 fputs("Usage: usb job-id user title copies options [file]\n", stderr);
6248387b 192 return (CUPS_BACKEND_FAILED);
a2c6b8b1 193 }
194
195 /*
cbeb5d2c 196 * Extract the device name and options from the URI...
a2c6b8b1 197 */
198
00a1fad8 199 uri = cupsBackendDeviceURI(argv);
a2c6b8b1 200
00a1fad8 201 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri,
202 method, sizeof(method), username, sizeof(username),
203 hostname, sizeof(hostname), &port,
204 resource, sizeof(resource)) < HTTP_URI_OK)
a2c6b8b1 205 {
cbeb5d2c 206 fputs("ERROR: No device URI found in argv[0] or in DEVICE_URI environment variable!\n", stderr);
207 return (1);
a2c6b8b1 208 }
209
095ce1ee 210 /*
cbeb5d2c 211 * See if there are any options...
095ce1ee 212 */
213
cbeb5d2c 214 if ((options = strchr(resource, '?')) != NULL)
964eb42f 215 {
216 /*
cbeb5d2c 217 * Yup, terminate the device name string and move to the first
218 * character of the options...
964eb42f 219 */
220
cbeb5d2c 221 *options++ = '\0';
964eb42f 222 }
a2c6b8b1 223
a9219725 224 /*
cbeb5d2c 225 * If we have 7 arguments, print the file named on the command-line.
226 * Otherwise, send stdin instead...
a9219725 227 */
a2c6b8b1 228
cbeb5d2c 229 if (argc == 6)
a9219725 230 {
cbeb5d2c 231 fp = 0;
232 copies = 1;
a9219725 233 }
273e40d6 234 else
a9219725 235 {
753453e4 236 /*
cbeb5d2c 237 * Try to open the print file...
753453e4 238 */
239
cbeb5d2c 240 if ((fp = open(argv[6], O_RDONLY)) < 0)
a2c6b8b1 241 {
cbeb5d2c 242 fprintf(stderr, "ERROR: unable to open print file %s - %s\n",
243 argv[6], strerror(errno));
6248387b 244 return (CUPS_BACKEND_FAILED);
a9219725 245 }
753453e4 246
cbeb5d2c 247 copies = atoi(argv[4]);
a2c6b8b1 248 }
93704f20 249
e1e31a38 250 /*
cbeb5d2c 251 * Finally, send the print file...
e1e31a38 252 */
253
cbeb5d2c 254 status = print_device(uri, hostname, resource, options, fp, copies);
a9219725 255
a9219725 256 /*
cbeb5d2c 257 * Close the input file and return...
a9219725 258 */
259
cbeb5d2c 260 if (fp != 0)
261 close(fp);
a9219725 262
cbeb5d2c 263 return (status);
a9219725 264}
265
266
267/*
c9d3f842 268 * End of "$Id$".
a2c6b8b1 269 */