]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/scsi.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / scsi.c
1 /*
2 * "$Id: scsi.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * SCSI printer backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 2003-2006 by Easy Software Products, all rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the
11 * following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the
15 * following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the
18 * above copyright notice, this list of conditions and
19 * the following disclaimer in the documentation and/or
20 * other materials provided with the distribution.
21 *
22 * 3. All advertising materials mentioning features or use
23 * of this software must display the following
24 * acknowledgement:
25 *
26 * This product includes software developed by Easy
27 * Software Products.
28 *
29 * 4. The name of Easy Software Products may not be used to
30 * endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS
34 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
35 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
38 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
44 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46 * DAMAGE.
47 *
48 * Contents:
49 *
50 * main() - Send a file to the specified SCSI printer.
51 */
52
53 /*
54 * Include necessary headers.
55 */
56
57 #include <cups/backend.h>
58 #include <cups/cups.h>
59 #include <cups/i18n.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <errno.h>
63 #include <cups/string.h>
64 #include <cups/i18n.h>
65 #include <signal.h>
66
67 #ifdef WIN32
68 # include <io.h>
69 #else
70 # include <unistd.h>
71 # include <fcntl.h>
72 # ifdef HAVE_SYS_IOCTL_H
73 # include <sys/ioctl.h>
74 # endif /* HAVE_SYS_IOCTL_H */
75 #endif /* WIN32 */
76
77
78 /*
79 * Local functions...
80 */
81
82 void list_devices(void);
83 int print_device(const char *resource, int fd, int copies);
84
85
86 #if defined(__linux__) && defined(HAVE_SCSI_SG_H)
87 # include "scsi-linux.c"
88 #elif defined(__sgi)
89 # include "scsi-irix.c"
90 #else
91 /*
92 * Dummy functions that do nothing on unsupported platforms...
93 */
94 void list_devices(void) {}
95 int print_device(const char *resource, int fd, int copies) { return (CUPS_BACKEND_FAILED); }
96 #endif /* __linux && HAVE_SCSI_SG_H */
97
98
99 /*
100 * 'main()' - Send a file to the specified SCSI printer.
101 *
102 * Usage:
103 *
104 * printer-uri job-id user title copies options [file]
105 */
106
107 int /* O - Exit status */
108 main(int argc, /* I - Number of command-line arguments (6 or 7) */
109 char *argv[]) /* I - Command-line arguments */
110 {
111 char method[255], /* Method in URI */
112 hostname[1024], /* Hostname */
113 username[255], /* Username info (not used) */
114 resource[1024], /* Resource info (device and options) */
115 *options; /* Pointer to options */
116 int port; /* Port number (not used) */
117 int fp; /* Print file */
118 int copies; /* Number of copies to print */
119 int status; /* Exit status */
120 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
121 struct sigaction action; /* Actions for POSIX signals */
122 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
123
124
125 /*
126 * Make sure status messages are not buffered...
127 */
128
129 setbuf(stderr, NULL);
130
131 /*
132 * Ignore SIGPIPE signals...
133 */
134
135 #ifdef HAVE_SIGSET
136 sigset(SIGPIPE, SIG_IGN);
137 #elif defined(HAVE_SIGACTION)
138 memset(&action, 0, sizeof(action));
139 action.sa_handler = SIG_IGN;
140 sigaction(SIGPIPE, &action, NULL);
141 #else
142 signal(SIGPIPE, SIG_IGN);
143 #endif /* HAVE_SIGSET */
144
145 /*
146 * Check command-line...
147 */
148
149 if (argc == 1)
150 {
151 list_devices();
152 return (CUPS_BACKEND_OK);
153 }
154 else if (argc < 6 || argc > 7)
155 {
156 fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
157 argv[0]);
158 return (CUPS_BACKEND_FAILED);
159 }
160
161 /*
162 * If we have 7 arguments, print the file named on the command-line.
163 * Otherwise, send stdin instead...
164 */
165
166 if (argc == 6)
167 {
168 fp = 0;
169 copies = 1;
170 }
171 else
172 {
173 /*
174 * Try to open the print file...
175 */
176
177 if ((fp = open(argv[6], O_RDONLY)) < 0)
178 {
179 perror("ERROR: unable to open print file");
180 return (CUPS_BACKEND_FAILED);
181 }
182
183 copies = atoi(argv[4]);
184 }
185
186 /*
187 * Extract the device name and options from the URI...
188 */
189
190 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
191 method, sizeof(method), username, sizeof(username),
192 hostname, sizeof(hostname), &port,
193 resource, sizeof(resource));
194
195 /*
196 * See if there are any options...
197 */
198
199 if ((options = strchr(resource, '?')) != NULL)
200 {
201 /*
202 * Yup, terminate the device name string and move to the first
203 * character of the options...
204 */
205
206 *options++ = '\0';
207 }
208
209 /*
210 * Finally, send the print file...
211 */
212
213 status = print_device(resource, fp, copies);
214
215 /*
216 * Close input file and return...
217 */
218
219 if (fp != 0)
220 close(fp);
221
222 return (status);
223 }
224
225
226 /*
227 * End of "$Id: scsi.c 6649 2007-07-11 21:46:42Z mike $".
228 */