]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/ipp-vars.c
Move debug printfs to internal usage only.
[thirdparty/cups.git] / cups / ipp-vars.c
CommitLineData
fd96ad89
MS
1/*
2 * IPP data file parsing functions.
3 *
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
9 */
10
11/*
12 * Include necessary headers...
13 */
14
15#include <cups/cups.h>
16#include "ipp-private.h"
17#include "string-private.h"
fb863569 18#include "debug-internal.h"
fd96ad89
MS
19
20
21/*
22 * '_ippVarsDeinit()' - Free all memory associated with the IPP variables.
23 */
24
25void
26_ippVarsDeinit(_ipp_vars_t *v) /* I - IPP variables */
27{
28 if (v->uri)
29 {
30 free(v->uri);
31 v->uri = NULL;
32 }
33
34 cupsFreeOptions(v->num_vars, v->vars);
35 v->num_vars = 0;
36 v->vars = NULL;
37}
38
39
40/*
41 * '_ippVarsExpand()' - Expand variables in the source string.
42 */
43
44void
45_ippVarsExpand(_ipp_vars_t *v, /* I - IPP variables */
46 char *dst, /* I - Destination buffer */
47 const char *src, /* I - Source string */
48 size_t dstsize) /* I - Destination buffer size */
49{
50 char *dstptr, /* Pointer into destination */
51 *dstend, /* End of destination */
52 temp[256], /* Temporary string */
53 *tempptr; /* Pointer into temporary string */
54 const char *value; /* Value to substitute */
55
56
57 dstptr = dst;
58 dstend = dst + dstsize - 1;
59
60 while (*src && dstptr < dstend)
61 {
62 if (*src == '$')
63 {
64 /*
65 * Substitute a string/number...
66 */
67
68 if (!strncmp(src, "$$", 2))
69 {
70 value = "$";
71 src += 2;
72 }
73 else if (!strncmp(src, "$ENV[", 5))
74 {
75 strlcpy(temp, src + 5, sizeof(temp));
76
77 for (tempptr = temp; *tempptr; tempptr ++)
78 if (*tempptr == ']')
79 break;
80
81 if (*tempptr)
82 *tempptr++ = '\0';
83
84 value = getenv(temp);
85 src += tempptr - temp + 5;
86 }
87 else
88 {
89 if (src[1] == '{')
90 {
91 src += 2;
92 strlcpy(temp, src, sizeof(temp));
93 if ((tempptr = strchr(temp, '}')) != NULL)
94 *tempptr = '\0';
95 else
96 tempptr = temp + strlen(temp);
97 }
98 else
99 {
100 strlcpy(temp, src + 1, sizeof(temp));
101
102 for (tempptr = temp; *tempptr; tempptr ++)
103 if (!isalnum(*tempptr & 255) && *tempptr != '-' && *tempptr != '_')
104 break;
105
106 if (*tempptr)
107 *tempptr = '\0';
108 }
109
110 value = _ippVarsGet(v, temp);
111
112 src += tempptr - temp + 1;
113 }
114
115 if (value)
116 {
117 strlcpy(dstptr, value, (size_t)(dstend - dstptr + 1));
118 dstptr += strlen(dstptr);
119 }
120 }
121 else
122 *dstptr++ = *src++;
123 }
124
125 *dstptr = '\0';
126}
127
128
129/*
130 * '_ippVarsGet()' - Get a variable string.
131 */
132
133const char * /* O - Value or @code NULL@ if not set */
134_ippVarsGet(_ipp_vars_t *v, /* I - IPP variables */
135 const char *name) /* I - Variable name */
136{
137 if (!strcmp(name, "uri"))
138 return (v->uri);
139 else if (!strcmp(name, "uriuser") || !strcmp(name, "username"))
140 return (v->username[0] ? v->username : NULL);
141 else if (!strcmp(name, "scheme") || !strcmp(name, "method"))
142 return (v->scheme);
143 else if (!strcmp(name, "hostname"))
144 return (v->host);
145 else if (!strcmp(name, "port"))
146 return (v->portstr);
147 else if (!strcmp(name, "resource"))
148 return (v->resource);
149 else if (!strcmp(name, "user"))
150 return (cupsUser());
151 else
152 return (cupsGetOption(name, v->num_vars, v->vars));
153}
154
155
156/*
157 * '_ippVarsInit()' - Initialize .
158 */
159
160void
a166e933
MS
161_ippVarsInit(_ipp_vars_t *v, /* I - IPP variables */
162 _ipp_fattr_cb_t attrcb, /* I - Attribute (filter) callback */
163 _ipp_ferror_cb_t errorcb, /* I - Error callback */
164 _ipp_ftoken_cb_t tokencb) /* I - Token callback */
fd96ad89
MS
165{
166 memset(v, 0, sizeof(_ipp_vars_t));
a166e933
MS
167
168 v->attrcb = attrcb;
169 v->errorcb = errorcb;
170 v->tokencb = tokencb;
fd96ad89
MS
171}
172
173
174/*
175 * '_ippVarsPasswordCB()' - Password callback using the IPP variables.
176 */
177
178const char * /* O - Password string or @code NULL@ */
179_ippVarsPasswordCB(
180 const char *prompt, /* I - Prompt string (not used) */
181 http_t *http, /* I - HTTP connection (not used) */
182 const char *method, /* I - HTTP method (not used) */
183 const char *resource, /* I - Resource path (not used) */
184 void *user_data) /* I - IPP variables */
185{
186 _ipp_vars_t *v = (_ipp_vars_t *)user_data;
187 /* I - IPP variables */
188
189
190 (void)prompt;
191 (void)http;
192 (void)method;
193 (void)resource;
194
195 if (v->username[0] && v->password && v->password_tries < 3)
196 {
197 v->password_tries ++;
198
199 cupsSetUser(v->username);
200
201 return (v->password);
202 }
203 else
204 {
205 return (NULL);
206 }
207}
208
209
210/*
211 * '_ippVarsSet()' - Set an IPP variable.
212 */
213
214int /* O - 1 on success, 0 on failure */
215_ippVarsSet(_ipp_vars_t *v, /* I - IPP variables */
216 const char *name, /* I - Variable name */
217 const char *value) /* I - Variable value */
218{
219 if (!strcmp(name, "uri"))
220 {
221 char uri[1024]; /* New printer URI */
222 http_uri_status_t uri_status; /* URI status */
223
224 if ((uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, value, v->scheme, sizeof(v->scheme), v->username, sizeof(v->username), v->host, sizeof(v->host), &(v->port), v->resource, sizeof(v->resource))) < HTTP_URI_STATUS_OK)
225 return (0);
226
227 if (v->username[0])
228 {
229 if ((v->password = strchr(v->username, ':')) != NULL)
230 *(v->password)++ = '\0';
231 }
232
233 snprintf(v->portstr, sizeof(v->portstr), "%d", v->port);
234
235 if (v->uri)
236 free(v->uri);
237
238 httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), v->scheme, NULL, v->host, v->port, v->resource);
239 v->uri = strdup(uri);
240
241 return (v->uri != NULL);
242 }
243 else
244 {
245 v->num_vars = cupsAddOption(name, value, v->num_vars, &v->vars);
246 return (1);
247 }
248}