]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/usb.c
Add Darwin USB backend (STR #661)
[thirdparty/cups.git] / backend / usb.c
1 /*
2 * "$Id: usb.c,v 1.54 2004/03/30 19:19:30 mike Exp $"
3 *
4 * USB port backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2004 by Easy Software Products, all rights reserved.
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
18 * Hollywood, Maryland 20636-3142 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * list_devices() - List all available USB devices to stdout.
29 * print_device() - Print a file to a USB device.
30 * main() - Send a file to the specified USB port.
31 */
32
33 /*
34 * Include necessary headers.
35 */
36
37 #ifdef __APPLE__
38 /* A header order dependency requires this be first */
39 # include <ApplicationServices/ApplicationServices.h>
40 #endif /* __APPLE__ */
41
42 #include <cups/cups.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <cups/string.h>
47 #include <signal.h>
48
49
50 /*
51 * Local functions...
52 */
53
54 void list_devices(void);
55 int print_device(const char *uri, const char *hostname,
56 const char *resource, const char *options,
57 int fp, int copies);
58
59
60 /*
61 * Include the vendor-specific USB implementation...
62 */
63
64 #ifdef __APPLE__
65 # include "usb-darwin.c"
66 #elif defined(__linux) || defined(__sun) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
67 # include "usb-unix.c"
68 #else
69 /*
70 * Use dummy functions that do nothing on unsupported platforms...
71 * These can be used as templates for implementing USB printing on new
72 * platforms...
73 */
74
75 /*
76 * 'list_devices()' - List all available USB devices to stdout.
77 */
78
79 void
80 list_devices(void)
81 {
82 /*
83 * Don't have any devices to list... Use output of the form:
84 *
85 * direct usb:/make/model?serial=foo "Make Model" "USB Printer"
86 *
87 * Note that "Hewlett Packard" or any other variation MUST be mapped to
88 * "HP" for compatibility with the PPD and ICC specs.
89 */
90 }
91
92
93 /*
94 * 'print_device()' - Print a file to a USB device.
95 */
96
97 int /* O - Exit status */
98 print_device(const char *uri, /* I - Device URI */
99 const char *hostname, /* I - Hostname/manufacturer */
100 const char *resource, /* I - Resource/modelname */
101 const char *options, /* I - Device options/serial number */
102 int fp, /* I - File descriptor to print */
103 int copies) /* I - Copies to print */
104 {
105 /*
106 * Can't print, so just reference the arguments to eliminate compiler
107 * warnings and return and exit status of 1. Normally you would use the
108 * arguments to send a file to the printer and return 0 if everything
109 * worked OK and non-zero if there was an error.
110 */
111
112 (void)uri;
113 (void)hostname;
114 (void)resource;
115 (void)options;
116 (void)fd;
117 (void)copies;
118
119 return (1);
120 }
121 #endif /* __APPLE__ */
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
132 int /* O - Exit status */
133 main(int argc, /* I - Number of command-line arguments (6 or 7) */
134 char *argv[]) /* I - Command-line arguments */
135 {
136 int fp; /* Print file */
137 int copies; /* Number of copies to print */
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 */
146 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
147 struct sigaction action; /* Actions for POSIX signals */
148 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
149
150
151 /*
152 * Make sure status messages are not buffered...
153 */
154
155 setbuf(stderr, NULL);
156
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
171 /*
172 * Check command-line...
173 */
174
175 if (argc == 1)
176 {
177 list_devices();
178 return (0);
179 }
180 else if (argc < 6 || argc > 7)
181 {
182 fputs("Usage: usb job-id user title copies options [file]\n", stderr);
183 return (1);
184 }
185
186 /*
187 * Extract the device name and options from the URI...
188 */
189
190 if (strncmp(argv[0], "usb:", 4))
191 uri = getenv("DEVICE_URI");
192 else
193 uri = argv[0];
194
195 if (!uri)
196 {
197 fputs("ERROR: No device URI found in argv[0] or in DEVICE_URI environment variable!\n", stderr);
198 return (1);
199 }
200
201 httpSeparate(argv[0], method, username, hostname, &port, resource);
202
203 /*
204 * See if there are any options...
205 */
206
207 if ((options = strchr(resource, '?')) != NULL)
208 {
209 /*
210 * Yup, terminate the device name string and move to the first
211 * character of the options...
212 */
213
214 *options++ = '\0';
215 }
216
217 /*
218 * If we have 7 arguments, print the file named on the command-line.
219 * Otherwise, send stdin instead...
220 */
221
222 if (argc == 6)
223 {
224 fp = 0;
225 copies = 1;
226 }
227 else
228 {
229 /*
230 * Try to open the print file...
231 */
232
233 if ((fp = open(argv[6], O_RDONLY)) < 0)
234 {
235 fprintf(stderr, "ERROR: unable to open print file %s - %s\n",
236 argv[6], strerror(errno));
237 return (1);
238 }
239
240 copies = atoi(argv[4]);
241 }
242
243 /*
244 * Finally, send the print file...
245 */
246
247 status = print_device(uri, hostname, resource, options, fp, copies);
248
249 /*
250 * Close the input file and return...
251 */
252
253 if (fp != 0)
254 close(fp);
255
256 if (!status)
257 fputs("INFO: Ready to print.\n", stderr);
258
259 return (status);
260 }
261
262
263 /*
264 * End of "$Id: usb.c,v 1.54 2004/03/30 19:19:30 mike Exp $".
265 */