]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
757d2cad | 2 | * "$Id: ieee1394.c 5241 2006-03-07 22:07:44Z mike $" |
ef416fc2 | 3 | * |
4 | * IEEE-1394 backend for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2002 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or | |
9 | * without modification, are permitted provided that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above | |
13 | * copyright notice, this list of conditions and the | |
14 | * following disclaimer. | |
15 | * | |
16 | * 2. Redistributions in binary form must reproduce the | |
17 | * above copyright notice, this list of conditions and | |
18 | * the following disclaimer in the documentation and/or | |
19 | * other materials provided with the distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use | |
22 | * of this software must display the following | |
23 | * acknowledgement: | |
24 | * | |
25 | * This product includes software developed by Easy | |
26 | * Software Products. | |
27 | * | |
28 | * 4. The name of Easy Software Products may not be used to | |
29 | * endorse or promote products derived from this software | |
30 | * without specific prior written permission. | |
31 | * | |
32 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS | |
33 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
34 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
35 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
36 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS | |
37 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
38 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
39 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
40 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
41 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
42 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | |
43 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
44 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
45 | * DAMAGE. | |
46 | * | |
47 | * Contents: | |
48 | * | |
49 | * main() - Send a file to the printer. | |
50 | * list_devices() - List all known printer devices... | |
51 | */ | |
52 | ||
53 | /* | |
54 | * Include necessary headers. | |
55 | */ | |
56 | ||
57 | #include "ieee1394.h" | |
58 | ||
59 | ||
60 | /* | |
61 | * Local functions... | |
62 | */ | |
63 | ||
64 | void list_devices(void); | |
65 | ||
66 | ||
67 | /* | |
68 | * 'main()' - Send a file to the printer. | |
69 | * | |
70 | * Usage: | |
71 | * | |
72 | * printer-uri job-id user title copies options [file] | |
73 | */ | |
74 | ||
75 | int /* O - Exit status */ | |
76 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
77 | char *argv[]) /* I - Command-line arguments */ | |
78 | { | |
79 | ieee1394_dev_t dev; /* Printer device */ | |
80 | int fp; /* Print file */ | |
81 | int copies; /* Number of copies to print */ | |
82 | int rbytes; /* Number of bytes read from device */ | |
83 | size_t nbytes, /* Number of bytes read from file */ | |
84 | tbytes; /* Total number of bytes written */ | |
85 | char buffer[8192]; /* Input/output buffer */ | |
86 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
87 | struct sigaction action; /* Actions for POSIX signals */ | |
88 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
89 | ||
90 | ||
91 | /* | |
92 | * Make sure status messages are not buffered... | |
93 | */ | |
94 | ||
95 | setbuf(stderr, NULL); | |
96 | ||
97 | /* | |
98 | * Check command-line... | |
99 | */ | |
100 | ||
101 | if (argc == 1) | |
102 | { | |
103 | list_devices(); | |
104 | ||
105 | return (0); | |
106 | } | |
107 | else if (argc < 6 || argc > 7) | |
108 | { | |
109 | fprintf(stderr, "Usage: %s job-id user title copies options [file]\n", | |
110 | argv[0]); | |
111 | return (1); | |
112 | } | |
113 | ||
114 | /* | |
115 | * If we have 7 arguments, print the file named on the command-line. | |
116 | * Otherwise, send stdin instead... | |
117 | */ | |
118 | ||
119 | if (argc == 6) | |
120 | { | |
121 | fp = 0; | |
122 | copies = 1; | |
123 | } | |
124 | else | |
125 | { | |
126 | /* | |
127 | * Try to open the print file... | |
128 | */ | |
129 | ||
130 | if ((fp = open(argv[6], O_RDONLY)) < 0) | |
131 | { | |
132 | perror("ERROR: unable to open print file"); | |
133 | return (1); | |
134 | } | |
135 | ||
136 | copies = atoi(argv[4]); | |
137 | } | |
138 | ||
139 | /* | |
140 | * Try to open the printer device... | |
141 | */ | |
142 | ||
757d2cad | 143 | fputs("STATE: +connecting-to-device\n", stderr); |
144 | ||
ef416fc2 | 145 | do |
146 | { | |
147 | if ((dev = ieee1394_open(argv[0])) == NULL) | |
148 | { | |
149 | fputs("INFO: Firewire printer busy; will retry in 30 seconds...\n", stderr); | |
150 | sleep(30); | |
151 | } | |
152 | } | |
153 | while (dev == NULL); | |
154 | ||
757d2cad | 155 | fputs("STATE: -connecting-to-device\n", stderr); |
156 | ||
ef416fc2 | 157 | /* |
158 | * Now that we are "connected" to the port, ignore SIGTERM so that we | |
159 | * can finish out any page data the driver sends (e.g. to eject the | |
160 | * current page... Only ignore SIGTERM if we are printing data from | |
161 | * stdin (otherwise you can't cancel raw jobs...) | |
162 | */ | |
163 | ||
164 | if (argc < 7) | |
165 | { | |
166 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
167 | sigset(SIGTERM, SIG_IGN); | |
168 | #elif defined(HAVE_SIGACTION) | |
169 | memset(&action, 0, sizeof(action)); | |
170 | ||
171 | sigemptyset(&action.sa_mask); | |
172 | action.sa_handler = SIG_IGN; | |
173 | sigaction(SIGTERM, &action, NULL); | |
174 | #else | |
175 | signal(SIGTERM, SIG_IGN); | |
176 | #endif /* HAVE_SIGSET */ | |
177 | } | |
178 | ||
179 | /* | |
180 | * Finally, send the print file... | |
181 | */ | |
182 | ||
183 | while (copies > 0) | |
184 | { | |
185 | copies --; | |
186 | ||
187 | if (fp != 0) | |
188 | { | |
189 | fputs("PAGE: 1 1\n", stderr); | |
190 | lseek(fp, 0, SEEK_SET); | |
191 | } | |
192 | ||
193 | tbytes = 0; | |
194 | while ((nbytes = read(fp, buffer, sizeof(buffer))) > 0) | |
195 | { | |
196 | /* | |
197 | * Write the print data to the printer... | |
198 | */ | |
199 | ||
200 | tbytes += nbytes; | |
201 | ||
202 | if (ieee1394_write(dev, buffer, nbytes) < 0) | |
203 | { | |
204 | perror("ERROR: Unable to send print file to printer"); | |
205 | break; | |
206 | } | |
207 | ||
208 | if ((rbytes = ieee1394_read(dev, buffer, sizeof(buffer))) > 0) | |
209 | fprintf(stderr, "INFO: Read %d bytes from printer...\n", rbytes); | |
210 | ||
211 | if (argc > 6) | |
212 | fprintf(stderr, "INFO: Sending print file, %lu bytes...\n", | |
213 | (unsigned long)tbytes); | |
214 | } | |
215 | } | |
216 | ||
217 | /* | |
218 | * Close the printer device and input file and return... | |
219 | */ | |
220 | ||
221 | ieee1394_close(dev); | |
222 | ||
223 | if (fp != 0) | |
224 | close(fp); | |
225 | ||
226 | fputs("INFO: Ready to print.\n", stderr); | |
227 | ||
228 | return (0); | |
229 | } | |
230 | ||
231 | ||
232 | /* | |
233 | * 'list_devices()' - List all known devices... | |
234 | */ | |
235 | ||
236 | void | |
237 | list_devices(void) | |
238 | { | |
239 | int i, /* Looping var */ | |
240 | num_info; /* Number of devices */ | |
241 | ieee1394_info_t *info; /* Devices... */ | |
242 | ||
243 | ||
244 | /* | |
245 | * Get the available devices... | |
246 | */ | |
247 | ||
248 | info = ieee1394_list(&num_info); | |
249 | ||
250 | /* | |
251 | * List them as needed... | |
252 | */ | |
253 | ||
254 | if (num_info > 0) | |
255 | { | |
256 | for (i = 0; i < num_info; i ++) | |
257 | printf("direct %s \"%s\" \"%s\"\n", info[i].uri, | |
258 | info[i].make_model, info[i].description); | |
259 | ||
260 | free(info); | |
261 | } | |
262 | } | |
263 | ||
264 | ||
265 | /* | |
757d2cad | 266 | * End of "$Id: ieee1394.c 5241 2006-03-07 22:07:44Z mike $". |
ef416fc2 | 267 | */ |