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