]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
a4d04587 | 2 | * "$Id: usb.c 5023 2006-01-29 14:39:44Z 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, | |
66 | int fp, int copies); | |
67 | ||
68 | ||
69 | /* | |
70 | * Include the vendor-specific USB implementation... | |
71 | */ | |
72 | ||
73 | #ifdef __APPLE__ | |
74 | # include "usb-darwin.c" | |
75 | #elif defined(__linux) || defined(__sun) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) | |
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 */ | |
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; | |
125 | (void)fp; | |
126 | (void)copies; | |
127 | ||
128 | return (CUPS_BACKEND_FAILED); | |
129 | } | |
130 | #endif /* __APPLE__ */ | |
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 | ||
141 | int /* O - Exit status */ | |
142 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
143 | char *argv[]) /* I - Command-line arguments */ | |
144 | { | |
145 | int fp; /* Print file */ | |
146 | int copies; /* Number of copies to print */ | |
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 */ | |
155 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
156 | struct sigaction action; /* Actions for POSIX signals */ | |
157 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
158 | ||
159 | ||
160 | /* | |
161 | * Make sure status messages are not buffered... | |
162 | */ | |
163 | ||
164 | setbuf(stderr, NULL); | |
165 | ||
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 | ||
180 | /* | |
181 | * Check command-line... | |
182 | */ | |
183 | ||
184 | if (argc == 1) | |
185 | { | |
186 | list_devices(); | |
187 | return (CUPS_BACKEND_OK); | |
188 | } | |
189 | else if (argc < 6 || argc > 7) | |
190 | { | |
191 | fputs("Usage: usb job-id user title copies options [file]\n", stderr); | |
192 | return (CUPS_BACKEND_FAILED); | |
193 | } | |
194 | ||
195 | /* | |
196 | * Extract the device name and options from the URI... | |
197 | */ | |
198 | ||
a4d04587 | 199 | uri = cupsBackendDeviceURI(argv); |
ef416fc2 | 200 | |
a4d04587 | 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) | |
ef416fc2 | 205 | { |
206 | fputs("ERROR: No device URI found in argv[0] or in DEVICE_URI environment variable!\n", stderr); | |
207 | return (1); | |
208 | } | |
209 | ||
ef416fc2 | 210 | /* |
211 | * See if there are any options... | |
212 | */ | |
213 | ||
214 | if ((options = strchr(resource, '?')) != NULL) | |
215 | { | |
216 | /* | |
217 | * Yup, terminate the device name string and move to the first | |
218 | * character of the options... | |
219 | */ | |
220 | ||
221 | *options++ = '\0'; | |
222 | } | |
223 | ||
224 | /* | |
225 | * If we have 7 arguments, print the file named on the command-line. | |
226 | * Otherwise, send stdin instead... | |
227 | */ | |
228 | ||
229 | if (argc == 6) | |
230 | { | |
231 | fp = 0; | |
232 | copies = 1; | |
233 | } | |
234 | else | |
235 | { | |
236 | /* | |
237 | * Try to open the print file... | |
238 | */ | |
239 | ||
240 | if ((fp = open(argv[6], O_RDONLY)) < 0) | |
241 | { | |
242 | fprintf(stderr, "ERROR: unable to open print file %s - %s\n", | |
243 | argv[6], strerror(errno)); | |
244 | return (CUPS_BACKEND_FAILED); | |
245 | } | |
246 | ||
247 | copies = atoi(argv[4]); | |
248 | } | |
249 | ||
250 | /* | |
251 | * Finally, send the print file... | |
252 | */ | |
253 | ||
254 | status = print_device(uri, hostname, resource, options, fp, copies); | |
255 | ||
256 | /* | |
257 | * Close the input file and return... | |
258 | */ | |
259 | ||
260 | if (fp != 0) | |
261 | close(fp); | |
262 | ||
263 | return (status); | |
264 | } | |
265 | ||
266 | ||
267 | /* | |
a4d04587 | 268 | * End of "$Id: usb.c 5023 2006-01-29 14:39:44Z mike $". |
ef416fc2 | 269 | */ |