]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/parallel.c
Moved MIME stuff to scheduler directory.
[thirdparty/cups.git] / backend / parallel.c
1 /*
2 * "$Id: parallel.c,v 1.9 2000/01/25 03:50:47 mike Exp $"
3 *
4 * Parallel port backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2000 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-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Send a file to the specified parallel port.
27 * list_devices() - List all parallel devices.
28 */
29
30 /*
31 * Include necessary headers.
32 */
33
34 #include <cups/cups.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <cups/string.h>
38
39 #if defined(WIN32) || defined(__EMX__)
40 # include <io.h>
41 #else
42 # include <unistd.h>
43 # include <fcntl.h>
44 # include <termios.h>
45 #endif /* WIN32 || __EMX__ */
46
47
48 /*
49 * Local functions...
50 */
51
52 void list_devices(void);
53
54
55 /*
56 * 'main()' - Send a file to the specified parallel port.
57 *
58 * Usage:
59 *
60 * printer-uri job-id user title copies options [file]
61 */
62
63 int /* O - Exit status */
64 main(int argc, /* I - Number of command-line arguments (6 or 7) */
65 char *argv[]) /* I - Command-line arguments */
66 {
67 char method[255], /* Method in URI */
68 hostname[1024], /* Hostname */
69 username[255], /* Username info (not used) */
70 resource[1024], /* Resource info (device and options) */
71 *options; /* Pointer to options */
72 int port; /* Port number (not used) */
73 FILE *fp; /* Print file */
74 int copies; /* Number of copies to print */
75 int fd; /* Parallel device */
76 int error; /* Error code (if any) */
77 size_t nbytes, /* Number of bytes written */
78 tbytes; /* Total number of bytes written */
79 char buffer[8192]; /* Output buffer */
80 struct termios opts; /* Parallel port options */
81
82
83 if (argc == 1)
84 {
85 list_devices();
86 return (0);
87 }
88 else if (argc < 6 || argc > 7)
89 {
90 fputs("Usage: parallel job-id user title copies options [file]\n", stderr);
91 return (1);
92 }
93
94 /*
95 * If we have 7 arguments, print the file named on the command-line.
96 * Otherwise, send stdin instead...
97 */
98
99 if (argc == 6)
100 {
101 fp = stdin;
102 copies = 1;
103 }
104 else
105 {
106 /*
107 * Try to open the print file...
108 */
109
110 if ((fp = fopen(argv[6], "rb")) == NULL)
111 {
112 perror("ERROR: unable to open print file");
113 return (1);
114 }
115
116 copies = atoi(argv[4]);
117 }
118
119 /*
120 * Extract the device name and options from the URI...
121 */
122
123 httpSeparate(argv[0], method, username, hostname, &port, resource);
124
125 /*
126 * See if there are any options...
127 */
128
129 if ((options = strchr(resource, '?')) != NULL)
130 {
131 /*
132 * Yup, terminate the device name string and move to the first
133 * character of the options...
134 */
135
136 *options++ = '\0';
137 }
138
139 /*
140 * Open the parallel port device...
141 */
142
143 if ((fd = open(resource, O_WRONLY)) == -1)
144 {
145 perror("ERROR: Unable to open parallel port device file");
146 return (1);
147 }
148
149 /*
150 * Set any options provided...
151 */
152
153 tcgetattr(fd, &opts);
154
155 opts.c_lflag &= ~(ICANON | ECHO | ISIG); /* Raw mode */
156
157 /**** No options supported yet ****/
158
159 tcsetattr(fd, TCSANOW, &opts);
160
161 /*
162 * Finally, send the print file...
163 */
164
165 while (copies > 0)
166 {
167 copies --;
168
169 if (fp != stdin)
170 {
171 fputs("PAGE: 1 1\n", stderr);
172 rewind(fp);
173 }
174
175 tbytes = 0;
176 while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
177 {
178 /*
179 * Write the print data to the printer...
180 */
181
182 if (write(fd, buffer, nbytes) < nbytes)
183 {
184 perror("ERROR: Unable to send print file to printer");
185 break;
186 }
187 else
188 tbytes += nbytes;
189
190 if (argc > 6)
191 fprintf(stderr, "INFO: Sending print file, %u bytes...\n", tbytes);
192 }
193 }
194
195 /*
196 * Close the socket connection and input file and return...
197 */
198
199 close(fd);
200 if (fp != stdin)
201 fclose(fp);
202
203 return (0);
204 }
205
206
207 /*
208 * 'list_devices()' - List all parallel devices.
209 */
210
211 void
212 list_devices(void)
213 {
214 #ifdef __linux
215 int i; /* Looping var */
216 char device[255]; /* Device filename */
217
218
219 for (i = 0; i < 4; i ++)
220 {
221 sprintf(device, "/dev/lp%d", i);
222 if (access(device, F_OK) == 0)
223 fprintf(stderr, "parallel parallel:/dev/lp%d \"\" \"Parallel Port #%d\"\n",
224 i, i + 1);
225 }
226 #elif defined(__sgi)
227 #elif defined(__sun)
228 #elif defined(__hpux)
229 #elif defined(__osf)
230 #elif defined(FreeBSD) || defined(OpenBSD) || defined(NetBSD)
231 #endif
232 }
233
234
235 /*
236 * End of "$Id: parallel.c,v 1.9 2000/01/25 03:50:47 mike Exp $".
237 */