1 | /*
|
---|
2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup lprint
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Print on a printer
|
---|
36 | *
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <errno.h>
|
---|
40 | #include <io/chardev.h>
|
---|
41 | #include <loc.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <str.h>
|
---|
45 |
|
---|
46 | #define NAME "lprint"
|
---|
47 |
|
---|
48 | #define BUF_SIZE 1024
|
---|
49 |
|
---|
50 | static void syntax_print(void);
|
---|
51 |
|
---|
52 | /** Get default printer port.
|
---|
53 | *
|
---|
54 | * @param sid Place to store service ID of the printer port
|
---|
55 | * @return EOK on success or error code
|
---|
56 | */
|
---|
57 | static errno_t lprint_get_def_printer_port(service_id_t *sid)
|
---|
58 | {
|
---|
59 | category_id_t cid;
|
---|
60 | service_id_t *sids;
|
---|
61 | size_t nsids;
|
---|
62 | errno_t rc;
|
---|
63 |
|
---|
64 | rc = loc_category_get_id("printer-port", &cid, 0);
|
---|
65 | if (rc != EOK)
|
---|
66 | return EIO;
|
---|
67 |
|
---|
68 | rc = loc_category_get_svcs(cid, &sids, &nsids);
|
---|
69 | if (rc != EOK)
|
---|
70 | return EIO;
|
---|
71 |
|
---|
72 | if (nsids < 1) {
|
---|
73 | free(sids);
|
---|
74 | return EIO;
|
---|
75 | }
|
---|
76 |
|
---|
77 | *sid = sids[0];
|
---|
78 | free(sids);
|
---|
79 | return EOK;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Print a message.
|
---|
83 | *
|
---|
84 | * @param chardev Character device
|
---|
85 | * @param argc Number of arguments
|
---|
86 | * @param argv Arguments - strings to print
|
---|
87 | *
|
---|
88 | * @return EOK on success or error code
|
---|
89 | */
|
---|
90 | static errno_t lprint_msg(chardev_t *chardev, int argc, char *argv[])
|
---|
91 | {
|
---|
92 | const char *msg;
|
---|
93 | size_t nbytes;
|
---|
94 | const char *sep;
|
---|
95 | errno_t rc;
|
---|
96 |
|
---|
97 | while (argc > 0) {
|
---|
98 | msg = *argv;
|
---|
99 | --argc;
|
---|
100 | ++argv;
|
---|
101 |
|
---|
102 | rc = chardev_write(chardev, msg, str_size(msg), &nbytes);
|
---|
103 | if (rc != EOK) {
|
---|
104 | printf(NAME ": Failed sending data.\n");
|
---|
105 | return EIO;
|
---|
106 | }
|
---|
107 |
|
---|
108 | sep = argc > 0 ? " " : "\n";
|
---|
109 |
|
---|
110 | rc = chardev_write(chardev, sep, str_size(sep), &nbytes);
|
---|
111 | if (rc != EOK) {
|
---|
112 | printf(NAME ": Failed sending data.\n");
|
---|
113 | return EIO;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | return EOK;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Print a file.
|
---|
121 | *
|
---|
122 | * @param chardev Character device
|
---|
123 | * @param fname File name
|
---|
124 | *
|
---|
125 | * @return EOK on success or error code
|
---|
126 | */
|
---|
127 | static errno_t lprint_file(chardev_t *chardev, const char *fname)
|
---|
128 | {
|
---|
129 | void *buf;
|
---|
130 | size_t nread, nwritten;
|
---|
131 | FILE *f = NULL;
|
---|
132 | errno_t rc;
|
---|
133 |
|
---|
134 | buf = malloc(BUF_SIZE);
|
---|
135 | if (buf == NULL) {
|
---|
136 | printf(NAME ": Out of memory.\n");
|
---|
137 | rc = ENOMEM;
|
---|
138 | goto error;
|
---|
139 | }
|
---|
140 |
|
---|
141 | f = fopen(fname, "rb");
|
---|
142 | if (f == NULL) {
|
---|
143 | printf(NAME ": Cannot open '%s'.\n", fname);
|
---|
144 | rc = EIO;
|
---|
145 | goto error;
|
---|
146 | }
|
---|
147 |
|
---|
148 | while (true) {
|
---|
149 | nread = fread(buf, 1, BUF_SIZE, f);
|
---|
150 | if (ferror(f)) {
|
---|
151 | printf(NAME ": Error reading file.\n");
|
---|
152 | rc = EIO;
|
---|
153 | goto error;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (nread == 0)
|
---|
157 | break;
|
---|
158 |
|
---|
159 | rc = chardev_write(chardev, buf, nread, &nwritten);
|
---|
160 | if (rc != EOK) {
|
---|
161 | printf(NAME ": Failed sending data.\n");
|
---|
162 | rc = EIO;
|
---|
163 | goto error;
|
---|
164 | }
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
168 | fclose(f);
|
---|
169 | return EOK;
|
---|
170 | error:
|
---|
171 | if (buf != NULL)
|
---|
172 | free(buf);
|
---|
173 | if (f != NULL)
|
---|
174 | fclose(f);
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 | int main(int argc, char **argv)
|
---|
179 | {
|
---|
180 | chardev_t *chardev;
|
---|
181 | errno_t rc;
|
---|
182 | async_sess_t *sess;
|
---|
183 | service_id_t sid;
|
---|
184 | const char *svc_name = NULL;
|
---|
185 | bool msg_mode = false;
|
---|
186 |
|
---|
187 | if (argc < 2) {
|
---|
188 | printf(NAME ": Error, argument missing.\n");
|
---|
189 | syntax_print();
|
---|
190 | return 1;
|
---|
191 | }
|
---|
192 |
|
---|
193 | --argc;
|
---|
194 | ++argv;
|
---|
195 |
|
---|
196 | while (*argv != NULL && *argv[0] == '-') {
|
---|
197 | if (str_cmp(*argv, "-d") == 0) {
|
---|
198 | --argc;
|
---|
199 | ++argv;
|
---|
200 |
|
---|
201 | if (*argv == NULL) {
|
---|
202 | printf(NAME ": Error, argument missing.\n");
|
---|
203 | syntax_print();
|
---|
204 | return 1;
|
---|
205 | }
|
---|
206 |
|
---|
207 | svc_name = *argv;
|
---|
208 | --argc;
|
---|
209 | ++argv;
|
---|
210 | continue;
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (str_cmp(*argv, "-m") == 0) {
|
---|
214 | msg_mode = true;
|
---|
215 | --argc;
|
---|
216 | ++argv;
|
---|
217 | continue;
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (str_cmp(*argv, "--help") == 0) {
|
---|
221 | --argc;
|
---|
222 | ++argv;
|
---|
223 |
|
---|
224 | if (*argv != NULL) {
|
---|
225 | printf(NAME ": Error, unexpected argument.\n");
|
---|
226 | syntax_print();
|
---|
227 | return 1;
|
---|
228 | }
|
---|
229 |
|
---|
230 | syntax_print();
|
---|
231 | return 0;
|
---|
232 | }
|
---|
233 |
|
---|
234 | printf(NAME ": Error, invalid argument.\n");
|
---|
235 | syntax_print();
|
---|
236 | return 1;
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (argc < 1) {
|
---|
240 | printf(NAME ": Error, argument missing.\n");
|
---|
241 | syntax_print();
|
---|
242 | return 1;
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (!msg_mode && argc > 1) {
|
---|
246 | printf(NAME ": Error, too many arguments.\n");
|
---|
247 | syntax_print();
|
---|
248 | return 1;
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (svc_name != NULL) {
|
---|
252 | rc = loc_service_get_id(svc_name, &sid, 0);
|
---|
253 | if (rc != EOK) {
|
---|
254 | printf(NAME ": Failed resolving printer port service "
|
---|
255 | "'%s'.\n", svc_name);
|
---|
256 | return 1;
|
---|
257 | }
|
---|
258 | } else {
|
---|
259 | rc = lprint_get_def_printer_port(&sid);
|
---|
260 | if (rc != EOK) {
|
---|
261 | printf(NAME ": No printer found.\n");
|
---|
262 | return 1;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | sess = loc_service_connect(sid, INTERFACE_DDF, 0);
|
---|
267 | if (sess == NULL) {
|
---|
268 | printf(NAME ": Failed connecting printer port service.\n");
|
---|
269 | return 1;
|
---|
270 | }
|
---|
271 |
|
---|
272 | rc = chardev_open(sess, &chardev);
|
---|
273 | if (rc != EOK) {
|
---|
274 | async_hangup(sess);
|
---|
275 | printf(NAME ": Failed opening printer port device.\n");
|
---|
276 | return 1;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (msg_mode) {
|
---|
280 | rc = lprint_msg(chardev, argc, argv);
|
---|
281 | if (rc != EOK) {
|
---|
282 | chardev_close(chardev);
|
---|
283 | return 1;
|
---|
284 | }
|
---|
285 | } else {
|
---|
286 | rc = lprint_file(chardev, argv[0]);
|
---|
287 | if (rc != EOK) {
|
---|
288 | chardev_close(chardev);
|
---|
289 | return 1;
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | chardev_close(chardev);
|
---|
294 | return 0;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** Print syntax help. */
|
---|
298 | static void syntax_print(void)
|
---|
299 | {
|
---|
300 | printf("syntax:\n"
|
---|
301 | "\tlprint [<options>] <file>\n"
|
---|
302 | "\tlprint [<options>] -m <message...>\n"
|
---|
303 | "options:\n"
|
---|
304 | "\t-d <device>Print to the specified device\n");
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * @}
|
---|
309 | */
|
---|