Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Backend functions for CUPS. |
3 | | * |
4 | | * Copyright 2007-2015 by Apple Inc. |
5 | | * Copyright 2006 by Easy Software Products. |
6 | | * |
7 | | * These coded instructions, statements, and computer programs are the |
8 | | * property of Apple Inc. and are protected by Federal copyright |
9 | | * law. Distribution and use rights are outlined in the file "LICENSE.txt" |
10 | | * which should have been included with this file. If this file is |
11 | | * missing or damaged, see the license at "http://www.cups.org/". |
12 | | * |
13 | | * This file is subject to the Apple OS-Developed Software exception. |
14 | | */ |
15 | | |
16 | | /* |
17 | | * Include necessary headers... |
18 | | */ |
19 | | |
20 | | #include "cups-private.h" |
21 | | #include "backend.h" |
22 | | #include "ppd.h" |
23 | | |
24 | | |
25 | | /* |
26 | | * Local functions... |
27 | | */ |
28 | | |
29 | | static void quote_string(const char *s); |
30 | | |
31 | | |
32 | | /* |
33 | | * 'cupsBackendDeviceURI()' - Get the device URI for a backend. |
34 | | * |
35 | | * The "argv" argument is the argv argument passed to main(). This |
36 | | * function returns the device URI passed in the DEVICE_URI environment |
37 | | * variable or the device URI passed in argv[0], whichever is found |
38 | | * first. |
39 | | * |
40 | | * @since CUPS 1.2/macOS 10.5@ |
41 | | */ |
42 | | |
43 | | const char * /* O - Device URI or @code NULL@ */ |
44 | | cupsBackendDeviceURI(char **argv) /* I - Command-line arguments */ |
45 | 0 | { |
46 | 0 | const char *device_uri, /* Device URI */ |
47 | 0 | *auth_info_required; /* AUTH_INFO_REQUIRED env var */ |
48 | 0 | _cups_globals_t *cg = _cupsGlobals(); /* Global info */ |
49 | 0 | int options; /* Resolve options */ |
50 | 0 | ppd_file_t *ppd; /* PPD file */ |
51 | 0 | ppd_attr_t *ppdattr; /* PPD attribute */ |
52 | | |
53 | |
|
54 | 0 | if ((device_uri = getenv("DEVICE_URI")) == NULL) |
55 | 0 | { |
56 | 0 | if (!argv || !argv[0] || !strchr(argv[0], ':')) |
57 | 0 | return (NULL); |
58 | | |
59 | 0 | device_uri = argv[0]; |
60 | 0 | } |
61 | | |
62 | 0 | options = _HTTP_RESOLVE_STDERR; |
63 | 0 | if ((auth_info_required = getenv("AUTH_INFO_REQUIRED")) != NULL && |
64 | 0 | !strcmp(auth_info_required, "negotiate")) |
65 | 0 | options |= _HTTP_RESOLVE_FQDN; |
66 | |
|
67 | 0 | if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL) |
68 | 0 | { |
69 | 0 | if ((ppdattr = ppdFindAttr(ppd, "cupsIPPFaxOut", NULL)) != NULL && |
70 | 0 | !_cups_strcasecmp(ppdattr->value, "true")) |
71 | 0 | options |= _HTTP_RESOLVE_FAXOUT; |
72 | |
|
73 | 0 | ppdClose(ppd); |
74 | 0 | } |
75 | |
|
76 | 0 | return (_httpResolveURI(device_uri, cg->resolved_uri, |
77 | 0 | sizeof(cg->resolved_uri), options, NULL, NULL)); |
78 | 0 | } |
79 | | |
80 | | |
81 | | /* |
82 | | * 'cupsBackendReport()' - Write a device line from a backend. |
83 | | * |
84 | | * This function writes a single device line to stdout for a backend. |
85 | | * It handles quoting of special characters in the device-make-and-model, |
86 | | * device-info, device-id, and device-location strings. |
87 | | * |
88 | | * @since CUPS 1.4/macOS 10.6@ |
89 | | */ |
90 | | |
91 | | void |
92 | | cupsBackendReport( |
93 | | const char *device_scheme, /* I - device-scheme string */ |
94 | | const char *device_uri, /* I - device-uri string */ |
95 | | const char *device_make_and_model, /* I - device-make-and-model string or @code NULL@ */ |
96 | | const char *device_info, /* I - device-info string or @code NULL@ */ |
97 | | const char *device_id, /* I - device-id string or @code NULL@ */ |
98 | | const char *device_location) /* I - device-location string or @code NULL@ */ |
99 | 0 | { |
100 | 0 | if (!device_scheme || !device_uri) |
101 | 0 | return; |
102 | | |
103 | 0 | printf("%s %s", device_scheme, device_uri); |
104 | 0 | if (device_make_and_model && *device_make_and_model) |
105 | 0 | quote_string(device_make_and_model); |
106 | 0 | else |
107 | 0 | quote_string("unknown"); |
108 | 0 | quote_string(device_info); |
109 | 0 | quote_string(device_id); |
110 | 0 | quote_string(device_location); |
111 | 0 | putchar('\n'); |
112 | 0 | fflush(stdout); |
113 | 0 | } |
114 | | |
115 | | |
116 | | /* |
117 | | * 'quote_string()' - Write a quoted string to stdout, escaping \ and ". |
118 | | */ |
119 | | |
120 | | static void |
121 | | quote_string(const char *s) /* I - String to write */ |
122 | 0 | { |
123 | 0 | fputs(" \"", stdout); |
124 | |
|
125 | 0 | if (s) |
126 | 0 | { |
127 | 0 | while (*s) |
128 | 0 | { |
129 | 0 | if (*s == '\\' || *s == '\"') |
130 | 0 | putchar('\\'); |
131 | |
|
132 | 0 | if (((*s & 255) < ' ' && *s != '\t') || *s == 0x7f) |
133 | 0 | putchar(' '); |
134 | 0 | else |
135 | 0 | putchar(*s); |
136 | |
|
137 | 0 | s ++; |
138 | 0 | } |
139 | 0 | } |
140 | |
|
141 | 0 | putchar('\"'); |
142 | 0 | } |