]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7deaa1a | 2 | * "$Id: usb.c 5590 2006-05-26 19:48:26Z mike $" |
ef416fc2 | 3 | * |
4 | * USB port backend for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 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 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/backend.h> | |
43 | #include <cups/cups.h> | |
44 | #include <stdio.h> | |
45 | #include <stdlib.h> | |
46 | #include <errno.h> | |
47 | #include <cups/string.h> | |
48 | #include <signal.h> | |
49 | ||
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 | ||
58 | ||
59 | /* | |
60 | * Local functions... | |
61 | */ | |
62 | ||
63 | void list_devices(void); | |
64 | int print_device(const char *uri, const char *hostname, | |
65 | const char *resource, const char *options, | |
ed486911 | 66 | int print_fd, int copies, int argc, char *argv[]); |
ef416fc2 | 67 | |
68 | ||
69 | /* | |
70 | * Include the vendor-specific USB implementation... | |
71 | */ | |
72 | ||
73 | #ifdef __APPLE__ | |
74 | # include "usb-darwin.c" | |
b423cd4c | 75 | #elif defined(__linux) || defined(__sun) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
ef416fc2 | 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 | ||
88 | void | |
89 | list_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 | } | |
100 | ||
101 | ||
102 | /* | |
103 | * 'print_device()' - Print a file to a USB device. | |
104 | */ | |
105 | ||
106 | int /* O - Exit status */ | |
107 | print_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 */ | |
ed486911 | 111 | int print_fd, /* I - File descriptor to print */ |
e53920b9 | 112 | int copies, /* I - Copies to print */ |
113 | int argc, /* I - Number of command-line arguments (6 or 7) */ | |
114 | char *argv[]) /* I - Command-line arguments */ | |
ef416fc2 | 115 | { |
116 | /* | |
117 | * Can't print, so just reference the arguments to eliminate compiler | |
118 | * warnings and return and exit status of 1. Normally you would use the | |
119 | * arguments to send a file to the printer and return 0 if everything | |
120 | * worked OK and non-zero if there was an error. | |
121 | */ | |
122 | ||
123 | (void)uri; | |
124 | (void)hostname; | |
125 | (void)resource; | |
126 | (void)options; | |
ed486911 | 127 | (void)print_fd; |
ef416fc2 | 128 | (void)copies; |
e53920b9 | 129 | (void)argc; |
130 | (void)argv; | |
ef416fc2 | 131 | |
132 | return (CUPS_BACKEND_FAILED); | |
133 | } | |
134 | #endif /* __APPLE__ */ | |
135 | ||
136 | ||
137 | /* | |
138 | * 'main()' - Send a file to the specified USB port. | |
139 | * | |
140 | * Usage: | |
141 | * | |
142 | * printer-uri job-id user title copies options [file] | |
143 | */ | |
144 | ||
145 | int /* O - Exit status */ | |
146 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
147 | char *argv[]) /* I - Command-line arguments */ | |
148 | { | |
ed486911 | 149 | int print_fd; /* Print file */ |
ef416fc2 | 150 | int copies; /* Number of copies to print */ |
151 | int status; /* Exit status */ | |
152 | int port; /* Port number (not used) */ | |
153 | const char *uri; /* Device URI */ | |
154 | char method[255], /* Method in URI */ | |
155 | hostname[1024], /* Hostname */ | |
156 | username[255], /* Username info (not used) */ | |
157 | resource[1024], /* Resource info (device and options) */ | |
158 | *options; /* Pointer to options */ | |
159 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
160 | struct sigaction action; /* Actions for POSIX signals */ | |
161 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
162 | ||
163 | ||
164 | /* | |
165 | * Make sure status messages are not buffered... | |
166 | */ | |
167 | ||
168 | setbuf(stderr, NULL); | |
169 | ||
170 | /* | |
171 | * Ignore SIGPIPE signals... | |
172 | */ | |
173 | ||
174 | #ifdef HAVE_SIGSET | |
175 | sigset(SIGPIPE, SIG_IGN); | |
176 | #elif defined(HAVE_SIGACTION) | |
177 | memset(&action, 0, sizeof(action)); | |
178 | action.sa_handler = SIG_IGN; | |
179 | sigaction(SIGPIPE, &action, NULL); | |
180 | #else | |
181 | signal(SIGPIPE, SIG_IGN); | |
182 | #endif /* HAVE_SIGSET */ | |
183 | ||
184 | /* | |
185 | * Check command-line... | |
186 | */ | |
187 | ||
188 | if (argc == 1) | |
189 | { | |
190 | list_devices(); | |
191 | return (CUPS_BACKEND_OK); | |
192 | } | |
193 | else if (argc < 6 || argc > 7) | |
194 | { | |
195 | fputs("Usage: usb job-id user title copies options [file]\n", stderr); | |
196 | return (CUPS_BACKEND_FAILED); | |
197 | } | |
198 | ||
199 | /* | |
200 | * Extract the device name and options from the URI... | |
201 | */ | |
202 | ||
a4d04587 | 203 | uri = cupsBackendDeviceURI(argv); |
ef416fc2 | 204 | |
a4d04587 | 205 | if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, |
206 | method, sizeof(method), username, sizeof(username), | |
207 | hostname, sizeof(hostname), &port, | |
208 | resource, sizeof(resource)) < HTTP_URI_OK) | |
ef416fc2 | 209 | { |
210 | fputs("ERROR: No device URI found in argv[0] or in DEVICE_URI environment variable!\n", stderr); | |
211 | return (1); | |
212 | } | |
213 | ||
ef416fc2 | 214 | /* |
215 | * See if there are any options... | |
216 | */ | |
217 | ||
218 | if ((options = strchr(resource, '?')) != NULL) | |
219 | { | |
220 | /* | |
221 | * Yup, terminate the device name string and move to the first | |
222 | * character of the options... | |
223 | */ | |
224 | ||
225 | *options++ = '\0'; | |
226 | } | |
227 | ||
228 | /* | |
229 | * If we have 7 arguments, print the file named on the command-line. | |
230 | * Otherwise, send stdin instead... | |
231 | */ | |
232 | ||
233 | if (argc == 6) | |
234 | { | |
ed486911 | 235 | print_fd = 0; |
236 | copies = 1; | |
ef416fc2 | 237 | } |
238 | else | |
239 | { | |
240 | /* | |
241 | * Try to open the print file... | |
242 | */ | |
243 | ||
ed486911 | 244 | if ((print_fd = open(argv[6], O_RDONLY)) < 0) |
ef416fc2 | 245 | { |
246 | fprintf(stderr, "ERROR: unable to open print file %s - %s\n", | |
247 | argv[6], strerror(errno)); | |
248 | return (CUPS_BACKEND_FAILED); | |
249 | } | |
250 | ||
251 | copies = atoi(argv[4]); | |
252 | } | |
253 | ||
254 | /* | |
255 | * Finally, send the print file... | |
256 | */ | |
257 | ||
ed486911 | 258 | status = print_device(uri, hostname, resource, options, print_fd, copies, |
259 | argc, argv); | |
ef416fc2 | 260 | |
261 | /* | |
262 | * Close the input file and return... | |
263 | */ | |
264 | ||
ed486911 | 265 | if (print_fd != 0) |
266 | close(print_fd); | |
ef416fc2 | 267 | |
268 | return (status); | |
269 | } | |
270 | ||
271 | ||
272 | /* | |
f7deaa1a | 273 | * End of "$Id: usb.c 5590 2006-05-26 19:48:26Z mike $". |
ef416fc2 | 274 | */ |