-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlprint.c
469 lines (391 loc) · 12.8 KB
/
lprint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//
// Main entry for LPrint, a Label Printer Application
//
// Copyright © 2019-2020 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
//
// Include necessary headers...
//
#define _LPRINT_C_
#include "lprint.h"
#include <spawn.h>
//
// Local globals...
//
static char *lprint_path = NULL; // Path to self
//
// Local functions...
//
static void usage(int status) LPRINT_NORETURN;
//
// 'main()' - Main entry for LPrint.
//
int // O - Exit status
main(int argc, // I - Number of command-line arguments
char *argv[]) // I - Command-line arguments
{
int i, j; // Looping vars
const char *opt, // Current option character
*subcommand = NULL; // Sub-command name
int num_files = 0; // Number of files
char *files[1000]; // Files
int num_options = 0; // Number of options
cups_option_t *options = NULL; // Options
static const char * const subcommands[] =
{ // List of sub-commands
"add",
"cancel",
"default",
"delete",
"devices",
"drivers",
"jobs",
"modify",
"options",
"printers",
"server",
"shutdown",
"status",
"submit"
};
lprint_path = argv[0];
for (i = 1; i < argc; i ++)
{
if (!strcmp(argv[i], "--help"))
{
usage(0);
}
else if (!strcmp(argv[i], "--version"))
{
puts(LPRINT_VERSION);
return (0);
}
else if (!strcmp(argv[i], "--"))
{
// Filename follows
i ++;
if (i >= argc)
{
fputs("lprint: Missing filename after '--'.\n", stderr);
usage(1);
}
else if (num_files >= (int)(sizeof(files) / sizeof(files[0])))
{
fputs("lprint: Too many files.\n", stderr);
return (1);
}
files[num_files ++] = argv[i];
}
else if (!strncmp(argv[i], "--", 2))
{
fprintf(stderr, "lprint: Unknown option '%s'.\n", argv[i]);
usage(1);
}
else if (!strcmp(argv[i], "-") || argv[i][0] != '-')
{
for (j = 0; j < (int)(sizeof(subcommands) / sizeof(subcommands[0])); j ++)
{
if (!strcmp(argv[i], subcommands[j]))
{
subcommand = argv[i];
break;
}
}
if (j >= (int)(sizeof(subcommands) / sizeof(subcommands[0])))
{
if (num_files >= (int)(sizeof(files) / sizeof(files[0])))
{
fputs("lprint: Too many files.\n", stderr);
return (1);
}
files[num_files ++] = argv[i];
}
}
else
{
for (opt = argv[i] + 1; *opt; opt ++)
{
switch (*opt)
{
case 'a' : // All
num_options = cupsAddOption("cancel-all", "true", num_options, &options);
break;
case 'd' : // printer name
i ++;
if (i >= argc)
{
fputs("lprint: Missing printer name.\n", stderr);
usage(1);
}
num_options = cupsAddOption("printer-name", argv[i], num_options, &options);
break;
case 'j' : // job ID
i ++;
if (i >= argc)
{
fputs("lprint: Missing job ID.\n", stderr);
usage(1);
}
num_options = cupsAddOption("job-id", argv[i], num_options, &options);
break;
case 'm' : // driver name
i ++;
if (i >= argc)
{
fputs("lprint: Missing driver name.\n", stderr);
usage(1);
}
num_options = cupsAddOption("lprint-driver", argv[i], num_options, &options);
break;
case 'n' : // copies
i ++;
if (i >= argc)
{
fputs("lprint: Missing copy count.\n", stderr);
usage(1);
}
num_options = cupsAddOption("copies", argv[i], num_options, &options);
break;
case 'o' : // name=value
i ++;
if (i >= argc)
{
fputs("lprint: Missing option.\n", stderr);
usage(1);
}
num_options = cupsParseOptions(argv[i], num_options, &options);
break;
case 't' : // title
i ++;
if (i >= argc)
{
fputs("lprint: Missing title.\n", stderr);
usage(1);
}
num_options = cupsAddOption("job-name", argv[i], num_options, &options);
break;
case 'u' : // printer-uri
i ++;
if (i >= argc)
{
fputs("lprint: Missing printer URI.\n", stderr);
usage(1);
}
num_options = cupsAddOption("printer-uri", argv[i], num_options, &options);
break;
case 'v' : // device-uri
i ++;
if (i >= argc)
{
fputs("lprint: Missing device URI.\n", stderr);
usage(1);
}
num_options = cupsAddOption("device-uri", argv[i], num_options, &options);
break;
default :
fprintf(stderr, "lprint: Unknown option '-%c'.\n", *opt);
usage(1);
}
}
}
}
#ifdef DEBUG
LPRINT_DEBUG("subcommand='%s'\n", subcommand);
LPRINT_DEBUG("num_options=%d\n", num_options);
for (i = 0; i < num_options; i ++)
LPRINT_DEBUG("options[%d].name='%s', value='%s'\n", i, options[i].name, options[i].value);
LPRINT_DEBUG("num_files=%d\n", num_files);
for (i = 0; i < num_files; i ++)
LPRINT_DEBUG("files[%d]='%s'\n", i, files[i]);
#endif // DEBUG
if (!subcommand || !strcmp(subcommand, "submit"))
{
return (lprintDoSubmit(num_files, files, num_options, options));
}
else if (num_files > 0)
{
fprintf(stderr, "lprint: Sub-command '%s' does not accept files.\n", subcommand);
usage(1);
}
else if (!strcmp(subcommand, "add"))
return (lprintDoAdd(num_options, options));
else if (!strcmp(subcommand, "cancel"))
return (lprintDoCancel(num_options, options));
else if (!strcmp(subcommand, "default"))
return (lprintDoDefault(num_options, options));
else if (!strcmp(subcommand, "delete"))
return (lprintDoDelete(num_options, options));
else if (!strcmp(subcommand, "devices"))
return (lprintDoDevices(num_options, options));
else if (!strcmp(subcommand, "drivers"))
return (lprintDoDrivers(num_options, options));
else if (!strcmp(subcommand, "jobs"))
return (lprintDoJobs(num_options, options));
else if (!strcmp(subcommand, "modify"))
return (lprintDoModify(num_options, options));
else if (!strcmp(subcommand, "options"))
return (lprintDoOptions(num_options, options));
else if (!strcmp(subcommand, "printers"))
return (lprintDoPrinters(num_options, options));
else if (!strcmp(subcommand, "server"))
return (lprintDoServer(num_options, options));
else if (!strcmp(subcommand, "shutdown"))
return (lprintDoShutdown(num_options, options));
else if (!strcmp(subcommand, "status"))
return (lprintDoStatus(num_options, options));
else
{
fprintf(stderr, "lprint: Unhandled sub-command '%s'.\n", subcommand);
usage(1);
}
}
//
// 'lprintAddPrinterURI()' - Add the printer-uri attribute and return a resource path.
//
void
lprintAddPrinterURI(
ipp_t *request, // I - IPP request
const char *printer_name, // I - Printer name
char *resource, // I - Resource path buffer
size_t rsize) // I - Size of buffer
{
char uri[1024]; // printer-uri value
snprintf(resource, rsize, "/ipp/print/%s", printer_name);
httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, resource);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
}
//
// 'lprintConnect()' - Connect to the local server.
//
http_t * // O - HTTP connection
lprintConnect(int auto_start) // I - 1 to start server if not running
{
http_t *http; // HTTP connection
char sockname[1024]; // Socket filename
// See if the server is running...
http = httpConnect2(lprintGetServerPath(sockname, sizeof(sockname)), 0, NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL);
if (!http && auto_start)
{
// Nope, start it now...
pid_t server_pid; // Server process ID
posix_spawnattr_t server_attrs; // Server process attributes
char * const server_argv[] = // Server command-line
{
lprint_path,
"server",
NULL
};
posix_spawnattr_init(&server_attrs);
posix_spawnattr_setpgroup(&server_attrs, 0);
if (posix_spawn(&server_pid, lprint_path, NULL, &server_attrs, server_argv, environ))
{
perror("Unable to start lprint server");
posix_spawnattr_destroy(&server_attrs);
return (NULL);
}
posix_spawnattr_destroy(&server_attrs);
// Wait for it to start...
while (access(lprintGetServerPath(sockname, sizeof(sockname)), 0))
usleep(250000);
http = httpConnect2(sockname, 0, NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL);
if (!http)
fprintf(stderr, "lprint: Unable to connect to server - %s\n", cupsLastErrorString());
}
return (http);
}
//
// 'lprintConnectURI()' - Connect to an IPP printer directly.
//
http_t * // O - HTTP connection or `NULL` on error
lprintConnectURI(
const char *printer_uri, // I - Printer URI
char *resource, // I - Resource path buffer
size_t rsize) // I - Size of buffer
{
char scheme[32], // Scheme (ipp or ipps)
userpass[256], // Username/password (unused)
hostname[256]; // Hostname
int port; // Port number
http_encryption_t encryption; // Type of encryption to use
http_t *http; // HTTP connection
// First extract the components of the URI...
if (httpSeparateURI(HTTP_URI_CODING_ALL, printer_uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, (int)rsize) < HTTP_URI_STATUS_OK)
{
fprintf(stderr, "lprint: Bad printer URI '%s'.\n", printer_uri);
return (NULL);
}
if (strcmp(scheme, "ipp") && strcmp(scheme, "ipps"))
{
fprintf(stderr, "lprint: Unsupported URI scheme '%s'.\n", scheme);
return (NULL);
}
if (userpass[0])
fputs("lprint: User credentials are not supported in URIs.\n", stderr);
if (!strcmp(scheme, "ipps") || port == 443)
encryption = HTTP_ENCRYPTION_ALWAYS;
else
encryption = HTTP_ENCRYPTION_IF_REQUESTED;
if ((http = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL)) == NULL)
fprintf(stderr, "lprint: Unable to connect to printer at '%s' - %s\n", printer_uri, cupsLastErrorString());
return (http);
}
//
// 'lprintGetServerPath()' - Get the UNIX domain socket for the server.
//
char * // O - Socket filename
lprintGetServerPath(char *buffer, // I - Buffer for filenaem
size_t bufsize) // I - Size of buffer
{
const char *tmpdir = getenv("TMPDIR");
// Temporary directory
#ifdef __APPLE__
if (!tmpdir)
tmpdir = "/private/tmp";
#else
if (!tmpdir)
tmpdir = "/tmp";
#endif // __APPLE__
snprintf(buffer, bufsize, "%s/lprint%d.sock", tmpdir, (int)getuid());
return (buffer);
}
//
// 'usage()' - Show program usage.
//
static void
usage(int status) // O - Exit status
{
FILE *fp = status ? stderr : stdout; // Where to send message
fputs("Usage: lprint SUB-COMMAND [OPTIONS] [FILENAME]\n", fp);
fputs(" lprint [OPTIONS] [FILENAME]\n", fp);
fputs(" lprint [OPTIONS] -\n", fp);
fputs("\n", fp);
fputs("Sub-commands:\n", fp);
fputs(" add PRINTER Add a printer.\n", fp);
fputs(" cancel Cancel one or more jobs.\n", fp);
fputs(" default Set the default printer.\n", fp);
fputs(" delete Delete a printer.\n", fp);
fputs(" devices List devices.\n", fp);
fputs(" drivers List drivers.\n", fp);
fputs(" jobs List jobs.\n", fp);
fputs(" modify Modify a printer.\n", fp);
fputs(" options List printer options.\n", fp);
fputs(" printers List printers.\n", fp);
fputs(" server Run a server.\n", fp);
fputs(" shutdown Shutdown a running server.\n", fp);
fputs(" status Show server/printer/job status.\n", fp);
fputs(" submit Submit a file for printing.\n", fp);
fputs("\n", fp);
fputs("Options:\n", fp);
fputs(" -a Cancel all jobs (cancel).\n", fp);
fputs(" -d PRINTER Specify printer.\n", fp);
fputs(" -j JOB-ID Specify job ID (cancel).\n", fp);
fputs(" -m DRIVER-NAME Specify driver (add/modify).\n", fp);
fputs(" -n COPIES Specify number of copies (submit).\n", fp);
fputs(" -o NAME=VALUE Specify option (add,modify,server,submit).\n", fp);
fputs(" -u URI Specify ipp: or ipps: printer/server.\n", fp);
fputs(" -v DEVICE-URI Specify socket: or usb: device (add/modify).\n", fp);
exit(status);
}